User:Matěj Suchánek/checkSitelinks.js

From Wikidata
Jump to navigation Jump to search

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.
/**
 * Adds an icon to sitelinks that link to a disambiguation or a redirect.
 *
 * To use it add this to your common.js:
 * // [[User:Matěj Suchánek/checkSitelinks.js]]
 * mw.loader.load( '//www.wikidata.org/w/index.php?title=User:Matěj_Suchánek/checkSitelinks.js&action=raw&ctype=text/javascript' );
 *
 * You are welcome to suggest more special cases this script could indicate.
 *
 * Author: Matěj Suchánek
 * Idea: ValterVB
 */

( function ( mw, $ ) {

	if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) {
		return;
	}

	var sitelinks;

	function checkSitelinks( $sitelinkgroupview ) {
		var group = $sitelinkgroupview.data( 'wb-sitelinks-group' );
		$( '.disambig-icon.sitelink-icon-' + group )
			.addClass( 'sitelink-icon-hidden' )
			.removeClass( 'disambig-icon' );
		$( '.redirect-icon.sitelink-icon-' + group )
			.addClass( 'sitelink-icon-hidden' )
			.removeClass( 'redirect-icon' );
		$sitelinkgroupview.find( '.wikibase-sitelinkview.listview-item' ).each( function() {
			var $this = $( this ),
				wiki = $this.data( 'wb-siteid' );
			if ( wiki === 'mediawikiwiki' || wiki === 'wikidatawiki' ) {
				return;
			}
			if ( wiki in sitelinks ) {
				var site = wb.sites.getSite( wiki ),
					wikiApi = new mw.ForeignApi( site.getApi() );
				wikiApi.get( {
					action: 'query',
					indexpageids: '',
					ppprop: 'disambiguation',
					prop: 'pageprops',
					redirects: '',
					titles: sitelinks[wiki].title
				} )
				.then( function( data ) {
					var icon = $this.find( '.sitelink-icon-' + group );
					if ( 'redirects' in data.query ) {
						icon.addClass( 'redirect-icon' ).removeClass( 'sitelink-icon-hidden' );
					} else if ( 'pageprops' in data.query.pages[data.query.pageids[0]] ) {
						icon.addClass( 'disambig-icon' ).removeClass( 'sitelink-icon-hidden' );
					}
				} );
			}
		} );
	}

	var entityLoaded = $.Deferred(),
		rendered = $.Deferred();

	function init() {
		$( '.wikibase-sitelinkgroupview' ).each( function () {
			var $this = $( this ),
				any = false,
				group = $this.data( 'wb-sitelinks-group' );
			$this.find( '.wikibase-sitelinkview' ).each( function () {
				any = true;
				$( this ).append(
					$( '<span>' )
					.addClass( 'sitelink-icon sitelink-icon-hidden sitelink-icon-' + group )
				);
			} );

			if ( any ) {
				$this.find( '.wikibase-edittoolbar-container' ).append(
					$( '<span>' )
					.addClass( 'checker' )
					.append(
						'[',
						$( '<a>' )
						.attr( 'href', '#' )
						.text( 'Check sitelinks!' )
						.click( function ( event ) {
							event.preventDefault();
							checkSitelinks( $this );
						} ),
						']'
					)
				);
			}
		} );
	}

	var initialized = $.when(
		mw.loader.using( [ 'wikibase.sites', 'mediawiki.ForeignApi' ] ),
		$.ready,
		entityLoaded,
		rendered
	).then( function () {
		init();
	} );

	mw.hook( 'wikibase.entityPage.entityView.rendered' ).add( function () {
		mw.loader.load( '//www.wikidata.org/w/index.php?title=User:Matěj_Suchánek/checkSitelinks.css&action=raw&ctype=text/css', 'text/css' );
		rendered.resolve();
	} );

	mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entity ) {
		sitelinks = entity.sitelinks || {};
		entityLoaded.resolve();
		$.each( entity.claims.P31, function ( i, claim ) {
			if (
				claim.mainsnak.datavalue &&
				claim.mainsnak.datavalue.value.id == 'Q4167410'
			) {
				initialized.then( function () {
					$( '.wikibase-sitelinkgroupview' ).each( function () {
						checkSitelinks( $( this ) );
					} );
				} );
				return false;
			}
		} );
	} );

} ( mediaWiki, jQuery ) );