From d3ba117b246a5e6e21c93b94cd40ae460d2d3541 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sat, 27 Jun 2015 19:29:11 +0200 Subject: Add logging support and fix refactoring errors. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The user now may enable logging in MediaWikiUtils. If logging is enabled, a log file is written to the user’s "My Documents" that contains all API requests. Furthermore, bugs introduced by the refactoring in commit 8862341672dc288596d6ef66f92751bba6dfef25 have been fixed. - set up logging in MediaWikiUtils - write to the log in MediaWikiApi.performRequest - fix error in MediaWikiExportServiceProvider.processRenderedPhotos - fix error in MediaWikiApi.parseXmlDom Fix #43. --- mediawiki.lrdevplugin/MediaWikiApi.lua | 12 +++++++++++- mediawiki.lrdevplugin/MediaWikiExportServiceProvider.lua | 3 +-- mediawiki.lrdevplugin/MediaWikiUtils.lua | 13 +++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) (limited to 'mediawiki.lrdevplugin') diff --git a/mediawiki.lrdevplugin/MediaWikiApi.lua b/mediawiki.lrdevplugin/MediaWikiApi.lua index ea9c396..de432a7 100755 --- a/mediawiki.lrdevplugin/MediaWikiApi.lua +++ b/mediawiki.lrdevplugin/MediaWikiApi.lua @@ -19,6 +19,7 @@ local LrPathUtils = import 'LrPathUtils' local LrXml = import 'LrXml' local Info = require 'Info' +local MediaWikiUtils = require 'MediaWikiUtils' local MediaWikiApi = { userAgent = string.format('LrMediaWiki %d.%d', Info.VERSION.major, Info.VERSION.minor), @@ -64,7 +65,7 @@ function MediaWikiApi.parseXmlDom(xmlDomInstance) end for i = 1, xmlDomInstance:childCount() do local child = xmlDomInstance:childAtIndex(i) - local childName = child:name()[0] + local childName = child:name() if childName then value[childName] = MediaWikiApi.parseXmlDom(child) end @@ -88,15 +89,24 @@ function MediaWikiApi.performRequest(arguments) value = 'application/x-www-form-urlencoded', }, } + MediaWikiUtils.trace('Performing API request'); + MediaWikiUtils.trace('Request body:'); + MediaWikiUtils.trace(requestBody); local resultBody, resultHeaders = LrHttp.post(MediaWikiApi.apiPath, requestBody, requestHeaders) + 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.')) elseif resultHeaders.status ~= 200 then LrErrors.throwUserError(LOC('$$$/LrMediaWiki/Api/HttpError=Received HTTP status ^1.', resultHeaders.status)) end + MediaWikiUtils.trace('Result body:'); + MediaWikiUtils.trace(resultBody); + 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)) diff --git a/mediawiki.lrdevplugin/MediaWikiExportServiceProvider.lua b/mediawiki.lrdevplugin/MediaWikiExportServiceProvider.lua index 8405cea..accbf88 100755 --- a/mediawiki.lrdevplugin/MediaWikiExportServiceProvider.lua +++ b/mediawiki.lrdevplugin/MediaWikiExportServiceProvider.lua @@ -60,8 +60,7 @@ MediaWikiExportServiceProvider.processRenderedPhotos = function(functionContext, MediaWikiInterface.prepareUpload(exportSettings.username, exportSettings.password, exportSettings.api_path) -- iterate over photos - for item in exportContext:renditions() do - local rendition = item[1] + for i, rendition in exportContext:renditions() do -- render photo local success, pathOrMessage = rendition:waitForRender() if success then diff --git a/mediawiki.lrdevplugin/MediaWikiUtils.lua b/mediawiki.lrdevplugin/MediaWikiUtils.lua index 0eeb7a9..7d1dd8d 100644 --- a/mediawiki.lrdevplugin/MediaWikiUtils.lua +++ b/mediawiki.lrdevplugin/MediaWikiUtils.lua @@ -13,7 +13,16 @@ -- doc: missing -- i18n: complete +local LrLogger = import 'LrLogger' local Info = require 'Info' +local myLogger = LrLogger('LrMediaWikiLogger') + +-- LOGGING +-- If enabled, the log file will appear in your "My Documents" folder. Warning: +-- LrMediaWiki will log all requests sent to MediaWiki, including your password! +-- If you share a log file, make sure you removed your password. +-- To enable logging, uncomment the following line: +-- myLogger:enable("logfile") local MediaWikiUtils = {} @@ -36,4 +45,8 @@ MediaWikiUtils.getVersionString = function() return str end +MediaWikiUtils.trace = function(message) + myLogger:trace(message) +end + return MediaWikiUtils -- cgit v1.2.3