Benutzer Diskussion:CennoxX/sandbox.js

Letzter Kommentar: vor 11 Jahren von Rillke

Wenn Du nur die Links haben willst (über < nowiki >s und Kommentare sollte man sich u.U. noch Gedanken machen), kannst Du einfach einen RegExp auf den Artikeltext loslassen.

Der folgende Code fügt einen Link zum Werkzeugkasten hinzu.

/*!  Copyright 2013 by Rainer Rillke
 *   This software is released under the terms and conditions of the 
 *   GPL v.3 license, CC-By-SA 3.0 and GFDL
 *   Choose the license(s) you like best.
 !*/
 
/** This script will iterate over one page's wikilinks and 
 ** replace them with the local equivalents in edit mode.
 ** It is of special interest for imported articles.
 **/ 
 
/*global jQuery:false, mediaWiki:false */
/*jshint curly:false*/
 
 (function ($, mw) {
 'use strict';
 
	/// [General Helper functions]
	var getObjLen = function (obj) {
		var i = 0;
		for (var elem in obj) {
			if (obj.hasOwnProperty(elem)) i++;
		}
		return i;
	};
	function firstItemName(o) {
		for (var i in o) {
			if (o.hasOwnProperty(i)) {
				return i;
			}
		}
	}
	/// End [General Helper functions]

	var ignoreRE = /^File:/i,
		$p, $a, $txt = $('<span>'),
		currentLink, execing, $tb1 = $('#wpTextbox1'),
		fails = 0;

	if (!$tb1.length) return;

	var exec = function () {
		// Indicate that we're busy
		execing = true;

		$txt.text("Parsing page.");
		$p.text('').append($txt).prepend($.createSpinner());

		// Fetch all link targets
		var linkTargets = $tb1.val().match(/\[\[.+?(?:\#.+?)?(?:\|.+?)?\]\]/g);
		// Avoid querying the same title twice
		var linkMap = {};

		$txt.text("Listing links to process.");
		$.each(linkTargets, function (i, target) {
			target = target.match(/\[\[(.+?)(?:\#.+?)?(?:\|.+?)?\]\]/)[1];
			if (!target) return;
			if (ignoreRE.test(target)) return;
			linkMap[$.ucFirst(target.replace(/_/g, ' '))] = 1;
		});

		var createLinkRegExp = function (target) {
			var first    = target.charAt(0).toUpperCase(),
				escfirst = $.escapeRE(first),
				lcfirst  = first.toLowerCase();
				
			if (escfirst === first) {
				if (first !== lcfirst) {
					first = '[' + first + '|' + lcfirst + ']';
				}
			} else {
				first = escfirst;
			}
			return new RegExp('(\\[\\[)' + first + $.escapeRE(target.slice(1)).replace(/ /g, '[ _]') + '(?:\\#.+?)?((?:\\|.+?)?\\]\\])', 'g');
		};

		// Query the API step by step (recursively)
		var doQuery = function () {
			var remaining = getObjLen(linkMap);
			currentLink = firstItemName(linkMap);

			// Completely done?
			if (!currentLink) {
				// Remove the inner text and the spinner
				$p.text("Done.");
				return;
			}
			$txt.text("Doing " + currentLink + ". Remaining: " + remaining);

			var a = new mw.Api();
			a.get({
				'format': 'json',
				'action': 'query',
				'list': 'langbacklinks',
				'lbllang': 'en', // TODO: Add a dialog asking for the language
				'lbltitle': currentLink
			}).done(function (r) {
				delete linkMap[currentLink];
				var re = createLinkRegExp(currentLink);
				
				// Never assume you'll always get the response you expect
				if (r && r.query.langbacklinks && r.query.langbacklinks.length) {
					$tb1.val($tb1.val().replace(re, '$1' + r.query.langbacklinks[0].title + '$2'));
					doQuery();
				} else {
					$tb1.val($tb1.val().replace(re, '$1!!!NOLANGLINKFOUND!!!' + currentLink + '!!!$2'));
					doQuery();
				}
			}).fail(function () {
				delete linkMap[currentLink];
				fails++;
				if (fails < 2) {
					doQuery();
				} else {
					$p.text("Server error.");
				}
			});
		};
		doQuery();
	};

	// Add a portlet link
	mw.loader.using(['mediawiki.util', 'mediawiki.api', 'jquery.spinner'], function () {
		// TODO: Add namespace and action conditions

		// Add a "launch-link" to the tools
		$p = $(mw.util.addPortletLink('p-tb', '#changeEnLinksToDeLinks', "Replace en links", 't-enlinkreplace')),
		$a = $p.find('a');

		if ($a.length) $p = $a;
		$p.click(function (e) {
			e.preventDefault();
			$p.addClass('ui-state-disabled');
			if (!execing) exec();
		});
	});

}(jQuery, mediaWiki));

-- RE rillke fragen? 00:56, 15. Apr. 2013 (CEST)Beantworten