User:Nihiltres/sandbox.js

nothingthree.util.generateRevItem = function (gri_title, gri_revid, gri_parentid, gri_user, gri_timestamp, gri_comment) { //Turns a bunch of variables (all strings or stringable) into, more or less, a history li element (as a *string*). Even adds the "block" element iff you're an admin! //ugly, but should function. Needs addition to handle whether user is anon or not (input rev JSON, use (therevobject.anon !== undefined) to test for it) var curlink, prevlink, abslink, timestring, parsedcomment, parsematch, sectionlink, gri_output; curlink = mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + mw.util.wikiUrlencode(gri_title) + "&diff=cur" + "&oldid=" + gri_parentid; prevlink = mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + mw.util.wikiUrlencode(gri_title) + "&diff=" + gri_revid + "&oldid=" + gri_parentid; abslink = mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + mw.util.wikiUrlencode(gri_title) + "&oldid=" + gri_revid; timestring = nothingthree.util.stringifyDate(nothingthree.util.parseDate(gri_timestamp), "dmy", "(UTC)"); //I'll make it respect user prefs with api.php?action=query&meta=userinfo&uiprop=options some other time; for now it uses my preferred "dmy". :P if (gri_comment) { parsedcomment = mw.html.escape(gri_comment); //escapes HTML to prevent injection attacks like this test that affected a previous version http://en.wikipedia.org/w/index.php?diff=423287916 while (parsedcomment.match(/\[\[([^\[\]\|]*?)?&amp;([^\[\]]*?)?\]\]/)) { //replaces &amp; within  (but only before a pipe "|" if there's one) with literal ampersands so as to not break links that are about to be parsed parsematch = parsedcomment.match(/\[\[([^\[\]\|]*?)?&amp;([^\[\]]*?)?\]\]/); parsedcomment = parsedcomment.replace(parsematch[0],  + parsematch[1] + '&' + parsematch[2] + ); }               while (parsedcomment.match(/\/\*\s?(.*?)\s?\*\//)) { //replaces cases like /* */ parsematch = parsedcomment.match(/\/\*\s?(.*?)\s?\*\//); while (parsematch[1].match(/\[\\[\]\{\}<>]+(\|[^\|\[\]\{\}]+)?\]\]/)) {                       parsematch[1] = parsematch[1].replace(/\[\[([^\|\[\]\{\}<>]+)\]\]/, "$1"); //ignore wikilinks like [[a for section link generation                        parsematch[1] = parsematch[1].replace(/\[\\[\]\{\}<>]+\|([^|\[\]\{\}]+)\]\]/, "$1");  //ignore wikilinks like [[a|b for section link generation                    }                    sectionlink = mw.util.wikiUrlencode(parsematch[1]); //do the basic URL encoding…                    while (sectionlink.match("%")) { // and then replace "%" with "."                        sectionlink = sectionlink.replace("%", ".");                    }                    parsedcomment = parsedcomment.replace(parsematch[0], ' &rarr;' + parsematch[1] + ' ');                }                while (parsedcomment.match(/\[\[([^|\[\]\{\}<>]+)\]\]/m)) { //replaces cases like a with actual links                    parsematch = parsedcomment.match(/\[\[([^|\[\]\{\}<>]+)\]\]/m);                    parsedcomment = parsedcomment.replace(parsematch[0], '' + parsematch[1] + '');                }                while (parsedcomment.match(/\[\[([^|\[\]\{\}<>]+)\|([^|\[\]\{\}]+)\]\]/m)) { //replaces cases like a|b with actual links                    parsematch = parsedcomment.match(/\[\[([^|\[\]\{\}<>]+)\|([^|\[\]\{\}]+)\]\]/m);                    parsedcomment = parsedcomment.replace(parsematch[0], '' + parsematch[2] + '');                }            }            gri_output = ' (cur | prev) ' + timestring + ' ' + gri_user + ' (talk</a> | <a href="' + mw.util.wikiUrlencode("Special:Contributions/" + gri_user).replace(/(.*)/, wgArticlePath) + '" title="Special:Contributions/' + gri_user + '">contribs</a>' + (wgUserGroups.indexOf("sysop") !== -1 ? ' | <a href="' + mw.util.wikiUrlencode("Special:Block/" + gri_user).replace(/(.*)/, wgArticlePath) + '" title="Special:Block/' + gri_user + '">block</a>' :  "") + ')  ' + (gri_comment ? ' (' + parsedcomment + ') ' : '') + '</li>';            return gri_output;        } //end generateRevItem

nothingthree.pageRC = { initDialog: function { nothingthree.pageRC.dialog = jQuery(' ').html(' ').dialog({               width: 600,                autoOpen: false,                title: 'Recent changes for this page',                modal: false,                buttons: {                    "Close": function  {                        jQuery(nothingthree.pageRC.dialog).dialog("close");                    }, //end close                    "Refresh": function  {                        nothingthree.pageRC.refresh;                    } //end refresh                } //end buttons            }); //end dialog function }, //end initDialog

loadingScreen: function { jQuery("#pageRC div#pageRCcontents").html('Loading&hellip; <img src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />'); }, //end loadingScreen

refresh: function { var revs, revnum, revblob; nothingthree.pageRC.loadingScreen; jQuery.ajax({               url: wgScriptPath + '/api.php?',                data: 'action=query&prop=revisions&rvlimit=10&format=json&pageids=' + wgArticleId,                dataType: 'json',                success: function (data) {                    revs = data.query.pages[wgArticleId].revisions;                    revblob = document.createElement("ul");                    revblob.className = "pagehistory";                    revblob.innerHTML = "";                    for (revnum = 0; revnum < revs.length; revnum++) {                        revblob.innerHTML += nothingthree.util.generateRevItem(wgTitle, revs[revnum].revid, revs[revnum].parentid, revs[revnum].user, revs[revnum].timestamp, revs[revnum].comment);                    } //end for                    jQuery("#pageRC div#pageRCcontents").html(revblob);                    nothingthree.settings.pageRCloaded = true;                } //end success }); //end jQuery.ajax       }, //end refresh

activate: function { jQuery(nothingthree.pageRC.dialog).dialog("open"); if (nothingthree.settings.pageRCloaded === false) { nothingthree.pageRC.refresh; } //end if       }, //end activate

activationTab: function { //Adds a tab to the menu that activates the recent edits dialog mw.loader.using('jquery.ui', function  {                 if (wgCanonicalNamespace !== 'Special' && wgAction !== "history" && jQuery("#ca-pagerc").length === 0) {                    nothingthree.pageRC.initDialog;                    mw.util.addPortletLink('p-cactions', 'javascript:nothingthree.pageRC.activate;', 'View recent revisions', 'ca-pagerc', 'View the latest 10 revisions', null, 'ca-sidebar');                    jQuery("#ca-pagerc").addClass("collapsible");                    jQuery("head").append("<style type=\"text/css\" charset=\"utf-8\">.ui-widget-content#pageRC a {color: #0645AD;}<\/style>"); //overrides a rule that make the links grey-black instead of blue                } //end if            }); //end mw.loader.using } //end activationTab } //end pageRC