User

Difference between revisions of "Nicolas NALLET/bluell.js"

< User:Nicolas NALLET

(copie content of https://lingualibre.org/index.php?title=User:NavidBoyWiki/bluell.js&action=edit)
Line 103: Line 103:
 
$('.selectors').append(lang.$element);
 
$('.selectors').append(lang.$element);
 
$('.selectors').append(list.$element);
 
$('.selectors').append(list.$element);
 +
}
 +
 +
var ab1;
 +
 +
 +
var AudioBox = function( recordQid, $node ) {
 +
this.wbRecord = new mw.recordWizard.wikibase.Item( recordQid );
 +
 +
this.$node = $node;
 +
this.audioNode = document.createElement( 'audio' );
 +
this.audioNode.preload = 'auto';
 +
 +
    this.api = new mw.Api();
 +
 +
this.recordQid = recordQid;
 +
this.langQid = null;
 +
this.speakerQid = null;
 +
 +
this.label = '';
 +
this.media = '';
 +
this.lang = '';
 +
this.speaker = '';
 +
 +
this.wbRecord.getFromApi( this.api ).then( this.processRecord.bind( this ), displayError );
 +
}
 +
 +
AudioBox.prototype.processRecord = function() {
 +
this.label = this.wbRecord.getLabel( 'en' );
 +
this.media = 'https://commons.wikimedia.org/wiki/Special:FilePath/' + this.wbRecord.getStatements( 'P3' )[ 0 ].getValue();
 +
this.langQid = this.wbRecord.getStatements( 'P4' )[ 0 ].getValue();
 +
    //this.langQid = this.wbRecord.getStatements( 'P4' )[ 0 ].getValue();
 +
this.speakerQid = this.wbRecord.getStatements( 'P5' )[ 0 ].getValue();
 +
 +
this.api.get( {
 +
action: "wbgetentities",
 +
format: "json",
 +
ids: this.langQid + '|' + this.speakerQid,
 +
props: "labels",
 +
languages: mw.config.get( 'wgUserLanguage' ) + "|en",
 +
languagefallback: 1,
 +
} ).then( this.processLabels.bind( this ), displayError )
 +
 +
 +
function testQuery(){
 +
 +
var query = "select ?record ?recordLabel ?locutorLabel ?languageLabel ?languageLevelLabel where { ?record prop:P2 entity:Q2 .?record prop:P5 ?locutor .?record prop:P4 ?language .?locutor llp:P4 ?languageStatement .?languageStatement llv:P4 ?language .?languageStatement llq:P16 ?languageLevel .SERVICE wikibase:label {bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en' .} } ORDER BY ?languageLabel ?languageLevelLabel";
 +
 +
  // $('#bodyContentResearch').append(queryDataset);
 +
$.ajax({
 +
dataType: 'json',
 +
data: {
 +
queryLn: 'SPARQL',
 +
query: query ,
 +
limit: 'none',
 +
infer: 'true',
 +
//Accept: 'application/sparql-results+json'
 +
},
 +
success: createAudioBoxes,
 +
error: displayError
 +
});
 +
}
 +
 +
 +
/*$.sparql()
 +
.select(["?record", "?recordLabel", "locutorLabel", "languageLabel", "?languageLevelLabel"])
 +
.where("?record prop:P2 entity:Q2 \.
 +
?record prop:P5 ?locutor \.
 +
?record prop:P4 ?language \.
 +
?locutor llp:P4 ?languageStatement \.
 +
?languageStatement llv:P4 ?language \.
 +
?languageStatement llq:P16 ?languageLevel \.
 +
SERVICE wikibase: label {bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en' \.}")
 +
.orderby(["?languageLabel", "?languageLevelLabel"])*/
 +
 +
    //console.log(languages);
 +
    //console.log(mw.config.get( 'wgUserLanguage' ));
 +
/*select ?record ?recordLabel ?locutorLabel ?languageLabel ?languageLevelLabel
 +
where {
 +
  ?record prop:P2 entity:Q2 .
 +
  ?record prop:P5 ?locutor .
 +
  ?record prop:P4 ?language .
 +
 
 +
  ?locutor llp:P4 ?languageStatement .
 +
  ?languageStatement llv:P4 ?language .
 +
  ?languageStatement llq:P16 ?languageLevel .
 +
 
 +
  SERVICE wikibase:label {
 +
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
 +
  }
 +
} ORDER BY ?languageLabel ?languageLevelLabel*/
 +
}
 +
 +
