User:Ricordisamoa/PropertyNames.js

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* <nowiki>
 *
 * PropertyNames.js
 * @author [[User:Ricordisamoa]]
 * Replaces the 'raw' entity ids with *useful* and *named* links in history and diff pages
 * --------------- OBSOLETE: see https://phabricator.wikimedia.org/T55247 ---------------
*/
$( document ).ready( function () {
	if (
		mw.config.get( 'wgNamespaceNumber' ) === 0 &&
		( mw.config.get( 'wgAction' ) === 'history' || $( 'table.diff' ).length > 0 )
	) {
		var ids = {};
		$( '.comment a' ).each( function () {
			var $this = $( this ),
				text = $this.text(),
				title = $this.attr( 'title' );
			if ( title === text && /^(Property:P|Q)\d+$/.test( text ) ) {
				var entityId = text.split( ':' ).slice( -1 )[0];
				if ( ids[entityId] === undefined ) {
					ids[entityId] = [ $this ];
				} else {
					ids[entityId].push( $this );
				}
			}
		} );
		var keys = Object.keys( ids );
		if ( keys.length > 0 ) {
			var userLang = mw.config.get( 'wgUserLanguage' );
			new mw.Api().get( {
				action: 'wbgetentities',
				ids: keys.join( '|' ),
				props: 'labels',
				languages: userLang,
				languagefallback: 1
			} )
			.done( function ( data ) {
				$.each( data.entities, function ( id, entity ) {
					if ( entity.labels !== undefined ) {
						var label = entity.labels[userLang];
						if ( label !== undefined && label.value !== undefined ) {
							$.each( ids[id], function () {
								this.text( label.value );
							} );
						}
					}
				} );
			} );
		}
	}
} );