MediaWiki

Difference between revisions of "Sparql2data.js"

(Created page with "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....")
 
m
Line 1: Line 1:
 +
/* ************************************************************************** */
 +
/* 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 [[Help:SPARQL 2#Tools]] > fill the form > click "Generate" or "Run !"
 +
// Hack pad: https://jsfiddle.net/hugolpz/x4qmcudj/
 +
// Documentations:  https://www.mediawiki.org/wiki/API:Edit#MediaWiki_JS
 +
// Author: Yug
 +
 
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';
Line 14: Line 23:
 
     api = new mw.Api();
 
     api = new mw.Api();
  
     api.postWithToken( 'csrf', params ).done( function ( data ) {
+
     api.formattWithToken( 'csrf', params ).done( function ( data ) {
 
         console.log( data );
 
         console.log( data );
 
     } );
 
     } );
Line 31: Line 40:
 
     );
 
     );
 
}
 
}
 +
 +
 +
 +
 +
 +
// Toolbox
 +
 +
/* ********************************************************************** */
 +
/* QUERIES BASIS ******************************************************** */
 +
// Current: Wikidata, Dbnary.
 +
 +
/* ********************************************************************** */
 +
/* 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 SPAQRL 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);})
 +
$('#s2d-languages').on('focusout',function() { checkAvailableData(2,languages);});
 +
checkAvailableData(2,languages);
 +
 +
 +
 +
/* ********************************************************************** */
 +
/* GENERATE QUERY STRING ************************************************ */
 +
// Current: Wikidata, Dbnary.
 +
var generateQuery = 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 qid2value = function (arr,qid,field){
 +
    field = field || 'label'
 +
    var match = arr.filter(item=> item.data==qid);
 +
    return match[0]?match[0][field]:null;
 +
  }
 +
  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(form)
 +
  //  console.log(form);
 +
  var query = form.queryDisplay;
 +
// Identify suitable query
 +
  query = '';
 +
 
 +
  // Edit suitable query 
 +
  query = query.concat(form.limit?`\nLIMIT 100`:'');
 +
 +
  // Inject query
 +
  queryDisplay.setValue(query);
 +
}
 +
 +
/* ********************************************************************** */
 +
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
 +
// Current: Wikidata, Dbnary. Broken: lingualibre.
 +
var runQuery = 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-run').on('click',function(){ runQuery(); });
 +
$('#s2d-generate').on('click',function(){ generateQuery(); });

Revision as of 11:47, 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 [[Help:SPARQL 2#Tools]] > fill the form > click "Generate" or "Run !"
// Hack pad: https://jsfiddle.net/hugolpz/x4qmcudj/
// Documentations:  https://www.mediawiki.org/wiki/API:Edit#MediaWiki_JS
// 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.
 
/* ********************************************************************** */
/* 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 SPAQRL 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);})
$('#s2d-languages').on('focusout',function() { checkAvailableData(2,languages);});
checkAvailableData(2,languages);



/* ********************************************************************** */
/* GENERATE QUERY STRING ************************************************ */
// Current: Wikidata, Dbnary.
 var generateQuery = 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 qid2value = function (arr,qid,field){
    field = field || 'label'
    var match = arr.filter(item=> item.data==qid);
    return match[0]?match[0][field]:null;
   }
   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(form)
   //  console.log(form);
   var query = form.queryDisplay;
// Identify suitable query
   query = '';
  
   // Edit suitable query   
   query = query.concat(form.limit?`\nLIMIT 100`:'');

   // Inject query
   queryDisplay.setValue(query);
 }

/* ********************************************************************** */
/* OPENS EXTERNAL QUERY SERVICE ***************************************** */
// Current: Wikidata, Dbnary. Broken: lingualibre.
var runQuery = 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-run').on('click',function(){ runQuery(); });
 $('#s2d-generate').on('click',function(){ generateQuery(); });