Jump to content

User:Shirulashem/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Shirulashem (talk | contribs) at 01:13, 3 November 2008 (assessment script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
importScript('User:AzaToth/twinkle.js');

importScript('User:Lupin/recent2.js');

importScript('User:Mr.Z-man/refToolbar.js');

importScript('User:Dr pda/persondata.js'); //[[User:Dr pda/persondata.js]]

// | WARNING: GLOBAL GADGET FILE
// | Changes to this page affect many users.
// | Please discuss changes on the talk page or on [[Wikipedia talk:Gadget]]
// | before editing.
// |
// | Imported from [[User:Pyrospirit/metadata.js]],
// | which itself is a modified version of [[User:Outriggr/metadata.js]].
// | Import begins below this line.
/**
 * Metadata assessment script
 * Finds the WP 1.0/WikiProject assessment of every article you go to, then 
 * displays that information in the article header.
 *
 * @author Outriggr - created the script and used to maintain it
 * @author Pyrospirit - currently maintains and updates the script
 */
 
// Import stylesheet with custom classes for header colors
importStylesheet('User:Pyrospirit/metadata.css');
 
/**
 * This is the constructor for the script object. All functions this script 
 * defines are inside this.
 *
 * @constructor
 */
MetadataScript = function () {};
 
/**
 * Starts the script object running. The main function of the script. If the 
 * getMainType() function can find the assessment, it uses that assessment 
 * for the page, parses it, and displays it in the header. Otherwise, it runs 
 * ajaxMain().
 */
MetadataScript.prototype.init = function () {
    this.initBefore();
    var initialAssessment = this.checkArticle(); // checks for types visible from article page
    if ( initialAssessment.exists ) {
        this.currentAssessment = initialAssessment;
        var data = this.talkAssess(this.currentAssessment);
        this.update(data.newClass, data.slogan, data.info);
    }
    else this.ajaxMain(); // proceed to check the talk page
    this.initAfter();
};
MetadataScript.prototype.initBefore = function () {};
MetadataScript.prototype.initAfter = function () {};
 
/**
 * The main function when an AJAX request is needed to find the assessment. 
 * Creates an AJAX request for the contents of a URL (defaults to the 
 * first section of the article's talk page), then sends the request. After 
 * getting the requested data back, it finds the assessment information in 
 * the data, then uses and displays that assessment in the header.
 *
 * @param {String} arguments[1] - Optional: override the default URL for the 
 *        request.
 */
MetadataScript.prototype.ajaxMain = function () {
    if ( arguments[1] && arguments[1].match(/^https?:\/\//i) ) // optional url override
        this.url = arguments[1];
    else this.url = wgServer + wgScript + '?title=Talk:' + encodeURIComponent(wgPageName)
        + '&action=raw&section=0';
    this.request = sajax_init_object();
    if ( this.request ) {
        var self = this; // store value of 'this'
        this.request.onreadystatechange = function () {
            self.stateChangeFunction.call(self);
        }
        this.request.open('GET', this.url, true);
        this.request.send(null);
    }
};
 
/**
 * This function is passed as a parameter to ajaxMain. It is called each time 
 * this.request updates, and the code inside the conditional runs when the 
 * data is available.
 */
MetadataScript.prototype.stateChangeFunction = function () {
    if ( this.request.readyState == 4 && this.request.status == 200 ) {
        this.text = this.request.responseText;
        var rating = this.getRating(this.text);
        this.currentAssessment = this.getAssessment(this.text, rating);
        var data = this.talkAssess(this.currentAssessment);
        this.update(data.newClass, data.slogan, data.info);
    }
};
 
/**
 * Checks for various objects on the article page that indicate a certain 
 * assessment, such as a featured star or disambiguation page notice. If this 
 * function can find the assessment, AJAX is not needed for this page.
 *
 * @return {Object} assess - the assessment in an easily readable format
 * @static
 */
MetadataScript.prototype.checkArticle = function () {
    var assess = {};
    assess.extra == '';
    assess.exists = true;
    if ( document.getElementById('disambig') )
        assess.rating = 'dab';
    else if ( document.getElementById('contentSub').innerHTML == 'Redirect page' )
        assess.rating = 'redir';
    else if ( document.getElementById('ca-talk').className == 'new' ) // no talk page
        assess.rating = 'none';
    else assess.exists = false; // none of the above, no assessment found
    return assess;
};
 
/**
 * Searches the provided wikicode for the rating part of an assessment and 
 * returns it as a string.
 *
 * Note that a higher assessment takes priority, and less-used assessments 
 * such as "list", "current", or "future" are used only if nothing else can 
 * be found.
 *
 * @param {String} text - some wikitext to be searched for assessment info
 * @return {String} rating - the article's current assessment
 */
MetadataScript.prototype.getRating = function (text) {
    this.getRatingBefore();
    if ( text.match(/\b(class|currentstatus) *= *fa\b/i) ) rating = 'fa';
    else if ( text.match(/\b(class|currentstatus) *= *fl\b/i) ) rating = 'fl';
    else if ( text.match(/\bclass *= *a\b/i) ) {
        if ( text.match(/\bclass *= *ga\b|\bcurrentstatus *= *(ffa\/)?ga\b/i) )
            rating = 'a/ga'; // A-class articles that are also GA's
        else rating = 'a';
    } else if ( text.match(/\bclass *= *ga\b|\bcurrentstatus *= *(ffa\/)?ga\b|\{\{ *ga *\|/i)
               && !text.match(/\bcurrentstatus *= *dga\b/i) ) rating = 'ga';
    else if ( text.match(/\bclass *= *b\b/i) ) rating = 'b';
    else if ( text.match(/\bclass *= *bplus\b/i) ) rating = 'bplus'; // used by WP Math
    else if ( text.match(/\bclass *= *c\b/i) ) rating = 'c';
    else if ( text.match(/\bclass *= *start/i) ) rating = 'start';
    else if ( text.match(/\bclass *= *stub/i) ) rating = 'stub';
    else if ( text.match(/\bclass *= *list/i) ) rating = 'list';
    else if ( text.match(/\bclass *= *(dab|disambig)/i) ) rating = 'dab';
    else if ( text.match(/\bclass *= *cur(rent)?/i) ) rating = 'cur';
    else if ( text.match(/\bclass *= *future/i) ) rating = 'future';
    else rating = 'none';
    this.getRatingAfter();
    return rating;
}
MetadataScript.prototype.getRatingBefore = function () {};
MetadataScript.prototype.getRatingAfter = function () {};
 
/**
 * Searches the provided wikicode for data on the article's current and past 
 * featured or good status and returns an object that contains this data 
 * along with some miscellaneous other bits of information.
 *
 * @param {String} text - some wikitext to be searched for assessment info
 * @return {Object} assess - the assessment data for the page
 */
MetadataScript.prototype.getAssessment = function (text, rating) {
    this.getAssessmentBefore();
    var assess = {};
    var actionNumber = 0; // action number in ArticleHistory template
    var reviewMatch; // temporarily stores match objects
    assess.rating = rating;
    assess.pageLink = null;
 
    // Current nominations (FAC, FLC, or GAN)
    if ( (assess.reg = text.match(/\{\{ *fac *[\|\}]/i)) ) {
        assess.extra = 'fac';
    } else if ( (assess.reg = text.match(/\{\{ *flc *\}\}/i)) ) {
        assess.extra = 'flc';
    } else if ( (assess.reg = text.match(/\{\{ *ga ?nominee *[\|\}]/i)) ) {
        assess.extra = 'gan';
    }
    // Current reviews of a status (FAR, FLRC, or GAR)
    else if ( (assess.reg = text.match(/\{\{ *far(ce?)? *[\|\}]/i)) ) {
        assess.extra = 'far';
    } else if ( (assess.reg = text.match(/\{\{ *flrc *[\|\}]/i)) ) {
        assess.extra = 'flrc';
    } else if ( (assess.reg = text.match(/\{\{ *gar\/link *[\|\}]/i)) ) {
        assess.extra = 'gar';
    }
    // Former statuses (FFA, FFL, or DGA)
    else if ( (assess.reg = text.match(/\bcurrentstatus *= *ffa\b/i)) ) {
        reviewMatch = text.match(/\baction(\d+) *= *far\b/gi);
        actionNumber = reviewMatch[reviewMatch.length - 1].match(/\d+/);
        assess.pageLink = true;
        assess.extra = 'ffa';
    } else if ( (assess.reg = text.match(/\{\{ *formerfa2?\b/i)) ) {
        assess.extra = 'ffa';
    } else if ( (assess.reg = text.match(/\bcurrentstatus *= *ffl\b/i)) ) {
        assess.extra = 'ffl';
    } else if ( (assess.reg = text.match(/\{\{ *ffl *[\|\}]/i)) ) {
        assess.extra = 'ffl';
    } else if ( (assess.reg = text.match(/\bcurrentstatus *= *dga\b/i)) ) {
        reviewMatch = text.match(/\baction(\d+) *= *gar\b/gi);
        actionNumber = reviewMatch[reviewMatch.length - 1].match(/\d+/);
        assess.pageLink = true;
        assess.extra = 'dga';
    } else if ( (assess.reg = text.match(/\{\{ *d(elisted)?ga *[\|\}]/i)) ) {
        assess.extra = 'dga';
    }
    // Former nominations (former FAC, FLC, or GAN)
    else if ( (assess.reg = text.match(/\baction(\d+) *= *fac\b/gi))
            && !assess.rating.match(/f[al]/i) ) {
        actionNumber = assess.reg[assess.reg.length - 1].match(/\d+/);
        assess.pageLink = true;
        assess.extra = 'ffac';
    } else if ( (assess.reg = text.match(/\{\{ *fac?(failed|(\-| \()?contested\)?) *[\|\}]/i)) ) {
        assess.extra = 'ffac';
    } else if ( (assess.reg = text.match(/\baction(\d+) *= *flc\b/gi))
            && !assess.rating.match(/f[al]/i) ) {
        actionNumber = assess.reg[assess.reg.length - 1].match(/\d+/);
        assess.pageLink = true;
        assess.extra = 'fflc';
    } else if ( (assess.reg = text.match(/\baction(\d+) *= *gan\b/gi))
            && !assess.rating.match(/f[al]|(a\/)?ga/i) ) {
        actionNumber = assess.reg[assess.reg.length - 1].match(/\d+/);
        assess.pageLink = true;
        assess.extra = 'fgan';
    } else if ( (assess.reg = text.match(/\{\{ *f(ailed ?)?ga *[\|\}]/i)) ) {
        assess.extra = 'fgan';
    } else assess.extra = 'none';
 
    // Looks for currently active peer reviews
    var peerReview;
    if ( (peerReview = text.match(/\{\{ *peer[_ ]?review *\| *archive *= *(\d+)\b/i)) ) {
        assess.review = 'Wikipedia:Peer_review/' + wgPageName + '/archive'
            + peerReview[1];
    } else assess.review = null;
 
    // Scans for the link associated with an action in ArticleHistory
    if ( assess.pageLink ) {
        var linkPattern = RegExp('\\baction' + actionNumber + 'link *= *([^\\n\\|]+)\\s*\\|');
        var linkMatch = text.match(linkPattern);
        assess.pageLink = linkMatch ? linkMatch[1] : null;
    }
 
    assess.exists = true;
    this.getAssessmentAfter();
    return assess;
}
MetadataScript.prototype.getAssessmentBefore = function () {};
MetadataScript.prototype.getAssessmentAfter = function () {};
 
/**
 * Parses an assessment object into the HTML and CSS code needed to update 
 * the article header. If it doesn't recognize a part of the information 
 * given, it will simply ignore it and mark as unassessed.
 *
 * @param {Object} assess - assessment information for this article
 * @return {String} newClass - the CSS class corresponding to its assessment
 * @return {String} slogan - HTML giving (with a link) the main assessment
 * @return {String} info - HTML giving (with a link) additional information
 */
MetadataScript.prototype.talkAssess = function (assess) {
    this.talkAssessBefore();
 
    var path = wgArticlePath.replace('$1', '');
    var assessLink = path + 'Wikipedia:Version_1.0_Editorial_Team/Assessment';
    if ( typeof assess.extra === 'undefined' ) assess.extra = '';
    var extra = assess.extra.toLowerCase();
    var rating = assess.rating.toLowerCase();
    var pageLink = this.encodePageName(assess.pageLink);
    var peerReview = this.encodePageName(assess.review);
 
    var info = this.getExtraInfo(extra, pageLink);
    info = this.addPeerReview(info, peerReview);
 
    if ( rating == 'a' || rating == 'a/ga' ) {
        newClass = 'assess-A-text';
        slogan = 'An <a href="' + assessLink + '">A-class</a> article';
        if ( rating == 'a/ga' ) {
            if ( info.length == 0 ) info += '.';
            info += ' Also a <a href="' + path + 'Wikipedia:Good_Articles">good article</a>.'
        }
    } else if ( rating == 'ga' ) {
        newClass = 'assess-GA-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Good_Articles">good article</a>'
    } else if ( rating == 'b' ) {
        newClass = 'assess-B-text';
        slogan = 'A <a href="' + assessLink + '">B-class</a> article';
    } else if ( rating == 'bplus' ) {
        newClass = 'assess-Bplus-text';
        slogan = 'A <a href="' + path + 'Wikipedia:WikiProject_Mathematics/Wikipedia_1.0'
            + '/Grading_scheme">B-plus-class</a> article';
    } else if ( rating == 'c' ) {
        newClass = 'assess-C-text';
        slogan = 'A <a href="' + assessLink + '">C-class</a> article';
    } else if ( rating == 'start' ) {
        newClass = 'assess-Start-text';
        slogan = 'A <a href="' + assessLink + '">start-class</a> article';
    } else if ( rating == 'stub' ) {
        newClass = 'assess-Stub-text';
        slogan = 'A <a href="' + assessLink + '">stub-class</a> article';
    } else if ( rating == 'list' ) {
        newClass = 'assess-List-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Lists">list-class</a> article';
    } else if ( rating == 'dab' ) {
        newClass = 'assess-Dab-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Disambiguation">disambiguation page</a>';
    } else if ( rating == 'redir' ) {
        newClass = 'assess-Redir-text';
        slogan = 'A <a href="' + path + 'Help:Redirect">redirect page</a>';
    } else if ( rating == 'fl' ) {
        newClass = 'assess-FL-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Featured_lists">featured list</a>';
    } else if ( rating == 'fa' ) {
        newClass = 'assess-FA-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Featured_articles">featured article</a>';
    } else if ( rating == 'cur' ) {
        newClass = 'assess-Cur-text';
        slogan = 'A <a href="' + path + 'Portal:Current_events">current-class</a> article';
    } else if ( rating == 'future' ) {
        newClass = 'assess-Future-text';
        slogan = 'A <a href="' + path + 'Category:Future-Class_articles">future-class</a>'
            + ' article';
    } else {
        newClass = '';
        slogan = 'An <a href="' + assessLink + '">unassessed</a> article';
    }
 
    // Add CSS classes to allow for customization
    slogan = '<span class="assess-article-rating">' + slogan + '</span>';
    info = '<span class="assess-info-all">' + info + '</span>';
 
    this.talkAssessAfter();
    return {newClass: newClass, slogan: slogan, info: info};
};
MetadataScript.prototype.talkAssessBefore = function () {};
MetadataScript.prototype.talkAssessAfter = function () {};
 
/**
 * Creates an info string based on the assessment info and a page link.
 */
MetadataScript.prototype.getExtraInfo = function (extra, pageLink) {
    var info = '';
    var page = this.encodePageName(wgPageName);
    if ( extra == 'fac' ) {
        info = this.makeInfoString('Currently a', pageLink, 'Wikipedia:Featured_article_candidates/'
            + page, 'featured article candidate', null);
    } else if ( extra == 'flc' ) {
        info = this.makeInfoString('Currently a', pageLink, 'Wikipedia:Featured_list_candidates/'
            + page, 'featured list candidate', null);
    } else if ( extra == 'gan' ) {
        info = this.makeInfoString('Currently a', pageLink, 'Wikipedia:Good_article_nominations',
            'good article nominee', null);
    } else if ( extra == 'far' ) {
        info = this.makeInfoString('Currently undergoing', pageLink, 'Wikipedia:Featured_article_review/'
            + page, 'review', 'of its featured status');
    } else if ( extra == 'flrc' ) {
        info = this.makeInfoString('Currently a', pageLink, 'Wikipedia:Featured_list_removal_candidates/'
            + page, 'candidate', 'for removal as a featured list');
    } else if ( extra == 'gar' ) {
        info = this.makeInfoString('Currently undergoing a', pageLink, 'Wikipedia:Good_article_reassessment',
            'reassessment', 'of its status as a good article');
    } else if ( extra == 'ffa' ) {
        info = this.makeInfoString('A', pageLink, 'Wikipedia:Featured_article_review/' + page,
            'former', 'featured article');
    } else if ( extra == 'ffl' ) {
        info = this.makeInfoString('A', pageLink, 'Wikipedia:Featured_list_removal_candidates/'
            + page, 'former', 'featured list');
    } else if ( extra == 'dga' ) {
        info = this.makeInfoString('A', pageLink, 'Wikipedia:Good_article_reassessment',
            'delisted', 'good article');
    } else if ( extra == 'ffac' ) {
        info = this.makeInfoString('A former', pageLink, 'Wikipedia:Featured_article_candidates/'
            + page, 'featured article candidate', null);
    } else if ( extra == 'fflc' ) {
        info = this.makeInfoString('A former', pageLink, 'Wikipedia:Featured_list_candidates/'
            + page, 'featured list candidate', null);
    } else if ( extra == 'fgan' ) {
        info = this.makeInfoString('A former', pageLink, 'Wikipedia:Good_article_nominations',
            'good article nominee', null);
    }
    return info;
};
 
/**
 * Adds the peer review text to an info string, if a peer review was detected earlier.
 */
MetadataScript.prototype.addPeerReview = function (info, peerReview) {
    var path = wgArticlePath.replace('$1', '');
    if ( peerReview ) {
        if ( info.length == 0 ) info += '.'; // the period is omitted if there's no extra info
        info += '<span class="assess-info-review"> Currently being <a href="' + path
            + peerReview + '">peer reviewed</a>.</span>';
    }
    return info;
};
 
/**
 * Updates article header with new assessment information by giving it a new 
 * class (for style information such as color) and altering the tagline below 
 * it to state the assessment found.
 *
 * @param {String} newClass - the CSS class name added to the article header
 * @param {String} slogan - italicized text prepended to the tagline, showing 
 *        the article's main assessment
 * @param {String} info - additional assessment info appended to the tagline
 * @static
 */
MetadataScript.prototype.update = function (newClass, slogan, info) {
    var firstHeading = document.getElementsByTagName('h1')[0];
    firstHeading.className += ' ' + newClass; // add newClass as an additional class
    document.getElementById('siteSub').innerHTML = slogan
        + ' from Wikipedia, the free encyclopedia' + info;
};
 
/**
 * Creates a string formatted for the 'info' parameter in the update method.
 *
 * @param start - text at the beginning of the string, before the link
 * @param pageLink - a link to the target page
 * @param defLink - the backup page link if !pageLink
 * @param linkText - the text of the link
 * @param end - text after the link
 * @return {String} output - the info string
 * @static
 */
MetadataScript.prototype.makeInfoString = function (start, pageLink, defLink, linkText, end) {
    var output;
    // path is usually just '/wiki/', but it's different on secure.wikimedia.org
    var path = wgArticlePath.replace('$1', '');
    var page = pageLink ? path + pageLink : (defLink ? path + defLink : null);
    start = start ? '. ' + start.toString() + ' ' : '';
    linkText = linkText ? linkText.toString() : '';
    end = end ? ' ' + end.toString() + '.' : '.';
    output = start + (page ? '<a href="' + page + '"' + (linkText ? '>' : ' \/>') : '')
        + linkText + ((page && linkText) ? '<\/a>' : '') + end;
    return output;
};
 
/**
 * Encodes the URL of a Wikipedia page for use in the talkAssess method.
 *
 * @param {String} inputText - the unencoded full page name
 * @return {String} outputText - the encoded page name
 * @static
 */
MetadataScript.prototype.encodePageName = function (inputText) {
    if ( !inputText ) return null;
    var outputText = encodeURIComponent(inputText);
    while ( outputText != null && outputText.match(/(\%20|\%2F)/i) ) {
        outputText = outputText.replace(/\%20/i, '_'); // unescape spaces for readability
        outputText = outputText.replace(/\%2F/i, '\/'); // %2F must be unescaped
    }
    return outputText;
};
 
/**
 * Creates the global MetadataObject as an instance of MetadataAssessmentScript, then 
 * calls the init() method of MetadataObject to start the script.
 */
if ( wgNamespaceNumber == 0 && (wgAction == 'view' || wgAction == 'purge')
    && wgPageName != 'Main_Page' ) {
    addOnloadHook(function () {
        if (typeof MetadataObject === 'undefined') { // only load object once
            MetadataObject = new MetadataScript();
        }
        MetadataObject.init();
    });
}