User:Hippietrail/wfMsgCentre.js
Appearance
(Redirected from User:Hippietrail/apitest.js)
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
//importScript('User:Hippietrail/hippajax.js');
document.write('<script type="text/javascript" src="/w/index.php?title=User:Hippietrail/hippajax.js&action=raw&ctype=text/javascript"><\/script>');
// uppercase first char, leave rest as is
function ucfirst(t) {
return t.charAt(0).toUpperCase() + t.substring(1);
}
// convert a full mediawiki page title into an href
function makehref(arg) {
return arg.match(/^http:/) ? arg : wgArticlePath.replace('$1', arg);
}
// factory
function wfMsgCentreFactory() {
}
wfMsgCentreFactory.create = function() {
if (true) {
if (true) {
// fast, supports message cache; needs newer api.php with amlang
return new wfMsgCentre_allmsgs();
} else {
// fast but doesn't support message cache; works on older api.php
return new wfMsgCentre_proprev();
}
} else {
// not as fast but supports message cache and doesn't need api.php
return new wfMsgCentre_actraw();
}
}
// overall base class
function wfMsgCentre() {
}
// non-api-derived class
wfMsgCentre_actraw.prototype = new wfMsgCentre();
wfMsgCentre_actraw.constructor = wfMsgCentre_actraw;
function wfMsgCentre_actraw() {
if (window.console) console.log('action raw\n');
wfMsgCentre.call(this);
}
wfMsgCentre_actraw.prototype.batch = function(msgarray) {
function getmsg(title, forcontent) {
function on200(req) {
var v = req.responseText;
var x = msgarray[title];
if (x.xform != undefined)
v = x.xform(v);
x.obj[x.attr] = v;
};
var urls = [
wgScriptPath + '/index.php?usemsgcache=yes&action=raw&title=MediaWiki:' + ucfirst(title) + '/' + wgUserLanguage,
wgScriptPath + '/index.php?usemsgcache=yes&action=raw&title=MediaWiki:' + ucfirst(title)
];
var index = 0;
if (!forcontent || wgUserLanguage == wgContentLanguage)
index = 1;
ajax(urls[index], on200, on400);
function on400() {
index++;
if (index < urls.length)
ajax(urls[index], on200, on400);
};
};
for (var m in msgarray) {
getmsg(m, msgarray[m].forcontent);
}
}
// api base class
wfMsgCentre_api.prototype = new wfMsgCentre();
wfMsgCentre_api.constructor = wfMsgCentre_api;
function wfMsgCentre_api() {
wfMsgCentre.call(this);
}
wfMsgCentre_api.prototype.batch = function(msgarray) {
var that = this;
function on200(req) {
var rt = eval('(' + req.responseText + ')');
that.handleresponse(msgarray, rt);
};
var url = this.buildurl(msgarray);
if (url != null) {
ajax(url, on200, function(){});
} else {
if (console != undefined) console.log('no titles\n');
}
}
// api-derived classes
// class which uses api.php to get the raw content of several mediawiki: namespace pages at once
// there is no way to access messages in the message cache but not in the mediawiki: namespace
wfMsgCentre_proprev.prototype = new wfMsgCentre_api();
wfMsgCentre_proprev.constructor = wfMsgCentre_proprev;
function wfMsgCentre_proprev() {
if (window.console) console.log('prop revisions\n');
wfMsgCentre_api.call(this);
this.buildurl = function(msgarray) {
var titles = '';
for (var i in msgarray) {
if (titles) titles += '|';
titles += 'MediaWiki:' + i;
if (msgarray[i] && wgUserLanguage != wgContentLanguage) {
if (titles) titles += '|';
titles += 'MediaWiki:' + i + '/' + wgUserLanguage;
}
}
if (titles)
return wgScriptPath + '/api.php?format=json&action=query&prop=revisions&rvprop=content&titles=' + titles;
else
return null;
}
}
wfMsgCentre_proprev.prototype.handleresponse = function(msgarray, response) {
// TODO handle error result
var pages = response.query.pages;
for (var p in pages) {
if (pages[p].missing == undefined) {
var t = pages[p].title;
var v = pages[p].revisions[0]['*'];
// strip any prefix and suffix
t = t.substring(10);
var lio = t.lastIndexOf('/' + wgUserLanguage);
if (lio != -1)
t = t.substring(0,lio);
var x = msgarray[t];
// TODO don't set nonlocalized text over localized
if (x.xform != undefined)
v = x.xform(v);
x.obj[x.attr] = v;
}
}
}
// class which uses api.php to call wfGetMsg for several messages at once
// there is no way to access messages in the message cache but not in the mediawiki: namespace
wfMsgCentre_allmsgs.prototype = new wfMsgCentre_api();
wfMsgCentre_allmsgs.constructor = wfMsgCentre_allmsgs;
function wfMsgCentre_allmsgs() {
if (window.console) console.log('all messages\n');
wfMsgCentre_api.call(this);
this.buildurl = function(msgarray) {
var titles = '';
for (var i in msgarray) {
if (titles) titles += '|';
titles += i;
}
if (titles)
return wgScriptPath + '/api.php?format=json&action=query&meta=allmessages&ammessages=' + titles + '&amlang=' + wgUserLanguage;
else
return null;
}
}
wfMsgCentre_allmsgs.prototype.handleresponse = function(msgarray, response) {
// TODO handle error result
var qam = response.query.allmessages;
for (var i in qam) {
var t = qam[i].name;
var v = qam[i]['*'];
if (v != '<' + t + '>') {
var x = msgarray[t];
if (x.xform != undefined)
v = x.xform(v);
x.obj[x.attr] = v;
}
}
}