MediaWiki

Difference between revisions of "Common.js"

m (Prepare modularisation of LastAudios)
(Removed some hacks, since the source code was fixed and deployed (see https://github.com/lingua-libre/RecordWizard/commits/master))
 
(31 intermediate revisions by 2 users not shown)
Line 1: Line 1:
// Modules loader, conditional  
+
// Page-specific scripts modules and conditional loading
// General sugar
 
// Main page sugar
 
 
// RecordWizard sugar
 
// RecordWizard sugar
  
 
/* *************************************************************** */
 
/* *************************************************************** */
/* CONDITIONALLY LOADED SCRIPTS ********************************** */
+
/* PAGE-SPECIFIC SCRIPTS LOADINGS ******************************** */
 
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
 
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
 
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
 
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
Line 11: Line 9:
 
// LastAudiosBoxes
 
// LastAudiosBoxes
 
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
 
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
    console.log('🎸 Last Audios');
+
  console.log('🎸 Last Audios');
    mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// SoundLibrary
 
// SoundLibrary
 
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
 
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
    console.log('🥁 Sound library');
+
  console.log('🥁 Sound library');
    mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
 
}
 
}
// Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
+
// Item pages additional sugar
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
+
if (/^(Q|Property:P)[0-9]+$/.test(mw.config.get('wgPageName'))) {
    console.log('🐣 Search by word');
+
  console.log('🍭 ItemsSugar ');
    mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:ItemsSugar.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// Lexeme Queries Generator (OOUI)
 
// Lexeme Queries Generator (OOUI)
if(/^Template:LexemeQueriesGenerator$/.test( mw.config.get( 'wgPageName' )) || /^Help:SPARQL_2$/.test( mw.config.get( 'wgPageName' )) ) {
+
if (/^Template:LexemeQueriesGenerator$/.test(mw.config.get('wgPageName')) || /^Help:SPARQL_/.test(mw.config.get('wgPageName'))) {
    console.log('📔 Lexeme Queries Generator');
+
  console.log('📔 Lexeme Queries Generator');
    mw.loader.load( '/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
 
}
 
}
 
// SPARQL to data (OOUI)
 
// SPARQL to data (OOUI)
if(/SPARQL/.test( mw.config.get( 'wgPageName' )) || /parql/.test( mw.config.get( 'wgPageName' )) ) {
+
if (/SPARQL/.test(mw.config.get('wgPageName')) || /parql/.test(mw.config.get('wgPageName'))) {
    console.log('🚀 SPARQL to data');
+
  console.log('🚀 SPARQL to data');
    mw.loader.load( '/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
 
}
 
}
// Language overview
+
// Click bug & browser
if(/Languages/.test( mw.config.get( 'wgPageName' )) ) {
+
if (/browser/.test(mw.config.get('wgPageName')) || /bug/.test(mw.config.get('wgPageName'))) {
    console.log('🌏 Languages gallery');
+
  console.log('🎷 Click bug & browser');
    mw.loader.load( '/index.php?title=MediaWiki:LanguagesGallery.js&action=raw&ctype=text/javascript');
+
  mw.loader.load('/index.php?title=MediaWiki:Browser.js&action=raw&ctype=text/javascript');
 
}
 
}
// Minorities measures
 
 
 
/* *************************************************************** */
 
/* *************************************************************** */
/* GENERAL SUGAR (?) ********************************************* */
+
/* In development *************************************** */
// Replace Wikidata IDs with their [label, description]
+
// User:Yug: Language overview
if ($('.wb-external-id').length > 0) {
+
if (/Languages/.test(mw.config.get('wgPageName'))) {
  mw.loader.using('mediawiki.ForeignApi', function() {
+
  console.log('🌏 Languages gallery');
    $('.wb-external-id').each(function() {
+
  mw.loader.load('/index.php?title=MediaWiki:LanguagesGallery.js&action=raw&ctype=text/javascript');
      if ($(this).attr('href').lastIndexOf('https://www.wikidata.org', 0) === 0) {
 
        var wikidataApi = new mw.ForeignApi('https://www.wikidata.org/w/api.php', {
 
            anonymous: true,
 
            parameters: { 'origin': '*' },
 
            ajax: { timeout: 10000 }
 
          }),
 
          lang = mw.config.get('wgUserLanguage'),
 
          node = $(this);
 
        wikidataApi.get({
 
          'action': 'wbgetentities',
 
          'format': 'json',
 
          'ids': node.text(),
 
          'props': 'labels|descriptions',
 
          'languages': lang,
 
          'languagefallback': 1,
 
          'origin': '*'
 
        }).then(function(data) {
 
 
 
          var entity = data.entities[node.text()],
 
            label = (entity.labels[lang] !== undefined ? entity.labels[lang].value + ' <i>(' + node.text() + ')</i>' : node.text()),
 
            description = (entity.descriptions[lang] !== undefined ? '<small>' + entity.descriptions[lang].value + '</small>' : '');
 
 
 
          node.html(label + '<br>' + description)
 
        });
 
      }
 
    });
 
  });
 
 
}
 
}
 
+
// User:Yug: Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
//Add an audio player to the audio records links in the wikibase items
+
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
+
   console.log('🐣 Search by word');
 
+
   mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
function playButton(audioUrl) {
 
  var button = new OO.ui.ButtonWidget({
 
    framed: false,
 
    icon: 'play',
 
    title: 'play'
 
  });
 
  button.on('click', function() {
 
    var audio = new Audio(audioUrl);
 
    audio.play();
 
  });
 
  return button.$element;
 
}
 
 
 
if ($('#P3 a.extiw').length > 0) {
 
  mw.loader.using(['oojs-ui-widgets', 'oojs-ui.styles.icons-media'], function() {
 
    $('#P3 a.extiw').each(function() {
 
      var $node = $(this);
 
      $node.before(playButton(BASE_FILE_URL + $node.text()));
 
    });
 
  });
 
}
 
 
 
 
 
 
 
/* *************************************************************** */
 
/* MAIN PAGE SUGAR *********************************************** */
 
// Display last records on main page
 
var ab1, ab2;
 
 
 
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.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);
 
}
 
 
 
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));
 
 
}
 
}
 +
