MediaWiki

Difference between revisions of "Sparql2data.js"

m
m
Line 15: Line 15:
 
       { data: 'xml', label: 'xml' },
 
       { data: 'xml', label: 'xml' },
 
       { data: 'csv', label: 'csv' },
 
       { data: 'csv', label: 'csv' },
 +
      { data: 'tsv', label: 'tsv' },
 
     ].map(o => new OO.ui.MenuOptionWidget({ data: o.data, label: o.label })),
 
     ].map(o => new OO.ui.MenuOptionWidget({ data: o.data, label: o.label })),
 
     endpointsArray = [
 
     endpointsArray = [
       { data: 'wikidata', label: 'Wikidata', selected: true },
+
       { data: 'https://lingualibre.org/sparql', label: 'LinguaLibre'},
       { data: 'LinguaLibre', label: 'LinguaLibre' },
+
       { data: 'https://query.wikidata.org/sparql', label: 'Wikidata'},
 
     ];
 
     ];
  
Line 24: Line 25:
 
var wikipage = new OO.ui.TextInputWidget({
 
var wikipage = new OO.ui.TextInputWidget({
 
     id: 's2d-wikipage',
 
     id: 's2d-wikipage',
     icon: 'book',
+
     icon: 'specialPages',
 
     placeholder: 'Sandbox',
 
     placeholder: 'Sandbox',
 
     value: 'Sandbox',
 
     value: 'Sandbox',
Line 31: Line 32:
 
   format = new OO.ui.DropdownWidget( {
 
   format = new OO.ui.DropdownWidget( {
 
     id: 's2d-format',
 
     id: 's2d-format',
 +
    icon: 'code',
 
label: 'Data format',
 
label: 'Data format',
 
menu: { items: formatArray }
 
menu: { items: formatArray }
Line 45: Line 47:
 
//value: `# Query will appear here.\n\n\n\n\n\n`,
 
//value: `# Query will appear here.\n\n\n\n\n\n`,
 
     placeholder: `# SPARQL query here.\n\n\n\n\n\n`,
 
     placeholder: `# SPARQL query here.\n\n\n\n\n\n`,
 +
    value: `SELECT ?item ?itemLabel WHERE {
 +
  SERVICE wikibase:label {
 +
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
 +
  }
 +
  ?item prop:P2 entity:Q5
 +
} `,
 
multiline: true,
 
multiline: true,
 
autosize: true,
 
autosize: true,
Line 50: Line 58:
 
maxRows: 20
 
maxRows: 20
 
} ),
 
} ),
 +
  fetchData = new OO.ui.ButtonWidget({
 +
  id: 's2d-fetchData',
 +
    label: 'Fetch data',
 +
  }),
 
   dataDisplay = new OO.ui.MultilineTextInputWidget( {  
 
   dataDisplay = new OO.ui.MultilineTextInputWidget( {  
 
     id: 's2d-dataDisplay',
 
     id: 's2d-dataDisplay',
     placeholder: `# Data will appear here.\n\n\n\n\n\n`,
+
     placeholder: `# Data will appear here after a while.\n\n\n\n\n\n`,
 
multiline: true,
 
multiline: true,
 
autosize: true,
 
autosize: true,
Line 58: Line 70:
 
maxRows: 20
 
maxRows: 20
 
} ),
 
} ),
   generate = new OO.ui.ButtonWidget({
+
   postData = new OO.ui.ButtonWidget({  
  id: 's2d-generate',
+
     id: 's2d-postData',
    label: 'Fetch data',
 
  }),
 
  run = new OO.ui.ButtonWidget({  
 
     id: 's2d-run',
 
 
     label:'Update data !',
 
     label:'Update data !',
 
   });
 
   });
Line 76: Line 84:
 
       content: [  
 
       content: [  
 
         new OO.ui.HorizontalLayout( { items: [
 
         new OO.ui.HorizontalLayout( { items: [
           wikipage, // keep -> wikipage
+
           wikipage,
           format, // keep -> format
+
           format,
      //    languages,
+
        ]}),
      //    infoTypes,
+
        new OO.ui.HorizontalLayout( { items: [
      //    translations,
+
        queryDisplay,
 
         ]}),
 
         ]}),
 
         new OO.ui.HorizontalLayout( { items: [
 
         new OO.ui.HorizontalLayout( { items: [
           generate, // REPLACE BY FETCH SPARQL ?
+
           fetchData,
        run,  // keep
+
           limit,
           limit, // keep
+
           limitLabel,
           limitLabel, // keep
+
           endpoints,
           endpoints, // keep
+
        ]}),
 +
      new OO.ui.HorizontalLayout( { items: [
 +
        dataDisplay,
 
         ]}),
 
         ]}),
        new OO.ui.HorizontalLayout( { items:  
+
      new OO.ui.HorizontalLayout( { items: [
         [ queryDisplay ]  // keep -> sparql_query
+
         postData,
        }),
+
      ]}),
        new OO.ui.HorizontalLayout( { items:
 
        [ dataDisplay ] // keep -> data
 
        }),
 
 
       ]
 
       ]
 
     }),{
 
     }),{
Line 102: Line 109:
 
]
 
]
 
);
 
);
$( "#s2d" ).append( fieldset.$element );
+
$( "#s2d" ).html( fieldset.$element );
  
 
// Check data
 
