User:Nardog/createRedirects.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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 *
 * Create redirects to a given page
 * First click on the toolbox link
 * highlights possible redirects
 * second click creates them
 *
 * Tested at [[Epipactis helleborine subsp. helleborine]]
 *
 * @author Rillke
 * @author Nardog
 * @license GPL v.3
 **/ 
 
 (function ($, mw) {

	mw.util.addCSS(
		'.syn-x { background:#ddd; cursor:pointer; }' +
		'.syn-to-create { background:#ffb; outline:dotted 1px #999; }' +
		'.syn-to-create:before { content: \" to be created - \"; }' +
		'.syn-created { background:#bfb; }' +
		'.syn-create-err { background:#fbb; }');
	var p = mw.util.addPortletLink('p-tb', '#createRedirs', 'Create Redirects',
		't-syn-redir', 'Create redirects using AJAX in batch');

	if (!p) return;
	var $p = $(p),
		$syns = $(),
		$count = $('<span>'),
		$txt = $p.find('a');

	$txt = $txt.length ? $txt : $p;

	var __updateCount = function () {
		$count.text('(' + $syns.filter('.syn-to-create').length + ')');
	};

	var __create = function () {
		var mwa = new mw.Api();
		// At the time of writing this mw.Api.edit was broken 
		// (no callback because it did not return the promise object 
		// nor did it forward the callback functions properly)

		var _post = function () {
			if (!$syns.filter('.syn-to-create').length) return $txt.text("Done!");
			var $el = $syns.filter('.syn-to-create').eq(0),
				title = $el.text();

			__updateCount();
			mwa.postWithEditToken({
				action: 'edit',
				createonly: true,
				text: '#REDIRECT [[' + mw.config.get('wgPageName').replace(/_/g, ' ') + ']]',
				title: title,
				watchlist: 'nochange'
			}).done(function () {
				$el.removeClass('syn-to-create').addClass('syn-created');
				_post();
			}).fail(function () {
				$el.removeClass('syn-to-create').addClass('syn-create-err');
				_post();
			});
		};
		_post();
	};
	var __highlight = function () {
		var _textsWeHave = new Set();
		var _highlightLi = function (i, el) {
			var $nodes = $(el.childNodes), $syn, t;

			if ($nodes.eq(0).is('i') && $nodes.eq(2).is('i') && !$nodes.eq(1).is('*')) {
				$syn = $nodes.slice(0, 3);
			} else {
				$syn = $nodes.eq(0);
				if ($syn.is('*') && !$syn.is('i')) return;
			}
			t = $syn.text().trim();

			if (!/^[A-Z][ .A-Za-z]*[a-z]$/.test(t) || _textsWeHave.has(t)) return;
			_textsWeHave.add(t);

			if ($syn.length > 1) {
				$syn = $syn.wrapAll('<span>').parent();
			} else if (!$syn.is('*')) {
				var $span = $('<span>').text(t);
				$syn.replaceWith($span, ' ');
				$syn = $span;
			}
			$syns = $syns.add($syn.addClass('syn-to-create syn-x'));
		};
		_textsWeHave.add(mw.config.get('wgPageName').replace(/_/g, ' '));

		$('.mw-heading3').has('#' + $.escapeSelector(mw.msg('synonyms').replace(/ /g, '_')) + ',#' + $.escapeSelector(mw.msg('synonymy').replace(/ /g, '_'))).nextUntil('.mw-heading3,.mw-heading2,hr').find('li').each(_highlightLi);

		$syns.click(function () {
			$(this).toggleClass('syn-to-create');
			__updateCount();
		});
		__updateCount();
		$txt.text("Create Redirs: ").append($count);
		$p.unbind('click').click(function (e) {
			e.preventDefault();
			$p.unbind('click').addClass('ui-state-disabled');
			$.createSpinner().prependTo($txt);
			__create($syns);
		});
	};

	$p.click(function (e) {
		e.preventDefault();
		$p.unbind('click');
		mw.loader.using(['jquery.spinner', 'mediawiki.util', 'mediawiki.api'], function () {
			new mw.Api().loadMessagesIfMissing(['synonyms', 'synonymy']).then(__highlight);
		});
	});
}(jQuery, mediaWiki));