MediaWiki

Sparql2data.js

Revision as of 12:58, 17 January 2022 by Yug (talk | contribs)

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* ************************************************************************** */
/* 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(); });