MediaWiki

Difference between revisions of "Sparql2data.js"

m
m
Line 7: Line 7:
 
// Documentations:  https://www.mediawiki.org/wiki/API:Edit#MediaWiki_JS
 
// Documentations:  https://www.mediawiki.org/wiki/API:Edit#MediaWiki_JS
 
// Author: Yug
 
// Author: Yug
 
var endpoint = 'https://lingualibre.org/sparql';
 
var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10';
 
var format = 'json';
 
var wikipage = 'MediaWiki:Mydata.js';
 
 
 
var data2wikipage = function(data,wikipage) {
 
    var params = {
 
            action: 'edit',
 
            title: wikipage,
 
            text: "Yug test!",
 
            format: 'json'
 
        },
 
    api = new mw.Api();
 
 
    api.formattWithToken( 'csrf', params ).done( function ( data ) {
 
        console.log( data );
 
    } );
 
}
 
 
var sparql2data = async function(sparql, wikipage, format, endpoint) {
 
wikipage = wikipage || 'Sandbox';
 
format = format || 'json';
 
    endpoint = endpoint || 'https://lingualibre.org/sparql';
 
    var res = await $.getJSON(endpoint,
 
        { query: sparql, format: format },
 
        function(data, wikipage){
 
        console.log('JQuery: ',data)
 
        data2wikipage(data,wikipage);
 
        }
 
    );
 
}
 
 
 
 
 
 
// Toolbox
 
 
/* ********************************************************************** */
 
/* QUERIES BASIS ******************************************************** */
 
// Current: Wikidata, Dbnary.
 
 
   
 
   
 
/* ********************************************************************** */
 
/* ********************************************************************** */
Line 140: Line 97:
 
       ]
 
       ]
 
     }),{
 
     }),{
label: 'Based on heavy SPAQRL query, update the raw data of the target wikipage',
+
label: 'Based on heavy SPARQL query, update the raw data of the target wikipage',
 
align: 'top'
 
align: 'top'
 
})
 
})
Line 152: Line 109:
 
console.log(identity+'a getElementId(): ',element.getElementId())
 
console.log(identity+'a getElementId(): ',element.getElementId())
 
console.log(identity+'b getData(): ',element.getData())
 
console.log(identity+'b getData(): ',element.getData())
  console.log(identity+'c getValue(): ',element.getValue())
+
    console.log(identity+'c getValue(): ',element.getValue())
 
}
 
}
  
 
$('#s2d-wikipage').on('focusout',function() {checkAvailableData(1,wikipage);})
 
$('#s2d-wikipage').on('focusout',function() {checkAvailableData(1,wikipage);})
  
 +
 +
/* ********************************************************************** */
 +
/* SPARQL QUERIES & WIKI EDIT ******************************************* */
 +
//
 +
var endpoint = 'https://lingualibre.org/sparql';
 +
var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10';
 +
var format = 'json';
 +
var wikipage = 'MediaWiki:Mydata.js';
 +
 +
var data2wikipage = function(data,wikipage) {
 +
    var params = {
 +
            action: 'edit',
 +
            title: wikipage,
 +
            text: "Yug test!",
 +
            format: 'json'
 +
        },
 +
    api = new mw.Api();
 +
    api.formattWithToken( 'csrf', params ).done( function ( data ) {
 +
        console.log( data );
 +
    } );
 +
}
 +
 +
var sparql2data = function(sparql, wikipage, format, endpoint) {
 +
wikipage = wikipage || 'Sandbox';
 +
format = format || 'json';
 +
    endpoint = endpoint || 'https://lingualibre.org/sparql';
 +
    var res = $.getJSON(endpoint,
 +
        { query: sparql, format: format },
 +
        function(data, wikipage){
 +
        console.log('JQuery: ',data)
 +
        data2wikipage(data,wikipage);
 +
        }
 +
    );
 +
}
  
 
/* ********************************************************************** */
 
/* ********************************************************************** */
 
