Difference between revisions of "User:Alexbfree/Code:GDPRMailAccess.js"

From Wikibase Personal data
Jump to navigation Jump to search
Line 1: Line 1:
 +
const DATA_CONTROLLER_ITEM_ID = 96;  // because Q96 is data controller"
 +
const INSTANCE_OF_PROPERTY = 'claims.P3'; // because P3 is the property "instance of"
 +
 +
function isDataController() {
 +
let retVal = false;
 +
if ( typeof mw.config.values.wbEntity !=  "undefined" ) // if we are on the page of a loaded entity
 +
{
 +
let obj = JSON.parse ( mw.config.values.wbEntity );
 +
if ( typeof check( obj, INSTANCE_OF_PROPERTY ) != "undefined" ) // if the entity has any "instance of" claim
 +
{
 +
for( var i = 0; i < dive( obj, INSTANCE_OF_PROPERTY ).length; i++ ) // retrieve each 'instance of' claim to check it
 +
{
 +
if( dive( dive( obj, INSTANCE_OF_PROPERTY)[i] , 'mainsnak.datavalue.value')['numeric-id'] === DATA_CONTROLLER_ITEM_ID )
 +
{
 +
  // it's a data controller
 +
  retVal = true; break;
 +
}
 +
}
 +
}
 +
}
 +
return retVal;
 +
}
 +
  
 
console.log ("start of Alex's Access template");
 
console.log ("start of Alex's Access template");
var controller_item_id = 96;
 
  
var wbEStruct = {
+
// get the ID of the current page
instanceOfRelations: 'claims.P3', // location of P3 relations in wbEntity
+
var $title2 = $( '.wikibase-title' );
IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
+
console.log($title2.find( '.wikibase-title-id' ).text());
IDName: 'numeric-id' // ID property name which checked
+
var qId = $title2.find( '.wikibase-title-id' ).text().replace( /[()]/g, '' );
};  
+
console.log('qid is '+qId);
  
// check if we are on a page of a loaded Entity
+
if (isDataController()) {
if ( typeof mw.config.values.wbEntity !=  "undefined" ) {
+
// make an API call to expand the letter template, using this item.
// defined entity
+
let api = new mw.Api();
// get wbEntity as json
+
    api.get( {
var obj = JSON.parse ( mw.config.values.wbEntity );
+
    action: 'expandtemplates',
// check if we have P3 relations and iterate through
+
    text: '{{User:Alexbfree/Template:GDPRMailtoAccess|qID='+qId+'}}'
if ( typeof check( obj, wbEStruct["instanceOfRelations"] ) != "undefined" ) {
+
} )
// checking each relation
+
.done( function ( data ) {
for( var i = 0; i < dive( obj, wbEStruct["instanceOfRelations"] ).length; i++ ) {
+
        var expanded = $(data.expandtemplates["*"])
// check if we have controller id constructed from wbEntity
+
    mw.notify( expanded , { autoHide: false } ); // pop up the notification with the link
if( dive( dive( obj, wbEStruct["instanceOfRelations"])[i] , wbEStruct["IDPropLoc"])[wbEStruct["IDName"]] === controller_item_id ) {
+
} );
// it's a data controller
 
console.log('got data controller:');
 
console.dir(obj);
 
console.log('diving gives:');
 
console.dir(dive( obj, wbEStruct["instanceOfRelations"]));
 
var $title2 = $( '.wikibase-title' ),
 
qId = $title2.find( '.wikibase-title-id' ).text().replace( /[()]/g, '' );
 
console.log('qid is '+qId);
 
var api = new mw.Api();
 
                                api.get( {
 
    action: 'expandtemplates',
 
    text: '{{User:Alexbfree/Template:GDPRMailtoAccess|qID='+qId+'}}'
 
} ).done( function ( data ) {
 
                                    var expanded = $(data.expandtemplates["*"])
 
                                    console.log ('alex API call returned '+typeof expanded+':');
 
                                    console.dir(expanded)
 
    //mw.notify( expanded , { autoHide: false } );
 
} );
 
}
 
}
 
}
 
// Else let's do nothing;
 
 
}
 
}
 +
// otherwise, we don't need to do anything
  
 
console.log ("end of Alex's Access template");
 
console.log ("end of Alex's Access template");

Revision as of 15:39, 27 January 2020

const DATA_CONTROLLER_ITEM_ID = 96;  // because Q96 is data controller"
const INSTANCE_OF_PROPERTY = 'claims.P3'; // because P3 is the property "instance of"

function isDataController() {
	let retVal = false;
	if ( typeof mw.config.values.wbEntity !=  "undefined" ) // if we are on the page of a loaded entity
	{
		let obj = JSON.parse ( mw.config.values.wbEntity );
		if ( typeof check( obj, INSTANCE_OF_PROPERTY ) != "undefined" ) // if the entity has any "instance of" claim
		{
			for( var i = 0; i < dive( obj, INSTANCE_OF_PROPERTY ).length; i++ ) // retrieve each 'instance of' claim to check it
			{
				if( dive( dive( obj, INSTANCE_OF_PROPERTY)[i] , 'mainsnak.datavalue.value')['numeric-id'] === DATA_CONTROLLER_ITEM_ID ) 
				{
				  // it's a data controller
				  retVal = true; break;
				}
			}
		}
	}
	return retVal;
}


console.log ("start of Alex's Access template");

// get the ID of the current page
var $title2 = $( '.wikibase-title' );
console.log($title2.find( '.wikibase-title-id' ).text());
var qId = $title2.find( '.wikibase-title-id' ).text().replace( /[()]/g, '' );
console.log('qid is '+qId);

if (isDataController()) {
	// make an API call to expand the letter template, using this item.
	let api = new mw.Api();
    api.get( {
			    action: 'expandtemplates',
			    text: '{{User:Alexbfree/Template:GDPRMailtoAccess|qID='+qId+'}}'
			} )
	.done( function ( data ) {
        var expanded = $(data.expandtemplates["*"])
    	mw.notify( expanded , { autoHide: false } ); // pop up the notification with the link
	} );
}
// otherwise, we don't need to do anything

console.log ("end of Alex's Access template");