Interwiki Bars. Si teniu propostes per l'aplicació, poseu-les aquí.
HTML
Javascript (amb jQuery)
Aquesta secció informarà de les novetats de l'aplicació Codi
S'inclou el codi utilitzat en la versió v1.1.HTML
Codi:
<input type="hidden" class="app-identifier" value="Interwiki_Bars"/>
<h2>Interwiki Bars</h2><br/>
<p>L'aplicació <b>Interwiki Bars</b> busca els articles més propers a un determinat article i els ordena per rellevància segons el nombre de vegades en què apareixen.
Per fer-ho utilitza l'API de <a class="external_link" href="https://www.mediawiki.org/wiki/MediaWiki"><b>Mediawiki</b></a>.</p>
<p>Article: <input class="bars_article" type="text"/>
<select name="selected_id" id="filter_lang_a">
<option value="de">Alemany</option>
<option value="en">Anglès</option>
<option value="ar">Àrab</option>
<option value="arz">Àrab egipci</option>
<option value="ca" selected="selected">Català</option>
<option value="ceb">Cebuà</option>
<option value="es">Espanyol</option>
<option value="fr">Francès</option>
<option value="it">Italià</option>
<option value="jp">Japonès</option>
<option value="nl">Neerlandès</option>
<option value="fa">Persa</option>
<option value="pl">Polonès</option>
<option value="pt">Portuguès</option>
<option value="ru">Rus</option>
<option value="sv">Suec</option>
<option value="uk">Ucraïnès</option>
<option value="vi">Vietnamita</option>
<option value="war">Waray-waray</option>
<option value="zh">Xinès</option>
</select>
<input class="button2" type="submit" name="bars_search" value="Buscar" tabindex="5"/><input class="button2" type="submit" name="bars_clear" value="Netejar" tabindex="5"/>
</p>
<h4>Registre de canvis</h4>
<p>
07-02-2021 - v1.0 - Plataforma bàsica per obtenir enllaços rellevants<br/>
08-02-2021 - v1.1 - Ampliació del llistat d'idiomes: <a class="external_link" href="https://en.wikipedia.org/wiki/List_of_Wikipedias#Detailed_list">20 amb més articles</a>
</p>
<h4>Enllaços relacionats</h4>
<p>
Viquipèdia - <a class="external_link" href="https://ca.wikipedia.org/wiki/Ajuda:Com_iniciar_una_p%C3%A0gina">Iniciar un article</a><br/>
Viquipèdia - <a class="external_link" href="https://ca.wikipedia.org/wiki/Viquip%C3%A8dia:Articles_demanats">Articles demanats</a><br/>
Twitter - Hashtag <a class="external_link" href="https://twitter.com/hashtag/ArticlesDemanats?src=hashtag_click"><i>#ArticlesDemanats</i></a>
</p>
<p>Aquesta aplicació ha estat creada per l'usuari <b><a class="external_link" href="https://ca.wikipedia.org/w/index.php?title=Usuari:Wecoc">Wecoc</a></b> mitjançant la plataforma <i>Forumotion</i>.</p><br/>
<p class="filter_state"> </p><br/>
<div class="bars_table"></div>
Javascript (amb jQuery)
Codi:
$(document).ready(function(){
var app = document.querySelector('.app-identifier');
if (app == null) { return; }
if (app.value !== "Interwiki_Bars") { return; }
// Set button functions
var search_button = document.querySelector('.button2[name="bars_search"]');
search_button.onclick = function(){ bars_search() };
// Set storage array
var articles_stored = [];
var cleaning_mode = false;
var global_search_counter = 0;
// Set button functions
var clear_button = document.querySelector('.button2[name=bars_clear]');
clear_button.onclick = function(){ cleaning_mode = true; bars_clear_data(); };
function bars_search(){
// Clear current data
bars_clear_data();
cleaning_mode = false;
var article = document.querySelector('.bars_article').value; if (article == "") return;
var lang = document.querySelector('#filter_lang_a').value;
var article_url = article.replace(/\s+/g, '_');
var url = "https://" + lang + ".wikipedia.org/w/api.php?action=query&list=backlinks&bltitle=" + article_url +
"&bllimit=500&format=json";
$.ajax({
crossOrigin: true,
dataType: 'jsonp',
url: url,
success: function(data) {
// Return if cleaning
if (cleaning_mode) { filter_clear_data(); return; }
// Get backlinks
var links = data.query.backlinks;
if (links.length == 0) return;
var articles = links.map(function(e){ return e.title.replace(/\s+/g, '_'); }), a;
for(a in articles) { if(articles[a].includes(":")){ delete articles[a]; } }
articles = Object.values(articles);
if (articles.length == 0) return;
var i = 0;
global_search_counter = 0;
bars_search_data(i, articles, lang);
}
});
}
function bars_clear_data(){
// Clear storage
articles_stored = [];
// Clear table
var table = document.querySelector('.bars_table');
table.innerHTML = "";
// Set state to null
var filter_state = document.querySelector('.filter_state');
filter_state.setAttribute("value", "null");
filter_state.innerText = "";
}
function bars_search_data(i, ary, lang){
var article_url = ary[i];
var url = "https://" + lang + ".wikipedia.org/w/api.php?action=query&titles=" + article_url +
"&prop=links&pllimit=max&format=json";
$.ajax({
crossOrigin: true,
dataType: 'jsonp',
url: url,
success: function(data) {
if (global_search_counter >= ary.length) {
store_into_bars_table(lang);
return;
}
global_search_counter += 1;
// Return if cleaning
if (cleaning_mode) { filter_clear_data(); return; }
// Change state to Processing
var filter_state = document.querySelector('.filter_state');
filter_state.setAttribute("value", "processing");
filter_state.innerText = "Buscant enllaços .... " + global_search_counter + " / " + ary.length;
// Get articles
var pageid = Object.keys(data.query.pages)[0];
var links = data.query.pages[pageid].links;
if (!links) { return; }
var articles = links.map(function(e){ return e.title.replace(/\s+/g, '_'); }), a;
for(a in articles) { if(articles[a].includes(":")){ delete articles[a]; } }
articles_stored = articles_stored.concat(articles);
}
});
if (i >= ary.length) return;
bars_search_data(i + 1, ary, lang);
}
function store_into_bars_table(lang){
var articles_hash = {};
for (i = 0; i < articles_stored.length; i++) {
if (articles_hash[articles_stored[i]]) {
articles_hash[articles_stored[i]]++;
} else {
articles_hash[articles_stored[i]] = 1;
}
}
var max_matches = Math.max.apply(null, Object.values(articles_hash));
for (i = 0; i < articles_stored.length; i++) {
var current = articles_stored[i];
if (articles_hash[current] < (max_matches / 100) || articles_hash[current] == 1) {
delete articles_hash[current];
}
}
var sorted_keys = Object.keys(articles_hash);
sorted_keys.sort(function(a, b) { return articles_hash[b] - articles_hash[a] });
var key_iteration = 0;
bars_check_link_status(key_iteration, articles_hash, sorted_keys, lang);
}
function bars_check_link_status(i, articles_hash, sorted_keys, lang) {
if (i >= sorted_keys.length) {
// Change state to Complete
var filter_state = document.querySelector('.filter_state');
filter_state.setAttribute("value", "complete");
filter_state.innerText = "Procés completat!";
return;
}
var article_url = sorted_keys[i];
if (article_url == "undefined") {
bars_check_link_status(i + 1, articles_hash, sorted_keys, lang);
return;
}
var table = document.querySelector('.bars_table');
var max_matches = articles_hash[sorted_keys[((sorted_keys[0] == "undefined") ? 1 : 0)]];
var url = "https://" + lang + ".wikipedia.org/w/api.php?action=query&titles=" + article_url + "&format=json";
$.ajax({
crossOrigin: true,
dataType: 'jsonp',
url: url,
success: function(data) {
// Return if cleaning
if (cleaning_mode) { filter_clear_data(); return; }
// Change state to Processing
var filter_state = document.querySelector('.filter_state');
filter_state.setAttribute("value", "processing");
filter_state.innerText = "Creant taula .... " + (i + 1) + " / " + sorted_keys.length;
// Get percentage
var perc = Math.round(articles_hash[article_url] * 100 / max_matches);
// Check link type
var pageid = Object.keys(data.query.pages)[0];
var a_url = (data.query.normalized) ? data.query.normalized[0]["from"] : article_url;
var a_title = (data.query.normalized) ? data.query.normalized[0]["to"] : article_url;
if (pageid != -1) {
// Blue link
var href = "https://" + lang + ".wikipedia.org/wiki/" + a_url;
var html = "<div class=\"found\"><div><a class=\"wiki\" href=\"" + href + "\">" + a_title + "</a></div>" +
"<div class=\"bar_back\"><div class=\"bar\" style=\"width:" + perc + "%\"><div class=\"digit\">" +
articles_hash[article_url] + "</div></div></div></div>";
table.innerHTML = table.innerHTML + html;
} else {
// Red link
var href = "https://" + lang + ".wikipedia.org/wiki/" + a_url;
var html = "<div class=\"not_found\"><div><a class=\"wiki_red\" href=\"" + href + "\">" + a_title + "</a></div>" +
"<div class=\"bar_back\"><div class=\"bar\" style=\"width:" + perc + "%\"><div class=\"digit\">" +
articles_hash[article_url] + "</div></div></div></div>";
table.innerHTML = table.innerHTML + html;
}
bars_check_link_status(i + 1, articles_hash, sorted_keys, lang);
}
});
}
});