/* GENERATE QUERY STRING ************************************************ */
 
/* GENERATE QUERY STRING ************************************************ */
 
// Current: Wikidata, Dbnary.
 
// Current: Wikidata, Dbnary.
var generateQuery = function () {
+
// getData from elements
  // getData from elements
+
var selectedDropdown = function (group){
  var selectedDropdown = function (group){
 
 
     var items = group.getMenu().items.filter(item=> item.selected==true )
 
     var items = group.getMenu().items.filter(item=> item.selected==true )
 
     return items[0]?items[0].data:null;
 
     return items[0]?items[0].data:null;
  }
+
}
  var selectedRadio = function (group){
+
var selectedRadio = function (group){
 
     var items = group.getMenu().items.filter(item=> item.selected==true )
 
     var items = group.getMenu().items.filter(item=> item.selected==true )
 
     return items[0]?items[0].data:null;
 
     return items[0]?items[0].data:null;
  }
+
}
  var qid2value = function (arr,qid,field){
+
var qid2value = function (arr,qid,field){
 
     field = field || 'label'
 
     field = field || 'label'
 
     var match = arr.filter(item=> item.data==qid);
 
     var match = arr.filter(item=> item.data==qid);
 
     return match[0]?match[0][field]:null;
 
     return match[0]?match[0][field]:null;
  }
+
}
 +
var endpoint = function(key){ return key == 'wikidata'? 'https://query.wikidata.org/sparql' : 'https://lingualibre.org/sparql'; }
 +
 
 +
var formStatus = function(wikipage,format,limit,endpoints,queryDisplay) {
 
   var form = {
 
   var form = {
 
     wikipage: wikipage.getValue(),//string
 
     wikipage: wikipage.getValue(),//string
 
     format: selectedDropdown(format),//Qid
 
     format: selectedDropdown(format),//Qid
     formatLabel: qid2value(formatArray,selectedDropdown(format)),//noun
+
     //formatLabel: qid2value(formatArray,selectedDropdown(format)),//noun
 
     limit: limit.isSelected(),//boolean
 
     limit: limit.isSelected(),//boolean
 
     endpoints: endpoints.getValue(),//wikidata|dbnary
 
     endpoints: endpoints.getValue(),//wikidata|dbnary
 
     queryDisplay: queryDisplay.getValue(),//string
 
     queryDisplay: queryDisplay.getValue(),//string
 
   };
 
   };
  console.log(form)
+
      console.log('formStatus: ',form)
 +
  return form;
 +
}
 +
 
 +
var setQuery = function (form) {
 
   //  console.log(form);
 
   //  console.log(form);
   var query = form.queryDisplay;
+
   var query = form.queryDisplay
// Identify suitable query
+
  .concat(form.limit?`\nLIMIT 100`:'');
  query = '';
+
       
 
+
sparql2data(form.sparql, form.wikipage, form.format, endpoint(form.endpoints))
  // Edit suitable query 
 
  query = query.concat(form.limit?`\nLIMIT 100`:'');
 
 
 
 
   // Inject query
 
   // Inject query
 
   queryDisplay.setValue(query);
 
   queryDisplay.setValue(query);
 
  }
 
  }
 +
 +
var form = formStatus(wikipage,format,limit,endpoints,queryDisplay);
 +
fetchQuery(form);
  
 
/* ********************************************************************** */
 
/* ********************************************************************** */
 
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
 
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
 
// Current: Wikidata, Dbnary. Broken: lingualibre.
 
// Current: Wikidata, Dbnary. Broken: lingualibre.
var runQuery = function () {
+
var postQuery = function () {
 
var endpointLabel =  endpoints.getValue(),
 
var endpointLabel =  endpoints.getValue(),
 
     queryText = queryDisplay.getValue(),
 
     queryText = queryDisplay.getValue(),
Line 211: Line 208:
 
   }
 
   }
 
  }
 
  }
  $('#s2d-run').on('click',function(){ runQuery(); });
