/**
* 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-fr.css');
/**
* This is the constructor for the script object. All functions this script
* defines are inside this.
* @constructor
*/
MetadataScript = function () {};
MetadataScript.createObject = true; // create an instance of MetadataScript
MetadataScript.autorun = true; // run automatically when the page finishes loading
/**
* 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 ) {
this.assessment = initialAssessment;
var data = this.talkAssess(this.assessment);
this.update(data.newClass, data.slogan);
}
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[0] - Optional: override the default URL for the
* request.
*/
MetadataScript.prototype.ajaxMain = function () {
if ( arguments[0] && arguments[0].match(/^https?:\/\//i) ) // optional url override
this.url = arguments[0];
else this.url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Discuter:' + encodeURIComponent(mw.config.get('wgPageName'))
+ '&action=raw§ion=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;
this.assessment = this.getRating(this.text);
var data = this.talkAssess(this.assessment);
this.update(data.newClass, data.slogan);
this.onCompletedRequest();
}
};
MetadataScript.prototype.onCompletedRequest = function () {};
/**
* 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 = '';
if ( document.getElementById('homonymie') )
assess = 'homonymie';
else if ( document.getElementById('contentSub').innerHTML == 'Page de redirection' )
assess = 'redir';
else if ( document.getElementById('ca-talk').className == 'new' ) // no talk page
assess = 'none';
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();
var rating = 'none';
if ( text.match(/\|\s*avancement\s*=\s*adq\b/i) )
rating = 'adq';
else if ( text.match(/\|\s*avancement\s*=\s*a\b/i) ) {
if ( text.match(/\|\s*avancement\s*=\s*ba\b/i) )
rating = 'a/ba'; // A-class articles that are also GA's
else rating = 'a';
}
else if ( text.match(/\|\s*avancement\s*=\s*ba\b/i) )
rating = 'ba';
else if ( text.match(/\|\s*avancement\s*=\s*b\b/i) )
rating = 'b';
else if ( text.match(/\|\s*avancement\s*=\s*bd/i) )
rating = 'bd';
else if ( text.match(/\|\s*avancement\s*=\s*ébauche/i) )
rating = 'ébauche';
this.getRatingAfter();
return rating;
}
MetadataScript.prototype.getRatingBefore = function () {};
MetadataScript.prototype.getRatingAfter = 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 + 'Projet:Wikipédia_1.0/avancement';
var rating = assess.toLowerCase();
if ( rating == 'a' || rating == 'a/ba' ) {
newClass = 'assess-A-text';
slogan = 'Un <a href="' + assessLink + '">article d\'avancement A</a> de Wikipédia, l\'encyclopédie libre.';
if ( rating == 'a/ba' ) {
slogan += ' Aussi un <a href="' + path + 'Wikipédia:Bons_articles">bon article</a>.'
}
} else if ( rating == 'ba' ) {
newClass = 'assess-GA-text';
slogan = 'Un <a href="' + path + 'Wikipédia:Bons_articles">bon article</a> de Wikipédia, l\'encyclopédie libre.'
} else if ( rating == 'b' ) {
newClass = 'assess-B-text';
slogan = 'Un <a href="' + assessLink + '">article d\'avancement B</a> de Wikipédia, l\'encyclopédie libre.';
} else if ( rating == 'bd' ) {
newClass = 'assess-Start-text';
slogan = 'Un <a href="' + assessLink + '">bon début</a> de Wikipédia, l\'encyclopédie libre.';
} else if ( rating == 'ébauche' ) {
newClass = 'assess-Stub-text';
slogan = 'Une <a href="' + assessLink + '">ébauche</a> de Wikipédia, l\'encyclopédie libre.';
} else if ( rating == 'homonymie' ) {
newClass = 'assess-Dab-text';
slogan = 'Une <a href="' + path + 'Aide:Homonymie">page d\'homonymie</a> de Wikipédia, l\'encyclopédie libre.';
} else if ( rating == 'redir' ) {
newClass = 'assess-Redir-text';
slogan = 'Une <a href="' + path + 'Aide:Redirection">page de redirection</a> de Wikipédia, l\'encyclopédie libre.';
} else if ( rating == 'adq' ) {
newClass = 'assess-FA-text';
slogan = 'Un <a href="' + path + 'Wikipédia:Articles_de_qualité">article de qualité</a> de Wikipédia, l\'encyclopédie libre.';
} else {
newClass = '';
slogan = 'Un article <a href="' + assessLink + '">à évaluer</a> de Wikipédia, l\'encyclopédie libre.';
}
// Add CSS classes to allow for customization
slogan = '<span class="assess-article-rating">' + slogan + '</span>';
this.talkAssessAfter();
return {newClass: newClass, slogan: slogan};
};
MetadataScript.prototype.talkAssessBefore = function () {};
MetadataScript.prototype.talkAssessAfter = function () {};
/**
* 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) {
if ( wgPageName == 'Accueil' ) return;
var firstHeading = document.getElementsByTagName('h1')[0];
firstHeading.className += ' ' + newClass; // add newClass as an additional class
var siteSub = document.getElementById('siteSub');
siteSub.innerHTML = slogan;
siteSub.style.display = 'inline';
siteSub.style.fontSize = '92%';
siteSub.style.fontWeight = 'normal';
siteSub.style.borderBottom = 'medium none';
};
/**
* 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')
&& document.location.href.search(/\?(.+\&)?printable=[^&]/i) == -1
&& wgPageName != 'Accueil' ) {
addOnloadHook(function () {
if ( !MetadataScript.createObject ) return;
if ( typeof MetadataObject === 'undefined' ) // only load object once
MetadataObject = new MetadataScript();
if ( MetadataScript.autorun )
MetadataObject.init();
});
}