User:JonnyJD/musicbrainz.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.
// Use with:
// importScript( 'User:JonnyJD/musicbrainz.js' );
// in https://www.wikidata.org/wiki/Special:MyPage/common.js
//
// The script runs as soon as the item is loaded
// and displays a red warning text in the upper right corner
// when the item includes an MBID, but the item is not linked at MusicBrainz.
var musicbrainz = {
running: false,
properties: {
'P434': 'artist',
'P435': 'work',
'P436': 'release-group',
'P966': 'label',
'P982': 'area',
'P1004': 'place'
},
// not used currently
init: function () {
var self = this;
var portletLink = mw.util.addPortletLink('p-tb', '#', 'MusicBrainz','t-musicbrainz');
$(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) {
if (data.entities !== undefined && data.entities[q].claims !== undefined) {
$.each(data.entities[q].claims, function(k, v) {
if (self.properties[k] !== undefined) {
$.each(v, function(k2, v2) {
if (v2.mainsnak.datavalue === undefined) return; // No value
var mbid = v2.mainsnak.datavalue.value;
self.checkMusicBrainz(self.properties[k], mbid, q);
});
}
});
}
});
},
checkMusicBrainz: function(entity, mbid, q) {
$.getJSON('//www.musicbrainz.org/ws/2/' + entity + '/' + mbid + '/', {
fmt: 'json',
inc: 'url-rels'
}, function(data) {
var found_q = false;
$.each(data.relations, function(k, v) {
if (v.type == "wikidata") {
if (v.url.resource.indexOf(q) != -1) {
found_q = true;
return false; // this breaks the $.each() loop
}
}
});
if (!found_q) {
var link = '<a href="https://musicbrainz.org/' + entity + '/' + mbid + '">MusicBrainz</a>';
mw.util.jsMessage('<span style="color:red;">Not linked on ' + link + '!</span>');
}
});
}
};
//addOnloadHook(function() { musicbrainz.init(); });
$(function() { musicbrainz.run(); });