+
  $('#s2d-generate').on('click',function(){ fetchQuery(); });
  $('#s2d-generate').on('click',function(){ generateQuery(); });
+
  $('#s2d-run').on('click',function(){ postQuery(); });

Revision as of 12:58, 17 January 2022

/* ************************************************************************** */
/* Sparql2data script ******************************************* */
// Description: given a sparql query,a wikipage, a sparql enpoints, a format, will formatt the data on that wikipage.
// Usage: [[Template:Sparql2data]]
// Usage: open [[Template:Sparql2data]] > fill the form > click "Fectch data", review if it looks normal, then click "Update data !"
// Hack pad: https://jsfiddle.net/hugolpz/x4qmcudj/
// Documentations:  https://www.mediawiki.org/wiki/API:Edit#MediaWiki_JS
// Author: Yug
 
/* ********************************************************************** */
/* OOJS / OOUI ********************************************************** */
// Data & options
var formatArray = [
      { data: 'json', label: 'json' },
      { data: 'xml', label: 'xml' },
      { data: 'csv', label: 'csv' },
    ].map(o => new OO.ui.MenuOptionWidget({ data: o.data, label: o.label })),
    endpointsArray = [
      { data: 'wikidata', label: 'Wikidata', selected: true },
      { data: 'LinguaLibre', label: 'LinguaLibre' },
    ];

// Elements
var wikipage = new OO.ui.TextInputWidget({
    id: 's2d-wikipage',
    icon: 'book',
    placeholder: 'Sandbox',
    value: 'Sandbox',
		label: 'Data hosting page',
  }),
  format = new OO.ui.DropdownWidget( {
    id: 's2d-format',
		label: 'Data format',
		menu: { items: formatArray }
	}),
  endpoints = new OO.ui.RadioSelectInputWidget( { 
    id: 's2d-endpoints',
  	label: 'Radios buttons',
    options: endpointsArray,
} ),
  limit = new OO.ui.CheckboxInputWidget({ id: 's2d-limit', selected: true, }),
  limitLabel = new OO.ui.LabelWidget( { label: 'Limit to 100 (faster)' }),
  queryDisplay = new OO.ui.MultilineTextInputWidget( { 
    id: 's2d-queryDisplay',
		//value: `# Query will appear here.\n\n\n\n\n\n`,
    placeholder: `# SPARQL query here.\n\n\n\n\n\n`,
		multiline: true,
		autosize: true,
    minRows: 10,
		maxRows: 20
	} ),
  dataDisplay = new OO.ui.MultilineTextInputWidget( { 
    id: 's2d-dataDisplay',
    placeholder: `# Data will appear here.\n\n\n\n\n\n`,
		multiline: true,
		autosize: true,
    minRows: 10,
		maxRows: 20
	} ),
  generate = new OO.ui.ButtonWidget({ 
  	id: 's2d-generate', 
    label: 'Fetch data',
  }),
  run = new OO.ui.ButtonWidget({ 
    id: 's2d-run',
    label:'Update data !',
  });
  
// An example of a fieldset with horizontal layout.
var fieldset = new OO.ui.FieldsetLayout( {
		label: 'Sparql to stored data tool'
	} );
fieldset.addItems( [ 
  new OO.ui.FieldLayout(
    new OO.ui.Widget( {
      content: [ 
        new OO.ui.HorizontalLayout( { items: [
          wikipage,  // keep -> wikipage
          format,  // keep -> format
      //    languages,
      //    infoTypes,
      //    translations,
        ]}),
        new OO.ui.HorizontalLayout( { items: [
          generate, // REPLACE BY FETCH SPARQL ?
        	run,  // keep
          limit,  // keep
          limitLabel,  // keep
          endpoints,  // keep
        ]}),
        new OO.ui.HorizontalLayout( { items: 
        	[ queryDisplay ]  // keep -> sparql_query
        }),
        new OO.ui.HorizontalLayout( { items: 
        	[ dataDisplay ]  // keep -> data
        }),
      ]
    }),{
			label: 'Based on heavy SPARQL query, update the raw data of the target wikipage',
			align: 'top'
		})
	]
);
$( "#s2d" ).append( fieldset.$element );

