Utilisateur:TomT0m/extraInterwiki.js
Apparence
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./*
{{Projet:JavaScript/Script}}
<nowiki> */
mw.loader.using(["oojs-ui.styles.icons-editing-advanced"],() => {
let max_input_items = 1000;
let query_too_much_incoming_uses = `
ASK WHERE {
FILTER(?count != ${max_input_items} )
{
SELECT (COUNT(*) AS ?count) WHERE {
{
SELECT * WHERE {
?item ?p wd:Q362.
_:b50 wikibase:directClaim ?p;
wikibase:propertyType wikibase:WikibaseItem.
}
LIMIT ${max_input_items}
}
}
}
}
`;
let lang = mw.config.get("wgContentLanguage");
function makeSPARQLQuery( endpointUrl, sparqlQuery, doneCallback ) {
var settings = {
headers: {
Accept: 'application/sparql-results+json'
},
data: { query: sparqlQuery.replace("[AUTO_LANGUAGE]",lang) }
};
return $.ajax( endpointUrl, settings ).then( doneCallback );
}
var endpointUrl = 'https://query.wikidata.org/sparql';
function make_iwextra_button(content){
var popupButton = new OO.ui.PopupButtonWidget( {
$overlay: true,
label: 'IW en +',
// icon: 'menu',
indicator: 'down',
icon:"language",
popup: {
$content: content,
padded: true,
align: 'forwards'
},
class:"mw-portlet-lang"
} );
return popupButton ;
// Append the button to the DOM.
}
function init_extralinks() {
"use strict";
function init(){
let qid = mw.config.get( 'wgWikibaseItemId' );
if (!(qid))
return;
// the gadget depends on a template defined on frwiki,
// so getting an API depending of the wiki the script is run on.
let frw_api = new mw.Api();
let server = mw.config.get("wgServer");
if ( server != "//fr.wikipedia.org" ){
frw_api = new mw.ForeignApi( 'https://fr.wikipedia.org/w/api.php' );
}
function handle_sparql_interwikis_results(resultats_req){
let extraiw_tuple = resultats_req.results.bindings;
if( extraiw_tuple.length == 0 ) {
return ; // like the interwiki button is not shown by mediawiki
// if there is no interwiki just show nothing
}
let content = $("<ul/>");
for (let idx in extraiw_tuple){
let binding = extraiw_tuple[idx];
let linkLabel = binding["élémentLienDeLangueLabel"].value;
let nb = binding.nbArticles.value;
let rel = binding.relationLabel.value;
let link = binding["élémentLienDeLangue"].value + "#sitelinks-wikipedia";
$("<li>")
.append(
$("<a />",{text:linkLabel, href:link}),
` (${nb}) — ${rel}`
).appendTo(content);
}
let button = make_iwextra_button(content);
/////////////// Ajouts à l’interface ///////////////////
// ajout à l’interface. Première ligne marche pour vector nouvelle génération, seconde pour vector 2010
let vector2022 = $("#p-lang-btn");
if (vector2022.length == 1){
$("#p-lang-btn").after(button.$element.addClass("mw-portlet").addClass("mw-portlet-lang"));
return ;
}
let vector2010 = $("#p-lang");
if (vector2010.length == 1) {
$("#p-lang").after(button.$element);
return;
}
// pour minerva neue, détection par la présence d’un élément d’id "p-tb"
// ajout d’un item dans le menu « plus »
let minerva_neue = $("#p-tb");
if (minerva_neue.length == 1){
$("<li>)").addClass("toggle-list-item")
.append(button.$element)
.appendTo(minerva_neue);
return;
}
/////////////////////////////////////////////////////
}
// Query parameter to compute the query that will search the interwikis
// by expanding the "Interwiki connexes" Mediawiki template on frwiki
var params = {
action: "expandtemplates",
text: "{{Interwiki connexes/query|"+ qid +"|wikiurl=" + server + "}}",
prop: "wikitext",
format: "json"
};
// launching the data loading
makeSPARQLQuery(endpointUrl,
query_too_much_incoming_uses,
(enough) => {
if (!enough.boolean){
// if there is too much link of the form « iw -> p -> article_item », we parameter the query not to include them
params.text = "{{Interwiki connexes/query|"+ qid +"|forward_only|wikiurl=" + server + "}}";
}
// expanding the query template and launching the extra iw links retrieving
frw_api.get( params ).done( function ( data ) {
makeSPARQLQuery(endpointUrl, data.expandtemplates.wikitext,
handle_sparql_interwikis_results // loading the extra iw in the interface.
);
});
}
);
}
$(init);
}
mw.hook('wikipage.content')
.add(init_extralinks)
.add(function(){
mw.hook('wikipage.content').remove(init_extralinks);
});
});
/* </nowiki> */