Difference between revisions of "User:Abel/experimental.js"

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(191 intermediate revisions by 2 users not shown)
Line 1: Line 1:
console.log('_');
+
// License: GPL
 +
// Experimental Main
 +
console.log("''");
  
var controller_item_id = 96,
+
var mwEStruct = {
    personal_data_id = 421,
+
wbXntity: 'config.values.wbEntity'
    interface_button_id = 487,
+
},
    wbEStruct = {
 
    subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
 
    defaultValRelation: 'claims.P108',
 
    instanceOfRelations: 'claims.P3',
 
        IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
 
    IDName: 'numeric-id', // ID property name which checked
 
    qIDPropName: 'id'
 
    },
 
    mwEStruct = {
 
    wbXntity: 'config.values.wbEntity'
 
    };
 
  
// check if we are on a page of any of the targeted Entity
+
language = "en",
// more exactly, use iterateAcheckBC to check in mw.config.values.wbEntity if instanceOfRelations exist, than if yes, check if numeric id of any matches controller id
 
  
 +
WBEStructure = {
 +
subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
 +
defaultValRelation: 'claims.P108',
 +
concernsRelation: 'claims.P110',
 +
instanceOfRelations: 'claims.P3',
 +
IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
 +
IDName: 'numeric-id', // ID property name which checked
 +
qIDPropName: 'id',
 +
standardValue: 'labels' + '.' + language + '.' + 'value'
 +
},
  
