MediaWiki:Gadget-I18n-js.js: Difference between revisions
From Tardis Wiki, the free Doctor Who reference
MediaWiki:Gadget-I18n-js.js (view source)
Revision as of 12:32, 19 September 2018
, 19 September 2018cache loaded messages in localStorage
m (warn on json syntax error; move 'indexpageids' to end of request params to avoid need for redirect (due to '.json' page name extension appearing at end of url)) |
(cache loaded messages in localStorage) |
||
Line 4: | Line 4: | ||
* | * | ||
* @author Cqm <https://dev.wikia.com/User:Cqm> | * @author Cqm <https://dev.wikia.com/User:Cqm> | ||
* @version 0. | * @version 0.5.0 | ||
* | * | ||
* @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page) | * @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page) | ||
Line 36: | Line 36: | ||
*/ | */ | ||
var conf = mw.config.get([ | var conf = mw.config.get([ | ||
'debug', | |||
'wgContentLanguage', | 'wgContentLanguage', | ||
'wgNow', | |||
'wgUserLanguage' | 'wgUserLanguage' | ||
]), | ]), | ||
Line 641: | Line 643: | ||
mw.log('[I18n-js] Comment-stripped i18n.json:', json); | mw.log('[I18n-js] Comment-stripped i18n.json:', json); | ||
return json; | return json; | ||
} | |||
/* | |||
* Save messages string to local storage for caching. | |||
* | |||
* @param name | |||
* @param json The JSON object. | |||
* @param cacheVersion Cache version requested by the loading script. | |||
*/ | |||
function saveToCache(name, json, cacheVersion) { | |||
var keyPrefix = 'i18n-cache-' + name; | |||
// don't cache empty JSON | |||
if (Object.keys(json).length === 0) { | |||
return; | |||
} | |||
try { | |||
localStorage.setItem(keyPrefix + '-content', JSON.stringify(json)); | |||
localStorage.setItem(keyPrefix + '-timestamp', Number(conf.wgNow)); | |||
localStorage.setItem(keyPrefix + '-version', cacheVersion || 0); | |||
} catch (e) {} | |||
} | } | ||
Line 648: | Line 672: | ||
* @param name | * @param name | ||
* @param res The JSON string. | * @param res The JSON string. | ||
* @param cacheVersion Cache version requested by the loading script. | |||
* | * | ||
* @return The resulting i18n object. | * @return The resulting i18n object. | ||
*/ | */ | ||
function parseMessagesToObject(name, res) { | function parseMessagesToObject(name, res, cacheVersion) { | ||
var json = {}, | var json = {}, | ||
obj, | obj, | ||
Line 674: | Line 699: | ||
// cache the result in case it's used multiple times | // cache the result in case it's used multiple times | ||
cache[name] = obj; | cache[name] = obj; | ||
if (typeof cacheVersion === 'number') { | |||
saveToCache(name, json, cacheVersion); | |||
} | |||
return obj; | return obj; | ||
} | |||
/* | |||
* Load messages string from local storage cache and add to cache object. | |||
* | |||
* @param name | |||
* @param minCacheVersion Minimum cache version requested by the loading script. | |||
*/ | |||
function loadFromCache(name, minCacheVersion) { | |||
var keyPrefix = 'i18n-cache-' + name, | |||
twoDays = 1000 * 60 * 60 * 24 * 2, | |||
cacheTimestamp, | |||
cacheVersion; | |||
try { | |||
cacheTimestamp = Number(localStorage.getItem(keyPrefix + '-timestamp')); | |||
cacheVersion = Number(localStorage.getItem(keyPrefix + '-version')); | |||
// only use cached messages if cache is less than two days old | |||
// and if cache version is greater than or equal to requested version | |||
if ( | |||
conf.wgNow - cacheTimestamp < twoDays && | |||
cacheVersion >= minCacheVersion | |||
) { | |||
parseMessagesToObject(name, localStorage.getItem(keyPrefix + '-content')); | |||
} | |||
} catch (e) {} | |||
} | } | ||
Line 683: | Line 740: | ||
* used to get messages from | * used to get messages from | ||
* https://dev.wikia.com/wiki/MediaWiki:Custom-name/i18n.json. | * https://dev.wikia.com/wiki/MediaWiki:Custom-name/i18n.json. | ||
* @param options Options that can be set by the loading script: | |||
* cacheVersion: Minimum cache version requested by the loading script. | |||
* noCache: Never load i18n from cache (not recommended for general use). | |||
* | * | ||
* @return A jQuery.Deferred instance. | * @return A jQuery.Deferred instance. | ||
*/ | */ | ||
function loadMessages(name) { | function loadMessages(name, options) { | ||
options = options || {}; | |||
var deferred = $.Deferred(), | var deferred = $.Deferred(), | ||
cacheVersion = Number(options.cacheVersion) || 0, | |||
customSource = name.match(/^u:([a-z0-9-]+):/), | customSource = name.match(/^u:([a-z0-9-]+):/), | ||
useCache = (options.noCache || conf.debug) !== true, | |||
apiEndpoint, | apiEndpoint, | ||
page, | page, | ||
params; | params; | ||
if (cache[name]) { | if (useCache) { | ||
loadFromCache(name, cacheVersion); | |||
} | |||
if (cache[name] && useCache) { | |||
deferred.resolve(cache[name]); | deferred.resolve(cache[name]); | ||
} else { | } else { | ||
Line 731: | Line 799: | ||
} | } | ||
deferred.resolve(parseMessagesToObject(name, res)); | deferred.resolve(parseMessagesToObject(name, res, cacheVersion)); | ||
}); | }); | ||
}); | }); | ||
Line 746: | Line 814: | ||
// 'hidden' functions to allow testing | // 'hidden' functions to allow testing | ||
_stripComments: stripComments, | _stripComments: stripComments, | ||
_saveToCache: saveToCache, | |||
_getMsg: getMsg, | _getMsg: getMsg, | ||
_handleArgs: handleArgs, | _handleArgs: handleArgs, |