User:Nux/wp sk.js

Source: Wikipedia, the free encyclopedia.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* ------------------------------------------------------------------------ *\
    Code clean-up tool
	
  Description (Polish):
                http://pl.wikipedia.org/wiki/WP:SK
  Description (English):
                http://en.wikipedia.org/wiki/WP:NUX_CC
	
    Copyright:  ©2007-2010 Maciej Jaros (pl:User:Nux, de:User:EcceNux, en:User:Nux)
      Licence:  GNU General Public License v2
                http://opensource.org/licenses/gpl-license.php

	Special thanks to:
	* Wikipedysta:ABach - za zebranie i opracowanie długiej listy elementów do sprzątania
	* Wikipedysta:Malarz pl - za garść kolejnych elementów do sprzątania
	* Wikipedysta:BartekChom - za pomysły i gotowe wyrażenia regularne
	* Wikipedysta:Gregul - za garść wyrażeń regularnych
	* Wikipedysta:PMG - za wytrwałe i szczegółowe testowanie
	* Wikipedysta:ToSter - za testy i pomysły na nowe rozwiązania
\* ------------------------------------------------------------------------ */
//  version:
	var tmp_VERSION = '2.3.5en-beta4';  // = wp_sk.version = wp_sk.ver
// ------------------------------------------------------------------------ //

//
// Special modules
//
if ((typeof nuxedtoolkit)!='object')
{
	document.write('<'
	+'script type="text/javascript" src="'
	+'http://pl.wikipedia.org/w/index.php?title=MediaWiki:Nuxedtoolkit.js'
	+'&action=raw&ctype=text/javascript&dontcountme=s&ver106'
	+'"><'
	+'/script>');
}

/* =====================================================
	Object Init
   ===================================================== */
if (wp_sk!=undefined)
{
	alert('Critical error - name conflict!\n\nOne of the scripts uses wp_sk as a global variable.');
}
var wp_sk = new Object();
wp_sk.ver = wp_sk.version = tmp_VERSION;

/* =====================================================
	Lang array / object
   ===================================================== */
wp_sk.lang = {
	'Long name' : 'Code cleanup',
	'Short name' : 'WP:NUX_CC',
	'Info link' : '[[WP:NUX_CC]]',

	'ver.' : 'ver.',
	'Please review changes!' : 'Please review changes!',
	'Redirs clenup - preparation' : 'Redirs clenup - preparation'
}

/* =====================================================
	Function: wp_sk.debug(htxt)

	Show html debug message if debug is active
   ===================================================== */
wp_sk.debug = function (htxt)
{ 
	if (typeof wp_sk_debug_enabled!='undefined' && wp_sk_debug_enabled && typeof nux_debug=='function')
	{
		nux_debug(htxt);
	}
}

/* =====================================================
	Function: wp_sk.button()
	
	Add wp_sk button
   ===================================================== */
wp_sk.button = function ()
{
	// prepare
	nuxedtoolkit.prepare();
	wp_sk.is_old_toolbar = nuxedtoolkit.is_old_toolbar;
	// grup
	wp_sk.btns = nuxedtoolkit.addGroup();
	// btn def.
	var btn_attrs = {
		title : wp_sk.lang['Long name'] +' ('+ wp_sk.lang['ver.'] +' '+wp_sk.ver+')',
		alt : wp_sk.lang['Short name'],
		id : "wp_sk_img_btn"
	}
	var icons = {
		// with blue background
		oldbar : 'http://upload.wikimedia.org/wikipedia/commons/2/2e/Button_broom.png',
		// plain no background, SVG advised
		newbar : 'http://commons.wikimedia.org/w/thumb.php?f=Broom%20icon.svg&width=22px'
	}
	nuxedtoolkit.addBtn(wp_sk.btns, 'wp_sk.cleanup(document.getElementById(\'wpTextbox1\'))', icons, btn_attrs)
}

/* =====================================================
	Function: wp_sk.warning(input)
	
	Add warning and add function to clear it
	after pressing the diff button.
   ===================================================== */
wp_sk.warning = function (input)
{
	var el=document.getElementById('wpSummary');
	if (wp_sk.nochanges)
	{
		el.style.border='2px solid #696';	// just colors if there were no changes
	}
	else
	{
		el.style.border='';
		if (el.value!='')
			el.value+=', ';
		el.value+=wp_sk.lang['Please review changes!'];
		el.className = 'warning';

		el=document.getElementById('wpDiff');
		el.className = 'warning';
		el.onclick=function()
		{
			var el=document.getElementById('wpSummary');
			el.value=el.value.replace(wp_sk.lang['Please review changes!'], wp_sk.lang['Info link']);
		}
	}
}

/* =====================================================
	Function: wp_sk.cleanup(input)
	
	Main cleanup function that calls the cleaner
   ===================================================== */
wp_sk.cleanup = function (input)
{
	var isPartSelected = false;
	if (input.selectionStart != undefined)
	{
		var sel_s = input.selectionStart;
		var sel_e = input.selectionEnd;
		if (sel_s!=sel_e)
		{
			var str = input.value.substring(sel_s, sel_e);
			isPartSelected = true;
		}
	}
	// IE...
	else if (document.selection)
	{
		var range = document.selection.createRange();
		if (range.parentElement()==input && range.text!='')
		{
			var str = range.text;
			isPartSelected = true;
		}
	}

	if (!isPartSelected)
	{
		var str = input.value;
	}
	// OMG - IE & Opera fix
	str = str.replace(/\r\n/g, '\n');

	//
	// Call main cleaner
	//
	str = str.replace(/\n+$/,''); // without last empty lines
	var str_pre = str;
	str = wp_sk.cleaner(str);
	wp_sk.nochanges = (str==str_pre);

	//
	// Save changes
	//
	if (!wp_sk.nochanges)
	{
		if (!isPartSelected)
		{
			input.value = str;
		}
		else if (input.selectionStart!=undefined)
		{
			input.value = input.value.substring(0, sel_s) + str + input.value.substring(sel_e)
		}
		// IE...
		else if (document.selection)
		{
			range.text = str;
			range.scrollIntoView(false);// at bottom
		}
	}

	input.focus();

	wp_sk.warning();
}

