User:Ricordisamoa/ScriptErrors.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Links to a category listing all pages with Scribunto errors on the current wiki.
 * @author [[User:Ricordisamoa]]
 */
$( function () {
	var msgName = 'scribunto-common-error-category',
	api = new mw.Api();
	api.get( {
		meta: 'allmessages',
		amlang: mw.config.get( 'wgContentLanguage' ),
		ammessages: msgName,
		amenableparser: true
	} )
	.done( function ( data ) {
		var catName = null;
		$.each( data.query.allmessages, function ( i, v ) {
			if ( v.name === msgName ) {
				catName = v['*'];
			}
		} );
		if ( typeof catName !== 'string' ) {
			return;
		}
		api.get( {
			titles: mw.config.get( 'wgFormattedNamespaces' )[14] + ':' + catName,
			prop: 'categoryinfo'
		} )
		.done( function ( data ) {
			if ( data.query && data.query.pages ) {
				var pages = data.query.pages;
				if ( Object.keys( pages ).length === 1 ) {
					var page = pages[Object.keys( pages )[0]];
					if ( page.categoryinfo && page.categoryinfo.size ) {
						var linkText = catName + ' (' + page.categoryinfo.size + ')';
						$( mw.util.addPortletLink( 'p-navigation', mw.util.getUrl( page.title ), linkText, 'scripterrors', 'Go to ' + page.title ) )
						.children( 'a' )
						.addClass( page.missing ? 'new' : '' );
					}
				}
			}
		} );
	} );
} );