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

From Wikibase Personal data
Jump to navigation Jump to search
Line 10: Line 10:
 
} ).done( function ( data ) {
 
} ).done( function ( data ) {
 
         var expanded = $(data.expandtemplates["*"])
 
         var expanded = $(data.expandtemplates["*"])
        var currentPageId = "{{#invoke:wd|label|raw}}";
+
     mw.notify( expanded , { autoHide: false } ); // pop up the notification with the link
     mw.notify( "<p>Got:"+currentPageId+"</p>"+expanded , { autoHide: false } ); // pop up the notification with the link
 
 
} );
 
} );
 
}
 
}

Revision as of 10:55, 28 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 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;
}

function isDataController() {
	let foundThatThisIsAnInstanceOfDataController = 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
		{
			let claims = dive( obj, INSTANCE_OF_PROPERTY ); // get the claims which use the "instance of" property
			if (claims.some(claimUsesDataControllerAsItsObject))
			{
			  foundThatThisIsAnInstanceOfDataController = true;
			}
		}
	}
	return foundThatThisIsAnInstanceOfDataController;
}

// get the ID of the current page
var pageID = document.querySelector('.wikibase-title .wikibase-title-id').innerText.replace( /[()]/g, '' );

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