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. |
/* Using User:Anomie/linkclassifier.js as a template. Original comment from that script included below:
-----------------------
If you want to use this script, simply add the following line to your [[Special:Mypage/monobook.js]]:
importScript('User:Anomie/linkclassifier.js'); // Linkback: [[User:Anomie/linkclassifier.js]]
* (Please keep the comment so I can see how many people use this). You will also want to
* add some CSS classes, such as those at [[User:Anomie/linkclassifier.css]].
*/
/* If you want this to run "on demand" instead of on every page, set "LinkClassifierOnDemand=true" and
* use addPortletLink() or the like to add a button calling LinkClassifier.onDemand().
*/
var LinkClassifier={
/* This object maps classes to the categories for which to apply them. Values may be an array of strings or a regex. */
cats:{
deletion:[
'Category:All articles proposed for deletion',
'Category:All books proposed for deletion',
'Category:All disputed non-free Wikipedia files',
'Category:All orphaned non-free use Wikipedia files',
'Category:All possibly unfree Wikipedia files',
'Category:All redirects for discussion',
'Category:All replaceable non-free use Wikipedia files',
'Category:All Wikipedia files with no copyright tag',
'Category:All Wikipedia files with no non-free use rationale',
'Category:All Wikipedia files with unknown copyright status',
'Category:All Wikipedia files with unknown source',
'Category:Articles for deletion',
'Category:Articles on deletion review',
'Category:Candidates for speedy deletion',
'Category:Candidates for undeletion',
'Category:Categories for conversion',
'Category:Categories for deletion',
'Category:Categories for listifying',
'Category:Categories for merging',
'Category:Categories for renaming',
'Category:Categories for speedy renaming',
'Category:Categories to be listified then deleted',
'Category:Duplicate or hardcoded templates awaiting deletion',
'Category:Items pending OTRS confirmation of permission for over 30 days',
'Category:Miscellaneous pages for deletion',
'Category:Stub categories for deletion',
'Category:Stub template deletion candidates',
'Category:Templates for deletion',
'Category:Wikipedia deprecated and orphaned templates',
'Category:Wikipedia files for deletion',
'Category:Wikipedia files with unknown source for deletion in dispute',
'Category:Wikipedia templates for deletion'
].sort(),
disambiguation:[
'Category:All disambiguation pages'
].sort(),
'set-index':[
'Category:All set index articles'
].sort(),
'featured-content':[
'Category:Featured articles',
'Category:Featured lists',
'Category:Featured pictures',
'Category:Featured sounds',
'Category:Featured videos',
'Category:Featured portals'
].sort(),
'good-content':[
'Category:Good articles'
].sort(),
'soft-redirect-cats':[
'Category:Wikipedia soft redirected categories',
].sort(),
'spoken-articles':[
'Category:Spoken articles'
].sort(),
stubcls:/^Category:.* stubs$/,
'nonfree-media':[
'Category:All non-free media'
].sort(),
unprintworthy:[
'Category:Unprintworthy redirects',
'Category:Middle-earth redirects from redundant titles'
].sort(),
'unprintworthy-shortcut':[
'Category:Redirects from shortcuts',
].sort()
},
articleClasses: {
'FA':[], 'GA':[], 'A':[], 'B':[], 'C':[], 'Start':[], 'Stub':[], 'FL':[], 'L':[]
},
/* This object maps page props to CSS classes for which to apply them. Values may be an array of strings or a function returning such. */
/* No longer used
props: {
disambiguation:[
'disambiguation'
],
}, // */
/* 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;
/* Don't think we need to follow redirs for Talk: pages
var redir={};
var redirlist=[];
if(r.redirects) for(var i=r.redirects.length-1; i>=0; i--){
redir[r.redirects[i].from]=r.redirects[i].to;
redirlist.push(r.redirects[i].from);
}
if(redirlist.length>0) {
var q = {
format:'json',
action:'query',
titles:redirlist.join('|'),
prop:'categories|info',
inprop:'protection',
cllimit:'max',
rawcontinue:1
};
$.ajax({
url:mw.util.wikiScript('api'),
dataType:'json',
type:'POST',
data:q,
rawdata:q,
success:arguments.callee,
error:function(xhr,textStatus,errorThrown){
throw new Error('AJAX error: '+textStatus+' '+errorThrown);
}
});
} // */
var prefix=(this.rawdata.redirects?'':'redir-');
var cats={};
var missing={};
var classes={};
if(r.pages) for(var i in r.pages){
classes[r.pages[i].title] = [];
missing[r.pages[i].title]=(typeof(r.pages[i].missing)!='undefined');
if(typeof(r.pages[i].categories)!='undefined'){
cats[r.pages[i].title]=r.pages[i].categories.map(function(a){ return a.title; }).sort();
// TODO: We should match for assessment grades here and just throw out category data for talk pages.
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) {
LinkClassifier.articleClasses['FA'].push(LinkClassifier.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('A Class') >= 0) {
LinkClassifier.articleClasses['A'].push(LinkClassifier.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else if (r.pages[i].categories[iCat].title.indexOf('GA Class') >= 0) {
LinkClassifier.articleClasses['GA'].push(LinkClassifier.getPageNameFromTalkPage(r.pages[i].title));
unClassified = false;
} else {
//document.log();
}
}
iCat+=1;
}
if (unClassified) document.log(LinkClassifier.getPageNameFromTalkPage(r.pages[i].title) + " is unclassified");
}
/* A bunch of stuff I don't think we need....
if(typeof(r.pages[i].pageprops)!='undefined'){
for ( var k in r.pages[i].pageprops ) {
if ( !LinkClassifier.props[k] ) {
continue;
}
var v = LinkClassifier.props[k];
if ( $.isFunction( v ) ) {
v = v( r.pages[i].pageprops[k], k, r.pages[i].title );
}
classes[r.pages[i].title].push.apply( classes[r.pages[i].title], v );
}
}
if(typeof(r.pages[i].protection)!='undefined'){
var x={};
for(var j=r.pages[i].protection.length-1; j>=0; j--){
var p=prefix+'protection-'+r.pages[i].protection[j].type+'-'+r.pages[i].protection[j].level;
if(typeof(x[p])=='undefined'){
x[p]=1;
classes[r.pages[i].title].push(p);
}
if(r.pages[i].protection[j].expiry=='infinity'){
p+='-indef';
if(typeof(x[p])=='undefined'){
x[p]=1;
classes[r.pages[i].title].push(p);
}
}
}
}
if(typeof(r.pages[i].flagged)!='undefined'){
if(r.pages[i].lastrevid!=r.pages[i].flagged.stable_revid){
classes[r.pages[i].title].push('needs-review');
}
} //*/
}
Array.prototype.forEach.call(a, function(a){
if(typeof(a.wikipage)=='undefined') return;
/* Not used...
if(typeof(redir[a.wikipage])!='undefined'){
$(a).addClass('redirect');
a.wikipage=redir[a.wikipage];
a.title=a.wikipage;
var cns=mw.config.get('wgCanonicalNamespace');
if(a.wikipage==(cns?cns+':':'')+mw.config.get('wgTitle'))
$(a).addClass('self-redirect');
if(missing[a.wikipage])
$(a).addClass('broken-redirect');
} // */
var m=a.href.match(/#.*/);
if(m && m[0].substr(0,10)!=="#cite_note"){
a.title=a.title.replace(/#.*/,'')+m[0].replace(/_/g,' ').replace(/\.([0-9A-F][0-9A-F])/gi, function(x,n){ return String.fromCharCode(parseInt(n,16)); });
}
if(LinkClassifier.intentionaldab.test(a.origwikipage)){
$(a).addClass('intentional-disambiguation');
}
if(typeof(classes[a.wikipage])!='undefined'){
for(var j=classes[a.wikipage].length-1; j>=0; j--)
$(a).addClass(classes[a.wikipage][j]);
}
if(a.wikipage!=a.origwikipage && typeof(classes[a.origwikipage])!='undefined'){
for(var j=classes[a.origwikipage].length-1; j>=0; j--)
$(a).addClass(classes[a.origwikipage][j]);
}
var c1=[];
if(typeof(cats[a.wikipage])!='undefined'){
c1=c1.concat(cats[a.wikipage]);
}
if(a.wikipage!=a.origwikipage && typeof(cats[a.origwikipage])!='undefined'){
c1=c1.concat(cats[a.origwikipage]);
}
if(c1.length>0){
c1=c1.sort();
for(var cls in LinkClassifier.cats){
var i1=c1.length-1;
var c2=LinkClassifier.cats[cls];
if(c2 instanceof RegExp){
while(i1>=0){
if(c2.test(c1[i1])){
$(a).addClass(cls);
break;
}
i1--;
}
} else {
var i2=c2.length-1;
while(i1>=0 && i2>=0){
if(c1[i1]==c2[i2]){
$(a).addClass(cls);
break;
}
(c1[i1]>c2[i2])?--i1:--i2;
}
}
}
}
});
},
/* 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 '';
if(!/(^|\s)(image)(\s|$)/.test(a.className)) a.className+=" nonimage";
a.wikipage=LinkClassifier.getPageName(a.href);
if(a.wikipage==self) a.wikipage='';
a.origwikipage=a.wikipage;
return LinkClassifier.getTalkPageFromPageName(a.wikipage); //return a.wikipage;
}).sort().filter(function(e,i,a){
return e!=='' && (i==0 || a[i-1]!==e);
});
// a is now a sorted array of links with wikipage attributes holding the title of talk pages
function processLinks(limit){
/* Not checking page properties
var props = [];
for ( var k in LinkClassifier.props ) {
props.push( k );
} // */
while(a.length>0){
var q={
format:'json',
action:'query',
titles:a.splice(0,limit).join('|'),
prop:'categories', //prop:'categories|pageprops|info|flagged', // Only interested in Talk page categories
//redirects:1,
cllimit:'max',
//inprop:'protection',
rawcontinue:1
};
/*
if ( props.length <= limit ) {
q.ppprop = props.join( '|' ); // What is ppprop?
} // Not checking page properties */
$.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(window.LinkClassifierOnDemand) return;
if(window.AJAXPreview) window.AJAXPreview.AddOnLoadHook(LinkClassifier.classifyChildren);
LinkClassifier.onDemand();
},
onDemand:function(){
var node=document.getElementById('wikiPreview');
if(!node) node=document.getElementById('bodyContent');
if(node) LinkClassifier.classifyChildren(node);
}
};
if(!window.LinkClassifierOnDemand) $(document).ready(LinkClassifier.onLoad);