|
|
Line 1: |
Line 1: |
| /*********************************************************************************************
| | mw.loader.load( 'https://www.wikidata.org/w/index.php?title=User:Techwizzie/forage.js&action=raw&ctype=text/javascript' ); |
| * This gadget displays related items on an item page. *
| |
| * *
| |
| * How to use : Go to [[Special:Preferences#mw-prefsection-gadgets]] and set a checkmark *
| |
| * on "relateditems". *
| |
| * via: https://www.wikidata.org/wiki/MediaWiki:Gadget-relateditems.js *
| |
| **********************************************************************************************/
| |
| | |
| (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 = require( './relateditems-i18n.json' ),
| |
| 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"></div> \
| |
| </div>';
| |
| | |
| 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 + ',mul,en" .}\
| |
| } ORDER BY ?property'
| |
| }).done(function(data) {
| |
| var stid = false;
| |
| var baseurl = 'https://www.wikidata.org/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('http://www.wikidata.org/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"> \
| |
| | |
| </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('http://www.wikidata.org/entity/', '');
| |
| if (eid.startsWith('Q')) {
| |
| entityurl = baseurl + 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"> \
| |
| | |
| </div> \
| |
| </div> \
| |
| </div> \
| |
| </div> \
| |
| </div> \
| |
| </div>';
| |
| $('.wikibase-statementgroupview').last().find('.wikibase-statementlistview-listview').append(statement);
| |
| }
| |
| if (data.results.bindings.length == 300) {
| |
| | |
| } 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', '#' )
| |
| .text( messages['show-inverse'] )
| |
| .click( function ( event ) {
| |
| event.preventDefault();
| |
| loaditems();
| |
| })
| |
| );
| |
| }
| |
| | |
| $(init);
| |
| | |
| }(mediaWiki, jQuery, wikibase));
| |