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

From Wikibase Personal data
Jump to navigation Jump to search
m
m (Replaced content with "// License: GPL console.log("'⌣'");")
Tag: Replaced
Line 1: Line 1:
 
// License: GPL
 
// License: GPL
−
console.log('_');
+
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',
 
−
        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'
 
−
    },
 
−
    mwEStruct = {
 
−
      wbXntity: 'config.values.wbEntity'
 
−
    };
 
−
 
 
−
      hashCode = function(s) {
 
−
          var h = 0, l = s.length, i = 0;
 
−
          if ( l > 0 )
 
−
            while (i < l)
 
−
              h = (h << 5) - h + s.charCodeAt(i++) | 0;
 
−
          return h;
 
−
      };
 
−
 
 
−
     
 
−
      var self = mw.user.getName();
 
−
     
 
−
      var idgen = hashCode(self);
 
−
 
 
−
      console.log(idgen);
 
−
      // Take username for standard databasename format
 
−
      var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
 
−
      // Use username in user to get up to date version of self
 
−
      var CurrentPerson = {id: idgen, name: {user: self}, age: 0};
 
−
 
 
−
      recordAdd( PersonalDataDataBaseName, CurrentPerson );
 
−
      recordCheck( 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;
 
−
  console.log('*-_-*');
 
−
}
 
−
 
 
−
// 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 = get_Y_from_X ( get_Y_from_X ( get_Y_from_X ( get_Y_from_X( obj, wbEStruct['concernsRelation'] ), '0'), wbEStruct['IDPropLoc']), wbEStruct['IDName'] ); //property name
 
−
  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 - concerns for input item */ ) {
 
−
  mw.loader.using( 'oojs-ui-core' ).done( function () {
 
−
    $( function () {
 
−
      var person = {};
 
−
      person = prs;
 
−
      if ( recordCheck( db, prs ) != undefined ) {
 
−
        person = recordCheck( db, prs );
 
−
      }
 
−
      prs = person;
 
−
     
 
−
      console.log( person );
 
−
 
 
−
//    var data = interjson( get_Y_from_X( mw, mwEStruct['wbXntity'] ) ); // wbEntity as json obj
 
−
 
 
−
      var
 
−
          deletButton = [],
 
−
          deletTextInput = [],
 
−
         
 
−
          button_save = [],
 
−
          button_delete = [],
 
−
          button_new = [],
 
−
          textInput = [],
 
−
          ii = Object.keys( check( person, rel_C + '' ) ).length;
 
−
 
 
−
      console.log( "Mark Finite" );
 
−
      console.log( rel_C );
 
−
      if ( check( person, rel_C + '' ) !== undefined ) {
 
−
        for ( var i = 0 ; i <= ii ; i++ ) {
 
−
          if ( check( person, rel_C + '.' + i) != undefined ) {
 
−
           
 
−
            button_save[i] = new OO.ui.ButtonWidget( { label: "Save", class: "pddh"  } );
 
−
            button_new[i] = new OO.ui.ButtonWidget( { label: "New", class: 'pddh' } );
 
−
            button_delete[i] = new OO.ui.ButtonWidget( { label: "Delete", class: "pddh" } );
 
−
            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, person , rel_C, i , ($(this).val())  );
 
−
                }
 
−
                interface_media( db, person, rel_A, rel_B, rel_C );
 
−
            });
 
−
 
 
−
            console.log( textInput[i].getValue() );
 
−
 
 
−
            button_new[i].on( 'click', function () {
 
−
       
 
−
              // new
 
−
              recordUpdate( db, person, rel_C, ii , '2' );
 
−
              interface_media( db, person, rel_A, rel_B, rel_C );
 
−
              return;
 
−
 
 
−
            } );
 
−
            button_delete[i].on( 'click', function () {
 
−
       
 
−
              // delete
 
−
              recordDeleteFrom( db, person, rel_C, i );
 
−
              interface_media( db, person, rel_A, rel_B, rel_C );
 
−
              return;
 
−
 
 
−
            } );
 
−
            button_save[i].on( 'click', function () {
 
−
             
 
−
              // save
 
−
              recordUpdate( db, person, rel_C, i , textInput[i].getValue() ) ;
 
−
              interface_media( db, person, rel_A, rel_B, rel_C );
 
−
              return;
 
−
 
 
−
            } );
 
−
            $( '#mw-content-text' ).prepend( textInput[i].$element );
 
−
            $( '#mw-content-text' ).prepend( '<br>' );
 
−
            $( '#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( '<br>' );
 
−
            $( '#mw-content-text' ).prepend( '<br>' );
 
−
 
 
−
          } else {
 
−
 
 
−
          };
 
−
        };
 
−
      };
 
−
    } );
 
