User:Anomie/linkclassifier.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

/* If you want to use this script, simply add the following line to your 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]].
*/
 
importScript('User:Anomie/util.js');
 
var LinkClassifier={
    /* This array lists "for deletion" categories */
    cats:{
        deletion:[
            'Category:All articles proposed for deletion',
            'Category:Articles for deletion',
            'Category:Articles on deletion review',
            'Category:Candidates for speedy deletion',
            'Category:Candidates for undeletion',
            'Category:Categories for deletion',
            'Category:Categories for discussion',
            'Category:Categories for merging',
            'Category:Categories for renaming',
            'Category:Images and media for deletion',
            'Category:Miscellaneous pages for deletion',
            'Category:Pending deletions',
            'Category:Redirects for deletion',
            'Category:User categories for discussion',
            'Category:Wikipedia templates for deletion'
        ].sort(),
        disambiguation:[
            'Category:Disambiguation',
            'Category:Airport disambiguation',
            'Category:Disambiguation categories',
            'Category:Disambiguation pages with Chinese character titles',
            'Category:Disambiguation pages in need of cleanup',
            'Category:Disambiguation pages in need of being split',
            'Category:Educational institution disambiguation',
            'Category:Disambiguation containing homophones',
            'Category:Lists of ambiguous hospital names',
            'Category:Lists of Interstate Highways sharing the same title',
            'Category:Mathematical disambiguation',
            'Category:Disambiguation lists of mountains',
            'Category:Lists of ambiguous numbers',
            'Category:Place of worship disambiguation',
            'Category:Lists of political parties by generic name',
            'Category:Lists of roads sharing the same title',
            'Category:Lists of Interstate Highways sharing the same title',
            'Category:Lists of U.S. Routes sharing the same title',
            'Category:Ship disambiguation',
            'Category:Fish common name disambiguation',
            'Category:Lists of ambiguous human names',
            'Category:Ambiguous place names',
            'Category:Redirects from incomplete disambiguations',
            'Category:Redirects to disambiguation pages'
        ].sort()
    },
 
    callback:function(r){
        if(!r.query) throw new Error('Bad response');
        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;
 
        var redir={};
        if(r.redirects) for(var i=r.redirects.length-1; i>=0; i--){
            redir[r.redirects[i].from]=r.redirects[i].to;
        }
        var cats={};
        if(r.pages) for(var i in r.pages){
            if(i<0 || typeof(r.pages[i].categories)=='undefined') continue;
            cats[r.pages[i].title]=r.pages[i].categories.map(function(a){ return a.title; }).sort();
        }
        Array.forEach(a, function(a){
            if(typeof(a.wikipage)=='undefined') return;
            if(typeof(redir[a.wikipage])!='undefined'){
                addClass(a,'redirect');
                a.wikipage=redir[a.wikipage];
                a.title=a.wikipage;
                if(a.wikipage==(wgCanonicalNamespace?wgCanonicalNamespace+':':'')+wgTitle)
                    addClass(a,'self-redirect');
            }
            if(typeof(cats[a.wikipage])!='undefined'){
                var c1=cats[a.wikipage];
                for(var cls in LinkClassifier.cats){
                    var i1=c1.length-1;
                    var c2=LinkClassifier.cats[cls];
                    var i2=c2.length-1;
                    while(i1>=0 && i2>=0){
                        if(c1[i1]==c2[i2]){
                            addClass(a,cls);
                            break;
                        }
                        (c1[i1]>c2[i2])?--i1:--i2;
                    }
                }
            }
        });
    },
 
    classifyChildren:function(node){
        var a=node.getElementsByTagName('A');
        if(a.length==0) return;
        var re=/(^|\s)(external|extiw|new)(\s|$)/;
        var re2=/(^|\s)image(\s|$)/;
        a=Array.map(a, function(a){
            a.wikipage=a.title;
            if(re.test(a.className)) a.wikipage='';
            else if(re2.test(a.className)) a.wikipage=decodeURIComponent(a.href.replace(/^.*\/wiki\/Image:/,'Image:'));
            return a.wikipage;
        }).sort().filter(function(e,i,a){
            return e!=='' && e.substr(0,13)!=='Edit section:' && e.substr(0,8)!=='Special:' && (i==0 || a[i-1]!==e);
        });
        while(a.length>0){
            api({action:'query', titles:a.splice(0,50), prop:'categories', redirects:1}, LinkClassifier.callback);
        }
    },
 
    onLoad:function(){
        var node=document.getElementById('wikiPreview');
        if(!node) node=document.getElementById('bodyContent');
        if(node) LinkClassifier.classifyChildren(node);
    }
};
 
if(doneOnloadHook) LinkClassifier.onLoad(); //if imported dynamically
else addOnloadHook(LinkClassifier.onLoad);