User:N8wilson/AQFetcher.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:N8wilson/AQFetcher and an accompanying .css page at User:N8wilson/AQFetcher.css. |
/* This script will classify as many links as possible (before reaching API limits) and assign
CSS classes accordingly. The following CSS classes are used:
AQ-FA AQ-A AQ-Start AQ-Unclassified
AQ-GA AQ-B AQ-Stub AQ-Unknown
AQ-C
These correspond to the *standard* grades listed at
http://en.wikipedia.org/wiki/Wikipedia:Version_1.0_Editorial_Team/Assessment
AQ-Unclassified is the only addition. Due to API limits, there may be articles with
a completed quality assessment which end up having the 'AQ-Unclassfied' style class
added to their link element. Thus, "unclassified" in this script means that the script
was not able to classify the target article and *not* that the target article itself is
without a quality grade.
Key acknowledgement goes to User:Anomie whose script (User:Anomie/linkclassifier.js)
was used as an initial template structure to get up an running quickly with MW api. Go
check it out if you're interested in a lot more ways to classify and color article links.
*/
var LinkClassifier={
articleClasses: {
'FA':[], 'GA':[], 'A':[], 'B':[], 'C':[], 'Start':[], 'Stub':[], 'FL':[], 'L':[], 'Unclassified':[]
},
/* This regex matches page titles to be marked as intentional links to disambiguation pages */
intentionaldab: / \(disambiguation\)$/,
callback:function(r, sts, xhr){
if(!r.query) {
if(typeof(window.console)=='undefined' || typeof(window.console.error)!='function')
throw new Error('Bad response');
window.console.error("Bad response", r);
return;
}
if(r['query-continue']){
var cc=this.rawdata;
for(var k in r['query-continue']){
for(var k2 in r['query-continue'][k]){
cc[k2]=r['query-continue'][k][k2];
}
}
$.ajax({
url:mw.util.wikiScript('api'),
dataType:'json',
type:'POST',
data:cc,
rawdata:cc,
success:arguments.callee,
error:function(xhr,textStatus,errorThrown){
throw new Error('AJAX error: '+textStatus+' '+errorThrown);
}
});
}
r=r.query;
var a=document.getElementById('wikiPreview');
if(!a) a=document.getElementById('bodyContent');
if(!a) throw new Error('Huh? No body content?');
a=a.getElementsByTagName('A');
if(a.length==0) return;
// Handy shortcut
var LC = LinkClassifier;
if(r.pages) for(var i in r.pages){
if(typeof(r.pages[i].categories)!='undefined'){
// If the corresponding Talk: page has appropriately-named categories we can
// match each article page to it's classification
unClassified = true;
iCat = 0;
while (unClassified && iCat < r.pages[i].categories.length) {
if (r.pages[i].categories[iCat].title!='undefined') {
if (r.pages[i].categories[iCat].title.indexOf('FA-Class') >= 0) {
LC.articleClasses['FA'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('A-Class') >= 0) {
LC.articleClasses['A'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('GA-Class') >= 0) {
LC.articleClasses['GA'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('B-Class') >= 0) {
LC.articleClasses['B'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('C-Class') >= 0) {
LC.articleClasses['C'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('Start-Class') >= 0) {
LC.articleClasses['Start'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('Stub-Class') >= 0) {
LC.articleClasses['Stub'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
}
}
iCat+=1;
}
if (unClassified) LC.articleClasses['Unclassified'].push(LC.getPageNameFromTalkPage(r.pages[i].title));
}
}
Array.prototype.forEach.call(a, function(a){
if(typeof(a.wikipage)=='undefined') return;
if (LC.articleClasses['FA'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-FA');
} else if (LC.articleClasses['GA'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-GA');
} else if (LC.articleClasses['A'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-A');
} else if (LC.articleClasses['B'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-B');
} else if (LC.articleClasses['C'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-C');
} else if (LC.articleClasses['Start'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-Start');
} else if (LC.articleClasses['Stub'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-Stub');
} else if (LC.articleClasses['Unclassified'].indexOf(a.wikipage) >= 0) {
$(a).addClass('AQ-Unclassified');
} else {
//$(a).addClass('AQ-Unknown');
}
});
},
/* Extract the name of the WP article from the URL */
getPageName:function(url){
var m=url.match(/\/wiki\/([^?#]+)/);
if(!m) m=url.match(/\/w\/index.php\?(?:.*&)?title=([^&#]+)/);
if(!m) return '';
var t=decodeURIComponent(m[1]).replace(/_/g,' ');
if(t.substr(0,6)=='Image:') t='File:'+t.substr(6);
if(t.substr(0,11)=='Image talk:') t='File talk:'+t.substr(6);
if(t.substr(0,8)=='Special:') t='';
return t;
},
/* If given a pagename which is not in the 'Talk:' namespace, adds 'Talk:'
* and returns the name of the page in the talk namespace. Returns
* unaltered input if the provided name is already in the "Talk:" namespace */
getTalkPageFromPageName:function(name){
return (name.substr(0,5)=='Talk:') ? name : 'Talk:'+name;
},
/* If given a pagename in "Talk:" namespace, removes the namespace to return the page name
* returns unaltered input if the provided name is not in the "Talk:" namespace */
getPageNameFromTalkPage:function(name){
return (name.substr(0,5)=='Talk:') ? name.substr(5,name.length) : name;
},
classifyChildren:function(node){
mw.loader.using(['mediawiki.util','mediawiki.user'], function(){
var a=node.getElementsByTagName('A');
if(a.length==0) return;
var self=LinkClassifier.getPageName(location.href);
a=Array.prototype.map.call(a, function(a){
a.wikipage='';
if(/(^|\s)(external|extiw)(\s|$)/.test(a.className)) return '';
a.wikipage=LinkClassifier.getPageName(a.href);
if(a.wikipage==self) a.wikipage='';
if(a.wikipage.indexOf(':')>0) a.wikipage=''; //filter out anything not in main namespace
a.origwikipage=a.wikipage;
return LinkClassifier.getTalkPageFromPageName(a.wikipage);
}).sort().filter(function(e,i,a){
return e!=='' && (i==0 || a[i-1]!==e) && e!=='Talk:';
});
// a is now a sorted array of links with wikipage attributes holding the title of talk pages
function processLinks(limit){
while(a.length>0){
var q={
format:'json',
action:'query',
titles:a.splice(0,limit).join('|'),
prop:'categories', // Only interested in Talk page categories
cllimit:'max',
rawcontinue:1
};
$.ajax({
url:mw.util.wikiScript('api'),
dataType:'json',
type:'POST',
data:q,
rawdata:q,
success:LinkClassifier.callback,
error:function(xhr,textStatus,errorThrown){
throw new Error('AJAX error: '+textStatus+' '+errorThrown);
}
});
}
}
if(a.length<=100){
// Not worth querying the API to see if the user has apihighlimits
processLinks(50);
} else {
// Note mw.user.getRights queries the API
mw.user.getRights(function(rights){
processLinks( (rights.indexOf('apihighlimits')>=0) ? 500 : 50 );
});
}
});
},
onLoad:function(){
if (mw.config.get( 'wgCanonicalNamespace' ) != "") {
console.log("Browsing in " + mw.config.get( 'wgCanonicalNamespace' ) + " namespace so article link styles will not be applied by classification.");
return;
}
// Figure out where the article content is...
var node=document.getElementById('wikiPreview');
if(!node) node=document.getElementById('bodyContent');
if(node) LinkClassifier.classifyChildren(node);
}
};
$(document).ready(LinkClassifier.onLoad);