User:Pcj/common.js

// LookupUser fixes $(function {	if (mw.config.get('wgCanonicalSpecialPageName') !== 'LookupUser') return;	var prom = [];	$('div.lookup-user__table-wrapper > table > tbody > tr > td:nth-child(2) > a:first-child').each(function{ var baseUrl = $(this).text.trim; prom.push($.getJSON(baseUrl+'api.php?action=query&meta=siteinfo&sipropr=variables&format=json&callback=?')); });	Promise.allSettled(prom).then(function(res) { for (var i in res) { if (res[i].status !== 'rejected') continue; // just assume if we have an error the wiki is closed. // does not consider further in the event the wiki is *otherwise* unreachable, // possible exceptions: behind the VPN, some error state in the API, server error, etc.			$('div.lookup-user__table-wrapper > table > tbody > tr:eq('+i+') > td:nth-child(1)').wrapInner(' ').append(' Closed '); }	}); });

// WikiConfig fixes mw.loader.using('jquery.makeCollapsible').then(function{	if (mw.config.get('wgCanonicalSpecialPageName') !== 'WikiConfig') return;	console.log('test');	$('.wikiconfig-changelog-table tr').each(function { if ($(this).find('.TablePager_col_cl_text .v1').text.length < 500 	   	&& $(this).find('.TablePager_col_cl_text .v2').text.length < 500	    	&& $(this).find('.TablePager_col_cl_text > pre').text.length < 500) return; $(this).find('.v1,.v2,pre').wrapAll(' '); });	$('.mw-collapsible').makeCollapsible; });

