מדיה ויקי:סקריפטים/55.js

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
// סקריפט 55. מבצע את ההחלפות של בוט החלפות על התוכן של תיבת העריכה. נכתב בידי [[משתמש:קיפודנחש]]

jsb_main = {
	regexes: [],
	badlines: [],
	textbox: null,
	build_regexes: function(event, data) {
		this.start = new Date();
		var t = $('#wpTextbox1');
		this.textbox = t.length ? t[0] : null;
		if (!this.textbox || this.textbox.value.length === 0 || /\{\{\s*ללא_בוט\s*\}\}/.test(this.textbox.value)) {
			mw.util.jsMessage('הדף מכיל תבנית "ללא בוט" ולכן לא יבוצעו החלפות');
			return;
		}
		if (data) {
			var lines = data.split(/\n/);
			var clear_nowiki = /\|<nowiki>(.*)<\/nowiki>/;
			var matches;
			var regex;
			while (lines.length) {
				if (! (matches = lines.shift().match(/^\|(\d+)/)))
					continue;
				var num = parseInt(matches[1], 10);
				if (! (matches = lines.shift().match(clear_nowiki)))
					continue;
				try {
					regex = new RegExp(matches[1], 'g');
				} catch(e) {
					this.badlines.push(num);
					continue;
				}
				if (! (matches = lines.shift().match(clear_nowiki)))
					continue;
				this.regexes[num] = [regex, matches[1]];
			}
			this.process_page();
		}
		else 
			$.ajax({
				url: mw.util.getUrl(window.replaceListPage || 'ויקיפדיה:בוט/בוט החלפות/רשימת החלפות נוכחית',  { action: 'raw', ctype: 'text/x-wiki'} ),
						// above is: ""
						// which all browsers except IE can work with.
				success: function(data, status){
					jsb_main.build_regexes(null, data);
				}
			});
	},
	
	process_page: function() {
		var t = this.textbox.value,
			skip_dict = {},
			skip_ar = [],
			actual_replaced = [],
			skipmatch = t.match(/{{ללא[_ ]בוט\|\s*(\d+)\s*}}/g);
		if (skipmatch) 
			for (var i = 0; i < skipmatch.length; i++) {
				var matches = skipmatch[i].match(/{{ללא[_ ]בוט\|\s*(\d+)\s*}}/);
				skip_dict[parseInt(matches[1], 10)] = true;
				skip_ar.push(matches[1]);
			}
		for (var i1 in this.regexes) 
			if (! skip_dict[i1] && ! isNaN(i1))
				if (this.regexes[i1][0].test(t)) {
					actual_replaced.push(t.match(this.regexes[i1][0]) + '=>' + this.regexes[i1][1].replace(/\$\d*/g, ''));
					t = t.replace(this.regexes[i1][0], this.regexes[i1][1]);
				}
		this.textbox.value = t;
		var msg = ['‏ריצת הסקריפט הסתיימה. אנא בצעו "הצגת שינויים" לפני שמירה, כדי לוודא שהסקריפט לא גרם נזק.‏'];
		if (skip_ar.length)
			msg.push('‏החלפות שלא התבצעו בגלל תבנית "ללא בוט": ‏' + skip_ar.join(', '));
		if (this.badlines.length)
			msg.push('‏החלפות שלא התבצעו בגלל בעיה בקוד:‏ ' + this.badlines.join(', ‏'));
		msg.push('');
		msg.push(actual_replaced.length 
			? '‏התבצעו ההחלפות הבאות: ‏' + actual_replaced.join('‏ ,‏')
			: '‏לא התבצעו החלפות - הדף "נקי".‏');
		msg.push('‏הריצה ארכה ' + (new Date() - this.start) + ' מילישניות.‏');
		mw.util.jsMessage(msg.join('<br />'));
		if (actual_replaced.length && $('#wpSummary').val() === '')
			$('#wpSummary').val('סקריפט החלפות - (' + actual_replaced.join('‏ ,‏') + ') ');
	},
	
	init: function() {
		$('#ca-edit').after($('<li>').append($('<span>').append($('<a>', {href: '#', text: 'בוט החלפות'}).click(function(){jsb_main.build_regexes();}))));
	}
};

if ($.inArray(mw.util.getParamValue('action'), ['edit', 'submit']) + 1) 
	$(document).ready(jsb_main.init);