MediaWiki:Van-RedirectCategories.js

/** * Automatically redirect to local tracking categories on non-EN wikis when accessed from EN names * This is a Vanguard tool * * @author Rail  */   'use strict';
 * ( function {

const conf = mw.config.get( [       'wgContentLanguage',        'wgNamespaceNumber',        'wgFormattedNamespaces',        'wgTitle'    ] );

if ( conf.wgContantLanguage === 'en' || conf.wgNamespaceNumber !== 14 ) { return; }

const self = { api: new mw.Api,

/**        * Sorry, no API to extract all of them automatically - had to hard define */       trackingCategories: [ {               category: 'Pages containing omitted template arguments', id: 'post-expand-template-argument-category' },           {                category: 'Pages using duplicate arguments in template calls', id: 'duplicate-args-category' },           {                category: 'Pages with template loops', id: 'template-loop-category' },           {                category: 'Pages where template include size is exceeded', id: 'post-expand-template-inclusion-category' },           {                category: 'Noindexed pages', id: 'noindex-category' },           {                category: 'Pages with ignored display titles', id: 'restricted-displaytitle-ignored' },           {                category: 'Pages with too many expensive parser function calls', id: 'expensive-parserfunction-category' },           {                category: 'Pages where node count is exceeded', id: 'node-count-exceeded-category' },           {                category: 'Pages where expansion depth is exceeded', id: 'expansion-depth-exceeded-category' },           {                category: 'Pages with script errors', id: 'scribunto-common-error-category' },           {                category: 'Scribunto modules with errors', id: 'scribunto-module-with-errors-category' }       ],

/**        * Not very optimal but easier scalable */       isTrackingCategory: function { const trackingCatsMap = self.trackingCategories.map(               function( item ) {                    return item.category;                }            ); const titleRegex = new RegExp( trackingCatsMap.join( '|' ) + '(\\):)?$' );

return titleRegex.test( conf.wgTitle ); },

getTrackingCatKey: function( callback ) { var retKey;

self.trackingCategories.forEach( function( item ) {               // Don't ask                var pageTitle;                if ( conf.wgTitle.slice( -2 ) === '):' ) { pageTitle = conf.wgTitle.slice( 0, -2 ); } else { pageTitle = conf.wgTitle; }

if ( item.category === pageTitle ) { retKey = item.id; }           } );

return retKey; },

init: function { // Add a killswitch if ( mw.util.getParamValue( 'redirect' ) === 'no' || !self.isTrackingCategory ) { return; }

self.api.get( {               action: 'query',                formatversion: 2,                meta: 'allmessages',                ammessages: self.getTrackingCatKey,                amlang: conf.wgContentLanguage            } ).done( function( resp ) {                const localCategoryName = resp.query.allmessages[0].content;                const localNamespaceName = conf.wgFormattedNamespaces[14];

location.replace(                   mw.util.getUrl( localNamespaceName + ':' + localCategoryName )                ); } );       }    };

mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( self.init ); } );