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

From Wikibase Personal data
Jump to navigation Jump to search
 
(23 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
     api.get( {
 
     api.get( {
 
    action: 'expandtemplates',
 
    action: 'expandtemplates',
    //text: '{{User:Alexbfree/Template:GDPRMailtoAccess|qID='+dataControllerItemID+'}}'
+
    text: '{{User:Alexbfree/Template:DataControllerMenu|qID='+dataControllerItemID+'}}'
    text: '{{access|qID='+dataControllerItemID+'}}'
 
 
} ).done( function ( data ) {
 
} ).done( function ( data ) {
 
         var expanded = $(data.expandtemplates["*"])
 
         var expanded = $(data.expandtemplates["*"])
Line 15: Line 14:
 
}
 
}
  
function isDataController() {
+
function claimUsesDataControllerAsItsObject(claim) {
let retVal = false;
+
return dive( claim , 'mainsnak.datavalue.value')['numeric-id'] === DATA_CONTROLLER_ITEM_ID;
if ( typeof mw.config.values.wbEntity !=  "undefined" ) // if we are on the page of a loaded entity
+
}
 +
 
 +
mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entity ) {
 +
    'use strict';
 +
     
 +
    // get the ID of the current page
 +
var pageID = document.querySelector('.wikibase-title .wikibase-title-id').innerText.replace( /[()]/g, '' );
 +
    //console.log(pageID);
 +
 
 +
    if (isDataController(entity)) {
 +
  generateGDPRLink(pageID);
 +
    }
 +
});
 +
 
 +
function isDataController(entity) {
 +
let foundThatThisIsAnInstanceOfDataController = false;
 +
 
 +
if ( typeof entity !=  "undefined" ) // if we are on the page of a loaded entity
 
{
 
{
let obj = JSON.parse ( mw.config.values.wbEntity );
+
  if ( typeof check( entity, INSTANCE_OF_PROPERTY ) != "undefined" ) // if the entity has any "instance of" claim
if ( typeof check( obj, INSTANCE_OF_PROPERTY ) != "undefined" ) // if the entity has any "instance of" claim
+
  {
 +
let claims = dive( entity, INSTANCE_OF_PROPERTY ); // get the claims which use the "instance of" property
 +
if (claims.some(claimUsesDataControllerAsItsObject))
 
{
 
{
let instance_of_claims = dive( obj, INSTANCE_OF_PROPERTY );
+
  foundThatThisIsAnInstanceOfDataController = true;
for( var i = 0; i < instance_of_claims.length; i++ ) // retrieve each 'instance of' claim to check it
 
{
 
let the_thing_it_is_an_instance_of = dive( instance_of_claims[i] , 'mainsnak.datavalue.value');
 
if (the_thing_it_is_an_instance_of['numeric-id'] === DATA_CONTROLLER_ITEM_ID )
 
{
 
  console.log('found data controller');
 
  // it's a data controller
 
  retVal = true; break;
 
}
 
}
 
 
}
 
}
 +
  }
 
}
 
}
return retVal;
+
return foundThatThisIsAnInstanceOfDataController;
 
}
 
}
  
// get the ID of the current page
+
function dive(array, read){
var $title2 = $( '.wikibase-title' );
+
return read.split('.').reduce(indexinterpolate, array);
var qId = $title2.find( '.wikibase-title-id' ).text().replace( /[()]/g, '' );
+
}
  
if (isDataController()) {
+
function check(array, read){
generateGDPRLink(qId);
+
        return read.split('.').reduce(indexcheck, array);
 
}
 
}

Latest revision as of 18:17, 25 March 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 generateGDPRLink(dataControllerItemID) {
	// 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:DataControllerMenu|qID='+dataControllerItemID+'}}'
			} ).done( function ( data ) {
        var expanded = $(data.expandtemplates["*"])
    	mw.notify( expanded , { autoHide: false } ); // pop up the notification with the link
	} );
}

function claimUsesDataControllerAsItsObject(claim) {
	return dive( claim , 'mainsnak.datavalue.value')['numeric-id'] === DATA_CONTROLLER_ITEM_ID;
}

mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entity ) {
     'use strict';
      
     // get the ID of the current page
	 var pageID = document.querySelector('.wikibase-title .wikibase-title-id').innerText.replace( /[()]/g, '' );
     //console.log(pageID);

     if (isDataController(entity)) {
	   generateGDPRLink(pageID);
     }
});

function isDataController(entity) {
	let foundThatThisIsAnInstanceOfDataController = false;

	if ( typeof entity !=  "undefined" ) // if we are on the page of a loaded entity
	{
	  if ( typeof check( entity, INSTANCE_OF_PROPERTY ) != "undefined" ) // if the entity has any "instance of" claim
	  {
		let claims = dive( entity, INSTANCE_OF_PROPERTY ); // get the claims which use the "instance of" property
		if (claims.some(claimUsesDataControllerAsItsObject))
		{
		  foundThatThisIsAnInstanceOfDataController = true;
		}
	  }
	}
	return foundThatThisIsAnInstanceOfDataController;
}

function dive(array, read){
	return read.split('.').reduce(indexinterpolate, array);
}

function check(array, read){
        return read.split('.').reduce(indexcheck, array);
}