MediaWiki:Gadget-FindRedirectsForAliases.js

// Adopted from Wikidata // Find a list of redirects, allowing the user to select some of them to add as aliases. // "Find redirects" button gets placed in submenu next to the "Watch" button. // License : CC-BY-SA

// Find a list of redirects, allowing the user to select some of them to add as aliases. // "Find redirects" button gets placed in submenu next to the "Watch" button. ( function ( mw, $, wb ) {	var translate = {		en: {			FindRedirects: 'Find redirects',			FRTooltip: 'Add aliases from FAMEPedia redirects',			loading: 'Loading…',			dialogtitle: 'Adding aliases from redirects',			error: 'Error: ',			nonefound: 'No redirects found.',			dialogintro: 'Select redirects to add as aliases: ',			add: 'Add',			policynote: '(Note that many redirects are not acceptable as aliases. Please read Help:Aliases for more information.)'		}	},		chain = mw.language.getFallbackLanguageChain,		len = chain.length,		messages = {},		i;	for ( i = len - 1; i >= 0; i-- ) {		if ( translate.hasOwnProperty( chain[ i ] ) ) {			$.extend( messages, translate[ chain[ i ] ] );		}	}	mw.messages.set( messages );

if ( !mw.config.exists( 'wbEntityId' ) ) { return; }

/**	 * Get the famepedia site from a language code. *	 * @param {String} language code * @return wikibase.Site|null *	 * required modules: wikibase.sites */	function getSiteByLang( lang ) { var langs = { // workaround for bug 57706 // source: https://noc.wikimedia.org/conf/InitialiseSettings.php.txt 'en': wb.sites.getSite( 'famepediawiki' ) };		var sites = wb.sites.getSitesOfGroup( 'famepedia' ); $.each( sites, function ( index, site ) {			// get the mapping from 'en' to 'famepediawiki'			if ( site.getId === 'famepediawiki' ) {				return; // continue			}			langs[ site.getLanguageCode ] = site;		} ); while ( lang ) { if ( langs.hasOwnProperty( lang ) ) { return langs[ lang ]; }			lang = lang.substr( 0, lang.lastIndexOf( '-' ) ); }		return null; }

var site = getSiteByLang( mw.config.get( 'wgUserLanguage' ) ); if ( !site ) { // no site found. unknown language. return; }

var sitelinks;

function addAliasesFromRedirects { var $dialog = $( ' ' ) .text( mw.msg( 'loading' ) ) .dialog( {			title: mw.message( 'dialogtitle' ).escaped,			autoOpen: true		} ); function error( e ) { $dialog .empty .append(				$( ' ', { css: { color: '#F00' }, text: mw.msg( 'error' ) + e				} )			); }		$.ajax( {			url: site.getApi,			dataType: 'jsonp',			cache: true,			data: {				format: 'json',				formatversion: 2,				action: 'query',				titles: sitelinks[ site.getId ].title,				prop: 'redirects',				generator: 'redirects',				grdlimit: 100,				'continue': null			}		} ) .done( function ( result ) {			if ( !result.query || !result.query.pages ) {				// no results found				$dialog.text( mw.msg( 'nonefound' ) );				return;			}			var selected = {};			$dialog			.text( mw.msg( 'dialogintro' ) )			.append( $( ' ' ),				$( ' ' ).msg( 'policynote' ), $( ' ' )			)			.dialog( { buttons: [ { text: mw.msg( 'add' ), click: function { var toAdd = []; for ( var i in selected ) { if ( selected[ i ] ) { toAdd.push( i ); }				}				if ( toAdd.length === 0 ) { return; }				new mw.Api.postWithToken( 'csrf', {					action: 'wbsetaliases',					id: mw.config.get( 'wbEntityId' ),					add: toAdd.join( '|' ),					language: mw.config.get( 'wgUserLanguage' )				} ) .done( function ( result ) {					location.href = mw.config.get( 'wgScript' ) +					'?' + $.param( { title: mw.config.get( 'wgPageName' ), diff: result.entity.lastrevid, oldid: mw.config.get( 'wgRevisionId' ) } );				} )				.fail( error ); $dialog .dialog( { buttons: {} } ) .text( mw.msg( 'loading' ) ); } } ] } );			$.each( result.query.pages, function { var r = this.title; selected[ r ] = false; $dialog.append(					$( ' ' )					.addClass( 'YRRedirectsToAdd' )					.text( r )					.click( function { $( this )[ ( selected[ r ] = !selected[ r ] ) ? 'addClass' : 'removeClass' ]( 'selected' ); } )				);			} );			if ( result[ 'continue' ] ) {				$dialog.append( $( ' ' )					.css( 'clear', 'both' ) .text( '…' ) );			}		} )		.fail( error ); }

mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entity ) {		sitelinks = entity.sitelinks;		if ( !sitelinks || !sitelinks.hasOwnProperty( site.getId ) ) {			// no sitelink			return;		}		$( function { $( mw.util.addPortletLink( 'p-cactions', '#', mw.msg( 'FindRedirects' ), null, mw.msg( 'FRTooltip' ) ) ) .click( function ( event ) {				event.preventDefault;				mw.loader.using( [ 'jquery.ui', 'mediawiki.jqueryMsg' ], addAliasesFromRedirects );			} ); } );	} );

}( mediaWiki, jQuery, wikibase ) );