AudioBox.prototype.processLabels = function( data ) {
 +
var langLabels;
 +
 +
if ( data.entities === undefined || data.entities[ this.langQid ] === undefined || data.entities[ this.speakerQid ] === undefined ) {
 +
displayError( 'dataerror' );
 +
return;
 +
}
 +
langLabels = data.entities[ this.langQid ].labels;
 +
 +
if ( langLabels[ mw.config.get( 'wgUserLanguage' ) ] !== undefined ) {
 +
this.lang = langLabels[ mw.config.get( 'wgUserLanguage' ) ].value;
 +
} else {
 +
this.lang = langLabels[ 'en' ].value;
 +
}
 +
 +
this.speaker = data.entities[ this.speakerQid ].labels[ 'en' ].value;
 +
 +
this.display();
 +
}
 +
 +
AudioBox.prototype.display = function() {
 +
this.$node.find( '.ab-title' ).text( this.label );
 +
this.$node.find( '.ab-metadata' ).text( this.lang + ' - ' + this.speaker );
 +
 +
this.audioNode.src = this.media;
 +
this.$node.find( '.ab-playbutton' ).click( this.audioNode.play.bind( this.audioNode ) );
 +
}
 +
 +
 +
 +
 +
 +
 +
function createAudioBoxes( data ) {
 +
if ( data.query === undefined || data.query.rwrecords === undefined || data.query.rwrecords.length < 1 ) {
 +
displayError( 'nodata' );
 +
return;
 +
}
 +
for (var i = 0; i < 100; i++) {
 +
ab1 = new AudioBox( data.query.rwrecords[ i ], $( '.audiobox' ).eq( i ) );
 +
$("body").append('<div class="audiobox"> <div class="ab-playbutton"><i></i></div> <div> <div class="ab-title">...</div> <div class="ab-metadata">...</div> </div> </div>');
 +
}
 +
//ab1 = new AudioBox( data.query.rwrecords[ 0 ], $( '.audiobox' ).eq( 0 ) );
 +
//ab2 = new AudioBox( data.query.rwrecords[ 1 ], $( '.audiobox' ).eq( 1 ) );
 +
}
 +
 +
/*function getLastRecords() {
 +
    var api = new mw.Api();
 +
api.get( {
 +
        action: 'query',
 +
        format: 'json',
 +
        list: 'rwrecords',
 +
        rwrlimit: '1',
 +
rwrsort: 'pageid',
 +
rwrdir: 'descending',
 +
rwrformat: 'qid'
 +
  } ).then( createAudioBoxes, displayError );
 +
}*/
 +
 +
function displayError( code, error ) {
 +
console.warn( code, error );
 +
}
 +
 +
if ( mw.config.get( 'wgPageName' ) === 'User:NavidBoyWiki' ) {
 +
    mw.loader.using( [ 'mediawiki.api', 'ext.recordWizard.wikibase' ] ).then( getLastRecords );
 
}
 
}

Revision as of 14:17, 15 July 2021

/* Fix links to WIkipedia on P19 property*/
$('div#P19 .wb-external-id').attr('href',  $( 'div#P19 .wb-external-id' ).attr('href').replace(/\+/g, "_"));

/*OOui Tuto*/

$( function () {
	var input = new OO.ui.TextInputWidget(),
		list = new OO.ui.SelectWidget();

	// Append to the wrapper
	$( '.wrapper' ).append(
		input.$element,
		list.$element
	);
} );

// Respond to 'enter' keypress
input.on( 'enter', function () {
	// Add the item
	list.addItems( [
		new OO.ui.OptionWidget( {
			data: input.getValue(),
			label: input.getValue()
		} )
	] );
} );

