User:Epìdosis/UseAsRef try.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.
/* *******************************************************************
			 _   _           ___      ______      __ 
			| | | |         / _ \     | ___ \    / _|
			| | | |___  ___/ /_\ \___ | |_/ /___| |_ 
			| | | / __|/ _ \  _  / __||    // _ \  _|
			| |_| \__ \  __/ | | \__ \| |\ \  __/ |  
			 \___/|___/\___\_| |_/___/\_| \_\___|_|  2.0.2
		
			use an external ID or other selected claims 
					as a reference for statements
	gadget written by User:Epìdosis and User:Bargioni - Oct 2021
******************************************************************* */

mw.loader.using('wikibase.Site').then(function () {
 ( function ( mw, wb, $ ) {

	// -------- object Claim

	function Claim(property, claim) {
		this.property = property;
		this.claim = claim;
		this.is_id = check_id;
		this.is_excluded_id = check_excluded_id;
		this.append_icon = append_icon;
	}
	
	function check_id() {
		// check if this.property is an external id
		var extid = this.claim[0].mainsnak.datatype == 'external-id'; // only check the first value
		return extid;
	}
	
	function check_excluded_id() {
		// check if this.property is an excluded id
		var excluded_ids = mw.UAR.config.excluded_ids; // ['P212', 'P213', 'P214', 'P236', 'P646', 'P2671', 'P791', 'P957', 'P2333', 'P7859'];
		return (excluded_ids.indexOf(this.property) > -1);
	}

	function append_icon(icon_type) {
		// a claim is always an array of one or more statements
		var order = 0;
		this.claim.forEach(statement => {
			var id = statement.id;
			id = '#' + id.replace(/\$/,'\\$'); // escape $, required by jQuery
			var icon = new Icon(icon_type, this.property, order);
			// $(id).find('.wikibase-statementview-mainsnak .wikibase-snakview-body').append(icon.html);
			$(id).find('.wikibase-statementview-mainsnak .wikibase-snakview-body').prepend(icon.html);
			order++;
		});
	}
	
	// -------- object Property

	function Property(property, claim) { // claim forse inutile
		this.property = property;
		this.claim = claim;
		this.is_valid_for_copy = check_valid_for_copy;
		// this.is_valid_for_paste = check_valid_for_paste;
	}
	
	function check_valid_for_copy() {
		// return true if this property is not an external id
		var included_statements = get_included_statements_from_config(); // ['P1343', 'P856', 'P1581', 'P8214', 'P7014', 'P7101', 'P973', 'P5715', 'P953', 'P2888', 'P8768', 'P1442', 'P1801'];
		return included_statements.indexOf(this.property) > -1;
	}
	
	// -------- object Icon

	function Icon(icon_type, prop, order) {
		// order is the occurrence # TODO
		var icon_copy = mw.UAR.config.icon_copy;
		var icon_paste = mw.UAR.config.icon_paste;
		this.type = icon_type;
		this.src = icon_copy;
		if (icon_type == 'paste') this.src = icon_paste;
		this.title = 'copy this as ref';
		if (icon_type == 'paste') this.title = 'paste copied ref';
		// this.style = "cursor:pointer; float:right; width:16px; border:3px solid yellow";
		this.style = "cursor:pointer; float:right; width:16px; user-select: none";
		if (this.type == 'paste') this.style += "; pointer-events:none";
		this.html = `<img src="${this.src}" 
					style="${this.style}" 
					title="${this.title}" 
					data-prop="${prop}"
					data-order="${order}"
					class="UAR_icon UAR_icon_${this.type}">`;
	}
	
	// -------- object Item
	
	function Item(json, qid) {
		this.json = json;
		this.Qid = qid;
		this.append_icons = append_icons;
	}

	function append_icons() {
		var claims = this.json.entities[this.Qid].claims;
		for (const [p, c] of Object.entries(claims)) {
			// append to external IDs and to included_statements
			var claim = new Claim(p, c);
			var prop = new Property(p, c);
			if ( (claim.is_id() && !claim.is_excluded_id()) || prop.is_valid_for_copy() ) {
				claim.append_icon('copy');
			}
			else {
				if (!claim.is_excluded_id())
					claim.append_icon('paste');
			}
		}
		$('.UAR_icon_paste').css('opacity','0.5');
		var ic = $('.UAR_icon_copy').length;
		var ip = $('.UAR_icon_paste').length;
		// console.log(`UAR: added ${ic} copy icons and ${ip} paste icons`); // test
	}
	
	// -------- object Clipboard
	
	function Clipboard (config, dpr, dor, statement) {
		this.dpr = dpr;
		this.dor = dor;
		this.statement = statement;
		this.value = clipboard_value(this,config.value);
		this.property_value = config.property_value;
		this.named_as = config.named_as ? qualifier_value(this,config.named_as) : '';
		this.pub_date = config.pub_date ? qualifier_value(this,config.pub_date) : '';
		this.archive_url = config.archive_url ? qualifier_value(this,config.archive_url) : '';
		this.archive_date = config.archive_date ? qualifier_value(this,config.archive_date) : '';
		this.language = config.language ? qualifier_value(this,config.language) : '';
		this.volume = config.volume ? qualifier_value(this,config.volume) : '';
		this.page = config.page ? qualifier_value(this,config.page) : '';
		this.section = config.section ? qualifier_value(this,config.section) : '';
		this.ref_type = clipboard_ref_type(this,config.ref_type);
		this.source = clipboard_source(this,config.source);
		
		mw.UAR.clipboard = this; // pass the new ref to the system clipboard
	}

	function clipboard_value(t,V) {
		if ( typeof(t.statement.mainsnak.datavalue.value) == 'object' ) t.statement.mainsnak.datavalue.value = t.statement.mainsnak.datavalue.value.id;
		if (!Array.isArray(V)) V = [V]; // cast V to array
		for (var i=0; i < V.length; i++) {
			var vv = V[i]; // Pnnn or 'value'
			if (vv != 'value' && t.statement.qualifiers && t.statement.qualifiers[vv]) 
				return t.statement.qualifiers[vv][0].datavalue.value.id;
			if (vv == 'value') return t.statement.mainsnak.datavalue.value;
		}
		return null;
	}
	function qualifier_value(t,P) {
		return t.statement.qualifiers && t.statement.qualifiers[P] ? t.statement.qualifiers[P][0].datavalue.value : null;
	}
	function clipboard_ref_type(t,RT) {
		if (RT == '') return null;
		if (RT.substr(0,1) == 'P') {
			if (t.statement.qualifiers && t.statement.qualifiers[RT])
				return t.statement.qualifiers[RT][0].datavalue.value;
			else return null;
		}
		else return RT; // Qnnn
	}
	function clipboard_source(t,S) {
		if (!S) return null;
		if (S.substr(0,1) == 'Q') return S;
		var sparql = `select ?P9073 ?P1629 where { optional { wd:${t.dpr} wdt:P9073 ?P9073 . } optional { wd:${t.dpr} wdt:P1629 ?P1629 . } }`;
		var cb = clipboard_source_cb.bind(t);
		makeSPARQLQuery('https://query.wikidata.org/sparql', sparql, cb);
	}

	function clipboard_source_cb(R) {
		var P1629;
		try {
			this.source = R.results.bindings[0].P9073.value.replace(/.+\//,'');
		}
		catch(e) {
			this.source = '';
			// mostrare messaggio di errore: non esiste P9073 per this.dpr
			mw.notify (`Error: ${this.dpr} doesn\'t have property P9073. Please, add it.`,
				{
					title: 'UseAsRef - Error',
					autoHide: false,
					type: 'error'
				}
			);

		}
		try { P1629 = R.results.bindings[0].P1629.value.replace(/.+\//,''); } 
		catch(e) {
			mw.notify (`Warning: ${this.dpr} doesn\'t have property P1629. Please, add it.`,
				{
					title: 'UseAsRef - Warning',
					autoHide: true,
					type: 'warn'
				}
			);
		}
		mw.UAR.clipboard = this;
	}
	
	function Reference() {
		this.init = init_reference;
		this.save = save_reference;
	}
	
	function init_reference() {
		var now = '+'+(new Date()).toISOString().substr(0,10)+'T00:00:00Z'; // WD doesn't handle hhmmss
		var source_qid = mw.UAR.clipboard.source ? mw.UAR.clipboard.source.substr(1) : ''; // the numeric part of Qnnnn
		if (source_qid) this.P248 = [
			{
				"snaktype": "value",
				"property": "P248", // source of info
				"datavalue": {
					"value": {
						"entity-type": "item",
						"numeric-id": source_qid
					},
					"type": "wikibase-entityid"
				}
			}
		];
		this.P813 = [
			{
				"snaktype": "value",
				"property": "P813", // stated on
				"datatype": "time",
				"datavalue": {
					"value": {
						"after": 0,
						"before": 0,
						"calendarmodel": "http://www.wikidata.org/entity/Q1985727",
						"precision": 11,
						"time": now,
						"timezone": 0
					},
					"type": "time"
				}
			}
		];
		if (mw.UAR.clipboard.property_value == 'original') {
			this[mw.UAR.clipboard.dpr] = [
				{
					"snaktype": "value",
					"property": mw.UAR.clipboard.dpr,
					"datavalue": {
						"value": mw.UAR.clipboard.value,
						"type": "string"
					},
					"datatype": "external-id"
				}
			];
		}
		else {
			this[mw.UAR.clipboard.property_value] = [
				{
					"snaktype": "value",
					"property": mw.UAR.clipboard.property_value,
						"datavalue": mw.UAR.clipboard.value.indexOf('Q') == 0 ? 
						{
							"value": {
								"entity-type": "item",
								"numeric-id": mw.UAR.clipboard.value.replace(/Q/,'')
							},
							"type": "wikibase-entityid"
						}
						:
						{
							"value": mw.UAR.clipboard.value,
							"type": "string"
						},
					"datatype": mw.UAR.clipboard.value.indexOf('Q') == 0 ? "wikibase-item" : "url"
				}
			];
		}
		if (mw.UAR.clipboard.named_as) {
			this.P1810 = [
				{
					"snaktype": "value",
					"property": "P1810", // named as
					"datatype": "string",
					"datavalue": {
						"value": mw.UAR.clipboard.named_as,
						"type": "string"
					},
					"datatype": "string"
				}
			];
		}
		if (mw.UAR.clipboard.pub_date) {
			this.P577 = [
				{
					"snaktype": "value",
					"property": "P577", // pub date
					"datavalue": {
						"value": mw.UAR.clipboard.pub_date,
						"type": "time"
					},
					"datatype": "time"
				}
			];
		}
		if (mw.UAR.clipboard.archive_url) {
			this.P1065 = [
				{
					"snaktype": "value",
					"property": "P1065", // archive url
					"datavalue": {
						"value": mw.UAR.clipboard.archive_url,
						"type": "string"
					},
					"datatype": "url"
				}
			];
		}
		if (mw.UAR.clipboard.archive_date) {
			this.P2960 = [
				{
					"snaktype": "value",
					"property": "P2960", // archive date
					"datavalue": {
						"value": mw.UAR.clipboard.archive_date,
						"type": "time"
					},
					"datatype": "time"
				}
			];
		}
		if (mw.UAR.clipboard.ref_type) {
			this.P3865 = [
				{
					"snaktype": "value",
					"property": "P3865", // ref type
					"datavalue": {
					"value": {
						"entity-type": "item",
						"numeric-id": typeof mw.UAR.clipboard.ref_type == 'object' ? 
							(mw.UAR.clipboard.ref_type['numeric-id'] ? mw.UAR.clipboard.ref_type['numeric-id'] : 0)
							: mw.UAR.clipboard.ref_type.replace(/Q/,'')
					},
						"type": "wikibase-entityid"
					},
				}
			];
		}
		if (mw.UAR.clipboard.language) {
			this.P478 = [
				{
					"snaktype": "value",
					"property": "P407", // language
					"datavalue": {
					"value": {
						"entity-type": "item",
						"numeric-id": typeof mw.UAR.clipboard.language == 'object' ? 
							(mw.UAR.clipboard.language['numeric-id'] ? mw.UAR.clipboard.language['numeric-id'] : 0)
							: mw.UAR.clipboard.language.replace(/Q/,'')
					},
						"type": "wikibase-entityid"
					},
				}
			];
		}
		if (mw.UAR.clipboard.volume) {
			this.P478 = [
				{
					"snaktype": "value",
					"property": "P478", // volume
					"datavalue": {
						"value": mw.UAR.clipboard.volume,
						"type": "string"
					},
					"datatype": "string"
				}
			];
		}
		if (mw.UAR.clipboard.page) {
			this.P304 = [
				{
					"snaktype": "value",
					"property": "P304", // page
					"datavalue": {
						"value": mw.UAR.clipboard.page,
						"type": "string"
					},
					"datatype": "string"
				}
			];
		}
		if (mw.UAR.clipboard.section) {
			this.P958 = [
				{
					"snaktype": "value",
					"property": "P958", // section
					"datavalue": {
						"value": mw.UAR.clipboard.section,
						"type": "string"
					},
					"datatype": "string"
				}
			];
		}
	}
	
	function save_reference(paste_icon) {
		// disable paste icon, without changing its visibility
		// perform only after a successfull save? FIXME
		$(paste_icon)
			.css({
				'border':'3px solid red',
				'padding':'2px'
			});
		$(paste_icon).parents('.pasteref-container').css('pointer-events','none');
		var so = ["P248", "P3865", mw.UAR.clipboard.dpr, "P10253", "P854", "P958", "P478", "P304", "P1810", "P577", "P1065", "P2960", "P813"];
		var claim_guid = $(paste_icon).parents('div[class~="wikibase-statementview"]').attr('id');
		// save new reference
		var api = new mw.Api();
		var token = mw.user.tokens.values.csrfToken;
		api.post({ 
			'action'     : 'wbsetreference',
			'summary'    : 'Added with [[User:Bargioni/UseAsRef|UseAsRef2.0]]',
			'statement'  : claim_guid,
			'snaks-order': JSON.stringify(so),
			'snaks'      : JSON.stringify(this),
			'format'     : 'json',
			'token'      : token
		})
		.then( function(aw) {
			if ( aw.success == 1 ) {
				var ps = aw.reference['snaks-order'].toString();
				mw.notify ('Reference successfully added using '+ps+'.\nTo see the changes, please reload the page.',
					{
						title: 'UseAsRef - Info',
						autoHide: true,
						type: 'info'
					}
				);
			}
			else {
				console.log('error',aw);
				mw.notify (aw.error,
					{
						title: 'UseAsRef - Error',
						autoHide: false,
						type: 'error'
					}
				);
			}
		} )
		.catch( function(e) {
			console.log('error',e);
			mw.notify (e,
				{
					title: 'UseAsRef - Error',
					autoHide: false,
					type: 'error'
				}
			);
		} );
	}
	
	function makeSPARQLQuery( endpointUrl, sparqlQuery, doneCallback ) {
		var settings = {
			headers: { Accept: 'application/sparql-results+json' },
			data: { query: sparqlQuery }
		};
		return $.ajax( endpointUrl, settings ).then( doneCallback );
	}

	// --------
	
	function get_included_statements_from_config() {
		var included_statements = mw.UAR.config.included_statements;
		var names = Object.entries(included_statements).map(function(p){return p[0]});
		return names;
	}
	
	function copy_ref(e) {
		mw.UAR.clipboard = {};
		var dpr = $(e.target).attr('data-prop');
		var dor = $(e.target).attr('data-order');
		var qid = mw.config.get( 'wbEntityId' );
		var statement = mw.UAR.Qjson.entities[qid].claims[dpr][dor];
		// decide quale parte di UAR_config usare
		var clipboard_config;
		if (statement.mainsnak.datatype == 'external-id') 
			clipboard_config = mw.UAR.config.included_statements['external-id'];
		else
			clipboard_config = mw.UAR.config.included_statements[dpr];
		$('.UAR_icon_copy').css({'border':'', 'padding':''}); // un-highlight previous copied source - Stefano Bargioni 2022-02-07
		// building a clipboard
		var clipboard = new Clipboard( clipboard_config, dpr, dor, statement );
		// $('.UAR_icon_copy, .UAR_icon_paste').css({'width':'16px', 'border':'3px solid yellow'});
		$('.UAR_icon_copy, .UAR_icon_paste').css({'width':'16px'});
		$(e.target)
			.css({
				'border':'3px solid green',
				'padding':'2px'
			});
		// notify user
		mw.notify( 'Reference copied: ' + dpr + ' ' + clipboard.value, {
			title: 'UseAsRef - Info',
			type: 'info'
		});
		// enable paste icons
		$('.UAR_icon_paste').css({'pointer-events':'', 'opacity':'1.0'});
	}

	function paste_ref(e) {
		/* reference will include following properties, when available from clipboard:
		current date  source  named_as  pub_date  archive_url  archive_date	 ref_type
		P813          P248    P1810	    P577      P1065	       P2960         P3865
		*/
		var ref = new Reference();
		ref.init();
		// console.log('ref',ref);
		ref.save(e.target);
	}
	
	function build_up_dialog () {
		$( '<div>' )
			.attr( 'id', 'UAR_dialog' )
			.append(
				$( '<p>' )
				.attr( 'id', 'UAR_dialog_msg' )
				.html('qui messaggio')
			)
			.dialog( {
				dialogClass: 'UAR_dialog',
				title: 'UseAsRef' + ' <img class="UAR_icon" src="'+mw.UAR.config.icon_copy+'">'+' <img class="UAR_icon" src="'+mw.UAR.config.icon_paste+'">',
				autoOpen: false,
				modal: true,
				width: 500,
				buttons: [ {
					id: 'UAR_dialog-button-close',
					text: 'Close',
					click: function ( event ) {
						event.preventDefault();
						$( '#UAR_dialog' ).dialog( 'close' );
					}
				} ]
			} );
	}

	function UAR_config() {
		// console.log('UAR_config');
		// set up mw.UAR object
		// object to paste
		mw.UAR.clipboard = {};
		// set up UAR config
		mw.UAR.config = {};
		// icons
		mw.UAR.config.icon_warning = 'https://upload.wikimedia.org/wikipedia/commons/d/d9/Warning_sign_font_awesome-red.svg';
		mw.UAR.config.icon_ok = 'https://upload.wikimedia.org/wikipedia/commons/0/05/Font_Awesome_5_solid_check-circle.svg';
		mw.UAR.config.icon_copy = 'https://upload.wikimedia.org/wikipedia/commons/1/15/Font_Awesome_5_solid_copy.svg';
		mw.UAR.config.icon_paste = 'https://upload.wikimedia.org/wikipedia/commons/4/4d/Font_Awesome_5_solid_file-import.svg';
		mw.UAR.config.icon_help = '?';
		
		build_up_dialog();
		var today = new Date;
		if ( today < new Date('2021-10-29') ) {
			mw.notify ('This is UseAsRef 2.0.\nPlease visit [[User:Bargioni/UseAsRef]] to discover new features.',
				{
					title: 'UseAsRef - Info',
					autoHide: true,
					type: 'info'
				}
			);
		}
		var section_for_info;
		section_for_info = $( '#identifiers' ).length ? $( '#identifiers' ) : $( '#claims' );
		section_for_info.after(' \
			<img class="UAR_info" src="'+mw.UAR.config.icon_paste+'"> \
			<img class="UAR_info" src="'+mw.UAR.config.icon_copy+'">');
		$( '.UAR_info' )
			.attr('title','UseAsRef Gadget - Info')
			.css({'position':'relative', 'float':'right', 'width':'16px', 'cursor':'pointer'})
			.click(function(){
				$( '#UAR_dialog_msg' ).html(`<div>
				<p>UseAsRef version 2.0 (2021-10-29)</p>
				<p><a target="_blank" href="/wiki/User:Bargioni/UseAsRef">More info</a></p>
				<p id="UAR_toggle_icons" style="cursor:pointer; color:darkblue; float:right">Hide / Show UseAsRef icons</p>
				</div>`);
				$( '#UAR_dialog' ).dialog( 'open' );
				$('#UAR_toggle_icons').click(function(){ $('.UAR_icon').toggle() });
			});
		// $('.UAR_info, .UAR_icon').css({'width':'16px', 'border':'3px solid yellow'});
		$('.UAR_info, .UAR_icon').css({'width':'16px'});

		mw.UAR.config.excluded_ids = ['P212', 'P213', 'P214', 'P236', 'P646', 'P2671', 'P791', 'P957', 'P2333', 'P7859'];
		mw.UAR.config.included_statements = {
			'P1343' : { // described by source; descritto nella fonte
				'value' : ['P805', 'value'], // P805 || value
				'property_value' : 'P248',
				'named_as' : 'P1810',	
				'pub_date' : 'P577',
				'archive_url' : '',
				'archive_date' : '',
				'language' : 'P407',
				'volume' : 'P478',
				'page' : 'P304',
				'section' : 'P958',
				'ref_type' : '',
				'source' : ''
			},
			'P856'  : { // official web site; sito web ufficiale
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : 'P577',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q22137024',
				'source' : ''
			},
			'P1581' : { // official blog; blog ufficiale
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : 'P577',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q41557148',
				'source' : ''
			},
			'P8214' : { // curriculum vitae; curriculum vitae
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : 'P577',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q186649',
				'source' : ''
			},
			'P7014' : { // terms of service URL; URL dei termini di servizio
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : 'P577',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q7214489',
				'source' : ''
			},
			'P7101' : { // privacy policy URL; URL della privacy policy
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : 'P577',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q1999831',
				'source' : ''
			}, 
			'P973'  : { // described at URL; descritto nell'URL
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : 'P577',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'P3831',
				'source' : ''
			},
			'P5715' : { // Academia.edu profile URL; URL del profilo Academia.edu
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : '',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : '',
				'source' : 'Q2777905'
			},
			'P953'  : { // full work available at URL; testo completo disponibile all'indirizzo
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : '',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : '',
				'source' : ''
			},
			'P2888' : { // exact match; corrisponde esattamente
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : '',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : '',
				'source' : ''
			},
			'P8768' : { // online catalog; catalogo online
				'value' : 'value',
				'property_value' : 'P854',
				'named_as' : '',
				'pub_date' : '',
				'archive_url' : 'P1065',
				'archive_date' : 'P2960',
				'language' : 'P407',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : '',
				'source' : ''
			},
			'P1442' : { // image of grave; immagine della sepoltura
				'value' : 'value',
				'property_value' : 'P10253',
				'named_as' : 'P1810',
				'pub_date' : '',
				'archive_url' : '',
				'archive_date' : '',
				'language' : '',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q203443',
				'source' : ''
			},
			'P1801' : { // commemorative plaque image; immagine di una targa commemorativa
				'value' : 'value',
				'property_value' : 'P10253',
				'named_as' : 'P1810',
				'pub_date' : '',
				'archive_url' : '',
				'archive_date' : '',
				'language' : '',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : 'Q721747',
				'source' : ''
			},
			'external-id' : {
				'value' : 'value',
				'property_value' : 'original',
				'named_as' : 'P1810', // if available
				'pub_date' : 'P577',
				'archive_url' : '',
				'archive_date' : '',
				'language' : '',
				'volume' : '',
				'page' : '',
				'section' : '',
				'ref_type' : '',
				'source' : '???'
			}
		};
	}

 	function UAR_init() {
 		// console.log('UAR_init'); // test
		var ids = $('.wikibase-statementgrouplistview>div').get(2); // the block containing identifiers
		var div_ids = $(ids).find('>div'); // the divs of each identifier; inside each div, more identifiers can exist: .find('a.external')

		// get the item in JSON format
		var qid = mw.config.get( 'wbEntityId' ); // Qnnn
		$.getJSON(`https://www.wikidata.org/wiki/Special:EntityData/${qid}.json`, function(J){
			mw.UAR.Qjson = J;
		});
		
		// wait for identifiers and item in JSON
		var flag = 0;
		if ( div_ids.length == 0 || typeof mw.UAR.Qjson == "undefined" && !flag ) {
			mw.UAR.UAR_retries++;
			if (mw.UAR.UAR_retries >= mw.UAR.UAR_max_retries) {
				if (div_ids.length == 0 && typeof mw.UAR.Qjson == "object") 
					flag = 1; // apply UAR to included statements only
				else {
					console.error('Unable to activate UAR, too many retries');
					return; // stop trying
				}
			}
			if (flag == 0) {
				setTimeout(function(){ UAR_init(); }, 200);
				return;
			}
		}
		console.log('UseAsRef 2.0: gadget loaded'); // this is not a test :-)
		UAR_config();
		var item = new Item(mw.UAR.Qjson, qid);
		item.append_icons();
		$('.UAR_icon_copy').click( function(e) { copy_ref(e) });
		$('.UAR_icon_paste').click( function(e) { paste_ref(e) });
 	}
 	
	// ===============
	
	// do not always apply UAR to a Wikidata page
	if (document.location.href.indexOf('Property:P') > -1) return;
	if (!mw.config.exists('wbEntityId')) return;
 
	mw.UAR = {};
	$.when(
		mw.loader.using( [
			'jquery.spinner', 'jquery.ui', 'mediawiki.api',
			'mediawiki.util', 'wikibase.api.RepoApi', 'mediawiki.jqueryMsg'
		] )
		.then( function () {
			mw.UAR.UAR_retries = 0; 
			mw.UAR.UAR_max_retries = 25;
		} ),
		$.ready
	)
	.then( UAR_init );

} ( mediaWiki, wikibase, jQuery ) );
});