User:ElanHR/SignedStatements.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.
/* SignedStatements.js
 * 
 * Gadget to allow users to verify whether a Wikidata statement cryptographically matches the provided signature in the reference.
 *
 * Original gadget coded by [[User:ElanHR]]
 */

var signed_statements = {
	
	p_prefix : 'P' ,
	q_prefix : 'Q' ,

	addBannerToWikidataItem : function (message) {
	/**
	 * Writes the provided message on a wikidata item page (below title, above labels).
	 */
		let entitycheck_conditions = ["/wiki/Q", "/wiki/P", "/wiki/L"];
		if (entitycheck_conditions.some(el => document.location.pathname.includes(el))) {
			let entitycheck_entity_html = '<div><span id="entityschema-simpleSearch"><span>';
			entitycheck_entity_html += message;
			entitycheck_entity_html += '</span></span>';
			var entitycheck_entityTitle = $(".wikibase-title-id" )[0].innerText;
			if (document.location.pathname.includes("/wiki/L")) {
				$(".mw-indicators" ).append( entitycheck_entity_html );
			} else {
				$(".wikibase-entitytermsview-heading" ).append( entitycheck_entity_html );
			}
		}	
	},
	init : function () {
	/**
	 *
	 */
		var self = this ;
		console.debug('signed_statements.init()'); 
		self.run();
	},
	run : function () {
	/**
	 *
	 */
		var self = this ;
    		console.debug('signed_statements.run()'); 
		if ( self.running ) return ;
		self.running = true ;
	},
	getEntityClaims: function(q, callback) {
	/**
	 * Gets 
	 */
		console.debug('(debug)signed_statements.findSignedStatements()');
	    $.getJSON ( '//www.wikidata.org/w/api.php?callback=?' , {
		action : 'wbgetentities' ,
		ids : q.join() ,
		format : 'json' ,
		props : 'claims'
	    } , function ( data ) {
		callback(data['entities'])
	    } ) ;
	},
	findSignedStatements : function (entity) {
	/** 
	 * For a given entity returns all statements with cryptographic signatures.
	 */
		console.debug('(debug)signed_statements.findSignedStatements()');
		const dummy_ids_for_fake_signed_statements = new Set(['Q42$44889d0f-474c-4fb9-1961-9a3366cbbb9e', 'Q42$2DD87345-81C1-4252-A5C2-727F1950AB17']);

		var output_statements = new Array()
		for (const claim_group of Object.values(entity['claims'])) {
			for (const claim of claim_group) {
				//console.debug('claim: ' +JSON.stringify(claim));	
				//console.debug('claim: ' +claim['mainsnak']['property'] + " - " +  claim['id']);	

				if (dummy_ids_for_fake_signed_statements.has(claim['id'])) {
					output_statements.push(claim);
				}
			}
		}
		return output_statements;
	},
	checkStatementSignature : function (statement) {
	/** 
	 * Returns whether a statement cryptographically matches it's signature.
	 */
		console.debug('signed_statements.checkSignedStatements()');
	},
	highlightCheckedStatement : function () {
	/** 
	 * Given a set of statement ids, finds the associated elements on the page and highlights them accordingly.
	 */
		console.debug('signed_statements.highlightCheckedStatement()');
	},
}

$( function() {
	signed_statements.addBannerToWikidataItem("Hello World!") ;
	signed_statements.getEntityClaims(['Q42'], function(entities){
		console.log('data:' + JSON.stringify(entities));
		signed_claims = signed_statements.findSignedStatements(entities['Q42']);
		for (const sc of signed_claims) {
			console.debug('signed_claim: ' +JSON.stringify(sc));	
		}
	});
});