/* global $, mw, pathoschild */
// <nowiki>
popupRevertSummaryPrompt=true;
popupFixRedirs=true;

importScript('User:Ale_jrb/Scripts/igloo.js'); // ]

// http://de.wikipedia.orghttps://wiki95.com/en/Benutzer:Stefan/Sperrstatus.js
$(function () {
   var farbe = '';
   if ( document.getElementById('ca-edit') ) {
       var obj  = document.getElementById('ca-edit');
       var temp = String(mw.config.get('wgRestrictionEdit'));
       if (temp == 'autoconfirmed') {
           farbe = '#ffa500';
       } else if (temp == 'sysop') {
           farbe = '#ff6a6a';
       }
   }
   if ( document.getElementById('ca-viewsource') ) {
       var obj = document.getElementById('ca-viewsource');
       farbe = '#ff0000';
   }
   if ( obj ) {
       obj.firstChild.style.backgroundColor = farbe;
   }
});


// Adds a "Replace" tab which pops up two prompt boxes; one for a regexp and one for a replacement
function wpTextboxReplace()
{
    var s = prompt("Search regexp:");
    var txt = document.editform.wpTextbox1;
    while ( true ) {
        if (!s) return;
        var s_r = new RegExp(s, 'mg');
        if (s_r.test(txt.value)) {
            var r = prompt("Replace /"+s+"/ with:");
            if(!r && r != '') return;
            txt.value = txt.value.replace(s_r, r);
            if (document.editform.wpSummary.value) {
                document.editform.wpSummary.value += ', s/' + s + '/' + r + '/mg';
            }
            else {
                document.editform.wpSummary.value = 's/' + s + '/' + r + '/mg';
            }
            return;
        }
        else {
            var s_0 = s;
            s = prompt('/' + s_0 + '/ does not match anything. Wanna try again?');
        }
    }
}
$(function () {
    if (document.forms.editform) {
        mw.util.addPortletLink('p-cactions', 'javascript:wpTextboxReplace()', 'Replace', 'ca-replace',
                       'Regexp replace for the edit window', 'R', document.getElementById('ca-history'));
    }
});