/** * @name: Adoptions * @description: Form for adoptions * @author: Pcj (based on work by Unai01, Lil' Miss Rarity, Jr Mime and bola) */ mw.loader.using(['jquery.client', 'mediawiki.base','mediawiki.api']).then(function {   var userName = mw.config.get('wgUserName');    if (mw.config.get('wgPageName') !== 'Adoption:Requests' || !userName) {        return;    }

mw.hook('dev.modal').add(function(modal) {       var adoptionModal = new window.dev.modal.Modal({ title: 'Adoption Request', content: '  Link  Wiki Name   Number of days edited in the past 10 days   Number of admins active in the past 60 days  Comments/Reasons to Adopt  ', id: 'requestWindow', size: 'large', buttons: [{ id: 'submitButton', text: 'Submit', primary: true, event: 'submitForm' }],           closeTitle: 'Cancel', events: { submitForm: function { var $form = $('#adoption'), wikiname = $form.find('#wikiname').val, url = $form.find('#adoptionUrl').val, numDays = $form.find('#numDays').val || 0, numAdmins = $form.find('#numAdmins').val || 0, comments = $form.find('#comment').val; if (url.trim === "") { mw.notify('Please enter the wiki URL.',{tag:'adoption',type:'warn'}); return; }                   if (wikiname.trim === "") { mw.notify('Please enter the wiki name.',{tag:'adoption',type:'warn'}); return; }                   if (comments.trim === "") { mw.notify('Please try to leave some rationale as to why you want to adopt this wiki and why you would be a good fit as admin.',{tag:'adoption',type:'warn'}); return; }                   var pagecontent = "\n\n" + "What is your username?\n" + userName + "\n\nPlease link to the wiki here:\n" + url + "\n\nHow many days have you edited the wiki in the past 10 days?\n" + numDays + "\n\nOn the Special Pages  → Special:ListAdmins, how many admins have been active in the past 60 days?\n" + numAdmins + "\n\nComments/Reasons for adoption:\n" + comments + "\n\n\n"; adoptionModal.hide; new mw.Api.get({                       action: 'query',                        list: 'allpages',                        apnamespace: 118,                        apprefix: wikiname,                        aplimit: 'max'                    }).done(function (data) {                        var suffix = '';                        var highestAdoption = 0;                        var suffixRE = /.*\((\d+)\)/;                        if (data.query) {                            if (data.query.allpages.length > 0) highestAdoption = 1;                            for (p in data.query.allpages) {                                if (data.query.allpages[p].title == undefined) continue;                                var match = data.query.allpages[p].title.match(suffixRE);                                if (!match) continue;                                if (parseInt(match[1]) > highestAdoption) highestAdoption = parseInt(match[1]); }                           if (highestAdoption > 0) suffix = ' ('+(highestAdoption+1)+')'; }                       new mw.Api.postWithEditToken({                            action: 'edit',                            title: "Adoption:"+wikiname+suffix,                            text: pagecontent                        }).done(function  {                            location.href = mw.util.getUrl("Adoption:"+wikiname+suffix);                        }).fail(function  {                            mw.notify('There were problems submitting your request.', {tag:'adoption',type:'error'});                        }); }).fail(function { mw.notify('There were problems submitting your request.', {tag:'adoption',type:'error'}); });               }            }        });        adoptionModal.create; $('.WikiaMainContentContainer #adoptions') .attr('class', 'wds-button btn-large') .text('Request an adoption') .wrap($(' ').css('text-align', 'center')) .css('cursor', 'pointer') .on('click', function {               adoptionModal.show;            }); });

importArticle({       type: 'script',        article: 'u:dev:MediaWiki:Modal.js'    }); $('body').off('change.adoptionURL').on('change.adoptionURL','#adoptionUrl',function {       $('.adoptionPrefill').prop('disabled',true);        function filterFandomDomain(input) {            var fandomDomainRE = /(?:https?:\/\/)?(.*?\.)(gamepedia\.com|wikia\.org|fandom\.com)(\/[^\/]*?)?(?:\/.*)?$/;            var filteredDomain = input.match(fandomDomainRE);            if (!filteredDomain) return null;            filteredDomain.splice(0,1);            if (filteredDomain[2] === '/wiki' || filteredDomain[2] === '/f') filteredDomain.pop;            return filteredDomain.join('');        }        var url = filterFandomDomain($('#adoptionUrl').val);        if (!url) {            mw.notify('The format of the URL provided was not recognized.',{tag:'adoption',type:'error'});            $('.adoptionPrefill').prop('disabled',false);            return;        }        $.getJSON('//'+url+'/api.php?format=json&callback=?',{ action:'query', meta:'siteinfo', siprop:'general|statistics', list:'allusers|usercontribs|users', uclimit:'max', ucuser:userName, ucnamespace:0, ucdir:'newer', ucstart:Math.floor((new Date.getTime-864000000)/1000), // edits by user in the last 10 days augroup:'sysop|bureaucrat', aulimit:'max', auwitheditsonly:1, // avoid auactiveusersonly usprop:'groups', ususers:userName }).done(function(data) { if (!data.query) { mw.notify('The wiki did not respond to an automated query. You will have to fill out the requested values.',{tag:'adoption',type:'error'}); $('.adoptionPrefill').prop('disabled',false); return; }           if (data.query.users) { if (data.query.users[0]) { if (data.query.users[0].groups) { if (data.query.users[0].groups.indexOf('sysop') > -1 || data.query.users[0].groups.indexOf('bureaucrat') > -1) { mw.notify('You are already an admin on this wiki. Please keep in mind you do not need to adopt a wiki you are already admin for unless you are applying to be a bureaucrat.',{tag:'adoption',type:'warn'}); }                   }                }            }            var ucDays = 0; if (data.query.usercontribs) { var ucDArr = []; for (u in data.query.usercontribs) { var ucDay = data.query.usercontribs[u].timestamp.slice(0,10); if (ucDArr.indexOf(ucDay) == -1) ucDArr.push(ucDay); }               ucDays = ucDArr.length; if (ucDays < 5) { mw.notify('Please keep in mind you should have contributed consistently to the wiki for a week prior to submitting a request.',{tag:'adoption',type:'warn'}); }           }            if (data.query.allusers) { var usProm = []; for (u in data.query.allusers) { usProm.push($.getJSON('//'+url+'/api.php?format=json&callback=?',{ action:'query', list:'usercontribs', uclimit:1, ucuserids:data.query.allusers[u].userid, ucend: Math.floor((new Date.getTime-5184000000)/1000) // 60 days }));               }                Promise.allSettled(usProm).then(function(usdata) {                    var numAdmins = 0;                    for (d in usdata) {                        if (usdata[d].value.query) {                            if (usdata[d].value.query.usercontribs.length > 0) numAdmins++;                        }                    }                    if (numAdmins > 0) {                        mw.notify('Please keep in mind if there are already active admins you should contact them first regarding becoming admin yourself.', {tag:'adoption',type:'warn'});                    }                    $('#numAdmins').val(numAdmins);                }); }           $('#wikiname').val(data.query.general.sitename); $('#numDays').val(ucDays); }).fail(function(data){ mw.notify('The wiki did not respond to an automated query. You will have to fill out the requested values.',{tag:'adoption',type:'error'}); $('.adoptionPrefill').prop('disabled',false); return; });   }); });