Jump to content

User:Anomie/linkclassifier.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anomie (talk | contribs) at 05:28, 15 November 2007 (Link fixer script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
importScript('User:Anomie/util.js');

var LinkFixer={
    /* This array lists "for deletion" categories */
    cats:[
        'Category:Articles for deletion',
        'Category:Categories for deletion',
        'Category:Images and media for deletion',
        'Category:Miscellaneous pages for deletion',
        'Category:Redirects for deletion',
        'Category:User categories for discussion',
        'Category:Wikipedia templates for deletion'        
    ].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(redir[a.title])!='undefined'){
                addClass(a,'redirect');
                a.title=redir[a.title];
            }
            if(typeof(cats[a.title])!='undefined'){
                var c1=cats[a.title];
                var i1=c1.length-1;
                var c2=LinkFixer.cats;
                var i2=c2.length-1;
                while(i1>=0 && i2>=0){
                    if(c1[i1]==c2[i2]){
                        addClass(a,'deletion');
                        break;
                    }
                    (c1[i1]>c2[i2])?--i1:--i2;
                }
            }
        });
    },
 
    onLoad:function(){
        var a;
        if(wgAction=='view'){
            api({action:'query', titles:wgPageName, generator:'links', prop:'categories', redirects:1}, LinkFixer.callback);
        } else if(document.getElementById("wikiPreview")){
            var a=document.getElementById("wikiPreview").getElementsByTagName('A');
            if(a.length==0) return;
            a=Array.map(a, function(a){ return a.title; }).sort().filter(function(e,i,a){ return i==0 || a[i-1]!==e; });
            api({action:'query', titles:a, prop:'categories', redirects:1}, LinkFixer.callback);
        }
    }
};

if(doneOnloadHook) LinkFixer.onLoad(); //if imported dynamically
else addOnloadHook(LinkFixer.onLoad);