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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
Line 97: Line 97:
 
           textInput = [];
 
           textInput = [];
 
       var ii;
 
       var ii;
       for ( var i = 0 ; i < recordcheck(db, prs, rel_C).length; i++ ) {
+
       for ( var i = 0 ; i < recordCheck(db, prs, rel_C).length; i++ ) {
 
         ii = i + 1;
 
         ii = i + 1;
 
         button_save[i] = new OO.ui.ButtonWidget( { label: "Save"  } );
 
         button_save[i] = new OO.ui.ButtonWidget( { label: "Save"  } );

Revision as of 04:04, 1 May 2019

console.log('_');

// parts of structures from the wikibase entity used for our interest

var controller_item_id = 96,
personal_data_id = 421,
interface_button_id = 487,
language = "en";
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',
        standardValue: 'labels' + '.' + language + '.' + 'value'
      },
      mwEStruct = {
        wbXntity: 'config.values.wbEntity'
      };

      // Take username for standard databasename format
      var PersonalDataDataBaseName = "PD.IO" + mw.user.getName() + "Database";
      // Use username in user to get up to date version of self
      var CurrentPerson = {id: 0, name: {user: mw.user.getName(), first: "", last: ""}, age: 0};

      recordAdd(PersonalDataDataBaseName, CurrentPerson);
/*
      recordUpdate(PersonalDataDataBaseName, CurrentPerson, '', value);
      recordCheck(PersonalDataDataBaseName, CurrentPerson);
      recordDeleteFrom(PersonalDataDataBaseName, CurrentPerson,                         )
*/

// check if we are on a page of any of the targeted Entity
// 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
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
if ( iterateAcheckBC(mw, mwEStruct['wbXntity'], wbEStruct['subclassOfRelations'], wbEStruct['IDPropLoc'], wbEStruct['IDName'], personal_data_id) ) {
  console.log("This is a Personal Data Type");
  input_display_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
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
function controller_display_function(){
  console.log( "it's a controller" )
  var obj2 = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
  var qId = dive( obj2, 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 data controller!</a>') , { autoHide: false } );
  } );
}

// show input for personal data
function input_display_function() {
  var obj = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) );
  var string_temporary_store_1 = get_Y_from_X( obj, wbEStruct['standardValue'] );
  var string_temporary_store_2 = get_Y_from_X ( get_Y_from_X ( get_Y_from_X( obj, wbEStruct['defaultValRelation'] ), '0'), wbEStruct['IDPropLoc'] );
  var string_temporary_store_3 = 0;

  mw.notify( 
    $('<input type="text" placeholder="' + string_temporary_store_2 + '" id="' + string_temporary_store_1 + '_' + 0 + '" />'), { autoHide: false } );
}

// show maintenance options for personal data
function data_maintenance_display() {
  var obj = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) ); // wbEntity as json obj
  var string_temporary_store_1 = get_Y_from_X( obj, wbEStruct['standardValue'] );  // input name
  var string_temporary_store_2 = get_Y_from_X ( get_Y_from_X ( get_Y_from_X( obj, wbEStruct['defaultValRelation'] ), '0'), wbEStruct['IDPropLoc'] ); //placeholder_text
  var string_temporary_store_3 = 0; //get user value & relevant object parts here
  interface_media( PersonalDataDataBaseName, CurrentPerson, string_temporary_store_1, string_temporary_store_2, string_temporary_store_3 );
}

// repeating code patterns

