User:JonnyJD/berlin transport.js
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)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
// based on importScript( 'User:JonnyJD/consistency_check.js' );
//
// Use with:
// importScript( 'User:JonnyJD/berlin_transport.js' );
// in https://www.wikidata.org/wiki/Special:MyPage/common.js
var berlin_transport = {
p_prefix: 'P',
q_prefix: 'Q',
running: false,
values: {
'P17': ['Q183'], // Germany
'P31': ['Q55488', 'Q928830', 'Q1793804'], // train station, metro (U), S-Bahn
'P131': ['Q64', 'Q1208'] // Berlin, Brandenburg
},
reciproke: {
'P197': ['P197'] // adjacent station
},
dependencies: {
'P197': ['P17', 'P31', 'P131', 'P625'] // adjacent station
},
init: function () {
var self = this;
var portletLink = mw.util.addPortletLink('p-tb', '#', 'Berlin Transport','t-berlin_transport');
$(portletLink).click(function () {
self.run();
return false ;
});
},
run: function () {
var self = this;
if (self.running) return;
self.running = true;
var q = mw.config.get('wgPageName');
$.getJSON ('//www.wikidata.org/w/api.php?callback=?', {
action: 'wbgetentities',
ids: q,
format: 'json',
props: 'claims'
}, function(data) {
var check = [];
var html_out = '';
var found_claims = false;
var dependencies;
if (data.entities[q].claims !== undefined) {
html_out += "<div>";
html_out += "<div style='float:left; padding-right:25px;'>";
html_out += "<h2 class='wb-section-heading'>Symmetry</h2><table border=0>" ;
$.each (data.entities[q].claims, function(k, v) {
if (self.reciproke[k] !== undefined) {
$.each (v, function(k2, v2) {
if (v2.mainsnak.datavalue === undefined) return; // No value
var q2 = self.q_prefix + v2.mainsnak.datavalue.value['numeric-id'];
var id = 'consistency_' + k + '_' + q2;
html_out += "<tr><th class='label_"+k+"'>" + k + "</th><td id='" + id + "_c' style='text-align:center'>?</td><td id='" + id + "_v'><a href='/wiki/" + q2.toUpperCase() + "'>" + q2.toUpperCase() + "</a></td></tr>";
check.push({prop:k, orig:q, target:q2, any:self.reciproke[k], id:id, q_prefix: self.q_prefix});
found_claims = true;
});
}
});
html_out += "</table></div>";
html_out += "<div style='float:left; padding-right:25px;'>";
html_out += "<h2 class='wb-section-heading'>Dependencies</h2><table border=0>";
$.each(data.entities[q].claims , function (k, v) {
dependencies = true;
if (self.dependencies[k] !== undefined) {
html_out += "<tr><th class='label_" + k + "'>" + k + "</th><td>";
$.each(self.dependencies[k], function(k2, v2) {
if (!(v2 in data.entities[q].claims) || !(!(k in self.values) || v2 in self.values[k])) {
if (dependencies) {
dependencies = false;
html_out += " <span style='color:red;'>missing:</span> ";
}
html_out += v2 + " ";
}
});
if (dependencies) {
html_out += " <span style='color:green;'>fine</span>";
}
html_out += "</td></tr>";
}
});
html_out += "</table></div>";
html_out += "</div>";
}
if (!found_claims) {
html_out = "<div>No consistency checks for the claims of " + q + "</div>";
}
// open dialog for summary
// partly taken from labelLister
mw.loader.using(['jquery.ui'], function () {
var summary;
var dialogWidth = Math.min(document.body.clientWidth - 200, 800);
var dialogHeight = Math.min(document.body.clientHeight - 60, 650);
if (document.getElementById("content")) {
summary = $(html_out).appendTo('#content');
} else {
summary = $(html_out).appendTo('#mw_content');
}
summary.dialog({
title: 'Consistency Check',
autoOpen: true,
modal: true,
width: dialogWidth,
height: dialogHeight,
show: "blind",
hide: "blind",
buttons: []
});
});
$.each(check, function(k, v) {
self.checkReciproke(v);
});
});
},
checkReciproke: function(d) {
$.getJSON('//www.wikidata.org/w/api.php?callback=?', {
action: 'wbgetentities',
ids: d.target,
format: 'json',
props: 'claims'
}, function(data) {
var oneway = "<span style='color:red;font-weight:bold'>→</span>";
var bothways = "<span style='color:green'>↔</span>";
var found = false;
if (data.entities[d.target].claims !== undefined) {
$.each(data.entities[d.target].claims, function(k, v) {
if (-1 == $.inArray(k, d.any)) return ;
$.each(v, function (k2, v2) {
if (v2.mainsnak.datavalue === undefined) return; // No value
var q2 = d.q_prefix + v2.mainsnak.datavalue.value['numeric-id'] ;
if (q2 == d.orig) found = true ;
});
});
}
if (found) {
$('#'+d.id+'_c').html(bothways);
} else {
$('#'+d.id+'_c').html(oneway);
}
});
},
the_end: ''
};
$(function() { berlin_transport.init ();});