/*Display a drilldown interface for record*/
list = new OO.ui.SelectWidget();
var lang  = new OO.ui.DropdownWidget( {
	label: 'Select a language',
	menu: {
		items: [
			new OO.ui.MenuOptionWidget( {
				data: 'French'
			} ),
			new OO.ui.MenuOptionWidget( {
				data: 'b',
				label: 'Second',
				indicator: 'clear'
			} ),
			new OO.ui.MenuOptionWidget( {
				data: 'c',
				label: 'Third'
			} ),
			new OO.ui.MenuOptionWidget( {
				data: 'c',
				label: 'The fourth option has an overly long label'
			} ),
			new OO.ui.MenuOptionWidget( {
				icon: 'feedback',
				data: 'd',
				label: 'The fifth option has an icon'
			} )
		]
	}
} );

//lang = new OO.ui.DropdownWidget();




AudioBox.prototype.display = function() {
	this.$node.find( '.ab-title' ).text( this.label );
	this.$node.find( '.ab-metadata' ).text( this.lang + ' - ' + this.speaker );
	
	this.audioNode.src = this.media;
	this.$node.find( '.ab-playbutton' ).click( this.audioNode.play.bind( this.audioNode ) );
}


function createAudioBoxes( data ) {
	if ( data.query === undefined || data.query.rwrecords === undefined || data.query.rwrecords.length < 2 ) {
		displayError( 'nodata' );
		return;
	}

	ab1 = new AudioBox( data.query.rwrecords[ 0 ], $( '.audiobox' ).eq( 0 ) );
	ab2 = new AudioBox( data.query.rwrecords[ 1 ], $( '.audiobox' ).eq( 1 ) );
}

function getRecords() {
    var api = new mw.Api();
	api.get( {
        action: 'query',
        format: 'json',
        list: 'rwrecords',
        rwrlimit: '2',
		rwrsort: 'pageid',
		rwrdir: 'descending',
		rwrformat: 'qid'
  } ).then( createAudioBoxes, displayError );
}

function displayError( code, error ) {
	console.warn( code, error );
}


if ( mw.config.get( 'wgPageName' ) === 'User:Nicolas_NALLET' ) {
    mw.loader.using( [ 'mediawiki.api', 'ext.recordWizard.wikibase' ] ).then( getRecords );
$('.selectors').append(lang.$element);
$('.selectors').append(list.$element);
}

var ab1;


var AudioBox = function( recordQid, $node ) {
	this.wbRecord = new mw.recordWizard.wikibase.Item( recordQid );
	
	this.$node = $node;
	this.audioNode = document.createElement( 'audio' );
	this.audioNode.preload = 'auto';

    this.api = new mw.Api();	

	this.recordQid = recordQid;
	this.langQid = null;
	this.speakerQid = null;
	
	this.label = '';
	this.media = '';
	this.lang = '';
	this.speaker = '';
	
	this.wbRecord.getFromApi( this.api ).then( this.processRecord.bind( this ), displayError );
}