// Check data
var checkAvailableData = function(identity,element){
	// console.log('2',languages)
	console.log(identity+'a getElementId(): ',element.getElementId())
	console.log(identity+'b getData(): ',element.getData())
    console.log(identity+'c getValue(): ',element.getValue())
}

$('#s2d-wikipage').on('focusout',function() {checkAvailableData(1,wikipage);})


/* ********************************************************************** */
/* SPARQL QUERIES & WIKI EDIT ******************************************* */
// 
var endpoint = 'https://lingualibre.org/sparql';
var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10';
var format = 'json';
var wikipage = 'MediaWiki:Mydata.js';

var data2wikipage = function(data,wikipage) {
    var params = {
            action: 'edit',
            title: wikipage,
            text: "Yug test!",
            format: 'json'
        },
    api = new mw.Api();
    api.formattWithToken( 'csrf', params ).done( function ( data ) {
        console.log( data );
    } );
}

var sparql2data = function(sparql, wikipage, format, endpoint) {
	wikipage = wikipage || 'Sandbox';
	format = format || 'json';
    endpoint = endpoint || 'https://lingualibre.org/sparql';
    var res = $.getJSON(endpoint,
        { query: sparql, format: format },
        function(data, wikipage){ 
        	console.log('JQuery: ',data)
        	data2wikipage(data,wikipage);
        }
    );
}

/* ********************************************************************** */
/* GENERATE QUERY STRING ************************************************ */
// Current: Wikidata, Dbnary.
// getData from elements
var selectedDropdown = function (group){
    var items = group.getMenu().items.filter(item=> item.selected==true )
    return items[0]?items[0].data:null;
}
var selectedRadio = function (group){
    var items = group.getMenu().items.filter(item=> item.selected==true )
    return items[0]?items[0].data:null;
}
var qid2value = function (arr,qid,field){
    field = field || 'label'
    var match = arr.filter(item=> item.data==qid);
    return match[0]?match[0][field]:null;
}
var endpoint = function(key){ return key == 'wikidata'? 'https://query.wikidata.org/sparql' : 'https://lingualibre.org/sparql'; }

var formStatus = function(wikipage,format,limit,endpoints,queryDisplay) {
   var form = {
     wikipage: wikipage.getValue(),//string
     format: selectedDropdown(format),//Qid
     //formatLabel: qid2value(formatArray,selectedDropdown(format)),//noun
     limit: limit.isSelected(),//boolean
     endpoints: endpoints.getValue(),//wikidata|dbnary
     queryDisplay: queryDisplay.getValue(),//string
   };
      console.log('formStatus: ',form)
   return form;
}

var setQuery = function (form) {
   //  console.log(form);
   var query = form.queryDisplay
   		.concat(form.limit?`\nLIMIT 100`:'');
        
	sparql2data(form.sparql, form.wikipage, form.format, endpoint(form.endpoints)) 
   // Inject query
   queryDisplay.setValue(query);
 }
 
 var form = formStatus(wikipage,format,limit,endpoints,queryDisplay);
fetchQuery(form);

/* ********************************************************************** */
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
// Current: Wikidata, Dbnary. Broken: lingualibre.
var postQuery = function () {
		var endpointLabel =  endpoints.getValue(),
    queryText = queryDisplay.getValue(),
    queryEncoded = encodeURIComponent(queryText),
    baseEndpointUrl = 
    	endpointLabel == 'wikidata'? 'https://query.wikidata.org/#'
      :'http://kaiko.getalp.org/sparql?default-graph-uri=&query=';
   if (queryText) {
       window.open(baseEndpointUrl.concat(queryEncoded), '_blank');
   }
 }
 $('#s2d-generate').on('click',function(){ fetchQuery(); });
 $('#s2d-run').on('click',function(){ postQuery(); });