function interface_media( db /* database */, prs /* user */ , rel_A /* current value location */, rel_B /* placeholder value */, rel_C /* which attribute it is in the inddb database */ ) {
  mw.loader.using( 'oojs-ui-core' ).done( function () {
    $( function () {
      
      var person = recordCheck( db, prs );
//    var data = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) ); // wbEntity as json obj

      var button = [],
          textInput = [];
      var ii;
      for ( var i = 0 ; i < recordCheck(db, prs, rel_C).length; i++ ) {
        ii = i + 1;
        button_save[i] = new OO.ui.ButtonWidget( { label: "Save"  } );
        button_new[i] = new OO.ui.ButtonWidget( { label: "New" } );
        button_delete[i] = new OO.ui.ButtonWidget( { label: "Delete" } );
        textInput[i] = new OO.ui.TextInputWidget( { 
          value: check( person, rel_C + '.' + i ),
          placeholder: rel_B  
        } );
        $(textInput[i]).on("keydown",function search(e) {
            if(e.keyCode == 13) {
                   recordUpdate( db, prs , rel_C + '.' + i , ($(this).val())  );
            }
        });
        button_new[i].on( 'click', function () {
    
          // new 
          recordUpdate( db, prs, rel_C + '.' + ii , '');
          interface_media( db, prs, rel_A, rel_B, rel_C );
          return;

        } );
        button_delete[i].on( 'click', function () {
    
          // delete 
          recordDeleteFrom( db, prs,  rel_C + '.' + i );
          interface_media( db, prs, rel_A, rel_B, rel_C );
          return;

        } );
        button_save[i].on( 'click', function () {
          
          // save
          recordUpdate( db, prs, rel_C + '.' + i , textInput[i].val()) ;
          interface_media( db, prs, rel_A, rel_B, rel_C );
          return;

        } );
        $( '#mw-content-text' ).prepend( button_new[i].$element );
        $( '#mw-content-text' ).prepend( button_delete[i].$element );
        $( '#mw-content-text' ).prepend( button_save[i].$element );
        $( '#mw-content-text' ).prepend( textInput[i].$element );
        $( '#mw-content-text' ).prepend( '<br>' );

      }

//
/*

      recordCheck(PersonalDataDataBaseName, CurrentPerson);

*/
//

    } );
  } );
}

// 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
function iterateAcheckBC( obj, rel_0, rel_A, rel_B, rel_C, to_check ) {
  var future = false;
  if ( check( obj, rel_0 ) != undefined ) {
    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;
}

// get a.b from a and b
function get_Y_from_X( obj, B ){
  if ( check(obj, B) !==  undefined ) {
    console.log( "entity is defined" )
    return dive(obj, B);
  }
}

// try to convert something into JSON
function interjson( obj ) {
  try {
    return JSON.parse( obj )
  } catch (e) {
    // if not JSON, do silly things
  };
}

//find second argument in first argument as a boolean
function check_Y_in_X(obj, to_check){
  if (obj === to_check) { return true }; 
  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
// Add new record (create pddbname@record) if it does not exist. 
// If it exists, the contents of record are disregarded, and the indexeddb version is returned

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.user"]);
  };
  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  ]||||||||||||||||||||||||||||||

    // Get the original record from the db...
    var getRecord = index.get([record.name.user]);
    getRecord.onsuccess = function() {
      if (getRecord.result != undefined) {
        // if found, let's use that from now on
        console.log("updating");
        inp_obj = getRecord.result;
        // let's return it so that our addrecord can be used as a getter as well
        return inp_obj;
      } else {
        // if it didn't exist, let's use the provided record to push to store
        inp_obj = record;
        store.put(inp_obj);
      }
      var sanitycheck = index.get([inp_obj.name.user]);
      sanitycheck.onsuccess = function() {
        console.log(sanitycheck);
      }
    }

    //|||||||||||||||||||||||||||||||||||||||||||||

    tx.oncomplete = function() {
      db.close();
    };  
  };
};

// Update pddbname@record:field value
// Use store copy if exists
// If record does not exist, create it
// Otherwise all other fields of the record object are disregarded
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.user"]);
  };
  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.user]);
        // let's try to get the record to be updated from the store
        getRecord.onsuccess = function() {
          if( getRecord.result != undefined ) {
            // if we have it, use that copy to update on
            console.log("updating");
            inp_obj = getRecord.result; 
          } else {
            // if it does not exist in store, use the given record
           inp_obj = record;
         }
         //give the value to the field to update
        inp_obj[field] = value;   // => "Bob"
         // write new version of record back to database
        store.put(inp_obj);


        // perform a sanity check if the update was successful, could be used to initiate a retry upon false
        getRecordAgain = index.get([record.name.user]);
        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.user"]);
  };
  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.user]);
    recordcheck.onsuccess = function() {
      if ( recordcheck.result != undefined ) {
        console.log(recordcheck);
        return recordCheck;
      } else {
        console.log("...else undefined");
        return undefined;
      };
    };

//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

    tx.oncomplete = function() {
      db.close();
    };  
  };
};

function recordDeleteFrom(pddbname, record,  field ){
  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.user"]);
  };
  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 inp_obj;
    var recordcheck = index.get([record.name.user]);
    recordcheck.onsuccess = function() {
      if ( recordcheck.result != undefined ) {
           console.log("updating");
           inp_obj = recordcheck.result; 
          } else {
           inp_obj = record;
          }
        delete check( inp_obj, field );   // => "Bob"
        store.put(inp_obj);
    }

//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

    tx.oncomplete = function() {
      db.close();
    };  
  };
};