−
  } );
 
−
};
 
−
 
 
−
// 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 recordDelta( pddbname, record, switch, control_1, control_2, control_3 ){
 
−
  console.log(record);
 
−
  var pddb = window.indexedDB.open(pddbname, 3);
 
−
  pddb.onupgradeneeded = function() {
 
−
  var db = pddb.result;
 
−
  var store = db.createObjectStore(pddbname, {keyPath: "id"});
 
−
 
 
−
  // shouldn't the index be the wiki.personaldata.io username?
 
−
 
 
−
  var index = store.createIndex("NameIndex", ["id"]);
 
−
  };
 
−
  pddb.onsuccess = function() {
 
−
    var db = pddb.result;
 
−
    var tx = db.transaction(pddbname, "readwrite");
 
−
    var store = tx.objectStore(pddbname);
 
−
    var index = store.index("NameIndex"); 
 
−
 
 
−
 
 
−
 
 
−
    // Get the original record from the db...
 
−
    var getRecord = index.get([record.id]);
 
−
    var inp_obj;
 
−
    getRecord.onsuccess = function() {
 
−
 
 
−
 
 
−
  //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[  Essential recordAdd  ]||||||||||||||||||||||||||||||     
 
−
      if ( switch === "aggressive push" ) {
 
−
        inp_obj = record;
 
−
        store.put( inp_obj );
 
−
        return inp_obj;
 
−
      } else {
 
−
 
 
−
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[  Essential recordCheck  ]||||||||||||||||||||||||||||||
 
−
 
 
−
        var getRecord = index.get([record.id]);
 
−
        getRecord.onsuccess = function() {
 
−
          if (getRecord.result != undefined) {     
 
−
           
 
−
            if ( switch === "get record" ) {
 
−
              // 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
 
−
 
 
−
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[  Essential recordUpdate  ]||||||||||||||||||||||||||||||
 
−
            Nif ( switch ==== "update record" )
 
−
            if ( typeof control_1 != undefined ) {
 
−
              if ( typeof control_2 != undefined ) {
 
−
                if ( typeof control_3 != undefined ) {
 
−
                    inp_obj[ control_1 ][ control_2 ] = control_3;  // => "Bob"
 
−
                    // write new version of record back to database
 
−
                    store.put(inp_obj);
 
−
                  } else {
 
−
                    if ( Array.isArray( inp_obj[ control_1 ] ) ){
 
−
                      inp_obj[ control_1 ].push( control_2 );
 
−
                    } else {
 
−
                      inp_obj[ control_1 ] = control_2;
 
−
                    }
 
−
                  }
 
−
    ////
 
−
    // +3
 
−
                }
 
−
      // +2
 
−
              }
 
−
      // +1
 
−
            }
 
−
      //
 
−
          }
 
−
      // endof onsuccess
 
−
        }
 
−
      // endof resultdefined 
 
−
      } else {
 
−
            // if it does not exist in store, use the given record
 
−
          inp_obj = record;
 
−
      }
 
−
  // endofELSE
 
−
    }
 
−
 
 
−
    // perform a sanity check if the update was successful, could be used to initiate a retry upon false
 
−
    getRecordAgain = index.get([inp_obj.id]);
 
−
    getRecordAgain.onsuccess = function() {
 
−
      console.log(getRecordAgain);  // => "Bob"
 
−
      if ( getRecordAgain.result[field] == value ) {
 
−
        console.log("Update succeeded");
 
−
      } else {
 
−
        console.log("Update unsuccessful");
 
−
      }
 
−
    };
 
−
 
 
−
    //}
 
−
 
 
−
      var sanitycheck = index.get([inp_obj.id]);
 
−
      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, cellnumber, value ) {
 
−
  console.log("Add " + pddbname + '@' + record.id + ":" + field + '/w:' + value);
 
−
  var pddb = window.indexedDB.open(pddbname, 3);
 
−
  pddb.onupgradeneeded = function() {
 
−
    var db = pddb.result;
 
−
    var store = db.createObjectStore(pddbname, {keyPath: "id"});
 
−
    var index = store.createIndex("NameIndex", ["id"]);
 
−
  };
 
−
  pddb.onsuccess = function() {
 
−
        // Start a new transaction
 
−
        var db = pddb.result;
 
−
        var tx = db.transaction(pddbname, "readwrite");
 
−
        var store = tx.objectStore(pddbname);
 
−
        var index = store.index("NameIndex");
 
−
 
 
−
    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(pddbname, {keyPath: "id"});
 
−
    var index = store.createIndex("NameIndex", ["id"]);
 
−
  };
 
−
  pddb.onsuccess = function() {
 
−
    var db = pddb.result;
 
−
    var tx = db.transaction(pddbname, "readwrite");
 
−
    var store = tx.objectStore(pddbname);
 
−
    var index = store.index("NameIndex"); 
 
−
 
 
−
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[ Essential recordCheck ]||||||||||||||
 
−
 
 
−
    var recordcheck = index.get([record.id]);
 
−
    recordcheck.onsuccess = function() {
 
−
      if ( recordcheck.result != undefined ) {
 
−
        console.log(recordcheck);
 
−
        return recordcheck.result;
 
−
      } else {
 
−
        console.log("...else undefined");
 
−
        return undefined;
 
−
      };
 
−
    };
 
−
 
 
−
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
−
 
 
−
    tx.oncomplete = function() {
 
−
      db.close();
 
−
    }; 
 
−
  };
 
−
};
 