/* =====================================================
	Function: wp_sk.cleaner(str)

	Main cleaner
	Calls more specific clener functions.
	Returns cleaned string.
   ===================================================== */
wp_sk.cleaner = function (str)
{
	//
	// hide contents of tags like: nowiki, pre, source i math
	str = wp_sk.nowiki.hide(str);

	//
	// basic cleanup
	str = wp_sk.cleanerLinks(str);		// wikilinks
	str = wp_sk.cleanerTpls(str);		// templates
	str = wp_sk.cleanerWikiVaria(str);	// other wikicode related
	
	str = wp_sk.cleanerTXT(str);		// text cleanup
	
	//
	// interwiki
	str = wp_sk.cleanerMagicLinks(str);

	//
	// show contents of hiden tags
	str = wp_sk.nowiki.show(str);
	
	return str;
}

/* =====================================================
	Function: wp_sk.cleanerLinks(str)

	Wikilinks cleanup
   ===================================================== */
wp_sk.cleanerLinks = function (str)
{
	// [[Kto%C5%9B_jaki%C5%9B#co.C5.9B|...]]→[[Ktoś jakiś#coś|...]]
	str = str.replace(/\[\[([^|#\]]*)([^|\]]*)(\||\]\])/g, wp_sk.rLinkdecode);

	// fix namespaces and minor related
	str = str.replace(/\[\[(:?) *([Ii]mage|[Ff]ile) *: *([^ ])/g,	function (a,dw,co,l1) {return '[['+dw+'File:'+l1.toUpperCase();} );
	str = str.replace(/\[\[(:?) *([Cc]ategory) *: *([^ ])/g, function (a,dw,co,l1) {return '[['+dw+'Category:'+l1.toUpperCase();} );
	str = str.replace(/\[\[ *(:?) *([Tt]emplate) *: *([^ ])/g, function (a,dw,co,l1) {return '[[Template:'+l1.toUpperCase();} );
	str = str.replace(/\[\[ *(:?) *([Ss]pecial) *: *([^ ])/g, function (a,dw,co,l1) {return '[[Special:'+l1.toUpperCase();} );

	str = str.replace(/\[\[ *:? *[Tt]alk( [a-z]*) *: */g, '[[Talk$1:');

	// dot
	str = str.replace(/(\[\[File:[^\|\]]+\|[^\|\]]+)\.\]\]/, '$1]]');
	// -mid spacje
	/* // zawiesza FF w niektórych warunkach, psuje niektóre opisy
	str = str.replace(/(\[\[File:[^\|\[\]]+)(\|[^\[\]\{\}]+ [^\[\]\{\}]*)(\|([^\|\[\]]+|[^\|\[\]]+\[\[[^\[\]]+\]\]){7,}\]\])/g, function(a,g1,gmid,gn)
	{
		return g1+ gmid.replace(/\s/g,'') +gn;
	});
	*/

	// remove [[:en:
	str = str.replace(/\[\[ *:? *en *: */g, '[[');

	// [[Wikilink|wikilink]] > [[wikilink]]
	str = str.replace(/\[\[([^|\]])([^|\]]*)\|([^\]])\2\]\]/g, function (a, w1_1, w_rest, w2_1)
		{
			return (w1_1.toUpperCase()==w2_1.toUpperCase()) ? '[['+w2_1+w_rest+']]' : a;
		}
	);
	
	// (un)wind wikilinks
	str = str.replace(/\[\[([^|\]]*)\|\1([a-zA-ZÄÖÜäößü]*)\]\]/g, '[[$1]]$2');
	str = str.replace(/\[\[([^|\]]+)\|([^|\]]+)\]\]([a-zA-ZÄÖÜäößü]+)/g, '[[$1|$2$3]]');

	// unwanted space in wikilinks
	str = str.replace(/\[\[ *([^\]\|:]*[^\]\| ]) *\|/g, '[[$1|');
	str = str.replace(/([^ \t\n])\[\[ +/g, '$1 [[');
	str = str.replace(/\[\[ +/g, '[[');
	str = str.replace(/([^ \t\n])\[\[([^\]\|:]+)\| +/g, '$1 [[$2|');
	str = str.replace(/\[\[([^\]\|:]+)\| +/g, '[[$1|');
	str = str.replace(/([^ \|]) +\]\]([^ \t\na-zA-ZÄÖÜäößü])/g, '$1]] $2');
	str = str.replace(/([^ \|]) +\]\]([^a-zA-ZÄÖÜäößü])/g, '$1]]$2');


	return str;
}
/* =====================================================
	Function: wp_sk.cleanerTpls(str)

	Templates cleanup
   ===================================================== */
wp_sk.cleanerTpls = function (str)
{
	// unneeded namespace
	str = str.replace(/\{\{ *([Tt]emplate|msg) *: */g, '{{');

	// lang correction
	str = str.replace(/\{\{[lL]ang\|cz\}\}/g, '{{lang|cs}}');
	str = str.replace(/\{\{[lL]ang\|dk\}\}/g, '{{lang|da}}');
	str = str.replace(/\{\{[lL]ang\|nb\}\}/g, '{{lang|no}}');
	/*
	str = str.replace(/(\{\{lang\|[a-z-]+\}\}[\t ]*){2,10}/g, function(a) {
		return '{{multilang'+a.replace(/\{\{lang\|([a-z-]+)\}\}\s*//*g, '|$1')+'}}';
	});
	*/

	// making infoboxes more readable
	// str = str.replace(/\{\{([^|}]+ infobo[^|}]+)((?:[^{}]+|\{\{(?:[^{}]+|\{\{[^{}]+\}\})+\}\})+)\}\}/g, wp_sk.rFriendlyIbox);
			

	return str;
}
/* =====================================================
	Function: wp_sk.cleanerWikiVaria(str)

	Cleanup of other wikisyntax related elements
   ===================================================== */
wp_sk.cleanerWikiVaria = function (str)
{

	// unified headers
//	str = str.replace(/[ \n\t]*\n'''? *(Siehe|Sehen) (auch|also):* *'''?[ \n\t]*/gi, '\n\n== Siehe auch ==\n');
//	str = str.replace(/[ \n\t]*\n(=+) *(Siehe|Sehen) (auch|also):* *=+[ \n\t]*/gi, '\n\n$1 Siehe auch $1\n');
//	str = str.replace(/[ \n\t]*\n'''? *(Web[ ]?links):* *'''?[ \n\t]*/gi, '\n\n== Weblinks ==\n');
//	str = str.replace(/[ \n\t]*\n(=+) *(Web[ ]?links):* *=+[ \n\t]*/gi, '\n\n$1 Weblinks $1\n');

	// headers
	str = str.replace(/(^|\n)(=+) *([^=\n]*[^ :=\n])[ :]*=/g, '$1$2 $3 ='); // =a= > = a =, =a:= > = a =
	str = str.replace(/(^|\n)(=+[^=\n]+=+)[\n]{2,}/g, '$1$2\n');	// jeden \n

	// add spaces in lists
	str = str.replace(/(\n[#*:;]+)([^ \t\n#*:;{])/g, '$1 $2');

	// unwind external links in lists
	str = str.replace(/\n\*[ \t]*\[(http:\/\/[^ \n\]]+)\]/g, "\n*[$1 $1]");

	return str;
}
/* =====================================================
	Function: wp_sk.cleanerTXT(str)

	Cleanup not related to wikisyntax
   ===================================================== */
wp_sk.cleanerTXT = function (str)
{
	// various corrections (order of commands might be important!)
	str = str.replace(/<\/?br ?\/?>/gi, '<br />');
	str = str.replace(/<sup>2<\/sup>/g, '²');
	str = str.replace(/<sup>3<\/sup>/g, '³');
	str = str.replace(/<sup>o<\/sup>/g, '°');
	str = str.replace(/([0-9]) (%|‰|°)/g, '$1$2');

	str = str.replace(/&deg;/g, '°');
	str = str.replace(/&sum;/g, '∑');
	str = str.replace(/&larr;/g, '←');
	str = str.replace(/&rarr;/g, '→');
	str = str.replace(/&uarr;/g, '↑');
	str = str.replace(/&darr;/g, '↓');
	str = str.replace(/&dagger;/g, '†');

	return str;
}
/* =====================================================
	Function: wp_sk.cleanerMagicLinks(str)

	Cleanup of elements related to magic links like:
	categories, interwiki and featured articles.
   ===================================================== */
wp_sk.cleanerMagicLinks = function (str)
{
	// gather
	str = wp_sk.cat.gather(str);
	str = wp_sk.iWiki.gather(str);
	str = wp_sk.iWikiFA.gather(str);

	// remove empty lines that were left after gathering magic links (or after any other cleanup actions)
	str = str.replace(/[\n]{3,}/g, '\n\n');

	// output at the end of the article
	str = str.replace(/\s*$/, wp_sk.cat.output);
	str = str.replace(/\s*$/, wp_sk.iWikiFA.output);
	str = str.replace(/\s*$/, wp_sk.iWiki.output);

	return str;
}

/* =====================================================
	Functions supporting cleanup                 {START}
   ----------------------------------------------------- */
//
// Infobox cleanup
//
wp_sk.rFriendlyIbox = function (a,nazwa,zaw)
{
	if (zaw.indexOf('<!--')!=-1 || zaw.indexOf('=')==-1 || zaw.indexOf('\n')==-1)
	{
		return a;
	}
	nazwa = nazwa.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");	// trim

	//
	// escape params
	//
	// inner templates
	zaw = zaw.replace(/<<<(#+)>>>/g,'<<<#$1>>>').replace(/\{\{[^}]+\}\}/g,function(a){ return a.replace(/\|/g,'<<<#>>>') });
	// inner links
	zaw = zaw.replace(/\[\[[^\]]+\]\]/g,function(a){ return a.replace(/\|/g,'<<<#>>>') });
	
	//
	// cleanup
	//
	// del empty
	zaw = zaw.replace(/\|\s*(?=\|)/g, function(a) {return (a.indexOf('\n')==-1)?'':'\n'}).replace(/\|\s*$/g, "");
	zaw = zaw.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");	// trim
	// move | at the beginning of the line
	zaw = '\n'+zaw+'\n';
	zaw = zaw.replace(/\s*\|(\s*)/g, function(a, post)
	{
		if (a.indexOf('\n')==-1)
		{
			return a;
		}
		else if (post.indexOf('\n')==-1)
		{
			return '\n |'+post;
		}
		else
		{
			return '\n | ';
		}
	});
	
	//
	// unescape
	zaw = zaw.replace(/<<<#>>>/g,'|').replace(/<<<#(#+)>>>/g,'<<<$1>>>');
 
	//
	// Finish
	//
	return '{{'+nazwa.substring(0,1).toUpperCase()+nazwa.substring(1)+zaw+'}}';
}
//
// Decode links
//
wp_sk.rLinkdecode = function(a,name,anchor,end)
{
	try
	{
		name=decodeURIComponent(name)
		anchor=decodeURIComponent(anchor.replace(/\.([0-9A-F]{2})\.([0-9A-F]{2})/g,'%$1%$2'))
		a='[['+name+anchor+end;
	}
	catch(err){}
	
	return a.replace(/_/g,' ');
}
/* -----------------------------------------------------
	Functions supporting cleanup                   {END}
   ===================================================== */

/* =====================================================
	Classes supporting cleanup                   {START}
   ----------------------------------------------------- */
/* =====================================================
	Class: wp_sk.nowiki

	Show/hide contents of: nowiki, pre, source and math
	
	.hide(str)
		hide special tags along with their contents
	.show(str)
		show hidden tags and their respective contents
   ===================================================== */
//
// object init
//
wp_sk.nowiki = new Object();

//
// .hide(str)
//
wp_sk.nowiki.hide = function(str)
{
	//
	// pre-hiding escaping of strings that might be confused with real nowiki-like escape strings
	str = str.replace(/<<<(#*[0-9]+)>>>/g, '<<<#$1>>>');

	//
	// hiding
	var re = /<(nowiki|pre|source|math|includeonly|noinclude)(|[ \t\n][^>]*)>/g;
	var m;
	wp_sk.nowiki.tags = new Array();
	// until opening tag was found
	for (var t_i = 0; (m=re.exec(str))!=null; t_i++)
	{
		var start, end, re_end;
		
		start = m.index;

		// search for ending tag: </tag([ \t\n]*)>
		re_end = new RegExp("</"+m[1]+"([ \t\n]*)>", "g")
		m = re_end.exec(str.substring(re.lastIndex));
		end = (m==null) ? str.length : re.lastIndex+re_end.lastIndex;
		
		// add to the contents array
		wp_sk.nowiki.tags[t_i] = str.substring(start,end);
		
		// change found contents to: <<<index>>>
		str = str.substring(0,start)+"<<<"+t_i+">>>"+str.substring(end);
		
		// search from the beginning, as some characters were removed
		re.lastIndex = start;
	}
	
	return str;
}
//
// .show(str)
//
wp_sk.nowiki.show = function(str)
{
	// tags
	str = str.replace(/<<<([0-9]+)>>>/g, function (a, i)
	{
		return wp_sk.nowiki.tags[i];
	});
	// unescape nowiki-like content
	str = str.replace(/<<<(#*[0-9]+)>>>/g, '<<<#$1>>>');
	
	return str;
}

/* =====================================================
	Class: wp_sk.cat

	Gather, cleanup and output categories
	
	.gather(str)
		gather categories from str,
		returns new str without categories
	.output(a)
		output resorted categories
		parameter a is not important

	.getDefSort()
		return a regexp for defaultsort
	.newDefSort()
		searches for new (most popular) defaultsort

	.art_def_sort
		defaultsort found in the current articles
	.def_sort
		defaultsort choosen for the article

	.arr
		array for categories ('name|sort key')
	.arr_i
		helpufull index and a number of elements in arr
   ===================================================== */
// object init
wp_sk.cat = new Object();
//
// .gather(str)
//
wp_sk.cat.gather = function(str)
{
	//
	// gather and remove
	wp_sk.cat.arr = new Array();
	wp_sk.cat.arr_i = 0;
	wp_sk.cat.art_def_sort = '';
	str = str.replace(/\{\{DEFAULTSORT:([^\}]+)\}\}/g, function(a, ds){wp_sk.cat.art_def_sort=ds; return ''});
	str = str.replace(/\[\[Category:([^\]\[]+)\]\]/g, function(a, cat){wp_sk.cat.arr[wp_sk.cat.arr_i++]=cat; return ''});
	wp_sk.cat.def_sort = wp_sk.cat.art_def_sort;
	
	return str;
}
//
// .output(a)
//
wp_sk.cat.output = function (a)
{
	if (wp_sk.cat.arr_i==0)
	{
		return a;
	}
	var str = '\n';

	//
	// sorting categories if a function is available
/*
	if(String.prototype.localeCompare)
	{
		function fn_sort(a,b)
		{
			return a.localeCompare(b)
		}
		wp_sk.cat.arr.sort(fn_sort);
	}
*/

	//
	// setup regexp for defaultsort (acording to the old or new key)
	var reDefSort = wp_sk.cat.getDefSort();
	
	//
	// output categories
	if (reDefSort!="") // if there was (or was found) a defaultsort
	{
		str += '\n{{DEFAULTSORT:'+wp_sk.cat.def_sort+'}}';
		for (var i=0; i<wp_sk.cat.arr_i; i++)
		{
			// if there is no defaultsort and empty, then add default so we won't bust anything
			if (!wp_sk.cat.art_def_sort.length && wp_sk.cat.arr[i].indexOf('|')==-1)
			{
				str += '\n[[Category:'+wp_sk.cat.arr[i]+'|{{PAGENAME}}]]';
			}
			else
			{
				wp_sk.cat.arr[i] = wp_sk.cat.arr[i].replace(reDefSort,'');	// remove key
				str += '\n[[Category:'+wp_sk.cat.arr[i]+']]';
			}
		}
	}
	else
	{
		for (var i=0; i<wp_sk.cat.arr_i; i++)
		{
			str += '\n[[Category:'+wp_sk.cat.arr[i]+']]';
		}
	}

	return str;
}
//
// .getDefSort()
//
wp_sk.cat.getDefSort = function ()
{
	//
	// choose a default sorting key
	if (wp_sk.cat.art_def_sort.length)
	{
		wp_sk.cat.def_sort = wp_sk.cat.art_def_sort;
	}
	// search for new default key if there are more then 1 categories
	else if (wp_sk.cat.arr_i>1)
	{
		wp_sk.cat.def_sort = wp_sk.cat.newDefSort();
	}
	
	var reDefSort="";
	if (wp_sk.cat.def_sort!="")
	{
		// changing to regexp (so we can omit partial matches)
		reDefSort = wp_sk.cat.def_sort.replace(/([(){}\[\]\\|.*?$^])/g, '\\$1');
		reDefSort = new RegExp('\\|'+reDefSort+'$');
	}
	
	return reDefSort;
}
//
// .newDefSort()
//
wp_sk.cat.newDefSort = function ()
{
	var def_sort = '';

	//
	// check if any cat sorting is used
	var sort_i;
	for (sort_i=0; sort_i<wp_sk.cat.arr_i && wp_sk.cat.arr[sort_i].indexOf('|')<0; sort_i++);
	//
	// if any cat soroting is used - search for new key (based on popularity of the key)
	if (sort_i!=wp_sk.cat.arr_i)
	{
		var def_sort_num = 0;
		var def_sort_forbiden = ['!', ' ', '*', '+'];
		for (var i = sort_i; i<wp_sk.cat.arr_i; i++)	// starting from the first found
		{
			var j, tmp_def_sort, tmp_def_sort_re, tmp_def_sort_num;
			
			// moving to new candidate key
			for (j = i; j<wp_sk.cat.arr_i && wp_sk.cat.arr[j].indexOf('|')<0; j++);
			if (j==wp_sk.cat.arr_i)
				continue;
			
			tmp_def_sort = wp_sk.cat.arr[j].substr(wp_sk.cat.arr[j].indexOf('|')+1);
			if (def_sort == tmp_def_sort)	// already checked
			{
				continue;
			}
			// changing to regexp (so we can omit partial matches)
			tmp_def_sort_re = tmp_def_sort.replace(/([(){}\[\]\\|.*?$^])/g, '\\$1');	// escaping special regexp characters
			tmp_def_sort_re = new RegExp('\\|'+tmp_def_sort_re+'$');
			
			// counting
			var tmp_def_sort_num=1;
			for (j++; j<wp_sk.cat.arr_i; j++)
			{
				if (tmp_def_sort_re.test(wp_sk.cat.arr[j]))
				{
					tmp_def_sort_num++;
				}
			}

			// candidate = new?
			if (tmp_def_sort_num<2 || def_sort_num > tmp_def_sort_num)
			{
				continue;
			}
			if (tmp_def_sort_num*2>wp_sk.cat.arr_i || def_sort_forbiden.indexOf(tmp_def_sort)<0) //>50% || forbidden
			{
				def_sort_num = tmp_def_sort_num;
				def_sort = tmp_def_sort;
			}
		}
	}
	
	return def_sort;
}

/* =====================================================
	Class: wp_sk.iWiki

	Gather, cleanup and output interwiki
	
	.gather(str)
		gather interwiki from str,
		returns new str without interwiki
	.output(a)
		output resorted interwiki
		parameter a is not important
	.comp(a, b)
		compare a with b and return value suitable
		for sort() function

	.order
		table of language codes sorted as the interwiki
		should be sorted
		(not used as sorting by language codes)
	.arr
		array for interwiki ([language, article])
	.arr_i
		helpufull index and a number of elements in arr
   ===================================================== */
// object init
wp_sk.iWiki = new Object();
//
// .gather(str)
//
wp_sk.iWiki.gather = function(str)
{
	wp_sk.iWiki.arr = new Array();
	wp_sk.iWiki.arr_i = 0;
	str = str.replace(
		// wg: http://meta.wikimedia.org/wiki/List_of_Wikipedias
		/\[\[([a-z\-]{2,3}|simple|ru-sib|be-x-old|zh-yue|map-bms|zh-min-nan|nds-nl|bat-smg|zh-classical|fiu-vro|roa-rup|tokipona|cbk-zam|roa-tara):([^\]\|\[]+)\]\]/g,
		function (a, lang, art)
		{
			// wg: http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3/maintenance/interwiki.sql
			if (lang!='wp' && lang!='mw' && lang!='gej' && lang!='ppr' && lang!='rfc' && lang!='uea' && lang!='why')
			{
				wp_sk.iWiki.arr[wp_sk.iWiki.arr_i] = new Array(lang,art);
				wp_sk.iWiki.arr_i++;
				return '';
			}
			else
			{
				return a;
			}	
		}
	);

	return str;
}
//
// .output(a)
//
wp_sk.iWiki.output = function (a)
{
	if (wp_sk.iWiki.arr_i==0)
	{
		return a;
	}
	var str = '\n';
	
	wp_sk.iWiki.arr.sort(wp_sk.iWiki.comp);
	for (var i=0; i<wp_sk.iWiki.arr_i; i++)
	{
		str += '\n[['+wp_sk.iWiki.arr[i][0]+':'+wp_sk.iWiki.arr[i][1]+']]';
	}

	return str;
}
//
// .comp(a,b)
//
wp_sk.iWiki.comp = function (a, b)
{
	if (wp_sk.iWiki.order.indexOf(a[0]) < wp_sk.iWiki.order.indexOf(b[0]))
	{
		return -1;
	}
	else if (wp_sk.iWiki.order.indexOf(a[0]) > wp_sk.iWiki.order.indexOf(b[0]))
	{
		return 1;
	}
	// else
	return 0;
}
/*
	based on:
	http://meta.wikimedia.org/wiki/Interwiki_sorting_order#By_order_of_alphabet.2C_based_on_local_language
*/
wp_sk.iWiki.order = [
            'ace', 'af', 'ak', 'als', 'am', 'ang', 'ab', 'ar', 'an', 'arc',
            'roa-rup', 'frp', 'as', 'ast', 'gn', 'av', 'ay', 'az', 'bm', 'bn',
            'zh-min-nan', 'nan', 'map-bms', 'ba', 'be', 'be-x-old', 'bh', 'bcl',
            'bi', 'bar', 'bo', 'bs', 'br', 'bg', 'bxr', 'ca', 'cv', 'ceb', 'cs',
            'ch', 'cbk-zam', 'ny', 'sn', 'tum', 'cho', 'co', 'cy', 'da', 'dk',
            'pdc', 'de', 'dv', 'nv', 'dsb', 'dz', 'mh', 'et', 'el', 'eml', 'en',
            'myv', 'es', 'eo', 'ext', 'eu', 'ee', 'fa', 'hif', 'fo', 'fr', 'fy',
            'ff', 'fur', 'ga', 'gv', 'gd', 'gl', 'gan', 'ki', 'glk', 'gu',
            'got', 'hak', 'xal', 'ko', 'ha', 'haw', 'hy', 'hi', 'ho', 'hsb',
            'hr', 'io', 'ig', 'ilo', 'bpy', 'id', 'ia', 'ie', 'iu', 'ik', 'os',
            'xh', 'zu', 'is', 'it', 'he', 'jv', 'kl', 'kn', 'kr', 'pam', 'ka',
            'ks', 'csb', 'kk', 'kw', 'rw', 'ky', 'rn', 'sw', 'kv', 'kg', 'ht',
            'ku', 'kj', 'lad', 'lbe', 'lo', 'la', 'lv', 'lb', 'lt', 'lij', 'li',
            'ln', 'jbo', 'lg', 'lmo', 'hu', 'mk', 'mg', 'ml', 'mt', 'mi', 'mr',
            'arz', 'mzn', 'ms', 'cdo', 'mwl', 'mdf', 'mo', 'mn', 'mus', 'my',
            'nah', 'na', 'fj', 'nl', 'nds-nl', 'cr', 'ne', 'new', 'ja', 'nap',
            'ce', 'pih', 'no', 'nb', 'nn', 'nrm', 'nov', 'ii', 'oc', 'mhr',
            'or', 'om', 'ng', 'hz', 'uz', 'pa', 'pi', 'pag', 'pnb', 'pap', 'ps',
            'km', 'pcd', 'pms', 'tpi', 'nds', 'pl', 'tokipona', 'tp', 'pnt',
            'pt', 'aa', 'kaa', 'crh', 'ty', 'ksh', 'ro', 'rmy', 'rm', 'qu',
            'ru', 'sah', 'se', 'sm', 'sa', 'sg', 'sc', 'sco', 'stq', 'st', 'tn',
            'sq', 'scn', 'si', 'simple', 'sd', 'ss', 'sk', 'cu', 'sl', 'szl',
            'so', 'ckb', 'srn', 'sr', 'sh', 'su', 'fi', 'sv', 'tl', 'ta', 'kab',
            'roa-tara', 'tt', 'te', 'tet', 'th', 'ti', 'tg', 'to', 'chr', 'chy',
            've', 'tr', 'tk', 'tw', 'udm', 'bug', 'uk', 'ur', 'ug', 'za', 'vec',
            'vi', 'vo', 'fiu-vro', 'wa', 'zh-classical', 'vls', 'war', 'wo',
            'wuu', 'ts', 'yi', 'yo', 'zh-yue', 'diq', 'zea', 'bat-smg', 'zh',
            'zh-tw', 'zh-cn'
]

/* =====================================================
	Class: wp_sk.iWikiFA

	Gather, cleanup and output of tempates of
	Featured Articles
	
	.gather(str)
		gather FA templates from str,
		returns new str without FA templates
	.output(a)
		output wikitext with resorted tempates of
		Featured Articles.
		parameter a is not important

	.arr
		array for FA templates ([language, article])
	.arr_i
		helpufull index and a number of elements in arr
   ===================================================== */
// object init
wp_sk.iWikiFA = new Object();
//
// .gather(str)
//
wp_sk.iWikiFA.gather = function(str)
{
	wp_sk.iWikiFA.arr = new Array();
	wp_sk.iWikiFA.arr_i = 0;
	// wg: http://meta.wikimedia.org/wiki/List_of_Wikipedias
	str = str.replace(
		/\{\{[Ll]ink FA\|([a-z\-]{2,3}|simple|ru-sib|be-x-old|zh-yue|map-bms|zh-min-nan|nds-nl|bat-smg|zh-classical|fiu-vro|roa-rup|tokipona|cbk-zam|roa-tara)\}\}/g,
		function (a, lang)
		{
			// wg: http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3/maintenance/interwiki.sql
			if (lang!='wp' && lang!='mw' && lang!='gej' && lang!='ppr' && lang!='rfc' && lang!='uea' && lang!='why')
			{
				wp_sk.iWikiFA.arr[wp_sk.iWikiFA.arr_i] = lang;
				wp_sk.iWikiFA.arr_i++;
				return '';
			}
			else
			{
				return a;
			}	
		}
	);

	return str;
}
//
// .output(a)
//
wp_sk.iWikiFA.output = function (a)
{
	if (wp_sk.iWikiFA.arr_i==0)
	{
		return a;
	}
	var str = '\n';
 
	wp_sk.iWikiFA.arr.sort(wp_sk.iWiki.comp);
	for (var i=0; i<wp_sk.iWikiFA.arr_i; i++)
	{
		str += '\n{{link FA|'+wp_sk.iWikiFA.arr[i]+'}}';
	}
 
	return str;
}

/* =====================================================
	Class: wp_sk.redir

	Fixing redirects.
	For it is based on the article preview in which
	redirects ar marked with special class (mw-redirect)
 
	.init()
		init redir replacement by changing the cleanup icon
		and sending the first request to resolve redirects
	.resp(res)
		response taking functions which does 
		basicly everything else
	
	.arr	- array for redirs resolved art. names used internally
	.arr_i	- index for the above array
	.url	- url of the first request
			needed in case of continuing request
   ===================================================== */
//
// object init
//
wp_sk.redir = new Object();
 
//
// .init()
//
wp_sk.redir.init = function()
{
	wp_sk.redir.base_url = mw.config.get('wgServer')+'/w/api.php?action=query&rawcontinue=&redirects&format=json&titles=';
	var img_loading = 'http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif';
	// var img_loading='http://upload.wikimedia.org/wikipedia/commons/3/32/Loader3.gif';
	
	// time boundry for re-request but only on submit (this is so the servers won't die)
	if (mw.config.get('wgAction')=='submit')
	{
		if (document.cookie.indexOf('wpsk_redir_time_disable=1')!=-1)
		{
			return;
		}
		else
		{
			var d = new Date();
			d = new Date(d.getTime()+300000); //+5min (num. seconds * 1000)
			document.cookie = "wpsk_redir_time_disable=1; path=/; expires=" + d.toGMTString();
		}
	}

	var elWikiBody = document.getElementById('wikiPreview');
	if (elWikiBody)
	{
		//
		// search for redirects
		wp_sk.redir.urls = new Array();
		wp_sk.redir.urls[0] = new Array();
		var url_i = url_j = 0;
		var as = elWikiBody.getElementsByTagName('a');
		for (var i=0; i<as.length; i++)
		{
			if (as[i].className=='mw-redirect')
			{
				var tmp = as[i].href.replace(/^.+\/wiki\/([^#]+).*$/,'$1').replace(/_/g,'%20');
				// new url?
				var isnew=true;
				for (var ui=0; ui<=url_i; ui++)
				{
					for (var uj=0; uj<url_j; uj++)
					{
						if (wp_sk.redir.urls[ui][uj]==tmp)
						{
							isnew=false;
							break;
						}
					}
					if (!isnew)
						break;
				}
				// add to array
				if (isnew)
				{
					wp_sk.redir.urls[url_i][url_j++] = tmp;
					if (url_j>=50)	// API limitation
					{
						if (url_i>=4)	// max (4+1)x50 links
						{
							break;
						}
						url_j = 0;
						wp_sk.redir.urls[++url_i] = new Array();
					}
				}
			}
		}
		//
		// final prepartion and sending a request
		if (wp_sk.redir.urls[0].length>0)
		{
			// loading...
			var nel = document.createElement('span');
			nel.id = 'wp_sk_redirs';
			nel.innerHTML = '<img title="'+wp_sk.lang['Redirs clenup - preparation']+'" alt="..." src="'+img_loading+'" border="0" width="18" />';
			wp_sk.btns.appendChild(nel)
			
			// for found redirects
			wp_sk.redir.arr = new Array();
			wp_sk.redir.arr_i = 0;

			// set up first portion
			wp_sk.redir.urls_i = 0;
			var url = wp_sk.redir.urls[wp_sk.redir.urls_i].join('|');
			wp_sk.redir.url = wp_sk.redir.base_url+url;
			wp_sk.redir.full_prev_url = wp_sk.redir.url;
			wp_sk.debug('<h2>['+wp_sk.redir.urls_i+']['+wp_sk.redir.urls[wp_sk.redir.urls_i].length+']</h2>');
			// run
			wp_sk.ajaxRun(wp_sk.redir.url, wp_sk.redir.resp);
		}
	}
}

//
// .resp(res)
//
wp_sk.redir.resp = function (res)
{
	wp_sk.debug(res+'<hr />');
	var jres = eval('('+res+')');
	
	// gather replace values for links
	var txtescape = /([\\^\$\*\+\?\.\(\)\[\]\{\}\:\=\!\|\,\-])/g;
	for (var r in jres.query.redirects)
	{
		r = jres.query.redirects[r];
		wp_sk.redir.arr[wp_sk.redir.arr_i++] = {
			'rdir' : r.from,
			'art' : r.to
		}
		wp_sk.debug('['+(wp_sk.redir.arr_i-1)+']rdir:'+r.from+'<br />art:'+r.to);
	}
	// continue?
	if (jres['query-continue']!=null)
	{
		var continue_url = wp_sk.redir.url + '&plcontinue='+encodeURIComponent(jres['query-continue'].links.plcontinue);
		if (wp_sk.redir.full_prev_url != continue_url)	// <s>api</s> potential bug workaround
		{
			wp_sk.redir.full_prev_url = continue_url;
			wp_sk.ajaxRun(continue_url, wp_sk.redir.resp);
			return;
		}
		else
		{
			wp_sk.debug('<p style="font-weight:bold;font-size:200%">Warning! Query continue loop.</p>');
		}
	}
	// another portion of links
	else if (wp_sk.redir.urls_i < wp_sk.redir.urls.length-1)
	{
		var url = wp_sk.redir.urls[++wp_sk.redir.urls_i].join('|');
		wp_sk.redir.url = wp_sk.redir.base_url+url;
		wp_sk.redir.full_prev_url = wp_sk.redir.url;
		wp_sk.debug('<h2>['+wp_sk.redir.urls_i+']['+wp_sk.redir.urls[wp_sk.redir.urls_i].length+']</h2>');
		wp_sk.ajaxRun(wp_sk.redir.url, wp_sk.redir.resp);
		return;
	}
	
	/*
	// debug - start
	var str;
	// szukane
	str = ''
	for (var i=0;i<wp_sk.redir.urls.length;i++)
		for (var j=0;j<wp_sk.redir.urls[i].length;j++)
			str += '\nwp.urls['+i+']['+j+']='+ wp_sk.redir.urls[i][j]
	;
	wp_sk.debug('<textarea>'+str+'</textarea>');

	// znalezione
	str = ''
	for (var i=0;i<wp_sk.redir.arr.length;i++)
		str += '\nwp.rdirs['+i+']='+ wp_sk.redir.arr[i].rdir
	;
	wp_sk.debug('<textarea>'+str+'</textarea>');
	// debug - end
	*/
	
	// prepare function to replace redirects
	wp_sk.cleanerLinks_orig = wp_sk.cleanerLinks;
	wp_sk.cleanerLinks = function (str)
	{
		var reTxtEscape = /([\\^\$\*\+\?\.\(\)\[\]\{\}\:\=\!\|\,\-])/g;
		for (var page in wp_sk.redir.arr)
		{
			page = wp_sk.redir.arr[page];
			var re = page.rdir.replace(reTxtEscape,'\\$1');
			if (re.search(/^[a-zÄÖÜäößü]/i)==0)
			{
				re = '['+ re[0].toLowerCase() + re[0].toUpperCase() +']'
					+ re.substr(1);
			}
			var re = new RegExp('\\[\\[('+re+')(\\||\\]\\])', 'g');
			str = str.replace(re, function (a, art, end)
			{
				return '[['+ page.art + (end=='|' ? '|' : '|'+art+']]');
			});
		}

		return wp_sk.cleanerLinks_orig(str);	// called now so that replaced links can be fixed/cleaned
	}
	
	// loading finished
	var redirs_loading_el = document.getElementById('wp_sk_redirs')
	if (redirs_loading_el)
	{
		redirs_loading_el.innerHTML='';
		redirs_loading_el.style.display='none';
	}
	var el = document.getElementById('wp_sk_img_btn');
	if (wp_sk.is_old_toolbar)
	{
		el.src = 'http://upload.wikimedia.org/wikipedia/commons/3/31/Button_broom_R.png';
	}
	else
	{
		el.src = 'http://commons.wikimedia.org/w/thumb.php?f=Broom%20icon%20R.svg&width=22px';
	}
}
/* -----------------------------------------------------
	Classes supporting cleanup                     {END}
   ===================================================== */

/* =====================================================
	Function: wp_sk.ajaxRun(url,fun_odbioru)
	
	Send request for given url and set fun_odbioru as 
	a function which will recieve the text response
	(text will be send through the first param.)
   ===================================================== */
wp_sk.ajaxRun = function (url,fun_odbioru)
{ 
	var xmlhttp = sajax_init_object();
	if(xmlhttp)
	{
		try
		{
			wp_sk.debug('<a href="'+url.replace(/[?&]format=[^&]+/g,'')+'>ajax call</a>');
			
			xmlhttp.open("GET", url, true);
			xmlhttp.onreadystatechange=function()
			{
				// zakończono przetwarzanie
				if (xmlhttp.readyState==4)
				{
					fun_odbioru (xmlhttp.responseText);
				}
			};
			xmlhttp.send(null);     	
		}
		catch (e)
		{
			if (window.location.hostname == "localhost")
			{
				alert("Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing.");
			}
			throw e;
		}
	}
}

/* =====================================================
	Function: Array.prototype.indexOf(elt)
	
	Not needed in: Gecko>1.8b2
   ===================================================== */
if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(elt /*, from*/)
	{
		var len = this.length;

		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0)
			from += len;

		for (; from < len; from++)
		{
			if (from in this && this[from] === elt)
				return from;
		}
		return -1;
	};
}

/* =====================================================
	OnLoad
   ===================================================== */
if (typeof wp_sk_show_as_button!='undefined' && wp_sk_show_as_button)
{
	if(mw.config.get('wgAction')=='submit' || mw.config.get('wgAction')=='edit')
		addOnloadHook(wp_sk.button);
}

if (typeof wp_sk_redir_enabled!='undefined' && wp_sk_redir_enabled)
{
	// it works immiediatly if someone has preview on first edit
	if(mw.config.get('wgAction')=='submit' || mw.config.get('wgAction')=='edit')
		addOnloadHook(wp_sk.redir.init)
	;
}