MediaWiki:AddWikidata.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// <nowiki>

/*
  Quick helper script to add the Wikidata id to an artwork. 

  Author: [[User:Multichill]], based on [[MediaWiki:Catcheck.js]] by [[User:Lupo]]
  License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)
  
  Choose whichever license of these you like best :-)
*/

/*jshint curly:false*/
/*global mw:false, $:false*/
$(function() {
'use strict';

if (mw.config.get('wgNamespaceNumber') !== 6 || mw.config.get('wgAction') !== 'edit') {
  return;
}

window.AddWikidata =
{
  run : function ()
  {
    // Grab the text
    var text = document.getElementById ('wpTextbox1');
    if (!text) return;
    // It already contains the Q id.
    if (text.value.search(/[wW]ikidata\s*=\s*Q\d*/) > 0) return;
    // Prompt for the id, would be great if we could pass this in the url
    var wikidataIdUrl = window.location.href.match(/wikidataid=(Q\d*)/)[1];
    var wikidataid = prompt('What is the wikidata id (Q included)?', wikidataIdUrl);
    if (wikidataid) {
      // Look for existing Wikidata field to update
      if (text.value.search(/wikidata\s*=/i) > 0) {
        text.value = (text.value || "").replace (/(wikidata\s*=)/i, "$1" + wikidataid + "\n");
      } else {
        // Replace the "Artwork" tag, with the "Artwork" tag + wikidata id. Also works for some other template types
        text.value = (text.value || "").replace (/\{\{\s*(Artwork|Painting|Art Photo|Google Art Project|Google Cultural Institute|LSH artwork|Walters Art Museum artwork|NARA-image-full|Category definition: Object)(\s*\n)/i, "{{$1$2| wikidata = " + wikidataid + "\n");
      }
      // Set edit summary
      var summary = document.getElementById ('wpSummary');
      if (!summary) return;
      var val = summary.value || "";
      summary.value = val + (val ? ' ' : "") + 
        "Linking this image to [[:d:" + wikidataid + "]] - [[User:Multichill/Same image without Wikidata|Help sort out these images!]]";
      // Autosave the page
      document.editform.wpSave.click();
    }
  }
};
window.AddWikidata.run();
});

// </nowiki>