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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(201 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 = {
+
 
 +
language = "en",
 +
 
 +
WBEStructure = {
 
subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
 
subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
 
defaultValRelation: 'claims.P108',
 
defaultValRelation: 'claims.P108',
 +
concernsRelation: 'claims.P110',
 
instanceOfRelations: 'claims.P3',
 
instanceOfRelations: 'claims.P3',
    IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
+
IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
IDName: 'numeric-id', // ID property name which checked  
+
IDName: 'numeric-id', // ID property name which checked  
qIDPropName: 'id'
+
qIDPropName: 'id',
 +
standardValue: 'labels' + '.' + language + '.' + 'value'
 
},
 
},
mwEStruct = {
 
wbXntity: 'config.values.wbEntity'
 
};
 
  
// check if we are on a page of any of the targeted Entity
+
Relational_id_list = {
// 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
+
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;
  
if ( iterateAcheckBC(mw, mwEStruct['wbXntity'], wbEStruct['instanceOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], controller_item_id) ) {
 
console.log("This is a Data Controller");
 
controller_display_function();
 
}
 
  
// 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
+
// JQ.'when' loads the current scripts asynchronously, continues when loading is 'done'
if ( iterateAcheckBC(mw, mwEStruct['wbXntity'], wbEStruct['subclassOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], personal_data_id) ) {
+
$.when(
console.log("This is a Personal Data Type");
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/WbProcessor.js&action=raw&ctype=text/javascript" ),
input_display_function();
+
    $.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(){
  
// 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
+
console.log('Main thread');
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
+
// WBproc reads current mediawiki Entity upon instantiation
function controller_display_function(){
+
PDWikibaseProcessor = new WikibaseProcessorContainer(mw, mwEStruct['wbXntity']);
console.log( "it's a controller" )
 
var obj = JSON.parse ( mw.config.values.wbEntity );
 
var qId = obj[wbEStruct["qIDPropName"]];
 
var api = new mw.Api();
 
api.get( {
 
action: 'expandtemplates',
 
text: '{{MailtoAccess|'+qId+'}}'
 
} ).done( function ( data ) {
 
mw.notify( $('<a href="'+data.expandtemplates["*"]+'"> Do a SAR to this controller!</a>') , { autoHide: false } );
 
} );
 
}
 
  
// show input for personal data
+
// PDxDB takes mediawiki, reads user name from it, creates a hash, and 'checks in' the user upon instantiation
function input_display_function() {
+
// (i.e. read record belonging to user, if it does not exist, create it)
var obj = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
+
PDindexedDB = new IndexedDBContainer( mw );
mw.notify( $('<input type="text" placeholder="' + get_Y_from_X ( get_Y_from_X( obj, wbEStruct['defaultValRelation'] ), wbEStruct['IDPropLoc'] ) + '" id="place_0" />'), { autoHide: false } );
+
}
+
// Contains classes for creating UI elements, no instantiation consequences
 +
PDOOUI = new InterfaceMediaContainer( PDWikibaseProcessor, PDindexedDB );
  
// show maintenance options for personal data
+
// Check if the current entity belongs to any path of interest.
function data_maintenance_display() {
+
// For each element in object argument, a corresponding element in the return object will contain one of the following:
var obj = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
+
// - 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
  
// repeating code patterns
+
Current_entity = PDWikibaseProcessor.checkentity( Relational_id_list );
 
+
// 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
+
// Business logicish part.
function iterateAcheckBC( obj, rel_0, rel_A, rel_B, rel_C, to_check ) {
+
// If any path of interest returned anything...
  var future = false;
+
if ( Current_entity != undefined ) {
  if ( typeof check( obj, rel_0 ) != undefined ){
+
Object.keys(Current_entity).forEach( function ( entity_checked ){
    var edata = interjson(get_Y_from_X( obj, rel_0 ))
+
//... for each path that returned something
    if ( typeof check( edata, rel_A ) != undefined ) {
+
if ( Current_entity[entity_checked] != undefined ){
      var current = get_Y_from_X( edata, rel_A );
+
if ( Object.keys( Current_entity[entity_checked] ).length > 0 ) {
      console.log( "parsed object" )
+
PDOOUI.renderInterface( entity_checked );
      // check if we have rel_A relations and iterate through
+
//... call the OOUI renderer to render what belongs to this input
      for( var i = 0; i < current.length; i++ ) {
+
}
        console.log("dived in");
+
}
        // check if we have controller id constructed from wbEntity
+
});
        if ( typeof check( current[i], rel_B ) != undefined ) {
 
          if ( typeof check( dive (current[i], rel_B ), rel_C ) ) {
 
            if ( check_Y_in_X( to_check, get_Y_from_X( get_Y_from_X( current[i], rel_B ), rel_C ) ) ) {
 
              future = true;
 
            };
 
          }
 
        }
 
      }
 
    }
 
  }
 
  return future;
 
}
 
 
 
function get_Y_from_X( obj, B ){
 
if ( typeof check(obj, B) !=  undefined ) {
 
console.log( "entity is defined" )
 
return dive(obj, B);
 
}
 
}
 
 
 
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] === 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 = "PDIO2";
 
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
				}
			}
		});
	}

});