MediaWiki:Gadget-metadata.js
Notă: După salvare, trebuie să treceți peste cache-ul browser-ului pentru a vedea modificările.
- Mozilla, Google Chrome, Safari, Konqueror: țineți apăsat Shift în timp ce apăsați butonul Reload (sau apăsați Ctrl-Shift-R).
- Internet Explorer: apăsați Ctrl-F5.
- Opera: apăsați F5.
/** _____________________________________________________________________________
* | |
* | === WARNING: GLOBAL GADGET FILE === |
* | Changes to this page affect many users. |
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
* |_____________________________________________________________________________|
*
* Imported from revision 185704269 as of January 20, 2008 18:40 from
* [[User:Pyrospirit/metadata.js]], itself a modified version of
* [[User:Outriggr/metadata.js]].
* Metadata assessment script
* Finds the WP 1.0/WikiProject assessment of every article you go to, then
* displays that information in the article header. See (new script homepage
* pending).
* @author Outriggr - created the script and used to maintain it
* @author Pyrospirit - used to maintain and update the script
* @author Nihiltres - Overhauled the script, current maintainer
*/
window.assessment = (function () {
var assessmentObj = {
props: {},
methods: {}
},
//internal shortcuts
ap = assessmentObj.props,
am = assessmentObj.methods;
/**
* The main function of the script. If the checkArticle() function can find
* the assessment, it parses and displays that assessment for the page.
* Otherwise, it tries to retrieve an assessment via AJAX.
*/
assessmentObj.init = function () {
if (!$("#siteSub").length || //incompatible skin
mw.config.get("wgNamespaceNumber") !== 0 || //non-mainspace page
(mw.config.get("wgAction") !== "view" && mw.config.get("wgAction") !== "purge") || //non-read action
mw.util.getParamValue("printable") || //printable page
mw.config.get("wgPageName") === "Main_Page" //Main Page
) {
return; //Don't run the script under any of these conditions.
}
ap.foundAssessment = am.checkArticle(); //checks for types visible from article page
if (!ap.foundAssessment.exists) { // no type visible on article, proceed to check the talk page
$.ajax({
url: mw.config.get("wgScript") + "?title=Discuție:" + mw.util.wikiUrlencode(mw.config.get("wgPageName")) + "&action=raw§ion=0",
async: true,
dataType: "text",
success: function (responseText) {
ap.text = responseText;
ap.foundAssessment = am.getAssessment(ap.text);
ap.updata = am.renderAssessment(ap.foundAssessment);
am.update();
}
});
} else {
ap.updata = am.renderAssessment(ap.foundAssessment);
am.update();
}
};
/**
* Checks for various objects on the article page that indicate a certain
* assessment, such as a disambiguation page notice. If this function can
* find the assessment, AJAX is not needed for this page.
* @returns {Object} checkResult - the assessment found
*/
am.checkArticle = function () {
var checkResult = {
extra: [],
exists: false
}, checksList = [
[$("#disambig, #disambig_disambigbox, #disambigbox").length, "dab"],
[$("#setindexbox").length, "setindex"],
[$("#contentSub").text() === "Pagină de redirecționare", "redir"],
/* [$("table.stub").length, "stub"], */
[$("#ca-talk").hasClass("new"), "none"] //no talk page
];
$.each(checksList, function (i, e) {
if (e[0]) {
checkResult.rating = e[1];
checkResult.exists = true;
return false;
}
});
return checkResult;
};
/**
* Searches the provided wikicode for the rating part of an assessment and
* returns it as a string.
* Note that higher assessments take 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
*/
am.getRating = function (text) {
var rating = "none",
standardChecks = [
[/\|\s*(class|clasament|currentstatus|statuscurent)\s*=\s*(ac|fa)\b/i, "fa"],
[/\|\s*(class|clasament|currentstatus|statuscurent)\s*=\s*(lc|fl)\b/i, "fl"],
[/\|\s*(class|clasament)\s*=\s*a\b/i, "a"],
[/\|\s*(class|clasament)\s*=\s*b\b/i, "b"],
[/\|\s*(class|clasament)\s*=\s*bplus\b/i, "bplus"], //used by WP Math
[/\|\s*(class|clasament)\s*=\s*c\b/i, "c"],
[/\|\s*(class|clasament)\s*=\s*(start|început)/i, "start"],
[/\|\s*(class|clasament)\s*=\s*(stub|ciot)/i, "stub"],
[/\|\s*(class|clasament)\s*=\s*al\b/i, "al"], // used by WP Military history & WP Highways
[/\|\s*(class|clasament)\s*=\s*bl\b/i, "bl"], // used by WP Military history
[/\|\s*(class|clasament)\s*=\s*cl\b/i, "cl"], // used by WP Military history
[/\|\s*(class|clasament)\s*=\s*list/i, "list"],
[/\|\s*(class|clasament)\s*=\s*sl\b/i, "sl"], // used by WP Plants
[/\|\s*(class|clasament)\s*=\s*(dab|disambig|dezambig)/i, "dab"],
[/\|\s*(class|clasament)\s*=\s*(curent|actual)/i, "cur"],
[/\|\s*(class|clasament)\s*=\s*(future|viitor)/i, "future"]
];
//evaluate the standard checks
$.each(standardChecks, function (i, e) {
if (text.match(e[0])) {
rating = e[1];
return false;
}
});
//and then the nonstandard ones. These override earlier ratings if applicable.
if (rating === "a" && text.match(/\|\s*(class|clasament)\s*=\s*(ga|ab)\b|\|\s*statuscurent\s*=\s*(fac\/)?ab\b|\{\{\s*ab\s*\|/i)) {
rating = "a/ga"; // A-class articles that are also GA's
} else if (text.match(/\|\s*(class|clasament)\s*=\s*(ga|ab)\b|\|\s*statuscurent\s*=\s*(fac\/)?ab\b|\{\{\s*ab\s*\|/i) &&
!text.match(/\|\s*statuscurent\s*=\s*fab\b/i)) {
rating = "ga";
}
return rating;
};
/**
* 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} gottenAssessment - the assessment data for the page
*/
am.getAssessment = function (text) {
var gottenAssessment = {
rating: am.getRating(text),
pageLink: [null, null],
extra: [],
activeReview: null,
exists: true
},
actionNumber = 0,
pageLinkFlag = false,
peerReview, linkPattern, linkMatch, currentList, formerList;
currentList = [
// Current nominations (FAC, FLC, or GAN)
{
reg: /\{\{\s*PAC\s*(?:[\|\}]\s*([^\|\}]*))?[^\}]*?\}\}/i,
extraName: "fac",
addArticleNameTo: "Wikipedia:Propuneri_pentru_articole_de_calitate\/"
},
{
reg: /\{\{\s*PLC\s*(?:[\|\}]\s*([^\|\}]*))?[^\}]*?\}\}/i,
extraName: "flc",
addArticleNameTo: "Wikipedia:Propuneri_pentru_liste_de_calitate\/"
},
{
reg: /\{\{\s*AB ?nom\s*[\|\}][^\}]*\}\}/i,
extraName: "gan",
addMatchReg: /\|\s*page\s*=\s*(\d+).*\|\s*status\s*=\s*\w+\b/i,
addMatchTo: "Discuție:" + mw.util.wikiUrlencode(mw.config.get("wgPageName")) + "\/AB"
},
// Current reviews of a status (FAR, FLRC, or GAR)
{
reg: /\{\{\s*RAC\s*(?:[\|\}]\s*([^\|\}]*))?[^\}]*?\}\}/i,
extraName: "far",
addArticleNameTo: "Wikipedia:Reevaluări_articole_de_calitate\/"
},
{
reg: /\{\{\s*RLC\s*(?:[\|\}]\s*([^\|\}]*))?[^\}]*?\}\}/i,
extraName: "flrc",
addArticleNameTo: "Wikipedia:Featured_list_removal_candidates\/"
},
{
reg: /\{\{\s*RAB\/link\s*[\|\}][^\}]*\}\}/i,
extraName: "gar",
addMatchReg: /\|\s*GARpage\s*=\s*(\d+).*\|/i,
addMatchTo: mw.util.wikiUrlencode(mw.config.get("wgPageName"))
}
];
$.each(currentList, function (i, e) {
var reg = text.match(e.reg),
articleName,
tempMatch;
if (reg) {
gottenAssessment.extra.push(e.extraName);
if (e.hasOwnProperty("addArticleNameTo") && reg[1]) {
articleName = am.decodeEntities($.trim(reg[1]));
if (articleName) {
gottenAssessment.pageLink[0] = e.addArticleNameTo + articleName;
}
}
if (e.hasOwnProperty("addMatchReg")) {
tempMatch = reg[0].match(e.addMatchReg);
if (tempMatch) {
gottenAssessment.pageLink[0] = (e.addMatchTo || "") + (tempMatch[1] || "");
}
if (e.extraName === "gar") { //Can't get around this special case easily
gottenAssessment.pageLink[0] = am.getGARLink(e.addMatchTo, tempMatch[1]);
}
}
return false;
}
});
formerList = [
// Former statuses (FFA, FFL, or DGA)
{
name: "ffa",
reg: /\|\s*statuscurent\s*=\s*(ffa|fac)\b/i,
getActionNumber: true,
getActionNumberReg: /\|\s*acțiune(\d+)\s*=\s*(far|rac)\b/gi
},
{
name: "ffa",
reg: /\|\s*acțiune(\d+)\s*=\s*(far|rac)\b/gi,
extraCondition: function (ec_reg) {
//Checks if the last FAR entry in ArticleHistory resulted in removal.
var match, ratingSearch;
if (!ec_reg) {
return false;
}
match = text.match(new RegExp(
"\\|\\s*acțiune" + ec_reg[ec_reg.length - 1].match(/\d+/) + "rezultat\\s*=\\s*retrogradat\b",
"i"
));
ratingSearch = (gottenAssessment.rating.search(/f[al]/i) === -1);
return (match && ratingSearch);
},
getActionNumber: true
},
{
name: "ffa",
reg: /\{\{\s*formerfa2?\b/i
},
{
name: "ffl",
reg: /\|\s*statuscurent\s*=\s*(ffl|flc)\b/i
},
{
name: "ffl",
reg: /\{\{\s*ffl\s*[\|\}]/i
},
{
name: "dga",
reg: /\|\s*statuscurent\s*=\s*(dga|fab)\b/i,
getActionNumber: true,
getActionNumberReg: /\|\s*action(\d+)\s*=\s*gar\b/gi
},
{
name: "dga",
reg: /\{\{\s*d(elisted)?ga\s*[\|\}]/i
},
// Former nominations (former FAC, FLC, or GAN)
{
name: "ffac",
reg: /\|\s*acțiune(\d+)\s*=\s*fpac\b/gi,
extraCondition: function () {
return (gottenAssessment.rating.search(/f[al]/i) === -1);
},
getActionNumber: true
},
{
name: "ffac",
reg: /\|\s*statuscurent\s*=\s*fpac\b/i
},
{
name: "ffac",
reg: /\{\{\s*fac?(failed|(\-|[ _]\()?contested\)?)\s*[\|\}]/i
},
{
name: "fflc",
reg: /\|\s*acțiune(\d+)\s*=\s*plc\b/gi,
extraCondition: function () {
return (gottenAssessment.rating.search(/f[al]/i) === -1);
},
getActionNumber: true
},
{
name: "fflc",
reg: /\|\s*currentstatus\s*=\s*plc\b/i
},
{
name: "fgan",
reg: /\|\s*acțiune(\d+)\s*=\s*nab\b/gi,
extraCondition: function () {
return (gottenAssessment.rating.search(/f[al]|(a\/)?ga/i) === -1);
},
getActionNumber: true
},
{
name: "fgan",
reg: /\|\s*statuscurent\s*=\s*fnab\b/i
},
{
name: "fgan",
reg: /\{\{\s*f(ailed ?)?ga\s*[\|\}]/i
}
];
$.each(formerList, function (i, e) {
var reg = text.match(e.reg),
extraCondition = !e.hasOwnProperty("extraCondition") ||
typeof e.extraCondition !== "function" ||
e.extraCondition(reg), //either true (ignored) or result of function
tempMatch;
if (reg && extraCondition) {
gottenAssessment.extra.push(e.name);
if (e.getActionNumber) {
tempMatch = (e.getActionNumberReg ? text.match(e.getActionNumberReg) : reg);
actionNumber = tempMatch[tempMatch.length - 1].match(/\d+/);
pageLinkFlag = true;
}
return false;
}
});
// Looks for currently active peer reviews
ap.showOldPeerReviews = false; //see TODO below
peerReview = text.match(/\{\{\s*peer[_ ]?review\s*\|\s*archive\s*=\s*(\d+)\b/i);
if (peerReview) {
gottenAssessment.review = "Wikipedia:Peer_review/" +
mw.config.get("wgPageName") + "/archive" + peerReview[1];
gottenAssessment.activeReview = true;
} else if (ap.showOldPeerReviews) {
$.noop(); // TODO: Add code for old peer reviews
} else {
gottenAssessment.review = null;
}
// Scans for the link associated with an action in ArticleHistory
if (pageLinkFlag) {
linkPattern = new RegExp("\\|\\s*acțiune" + actionNumber + "legătură\\s*=\\s*([^\\n\\|]+)\\s*\\|");
linkMatch = text.match(linkPattern);
gottenAssessment.pageLink[1] = linkMatch ? am.decodeEntities(linkMatch[1]) : null;
}
return gottenAssessment;
};
/**
* 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
*/
am.renderAssessment = function (assess) {
var assessLink = mw.util.getUrl("Wikipedia:Evaluarea_articolelor"),
peerReviewText = am.addPeerReview(assess.review, assess.activeReview),
pageLink = assess.pageLink || [null, null],
info = am.getExtraInfo((assess.extra || []), pageLink),
newClass,
slogan,
ratingList;
if (peerReviewText) {
info.push(peerReviewText);
}
ratingList = [
{
name: "a",
className: "assess-a-text",
text: "Un articol <a>clasa A<\/a>"
},
{
name: "a/ga",
className: "assess-a-text",
text: "Un articol <a>clasa A<\/a>",
info: "În plus, <a href=\"" + mw.util.getUrl("Wikipedia:Articole_bune") + "\">articol bun<\/a>."
},
{
name: "ga",
className: "assess-ga-text",
text: "Un <a>articol bun<\/a>",
url: mw.util.getUrl("Wikipedia:Articole_bune")
},
{
name: "b",
className: "assess-b-text",
text: "Un articol <a>clasa B<\/a>"
},
{
name: "bplus",
className: "assess-bplus-text",
text: "Un articol <a>clasa B-plus<\/a>",
url: mw.util.getUrl("Wikipedia:WikiProject_Mathematics/Wikipedia_1.0/Grading_scheme")
},
{
name: "c",
className: "assess-c-text",
text: "Un articol <a>clasa C<\/a>"
},
{
name: "început",
className: "assess-start-text",
text: "Un articol <a>aflat la început<\/a>"
},
{
name: "start",
className: "assess-start-text",
text: "Un articol <a>aflat la început<\/a>"
},
{
name: "stub",
className: "assess-stub-text",
text: "Un <a>ciot<\/a> de articol"
},
{
name: "al",
className: "assess-al-text",
text: "O listă <a>clasa A<\/a>",
url: mw.util.getUrl("Wikipedia:WikiProject_Military_history/Assessment") + "#SCALE" //Could use a more general link if one is available
},
{
name: "bl",
className: "assess-bl-text",
text: "O listă <a>clasa B<\/a>",
url: mw.util.getUrl("Wikipedia:WikiProject_Military_history/Assessment") + "#SCALE"
},
{
name: "cl",
className: "assess-cl-text",
text: "O listă <a>clasa C<\/a>",
url: mw.util.getUrl("Wikipedia:WikiProject_Military_history/Assessment") + "#SCALE"
},
{
name: "sl",
className: "assess-sl-text",
text: "Un <a>ciot<\/a> de listă"
},
{
name: "list",
className: "assess-list-text",
text: "Un articol <a>de tip listă<\/a>",
url: mw.util.getUrl("Wikipedia:Liste")
},
{
name: "dab",
className: "assess-dab-text",
text: "O pagină de <a>dezambiguizare<\/a>",
url: mw.util.getUrl("Wikipedia:Dezambiguizare")
},
{
name: "setindex",
className: "assess-setindex-text",
text: "Un <a>articol index<\/a>",
url: mw.util.getUrl("Wikipedia:Dezambiguizare") + "#Set_index_articles"
},
{
name: "redir",
className: "assess-redir-text",
text: "O pagină de <a>redirecționare<\/a>",
url: mw.util.getUrl("Wikipedia:Redirecționare")
},
{
name: "fl",
className: "assess-fl-text",
text: "O <a>listă de calitate<\/a>",
url: mw.util.getUrl("Wikipedia:Liste_de_calitate")
},
{
name: "fa",
className: "assess-fa-text",
text: "Un <a>articol de calitate<\/a>",
url: mw.util.getUrl("Wikipedia:Articole_de_calitate")
},
{
name: "cur",
className: "assess-cur-text",
text: "Un articol <a>de actualitate<\/a>",
url: mw.util.getUrl("Portal:Actualități")
},
{
name: "future",
className: "assess-future-text",
text: "Un articol <a>despre viitor<\/a>",
url: mw.util.getUrl("Category:Future-Class_articles")
}
];
$.each(ratingList, function (i, e) {
if (assess.rating === e.name) {
newClass = e.className;
slogan = $("<span>").html(e.text).children().attr({href: (e.url || assessLink)}).parent().html();
if (e.info) {
info.push(e.info);
}
return false;
}
});
if (!newClass) {
newClass = "assess-unassessed-text";
slogan = "Un articol <a href=\"" + assessLink + "\">neevaluat<\/a>";
}
return {newClass: newClass, slogan: slogan, info: info};
};
/**
* Creates an info string based on the assessment info and a page link.
*/
am.getExtraInfo = function (extra, pageLink) {
var info = [], page = mw.config.get("wgPageName"), typeList;
typeList = [
// Current nominations and reviews
{
name: "fac",
html: "Actualmente <a>candidat la articole de calitate<\/a>.",
url: pageLink[0] || ("Wikipedia:Propuneri_pentru_articole_de_calitate/" + page)
},
{
name: "flc",
html: "Actualmente <a>candidat la liste de calitate<\/a>.",
url: pageLink[0] || ("Wikipedia:Propuneri_pentru_liste_de_calitate/" + page)
},
{
name: "gan",
html: "Actualmente <a>nominalizat ca articol bun<\/a>.",
url: pageLink[0] || "Wikipedia:Nominalizări_articole_bune"
},
{
name: "far",
html: "Aflat în <a>revizuire<\/a> a statutului de articol de calitate.",
url: pageLink[0] || ("Wikipedia:Propuneri_pentru_articole_de_calitate/Reevaluare/" + page)
},
{
name: "flrc",
html: "Actualmente <a>candidat<\/a> pentru retragerea statutului de listă de calitate.",
url: pageLink[0] || ("Wikipedia:Featured_list_removal_candidates/" + page)
},
{
name: "gar",
html: "Actualmente în <a>reevaluare a statutului de articol bun<\/a>.",
url: pageLink[0] || "Wikipedia:Reevaluare_articole_bune",
wrapper: "<span id=\"assess-gar-link\"><\/span>"
},
// Past statuses and nominations
{
name: "ffa",
html: "Un <a>fost<\/a> articol de calitate.",
url: pageLink[1] || ("Wikipedia:Propuneri_pentru_articole_de_calitate/Reevaluare/" + page)
},
{
name: "ffl",
html: "O <a>fostă<\/a> listă de calitate.",
url: pageLink[1] || ("Wikipedia:Featured_list_removal_candidates/" + page)
},
{
name: "dga",
html: "Un <a>fost<\/a> articol bun.",
url: pageLink[1] || "Wikipedia:Reevaluare_articole_bune"
},
{
name: "ffac",
html: "Un fost <a>candidat la titlul de articol de calitate<\/a>.",
url: pageLink[1] || ("Wikipedia:Propuneri_pentru_articole_de_calitate/" + page)
},
{
name: "fflc",
html: "Fost <a>candidat la titlul de listă de calitate<\/a>.",
url: pageLink[1] || ("Wikipedia:Propuneri_pentru_liste_de_calitate/" + page)
},
{
name: "fgan",
html: "Fost <a>candidat la articole bune<\/a>.",
url: pageLink[1] || "Wikipedia:Nominalizări_articole_bune"
}
];
$.each(typeList, function (i, e) {
if (extra.indexOf(e.name) !== -1) {
info.push($("<span>").html(e.html).children().attr({href: mw.util.getUrl(e.url)})
.parent().wrapInner(e.wrapper || null).html());
}
});
return info;
};
/**
* Get the correct link for Good Article reassessments. These things require an
* additional AJAX request to determine whether it's a community or individual
* reassessment. The trick is to assume it's a community reassessment, then
* switch the link once the request returns if it's actually not.
* @param {String} articleName - the name of the article to use
* @param {String} reviewNumber - the number of the GAR to look for
*/
am.getGARLink = function (articleName, reviewNumber) {
var communityTitle = "Wikipedia:Reevaluări_articole_bune\/" + articleName + "\/" + reviewNumber,
individualTitle = "Discuție:" + articleName + "\/AB" + reviewNumber,
titlesList = [communityTitle, individualTitle],
api = new mw.Api();
api.get({
action: "query",
titles: titlesList.join("|"),
prop: "info",
format: "json"
}).done(function (data) {
var i, j, noCommunityAssessment,
query = data.query,
communityTitleNorm = titlesList[0],
individualTitleNorm = titlesList[1],
len = query.normalized.length,
garLink = $("#assess-gar-link a:first");
for (j = 0; j < len; j++) {
switch (query.normalized[j].from) {
case titlesList[0]:
communityTitleNorm = query.normalized[j].to;
break;
case titlesList[1]:
individualTitleNorm = query.normalized[j].to;
break;
}
}
noCommunityAssessment = false;
for (i = -1; i >= -2; i--) {
if (query.pages[i] && typeof query.pages[i].missing === "string") {
if (query.pages[i].title === individualTitleNorm) {
// No individual assessment, no need to change anything.
return;
}
if (query.pages[i].title === communityTitleNorm) {
// There's no community assessment, so flag it.
noCommunityAssessment = true;
}
}
}
if (noCommunityAssessment && garLink.length) {
// There's an individual assessment but no community assessment. Switch the link.
garLink.attr("href", mw.util.getUrl(titlesList[1]));
}
});
return communityTitle;
};
/**
* Creates the peer review text from an info string, if a peer review was detected earlier.
*/
am.addPeerReview = function (peerReview, activeReview) {
var reviewText = null;
if (peerReview) {
reviewText = $("<div><span class=\"assess-info-review\">" +
(activeReview ? "Actualmente" : "Anterior") + " <a>recenzat<\/a>.<\/span><\/div>");
reviewText.find("a").attr({href: mw.util.getUrl(peerReview)});
reviewText = reviewText.html(); //Note div wrapper above.
}
return reviewText;
};
/**
* 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.
*/
am.update = function () {
var info = ap.updata.info,
infoSpan = $("<span class=\"assess-info-all\"><\/span>"),
siteSub = $("<div><span class=\"assess-article-rating\"><\/span> de la Wikipedia, enciclopedia liberă<\/div>");
siteSub.children().html(ap.updata.slogan);
if (info && info.length > 0) {
infoSpan.html(".");
$.each(info, function (i, e) {
infoSpan.append(" ").append(e);
});
siteSub.append(infoSpan);
}
$("h1:first").addClass(ap.updata.newClass || null);
$("#siteSub").html(siteSub.html());
};
/**
* Decodes all HTML entities in the string provided.
*/
am.decodeEntities = function (str) {
var t = document.createElement("textarea");
t.innerHTML = str;
return t.value;
};
return assessmentObj;
}());
/**
* Initializes the script on page load
*/
$(assessment.init);