User:Magnus Manske/ac2wd.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.
// <nowiki>
/*
This script adds an "AC2WD" link in the tools sidebar. 
When you click on it, it uses the [https://ac2wd.toolforge.org AC2WD] tool to check the item for certain Authority Control IDs (eg VIAF).
It then checks these AC datasets for statements (and more AC IDs).
It will then add any new information it found as new statements, or add more references to existing statements where possible.
A green checkmark will be appended to the link if data was added (reload the page to see), otherwise a "&mdash;" if no new data was available.

TO ACTIVATE, ADD

importScript( 'User:Magnus_Manske/ac2wd.js' ); // [[User:Magnus Manske/ac2wd.js]]

TO YOUR [[Special:MyPage/common.js]] USER SUBPAGE.
*/
var ac2wd = {
	initialized : false ,
	has_portlet_link : false ,
	
	// Update from https://ac2wd.toolforge.org/supported_properties
	supported_properties : ["P214","P227","P244","P268","P269","P906","P950","P1006"],

	init : function () {
		this.initialized = true ;
		let query_string = "#"+this.supported_properties.join(", #");
		if ( $(query_string).length==0 ) return; // No useful properties, don't show link
		this.add_portlet_link();
	} ,
	
	add_portlet_link : function () {
		let self = this;
		if ( self.has_portlet_link ) return ;
		self.has_portlet_link = true ;
		let portletLink = mw.util.addPortletLink( 'p-tb', '#', 'AC2WD','t-wd_ac2wd');
		$(portletLink).click ( function () {
			self.run() ;
			return false ;
		} ) ;
	},

	run : function () {
		let self = this ;
		$('#t-wd_ac2wd').css({'background-color':'#6094DB'});
		let item_id = mw.config.get("wbEntityId");
		$.getJSON ( "https://ac2wd.toolforge.org/extend/"+item_id , function ( new_data ) {
			if ( Object.keys(new_data).length === 0 ) {
				$('#t-wd_ac2wd').css({'background-color':''});
				$('#t-wd_ac2wd').append("<span title='No new data added'>&nbsp;&mdash;</span>");
				return;
			}

			let payload = {
				action: 'wbeditentity',
				id: item_id,
				data: JSON.stringify(new_data) ,
				summary: "Import of Authority Control data via https://ac2wd.toolforge.org"
			} ;

			let api = new mw.Api();
			api.postWithToken("csrf", payload ).done(function( token_data ) {
				//console.log(token_data) ;
				//console.log("Edit done")
				$('#t-wd_ac2wd').css({'background-color':''});
				$('#t-wd_ac2wd').append("<span style='color:#1FCB4A' title='New data added'>&nbsp;✓</span>");
			} ).fail( function(code, token_data) {
				console.log(code,token_data);
				console.log( api.getErrorMessage( token_data ).text());
				$('#t-wd_ac2wd').css({'background-color':''});
				$('#t-wd_ac2wd').append("<span title='An error occurred'>&nbsp;🚫</span>");
			} );

		} ) ;
	}

	
} ;

function try_ac2wd() {
	if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
	if ( mw.config.get('wgAction') != 'view' ) return ;

	mw.loader.using( [ 'user.options' ] ).then( function () {
		ac2wd.init () ;
	} ) ;
}

$( function(){try_ac2wd();} ) ;
// </nowiki>