User:*Youngjin/common.js

From Wikidata
Jump to navigation Jump to search

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.
// **********************************************************************
// **                 ***WARNING GLOBAL GADGET FILE***                 **
// **             changes to this file affect many users.              **
// **           please discuss on the talk page before editing         **
// **                                                                  **
// **********************************************************************

$( function() {
	var $alinks = mw.util.$content.find( 'a' );
	$alinks.each( function() {
		var $tablink = $( this );
		if ( $tablink.hasClass( 'external' ) && $tablink.attr( 'href' ).indexOf( mw.config.get( 'wgServer' ) ) !== 0 ) {
			$tablink.attr( 'target', '_blank' );
		}
	});
});

// [[User:Ricordisamoa/DeletionHelper.js]]
mw.loader.load('//www.wikidata.org/w/index.php?title=User:Ricordisamoa/DeletionHelper.js&action=raw&ctype=text/javascript');

// markblocked
mw.loader.load('//ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-markblocked.js&action=raw&ctype=text/javascript');

//massrollback.js
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Writ Keeper/Scripts/massRollback.js&action=raw&ctype=text/javascript');

// wikidata item info 
mw.loader.load("//www.wikidata.org/w/index.php?title=User:Yair rand/WikidataInfo.js&action=raw&ctype=text/javascript");

/*!
 * Some enhancement on RfD that is enabled by default just for admins
 * Originally was developed inside Merge.js by me but I decided to split it
 * It will add a quick delete button near each header that is requested by merge.js
 * and will add a number near links button so admins can quickly detect an item has
 * not any back link then safely delete it. It also will check recent delete
 * log every 5 seconds and will make deleted item links on RfD red link
 * to prevent conflicts on deletion.
 * 
 * @author User:Ebraminio <ebrahim -at- gnu.org>
 * @license CC-Zero
 */

/*jslint indent: 2, regexp: true, unparam: true, browser: true*/
/*jshint unused: false*/
/*global mediaWiki, jQuery*/
(function ($, mw) {
  'use strict';

  var api = new mw.Api();

  /**
   * Get backlink (50 limited)
   */
  function getBackLinks(itemId) {
    return api.get({
      action: 'query',
      list: 'backlinks',
      bltitle: itemId,
      blnamespace: 0,
      bllimit: 50,
      format: 'json'
    }).then(function (data) {
      return $.map(data.query.backlinks, function (value) {
        return value.title;
      });
    });
  }

  /**
   * Delete an item
   */
  function deleteItem(id, reason) {
    return api.post({
      action: 'delete',
      title: id,
      reason: reason,
      token: mw.user.tokens.get('csrfToken')
    });
  }

  /**
   * Get recent deletions
   */
  function recentDeletions() {
    return api.get({
      action: 'query',
      list: 'recentchanges',
      rctype: 'log',
      format: 'json',
      rcprop: 'title|loginfo'
    });
  }

  if (mw.config.get('wgPageName').indexOf('Wikidata:Requests_for_deletions') === 0 &&
      mw.config.get('wgAction') === 'view') {
    $.when(
      $.ready,
      mw.loader.using('mediawiki.language')
    ).then(function () {
      var itemLinks = {};
      $('a[href^="/wiki/Q"]:not(.new)').each(function (i, x) { itemLinks[x.href.replace(/.*\//, '')] = x; });

      setInterval(function () {
        recentDeletions().then(function (r) {
          $.each(r.query.recentchanges, function (i, x) {
            if (x.ns !== 0 || x.logtype !== 'delete') { return; }
            $(itemLinks[x.title]).addClass('new');
            itemLinks[x.title] = undefined;
          });
        });
      }, 5000);

      $("#mw-content-text > p > .plainlinks").each(function (i, x) {
        var link = $(x).find('a:first'),
          deleteQuery = new mw.Uri(link.prop('href')).query,
          header,
          button;
        if (deleteQuery.action !== 'delete') {
          return;
        }

        header = link.parent().parent().prev().find('.mw-headline a');

        // add backlinks count
        getBackLinks(deleteQuery.title).then(function (backlinks) {
          var blcount = backlinks.length;
          if (blcount === 50) {
            blcount = '50+';
          }

          $('[href*="Special:WhatLinksHere"]', x).after(
            $('<span />')
              .css('color', blcount === 0 ? 'black' : 'red')
              .text(' (' + mw.language.convertNumber(blcount) + ')')
          );
        });
        
        // don't add delete button if is deleted already
        if (header.hasClass('new')) {
          return;
        }

        button = $('<button>', {
          title: 'Quick Delete',
          style: 'cursor: pointer'
        }).append($('<img />', {
          src: '//upload.wikimedia.org/wikipedia/commons/thumb/8/89/Symbol_delete_vote.svg/15px-Symbol_delete_vote.svg.png',
          width: '15',
          height: '15'
        })).click(function (x) {
          header.css('text-decoration', 'line-through');
          deleteItem(deleteQuery.title, deleteQuery.wpReason).then(function () {
            header.addClass('new');
          });
          // delete button after delete request
          button.remove();
        });
        // insert the button
        header.after(button);
      });
    });
  }
}(jQuery, mediaWiki));