User:Mbch331/Gadget-AuthorityControl.js
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.
/* AuthorityControl.js
* Provides a link to various Authority Control tools (VIAF, GND, etc.) for Wikidata statements that
* feature certain properties.
*
* Original gadget coded by [[User:Ricordisamoa]]
*/
( function ( mw, wb, $ ) {
'use strict';
if ( ( mw.config.get( 'wgNamespaceNumber' ) !== 0 && mw.config.get( 'wgNamespaceNumber' ) !== 120 ) || !mw.config.exists( 'wbEntityId' ) ) {
// Only item pages feature appropriate statements.
return;
}
var PROPERTIES = {};
/*
*/
function getGeoHackParams( coord ) {
// TODO: individual scale for every precision
var globes = {
Q2: 'earth',
Q111: 'mars',
Q308: 'mercury',
Q313: 'venus',
Q405: 'moon',
Q15034: 'mimas'
};
/* BUG: .getGlobe() returns Q2 always */
var globeQKey = coord.globe.replace( 'http://www.wikidata.org/entity/', '' );
var globe = globes[ globeQKey ];
return coord.latitude + '_N_' + coord.longitude + '_E_globe:' + globe;
}
/**
* Get the snak value formatted with a link.
*
* @param {number} numericPropertyId Refers to PROPERTIES.
* @param {string} value
*/
function getLinkValueForString( numericPropertyId, value ) {
var linkValue;
switch ( Number( numericPropertyId ) ) {
case 213: // ISNI
linkValue = value.replace( / /g, '' );
break;
case 345: // IMDb
linkValue = '/' + value;
switch ( value.substring( 0, 2 ) ) {
case 'nm':
linkValue = 'name' + linkValue + '/';
break;
case 'tt':
linkValue = 'title' + linkValue + '/';
break;
case 'ch':
linkValue = 'character' + linkValue + '/';
break;
case 'ev':
linkValue = 'event' + linkValue;
break;
case 'co':
linkValue = 'company' + linkValue + '/';
break;
}
break;
case 502: // HURDAT
linkValue = value.substring( 4, 8 ); // year
if ( Number( linkValue ) <= 2005 ) {
linkValue += '/'; // Brings a list for that year
// For direct link, would need to append <name of hurricane>.[s]htlm
// ex: http://www.nhc.noaa.gov/archive/2004/ALEX.shtml or 2001/ALLISON.html
} else { // after 2005
linkValue += '/' + value.substring( 0, 4 ).toLowerCase() + '/';
}
break;
case 628: // E Number
linkValue = value.substring( 1, 16 ); // Skip initial letter
break;
case 882: // FIPS
linkValue = value;
linkValue = linkValue.substr( 0, 2 ) + '/' + linkValue;
break;
case 1323: // TA98
linkValue = value.substring( 1, 16 ); // Skip initial letter
break;
default:
linkValue = value;
}
return linkValue;
}
function makeLink( numericPropertyId, linkValue, displayText ) {
var linkTemplate = PROPERTIES[ numericPropertyId ];
switch ( Number( numericPropertyId ) ) {
case 426:
if ( linkValue.substring( 0, 1 ) === 'N' ) {
linkTemplate = 'http://registry.faa.gov/aircraftinquiry/NNum_Results.aspx?NNumbertxt=$1';
} else if ( linkValue.substring( 0, 2 ) === 'G-' ) {
linkTemplate = 'https://www.caa.co.uk/application.aspx?catid=60&pagetype=65&appid=1&mode=detailnosummary&fullregmark=$1';
linkValue = linkValue.substring( 2 );
} else {
return $( '<span>' )
.text( displayText );
}
break;
default:
linkTemplate = PROPERTIES[ numericPropertyId ];
}
return $( '<a>' )
.text( displayText )
.attr( 'href', linkTemplate.replace( /\$1/g, linkValue ) )
// Show the 'external link' icon:
.addClass( 'external' );
}
function createLinkForString( numericPropertyId, value ) {
var linkValue = getLinkValueForString( numericPropertyId, value );
return makeLink( numericPropertyId, linkValue, value );
}
function createLinkForSnakValue( numericPropertyId, datavalue, displayText ) {
var dataValueType = datavalue.type,
value = datavalue.value;
// @fixme shouldn't happen but in case of any unexpected data value types,
// then there should be better error handling here.
var linkValue = '';
if ( dataValueType === 'string' ) {
linkValue = getLinkValueForString( numericPropertyId, value );
} else if ( dataValueType === 'globecoordinate' ) {
linkValue = getGeoHackParams( value );
}
return makeLink( numericPropertyId, linkValue, displayText );
}
function linkSnakView( el, propertySelector, valueSelector ) {
var $propLink = $( el ).find( propertySelector );
var title = $propLink.attr( 'title' );
if ( title ) {
var titleParts = title.split( ':P' ),
numericPropertyId = titleParts[ 1 ];
if ( PROPERTIES.hasOwnProperty( numericPropertyId ) ) {
var $value = $( el ).find( valueSelector ).first(),
$link = createLinkForString( numericPropertyId, $value.text() );
$value.html( $link );
}
}
}
/**
* Initializes the gadget.
* This procedure needs to be performed as good as possible. jQuery selector usage should be limited
* to a minimum.
*/
function initGadget() {
$( ':wikibase-statementview' ).each( function () {
var statementview = $.data( this, 'statementview' ),
statement = statementview.value(),
mainSnak = statement.getClaim().getMainSnak(),
numericPropertyId = mainSnak.getPropertyId().slice( 1 ),
snakview = statementview.$mainSnak.data( 'snakview' ),
datavalue = snakview.value().datavalue;
if ( datavalue && PROPERTIES.hasOwnProperty( numericPropertyId ) ) {
var displayText = snakview.$snakValue.text(),
snakLink = createLinkForSnakValue( numericPropertyId, datavalue, displayText );
snakview.$snakValue.html( snakLink );
}
} );
/*
@fixme this works for main snak, except for coordinates which need the data value
$( '.wikibase-statementgroupview' ).each( function () {
linkSnakView( this, '.wikibase-statementgroupview-property-label > a', '.wikibase-snakview-value' );
} );
*/
$( '.wikibase-statementview-qualifiers' ).each( function () {
linkSnakView( this, '.wikibase-snakview-property > a', '.wikibase-snakview-value' );
} );
$( '.wikibase-referenceview .wikibase-snaklistview-listview' ).each( function () {
linkSnakView( this, '.wikibase-snakview-property > a', '.wikibase-snakview-value' );
} );
}
function getProperties() {
var api = new mw.Api(),
repoApi = new wb.api.RepoApi( api ),
claim,
claims = [],
entity = JSON.parse( mw.config.get( 'wbEntity' ) );
for ( claim in entity.claims ) {
claims.push( claim );
$.each( entity.claims[ claim ], function ( i, cl ) {
$.each( cl.references || [], function ( i, ref ) {
for ( claim in ref.snaks ) {
if ( claims.indexOf( claim ) === -1 ) {
claims.push( claim );
}
}
} );
for ( claim in cl.qualifiers || {} ) {
if ( claims.indexOf( claim ) === -1 ) {
claims.push( claim );
}
}
} );
}
if ( !claims.length ) {
return $.Deferred().resolve();
}
return repoApi.getEntities( claims, 'claims' )
.done( function ( data ) {
$.each( data.entities, function ( entityId, entity ) {
$.each( entity.claims, function ( claimId, claim ) {
if ( claimId === 'P1630' ) {
PROPERTIES[ entityId.slice( 1 ) ] = claim[ 0 ].mainsnak.datavalue.value;
}
} );
} );
} );
}
getProperties().done( function () {
$( initGadget );
} );
}( mediaWiki, wikibase, jQuery ) );