User:Ricky Lau/fixWikiCore.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/*<nowiki>
	fixWikiCore.js - fixWiki core functions
	Author: Ricky Lau (RickyLauKO [at] GMail [dot] Com)
	Last modified: 2009-06-01T13:35Z

	This file should not be edited unless you know what these syntax exactly do
*/

// fiwProjectPageId - WikiProject Check Wikipedia page ID (wgArticleId);
var fiwProjectPageId = ['1026711','1050057'];

/*if (wgPageName == 'User:' + wgUserName.replace (' ', '_') + '/fixWiki.js' ) {
	$('firstHeading').update ('FixWiki');
	var siteSub = $('siteSub');
	var subpages = $('subpages');
	var jump_to_nav = $('jump-to-nav');
	with ($('bodyContent')) {
		update ();
		insert (siteSub);
		insert (subpages);
		insert (jump_to_nav);
		
	}
}*/

// fiwAutofix - Autofix
// usage - Void fiwAutofix (Boolean Manual_start) [call automatically on edit page load except fiwPromptBeforeFix set to false]
var fiwAutofixed = [];
var fiwAFSummaries = [];
function fiwAutofix (manual) {
	fiwAutofixed.clear ();
	fiwAFSummaries.clear ();
	fiwAutofixes.each (function (rule) {
		var matches = $F('wpTextbox1').match (rule.find);
		if (matches == null) { return; }
		// If false positives are found, specify check and valid rule to reduce false positives
		var errors = [];
		if (!(Object.isUndefined (rule.check) || Object.isUndefined (rule.valid))) {
			matches.each (function (text) {
				var check = text.match (rule.check);
				var valid = text.match (rule.valid);
				if (valid == null || valid.length != check.length) { errors.push (text); }
			});
		} else {
			errors = matches;
		}
		if (errors.length == 0) { return; }
		var promptFix = fiwAutofixSetting ('PromptBeforeFix') == 2 || (fiwAutofixSetting ('PromptBeforeFix') == 1 && rule.trusted !== true)
		var doFix = true;
		if (promptFix) {
			doFix = confirm ('Finding ' + rule.description + ' (' + rule.find + ')...\n\nFound ' + errors.length + ' errors');
		}
		if (!doFix) { return; }
		for (var i = errors.length - 1; i >= 0; i--) {
			var original = $F('wpTextbox1').match (rule.find) [i];
			var changed = original.replace (rule.pattern, rule.replace);
			if (original === changed) { continue; }
			if (Object.isUndefined (fiwAutofixed.find (function (el) { return el.ori == original; }))) {
				if (promptFix) {
					changed = window.showModalDialog (fiwAutofixSetting ('PromptURL'), {
						replaceWith: fiwText.replaceWith (original),
						replacement: changed,
						ok: fiwText.ok,
						cancel: fiwText.cancel
					}, 'center: yes; dialogwidth: 310; dialogheight: 160; resizable: no; scroll: yes');
				}
				fiwAutofixed.push ({ ori: original, rep: changed });
			} else {
				changed = fiwAutofixed.find (function (el) { return el.ori == original; }).rep;
			}
			if (original === changed) { continue; }
			if (changed == null) { continue; }
			$('wpTextbox1').value = $F('wpTextbox1').replace (original, changed);
			fiwAFSummaries.push (rule.description);
		}
	});
	if (fiwAFSummaries.length > 0) {
		fiwAFSummaries = fiwAFSummaries.uniq ().join (', ');
		addSummary (fiwSetting ('summaryPrefix') + fiwAFSummaries + fiwSetting ('summarySuffix'));
		switch (fiwAutofixSetting ('Submit')) {
		case 0: commitChange (); break;
		case 1: previewChange (); break;
		case 2: showDifferences (); break;
		}
	}
	if (manual) { alert (fiwText.afDone (fiwAFSummaries.length)); }
}

// fiwAutofixSetting - Alias of fiwSetting ('autofix*****'), plus checks values' validity
// usage - Mixed fiwAutofixSetting (id)
function fiwAutofixSetting (id) {
	var value = fiwSetting ('autofix' + id);
	var valid = [];
	switch (id.toLowerCase ()) {
	case 'enabled':
		valid = [true, false];
		break;
	case 'onedit':
	case 'promptbeforefix':
		valid = [2, 1, 0];
		break;
	case 'submit':
		valid = [false, 0, 1, 2];
		break;
	}
	return (valid.include (value) || valid.length == 0)?value:valid.first ();
}

