Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Usage: adds a search button to all sections, that after clicking tries to search the articles referenced in that section, gather all see/do listings (with wikidata and wikipedia article in the specified language). Then it sorts the listings acc. to number of the views of the wikipedia article, and prints first 9 listings as templates for copy/paste. */ function gatherPagePOIs(href) { return $.ajax({ url: href, error: (xhr, status, error) => { console.error('Error fetching page:', href, status, error); } }); } function getAverageViews(lang, articleName, res) { var d = new Date(); d.setMonth(-1); d.setDate(1); var to = d.toISOString().split('T')[0].replaceAll('-', ''); d.setMonth(-6); var from = d.toISOString().split('T')[0].replaceAll('-', ''); var url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/" + lang + ".wikipedia/all-access/all-agents/" + articleName + "/monthly/" + from + "/" + to; return $.ajax({ url: url, method: 'GET', dataType: 'json', }).then((data) => { var views = data.items.map(function(item) {return item.views;}); var averageViews = views.length ? (views.reduce(function(a, b) {return (a + b);}) / views.length) : 0; averageViews = Math.ceil(averageViews); res[2] = averageViews; }); } function makeSPARQLQuery(endpointUrl, sparqlQuery ) { var settings = { headers: { Accept: 'application/sparql-results+json' }, data: { query: sparqlQuery } }; return $.ajax( endpointUrl, settings ); } function getWpViews(entries, lang) { const wdEndpointUrl = 'https://query.wikidata.org/sparql'; const wdStr = entries.map((v) => ("wd:" + v[1])).join(" "); const wdQuery = ` SELECT ?item ?itemLabel_en ?itemLabel_main ?articleTitle WHERE { VALUES ?item { ` + wdStr + ` } ?sitelink schema:about ?item; schema:isPartOf <https://${lang}.wikipedia.org/>; schema:name ?articleTitle. SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . ?item rdfs:label ?itemLabel_en . } SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang}" . ?item rdfs:label ?itemLabel_main . } } `; return makeSPARQLQuery(wdEndpointUrl, wdQuery) .then((data) => { promises = []; data.results.bindings.forEach((r) => { wdId = 'Q' + r.item.value.split("/Q")[1]; e = entries.find((v) => {return v[1] == wdId;}) e[3] = r.itemLabel_en.value; e[4] = r.itemLabel_main.value; promises.push( getAverageViews(lang, r.articleTitle.value, e) ); }); return Promise.all(promises); }) } function appendTemplate(parent, type, e) { alt = ""; if (e[3] != e[4]) alt = `| alt=${e[4]} `; parent.append(`<br/>* {{${type} | name=[[${e[0]}#${e[1]} | ${e[3]}]] ${alt} | wikidata=${e[1]}\}\} <!-- ${e[2]} -->`) } function appendSection(e, title) { const p = $('<p>'); e.append($('<div>').append($('<h4>').text(title)).append(p)); return p; } function getListings(id) { var childWdSee = []; var childWdDo = []; var childWdSeeViews = []; var childWdDoViews = []; var promises = []; targetElement = $("#" + id).parent(); targetLang = $("#lang" + id)[0].value; targetElement.find("a[rel='mw:WikiLink']").each((_, link) => { const href = $(link).attr('href'); const page = href.substr(href.indexOf("/wiki/") + 6); if (href) { promises.push(gatherPagePOIs(href).then(function(data) { // Create a temporary DOM element to parse the returned page content const tempDom = $('<div>').html(data); s=tempDom.find("section:has(h2[id='See'])"); s.find(".vcard > span[id^=Q]").each((_, child) => { const wikidataID = $(child).attr('id'); //console.log(href, wikidataID); childWdSee.push([page, wikidataID, 0, "", ""]); }); s=tempDom.find("section:has(h2[id='Do'])"); s.find(".vcard > span[id^=Q]").each((_, child) => { const wikidataID = $(child).attr('id'); //console.log(href, wikidataID); childWdDo.push([page, wikidataID, 0, "", ""]); }); }) ); } }); Promise.all( promises ).then( function() { /* childWdSee = childWdSee.slice(0, 5); childWdDo = childWdDo.slice(0, 5); */ var getViewsPromises = []; getViewsPromises.push(getWpViews(childWdSee, targetLang)); getViewsPromises.push(getWpViews(childWdDo, targetLang)); Promise.all(getViewsPromises).then(function() { childWdSee = childWdSee.sort((a,b) => {return b[2] - a[2];}); childWdDo = childWdDo.sort((a,b) => {return b[2] - a[2];}); seeSubel = appendSection(targetElement, "See"); childWdSee.slice(0, 9).forEach((v) => { appendTemplate(seeSubel, "see", v); }); doSubel = appendSection(targetElement, "Do"); childWdDo.slice(0, 9).forEach((v) => { appendTemplate(doSubel, "do", v); }); }); }); } jQuery(document).ready(function() { if ('wgCoordinates' in RLCONF) { $('.mw-heading2').each((_, el) => { const id = $(el).attr('id'); $(`<span style="background: #eee;padding: 0 3px 3px 3px"><button id="btn${id}">🔎</button><input id="lang${id}" type="text" size="2" value="en" title="searched wikipedia language"></span>`) .appendTo($(el)); $(`#btn${id}`).on('click', () => { getListings(id); }) }); } });