User:Ricordisamoa/Wikispecies.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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 *
 * Wikispecies.js
 * @author [[User:Ricordisamoa]]
 * Provides a link to Wikispecies for elements with 'taxon name' (Property:P225)
 * ...and an image for the IUCN conservation status (Property:P141)
*/
/* global $, mw */
$( document ).ready( function () {
	'use strict';

	var entity, claims, nameProp, statusProp, name, wikispeciesApi,
		codes, $claims;

	mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entity ) {
		// only on entities
		if ( entity.claims === undefined || entity.claims.length === 0 ) {
			return;
		}
		claims = entity.claims;
		nameProp = 'P225';
		statusProp = 'P141';
		if (
			claims[ nameProp ] &&
			claims[ nameProp ].length === 1 &&
			claims[ nameProp ][ 0 ].mainsnak &&
			claims[ nameProp ][ 0 ].mainsnak.datavalue &&
			claims[ nameProp ][ 0 ].mainsnak.datavalue.value
		) {
			// 'taxon name' is present
			name = claims[ nameProp ][ 0 ].mainsnak.datavalue.value;
			wikispeciesApi = new mw.Api( {
				ajax: {
					url: '//species.wikimedia.org/w/api.php',
					dataType: 'jsonp'
				}
			} );
			wikispeciesApi.get( {
				list: 'allpages',
				apfrom: name,
				aplimit: 1,
				prop: ''
			} )
			.done( function ( data ) {
				var titleExists = (
					data &&
					data.query &&
					data.query.allpages.length === 1 &&
					data.query.allpages[ 0 ].title === name
				);
				$( '<a>' )
				.attr( 'href', '//species.wikimedia.org/wiki/' + mw.util.wikiUrlencode( name ) )
				.append(
					$( '<img>' )
					.attr( {
						src: '//upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikispecies-logo.svg/22px-Wikispecies-logo.svg.png',
						title: 'Wikispecies'
					} )
					.css( {
						opacity: titleExists ? 1 : 0.5,
						marginTop: '-.4em'
					} )
				)
				.wrap( '<span>' ).parent()
				.wrap( '<li>' ).parent()
				.prependTo( '#p-views ul' );
			} );
		}
		if ( claims[ statusProp ] ) {
			// 'IUCN conservation status' is present
			codes = {
				211005: 'LC',// least concern
				719675: 'NT',// near threatened
				278113: 'VU',// vulnerable
				11394: 'EN', // endangered
				219127: 'CR',// critically endangered
				239509: 'EW',// extinct in the wild
				237350: 'EX' // extinct
			};
			$claims = $( '.wikibase-statementview' );
			$.each( claims[ statusProp ], function () {
				var value, code, imagename,
					claim = this;
				if (
					claim.id &&
					claim.mainsnak &&
					claim.mainsnak.datavalue &&
					claim.mainsnak.datavalue.value &&
					claim.mainsnak.datavalue.value[ 'numeric-id' ]
				) {
					value = claim.mainsnak.datavalue.value[ 'numeric-id' ];
					code = codes[ value ];
					imagename = mw.util.wikiUrlencode(
						value === 3245245 ? 'Status none DD.svg' : ( 'Status iucn3.1 ' + code + '.svg' )
					);
					$claims
					.filter( function () {
						return $( this ).hasClass( 'wikibase-statement-' + claim.id );
					} )
					.find( '.wikibase-statementview-mainsnak .wikibase-snakview' ).first()
					.append(
						$( '<a>' )
						.attr( 'href', '//commons.wikimedia.org/wiki/File:' + imagename )
						.append(
							$( '<img>' )
							.attr( 'src', '//commons.wikimedia.org/wiki/Special:Filepath/' + imagename )
						)
					);
				}
			} );
		}
	} );
} );