MediaWiki

Difference between revisions of "Common.js"

(Removed some hacks, since the source code was fixed and deployed (see https://github.com/lingua-libre/RecordWizard/commits/master))
 
(98 intermediate revisions by 5 users not shown)
Line 1: Line 1:
// Replace Wikidata IDs with their [label, description]
+
// Page-specific scripts modules and conditional loading
$( '.wb-external-id' ).each( function() {
+
// RecordWizard sugar
    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 ].value,
 
                description = entity.descriptions[ lang ].value;
 
           
 
            node.html( label + ' <i>(' + node.text() + ')</i><br><small>' + description + '</small>' )
 
        } );
 
    }
 
} );
 
  
// Fetch and display DataViz results
+
/* *************************************************************** */
const sparqlEndpoint = "https://v2.lingualibre.fr/bigdata/namespace/wdq/sparql";
+
/* PAGE-SPECIFIC SCRIPTS LOADINGS ******************************** */
mw.loader.using( 'jquery.tablesorter', function() {
+
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
    $( '.dataviz' ).each( function() {
+
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
        var node = $( this ),
+
//🎷🥁🎸🐣📔🗻🌏
            query = node.children( '.dataviz-query' ).text().replace(/\u00A0/g, ''),
+
// LastAudiosBoxes
            resultNode = node.children( '.dataviz-result' );
+
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
  
        $.post( sparqlEndpoint, { format: 'json', query: query } ).then( function( data ) {
+
// Iframe gallery for Lingualibre:Apps
            console.log( data );
+
// Prototype https://jsfiddle.net/hugolpz/meygpo6t/
            var order = [],
+
// Problem: iframe are not accepted into wikicode. Solution: hide key values in html tag, generate iframe via JS injection.
                theadTr = $( '<tr>' ),
 
                thead = $( '<thead>' ).append( theadTr ),
 
                tbody = $( '<tbody>' ),
 
                table = $( '<table>' )
 
            .addClass( 'wikitable sortable' )
 
            .append( thead ).append( tbody );
 
  
            for ( var i = 0; i < data.head.vars.length; i++ ) {
 
                var column = data.head.vars[ i ];
 
                theadTr.append( $( '<th>' ).text( column ) );
 
                order.push( column );
 
            }
 
  
            for ( var i=0; i < data.results.bindings.length; i++ ) {
+
/*Query visualisation: Replace loading text by a spinner gif***********************************************************/
                var row = data.results.bindings[ i ],
+
$( '.queryviz-loading' ).html( '<center><img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Balls.gif" width="50" /></center>' );
                    tr = $( '<tr>' ).appendTo( tbody );
 
 
 
                for ( var j=0; j < order.length; j++ ) {
 
                    var cell = row[ order[ j ] ];
 
                    if ( cell.type === 'uri' ) {
 
                        cell.value = $( '<a>' )
 
                            .attr( 'href', cell.value )
 
                            .text( cell.value.split( '/' ).pop() );
 
                    }
 
                    tr.append( $( '<td>' ).html( cell.value ) );
 
                }
 
            }
 
 
 
            resultNode.html( table );
 
            table.tablesorter();
 
        } ).fail( function( data ) {
 
            console.log( data.responseText );
 
            //TODO: manage errors
 
        } );
 
    } );
 
} );
 

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>' );