// ]

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.orghttps://wiki95.com/en/TemplateScript
 * @update-token ]
 */
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	pathoschild.TemplateScript.add([
		{
			name: 'General article fixes',
			script: function(editor) { // own script of gen. fixes for articles
				// Some workarounds
				// (+): JavaScript can't match newlines with ".", thus it needs to be done by using regex for whitespace character and non-whitespace characters
				// (\n): if stuff needs to explicitely match a newline, wrap it into brackets and then reference it by using $1 (or similar) in the replace string
				
				// first fix
				editor
					.replace(/\{\{template:/gi,'{{'); // template is the default
					
				// then, let's bypass redirects so we can use proper names for further fixes
				editor
					.replace(/\{\{(facts?|(source|os cite|ref(erence)?|cit(e|ation))?needed|uncited|(a|c)n|needs citation|sourceme|cit(e|ation) missing|proveit|(really|source|ref|are you sure)\?|refplease|need cite|cite ref pls|needsref|bollocks)(\||\}{2})/gi,'{{Citation needed$8') // {{Citation needed}} and similar
					.replace(/\{\{(commons?\s?cat|commonscat\simages|commonscategory|commons2|catistcommons|Cms-catlist-up)(\||\}{2})/gi,'{{Commons category$2') // {{Commons category}}
					.replace(/\{\{(Article\s?issues|Issues|AI)(\||\}{2})/gi,'{{Multiple issues$2') // {{Multiple issues}}
					.replace(/\{\{(wikimedia|sister\s?links2?)(\||\}{2})/gi,'{{Sister project links$2') // {{Sister project links}}
					.replace(/\{\{(CI|cleanup?cat|few\s?cat(s|egories)|improve?cat(s|egories)|more\s?cat(egories|s|egory)?|cat(egory|egories)??improve|more cats)(\||\}{2})/gi,'{{Cat improve$6') //  {{Cat improve}}
					.replace(/\{\{(attention\s?(\(on talk page\)|needed|see talk)?|cleanup(-since|quality|date|pitt)?|cl?u|tidy|unfinished|attn|ugly|improve|quality)(\||\}{2})/gi,'{{Cleanup$4') // {{Cleanup}}
					.replace(/\{\{(cleanup(-clarity|\s?confusing)|simplify|unclear)/gi,'{{Confusing') // {{Confusing}}
					.replace(/\{\{((g|grammar)\s?check|(cleanup-)?copy-?edit|cleanup-english|ortogramrevision|grammar)/gi,'{{Copy edit') // {{Copy edit}}
					.replace(/\{\{(cleanup-(references|citation|refs)|ref-cleanup|citation-?style|ref(erence)?style|citestyle|cleanrefs|refs?clean)/gi,'{{Citation style') // {{Citation style}}
					.replace(/\{\{(internal\s?links|dep|dead end page|needs links|deadend)/gi,'{{Dead end') // {{Dead end}}
					.replace(/\{\{(essay-opinion|essay-entry|essaylike|cleanup-?essay)/gi,'{{Essay-like') // {{Essay-like}}
					.replace(/\{\{(expansion|very\s?short|too-short)/gi,'{{Expand') // {{Expand}}
					.replace(/\{\{(cleanupfiction-as-fact|in\s?universe)/gi,'{{In-universe') // {{In-universe}}
					.replace(/\{\{(no-?intro(duction)?|no\s?lead(\ssection)?|missingintro|intro?missing|opening|leadsection)/gi,'{{Lead missing') // {{Lead missing}}
					.replace(/\{\{(lead section|(expand|short)\s?lead|(lead)?too\s?short(\s2)?|short intro|intro-tooshort|expandlede)/gi,'{{Lead too short')
					.replace(/\{\{(vagueintro|(intro|lead)-?rewrite|intro|lead|lede)/gi,'{{Lead rewrite');
					
				// now let's start with the real 'general' fixes
				editor
					.replace(/\[\[Image:/gi,'[[File:') // New namespace name
					.replace(/\|thumb\|right/g,'|thumb') // Thumbs are defaulted to right
					.replace(/<gallery>(*\n)Image:(+)<\/gallery>/gi,'<gallery>$1File:$2</gallery>') // namespace within galleries
					.replace(/\[\[File:(.+)_(.+)_(.+)_(.+)_(.+)_(.+)_(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2 $3 $4 $5 $6 $7 $8.$9') // remove underscores from filenames (7) -- MAXIMUM
					.replace(/\[\[File:(.+)_(.+)_(.+)_(.+)_(.+)_(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2 $3 $4 $5 $6 $7.$8') // remove underscores from filenames (6)
					.replace(/\[\[File:(.+)_(.+)_(.+)_(.+)_(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2 $3 $4 $5 $6.$7') // remove underscores from filenames (5)
					.replace(/\[\[File:(.+)_(.+)_(.+)_(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2 $3 $4 $5.$6') // remove underscores from filenames (4)
					.replace(/\[\[File:(.+)_(.+)_(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2 $3 $4.$5') // remove underscores from filenames (3)
					.replace(/\[\[File:(.+)_(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2 $3.$4') // remove underscores from filenames (2)
					.replace(/\[\[File:(.+)_(.+)\.(\w+)/gi,'[[File:$1 $2.$3') // remove underscores from filenames (1)
					.replace(/\{\{Commons\|Category:(.+)(\|\1)?\}\}/gi,'{{Commons category|$1}}') // Wrong template ({{Commons|Category:Somethin}} - {{Commons category|Something}}
					.replace(/\{\{Commons\|Category:(.+)\|(.+)\}\}/gi,'{{Commons category|$1|$2}}') // {{Commons|Category:Somethin|Different text}} - {{Commons category|Something|different text}}
					.replace(/\{\{Multiple issues\|(+)=((January|February|March|April|May|June|July|August|September|October|November|December)\s\d{4})\}\}/g,'{{$1|date=$2}}') // only one concern within {{Multiple issues}}
					.replace(/\{\{Multiple issues\}\}/g,'') // please specify issues within {{Multiple issues}}
					.replace(/\{\{DEFAULTSORT\|(.+)\}\}/g,'{{DEFAULTSORT:$1}}') // usage of magic words
					.replace(/(={2,6})\s*'''(.+)'''\s*\1(\n)/g, '$1$2$1$3') // unnecessary bolding removed from headings
					.replace(/(={2,6})\s*(.+):\s*\1(\n)/g, '$1$2$1$3') // remove colon from headings
					.replace(/(<br>|<\br>|<\/br\s>|<br\/>|<br\/\s>)/gi,'<br />') // proper <br />
					.replace(/\*\+) official (web\s?)?site\]/gi,'*{{Official|http://$1}}') // usage of {{Official}}
					.replace(/\{\{lifetime\|/gi,'{{subst:ltm|') // subst: {{lifetime}}
					.replace(/(==+)\s*external\s*links\s*(==+)/gi,'$1External links$1') // fix headers
					.replace(/(==+)\s*references\s*(==+)/gi,'$1References$1')
					.replace(/&deg;/g, '°') // convert HTML entity into actual character
					.replace(/\&dagger\;/g, '†')
					.replace(/\&ndash\;/g, '–')
					.replace(/\&mdash\;/g, '—')
					.replace(/\&rarr\;/g, '→')
					.replace(/\&larr\;/g, '←')
					.replace(/\&rt\;/g, '>')
					.replace(/\&lt\;/g, '<')
					.replace(/\&hellip\;/g, '…')
					.replace(/º/g, '°')
					.replace(/&sup2;/g, '²')
					.replace(/&sup3;/g, '³')
					.replace(/<\/b>/gim, '\'\'\'')  // HTML2Wiki
					.replace(/<li>/g, '*')
					.replace(/<\/li>/g, '')
					.replace(/<ul>/g, '')
					.replace(/<\/ul>/g, '')
					.replace(/<br \/>(\n){2}/g, '\n\n') // unnecessary <br />
					.replace(/<\/i>/gim, '\'\'')
					.replace(/<b>/gim, '\'\'\'')
					.replace(/<i>/gim, '\'\'')
					.replace(/\t/g, ' ') // replace tabulators with spaces
					.replace(/ +/g, ' ')   // reduce unnecessary spaces
					.replace(/ (\n)/g, '$1') // remove spaces at the end of a line
					.replace(/\*(|]+?)*\|*( \t]+?)*\]\]/g, ']') // link syntax
					.replace(/\]+)\|\1( \t]+)\]\]/g, ']$2')
					.replace(/(\n)\|image=Replacethisimage(fe)?male\.svg/gi,'$1|image=') // remove placeholders
					.replace(/(\n)\|image=\thisimage(fe)?male\.svg.+\1/gi,'$1|image=$1')
					.replace(/\[\[category:/g, '[[Category:')  // capitalize categories
					.replace(/\[\[Category: /g, '[[Category:')
					.replace(/\[\[Category:a/g, '[[Category:A')
					.replace(/\[\[Category:b/g, '[[Category:B')
					.replace(/\[\[Category:c/g, '[[Category:C')
					.replace(/\[\[Category:d/g, '[[Category:D')
					.replace(/\[\[Category:e/g, '[[Category:E')
					.replace(/\[\[Category:f/g, '[[Category:F')
					.replace(/\[\[Category:g/g, '[[Category:G')
					.replace(/\[\[Category:h/g, '[[Category:H')
					.replace(/\[\[Category:i/g, '[[Category:I')
					.replace(/\[\[Category:j/g, '[[Category:J')
					.replace(/\[\[Category:k/g, '[[Category:K')
					.replace(/\[\[Category:l/g, '[[Category:L')
					.replace(/\[\[Category:m/g, '[[Category:M')
					.replace(/\[\[Category:n/g, '[[Category:N')
					.replace(/\[\[Category:o/g, '[[Category:O')
					.replace(/\[\[Category:p/g, '[[Category:P')
					.replace(/\[\[Category:q/g, '[[Category:Q')
					.replace(/\[\[Category:r/g, '[[Category:R')
					.replace(/\[\[Category:s/g, '[[Category:S')
					.replace(/\[\[Category:t/g, '[[Category:T')
					.replace(/\[\[Category:u/g, '[[Category:U')
					.replace(/\[\[Category:v/g, '[[Category:V')
					.replace(/\[\[Category:w/g, '[[Category:W')
					.replace(/\[\[Category:x/g, '[[Category:X')
					.replace(/\[\[Category:y/g, '[[Category:Y')
					.replace(/\[\[Category:z/g, '[[Category:Z')
					.replace(/\[\[file:/g, '[[File:') // capitalize files
					.replace(/\[\[File:\s+/g, '[[File:')
					.replace(/\[\[File:a/g, '[[File:A')
					.replace(/\[\[File:b/g, '[[File:B')
					.replace(/\[\[File:c/g, '[[File:C')
					.replace(/\[\[File:d/g, '[[File:D')
					.replace(/\[\[File:e/g, '[[File:E')
					.replace(/\[\[File:f/g, '[[File:F')
					.replace(/\[\[File:g/g, '[[File:G')
					.replace(/\[\[File:h/g, '[[File:H')
					.replace(/\[\[File:i/g, '[[File:I')
					.replace(/\[\[File:j/g, '[[File:J')
					.replace(/\[\[File:k/g, '[[File:K')
					.replace(/\[\[File:l/g, '[[File:L')
					.replace(/\[\[File:m/g, '[[File:M')
					.replace(/\[\[File:n/g, '[[File:N')
					.replace(/\[\[File:o/g, '[[File:O')
					.replace(/\[\[File:p/g, '[[File:P')
					.replace(/\[\[File:q/g, '[[File:Q')
					.replace(/\[\[File:r/g, '[[File:R')
					.replace(/\[\[File:s/g, '[[File:S')
					.replace(/\[\[File:t/g, '[[File:T')
					.replace(/\[\[File:u/g, '[[File:U')
					.replace(/\[\[File:v/g, '[[File:V')
					.replace(/\[\[File:w/g, '[[File:W')
					.replace(/\[\[File:x/g, '[[File:X')
					.replace(/\[\[File:y/g, '[[File:Y')
					.replace(/\[\[File:z/g, '[[File:Z')
					.replace(/\{\{(+)\|(\n)()()\}\}/g,'{{$1$2|$3$4}}') // wrong {{Infobox}}
					.replace(/<references \/>/gi, '{{Reflist}}');
					
				editor
					.setEditSummary('general fixes')
					.clickDiff();
			}
		},
	]);
});

//importScript('User:The Evil IP address/debug.js');
importScript('User:GregU/dashes.js');
//importScript('User:The Evil IP address/hdedit.js');
importScript('User:Ohconfucius/script/MOSNUM dates.js');
importScript('User:Ohconfucius/script/EngvarB.js');  //]
/* </nowiki> */