−
 
 
−
function recordDeleteFrom(pddbname, record,  field, cellnumber ){
 
−
  var pddb = window.indexedDB.open(pddbname, 3);
 
−
  pddb.onupgradeneeded = function() {
 
−
    var db = pddb.result;
 
−
    var store = db.createObjectStore(pddbname, {keyPath: "id"});
 
−
    var index = store.createIndex("NameIndex", ["id"]);
 
−
  };
 
−
  pddb.onsuccess = function() {
 
−
    var db = pddb.result;
 
−
    var tx = db.transaction(pddbname, "readwrite");
 
−
    var store = tx.objectStore(pddbname);
 
−
    var index = store.index("NameIndex"); 
 
−
 
 
−
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[ Essential recordDeleteFrom ]||||||||||||||
 
−
   
 
−
    var inp_obj;
 
−
    var recordcheck = index.get([record.id]);
 
−
    recordcheck.onsuccess = function() {
 
−
      if ( recordcheck.result != undefined ) {
 
−
          console.log("updating");
 
−
          inp_obj = recordcheck.result;
 
−
          } else {
 
−
          inp_obj = record;
 
−
          }
 
−
      delete inp_obj[field][cellnumber];  // => "Bob"
 
−
      store.put( inp_obj );
 
−
   
 
−
      getRecordAgain = index.get([inp_obj.id]);
 
−
      getRecordAgain.onsuccess = function() {
 
−
        console.log(getRecordAgain);  // => "Bob"
 
−
        if ( getRecordAgain.result[field][cellnumber] == undefined ) {
 
−
          console.log("Delete succeeded");
 
−
        } else {
 
−
          console.log("Delete unsuccessful");
 
−
        }
 
−
      };
 
−
    }
 
−
 
 
−
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
−
 
 
−
    tx.oncomplete = function() {
 
−
      db.close();
 
−
    }; 
 
−
  };
 
−
};
 

Revision as of 23:55, 13 May 2019

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