if ( iterateAcheckBC(mw, mwEStruct['wbXntity'], wbEStruct['instanceOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], controller_item_id) ) {
+
Relational_id_list = {
console.log("This is a Data Controller");
+
controller_id: [ WBEStructure['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 96 ],
controller_display_function();
+
personal_data_id: [ WBEStructure['subclassOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 421 ],
}
+
interface_button_id: [ WBEStructure ['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 487 ]
 +
},
  
// use iterateAcheckBC to check in mw.config.values.wbEntity if subClassOfRelations exist, than if yes, check if numeric id of any matches personal data id
+
PDWikibaseProcessor,
if ( iterateAcheckBC(mw, mwEStruct['wbXntity'], wbEStruct['subclassOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], personal_data_id) ) {
+
PDindexedDB,
console.log("This is a Personal Data Type");
+
PDOOUI,
input_display_function();
+
Current_entity;
}
 
  
// use iterateAcheckBC to check in mw.config.values.wbEntity if instanceOfRelations exist, than if yes, check if numeric id of any matches interface button id
 
if ( iterateAcheckBC(mw, mwEStruct['wbXntity'], wbEStruct['instanceOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], interface_button_id) ) {
 
console.log("This is a Data Input Interface");
 
data_maintenance_display();
 
}
 
  
// show popup from template for controllers
+
// JQ.'when' loads the current scripts asynchronously, continues when loading is 'done'
function controller_display_function(){
+
$.when(
console.log( "it's a controller" )
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/WbProcessor.js&action=raw&ctype=text/javascript" ),
var obj2 = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/PersonalData.js&action=raw&ctype=text/javascript" ),
var qId = obj2[wbEStruct["qIDPropName"]];
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/OOInterface.js&action=raw&ctype=text/javascript" ),
var api = new mw.Api();
+
    $.Deferred( function( deferred ){
api.get( {
+
        $( deferred.resolve );
action: 'expandtemplates',
+
    })
text: '{{MailtoAccess|'+qId+'}}'
+
).done( function(){
} ).done( function ( data ) {
 
mw.notify( $('<a href="'+data.expandtemplates["*"]+'"> Do a SAR to this data controller!</a>') , { autoHide: false } );
 
} );
 
}
 
  
// show input for personal data
+
console.log('Main thread');
function input_display_function() {
 
var obj = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
 
mw.notify( $('<input type="text" placeholder="' + get_Y_from_X ( get_Y_from_X( obj, wbEStruct['defaultValRelation'] ), wbEStruct['IDPropLoc'] ) + '" id="place_0" />'), { autoHide: false } );
 
}
 
  
// show maintenance options for personal data
+
// WBproc reads current mediawiki Entity upon instantiation
function data_maintenance_display() {
+
PDWikibaseProcessor = new WikibaseProcessorContainer(mw, mwEStruct['wbXntity']);
var obj = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
 
}
 
  
// repeating code patterns
+
// PDxDB takes mediawiki, reads user name from it, creates a hash, and 'checks in' the user upon instantiation
 +
// (i.e. read record belonging to user, if it does not exist, create it)
 +
PDindexedDB = new IndexedDBContainer( mw );
 +
 +
// Contains classes for creating UI elements, no instantiation consequences
 +
PDOOUI = new InterfaceMediaContainer( PDWikibaseProcessor, PDindexedDB );
  
// get obj.'rel_0' (gets wbEntity), check if it has 'A', iterate over 'A', get 'B' and 'C' and check if any contains 'to_check'. if yes, return true
+
// Check if the current entity belongs to any path of interest.  
function iterateAcheckBC( obj, rel_0, rel_A, rel_B, rel_C, to_check ) {
+
// For each element in object argument, a corresponding element in the return object will contain one of the following:
  var future = false;
+
// - undefined - the path didn't exist even partly)
  if ( check( obj, rel_0 ) != undefined ) {
+
// - [ value or object ] - if the path existed fully, it returns what is at the end of the path. If last element in path is a value, it will return the value if found
    var edata = interjson(get_Y_from_X( obj, rel_0 ));
 
    console.log(edata);
 
    if ( check( edata, rel_A ) != undefined ) {
 
      var current = get_Y_from_X( edata, rel_A );
 
      console.log( "parsed object ")
 
      console.log( current );
 
      // check if we have rel_A relations and iterate through
 
      for( var i = 0; i < current.length; i++ ) {
 
        console.log("dived in ");
 
        console.log(current[i]);
 
        // check if we have controller id constructed from wbEntity
 
        if ( check( current[i], rel_B ) != undefined ) {
 
          if ( check( check (current[i], rel_B ), rel_C ) != undefined ) {
 
            if ( check_Y_in_X( get_Y_from_X( get_Y_from_X( current[i], rel_B ), rel_C ), to_check ) ) {
 
              console.log("mark +");
 
              future = true;
 
            };
 
          }
 
        }
 
      }
 
    }
 
  }
 
  return future;
 
}
 
  
function get_Y_from_X( obj, B ){
+
Current_entity = PDWikibaseProcessor.checkentity( Relational_id_list );
if ( check(obj, B) != undefined ) {
+
console.log( "entity is defined" )
+
// Business logicish part.
return dive(obj, B);
+
// If any path of interest returned anything...
 +
if ( Current_entity != undefined ) {
 +
Object.keys(Current_entity).forEach( function ( entity_checked ){
 +
//... for each path that returned something
 +
if ( Current_entity[entity_checked] != undefined ){
 +
if ( Object.keys( Current_entity[entity_checked] ).length > 0 ) {
 +
PDOOUI.renderInterface( entity_checked );
 +
//... call the OOUI renderer to render what belongs to this input
 +
}
 +
}
 +
});
 
}
 
}
}
 
  
function interjson(obj) {
+
});
try {
 
return JSON.parse( obj )
 
} catch (e) {
 
// if not JSON, do silly things
 
};
 
 
 
}
 
 
 
function check_Y_in_X(obj, to_check){
 
for (var i = 0; i < obj.length; i++ ){
 
if (obj[i] === to_check) { return true };
 
}
 
return false;
 
}
 
 
 
function indexinterpolate(obj,i) { return  (obj[i] != undefined) ? obj[i] : obj};
 
// helper function for checking a part of an array exists
 
function indexcheck(obj,i) {  return  (obj[i] != undefined) ? obj[i] : undefined };
 
// dive selects matrix.a.b.c.d from the array called matrix and 'a.b.c.d' as string
 
// if a.b.c.d does not exists, it returns the substructure until the substructure exists, if d does not exist, it returns matrix.a.b.c e.g.
 
function dive(array, read){
 
return read.split('.').reduce(indexinterpolate, array);
 
}
 
function check(array, read){
 
return read.split('.').reduce(indexcheck, array);
 
}
 
 
 
// ADD MORE DOC
 
 
 
var PersonalDataDataBaseName = "PDIO3";
 
var Jonathan = {id: 11345, name: {first: "Jonatha", last: "Lemmings"}, age: 32};
 
 
 
recordAdd(PersonalDataDataBaseName, Jonathan);
 
recordUpdate(PersonalDataDataBaseName, Jonathan, 'phone', '+313851777');
 
recordCheck(PersonalDataDataBaseName, Jonathan);
 
 
 
function recordAdd(pddbname, record){
 
  var pddb = window.indexedDB.open(pddbname, 3);
 
  pddb.onupgradeneeded = function() {
 
    var db = pddb.result;
 
    var store = db.createObjectStore(PersonalDataDataBaseName, {keyPath: "id"});
 
// shouldn't the index be the wiki.personaldata.io username?
 
var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
 
};
 
pddb.onsuccess = function() {
 
  var db = pddb.result;
 
  var tx = db.transaction(PersonalDataDataBaseName, "readwrite");
 
  var store = tx.objectStore(PersonalDataDataBaseName);
 
  var index = store.index("NameIndex"); 
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[  Essential recordAdd  ]||||||||||||||||||||||||||||||
 
var getRecord = index.get([record.name.last, record.name.first]);
 
getRecord.onsuccess = function() {
 
  if (getRecord.result != undefined) {
 
    console.log("updating");
 
    inp_obj = getRecord.result;
 
  } else {
 
    inp_obj = record;
 
    store.put(inp_obj);
 
  }
 
  var sanitycheck = index.get([inp_obj.name.last, inp_obj.name.first]);
 
  sanitycheck.onsuccess = function() {
 
    console.log(sanitycheck);
 
  }
 
}
 
    //|||||||||||||||||||||||||||||||||||||||||||||
 
    tx.oncomplete = function() {
 
      db.close();
 
    }; 
 
  };
 
};
 
 
 
function recordUpdate(pddbname, record, field, value){
 
  var pddb = window.indexedDB.open(pddbname, 3);
 
  pddb.onupgradeneeded = function() {
 
    var db = pddb.result;
 
    var store = db.createObjectStore(PersonalDataDataBaseName, {keyPath: "id"});
 
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
 
  };
 
  pddb.onsuccess = function() {
 
        // Start a new transaction
 
        var db = pddb.result;
 
        var tx = db.transaction(PersonalDataDataBaseName, "readwrite");
 
        var store = tx.objectStore(PersonalDataDataBaseName);
 
        var index = store.index("NameIndex");
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[  Essential recordUpdate  ]||||||||||||||||||||||||||||||
 
var inp_obj;
 
var getRecord = index.get([record.name.last, record.name.first]);
 
getRecord.onsuccess = function() {
 
  if( getRecord.result != undefined ) {
 
  console.log("updating");
 
  inp_obj = getRecord.result;
 
} else {
 
  inp_obj = record;
 
}
 
 
 
            inp_obj[field] = value;  // => "Bob"
 
            store.put(inp_obj);
 
 
 
            getRecordAgain = index.get([record.name.last, record.name.first]);
 
            getRecordAgain.onsuccess = function() {
 
                console.log(getRecordAgain);  // => "Bob"
 
                if ( getRecordAgain.result[field] == value ) {
 
                  console.log("Update succeeded");
 
                } else {
 
                  console.log("Update unsuccessful");
 
                }
 
              }; 
 
            };
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
tx.oncomplete = function() {
 
  db.close();
 
};
 
};
 
};
 
 
 
function recordCheck(pddbname, record){
 
  var pddb = window.indexedDB.open(pddbname, 3);
 
  pddb.onupgradeneeded = function() {
 
    var db = pddb.result;
 
    var store = db.createObjectStore(PersonalDataDataBaseName, {keyPath: "id"});
 
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
 
  };
 
  pddb.onsuccess = function() {
 
    var db = pddb.result;
 
    var tx = db.transaction(PersonalDataDataBaseName, "readwrite");
 
    var store = tx.objectStore(PersonalDataDataBaseName);
 
    var index = store.index("NameIndex"); 
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[ Essential recordCheck ]||||||||||||||
 
var recordcheck = index.get([record.name.last, record.name.first]);
 
recordcheck.onsuccess = function() {
 
  if ( recordcheck.result != undefined ) {
 
  console.log(recordcheck);
 
} else {
 
  console.log("...else undefined");
 
}
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
}
 
tx.oncomplete = function() {
 
  db.close();
 
}; 
 
};
 
};
 

Latest revision as of 11:14, 24 May 2019

// License: GPL
// Experimental Main
console.log("'⌣'");

var mwEStruct = {
		wbXntity: 'config.values.wbEntity'
	},

	language = "en",

	WBEStructure = {
		subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
		defaultValRelation: 'claims.P108',
		concernsRelation: 'claims.P110',
		instanceOfRelations: 'claims.P3',
		IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
		IDName: 'numeric-id', // ID property name which checked 
		qIDPropName: 'id',
		standardValue: 'labels' + '.' + language + '.' + 'value'
	},

	Relational_id_list = { 
			controller_id: [ WBEStructure['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 96 ],
			personal_data_id: [ WBEStructure['subclassOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 421 ],
			interface_button_id: [ WBEStructure ['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 487 ]
	},

	PDWikibaseProcessor,
	PDindexedDB,
	PDOOUI,
	Current_entity;


// JQ.'when' loads the current scripts asynchronously, continues when loading is 'done'
$.when(
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/WbProcessor.js&action=raw&ctype=text/javascript" ),
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/PersonalData.js&action=raw&ctype=text/javascript" ),
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/OOInterface.js&action=raw&ctype=text/javascript" ),
    $.Deferred( function( deferred ){
        $( deferred.resolve );
    })
).done( function(){

	console.log('Main thread');

	// WBproc reads current mediawiki Entity upon instantiation
	PDWikibaseProcessor = new WikibaseProcessorContainer(mw, mwEStruct['wbXntity']);

	// PDxDB takes mediawiki, reads user name from it, creates a hash, and 'checks in' the user upon instantiation
	// (i.e. read record belonging to user, if it does not exist, create it)
	PDindexedDB = new IndexedDBContainer( mw );
	
	// Contains classes for creating UI elements, no instantiation consequences
	PDOOUI = new InterfaceMediaContainer( PDWikibaseProcessor, PDindexedDB );

	// Check if the current entity belongs to any path of interest. 
	// For each element in object argument, a corresponding element in the return object will contain one of the following:
	// - undefined - the path didn't exist even partly)
	// - [ value or object ] - if the path existed fully, it returns what is at the end of the path. If last element in path is a value, it will return the value if found

	Current_entity = PDWikibaseProcessor.checkentity( Relational_id_list );
	
	// Business logicish part.
	// If any path of interest returned anything...
	if ( Current_entity != undefined ) {
		Object.keys(Current_entity).forEach( function ( entity_checked ){
			//... for each path that returned something
			if ( Current_entity[entity_checked] != undefined ){
				if ( Object.keys( Current_entity[entity_checked] ).length > 0 ) {
					PDOOUI.renderInterface( entity_checked );
					//... call the OOUI renderer to render what belongs to this input
				}
			}
		});
	}

});