// Minorities languages statistics page
  
function createAudioBoxes(data) {
+
// Iframe gallery for Lingualibre:Apps
  if (data.query === undefined || data.query.rwrecords === undefined || data.query.rwrecords.length < 2) {
+
// Prototype https://jsfiddle.net/hugolpz/meygpo6t/
    displayError('nodata');
+
// Problem: iframe are not accepted into wikicode. Solution: hide key values in html tag, generate iframe via JS injection.
    return;
 
  }
 
  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: '2',
 
    rwrsort: 'pageid',
 
    rwrdir: 'descending',
 
    rwrformat: 'qid'
 
  }).then(createAudioBoxes, displayError);
 
}
 
  
function displayError(code, error) {
+
/*Query visualisation: Replace loading text by a spinner gif***********************************************************/
  console.warn(code, error);
+
$( '.queryviz-loading' ).html( '<center><img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Balls.gif" width="50" /></center>' );
}
 
 
 
if (mw.config.get('wgPageName') === 'LinguaLibre:Main_Page') {
 
    mw.loader.using(['mediawiki.api', 'ext.recordWizard.wikibase']).then(getLastRecords);
 
}
 
 
 
/* *************************************************************** */
 
/* RECORD WIZARD SUGAR ******************************************* */
 
/*Confirmation message when users try to leave the Record Wizard
 
(works from the second step onwards) */
 
$(function() {
 
if (mw.config.get('wgPageName') !== 'Special:RecordWizard') {
 
    return;
 
}
 
window.onbeforeunload = function() {
 
    return "Do you want to leave the page? All your unsaved changes will be lost.";
 
};
 
});
 
/*
 
if (mw.config.get('wgPageName') === 'Special:RecordWizard') {
 
    window.onbeforeunload = function() {
 
        return "Do you want to leave the page? All your unsaved changes will be lost.";
 
    };
 
}
 
*/
 

Latest revision as of 08:51, 20 April 2023

// Page-specific scripts modules and conditional loading
// RecordWizard sugar

/* *************************************************************** */
/* PAGE-SPECIFIC SCRIPTS LOADINGS ******************************** */
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
//🎷🥁🎸🐣📔🗻🌏
// LastAudiosBoxes
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
  console.log('🎸 Last Audios');
  mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
}
// SoundLibrary
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
  console.log('🥁 Sound library');
  mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
}
// Item pages additional sugar
if (/^(Q|Property:P)[0-9]+$/.test(mw.config.get('wgPageName'))) {
  console.log('🍭 ItemsSugar ');
  mw.loader.load('/index.php?title=MediaWiki:ItemsSugar.js&action=raw&ctype=text/javascript');
}
// Lexeme Queries Generator (OOUI)
if (/^Template:LexemeQueriesGenerator$/.test(mw.config.get('wgPageName')) || /^Help:SPARQL_/.test(mw.config.get('wgPageName'))) {
  console.log('📔 Lexeme Queries Generator');
  mw.loader.load('/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
}
// SPARQL to data (OOUI)
if (/SPARQL/.test(mw.config.get('wgPageName')) || /parql/.test(mw.config.get('wgPageName'))) {
  console.log('🚀 SPARQL to data');
  mw.loader.load('/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
}
// Click bug & browser
if (/browser/.test(mw.config.get('wgPageName')) || /bug/.test(mw.config.get('wgPageName'))) {
  console.log('🎷 Click bug & browser');
  mw.loader.load('/index.php?title=MediaWiki:Browser.js&action=raw&ctype=text/javascript');
}
/* *************************************************************** */
/* In development *************************************** */
// User:Yug: Language overview
if (/Languages/.test(mw.config.get('wgPageName'))) {
  console.log('🌏 Languages gallery');
  mw.loader.load('/index.php?title=MediaWiki:LanguagesGallery.js&action=raw&ctype=text/javascript');
}
// User:Yug: Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
  console.log('🐣 Search by word');
  mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
}
// Minorities languages statistics page

// Iframe gallery for Lingualibre:Apps
// Prototype https://jsfiddle.net/hugolpz/meygpo6t/
// Problem: iframe are not accepted into wikicode. Solution: hide key values in html tag, generate iframe via JS injection.


/*Query visualisation: Replace loading text by a spinner gif***********************************************************/
$( '.queryviz-loading' ).html( '<center><img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Balls.gif" width="50" /></center>' );