User:Nikki/LinkLabelsToLexemes.js

From Wikidata
Jump to navigation Jump to search

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.
/**
 * This adds links in the termbox to lexemes for each label where there is
 * a lexeme with a matching lemma in the same language and the lexeme has
 * a sense linking back to the item using P5137 (item for this sense).
 * 
 * To use it, add the following line to your common.js:
 * mw.loader.load("//www.wikidata.org/w/index.php?title=User:Nikki/LinkLabelsToLexemes.js&action=raw&ctype=text/javascript");
 * 
 * @license CC0-1.0
*/
/* jshint esversion: 6 */

(function () {
	if (mw.config.get("wgNamespaceNumber") !== 0)
		return;

	let mapping = {};

	function fetch_data(e) {
		let query = `
			select ?l ?lang ?label {
				values ?item { wd:${e.id} }
				?s wdt:P5137 ?item.
				?item rdfs:label|skos:altLabel ?label.
				?l ontolex:sense ?s;
					wikibase:lemma ?label.
				bind(lang(?label) as ?lang).
			}
		`;

		$.post("https://query.wikidata.org/sparql?format=json", { query: query }, function (data) {
			if (!data.results.bindings.length)
				return;

			$.each(data.results.bindings, function (i, v) {
				let lid = v.l.value.replace(/.*\//, "");		
				let lang = v.lang.value;
				let label = v.label.value;
				if (!mapping.hasOwnProperty(lang))
					mapping[lang] = [];
				if (!mapping[lang].hasOwnProperty(label))
					mapping[lang][label] = [];
				mapping[lang][label].push(lid);
			});

			link_labels_to_lexemes();
			document.querySelector(".wikibase-entitytermsforlanguagelistview-more a").onclick = link_labels_to_lexemes;
			mw.util.addCSS(`
				.wb-edit .n-lexeme-link { display: none; }
				.n-lexeme-link { font-size: smaller; }
			`);
		});
	}

	function link_labels_to_lexemes() {
		for (let lang of Object.keys(mapping)) {
			for (let row of document.querySelectorAll(`
				.wikibase-entitytermsforlanguageview-${lang} .wikibase-labelview-container,
				.wikibase-entitytermsforlanguageview-${lang} .wikibase-aliasesview-list-item
			`)) {
				if (row.querySelector(".n-lexeme-link") || row.nextElementSibling && row.nextElementSibling.classList.contains("n-lexeme-link")) {
					continue;
				}

				let label;
				let insert = "beforeend";
				if (!row.querySelector(".wikibase-labelview-text")) {
					insert = "afterend";
					label = row.textContent;
				} else {
					label = row.querySelector(".wikibase-labelview-text").textContent;
				}

				if (!mapping[lang][label])
					continue;
				let links = mapping[lang][label].map(x => `<a href="/wiki/Lexeme:${x}">${x}</a>`).join(mw.msg("comma-separator"));
				row.insertAdjacentHTML(insert, `<span class="n-lexeme-link"> ${mw.msg("parentheses", links)}</span>`);
			}
		}
	}

	mw.hook('wikibase.entityPage.entityView.rendered').add(function () {
		mw.hook("wikibase.entityPage.entityLoaded").add(fetch_data);
	});

})();