MediaWiki:Gadget-relateditems.js

/*********************************************************************************************
 * This gadget displays related items on an item page.                                       *
 * How to use : Go to Special:Preferences and set a checkmark     *
 * on "relateditems".                                                          *
 * on "relateditems".                                                          *

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

if (mw.config.get('wgNamespaceNumber') !== 0 || !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...' },          nb: { 'title': 'avledet-utsagn', 'more': 'mer', 'inverse': 'invers', 'show-inverse': 'vis inverse utsagn', 'no-result': 'ingen resultat', 'loading': 'laster...' },          '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 = '\  \ \            \             \        ';

function loaditems { $('span#inverseclaims').text(messages.title); $('#inversesection').find('.wikibase-showinverse').html(messages.loading); var api = new mw.Api({           ajax: {                url: 'https://query.wikidata.org/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:P7087 ?inverseproperty .\				} LIMIT 300 }\				SERVICE wikibase:label {bd:serviceParam wikibase:language "' + lang + ',en" .}\			} ORDER BY ?property'        }).done(function(data) {        	var stid = false;            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('http://gratisdata.miraheze.org/entity/', 'i'); if (pid != stid){ var statementgroup = '\  \ \	                               \	                                    ' + page.inversepropertyLabel.value + ' \ \	                           \	                            \	                                \	                                \	                            \	                        ';					$('#inversesection').find('.wikibase-listview').append(statementgroup); stid = pid; }               var statement = ' \ \						 \ \                   \                        \                            \                                \                                    \                                    \                                \                                \                                    \                                        ' + page.subjectLabel.value + '\ \                               \                            \                        \                    \                ';                $('.wikibase-statementgroupview').last.find('.wikibase-statementlistview-listview').append(statement); }           if (data.results.bindings.length == 300) { $('#inversesection').find('.wikibase-showinverse').html('' + messages.more + ''); } 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( $( '' ) .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));