User:Ahecht/useridentifier.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
Ahecht (talk | contribs)
debug
Ahecht (talk | contribs)
Lets see if this fails gracefully now
Line 153: Line 153:
list:'users',
list:'users',
ususers:name,
ususers:name,
usprop:'editcount|groups|registration|emailable|gender'
usprop:'editcount|groups|registration|emailable|gender',
meta:'globaluserinfo',
guiuser:name,
guiprop:'groups'
},
},
success:cb,
success:cb,

Revision as of 14:32, 12 May 2022

/* If you want to use this script, simply add the following line to your monobook.js:

importScript('User:Anomie/useridentifier.js'); // Linkback: [[User:Anomie/useridentifier.js]]

* (Please keep the comment so I can see how many people use this).
*/

var UserIdentifier={
    images:{
        '**':'//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Wikipedia_svg_logo.svg/20px-Wikipedia_svg_logo.svg.png',
        'bot':'//upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Crystal_Clear_action_run.png/20px-Crystal_Clear_action_run.png',
        'sysop':'//upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Mop.svg/20px-Mop.svg.png',
        'bureaucrat':'//upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Human-preferences-desktop.svg/20px-Human-preferences-desktop.svg.png',
        'checkuser':'//upload.wikimedia.org/wikipedia/commons/thumb/1/1e/Gnome-searchtool.svg/20px-Gnome-searchtool.svg.png',
        'reviewer':'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Wikipedia_Reviewer.svg/20px-Wikipedia_Reviewer.svg.png',
        'accountcreator':'//upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Wikipedia_Accountcreators_v2.svg/20px-Wikipedia_Accountcreators_v2.svg.png',
        'suppress':'//upload.wikimedia.org/wikipedia/commons/thumb/0/06/Oversight_logo.png/20px-Oversight_logo.png',
        'rollbacker':'//upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikipedia_Rollback.svg/20px-Wikipedia_Rollback.svg.png',
        'filemover':'//upload.wikimedia.org/wikipedia/commons/thumb/8/8d/Wikipedia_File_mover.svg/20px-Wikipedia_File_mover.svg.png',
        'autoreviewer':'//upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Wikipedia_Autopatrolled.svg/20px-Wikipedia_Autopatrolled.svg.png',
        'abusefilter':'//upload.wikimedia.org/wikipedia/commons/thumb/6/64/Wikipedia-Crystal_clear-advancedsetting.png/20px-Wikipedia-Crystal_clear-advancedsetting.png',
        'templateeditor':'//upload.wikimedia.org/wikipedia/commons/thumb/4/41/Wikipedia_Template_editor_icon_%281%29.svg/20px-Wikipedia_Template_editor_icon_%281%29.svg.png',
        'massmessage-sender':'//upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Wikipedia_mass_messenger.svg/20px-Wikipedia_mass_messenger.svg.png',
        'extendedmover':'//upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Wikipedia_page_mover.svg/20px-Wikipedia_page_mover.svg.png',
        'patroller':'//upload.wikimedia.org/wikipedia/commons/thumb/5/57/Wikipedia_New_page_reviewer.svg/20px-Wikipedia_New_page_reviewer.svg.png',
        'eventcoordinator': '//upload.wikimedia.org/wikipedia/commons/thumb/c/c6/Wikipedia_Event_coordinator.svg/20px-Wikipedia_Event_coordinator.svg.png',
        'interface-admin': '//upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Wikipedia_Interface_administrator.svg/20px-Wikipedia_Interface_administrator.svg.png'
    },
    gimages:{
        'steward':'//upload.wikimedia.org/wikipedia/commons/thumb/7/75/Wikimedia_Community_Logo.svg/20px-Wikimedia_Community_Logo.svg.png',
        'Global_rollback': '//upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Meta-Wiki_Global_rollback-2000px.png/20px-Meta-Wiki_Global_rollback-2000px.png',
        'global-rollbacker': '//upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Meta-Wiki_Global_rollback-2000px.png/20px-Meta-Wiki_Global_rollback-2000px.png',
        'global-sysop' : '//upload.wikimedia.org/wikipedia/commons/thumb/5/57/Meta-Wiki_Global_sysop-2000px.png/20px-Meta-Wiki_Global_sysop-2000px.png',
        'Global_sysops' : '//upload.wikimedia.org/wikipedia/commons/thumb/5/57/Meta-Wiki_Global_sysop-2000px.png/20px-Meta-Wiki_Global_sysop-2000px.png'
    },

    make_callback:function(name){
        var users=null;
        var globaluserinfo=null;
        var lastedit=null;
        return function(r){
            if(!r.query) { console.error('Bad response'); return; }
            if(r.query.users){
                if(r.query.users.length!=1) { console.error('Bad response'); return; }
                users=r.query.users[0];
            } else {
            	console.log('No user table found in query, assuming usercontribs:');
            	console.log(r);
            }
            if(r.query.usercontribs){
                lastedit=r.query.usercontribs.length?r.query.usercontribs[0].timestamp:'never';
            } else {
            	console.log('No usercontribs table found in query, assuming groups:');
            	console.log(r);
            }
            if(!users || !lastedit) return;
            r=users;
            if(r.name!=name) return;
            r.emailable=(typeof(r.emailable)!='undefined');
            var d=document.createElement('DIV');
            var d2=document.createElement('DIV');
            d2.style.display='inline-block';
            d2.style.styleFloat='left';
            d2.style.overflow='visible';
            d2.style.cursor='pointer';
            d2.className='metadata';
            d2.title='Registered: '+r.registration+'\nLast edit: '+lastedit+'\nEdits: '+r.editcount+'\nEmailable: '+(r.emailable?'yes':'no')+'\nGender: '+r.gender;
            d2.onclick=function(){ d.style.display=d.style.display?'':'none'; };
            d.style.display='none';
            d.style.position='absolute';
            d.style.background='#eef';
            d.style.border='1px solid black';
            d.style.padding='3px';
            d.style.zIndex=1000;
            d.appendChild(document.createTextNode('Registered: '+r.registration));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Last edit: '+lastedit));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Edits: '+r.editcount));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Emailable: '+(r.emailable?'yes':'no')));
            d.appendChild(document.createElement('BR'));
            d.appendChild(document.createTextNode('Gender: '+r.gender));
            if(r.groups && r.groups.length>0){
                d.appendChild(document.createElement('BR'));
                d.appendChild(document.createTextNode('Groups: '+r.groups.join(', ')));
                for(var j=r.groups.length-1; j>=0; j--){
                    if(UserIdentifier.images[r.groups[j]]){
                        var i=document.createElement('IMG');
                        i.src=UserIdentifier.images[r.groups[j]];
                        i.width=20;
                        i.height=20;
                        i.style.cursor='pointer';
                        i.title=r.groups[j];
                        d2.appendChild(i);
                    }
                }
                d2.title+='\nGroups: '+r.groups.join(', ');
            }
            if(globaluserinfo !== null && globaluserinfo.groups && globaluserinfo.groups.length>0){
                d.appendChild(document.createElement('BR'));
                d.appendChild(document.createTextNode('Global groups: '+globaluserinfo.groups.join(', ')));
                for(var j=globaluserinfo.groups.length-1; j>=0; j--){
                    if(UserIdentifier.gimages[globaluserinfo.groups[j]]){
                        var i=document.createElement('IMG');
                        i.src=UserIdentifier.gimages[globaluserinfo.groups[j]];
                        i.width=20;
                        i.height=20;
                        i.style.cursor='pointer';
                        i.title=globaluserinfo.groups[j];
                        d2.appendChild(i);
                    }
                }
                d2.title+='\nGlobal groups: '+globaluserinfo.groups.join(', ');
            }
            if(!d2.firstChild){
                var i=document.createElement('IMG');
                i.src=UserIdentifier.images['**'];
                i.width=20;
                i.height=20;
                i.style.cursor='pointer';
                d2.appendChild(i);
            }
            var i=document.createElement('SPAN');
            i.style.fontSize='16px';
            if(r.gender=='male'){
                i.appendChild(document.createTextNode('♂'));
                i.style.color='#89CFF0';
            } else if(r.gender=='female'){
                i.appendChild(document.createTextNode('♀'));
                i.style.color='#F4C2C2';
            }
            if(i.firstChild) d2.insertBefore(i, d2.firstChild);
            d.onclick=function(){ d.style.display='none'; };
            var h=document.getElementById('firstHeading');
            h.insertBefore(d2,h.firstChild);
            h.parentNode.insertBefore(d,h.nextSibling);
            if(window.CascadeTopicons) CascadeTopicons();
        };
    },

    IDUser:function(name){
        mw.loader.using('mediawiki.util', function(){
            name = decodeURIComponent(name).replace(/_/g, ' ');
            var cb=UserIdentifier.make_callback(name);
            $.ajax({
                url:mw.util.wikiScript('api'),
                dataType:'json',
                type:'POST',
                data:{
                    format:'json',
                    action:'query',
                    list:'users',
                    ususers:name,
                    usprop:'editcount|groups|registration|emailable|gender',
                    meta:'globaluserinfo',
                    guiuser:name,
                    guiprop:'groups'
                },
                success:cb,
                error:function(xhr,textStatus,errorThrown){
                    throw new Error('AJAX error: '+textStatus+' '+errorThrown);
                }
            });
            $.ajax({
                url:mw.util.wikiScript('api'),
                dataType:'json',
                type:'POST',
                data:{
                    format:'json',
                    action:'query',
                    list:'usercontribs',
                    ucuser:name,
                    ucprop:'timestamp',
                    uclimit:1
                },
                success:cb,
                error:function(xhr,textStatus,errorThrown){
                    throw new Error('AJAX error: '+textStatus+' '+errorThrown);
                }
            });
            mw.util.addPortletLink('p-cactions',mw.util.wikiScript()+'?title=Special%3AContributions&target='+encodeURIComponent(name),'Contribs');
            mw.util.addPortletLink('p-cactions',mw.util.wikiScript()+'?title=Special%3ACentralAuth/'+encodeURIComponent(name),'SUL');
        });
    },
 
    onLoad:function(){
        if(mw.config.get('wgNamespaceNumber')==2 || mw.config.get('wgNamespaceNumber')==3){ 
            var u=mw.config.get('wgTitle');
            var i=u.indexOf('/');
            if(i>0) u=u.substr(0,i);
            UserIdentifier.IDUser(u);
        } else if(mw.config.get('wgCanonicalNamespace')=='Special' && mw.config.get('wgCanonicalSpecialPageName')=='Contributions'){
            var contentSub = document.getElementById('contentSub');
            var contentSubLink = contentSub && contentSub.getElementsByTagName('A')[0]
            var a=contentSubLink && contentSubLink.href;
            var u= a && a.match(/\/wiki\/User:([^\/]+)/);
            if(u) UserIdentifier.IDUser(u[1]);
            u=a.match(/[?&]title=User:([^&]+)/);
            if(u) UserIdentifier.IDUser(u[1]);
        } else if(mw.config.get('wgCanonicalNamespace')=='Special' && mw.config.get('wgCanonicalSpecialPageName')=='Log'){
            var u=['', $('#mw-input-user input')[0].defaultValue];
            if(!u[1]) u=$('#mw-input-page input')[0].defaultValue.match(/^User:([^\/]+)/);
            if(u) UserIdentifier.IDUser(u[1]);
        }
    }
};

$(document).ready(UserIdentifier.onLoad);