// fiwMain - Main function of fixWiki
// usage - Void fiwMain () [call automatically on load]
function fiwMain () {
	// Check if everything is ok
	if (!(typeof fiwLangOK == 'function' && fiwLangOK () == true)) {
		alert ('fixWiki language file is missing.\n\nPlease go to User:' + fiwAuthor + '/fixWiki for installation instructions.');
		return;
	}
	if (!(typeof commToolsOK == 'function' && commToolsOK () == true)) {
		alert (fiwText.commToolsMissing); return;
	}
	if (typeof Prototype == 'undefined') { alert (fiwText.prototypeMissing); return; }
	if (!(typeof fiwSettingsOK == 'function' && fiwSettingsOK () == true)) { alert (fiwText.fiwSettingsMissing); return; }
	if (!(typeof fiwAutofixes == 'object' && fiwAutofixes.constructor == Array)) { alert (fiwText.fiwAFMissing); return; }
	// Every autofix rule has find pattern with g flag?
	var stop = false
	fiwAutofixes.each (function (rule) {
		if (!rule.find.global) {
			stop = !confirm (fiwText.nonGlobal (rule));
			if (stop) { throw $break; }
		}
	});
	if (stop) { return; }
	// Call fiwProjectPageLinkSubst?
	if (fiwProjectPageId.include (wgArticleId)) { fiwProjectPageLinkSubst (); }
	// Call fiwSearch? - If GET parameter 'fiwSearch' is set
	if (isEditing (true) && $_GET ('fiwSearch') != null) { fiwSearch ($_GET ('fiwSearch')); }
	// Call fiwAutofix? - Main namespace (0) edit page only if autofix is enabled
	if (isEditing () && wgNamespaceNumber == 0 && fiwAutofixSetting ('Enabled')) {
		$('wpDiff').insert ({ after: ' | ' });
		var autofixLink = new Element ('a', { href: '#', onclick: 'fiwAutofix (true);' });
		autofixLink.update (fiwText.autoFix);
		$('wpDiff').insert ({ after: autofixLink });
		if (isEditing (true)) {
			switch (fiwAutofixSetting ('OnEdit')) {
			case 1:
				if (!confirm (fiwText.startAutofix)) { break; }
			case 2:
				fiwAutofix ();
				break;
			}
		}
	}
}

// fiwProjectPageLinkSubst - Substitutes links in project page so that edit page will load upon clicking link
// usage: Void fiwProjectPageLinkSubst () [call automatically on project page load]
function fiwProjectPageLinkSubst () {
	$$('table.wikitable:not(.sortable) a').each (function (el){ 
		var articleName = el.href.replace (/\/wiki\//, '').replace (wgServer, '');
		el.href = '/w/index.php?title=' + articleName + '&action=edit&fiwSearch=' + encodeURIComponent (htmlEntityDecode (el.parentNode.next ().innerHTML).truncate (20, ''));
		el.target = '_blank';
	});
}

// fiwSearch - Searches text in edit box with search value, and highlights search value.
// usage: Void fiwSearch (search_value) [call automatically on edit page load with GET fiwSearch parameter]
function fiwSearch (search) {
	$ ('wpTextbox1').focus ();
	if (Prototype.Browser.IE) {
		var txt = $ ('wpTextbox1').createTextRange ();
		if (txt != null) {
			txt.collapse (true);
			txt.collapse(false);
			var matches = txt.findText (search);
			if (!matches) { alert (fiwText.notFound (search)); } else { txt.select(); }
		}
	} else if ($ ('wpTextbox1').setSelectionRange) {
		var matches = $F ('wpTextbox1').match (regexpEscape (search));
		if (matches == null) { alert (fiwText.notFound (search)); } else {
			var start = $F ('wpTextbox1').search (regexpEscape (search));
			var end = start + search.length;
			$ ('wpTextbox1').setSelectionRange (start, end);
		}
	} else {
		return;
	}
	$ ('wpTextbox1').focus ();
}

// fiwSetting - fixWiki settings
// usage: Mixed fiwSetting (id)
function fiwSetting (id) {
	if (fiwSettings.find (function (entry) { return entry.id == id }) == null) { return null; }
	return fiwSettings.find (function (entry) { return entry.id == id }).value;
}

// On load run fiwMain once.
$ (fiwMain);

// fiwCoreOK - just check if fixWiki core file is loaded correctly
// usage: True fiwCoreOK ()
function fiwCoreOK () { return true; }

/*
	This is the end of fixWikiCore.js
</nowiki>*/