From 24875af9d53e3f1a90fa5e6bd4aaef36bb0c3591 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sat, 27 Jun 2015 23:05:52 +0200 Subject: Check for new versions after start. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After LrMediaWiki is loaded, GitHub is checked for new versions if the user enabled that setting. To parse the response of the GitHub API, a third-party library is used for JSON parsing (JSON.lua). As the GitHub API query for the latest release only returns non-pre-release versions, all releases are queried and the first one is considered the latest. - create MediaWikiInit that queries the new version and shows a message (if necessary) - add MediaWikiInit to Info so that it is run after LrMediaWiki is loaded - add ‘Check version’ setting to the configuration - refactor MediaWikiApi and create performHttpRequest as a generic HTTP request method - add MediaWikiApi.getCurrentPluginVersion to query the GitHub API Fix #44. --- mediawiki.lrdevplugin/MediaWikiApi.lua | 64 +++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'mediawiki.lrdevplugin/MediaWikiApi.lua') diff --git a/mediawiki.lrdevplugin/MediaWikiApi.lua b/mediawiki.lrdevplugin/MediaWikiApi.lua index de432a7..e54e973 100755 --- a/mediawiki.lrdevplugin/MediaWikiApi.lua +++ b/mediawiki.lrdevplugin/MediaWikiApi.lua @@ -18,12 +18,14 @@ local LrHttp = import 'LrHttp' local LrPathUtils = import 'LrPathUtils' local LrXml = import 'LrXml' +local JSON = require 'JSON' local Info = require 'Info' local MediaWikiUtils = require 'MediaWikiUtils' local MediaWikiApi = { userAgent = string.format('LrMediaWiki %d.%d', Info.VERSION.major, Info.VERSION.minor), apiPath = nil, + githubApiVersion = 'https://api.github.com/repos/robinkrahl/LrMediaWiki/releases', } --- URL-encode a string according to RFC 3986. @@ -53,7 +55,7 @@ function MediaWikiApi.createRequestBody(arguments) end body = body .. MediaWikiApi.urlEncode(key) .. '=' .. MediaWikiApi.urlEncode(value) end - return body + return body or '' end function MediaWikiApi.parseXmlDom(xmlDomInstance) @@ -76,30 +78,27 @@ function MediaWikiApi.parseXmlDom(xmlDomInstance) return value end -function MediaWikiApi.performRequest(arguments) - arguments.format = 'xml' +function MediaWikiApi.performHttpRequest(path, arguments, requestHeaders, post) local requestBody = MediaWikiApi.createRequestBody(arguments) - local requestHeaders = { - { - field = 'User-Agent', - value = MediaWikiApi.userAgent, - }, - { - field = 'Content-Type', - value = 'application/x-www-form-urlencoded', - }, - } - MediaWikiUtils.trace('Performing API request'); + + MediaWikiUtils.trace('Performing HTTP request'); + MediaWikiUtils.trace('Path:') + MediaWikiUtils.trace(path) MediaWikiUtils.trace('Request body:'); MediaWikiUtils.trace(requestBody); - local resultBody, resultHeaders = LrHttp.post(MediaWikiApi.apiPath, requestBody, requestHeaders) + local resultBody, resultHeaders + if post then + resultBody, resultHeaders = LrHttp.post(path, requestBody, requestHeaders) + else + resultBody, resultHeaders = LrHttp.get(path .. '?' .. requestBody, requestHeaders) + end MediaWikiUtils.trace('Result status:'); MediaWikiUtils.trace(resultHeaders.status); if not resultHeaders.status then - LrErrors.throwUserError(LOC('$$$/LrMediaWiki/Api/NoConnection=Cannot connect to the MediaWiki API.')) + LrErrors.throwUserError(LOC('$$$/LrMediaWiki/Api/NoConnection=No network connection.')) elseif resultHeaders.status ~= 200 then LrErrors.throwUserError(LOC('$$$/LrMediaWiki/Api/HttpError=Received HTTP status ^1.', resultHeaders.status)) end @@ -107,6 +106,23 @@ function MediaWikiApi.performRequest(arguments) MediaWikiUtils.trace('Result body:'); MediaWikiUtils.trace(resultBody); + return resultBody +end + +function MediaWikiApi.performRequest(arguments) + arguments.format = 'xml' + local requestHeaders = { + { + field = 'User-Agent', + value = MediaWikiApi.userAgent, + }, + { + field = 'Content-Type', + value = 'application/x-www-form-urlencoded', + }, + } + + local resultBody = MediaWikiApi.performHttpRequest(MediaWikiApi.apiPath, arguments, requestHeaders, true) local resultXml = MediaWikiApi.parseXmlDom(LrXml.parseXml(resultBody)) if resultXml.error then LrErrors.throwUserError(LOC('$$$/LrMediaWiki/Api/MediaWikiError=The MediaWiki error ^1 occured: ^2', resultXml.error.code, resultXml.error.info)) @@ -114,6 +130,22 @@ function MediaWikiApi.performRequest(arguments) return resultXml end +function MediaWikiApi.getCurrentPluginVersion() + local requestHeaders = { + { + field = 'User-Agent', + value = MediaWikiApi.userAgent, + }, + } + local resultBody = MediaWikiApi.performHttpRequest(MediaWikiApi.githubApiVersion, {}, requestHeaders, false) + local resultJson = JSON:decode(resultBody) + local firstKey = MediaWikiUtils.getFirstKey(resultJson) + if firstKey ~= nil then + return resultJson[firstKey].tag_name + end + return nil +end + function MediaWikiApi.login(username, password, token) local arguments = { action = 'login', -- cgit v1.2.1