MediaWiki:Gadget-EmptyDetect.js

From MaRDI portal

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// from https://www.wikidata.org/w/index.php?title=MediaWiki:Gadget-EmptyDetect.js&oldid=1620417893
/*!
 * auto detect that the item is previously merged with which item
 * Written by me inside MediaWiki:Gadget-Merge.js
 * now its code is extracted but currently is not a separate gadget
 * and auto enabled with Merge.js
 * 
 * @author User:Ebrahim <ebrahim -at- gnu.org>
 */

/*jslint browser: true, regexp: true, indent: 2, unparam: true*/
/*jshint unused: false*/
/*global $, mw, mergeTool*/
$(function () {
  'use strict';
  if (mw.config.get('wgNamespaceNumber') !== 0 || !mw.config.exists('wbEntityId')) { return; }
  mw.hook('wikibase.entityPage.entityLoaded').add(function (entity) {
    // give up if there is a link on item
  	if (entity.sitelinks !== undefined) { return; }
    new mw.Api().get({
      action: 'query',
      prop: 'revisions',
      titles: mw.config.get('wbEntityId'),
      rvlimit: 1,
      rvprop: 'content',
      format: 'json',
      rvdir: 'newer'
    }).then(function (x) {
      var itemOldEntity, site, title;
      itemOldEntity = JSON.parse($.map(x.query.pages, function (x) { return x; })[0].revisions[0]['*']);
      // if previous item had not any site link, give up
      if (itemOldEntity.links === undefined) { return; }

      // put last item site link on local variables
      $.map(itemOldEntity.links, function (x, y) { site = y; title = x.name; });

      new mw.Api().get({
        action: 'wbgetentities',
        sites: site,
        titles: title
      }).then(function (x) {
        // if query not contains any entity, give up
        if (x.entities['-1'] !== undefined) { return; }

        var dest = $.map(x.entities, function (x, y) { return y; })[0].toUpperCase();

        mw.notify(
          $('<span>').append(
            'Might have been merged with ',
            $('<a href="' + mw.util.getUrl(dest) + '">' + dest + '</a>'),
            ' ',
            window.mergeTool === undefined ? '' : $('<a href="#" title="Merge it">').append($('<img>', {
              src: '//upload.wikimedia.org/wikipedia/commons/thumb/1/10/Pictogram_voting_merge.svg/15px-Pictogram_voting_merge.svg.png'
            }).click(function (event) { event.preventDefault(); mergeTool.launchDialog(dest); }))
          ),
          {
            autoHide: false
          }
        );
      });
    });
  });
});