// Check data
Line 118: Line 125:
 
/* SPARQL QUERIES & WIKI EDIT ******************************************* */
 
/* SPARQL QUERIES & WIKI EDIT ******************************************* */
 
//  
 
//  
var endpoint = 'https://lingualibre.org/sparql';
+
//var endpoint = 'https://lingualibre.org/sparql';
var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10';
+
//var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10';
var format = 'json';
+
//var format = 'json';
var wikipage = 'MediaWiki:Mydata.js';
+
//var wikipage = 'MediaWiki:Mydata.js';
 +
 
 +
var cleanResponseDataBindings = function(dataBindings){
 +
    var res = dataBindings.map(item => {
 +
      var keys = Object.keys(item);
 +
      var obj = {};
 +
      for(var i=0;i<keys.length;i++){
 +
          var key = keys[i],
 +
              val = item[key].value;
 +
          obj[key]=val;
 +
      } return obj
 +
    });
 +
    return res;
 +
}
 +
 
 +
var sparql2data = function(sparql,endpoint,responseFormat) {
 +
    console.log(sparql,endpoint,responseFormat)
 +
    responseFormat = responseFormat || 'json';
 +
    endpoint = endpoint || 'https://lingualibre.org/sparql';
 +
    console.log({
 +
        "SPARQL endpoint: ":endpoint,
 +
        "Response format requested: ": responseFormat,
 +
        "SPARQL query: ":`${sparql}`})
 +
    $.getJSON(
 +
        endpoint,
 +
        { query: sparql, format: responseFormat },
 +
        function(data){
 +
      console.log("SPARQL query's response: ",data);
 +
        // Inject query
 +
        dataDisplay.setValue(JSON.stringify(data.results.bindings));
 +
    }
 +
    );
 +
}
  
