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" |
| | | |
− | console.log ("start of Alex's Access template");
| + | function generateGDPRLink(dataControllerItemID) { |
− | var controller_item_id = 96; | + | // 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); |
| + | } |
| + | }); |
| | | |
− | var wbEStruct = {
| + | function isDataController(entity) { |
− | instanceOfRelations: 'claims.P3', // location of P3 relations in wbEntity
| + | let foundThatThisIsAnInstanceOfDataController = false; |
− | IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
| |
− | IDName: 'numeric-id' // ID property name which checked
| |
− | };
| |
| | | |
− | // check if we are on a page of a loaded Entity
| + | if ( typeof entity != "undefined" ) // if we are on the page of a loaded entity |
− | if ( typeof mw.config.values.wbEntity != "undefined" ) { | + | { |
− | // defined entity
| + | if ( typeof check( entity, INSTANCE_OF_PROPERTY ) != "undefined" ) // if the entity has any "instance of" claim |
− | // get wbEntity as json | + | { |
− | var obj = JSON.parse ( mw.config.values.wbEntity );
| + | let claims = dive( entity, INSTANCE_OF_PROPERTY ); // get the claims which use the "instance of" property |
− | // check if we have P3 relations and iterate through
| + | if (claims.some(claimUsesDataControllerAsItsObject)) |
− | if ( typeof check( obj, wbEStruct["instanceOfRelations"] ) != "undefined" ) {
| + | { |
− | // checking each relation
| + | foundThatThisIsAnInstanceOfDataController = true; |
− | for( var i = 0; i < dive( obj, wbEStruct["instanceOfRelations"] ).length; i++ ) {
| |
− | // check if we have controller id constructed from wbEntity
| |
− | 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, '' );
| |
− | 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; | + | return foundThatThisIsAnInstanceOfDataController; |
| + | } |
| + | |
| + | function dive(array, read){ |
| + | return read.split('.').reduce(indexinterpolate, array); |
| } | | } |
| | | |
− | console.log ("end of Alex's Access template");
| + | function check(array, read){ |
| + | return read.split('.').reduce(indexcheck, array); |
| + | } |