Jump to content

User:Samwilson/LinkedLintErrors.js

From Wikisource
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

( function ( mw, $ ) {

	function addToContentSub( el, newline = true ) {
		var contentSub = document.getElementById( 'contentSub' );
		if ( newline ) {
			contentSub.append( document.createElement( 'br' ) );
		}
		contentSub.append( el );
	}

	function run() {
		var lintErrorHeader = document.createElement( 'strong' );
		lintErrorHeader.innerText = 'Lint errors (may take a moment to load)...';
		addToContentSub( lintErrorHeader );
		// Get all pages linked to by this one.
		var links = document.querySelectorAll( '#mw-content-text a' );
		titleBatch = [ mw.config.get( 'wgPageName' ) ];
		links.forEach( function ( link, i ) {
			// Strip leading '/wiki/'.
			var title = link.pathname.substring( '/wiki/'.length );
			titleBatch.push( decodeURIComponent( title ) );
			if ( titleBatch.length === 50 || i === ( links.length - 1 ) ) {
				// If we've got a full batch or are at the end of the queue.
				fetchBatch( titleBatch );
				titleBatch = [];
			}
		} );
	}

	function fetchBatch( titles ) {
		var request = { action: 'query', titles: titles.join( '|' ) };
		new mw.Api()
			.get( request )
			.done( function ( result ) {
				addToContentSub( ' . ', false );
				if ( result && result.query && result.query.pages ) {
					for ( var pageId in result.query.pages ) {
						if ( pageId < 1 ) {
							continue;
						}
						processPage( pageId );
					}
				}
			} );
	}

	function processPage( pageId ) {
		var request = {
			action: "query",
			list: "linterrors",
			lntpageid: pageId
		}
		new mw.Api()
			.get( request )
			.done( function ( result ) {
				addToContentSub( ' . ', false );
				if ( result.query && result.query.linterrors ) {
					errorsFound = true;
					addErrorsToList( result.query.linterrors );
				}
			} );
	}

	function addErrorsToList( errors ) {
		errors.forEach( function ( error ) {
			console.log(error);
			var link = document.createElement( 'a' );
			link.href = '/w/index.php?curid=' + error.pageid + '&action=edit&lintid=' + error.lintId;
			link.text = 'Lint error ' + error.category + ': ' + error.title + ' (' + error.params.name + ')';
			link.target = '_blank';
			addToContentSub( link );
		} );
	}

	link = mw.util.addPortletLink(
		'p-tb',
		'#contentSub',
		'Linked Lint Errors',
		't-linkedlinterrors',
		'Show lint errors of this page and of pages it links to'
	);
	$( link ).on( 'click', run );

}( mediaWiki, jQuery ) );