var data2wikipage = function(data,wikipage) {
+
var data2wikipage = function(data, wikipage) {
 
     var params = {
 
     var params = {
 
             action: 'edit',
 
             action: 'edit',
 
             title: wikipage,
 
             title: wikipage,
             text: "Yug test!",
+
             text: data,
 
             format: 'json'
 
             format: 'json'
 
         },
 
         },
 
     api = new mw.Api();
 
     api = new mw.Api();
     api.formattWithToken( 'csrf', params ).done( function ( data ) {
+
     api.formattWithToken( 'csrf', params )
        console.log( data );
+
    .done( function ( data ) { console.log('d2w: ', 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 ************************************************ */
+
/* FETCHDATA QUERY STRING ************************************************ */
// Current: Wikidata, Dbnary.
 
 
// getData from elements
 
// getData from elements
var selectedDropdown = function (group){
+
var fetchQuery = function () {
 +
  // getData from elements
 +
  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){
 
    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 = {
 
   var form = {
    wikipage: wikipage.getValue(),//string
+
      wikipage: wikipage.getValue(), //string
    format: selectedDropdown(format),//Qid
+
      format: selectedDropdown(format), //string of value
    //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
+
      endpoint: endpoints.getValue(), // url
    queryDisplay: queryDisplay.getValue(),//string
+
      sparql: queryDisplay.getValue(), //string
 
   };
 
   };
      console.log('formStatus: ',form)
+
  dataDisplay.setValue('');
  return form;
+
    sparql2data(form.sparql,form.endpoint,form.format);  
 
}
 
}
  
var setQuery = function (form) {
+
var setLimit = function () {
  //  console.log(form);
+
    var query = queryDisplay.getValue()
  var query = form.queryDisplay
+
    .replace(`\nLIMIT 100`,'')
  .concat(form.limit?`\nLIMIT 100`:'');
+
    .concat(limit.isSelected()?`\nLIMIT 100`:'');
       
+
    queryDisplay.setValue(query);
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);
 
  
 
/* ********************************************************************** */
 
/* ********************************************************************** */
Line 198: Line 211:
 
// Current: Wikidata, Dbnary. Broken: lingualibre.
 
// Current: Wikidata, Dbnary. Broken: lingualibre.
 
var postQuery = function () {
 
var postQuery = function () {
var endpointLabel = endpoints.getValue(),
+
var data = dataDisplay.getValue(),
    queryText = queryDisplay.getValue(),
+
    wikipage = wikipage.getValue(),
    queryEncoded = encodeURIComponent(queryText),
+
    queryEncoded = encodeURIComponent(queryText);
    baseEndpointUrl =
+
   if (dataDisplay) {
    endpointLabel == 'wikidata'? 'https://query.wikidata.org/#'
+
  data2wikipage(data, wikipage);
      :'http://kaiko.getalp.org/sparql?default-graph-uri=&query=';
+
      // window.open(baseEndpointUrl.concat(queryEncoded), '_blank');
   if (queryText) {
 
      window.open(baseEndpointUrl.concat(queryEncoded), '_blank');
 
 
   }
 
   }
 
  }
 
  }
  $('#s2d-generate').on('click',function(){ fetchQuery(); });
+
  $('#s2d-limit').on('click',function(){ setLimit(); });
  $('#s2d-run').on('click',function(){ postQuery(); });
+
$('#s2d-fetchData').on('click',function(){ fetchQuery(); });
 +
  $('#s2d-postData').on('click',function(){ postQuery(); });

Revision as of 22:04, 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' },
      { data: 'tsv', label: 'tsv' },
    ].map(o => new OO.ui.MenuOptionWidget({ data: o.data, label: o.label })),
    endpointsArray = [
      { data: 'https://lingualibre.org/sparql', label: 'LinguaLibre'},
      { data: 'https://query.wikidata.org/sparql', label: 'Wikidata'},
    ];

// Elements
var wikipage = new OO.ui.TextInputWidget({
    id: 's2d-wikipage',
    icon: 'specialPages',
    placeholder: 'Sandbox',
    value: 'Sandbox',
		label: 'Data hosting page',
  }),
  format = new OO.ui.DropdownWidget( {
    id: 's2d-format',
    icon: 'code',
		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`,
    value: `SELECT ?item ?itemLabel WHERE {
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
  } 
  ?item prop:P2 entity:Q5
} `,
		multiline: true,
		autosize: true,
    minRows: 10,
		maxRows: 20
	} ),
  fetchData = new OO.ui.ButtonWidget({ 
  	id: 's2d-fetchData', 
    label: 'Fetch data',
  }),
  dataDisplay = new OO.ui.MultilineTextInputWidget( { 
    id: 's2d-dataDisplay',
    placeholder: `# Data will appear here after a while.\n\n\n\n\n\n`,
		multiline: true,
		autosize: true,
    minRows: 10,
		maxRows: 20
	} ),
  postData = new OO.ui.ButtonWidget({ 
    id: 's2d-postData',
    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,
          format,
        ]}),
        new OO.ui.HorizontalLayout( { items: [
        	queryDisplay,
        ]}),
        new OO.ui.HorizontalLayout( { items: [
          fetchData,
          limit,
          limitLabel,
          endpoints,
        ]}),
       	new OO.ui.HorizontalLayout( { items: [ 
        	dataDisplay,
        ]}),
       	new OO.ui.HorizontalLayout( { items: [ 
        	postData,
      	]}),
      ]
    }),{
			label: 'Based on heavy SPARQL query, update the raw data of the target wikipage',
			align: 'top'
		})
	]
);
$( "#s2d" ).html( 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 cleanResponseDataBindings = function(dataBindings){
    var res = dataBindings.map(item => {
       var keys = Object.keys(item);
       var obj = {};
       for(var i=0;i<keys.length;i++){
           var key = keys[i],
               val = item[key].value;
           obj[key]=val;
       } return obj 
    });
    return res;
}

var sparql2data = function(sparql,endpoint,responseFormat) {
    console.log(sparql,endpoint,responseFormat)
    responseFormat = responseFormat || 'json';
    endpoint = endpoint || 'https://lingualibre.org/sparql';
    console.log({ 
        "SPARQL endpoint: ":endpoint,
        "Response format requested: ": responseFormat,
        "SPARQL query: ":`${sparql}`})
    $.getJSON(
        endpoint,
        { query: sparql, format: responseFormat },
        function(data){ 
       		console.log("SPARQL query's response: ",data);
        	// Inject query
        	dataDisplay.setValue(JSON.stringify(data.results.bindings));
    	}
    );
}

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


/* ********************************************************************** */
/* FETCHDATA QUERY STRING ************************************************ */
// getData from elements
var fetchQuery = function () {
   // 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 form = {
       wikipage: wikipage.getValue(),	//string
       format: selectedDropdown(format),	//string of value
       //formatLabel: qid2value(formatArray,selectedDropdown(format)),	//noun
       limit: limit.isSelected(),	//boolean
       endpoint: endpoints.getValue(),	// url
       sparql: queryDisplay.getValue(),	//string
   };
   	dataDisplay.setValue('');
    sparql2data(form.sparql,form.endpoint,form.format); 
}

var setLimit = function () {
    var query = queryDisplay.getValue()
    	.replace(`\nLIMIT 100`,'')
    	.concat(limit.isSelected()?`\nLIMIT 100`:'');
    queryDisplay.setValue(query);
}

/* ********************************************************************** */
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
// Current: Wikidata, Dbnary. Broken: lingualibre.
var postQuery = function () {
	var data = dataDisplay.getValue(),
    	wikipage = wikipage.getValue(),
    	queryEncoded = encodeURIComponent(queryText);
   if (dataDisplay) {
   		data2wikipage(data, wikipage);
      // window.open(baseEndpointUrl.concat(queryEncoded), '_blank');
   }
 }
 $('#s2d-limit').on('click',function(){ setLimit(); });
 $('#s2d-fetchData').on('click',function(){ fetchQuery(); });
 $('#s2d-postData').on('click',function(){ postQuery(); });