AudioBox.prototype.processRecord = function() {
	this.label = this.wbRecord.getLabel( 'en' );
	this.media = 'https://commons.wikimedia.org/wiki/Special:FilePath/' + this.wbRecord.getStatements( 'P3' )[ 0 ].getValue();
	this.langQid = this.wbRecord.getStatements( 'P4' )[ 0 ].getValue();
    //this.langQid = this.wbRecord.getStatements( 'P4' )[ 0 ].getValue();
	this.speakerQid = this.wbRecord.getStatements( 'P5' )[ 0 ].getValue();

	this.api.get( {
		action: "wbgetentities",
		format: "json",
		ids: this.langQid + '|' + this.speakerQid,
		props: "labels",
		languages: mw.config.get( 'wgUserLanguage' ) + "|en",
		languagefallback: 1,
	} ).then( this.processLabels.bind( this ), displayError )
	

	function testQuery(){
		
		var query = "select ?record ?recordLabel ?locutorLabel ?languageLabel ?languageLevelLabel where { ?record prop:P2 entity:Q2 .?record prop:P5 ?locutor .?record prop:P4 ?language .?locutor llp:P4 ?languageStatement .?languageStatement llv:P4 ?language .?languageStatement llq:P16 ?languageLevel .SERVICE wikibase:label {bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en' .} } ORDER BY ?languageLabel ?languageLevelLabel";
	
	   // $('#bodyContentResearch').append(queryDataset);
		$.ajax({
					dataType: 'json',
					data: {
						queryLn: 'SPARQL',
						query: query ,
						limit: 'none',
						infer: 'true',
						//Accept: 'application/sparql-results+json'
					},
					success: createAudioBoxes,
					error: displayError
			});
	}
	

	/*$.sparql()
	.select(["?record", "?recordLabel", "locutorLabel", "languageLabel", "?languageLevelLabel"])
		.where("?record prop:P2 entity:Q2 \.
		?record prop:P5 ?locutor \.
		?record prop:P4 ?language \.
		?locutor llp:P4 ?languageStatement \.
		?languageStatement llv:P4 ?language \.
		?languageStatement llq:P16 ?languageLevel \.
		SERVICE wikibase: label {bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en' \.}")
	.orderby(["?languageLabel", "?languageLevelLabel"])*/

    //console.log(languages);
    //console.log(mw.config.get( 'wgUserLanguage' ));
	/*select ?record ?recordLabel ?locutorLabel ?languageLabel ?languageLevelLabel
where {
  ?record prop:P2 entity:Q2 .
  ?record prop:P5 ?locutor .
  ?record prop:P4 ?language .
  
  ?locutor llp:P4 ?languageStatement .
  ?languageStatement llv:P4 ?language .
  ?languageStatement llq:P16 ?languageLevel .
  
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
  } 
} ORDER BY ?languageLabel ?languageLevelLabel*/
}

AudioBox.prototype.processLabels = function( data ) {
	var langLabels;

	if ( data.entities === undefined || data.entities[ this.langQid ] === undefined || data.entities[ this.speakerQid ] === undefined ) {
		displayError( 'dataerror' );
		return;
	}
	langLabels = data.entities[ this.langQid ].labels;
	
	if ( langLabels[ mw.config.get( 'wgUserLanguage' ) ] !== undefined ) {
		this.lang = langLabels[ mw.config.get( 'wgUserLanguage' ) ].value;
	} else {
		this.lang = langLabels[ 'en' ].value;
	}
	
	this.speaker = data.entities[ this.speakerQid ].labels[ 'en' ].value;

	this.display();
}

AudioBox.prototype.display = function() {
	this.$node.find( '.ab-title' ).text( this.label );
	this.$node.find( '.ab-metadata' ).text( this.lang + ' - ' + this.speaker );
	
	this.audioNode.src = this.media;
	this.$node.find( '.ab-playbutton' ).click( this.audioNode.play.bind( this.audioNode ) );
}






function createAudioBoxes( data ) {
	if ( data.query === undefined || data.query.rwrecords === undefined || data.query.rwrecords.length < 1 ) {
		displayError( 'nodata' );
		return;
	}
	for (var i = 0; i < 100; i++) {
		ab1 = new AudioBox( data.query.rwrecords[ i ], $( '.audiobox' ).eq( i ) );
		$("body").append('<div class="audiobox"> <div class="ab-playbutton"><i></i></div> <div> <div class="ab-title">...</div> <div class="ab-metadata">...</div> </div> </div>');	
	}
	//ab1 = new AudioBox( data.query.rwrecords[ 0 ], $( '.audiobox' ).eq( 0 ) );
	//ab2 = new AudioBox( data.query.rwrecords[ 1 ], $( '.audiobox' ).eq( 1 ) );
}

/*function getLastRecords() {
    var api = new mw.Api();
	api.get( {
        action: 'query',
        format: 'json',
        list: 'rwrecords',
        rwrlimit: '1',
		rwrsort: 'pageid',
		rwrdir: 'descending',
		rwrformat: 'qid'
  } ).then( createAudioBoxes, displayError );
}*/

function displayError( code, error ) {
	console.warn( code, error );
}

if ( mw.config.get( 'wgPageName' ) === 'User:NavidBoyWiki' ) {
    mw.loader.using( [ 'mediawiki.api', 'ext.recordWizard.wikibase' ] ).then( getLastRecords );
}