Difference between revisions of "MediaWiki:Gadget-relateditems.js"

From wikibase-docker
(Created page with "(function (mw, $, wb) { "use strict"; var inverseLabelItemProperty = "P127"; var wikibaseUrl = "https://wkibase.dh-lab.hu"; var wdqsUrl = "https://wdqs.wkibase.dh-lab...")
 
 
Line 3: Line 3:
  
 
   var inverseLabelItemProperty = "P127";
 
   var inverseLabelItemProperty = "P127";
   var wikibaseUrl = "https://wkibase.dh-lab.hu";
+
   var wikibaseUrl = "https://itidata.abtk.hu";
   var wdqsUrl = "https://wdqs.wkibase.dh-lab.hu";
+
   var wdqsUrl = "https://query.itidata.abtk.hu";
  
 
   var itemNamespace = 120;
 
   var itemNamespace = 120;

Latest revision as of 15:01, 6 April 2022

(function (mw, $, wb) {
  "use strict";

  var inverseLabelItemProperty = "P127";
  var wikibaseUrl = "https://itidata.abtk.hu";
  var wdqsUrl = "https://query.itidata.abtk.hu";

  var itemNamespace = 120;

  if (
    mw.config.get("wgNamespaceNumber") !== itemNamespace ||
    !mw.config.exists("wbEntityId")
  ) {
    return;
  }

  var lang = mw.config.get("wgUserLanguage");
  var messages,
    entityid = mw.config.get("wbEntityId"),
    api = new mw.Api();

  messages = (function () {
    var translations = {
        de: {
          title: "Abgeleitete Aussagen",
          more: "mehr",
          inverse: "invers",
          "show-inverse": "zeige abgeleitete Aussagen",
          "no-result": "kein Ergebnis",
          loading: "laden...",
        },
        en: {
          title: "Derived statements",
          more: "more",
          inverse: "inverse",
          "show-inverse": "show derived statements",
          "no-result": "no result",
          loading: "loading...",
        },
        fr: {
          title: "Déclarations dérivées",
          more: "plus",
          inverse: "inverse",
          "show-inverse": "voir les déclarations dérivées",
          "no-result": "pas de résultats",
          loading: "chargement...",
        },
        hu: {
          title: "Származtatott állítások",
          more: "több",
          inverse: "inverz",
          "show-inverse": "származtatott állítások megjelenítése",
          "no-result": "nincs találat",
          loading: "betöltés...",
        },
        nb: {
          title: "avledet-utsagn",
          more: "mer",
          inverse: "invers",
          "show-inverse": "vis inverse utsagn",
          "no-result": "ingen resultat",
          loading: "laster...",
        },
        sv: {
          title: "Härledda uttalanden",
          more: "mer",
          inverse: "omvänt",
          "show-inverse": "visa härledda uttalanden",
          "no-result": "inget resultat",
          loading: "laddar...",
        },
        "zh-hans": {
          title: "派生陈述",
          more: "更多",
          inverse: "反向",
          "show-inverse": "显示派生陈述",
          "no-result": "无结果",
          loading: "加载中...",
        },
      },
      chain = mw.language.getFallbackLanguageChain(),
      len = chain.length,
      ret = {},
      i = len - 1;
    while (i >= 0) {
      if (translations.hasOwnProperty(chain[i])) {
        $.extend(ret, translations[chain[i]]);
      }
      i = i - 1;
    }
    return ret;
  })();

  var html =
    '\
        <h2 class="wb-section-heading section-heading wikibase-statements" dir="auto"><span id="inverseclaims" class="mw-headline"></span></h2>\
        <div class="wikibase-statementgrouplistview" id="inversesection"> \
             <div class="wikibase-listview"></div> \
             <div class="wikibase-showinverse" style="padding:10px;overflow:hidden;"></div> \
        </div>';

  function loaditems() {
    $("span#inverseclaims").text(messages.title);
    $("#inversesection").find(".wikibase-showinverse").html(messages.loading);
    var api = new mw.Api({
      ajax: {
        url: wdqsUrl + "/proxy/wdqs/bigdata/namespace/wdq/sparql?",
        dataType: "json",
        cache: true,
      },
    });
    api
      .get({
        query:
          "SELECT DISTINCT ?subject ?subjectLabel ?property ?inversepropertyLabel ?rank WHERE {{\
				SELECT ?subject ?property ?inverseproperty ?rank WHERE { \
		       	?subject ?claimpred ?statement .\
				?statement ?valpred wd:" +
          entityid +
          " ;\
				          wikibase:rank ?rank .\
				?property wikibase:claim ?claimpred;\
				          wikibase:statementProperty ?valpred;\
				          wdt:" +
          inverseLabelItemProperty +
          ' ?inverseproperty .\
				} LIMIT 300 }\
				SERVICE wikibase:label {bd:serviceParam wikibase:language "' +
          lang +
          ',en" .}\
			} ORDER BY ?property',
      })
      .done(function (data) {
        var stid = false;
        var baseurl = wikibaseUrl + "/wiki/";
        for (var k in data.results.bindings) {
          var page = data.results.bindings[k];
          var rank = page.rank.value
            .replace("http://wikiba.se/ontology#", "")
            .replace("Rank", "")
            .toLowerCase();
          var pid = page.property.value.replace(wikibaseUrl + "/entity/", "i");
          if (pid != stid) {
            var statementgroup =
              '\
	                        <div id="' +
              pid +
              '" class="wikibase-statementgroupview listview-item"> \
	                            <div class="wikibase-statementgroupview-property"> \
	                                <div class="wikibase-statementgroupview-property-label" dir="auto"> \
	                                    <a href="' +
              page.property.value.replace(
                wikibaseUrl + "/entity/",
                wikibaseUrl + "/wiki/Property:"
              ) +
              '">' +
              page.inversepropertyLabel.value +
              '</a> \
	                                </div> \
	                            </div> \
	                            <div class="wikibase-statementlistview"> \
	                                <div class="wikibase-statementlistview-listview"> \
	                                </div> \
	                            </div> \
	                        </div>';
            $("#inversesection")
              .find(".wikibase-listview")
              .append(statementgroup);
            stid = pid;
          }
          var entityurl = page.subject.value;
          var eid = entityurl.replace(wikibaseUrl + "/entity/", "");
          if (eid.startsWith("Q")) {
            entityurl = baseurl + "Item:" + eid;
          } else if (eid.startsWith("P")) {
            entityurl = baseurl + "Property:" + eid;
          } else if (eid.startsWith("L")) {
            entityurl = baseurl + "Lexeme:" + eid.replace(/-/, "#");
          }
          var statement =
            '<div class="wikibase-statementview wb-normal listview-item wikibase-toolbar-item"> \
					<div class="wikibase-statementview-rankselector"><div class="wikibase-rankselector ui-state-disabled"> \
						<span class="ui-icon ui-icon-rankselector wikibase-rankselector-' +
            rank +
            '"></span> \
					</div></div>  \
                    <div class="wikibase-statementview-mainsnak-container"> \
                        <div class="wikibase-statementview-mainsnak" dir="auto"> \
                            <div class="wikibase-snakview"> \
                                <div class="wikibase-snakview-property-container"> \
                                    <div class="wikibase-snakview-property" dir="auto"> \
                                    </div> \
                                </div> \
                                <div class="wikibase-snakview-value-container" dir="auto"> \
                                    <div class="wikibase-snakview-value wikibase-snakview-variation-valuesnak"> \
                                        <a href="' +
            entityurl +
            '">' +
            page.subjectLabel.value +
            "</a>\
                                    </div> \
                                </div> \
                            </div> \
                        </div> \
                    </div> \
                </div>";
          $(".wikibase-statementgroupview")
            .last()
            .find(".wikibase-statementlistview-listview")
            .append(statement);
        }
        if (data.results.bindings.length == 300) {
          $("#inversesection")
            .find(".wikibase-showinverse")
            .html(
              '<a href="' +
                wikibaseUrl +
                "/w/index.php?title=Special:WhatLinksHere&target=" +
                entityid +
                "&namespace=" +
                itemNamespace +
                '">' +
                messages.more +
                "</a>"
            );
        } else if (data.results.bindings.length === 0) {
          $("#inversesection")
            .find(".wikibase-showinverse")
            .html(messages["no-result"]);
        } else {
          $("#inversesection").find(".wikibase-showinverse").html("");
        }
      });
  }
  function init() {
    $(".wikibase-entityview-main").append(html);
    $("#inversesection")
      .find(".wikibase-showinverse")
      .append(
        $("<a>")
          .attr("href", "#")
          .attr("style", "border:1px solid #AAA;padding:10px 80px;")
          .text(messages["show-inverse"])
          .click(function (event) {
            event.preventDefault();
            loaditems();
          })
      );
  }

  $(init);
})(mediaWiki, jQuery, wikibase);