Jump to content

User:Alexis Jazz/LuckyRename-test.js

From 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.
//<nowiki>
//LuckyRename, a script to streamline moving files and easily request renames. See User:Alexis Jazz/LuckyRename for info
//LuckyRename is public domain, irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz.
/*globals $:false,mw:false,OO:false*/
window.LuckyRename = {};
var LuckyRename = window.LuckyRename;
LuckyRename.settings = {};

if ( mw.config.get('wgNamespaceNumber') == 6 && mw.config.get('wgAction') == "view" && mw.config.get('wgDiffNewId') == null && window.location.href.match('veaction=') == null && window.location.href.match('oldid=') == null) { //only load on file pages
LuckyRename.escapeRegExp = function(string) {
	return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
};
LuckyRename.createCSSClass = function(css) {
	LuckyRename.style = document.createElement('style');
	LuckyRename.style.type = 'text/css';
	LuckyRename.style.innerHTML = css;
	document.getElementsByTagName('head')[0].appendChild(LuckyRename.style);
};
LuckyRename.createCSSClass('.LuckyRenamePendingBlink { animation: LuckyRenameBlinker 1s linear infinite; }');
LuckyRename.createCSSClass('@keyframes LuckyRenameBlinker { 50% { opacity: 0.5; } }');
LuckyRename.createCSSClass('.LuckyRenameActionCompleted { color: green;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionFailed { color: red;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionSkipped { color: orange;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionSkippedDisallowed { color: orange;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionFailedMWE { color: red;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionFailedMissingTitle { color: red;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionFailedOther { color: red;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionFailedRetry { animation: LuckyRenameBlinker 1s linear infinite;color: orange;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameActionFailedGaveUp { color: red;font-weight: bold; }');
LuckyRename.createCSSClass('.actionFilenamenotfound { color: orange;font-weight: bold; }');
LuckyRename.createCSSClass('.LuckyRenameFloatRight { float: right; }');
LuckyRename.createCSSClass('.LuckyRenameHalfOpacity { opacity: 0.50; }');
LuckyRename.createCSSClass('.LuckyRenameVisible { display:block; }');
LuckyRename.createCSSClass('.LuckyRenameInvisible { display:none; }');
LuckyRename.createCSSClass('.LuckyRenameButtonBarPadding { padding-top:0.5em; }');
LuckyRename.createCSSClass('.LuckyRename2Em { font-size:2em; margin-top:-8px; }');
LuckyRename.updateMoveStatus = function(title, status, description) {
	if ( typeof document.getElementById("movePage_" + encodeURIComponent(title).replace( /[%\._]/g, '')) != "undefined" && typeof document.getElementById("movePage_" + encodeURIComponent(title).replace( /[%\._]/g, '')).classList != "undefined" ) {
		document.getElementById("movePage_" + encodeURIComponent(title).replace( /[%\._]/g, '')).classList = [ status ];
		document.getElementById("movePage_" + encodeURIComponent(title).replace( /[%\._]/g, '')).innerHTML = LuckyRename.msgs[status];
		if ( typeof description != "undefined" ) {
			document.getElementById("movePageStatusMsg_" + encodeURIComponent(title).replace( /[%\._]/g, '')).innerHTML = description;
		}
	}
};
LuckyRename.reportDebugMsg = function(msg) {
	if ( typeof msg == 'string' ) {
		console.log('LuckyRename: ' + msg);
	} else {
		console.log(msg);
	}
	if ( LuckyRename.settings.debugUI ) {
		$( '.LuckyRename' ).append( msg + '<br \>');
	}
};
LuckyRename.escapeReplacement = function(text) {
	return text.replace(/\$/g, '$$$$');
};
LuckyRename.UserCanMoveFiles = function() {
	if ( mw.config.get('wgUserGroups').indexOf( 'filemover' ) !== -1 || mw.config.get('wgUserGroups').indexOf( 'sysop' ) !== -1 ) {
		return true;
	} else {
		return false;
	}
};
LuckyRename.UserCanSuppressRedirects = function() {
	if ( mw.config.get('wgUserGroups').indexOf( 'extendedmover' ) !== -1 || mw.config.get('wgUserGroups').indexOf( 'sysop' ) !== -1 || mw.config.get('wgUserGroups').indexOf( 'bot' ) !== -1 || mw.config.get('wgUserGroups').indexOf( 'bureaucrat' ) !== -1 ) { //I know bots don't use LuckyRename
		return true;
	} else {
		return false;
	}
};

LuckyRename.updateReplaceUsageStatus = function(title, status, description) {
	if ( document.getElementById("replaceusage_" + encodeURIComponent(title).replace( /[%\._]/g, '')) ) {
		document.getElementById("replaceusage_" + encodeURIComponent(title).replace( /[%\._]/g, '')).classList = [ status ];
		document.getElementById("replaceusage_" + encodeURIComponent(title).replace( /[%\._]/g, '')).innerHTML = LuckyRename.msgs[status];
		if ( typeof description != "undefined" ) {
			document.getElementById("replaceusageStatusMsg_" + encodeURIComponent(title).replace( /[%\._]/g, '')).innerHTML = description;
		}
	} else if ( typeof title != "undefined" && title != LuckyRename.oldFile ){
		$( '.LuckyRename' ).append('<br \>Element with ID "replaceusage_' + encodeURIComponent(title).replace( /[%\._]/g, '') + '" could not be found. Please <a href="https://wiki.riteme.site/wiki/User_talk:Alexis_Jazz/LuckyRename">report this error</a>.');
	} else if ( title != LuckyRename.oldFile ) {
		$( '.LuckyRename' ).append('<br \>Attempt to run updateReplaceUsageStatus without a title. Please <a href="https://wiki.riteme.site/wiki/User_talk:Alexis_Jazz/LuckyRename">report this error</a>.');
	} else {
		LuckyRename.reportDebugMsg('Attempted to run updateReplaceUsageStatus on the current page');
	}
};

LuckyRename.getWikidataLabel = function(qnum,filepageText,articleqid,luckyFilename) { //returns English Wikidata label for a Wikidata Q number
	if (typeof LuckyRename.commonLabels[qnum] != "undefined") {
		LuckyRename.reportDebugMsg('Label ' + qnum + ' already known');
		return LuckyRename.commonLabels[qnum];
	} else if (typeof LuckyRename.wikidataprop.entities == "undefined" || typeof LuckyRename.wikidataprop.entities[qnum] == "undefined") {
		LuckyRename.wdapi.get( {'action':'wbgetentities','format':'json','ids':qnum} ).then( function ( data ) {
			LuckyRename.wikidataprop.entities = Object.assign(LuckyRename.wikidataprop.entities, data.entities);
			LuckyRename.Dolucky(filepageText,articleqid,luckyFilename);
		});
		return false;
	} else if ( LuckyRename.settings.debug ) {
		LuckyRename.reportDebugMsg('Recycled wikidata ' + qnum);
	}
	LuckyRename.contentLang = mw.config.get('wgContentLanguage');
	if ( LuckyRename.settings.debug ) {
		$( '.LuckyRename' ).append( '\'' + qnum + '\': \'' + LuckyRename.wikidataprop.entities[qnum].labels[LuckyRename.contentLang].value + '\'<br \>');
	}
	return LuckyRename.wikidataprop.entities[qnum].labels[LuckyRename.contentLang].value;
};

LuckyRename.getWikidataClaim = function(qnum,prop,filepageText,articleqid,luckyFilename) { //returns Q number for the requested property
	//LuckyRename.wikidataprop.entities should actually be something specific to the qnum, Object assign isn't doing what it should (create a variable with content from all queries made)
	if (typeof LuckyRename.wikidataprop == "undefined" || typeof LuckyRename.wikidataprop.entities == "undefined" || typeof LuckyRename.wikidataprop.entities[qnum] == "undefined") {
		LuckyRename.wdapi.get( {'action':'wbgetentities','format':'json','ids':qnum} ).then( function ( data ) {
			LuckyRename.wikidataprop.entities = Object.assign(LuckyRename.wikidataprop.entities, data.entities); //.entities[0].id Q17320061, Q17320061=Astra Linux
			LuckyRename.Dolucky(filepageText,articleqid,luckyFilename);
		});
		return false;
	} else if ( LuckyRename.settings.debug ) {
		LuckyRename.reportDebugMsg('Recycled wikidata ' + qnum);
	}
	if ( typeof LuckyRename.wikidataprop != "undefined" && typeof LuckyRename.wikidataprop.entities != "undefined" && typeof LuckyRename.wikidataprop.entities[qnum] != "undefined" && typeof LuckyRename.wikidataprop.entities[qnum].claims != "undefined" && typeof LuckyRename.wikidataprop.entities[qnum].claims[prop] != "undefined" ) {
		return LuckyRename.wikidataprop.entities[qnum].claims[prop];
	} else {
		LuckyRename.reportDebugMsg('Wikidata claim ' + prop + ' for ' + qnum + 'not found');
	}
};

LuckyRename.enableMoveButton = function(color,bgcolor) {
	if ( document.getElementById('LRprogress') == null || LuckyRename.overridden == 1 ) { //only enable the move button if it hasn't been used yet unless in override mode. the request button could still be enabled after use but this doesn't cause any problems as it would just edit the request if the page hasn't reloaded already
		if ( typeof color == "undefined" ) {
			bgcolor = 'GreenYellow';
		} else {
			bgcolor = color;
		}
		if ( document.getElementById('renameButton') != null && mw.config.get('wgIsProbablyEditable') == true && mw.config.get('wgArticleId') != 0 ) { // if wgArticleId is 0 it's probably a file on Commons
			LuckyRename.renameButton.setDisabled(false);
		}	else if ( document.getElementById('requestButton') != null && mw.config.get('wgIsProbablyEditable') == true && mw.config.get('wgArticleId') != 0 ) {
			LuckyRename.requestButton.setDisabled(false);
		}
	 	document.getElementById("NewFilename").childNodes[0].style.background = bgcolor;
	}
};
LuckyRename.overrideEnableMoveButton = function() {
		if ( document.getElementById('renameButton') != null ) {
			LuckyRename.renameButton.setDisabled(false);
			LuckyRename.renameButton.setLabel(LuckyRename.msgs.renamebuttonlabelOverride);
			LuckyRename.renameButton.setFlags('destructive');
		} else if ( document.getElementById('requestButton') != null ) {
			LuckyRename.requestButton.setDisabled(false);
			LuckyRename.requestButton.setFlags('destructive');
		}
		LuckyRename.declineButton.setDisabled(false);
		LuckyRename.declineButton.setFlags('destructive');
		$( '.LuckyRename' )[0].style.background = '#F99';
		if ( LuckyRename.overridden == 0 ) {
			$( '.LuckyRename' ).append('<br />');
			$( '.LuckyRename' ).append(LuckyRename.resolveRedirectsButton.$element);
			LuckyRename.enableLuckyButton();
			$( '.LuckyRename' ).prepend('<div style="height:0.5em"></div>').prepend(LuckyRename.LuckyUseArticleTitle.$element);
			$( '.LuckyRename' ).prepend('<div style="width: 100%;text-align: center; font-size: 12em; line-height: 0.8;">☠</div>');
		}
		LuckyRename.overridden = 1;
};
LuckyRename.disableMoveButton = function(color) {
			if ( typeof color == "undefined" ) {
				color = '#fee7e6'; //light red
			}
			if ( color != 'nochange' ) {
				document.getElementById("NewFilename").childNodes[0].style.background = color;
			}
			if ( document.getElementById('renameButton') != null ) {
				LuckyRename.renameButton.setDisabled(true);
			} else if ( document.getElementById('requestButton') != null ) {
				LuckyRename.requestButton.setDisabled(true);
			}
};
LuckyRename.disableLuckyButton = function() {
				LuckyRename.luckyButton.setDisabled(true);
};
LuckyRename.enableLuckyButton = function() {
				LuckyRename.luckyButton.setDisabled(false);
};
LuckyRename.updateNewFileProblemHint = function(hint) {
				var intDisplayFilenameError = setInterval(function () {
					clearInterval(intDisplayFilenameError);
					//this element doesn't exist yet when the check runs upon loading (I guess), so this change is made with a 20ms delay.
					document.getElementById("NewFilename").childNodes[3].textContent = hint;
				}, 20);
};
LuckyRename.resolveRedirects = function() {
	LuckyRename.NewFilename.setValue(mw.config.get('wgPageName').replace( /_/g, ' ').replace(/.*:/g, '')); // Ensure whatever filename is entered is ignored. This function essentially "null-moves" the file to it's current location.
	LuckyRename.DoRename();
};
LuckyRename.allowBots = function(text, user){ // this function was copied from https://wiki.riteme.site/wiki/Template:Bots/doc by User:Tóraí, CC BY-SA 3.0
	if (!new RegExp("\\{\\{\\s*(nobots|bots[^}]*)\\s*\\}\\}", "i").test(text)) return true;
	return (new RegExp("\\{\\{\\s*bots\\s*\\|\\s*deny\\s*=\\s*([^}]*,\\s*)*" + user.replace(/([\(\)\*\+\?\.\-\:\!\=\/\^\$])/g, "\\$1") + "\\s*(?=[,\\}])[^}]*\\s*\\}\\}", "i").test(text)) ? false : new RegExp("\\{\\{\\s*((?!nobots)|bots(\\s*\\|\\s*allow\\s*=\\s*((?!none)|([^}]*,\\s*)*" + user.replace(/([\(\)\*\+\?\.\-\:\!\=\/\^\$])/g, "\\$1") + "\\s*(?=[,\\}])[^}]*|all))?|bots\\s*\\|\\s*deny\\s*=\\s*(?!all)[^}]*|bots\\s*\\|\\s*optout=(?!all)[^}]*)\\s*\\}\\}", "i").test(text);
};
LuckyRename.checkTitleInUse = function( checkTitle,fileusageparams ) {
			//For a file that exists locally, query.pages.(filepage ID) is defined and greater than 0.
			//For a file that exists on Commons, query.pages.-1.known is defined but empty. query.pages.-1.missing is also defined but empty.
			//For a filename that doesn't exist on either project, query.pages.-1.known is undefined. query.pages.-1.missing is defined but empty.
			LuckyRename.reportDebugMsg('Checking if entered title is in use');
			checkTitle = mw.config.get('wgCanonicalNamespace') + ':' + checkTitle;
			if ( checkTitle.replace( /_/g, ' ' ) == mw.config.get('wgPageName').replace( /_/g, ' ' ) ) {
				LuckyRename.reportDebugMsg('Entered title is identical to the current title');
				LuckyRename.updateNewFileProblemHint(LuckyRename.msgs.currentname);
				LuckyRename.disableMoveButton('#FFFFCC');
				} else {
	 			fileusageparams = {
				action: 'query',
				titles: checkTitle,
				prop: 'fileusage'
				};
				LuckyRename.doAPICall( fileusageparams, function ( r ) { } );
			}
};
LuckyRename.checkNewFilename = function() {

			// see global MW function wfStripIllegalFilenameChars for details. we also add some original ones that may conflict with various fs or scripts: '?', '!', '"', "'"
			// In Windows filenames / ? < > \ : * | " are illegal. When using FAT fs the caret ^ is also illegal so we exclude all of these. On Mac only the colon : is illegal
			// A period can't be the first character (this would be a hidden file on various *nix) so we disallow that too, but periods are otherwise ok
			// By default, $wgIllegalFileChars includes ':', '/', '\'
			// testLegalChars includes '#', '[', ']', '{', '}', '<', '>', '|' and non-ascii characters /[\\x00-\\x1f\\x7f]/
			// we use /[^\x00-\xFF]/ instead because I have no idea what this means
			LuckyRename.hasIllegalCharacters = LuckyRename.NewFilename.getValue().search( /[^\x00-\xFF]/ );	//search for characters that aren't legal, there should be none
			if ( LuckyRename.hasIllegalCharacters == -1 ) {
				LuckyRename.hasIllegalCharacters = LuckyRename.NewFilename.getValue().search( /[\/\\\:\#\[\]\{\}<>\|\!\?\"\\*\^]/ );	//search for characters that are illegal, there should be none
			}
			if ( LuckyRename.hasIllegalCharacters == -1 ) {
				LuckyRename.hasIllegalCharacters = LuckyRename.NewFilename.getValue().search( /^\./ ); //check if the first character is a period
			}
			LuckyRename.NewFilenameExtension = LuckyRename.NewFilename.getValue().replace( /.*\./g, "");
				
			if ( LuckyRename.hasIllegalCharacters != -1 ) {
				LuckyRename.updateNewFileProblemHint(LuckyRename.msgs.illegalchars);
				LuckyRename.disableMoveButton();
			} else if ( LuckyRename.NewFilenameExtension != LuckyRename.correctFilenameExtension ) {
				LuckyRename.updateNewFileProblemHint(LuckyRename.msgs.badextension);
				LuckyRename.disableMoveButton();
			} else {
				LuckyRename.isAvailable = LuckyRename.checkTitleInUse( LuckyRename.NewFilename.getValue() ); // LuckyRename.checkTitleInUse calls doAPIcall, doAPIcall enables/disables the move button
			}
};
LuckyRename.updateProgressBar = function(percent) {
		if ( document.getElementById('LRprogress') != null ) {
			document.getElementById('LRprogress').children[0].attributes[0].value = 'width: ' + percent + '%';
		}
};
LuckyRename.articletextUrl = mw.config.get('wgServer') + '/wiki/';

if ( mw.config.get('wgServer') == "https://en.wikipedia.beta.wmflabs.org" ) {
	LuckyRename.wikidataApiUrl = 'https://wikidata.beta.wmflabs.org/w/api.php';
	LuckyRename.wikidataInstanceProp = 'P694'; //beta
} else {
	LuckyRename.wikidataApiUrl = 'https://www.wikidata.org/w/api.php';
	LuckyRename.wikidataInstanceProp = 'P31';
}

LuckyRename.fncshadow = "[[WP:FNC#9|Rename files that shadow a Commons file or redirect]]";
// do NOT translate "NEWTITLE" or "RENAMEREASON"
LuckyRename.msgs = {};
LuckyRename.msgs.en = {
	"renamebuttonlabel": "Move file",
	"renamebuttonlabelOverride": "YOLO!",
	"declinebuttonlabel": "Decline",
	"luckybuttonlabel": "Feelin' lucky",
	"overridebuttonlabel": "Override",
	"resolveRedirectsButtonlabel": "Resolve redirects",
	"requestbuttonlabel": "Request move",
	"luckycustomarticlelabel": "Article title to use for Feelin' lucky",
	"preserveredirect": " Preserve redirect",
	"leaveredirect": " Leave redirect",
	"settings": "Settings",
	"settingdebug": "Enable debug/test mode",
	"settingdebugui": "Log debug messages to both console and UI",
	"settingrememberinputs": "Memorize inputs (mostly useful for batches)",
	"settinguncheckleaveredirect": "Uncheck \"leave redirect\" by default (<a href=\"https://wiki.riteme.site/wiki/WP:FMNN\">not recommended</a>)",
	"saveprefs": "Apply and save",
	"deleteLS": "Delete saved settings",
	"creditlink": "<span style=\"float:right; font-size:1.5em\" title=\"20 October 2022\"><a href=\"https://wiki.riteme.site/wiki/User:Alexis_Jazz/LuckyRename\">🍀</a></span>",
	"fncshadow": LuckyRename.fncshadow,
	"fnc1": "[[WP:FNC#1|Uploader requested]]",
	"fnc2": "[[WP:FNC#2|Completely meaningless name]]",
	"fnc3": "[[WP:FNC#3|Correct misleading names into accurate ones]]",
	"fnc4": "[[WP:FNC#4|Change generic bio-names into binomial scientific names]]",
	"fnc5": "[[WP:FNC#5|Obvious error]]",
	"fnc6": "[[WP:FNC#6|Harmonize file names of a set]]",
	"fnc7": "[[WP:FNC#7|Disambiguate overly similar names]]",
	"fnc8": "[[WP:FNC#8|Remove pejorative, offensive or crude language]]",
	"fnc9": LuckyRename.fncshadow,
	"fnclabel1": "1. Uploader requested",
	"fnclabel2": "2. Completely meaningless name",
	"fnclabel3": "3. Correct misleading names into accurate ones",
	"fnclabel4": "4. Change generic bio-names into binomial scientific names",
	"fnclabel5": "5. Obvious error",
	"fnclabel6": "6. Harmonize file names of a set",
	"fnclabel7": "7. Disambiguate overly similar names",
	"fnclabel8": "8. Remove pejorative, offensive or crude language",
	"fnclabel9": "9. Rename files that shadow a Commons file or redirect",
	"defaultreasons": "Default FNC reasons",
	"fail_error": "<span style=\"color:red;font-weight: bold;\">Error: </span>",
	"fail_1": "FAIL 1 for ",
	"fail_2": "FAIL 2 for ",
	"fail_3": "FAIL 3 for ",
	"fail_mwe": "<a href=\"https://phabricator.wikimedia.org/T235589\" class=\"plainlinks\">T235589</a>? ",
	"failretry": "Retrying..",
	"failgiveup": "Giving up.",
	"stacktracedetails": "Stack trace / details",
	"edited": "Edited ",
	"edit": "Edit ",
	"unknownstatus": "Unknown status",
	"moved": "Moved",
	"move": "Move",
	"to": " to ",
	"redirtagspeedy1": "Tag redirect at ",
	"redirtagspeedy2": " for speedy deletion",
	"summaryoverride": " (override)",
	"summaryrenamed": "Renamed",
	"summaryfile": "file",
	"summaryto": "to",
	"summaryfilepage": "Removing template; rename done ",
	"summaryredirectshadow": "Tagging redirect after move per [[WP:FNC#9]] (files that shadow a Commons file or redirect)",
	"summaryredirectother": "Tagging redirect after move per [[WP:PMRC#10]] (little-used files for which all uses have been replaced)",
	"summarydeclined": "[[Wikipedia:File names#What files should not be renamed?|Rename declined]]: ",
	"summaryrequestmove": "Request renaming this file to ",
	"tagredirect": "Tagging redirect for speedy deletion",
	"unabletagredirect": "Unable to tag redirect: could not detect redirect (text: ",
	"renamedeclined": "Rename declined",
	"renamerequested": "Rename requested, reloading page",
	"pleasewait": "Just a moment..",
	"renamerequestedfailed": "Rename request failed.",
	"suggestion": "Suggestion ",
	"suggestionfound": " already found in article title!",
	"skipping": "Skipping",
	"toolcredit": " ([[w:en:User:Alexis Jazz/LuckyRename|LuckyRename]])",
	"redirectspeedyshadow": "{{Db-r4}}",
	"redirectspeedyother": "{{Db-g6|rationale=it is little-used and all uses have been replaced ([[WP:PMRC#10]]) ([[User:Alexis Jazz/LuckyRename|LuckyRename]])}}",
	"requesttemplate": "{{Rename media|1=NEWTITLE|2=RENAMEREASON}}",
	"ns6": "File:",
	"book": "book",
	"postlogo": " logo",
	"postseal": " seal",
	"postscreenshot": " screenshot",
	"postcover": " cover",
	"postposter": " poster",
	"postuniform": " uniform",
	"existsoncommons": "Exists on Commons",
	"existslocally": "Exists locally",
	"currentname": "Current name",
	"illegalchars": "Illegal characters",
	"badextension": "Bad extension",
	"fileonsharedrepo": "<span style=\"color:red;font-weight: bold;\">This file probably resides on Commons. Try moving it there.</span>",
	"filemaybeprotected": "<span style=\"color:red;font-weight: bold;\">This file appears to be protected.</span>",
	"pending": "Pending",
	"done": "Done",
	"disallowed": ", disallowed by {{<a class=\"plainlinks\" href=\"/wiki/Template:Bots\">Bots</a>}}",
	"LuckyRenameActionCompleted": "Done",
	"actionFilenamenotfound": "Filename not found",
	"actionAutomaticallyGenerated": "Page generated by automated process",
	"LuckyRenameActionSkippedTooManyUses": "File is used on 7 or more pages, not tagging the redirect for deletion.",
	"LuckyRenameActionFailed": "FAILED",
	"LuckyRenameActionFailed1": "FAILED (debug code 1)",
	"LuckyRenameActionFailed2": "FAILED (debug code 2)",
	"LuckyRenameActionFailed3": "FAILED (debug code 3)",
	"LuckyRenameActionFailed4": "FAILED (debug code 4)",
	"LuckyRenameActionFailedMWE": "Encountered MWException, possibly <a href=\"https://phabricator.wikimedia.org/T235589\" class=\"plainlinks\">T235589</a>. ",
	"LuckyRenameActionFailedMissingTitle": "Missing title",
	"LuckyRenameActionFailedOther": "Encountered an error",
	"LuckyRenameActionFailedRetry": "Retrying..",
	"LuckyRenameActionFailedGaveUp": "Giving up.",
	"LuckyRenameActionSkippedDisallowed": "Skipped, disallowed by {{<a class=\"plainlinks\" href=\"/wiki/Template:Bots\">Bots</a>}}",
	"actionDisallowed": ", disallowed by {{<a class=\"plainlinks\" href=\"/wiki/Template:Bots\">Bots</a>}}",
	"LuckyRenameActionSkipped" : "Skipped",
	"deletedoncommons": "{{Deleted on Commons",
	"noWikidataItem": " has no Wikidata item! Maybe <a href=\"https://www.wikidata.org/wiki/Special:NewItem\">create it</a>? (hit the button again to try the next article if there is one)",
	"resolvingRedirects": "Resolving redirects for moved file",
	"MoveReason":"Additional reasoning for move",
};

LuckyRename.msgs = LuckyRename.msgs.en; // only English for now

if ( mw.config.get('wgDBname') == 'enwiki' ) {
	LuckyRename.automaticallyGeneratedTemplate = new RegExp('{{[\s]*[Aa]utomatically[ _]generated(?![^}]*allow[\s]*=[^\|}]*luckyrename)', '');
} else {
	LuckyRename.automaticallyGeneratedTemplate = new RegExp('{{[\s]*[Aa]utomatically[ _]generated(?![^}]*allow[\s]*=[^\|}]*luckyrename)', '');
}

//this helps to cut down on Wikidata requests, makes lucky more responsive can also be used to overrule strange wikidata naming
LuckyRename.commonLabels = {};
LuckyRename.commonLabels.en = {
	'Q47461344': LuckyRename.msgs.book, //written work, but "book" is better for filenames
	'Q7725634': LuckyRename.msgs.book, //literary work, but "book" is better for filenames
	'Q15632617': 'fictional character', //fictional human, but "fictional character" is better for filenames
	'Q1792450': 'art historian',
	'Q1622272': 'university teacher',
	'Q5': 'human',
	'Q33999': 'actor',
	'Q10798782': 'television actor',
	'Q10800557': 'film actor',
	'Q177220': 'singer',
	'Q488205': 'singer-songwriter',
	'Q36834': 'composer',
	'Q639669': 'musician',
	'Q49757': 'poet',
	'Q482980': 'author',
	'Q36180': 'writer',
	'Q201788': 'historian',
	'Q644687': 'illustrator',
	'Q37226': 'teacher',
	'Q482994': 'album',
	'Q11424': 'film',
	'Q134556': 'single',
	'Q7366': 'song',
	'Q179700': 'statue',
	'Q4830453': 'business',
	'Q7889': 'video game',
	'Q245068': 'comedian',
	'Q18545066': 'stand-up comedian',
	'Q21191270': 'television series episode',
	'Q21198342': 'manga series',
	'Q28389': 'screenwriter',
	'Q2526255': 'film director',
	'Q11032': 'newspaper',
	'Q476028': 'association football club',
	'Q44529188': 'town',
	'Q7397': 'software',
	'Q18127': 'record label',
	'Q9826': 'high school',
	'Q3918': 'university',
	'Q5398426': 'television series',
	'Q46970': 'airline',
	'Q18536800': 'mixed martial arts event',
	'Q215380': 'musical group',
	'Q15275719': 'recurring event',
	'Q15416': 'television program',
	'Q4671277': 'academic institution',
	'Q163740': 'nonprofit organization',
	'Q6625963': 'novelist',
	'Q2405480': 'voice actor',
	'Q2259451': 'stage actor',
	'Q1028181': 'painter'
};
LuckyRename.commonLabels = LuckyRename.commonLabels.en; // only English for now
LuckyRename.RenameTemplate = /\{\{(?:rename[_ ]media|rename[_ ]image|image[_ ]move|rename-image|media[_ ]rename|renamemedia|rename[_ ]file|shadowscommons|shadows[_ ]commons)\s*.*?\}\}(?:\n)?/gi;
LuckyRename.RenameTemplateReason = /.*\{\{(?:rename[_ ]media|rename[_ ]image|image[_ ]move|rename-image|media[_ ]rename|renamemedia|rename[_ ]file|shadowscommons|shadows[_ ]commons)\s*\|[^|]*\|(2\=|)(.*)\}\}(?:\n)?/i;

LuckyRename.totalActionsTodo = 0;
LuckyRename.totalActionsDone = 0;
LuckyRename.overridden = 0;
LuckyRename.correctFilenameExtension = mw.config.get('wgPageName').replace( /.*\./g , '').replace( /^jpeg$/i, 'jpg').replace( /^jpg$/i, 'jpg').replace( /^png$/i, 'png').replace( /^gif$/i, 'gif').replace( /^tif$/i, 'tif').replace( /^tiff$/i, 'tif').replace( /^svg$/i, 'svg');
LuckyRename.wikidataprop = { "entities": { } }; //empty object

	LuckyRename.doAPICall = function( params,i,mode ) {
		params = $.extend( {
			action: 'query',
			format: 'json'
		}, params );

		i = 0;
			LuckyRename.callback = function ( jqXHR, textStatus, errorThrown ) {

				if ( typeof params.prop != "undefined" && typeof params.titles != "undefined" && params.prop == 'fileusage' && params.titles == 'File:' + LuckyRename.NewFilename.getValue() ) {
					LuckyRename.reportDebugMsg('received usage info for '+params.titles);

					LuckyRename.usageResponse = jqXHR;
					if ( typeof LuckyRename.usageResponse != "undefined" && typeof LuckyRename.usageResponse.query != "undefined" && LuckyRename.usageResponse.query.pages != "undefined" ) {
					LuckyRename.usagepageID = Object.keys(LuckyRename.usageResponse.query.pages)[0];
						if ( LuckyRename.usagepageID == "-1" && typeof LuckyRename.usageResponse.query.pages[-1].known != "undefined") {
							LuckyRename.updateNewFileProblemHint(LuckyRename.msgs.existsoncommons);
							LuckyRename.disableMoveButton();
						} else if ( LuckyRename.usagepageID == "-1" && typeof LuckyRename.usageResponse.query.pages[-1].missing != "undefined") {
							document.getElementById("NewFilename").childNodes[3].textContent = ' ';
							LuckyRename.enableMoveButton();
						} else if ( LuckyRename.usagepageID > 0 ) {
							LuckyRename.updateNewFileProblemHint(LuckyRename.msgs.existslocally);
							LuckyRename.disableMoveButton();
						} else {
							LuckyRename.reportDebugMsg('Could not determine if ' + params.titles + ' is free');
						}
					}
				} else if (typeof jqXHR.warnings != "undefined" && typeof jqXHR.warnings.main != "undefined") {
					LuckyRename.reportDebugMsg('API warning:');
					LuckyRename.reportDebugMsg(jqXHR.warnings);
					if ( params.action == 'move' ) {
						LuckyRename.updateMoveStatus(params.from, 'LuckyRenameActionFailed', LuckyRename.msgs.LuckyRenameActionFailed1);
					} else {
						LuckyRename.updateReplaceUsageStatus(params.title, 'LuckyRenameActionFailed', LuckyRename.msgs.LuckyRenameActionFailed1);
					}

				} else if (typeof jqXHR.edit != "undefined" && typeof jqXHR.edit.result != "undefined" && jqXHR.edit.result == 'Success') { // edit succeeded
					LuckyRename.reportDebugMsg(jqXHR);
					LuckyRename.reportDebugMsg(params);
					LuckyRename.reportDebugMsg('edited '+params.title);
					LuckyRename.reportDebugMsg('edited '+jqXHR.edit.title);
					LuckyRename.reportDebugMsg(mode);
					if ( typeof LuckyRename.renameRequestWikitext == "undefined" && mode != 'decline' ) { // there is no progress bar or status table when requesting
						LuckyRename.updateReplaceUsageStatus(jqXHR.edit.title, 'LuckyRenameActionCompleted');
						LuckyRename.totalActionsDone++; // +1
						LuckyRename.updateProgressBar(100 / LuckyRename.totalActionsTodo * LuckyRename.totalActionsDone);
					} else if ( mode == 'request' ) {
						$( '.LuckyRename' ).append('<br>' + LuckyRename.msgs.renamerequested);
						var myIntReloadRequested = setInterval(function () {
							clearInterval(myIntReloadRequested);
							location.reload();
						}, 3000);
					}
				} else if (typeof jqXHR.edit != "undefined" && typeof jqXHR.edit.result != "undefined") { 
					if ( params.action == 'move' ) {
						LuckyRename.reportDebugMsg('moved '+params.title);
						LuckyRename.updateMoveStatus(params.from, 'LuckyRenameActionFailed', LuckyRename.msgs.LuckyRenameActionFailed2);
					} else {
						LuckyRename.reportDebugMsg('no edit done for '+params.title);
						LuckyRename.updateReplaceUsageStatus(params.title, 'LuckyRenameActionFailed', LuckyRename.msgs.LuckyRenameActionFailed2);
					}
				} else if (typeof jqXHR.error != "undefined" && typeof jqXHR.error.code != "undefined") { // API action failed
					if ( i < 5 ) {							//number of retries in case of failure
						window.setTimeout( LuckyRename.doCall, 3000 );
						i++;
						LuckyRename.retrymsg = LuckyRename.msgs.failretry;
					} else {
						i++;
						LuckyRename.retrymsg = LuckyRename.msgs.failgiveup;
					}
					if ( jqXHR.error.code == 'internal_api_error_MWException' ) {
						LuckyRename.explainError = LuckyRename.msgs.fail_mwe;
					} else if ( jqXHR.error.code == 'missingtitle' ) {
						LuckyRename.explainError = LuckyRename.msgs.LuckyRenameActionFailedMissingTitle;
					} else {
						LuckyRename.explainError = '';
					}
					
					//$( '.LuckyRename' ).append('<br /><span class="LR_MWE_error' + i + '" style="color: red;font-weight: bold;">' + jqXHR.error.info.replace( /\[\[:?([^\]]*)\]\]/g, '<a href="$1">$1<\/a>') + ' (code: ' + jqXHR.error.code + ') ' + LuckyRename.explainError + LuckyRename.retrymsg + '</span>');
					LuckyRename.stackTracePopup = new OO.ui.PopupButtonWidget( {
						icon: 'error',
						framed: false,
						$content: LuckyRename.msgs.fail_error + jqXHR.error.info.replace( /\[\[:?([^\]]*)\]\]/g, '<span class="plainlinks"><a href="$1">$1<\/a><\/span>') + ' (code: ' + jqXHR.error.code + ') ' + LuckyRename.explainError + ' - ' + LuckyRename.retrymsg,
						invisibleLabel: false,
						popup: {
						head: true,
							icon: 'infoFilled',
							label: LuckyRename.msgs.stacktracedetails,
							$content: $( '<p>' + jqXHR.error["*"].replace( /\n/g , '<br />') + '</p>'), //jqXHR.error["*"].replace( '\n', '<br />')
							padded: true,
							align: 'force-right'
						}
					} );
					if ( jqXHR.error.code == 'missingtitle' && params.title == 'File:' + LuckyRename.NewFilename.getValue()) { //missingtitle is very common as this always fails until the file has been moved
						LuckyRename.reportDebugMsg('file not moved yet, can\'t update file page');
					} else {
						$( '.LuckyRename' ).append(LuckyRename.stackTracePopup.$element);
						if ( params.action == 'move' ) {
							LuckyRename.updateMoveStatus(params.from, 'LuckyRenameActionFailed' );
							$("#movePageStatusMsg_" + encodeURIComponent(params.from).replace( /[%\._]/g, '')).append(LuckyRename.stackTracePopup.$element);
						} else if ( typeof LuckyRename.renameRequestWikitext != "undefined" ) { // this is a request, no progress indicator or table loaded
							$( '.LuckyRename' ).append('<br>' + LuckyRename.msgs.renamerequestedfailed).append(LuckyRename.stackTracePopup.$element);
						} else { // usage replacement that failed
							LuckyRename.updateReplaceUsageStatus(params.title, 'LuckyRenameActionFailed', jqXHR.error.info.replace( /\[\[:?([^\]]*)\]\]/g, '<a href="$1">$1<\/a>') + ' (code: ' + jqXHR.error.code + ') ');
							$("#replaceusageStatusMsg_" + encodeURIComponent(params.from).replace( /[%\._]/g, '')).append(LuckyRename.stackTracePopup.$element);
						}
					}
				} else if (typeof jqXHR.move != "undefined" && typeof jqXHR.move.from != "undefined" && typeof jqXHR.move.to != "undefined") { // move succeeded
					LuckyRename.updateMoveStatus(params.from, 'LuckyRenameActionCompleted');
					LuckyRename.totalActionsDone++; // +1
					LuckyRename.updateProgressBar(100 / LuckyRename.totalActionsTodo * LuckyRename.totalActionsDone);
				} else {
				LuckyRename.reportDebugMsg(LuckyRename.msgs.unknownstatus);
				}
			};
		LuckyRename.doCall = function () {
			LuckyRename.api.postWithEditToken( params ).then( function ( data ) {

				LuckyRename.callback(data);

			}, function ( code, data ) {
				//fail
			});
		};
		LuckyRename.doCall();
	};

LuckyRename.getUsageAndRedir = function(int,filepageText) {
	LuckyRename.redirectlinksdone[int] = 1;
	LuckyRename.api.get( {'action':'query','format':'json','prop':'linkshere','lhlimit':'500','titles': LuckyRename.linkshere1[int].title} ).then( function ( data ) {
		if (typeof data != "undefined" && typeof data.query != "undefined" && typeof data.query.pages != "undefined" ) {
			LuckyRename.sourcePageID = Object.keys(data.query.pages)[0];
			if (typeof data.query.pages[ LuckyRename.sourcePageID ] != "undefined" && typeof data.query.pages[ LuckyRename.sourcePageID ].linkshere != "undefined") {
				LuckyRename.redirectlinks[int] = data.query.pages[ LuckyRename.sourcePageID ].linkshere;
				LuckyRename.linkshere2 = LuckyRename.linkshere2.concat(LuckyRename.redirectlinks[int]);
			}
		}
		LuckyRename.api.get( {'action':'query','format':'json','prop':'fileusage','fulimit':'500','titles':LuckyRename.linkshere1[int].title} ).then( function ( data ) {
			if (typeof data != "undefined" && typeof data.query != "undefined" && typeof data.query.pages[ mw.config.get('wgArticleId') ] != "undefined" && typeof data.query.pages[ mw.config.get('wgArticleId') ].fileusage != "undefined") {
	 			LuckyRename.sourcePageID = Object.keys(data.query.pages)[0];
	 			LuckyRename.redirectusage[int] = data.query.pages[ LuckyRename.sourcePageID ].fileusage;
				LuckyRename.fileUsages2 = LuckyRename.fileUsages2.concat(LuckyRename.redirectusage[int]);
			}
			if ( int == LuckyRename.linkshere1.length-1 ) { //last entry
				LuckyRename.DoRename(filepageText,1);
			}
		});
	});
};

LuckyRename.redirectlinks = {};
LuckyRename.redirectlinksdone = {};
LuckyRename.redirectusage = {};
LuckyRename.redirectusagedone = {};

LuckyRename.DoRename = function (filepageText,skipLinksHere,int,int2,int3){

	if ( typeof filepageText == 'undefined' ) {
		LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
			LuckyRename.reportDebugMsg('got filepageText for DoRename');
			LuckyRename.filepageText = LuckyRename.getWikitextFromExport(data.query.export['*']); 
			if ( LuckyRename.filepageText ) {
				LuckyRename.DoRename(LuckyRename.filepageText);
			} else {
				LuckyRename.reportDebugMsg('extraction of wikitext failed for '+mw.config.get('wgPageName'));
			}
		});
		return;
	}

	LuckyRename.disableMoveButton('nochange'); //no need to use this button anymore, we've started, but don't change the bgcolor of the filename field
	LuckyRename.saveSettings(); //save move reason for memorize inputs
	if ( document.getElementById('LRprogress') == null ) {
		$( '.LuckyRename' ).append(LuckyRename.ProgressBar.$element);
	}

	LuckyRename.totalActionsDone = 0;
	LuckyRename.updateProgressBar(0);
	LuckyRename.oldFile = LuckyRename.OriginalFilename.getValue();
	LuckyRename.newFile = 'File:' + LuckyRename.NewFilename.getValue();

	if ( LuckyRename.MoveReasonSelect.getValue() != "" && LuckyRename.MoveReason.getValue() != "" ) {
		LuckyRename.MoveReasonSummary = LuckyRename.MoveReasonSelect.getValue() + ", " + LuckyRename.MoveReason.getValue();
	} else {
		LuckyRename.MoveReasonSummary = LuckyRename.MoveReasonSelect.getValue() + LuckyRename.MoveReason.getValue();
	}

	LuckyRename.Summary = LuckyRename.msgs.summaryrenamed + ' [[:' + LuckyRename.oldFile.replace( /_/g, ' ') + '|' + LuckyRename.msgs.summaryfile + ']] ' + LuckyRename.msgs.summaryto + ' [[:' + LuckyRename.newFile + ']]: ' + LuckyRename.MoveReasonSummary + LuckyRename.msgs.toolcredit;
	LuckyRename.SummaryFilePage = LuckyRename.msgs.summaryfilepage + LuckyRename.msgs.toolcredit;
	LuckyRename.SummaryForMove = LuckyRename.MoveReasonSummary + LuckyRename.msgs.toolcredit;

	//replacement of interwiki links that aren't interwiki links (e.g. [[:w:File:Example.jpg]] on English Wikipedia), more sites could be added as needed, let me know if you use LuckyRename on another project
	if ( mw.config.get('wgSiteName') == 'Wikipedia' && mw.config.get('wgContentLanguage') == 'en' ) {
		LuckyRename.interWiki = '|(\[\[:?[Ww]:|\[\[:?[Ee][Nn]:|\[\[:?[Ww]:[Ee][Nn]:|\[\[:?[Ee][Nn]:[Ww]:)\\b[^:]+:';
	} else if ( mw.config.get('wgSiteName') == 'Wikipedia' ) {
		LuckyRename.interWiki = '|(\[\[:?[Ww]:' + mw.config.get('wgContentLanguage') + ':|\[\[:?' + mw.config.get('wgContentLanguage') + ':)\\b[^:]+:'; //does not catch e.g. [[:JA:Example.jpg]] but I don't care
	} else if ( mw.config.get('wgSiteName') == 'Wikimedia Commons' ) { //to test LuckyRename on betacommons
		LuckyRename.interWiki = '|\[\[:?[Cc]:\\b[^:]+:';
	} else if ( mw.config.get('wgSiteName') == 'Meta' ) {
		LuckyRename.interWiki = '|\[\[:?[Mm]:\\b[^:]+:';
	}

	LuckyRename.oldFileReplaceFirstChar = LuckyRename.escapeRegExp(LuckyRename.oldFile).replace( /File:/, '').slice(0, 1).toUpperCase() + LuckyRename.escapeRegExp(LuckyRename.oldFile).replace( /File:/, '').slice(0, 1).toLowerCase();
	LuckyRename.oldFileReplaceRest = LuckyRename.escapeRegExp(LuckyRename.oldFile).replace ( /File:/, '').slice(1, LuckyRename.escapeRegExp(LuckyRename.oldFile).length);
	LuckyRename.oldFileReplace = new RegExp('([\n=\|\^]\\s*|([^a-z]:|[^:])\\b[^:]+:' + LuckyRename.interWiki + ')[' + LuckyRename.oldFileReplaceFirstChar + ']' + LuckyRename.oldFileReplaceRest.replace( /[_ ]/g, '[_ ]'), "g");
	LuckyRename.newFileReplace = LuckyRename.newFile.replace( /File:/, '').replace ( /_/g, ' ').slice(0, 1).toUpperCase() + LuckyRename.newFile.replace( /File:/, '').replace( /_/g, ' ').slice(1, LuckyRename.newFile.replace( /File:/, '').length);
	// Explain regexp part 1 (filenames without a namespace) [\n=\|\^]\\s?
	// \n : filename preceded by a newline (typical for <gallery> usage)
	// = : filename preceded by an equals sign (typical for use as a named template argument)
	// | : filename preceded by a pipe (typical for use as an unnamed template argument)
	// \^ : filename preceded by start-of-document (unlikely, perhaps if someone created a talk page without a header and started with "Example.jpg is a good image", it would have to be linked later on to get detected by linkshere though)
	// \\s? : whitespace character that may or may not exist between the above and the filename
	// part 2 (filenames with a namespace) ([^a-z]:|[^:])\\b[^:]+:
	// ([^a-z]:|[^:]) : non-letter character plus a colon or a non-colon character. This ensures c:File:Example.jpg won't match but [[:File:Example.jpg]] or File:Example.jpg on a new line (common in <gallery> usage) will
	// \\b[^:]+: : beginning of a word (does not match colons, asterisks, etc), any character that isn't a colon, colon (this matches any namespace, e.g. File:, Image:, Datei:, ファイル:, etc)

	if ( LuckyRename.overridden == 1 ) {
		LuckyRename.Summary = LuckyRename.Summary + LuckyRename.msgs.summaryoverride;
		LuckyRename.SummaryFilePage = LuckyRename.SummaryFilePage + LuckyRename.msgs.summaryoverride;
		LuckyRename.SummaryForMove = LuckyRename.SummaryForMove + LuckyRename.msgs.summaryoverride;
	}
	if ( LuckyRename.oldFile == LuckyRename.newFile && LuckyRename.MoveReason.getValue() != "" ) { //no actual move, just resolving redirects
		LuckyRename.Summary = LuckyRename.msgs.resolvingRedirects + ' [[:' + LuckyRename.newFile + '|' + LuckyRename.newFile.replace(/.*:/, '') + ']]: ' + LuckyRename.MoveReason.getValue() + LuckyRename.msgs.toolcredit;
	} else if ( LuckyRename.oldFile == LuckyRename.newFile ) {
		LuckyRename.Summary = LuckyRename.msgs.resolvingRedirects + ' [[:' + LuckyRename.newFile + '|' + LuckyRename.newFile.replace(/.*:/, '') + ']]' + LuckyRename.msgs.toolcredit;
	}

	if ( typeof LuckyRename.linkshere1 == 'undefined' && !LuckyRename.linkshere1done ) {
		LuckyRename.linkshere1done = 1;
		LuckyRename.api.get( {'action':'query','format':'json','prop':'linkshere','lhlimit':'500','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
			if ( data.query != "undefined" && typeof data.query.pages != "undefined" ) {
				LuckyRename.sourcePageID = Object.keys(data.query.pages)[0];
				if (typeof data.query.pages[ LuckyRename.sourcePageID ] != "undefined" && typeof data.query.pages[ LuckyRename.sourcePageID ].linkshere != "undefined") {
					LuckyRename.linkshere1 = data.query.pages[ LuckyRename.sourcePageID ].linkshere;
				}
			}
			LuckyRename.DoRename(filepageText);
		});
		LuckyRename.reportDebugMsg('exit linkshere1');
		return;
	}

	if ( typeof LuckyRename.linksheretalk == 'undefined' && !LuckyRename.linksheretalkdone ) {
		LuckyRename.api.get( {'action':'query','format':'json','prop':'linkshere','lhlimit':'500','titles': 'File_talk:'+mw.config.get('wgTitle')} ).then( function ( data ) {
			LuckyRename.linksheretalkdone = 1;
			if (typeof data != "undefined" && typeof data.query != "undefined" && typeof data.query.pages != "undefined" ) {
				LuckyRename.sourcePageID = Object.keys(data.query.pages)[0];
				if (typeof data.query.pages[ LuckyRename.sourcePageID ] != "undefined" && typeof data.query.pages[ LuckyRename.sourcePageID ].linkshere != "undefined") {
					LuckyRename.linksheretalk = data.query.pages[ LuckyRename.sourcePageID ].linkshere;
				}
			}
			LuckyRename.DoRename(filepageText);
		});
		LuckyRename.reportDebugMsg('exit linksheretalk');
		return;
	}

	if ( typeof LuckyRename.fileUsages1 == 'undefined' && !LuckyRename.fileUsages1done ) {
		LuckyRename.api.get( {'action':'query','format':'json','prop':'fileusage','fulimit':'500','titles':mw.config.get('wgPageName')} ).then( function ( data ) {
			LuckyRename.fileUsages1done = 1;
			if (typeof data != "undefined" && typeof data.query != "undefined" && typeof data.query.pages[ mw.config.get('wgArticleId') ] != "undefined" && typeof data.query.pages[ mw.config.get('wgArticleId') ].fileusage != "undefined") {
	 			LuckyRename.sourcePageID = Object.keys(data.query.pages)[0];
	 			LuckyRename.fileUsages1 = data.query.pages[ LuckyRename.sourcePageID ].fileusage;
			}
			LuckyRename.DoRename(filepageText);
		});
		LuckyRename.reportDebugMsg('exit fileUsages1');
		return;
	}

	if ( typeof LuckyRename.linkshere1 == "undefined" ) { LuckyRename.linkshere1 = []; }
	if ( typeof LuckyRename.linksheretalk == "undefined" ) { LuckyRename.linksheretalk = []; }
	if ( typeof LuckyRename.fileUsages1 == "undefined" ) { LuckyRename.fileUsages1 = []; }
	LuckyRename.linkshere2 = LuckyRename.linkshere1;
	LuckyRename.fileUsages2 = LuckyRename.fileUsages1;

	LuckyRename.waitforredirectinfo = 0;

	for (int = 0; int < LuckyRename.linkshere1.length; int++) {
		if ( typeof LuckyRename.linkshere1[int].redirect != "undefined" ) {
			if ( typeof LuckyRename.redirectlinks[int] == 'undefined' && !LuckyRename.redirectlinksdone[int] ) {
				LuckyRename.waitforredirectinfo = 1;
				LuckyRename.getUsageAndRedir(int,filepageText);
			}
		}
	}

	if ( LuckyRename.waitforredirectinfo ) {
		return;
	}

	LuckyRename.allUsages = LuckyRename.linkshere2.concat(LuckyRename.fileUsages2).concat(LuckyRename.linksheretalk);

	LuckyRename.filepageText = filepageText;
	LuckyRename.filepageTextNew = LuckyRename.filepageText.replace(LuckyRename.RenameTemplate,'').replace( /^[\s]*/, ''); // remove {{Rename media}}, also remove whitespace/newlines from the top
	if ( LuckyRename.filepageText.search( LuckyRename.msgs.deletedoncommons ) == -1 ) { // if the page contains a {{Deleted on Commons}} template replacement is more likely to do bad than good
		for (int = 0; int < LuckyRename.allUsages.length; int++) { // go over usages 
			if ( LuckyRename.allUsages[int].title == LuckyRename.oldFile ) { //if the file is listed on fileusages or linkshere perform link replacement. Note that links to self are not reported by linkshere (T290145)
				LuckyRename.filepageTextNew = LuckyRename.filepageTextNew.replace( LuckyRename.oldFileReplace, '$1' + LuckyRename.escapeReplacement(LuckyRename.newFileReplace) );
				for (int2 = 0; int2 < LuckyRename.allUsages.length; int2++) {
					if ( typeof LuckyRename.allUsages[int2].redirect != "undefined" ) {
						LuckyRename.redirectTitle = LuckyRename.allUsages[int2].title;
						LuckyRename.redirectFileReplaceFirstChar = LuckyRename.escapeRegExp(LuckyRename.redirectTitle).replace( /File:/, '').slice(0, 1).toUpperCase() + LuckyRename.escapeRegExp(LuckyRename.redirectTitle).replace( /File:/, '').slice(0, 1).toLowerCase();
						LuckyRename.redirectFileReplaceRest = LuckyRename.escapeRegExp(LuckyRename.redirectTitle).replace( /File:/, '').slice(1, LuckyRename.escapeRegExp(LuckyRename.redirectTitle).length);
						LuckyRename.redirectFileReplace = new RegExp('([\n=\|\^]\\s?|([^a-z]:|[^:])\\b[^:]+:)[' + LuckyRename.redirectFileReplaceFirstChar + ']' + LuckyRename.redirectFileReplaceRest.replace( /[_ ]/g, '[_ ]'), "g");
						LuckyRename.filepageTextNew = LuckyRename.filepageTextNew.replace( LuckyRename.redirectFileReplace, '$1' + LuckyRename.escapeReplacement(LuckyRename.newFileReplace));
					}
				}
			}
		}
	}
	if ( LuckyRename.filepageText.search( LuckyRename.RenameTemplate ) == -1 ) {	// adjust summary if there is no {{Rename media}} but a link on the page needs to be updated
		LuckyRename.SummaryFilePage = LuckyRename.Summary;
	}
	LuckyRename.movedata = {
		action: 'move',
		from: LuckyRename.oldFile,
		to: LuckyRename.newFile,
		reason: LuckyRename.SummaryForMove,
		movetalk: 1,
		watchlist: 'nochange',
	};

	if (LuckyRename.LeaveRedirectCheck.isSelected() == false && LuckyRename.UserCanSuppressRedirects() ) {
		LuckyRename.movedata = $.extend( {
			noredirect: 'true',
		}, LuckyRename.movedata);
	}
	LuckyRename.filepagedata = {
		action: 'edit',
		minor: '1',
		title: LuckyRename.newFile,
		nocreate: true,
		summary: LuckyRename.SummaryFilePage,
		text: LuckyRename.filepageTextNew,
		watchlist: 'nochange',
		//tags: '',
		//token: LuckyRename.getToken(),	// filepage needs a new token to prevent edit conflict
	};
	if ( LuckyRename.movedata.from != LuckyRename.movedata.to ) {
		LuckyRename.totalActionsTodo = LuckyRename.allUsages.length + 1; // +1 for the actual move
	} else {
		LuckyRename.totalActionsTodo = LuckyRename.allUsages.length; //only move if to and from are different. They can be the same when using override to resolve redirects in usage
	}
	if ( LuckyRename.filepageText != LuckyRename.filepageTextNew ) {
		LuckyRename.totalActionsTodo++; // +1 for the update to the filepage itself
	}

	LuckyRename.queueTableMoveRow = '<tr><td class="LuckyRenamePendingBlink LuckyRenameMoveStatusCell" id="movePage_' + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.pending + '</td><td id="movePageStatusMsg_' + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '') + '"></td><td id="movePageDescription_' + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.move + ' ' + LuckyRename.msgs.to + ' <a href="' + mw.config.get('wgServer') + '/wiki/' + encodeURIComponent(LuckyRename.newFile) + '">' + LuckyRename.newFile + '</a>' + '</td></tr>';
	LuckyRename.queueTableRedirSpeedyRow = '<tr><td class="LuckyRenamePendingBlink LuckyRenameReplaceStatusCell" id="replaceusage_' + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.pending + '</td><td id="replaceusageStatusMsg_' + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '') + '"></td><td id="replaceusageDescription_' + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.redirtagspeedy1 + ' <a href="' + mw.config.get('wgServer') + '/wiki/' + encodeURIComponent(LuckyRename.oldFile) + '">' + LuckyRename.oldFile + '</a>' + LuckyRename.msgs.redirtagspeedy2 + '</td></tr>';
	LuckyRename.queueTableFilePageRow = '<tr><td class="LuckyRenamePendingBlink LuckyRenameReplaceStatusCell" id="replaceusage_' + encodeURIComponent(LuckyRename.newFile).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.pending + '</td><td id="replaceusageStatusMsg_' + encodeURIComponent(LuckyRename.newFile).replace( /[%\._]/g, '') + '"></td><td id="replaceusageDescription_' + encodeURIComponent(LuckyRename.newFile).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.edit + '<a href="' + mw.config.get('wgServer') + '/wiki/' + encodeURIComponent(LuckyRename.newFile) + '">' + LuckyRename.newFile + '</a></td></tr>';
	LuckyRename.queueTable = '<table>';
	if ( LuckyRename.movedata.from != LuckyRename.movedata.to ) { //only move if to and from are different. They can be the same when using override to resolve redirects in usage
		LuckyRename.queueTable = LuckyRename.queueTable + LuckyRename.queueTableMoveRow;
	}
	if ( ( LuckyRename.MoveReasonSelect.getValue() == LuckyRename.msgs.fncshadow && LuckyRename.LeaveRedirectCheck.isSelected() ) || ( ! LuckyRename.UserCanSuppressRedirects() && ! LuckyRename.LeaveRedirectCheck.isSelected() ) ) { //only add a row if a redirect is left that needs to be speedied
		LuckyRename.queueTable = LuckyRename.queueTable + LuckyRename.queueTableRedirSpeedyRow;
	}
	if ( LuckyRename.filepageText != LuckyRename.filepageTextNew ) { //only add a row for the file page if we're actually going to edit it
		LuckyRename.queueTable = LuckyRename.queueTable + LuckyRename.queueTableFilePageRow;
	}
	LuckyRename.allUsagesArray = []; //initialize empty array
	LuckyRename.allUsages.push({pageid: 0,ns:7,title:'File talk:'+LuckyRename.NewFilename.getValue()});

	for (int = 0; int < LuckyRename.allUsages.length; int++) {
		if ( LuckyRename.allUsagesArray.indexOf(LuckyRename.allUsages[int].title) == -1 && LuckyRename.allUsages[int].title != LuckyRename.oldFile ) { //not a duplicate and not the move target which is processed by DoEditPages. duplicates happen when one page links AND uses an image
			LuckyRename.allUsagesArray.push(LuckyRename.allUsages[int].title); //add title to our array so we'll know if we hit a duplicate
			if ( typeof LuckyRename.allUsages[int].redirect != "undefined" ) {
				LuckyRename.usageRedirectIndicator = '<span style="font-weight:bold;"> R</span>';
				LuckyRename.usageNoRedirect = '?redirect=no';
			} else {
				LuckyRename.usageRedirectIndicator = '';
				LuckyRename.usageNoRedirect = '';
			}
			// File talk page with the same name as the current page. 99% false positives, we'll process this anyway because the file page might link the corresponding file, but we'll hide this entry from the progress table. See https://wiki.riteme.site/wiki/Wikipedia:Village_pump_(technical)/Archive_191#All_file_page_talk_pages_with_Template:WPBannerMeta_link_to_their_corresponding_file
			if ( LuckyRename.allUsages[int].ns == 7 && (LuckyRename.allUsages[int].title.replace(/.*:/, '') == LuckyRename.movedata.from.replace(/.*:/, '') || LuckyRename.allUsages[int].title == 'File talk:'+LuckyRename.NewFilename.getValue() ) ) {
				LuckyRename.hiddenFileTalkRow = ' style="display:none"';
			} else {
				LuckyRename.hiddenFileTalkRow = '';
			}
			LuckyRename.queueTable = LuckyRename.queueTable + '<tr' + LuckyRename.hiddenFileTalkRow + '><td class="LuckyRenamePendingBlink LuckyRenameReplaceStatusCell" id="replaceusage_' + encodeURIComponent(LuckyRename.allUsages[int].title).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.pending + '</td><td id="replaceusageStatusMsg_' + encodeURIComponent(LuckyRename.allUsages[int].title).replace( /[%\._]/g, '') + '"></td><td id="replaceusageDescription_' + encodeURIComponent(LuckyRename.allUsages[int].title).replace( /[%\._]/g, '') + '">' + LuckyRename.msgs.edit + '<a href="' + mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace( '$1', LuckyRename.escapeReplacement(encodeURIComponent(LuckyRename.allUsages[int].title)) ) + LuckyRename.usageNoRedirect + '">' + LuckyRename.allUsages[int].title + '</a>' + LuckyRename.usageRedirectIndicator + '</td></tr>';
		} else {
			LuckyRename.totalActionsTodo = LuckyRename.totalActionsTodo - 1; //one less thing to do
		}
	}

	LuckyRename.queueTable = LuckyRename.queueTable + '</table><hr>';
	$( '.LuckyRename' ).append(LuckyRename.queueTable);

	LuckyRename.replaceUsage = function(articlepageText,articleTitle,int,allowedToEditLucky,allowedToEditUserscript,articlepageTextNew,redirectFileReplaceFirstChar,redirectFileReplaceRest,redirectFileReplace,allowedToEditAutomaticallyGeneratedPage) {

		if (!articlepageText) {
			LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': articleTitle} ).then( function ( data ) {
				LuckyRename.reportDebugMsg('got filepagetext for replaceUsage');
				LuckyRename.pageText = LuckyRename.getWikitextFromExport(data.query.export['*']); 
				if ( LuckyRename.pageText ) {
					LuckyRename.replaceUsage(LuckyRename.pageText,articleTitle,int);
				} else {
					LuckyRename.reportDebugMsg('extraction of wikitext failed for '+articleTitle);
				}
			});
			return;
		}

		articlepageTextNew = articlepageText.replace( LuckyRename.oldFileReplace, '$1' + LuckyRename.escapeReplacement(LuckyRename.newFileReplace));
			for (int3 = 0; int3 < LuckyRename.allUsages.length; int3++) {
				if ( typeof LuckyRename.allUsages[int3].redirect != "undefined" ) {
					LuckyRename.redirectTitle = LuckyRename.allUsages[int3].title;
					redirectFileReplaceFirstChar = LuckyRename.escapeRegExp(LuckyRename.redirectTitle).replace( /File:/, '').slice(0, 1).toUpperCase() + LuckyRename.escapeRegExp(LuckyRename.redirectTitle).replace ( /File:/, '').slice(0, 1).toLowerCase();
					redirectFileReplaceRest = LuckyRename.escapeRegExp(LuckyRename.redirectTitle).replace( /File:/, '').slice(1, LuckyRename.escapeRegExp(LuckyRename.redirectTitle).length);
					redirectFileReplace = new RegExp('([\n=\|\^]\\s?|([^a-z]:|[^:])\\b[^:]+:)[' + redirectFileReplaceFirstChar + ']' + redirectFileReplaceRest.replace( /[_ ]/g, '[_ ]'), "g");
					articlepageTextNew = articlepageTextNew.replace( redirectFileReplace, '$1' + LuckyRename.escapeReplacement(LuckyRename.newFileReplace));
				}
			}
		allowedToEditLucky = LuckyRename.allowBots(articlepageText, 'luckyrename');
		allowedToEditUserscript = LuckyRename.allowBots(articlepageText, 'userscript');
		allowedToEditAutomaticallyGeneratedPage = articlepageText.search( LuckyRename.automaticallyGeneratedTemplate );
					
		if ( articlepageText != articlepageTextNew && allowedToEditLucky && allowedToEditUserscript && typeof LuckyRename.allUsages[int].title != "undefined" && allowedToEditAutomaticallyGeneratedPage == -1 ) {
			LuckyRename.articledata = {
				action: 'edit',
				minor: '1',
				nocreate: true,
				title: LuckyRename.allUsages[int].title,
				summary: LuckyRename.Summary,
				text: articlepageTextNew,
				watchlist: 'nochange',
			};
			LuckyRename.reportDebugMsg('Replacing file name on ' + LuckyRename.allUsages[int].title + ' from ' + LuckyRename.oldFile + ' to ' + LuckyRename.newFile);
			LuckyRename.doAPICall( LuckyRename.articledata, function ( r ) {	} );
		} else if ( ! allowedToEditLucky || ! allowedToEditUserscript ) {
			LuckyRename.updateReplaceUsageStatus(LuckyRename.allUsages[int].title, 'LuckyRenameActionSkipped', LuckyRename.msgs.LuckyRenameActionSkippedDisallowed);
			LuckyRename.totalActionsDone++; // +1
			LuckyRename.updateProgressBar(100 / LuckyRename.totalActionsTodo * LuckyRename.totalActionsDone);
		} else if ( allowedToEditAutomaticallyGeneratedPage != -1 ) {
			LuckyRename.updateReplaceUsageStatus(LuckyRename.allUsages[int].title, 'LuckyRenameActionSkipped', LuckyRename.msgs.actionAutomaticallyGenerated);
			LuckyRename.totalActionsDone++; // +1
			LuckyRename.updateProgressBar(100 / LuckyRename.totalActionsTodo * LuckyRename.totalActionsDone);
		} else if ( articlepageText == articlepageTextNew ) {
			LuckyRename.updateReplaceUsageStatus(LuckyRename.allUsages[int].title, 'LuckyRenameActionSkipped', LuckyRename.msgs.actionFilenamenotfound);
			LuckyRename.totalActionsDone++; // +1
			LuckyRename.updateProgressBar(100 / LuckyRename.totalActionsTodo * LuckyRename.totalActionsDone);
		} else if ( typeof int != "undefined" && typeof LuckyRename.allUsages[int].title != "undefined" ) {
			LuckyRename.totalActionsDone++; // +1
			LuckyRename.updateProgressBar(100 / LuckyRename.totalActionsTodo * LuckyRename.totalActionsDone);
		}
	};

	LuckyRename.DoReplaceUsages = function() {
		if (typeof LuckyRename.linkshere2[0] != "undefined" || typeof LuckyRename.fileUsages2[0] != "undefined") { //something links here or this is used somewhere
			LuckyRename.allUsagesArray = [];
			for (int = 0; int < LuckyRename.allUsages.length; int++) {
				if ( LuckyRename.allUsagesArray.indexOf(LuckyRename.allUsages[int].title) == -1 && LuckyRename.allUsages[int].title != LuckyRename.newFile && LuckyRename.allUsages[int].title != LuckyRename.oldFile ) { //not a duplicate and not the move target which is processed by DoEditPages. Also not the current page. Duplicates happen when one page links AND uses an image
					LuckyRename.allUsagesArray.push(LuckyRename.allUsages[int].title); //add title to our array so we'll know if we hit a duplicate
					LuckyRename.replaceUsageTitle = LuckyRename.allUsages[int].title;
					LuckyRename.replaceUsage(null,LuckyRename.replaceUsageTitle,int);
				} //end not a duplicate
			} //end for
		} else {
			LuckyRename.reportDebugMsg('nothing links here. At all.');
		}	//end something links here or this is used somewhere
	}; //end DoReplaceUsages

	LuckyRename.DoMovePage = function() {
		if ( LuckyRename.movedata.from != LuckyRename.movedata.to ) { //only move if to and from are different. They can be the same when using override to resolve redirects in usage
			LuckyRename.doAPICall( LuckyRename.movedata, function ( r ) {	} );
		}
	};
	LuckyRename.TagRedirectSpeedy = function(filepageText,SummaryRedirect,filepageRedirectText,filepageRedirectTextNew) {
		if ( ( LuckyRename.MoveReasonSelect.getValue() == LuckyRename.msgs.fncshadow && LuckyRename.LeaveRedirectCheck.isSelected() ) || ( ! LuckyRename.UserCanSuppressRedirects() && ! LuckyRename.LeaveRedirectCheck.isSelected() && LuckyRename.allUsagesArray.length < 20 ) ) {
		//file is moved because it shadows a Commons file and the user chose to leave a redirect, or the user can't suppress redirects but chose not to leave a redirect and the file has less than 20 uses (including links) to meet the "little-used" requirement of WP:PMRC#10. In these cases we need to tag the redirect for speedy deletion.
			if (LuckyRename.MoveReasonSelect.getValue() == LuckyRename.msgs.fncshadow ) {
				LuckyRename.SpeedyTag = LuckyRename.msgs.redirectspeedyshadow;
				SummaryRedirect = LuckyRename.msgs.summaryredirectshadow + LuckyRename.msgs.toolcredit;
			} else {
				LuckyRename.SpeedyTag = LuckyRename.msgs.redirectspeedyother;
				SummaryRedirect = LuckyRename.msgs.summaryredirectother + LuckyRename.msgs.toolcredit;
			}

			if ( typeof filepageText == 'undefined' ) {
				LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
					LuckyRename.reportDebugMsg('got filepageText for TagRedirectSpeedy');
					LuckyRename.filepageText = LuckyRename.getWikitextFromExport(data.query.export['*']); 
					if ( LuckyRename.filepageText ) {
						LuckyRename.TagRedirectSpeedy(LuckyRename.filepageText);
					} else {
						LuckyRename.reportDebugMsg('extraction of wikitext failed for '+mw.config.get('wgPageName'));
					}
				});
				return;
			}

			//after move this should be a redirect
			filepageRedirectTextNew = filepageText.replace( /#REDIRECT(.*)/ , '#REDIRECT$1\n' + LuckyRename.escapeReplacement(LuckyRename.SpeedyTag) );

			if ( filepageText != filepageRedirectTextNew ) {
				LuckyRename.filepagedata = {
				action: 'edit',
				nocreate: true,
				title: LuckyRename.oldFile,
				summary: SummaryRedirect,
				text: filepageRedirectTextNew,
				watchlist: 'nochange',
				//tags: '',
			};
			LuckyRename.reportDebugMsg('Tagging redirect for speedy deletion');
			LuckyRename.doAPICall( LuckyRename.filepagedata, function ( r ) { } );
			} else if ( filepageText == filepageRedirectTextNew ) {
			}
		} else if ( ! LuckyRename.UserCanSuppressRedirects() && ! LuckyRename.LeaveRedirectCheck.isSelected() && LuckyRename.allUsagesArray.length > 19 ) { //user without suppressredirect tried to skip leaving a redirect for a file with 20 or more uses (including links)
			LuckyRename.updateReplaceUsageStatus(encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, ''), 'LuckyRenameActionSkipped', LuckyRename.msgs.LuckyRenameActionSkippedTooManyUses);
			LuckyRename.reportDebugMsg('Skipping the tagging of the redirect that was left for speedy deletion because it has more than 6 uses');
		}
	};
	LuckyRename.WaitForPendingIntMax = 300; // 300*200ms=1 minute
	LuckyRename.WaitForPendingInt = 0;

	LuckyRename.SetAllPendingTableCellsToSkipped = function(int) {
		for (int = 0; int < document.getElementsByClassName('LuckyRenameReplaceStatusCell').length; int++) {
			document.getElementsByClassName('LuckyRenameReplaceStatusCell')[int].classList = [ 'LuckyRenameActionSkipped LuckyRenameReplaceStatusCell' ];
			document.getElementsByClassName('LuckyRenameReplaceStatusCell')[int].innerHTML = LuckyRename.msgs.LuckyRenameActionFailed;
		}
	};

	LuckyRename.DoEditPages = function() {
		if ( typeof document.getElementById("movePage_" + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '')) != "undefined" ) {
			LuckyRename.PageMoveStatus = document.getElementById("movePage_" + encodeURIComponent(LuckyRename.oldFile).replace( /[%\._]/g, '')).classList[0];
			if ( LuckyRename.PageMoveStatus == 'LuckyRenamePendingBlink' ) {
				if ( LuckyRename.WaitForPendingInt < LuckyRename.WaitForPendingIntMax ) {
					LuckyRename.WaitForPendingInt = LuckyRename.WaitForPendingInt + 1;
					window.setTimeout( function(){LuckyRename.DoEditPages();}, 200 );
					LuckyRename.reportDebugMsg('File not moved yet, waiting before doing replacements');
				} else {
					LuckyRename.reportDebugMsg('File still not moved, something must have gone wrong, giving up');
					LuckyRename.SetAllPendingTableCellsToSkipped();
				}
			} else if ( LuckyRename.PageMoveStatus == 'LuckyRenameActionFailed' ) {
				LuckyRename.reportDebugMsg('File move failed, aborting related edits');
				LuckyRename.SetAllPendingTableCellsToSkipped();
			} else if ( LuckyRename.PageMoveStatus == 'LuckyRenameActionCompleted' ) {
				if ( LuckyRename.filepageText != LuckyRename.filepageTextNew ) {
					LuckyRename.doAPICall( LuckyRename.filepagedata, function ( r ) { } );
				}
				LuckyRename.DoReplaceUsages();
				LuckyRename.TagRedirectSpeedy(); //only tags the redirect left when appropriate
			}
		}
	};
	LuckyRename.DoMovePage();
	LuckyRename.DoEditPages();

}; // end LuckyRename.DoRename function

LuckyRename.getWikitextFromExport = function(text) {
	if ( text.match(/[^]*<text bytes[^>]*>([^]*)<\/text>[^]*/) ) {
		return text.replace(/[^]*<text bytes[^>]*>([^]*)<\/text>[^]*/, '$1').replace(/\&lt\;/g, '<').replace(/\&gt\;/g, '>').replace(/\&amp\;/g, '&');
	} else {
		return '';
	}
};
LuckyRename.DoDecline = function(filepageText) {
	LuckyRename.saveSettings(); //save move reason for memorize inputs
	LuckyRename.SummaryFilePage = LuckyRename.msgs.summarydeclined + LuckyRename.MoveReason.getValue() + LuckyRename.msgs.toolcredit;
	LuckyRename.oldFile = mw.config.get('wgPageName');
	if ( typeof filepageText == 'undefined' ) {
		LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
			LuckyRename.reportDebugMsg('got filepageText for DoDecline');
			LuckyRename.filepageText = LuckyRename.getWikitextFromExport(data.query.export['*']);
			if ( LuckyRename.filepageText ) {
				LuckyRename.DoDecline(LuckyRename.filepageText);
			} else {
				LuckyRename.reportDebugMsg('extraction of wikitext failed for '+mw.config.get('wgPageName'));
			}
		});
		return;
	}
	LuckyRename.filepageText = filepageText;
	LuckyRename.filepageTextNew = LuckyRename.filepageText.replace(LuckyRename.RenameTemplate,'').replace( /^[\s]*/, '');
	LuckyRename.filepagedata = {
		action: 'edit',
		nocreate: true,
		title: LuckyRename.oldFile,
		summary: LuckyRename.SummaryFilePage,
		text: LuckyRename.filepageTextNew,
		watchlist: 'nochange',
		//tags: '',
		//token: LuckyRename.getToken(),
	};
	if ( LuckyRename.filepageText != LuckyRename.filepageTextNew) {
		LuckyRename.doAPICall( LuckyRename.filepagedata, function ( r ) { },'decline' );
	}
	$( document ).ready( function() {
		$( '.LuckyRename' ).append('<br>' + LuckyRename.msgs.renamedeclined);
	} );
};
LuckyRename.DoRequestMove = function(filepageText) {
	LuckyRename.disableMoveButton('nochange'); //no need to use this button anymore, we've started, but don't change the bgcolor of the filename field
	LuckyRename.saveSettings(); //save move reason for memorize inputs

	if ( typeof filepageText == 'undefined' ) {
		LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
			LuckyRename.reportDebugMsg('got filepageText for DoRequestMove');
			LuckyRename.filepageText = LuckyRename.getWikitextFromExport(data.query.export['*']);
			if ( LuckyRename.filepageText ) {
				LuckyRename.DoRequestMove(LuckyRename.filepageText);
			} else {
				LuckyRename.reportDebugMsg('extraction of wikitext failed for '+mw.config.get('wgPageName'));
			}
		});
		return;
	}
	$( '.LuckyRename' ).append('<br>' + LuckyRename.msgs.pleasewait);
	LuckyRename.oldFile = mw.config.get('wgPageName').replace( /_/g, " ");
	LuckyRename.newFile = LuckyRename.NewFilename.getValue();
	if (LuckyRename.MoveReasonSelect.getValue() != "" && LuckyRename.MoveReason.getValue() != "" ) {
		LuckyRename.MoveReasonTotal = LuckyRename.MoveReasonSelect.getValue() + ", " + LuckyRename.MoveReason.getValue();
	} else {
		LuckyRename.MoveReasonTotal = LuckyRename.MoveReasonSelect.getValue() + LuckyRename.MoveReason.getValue();
	}
	LuckyRename.renameRequestText = LuckyRename.msgs.requesttemplate.replace( "NEWTITLE", LuckyRename.escapeReplacement(LuckyRename.newFile) ).replace( "RENAMEREASON", LuckyRename.escapeReplacement(LuckyRename.MoveReasonTotal) ).replace( "|}}", "}}" );
	LuckyRename.SummaryRequest = LuckyRename.msgs.summaryrequestmove + '[[:' + LuckyRename.msgs.ns6 + LuckyRename.newFile + ']]: ' + LuckyRename.MoveReasonTotal + LuckyRename.msgs.toolcredit;
	if ( LuckyRename.overridden == 1 ) {
		LuckyRename.SummaryRequest = LuckyRename.SummaryRequest + LuckyRename.msgs.summaryoverride;
	}

	LuckyRename.renameRequestWikitext = filepageText.replace(LuckyRename.RenameTemplate,'').replace( /^[\s]*/, ''); //remove any existing rename templates so they can't pile up

	LuckyRename.renameRequestWikitext = LuckyRename.renameRequestText + "\n" + LuckyRename.renameRequestWikitext;
		LuckyRename.filepagerequestdata = {
			action: 'edit',
			nocreate: true,
			title: LuckyRename.oldFile,
			summary: LuckyRename.SummaryRequest,
			text: LuckyRename.renameRequestWikitext.replace( /\n\n\n+/g, '\n\n' ),
			watchlist: 'nochange',
			//tags: '',
			//token: LuckyRename.getToken(),
		};

	LuckyRename.doAPICall( LuckyRename.filepagerequestdata, function ( r ) { },'request' );
};

LuckyRename.Dolucky = function(filepageText,articleqid,luckyFilename,luckyArticleInt,articletitle,articleinstance,articleinstancelabel,occupation,occupationint,oneOccupationQ,oneOccupation,luckySuggest,businessIndustry,businessIndustryLabel,performingArtist,performingArtistLabel,bookAuthor,bookAuthorLabel,publicationYearNeeded,yearOfPublication,luckySuggestPlain,fileIsLogo,fileIsScreenshot,fileIsCover,fileIsUniform,fileIsPoster,fileIsSeal,luckyPost,luckySuggestSearch,searchTitleForSuggestion) {

	if ( typeof filepageText == 'undefined' ) {
		LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
			LuckyRename.reportDebugMsg('got filepageText for Dolucky');
			LuckyRename.filepageText = LuckyRename.getWikitextFromExport(data.query.export['*']); 
			if ( LuckyRename.filepageText ) {
				LuckyRename.Dolucky(LuckyRename.filepageText);
			} else {
				LuckyRename.reportDebugMsg('extraction of wikitext failed for '+mw.config.get('wgPageName'));
			}
		});
		return;
	}
	LuckyRename.filepageText = filepageText;

	if ( !articleqid ) {
		if ( typeof luckyArticleInt == "undefined" ) {	// first time Feelin' lucky is used on this page. Cycles through the mainspace articles the file is used on
			luckyArticleInt = 0;
		} else if (typeof document.getElementsByClassName("mw-imagepage-linkstoimage-ns0")[ luckyArticleInt + 1 ] != "undefined") {
			luckyArticleInt++;
		} else {	// luckyArticleInt is already defined and is selecting the last available mainspace article, cycle back to the first
			luckyArticleInt = 0;
		}
		if ( typeof LuckyRename.LuckyUseArticleTitle != "undefined" && LuckyRename.LuckyUseArticleTitle.getValue() != "" ) {
			articletitle = LuckyRename.LuckyUseArticleTitle.getValue();
		} else {
			articletitle = document.getElementsByClassName("mw-imagepage-linkstoimage-ns0")[ luckyArticleInt ].innerText; 
		}
		luckyFilename = articletitle.replace(/ \(.*\)/, ''); //remove the parenthesis content and ignore it for the filename
		LuckyRename.wdapi.get( {'action':'wbgetentities','format':'json','sites':mw.config.get('wgDBname'),'titles':articletitle} ).then( function ( data,wikidataq1,wikidataq2,wikidataq3,wikidataq4,wikidataq5 ) {
			LuckyRename.wikidataprop.entities = Object.assign(LuckyRename.wikidataprop.entities, data.entities);
			wikidataq1 = data;
			wikidataq2=Object.values(wikidataq1);
			wikidataq3=Object.values(wikidataq2);
			wikidataq4=wikidataq3[0];
			wikidataq5=Object.keys(wikidataq4);
			LuckyRename.Dolucky(filepageText,wikidataq5[0],luckyFilename);
		});
		return;
	}
	if ( articleqid == -1 ) {
		$( '.LuckyRename' ).append('<a href="' + mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', LuckyRename.escapeReplacement(articletitle)) + '">' + articletitle + '</a>' + LuckyRename.msgs.noWikidataItem + '<br />');
	} else { // article has Wikidata item
		articleinstance = LuckyRename.getWikidataClaim(articleqid, LuckyRename.wikidataInstanceProp,filepageText,articleqid,luckyFilename);
		if ( articleinstance == false ) { return; }
		if ( typeof articleinstance == "undefined" ) { //try subclass
			articleinstance = LuckyRename.getWikidataClaim(articleqid, 'P279',filepageText,articleqid,luckyFilename);
			if ( articleinstance == false ) { return; }
		}

		if ( typeof articleinstance == "undefined" ) {
			articleinstance = false;
		}
	if ( articleinstance ) {
		articleinstance = articleinstance[0].mainsnak.datavalue.value.id;
		articleinstancelabel = LuckyRename.getWikidataLabel(articleinstance,filepageText,articleqid,luckyFilename);
		if ( articleinstancelabel == false ) { return; }
		if ( articleinstance == 'Q5' ) {
			occupation = LuckyRename.getWikidataClaim(articleqid, 'P106',filepageText,articleqid,luckyFilename);
			if ( occupation == false ) { return; }
			if ( typeof occupation != "undefined" ) {
				LuckyRename.occupationstring = '';
				for (occupationint = 0; occupationint < occupation.length; occupationint++) {
					oneOccupationQ = occupation[occupationint].mainsnak.datavalue.value.id;
					oneOccupation = LuckyRename.getWikidataLabel(oneOccupationQ,filepageText,articleqid,luckyFilename);
					if ( oneOccupation == false ) { return; }
					LuckyRename.occupationstring = LuckyRename.occupationstring + oneOccupation;
					if ( occupationint != occupation.length - 1 ) {
						LuckyRename.occupationstring = LuckyRename.occupationstring + ', ';
					}
				}
			}
			LuckyRename.yearOfBirth = LuckyRename.getWikidataClaim(articleqid, 'P569',filepageText,articleqid,luckyFilename);
			if ( LuckyRename.yearOfBirth == false ) { return; }

			if ( typeof LuckyRename.yearOfBirth != "undefined" ) {
				LuckyRename.yearOfBirth = LuckyRename.yearOfBirth[0].mainsnak.datavalue.value.time;
				LuckyRename.yearOfBirth = LuckyRename.yearOfBirth.replace( /\+([0-9][0-9][0-9][0-9]).*/, '$1');
			}
			if ( typeof LuckyRename.yearOfBirth != "undefined" && typeof LuckyRename.occupationstring != "undefined" ) {
				luckySuggest = LuckyRename.occupationstring + ', born ' + LuckyRename.yearOfBirth;
			} else if ( typeof LuckyRename.occupationstring != "undefined" ) {
				luckySuggest = LuckyRename.occupationstring;
			} else if ( typeof LuckyRename.yearOfBirth != "undefined" ) {
				luckySuggest = 'born ' + LuckyRename.yearOfBirth;
			} else {
				luckySuggest = '';
			}
		} else if ( articleinstance == 'Q4830453' ) { //business
			businessIndustry = LuckyRename.getWikidataClaim(articleqid, 'P452',filepageText,articleqid,luckyFilename);
			if ( businessIndustry == false ) { return; }
			if ( typeof businessIndustry != "undefined" ) { //industry
				businessIndustry = businessIndustry[0].mainsnak.datavalue.value.id; 
		 		businessIndustryLabel = LuckyRename.getWikidataLabel(businessIndustry,filepageText,articleqid,luckyFilename);
				if ( businessIndustryLabel == false ) { return; }
				luckySuggest = businessIndustryLabel + ' ' + articleinstancelabel;
			} else {
				luckySuggest = articleinstancelabel;
			}
		} else if ( articleinstance == 'Q7366' || articleinstance == 'Q134556' || articleinstance == 'Q482994' || articleinstance == 'Q169930') { //song, single, album, extended play

			performingArtist = LuckyRename.getWikidataClaim(articleqid, 'P175',filepageText,articleqid,luckyFilename);
			if ( performingArtist == false ) { return; }

			if ( typeof performingArtist != "undefined" ) { //performer
				performingArtist = performingArtist[0].mainsnak.datavalue.value.id; 
		 		performingArtistLabel = LuckyRename.getWikidataLabel(performingArtist,filepageText,articleqid,luckyFilename);
				if ( performingArtistLabel == false ) { return; }
				luckySuggest = performingArtistLabel + ' ' + articleinstancelabel;
			} else {
				luckySuggest = articleinstancelabel;
			}
		} else if ( articleinstance == 'Q47461344' || articleinstance == 'Q7725634' || articleinstance == 'Q8261' || articleinstance == 'Q571' ) { //written work, literary work, novel, book

			bookAuthor = LuckyRename.getWikidataClaim(articleqid, 'P50',filepageText,articleqid,luckyFilename);
			if ( bookAuthor == false ) { return; }

			if ( typeof bookAuthor != "undefined" ) { //author
				bookAuthor = bookAuthor[0].mainsnak.datavalue.value.id; 
		 		bookAuthorLabel = LuckyRename.getWikidataLabel(bookAuthor,filepageText,articleqid,luckyFilename);
				if ( bookAuthorLabel == false ) { return; }
				luckySuggest = bookAuthorLabel + ' ' + articleinstancelabel;
			} else {
				luckySuggest = articleinstancelabel;
			}
		} else {
			luckySuggest = articleinstancelabel;
		}
		//song, single, album, film, feature film, comics, written work, literary work, animated feature film, video game, graphic novel, television film, novel, book, short film, extended play, animated short film, animated film, 3D film:
		publicationYearNeeded = [ 'Q7366', 'Q134556', 'Q482994', 'Q11424', 'Q24869', 'Q1004', 'Q47461344', 'Q7725634', 'Q29168811', 'Q7889', 'Q725377', 'Q506240', 'Q8261', 'Q571', 'Q24862', 'Q169930', 'Q17517379', 'Q202866', 'Q229390' ];
		if ( $.inArray( articleinstance, publicationYearNeeded ) > -1 ) {
		yearOfPublication = LuckyRename.getWikidataClaim(articleqid, 'P577',filepageText,articleqid,luckyFilename);
		if ( yearOfPublication == false ) { return; }

				if (typeof yearOfPublication != "undefined" && yearOfPublication != "") {
					yearOfPublication = yearOfPublication[0].mainsnak.datavalue.value.time;
					yearOfPublication = yearOfPublication.replace( /\+([0-9][0-9][0-9][0-9]).*/, '$1');
					luckySuggest = yearOfPublication + ' ' + luckySuggest;
				}
		} else if ( articleinstance == 'Q5398426' ) { // television series
		yearOfPublication = LuckyRename.getWikidataClaim(articleqid, 'P580',filepageText,articleqid,luckyFilename);	//start time
		if ( yearOfPublication == false ) { return; }

				if (typeof yearOfPublication != "undefined" && yearOfPublication != "") {
					yearOfPublication = yearOfPublication[0].mainsnak.datavalue.value.time;
					yearOfPublication = yearOfPublication.replace( /\+([0-9][0-9][0-9][0-9]).*/, '$1');
					luckySuggest = yearOfPublication + ' ' + luckySuggest;
				}
		} else if ( articleinstance == 'Q25379' ) { // play
		yearOfPublication = LuckyRename.getWikidataClaim(articleqid, 'P1191',filepageText,articleqid,luckyFilename);	//date of first performance
		if ( yearOfPublication == false ) { return; }

				if (typeof yearOfPublication != "undefined" && yearOfPublication != "") {
					yearOfPublication = yearOfPublication[0].mainsnak.datavalue.value.time;
					yearOfPublication = yearOfPublication.replace( /\+([0-9][0-9][0-9][0-9]).*/, '$1');
					luckySuggest = yearOfPublication + ' ' + luckySuggest;
				}
		}
	} else {	// end check for articleinstance
		luckySuggest = '';
	}
	luckySuggestPlain = luckySuggest;
	if ( luckySuggest != '' ) {
		luckySuggest = ' (' + luckySuggest + ')';
	}

	fileIsLogo = LuckyRename.filepageText.search( /({{PD-textlogo|{{Non-free logo)/i );
	fileIsSeal = LuckyRename.filepageText.search( /{{Non-free seal/i );
	fileIsScreenshot = LuckyRename.filepageText.search( /({{Non-free Microsoft screenshot|{{Non-free software screenshot|{{Non-free video game screenshot|{{Game-screenshot|{{Non-free game screenshot|{{Non-free web screenshot)/i );
	fileIsCover = LuckyRename.filepageText.search( /({{Non-free book cover|{{Non-free magazine cover)/i );
	fileIsUniform = LuckyRename.filepageText.search( /({{Non-free sports uniform)/i );
	fileIsPoster = LuckyRename.filepageText.search( /({{Non-free use rationale poster|{{Non-free poster|{{Non-free movie poster)/i );
	if ( fileIsLogo != -1 ) {
		luckySuggest = luckySuggest + LuckyRename.msgs.postlogo;
		luckyPost = LuckyRename.msgs.postlogo;
	} else if ( fileIsSeal != -1) {
		luckySuggest = luckySuggest + LuckyRename.msgs.postseal;
		luckyPost = LuckyRename.msgs.postseal;
	} else if ( fileIsScreenshot != -1 ) {
		luckySuggest = luckySuggest + LuckyRename.msgs.postscreenshot;
	 	luckyPost = LuckyRename.msgs.postscreenshot;
	} else if ( fileIsCover != -1 ) {
		luckySuggest = luckySuggest + LuckyRename.msgs.postcover;
	 	luckyPost = LuckyRename.msgs.postcover;
	} else if ( fileIsUniform != -1 ) {
		luckySuggest = luckySuggest + LuckyRename.msgs.postuniform;
	 	luckyPost = LuckyRename.msgs.postuniform;
	} else if ( fileIsPoster != -1 ) {
		luckySuggest = luckySuggest + LuckyRename.msgs.postposter;
	 	luckyPost = LuckyRename.msgs.postposter;
	} else {
		luckySuggest = luckySuggest;
		luckyPost = '';
	}
										
	luckySuggestSearch = new RegExp(LuckyRename.escapeRegExp(luckySuggestPlain), "i");
	searchTitleForSuggestion = luckyFilename.search( luckySuggestSearch );
	if ( searchTitleForSuggestion == -1 && luckySuggest != "" ) {
		luckyFilename = luckyFilename + luckySuggest + '.' + LuckyRename.correctFilenameExtension;
		luckyFilename = luckyFilename.replace( /[\?\!]/g, '').replace( /\:/g, ',');
	} else { //suggestion is already part of the title, toss it, use articletitle to keep parenthesized article title info if there was any
		luckyFilename = articletitle + luckyPost + '.' + LuckyRename.correctFilenameExtension;
		luckyFilename = luckyFilename.replace( /[\?\!]/g, '').replace( /\:/g, ',');
	}
	LuckyRename.NewFilename.setValue(luckyFilename);
	LuckyRename.checkNewFilename();
	} // end article has Wikidata item
}; // end Dolucky

mw.loader.using( ['oojs-ui-core','mediawiki.api','mediawiki.ForeignApi'] ).done( function () {

		LuckyRename.wdapi = new mw.ForeignApi('https://www.wikidata.org/w/api.php',{anonymous:true});
		LuckyRename.api = new mw.Api();

		LuckyRename.renameButton = new OO.ui.ButtonWidget( {
			id: 'renameButton',
				label: LuckyRename.msgs.renamebuttonlabel,
				flags: [ 'primary', 'progressive' ],
		} );
		LuckyRename.renameButton.on( 'click', function(){LuckyRename.DoRename();});

		LuckyRename.declineButton = new OO.ui.ButtonWidget( {
			id: 'declineButton',
				label: LuckyRename.msgs.declinebuttonlabel,
				flags: [ 'primary', 'destructive' ],
		} );
		LuckyRename.declineButton.on( 'click', function(){LuckyRename.DoDecline();});

		LuckyRename.requestButton = new OO.ui.ButtonWidget( {
			id: 'requestButton',
				label: LuckyRename.msgs.requestbuttonlabel,
				flags: [ 'primary', 'progressive' ],
		} );
		LuckyRename.requestButton.on( 'click', function(){LuckyRename.DoRequestMove();});

		LuckyRename.luckyButton = new OO.ui.ButtonWidget( {
			id: 'luckyButton',
				label: LuckyRename.msgs.luckybuttonlabel,
		} );
		LuckyRename.luckyButton.on( 'click', function(){LuckyRename.Dolucky();});

		LuckyRename.overrideButton = new OO.ui.ButtonWidget( {
			id: 'overrideButton',
				label: LuckyRename.msgs.overridebuttonlabel,
				flags: [ 'primary' ],
		} );
		LuckyRename.overrideButton.on( 'click', LuckyRename.overrideEnableMoveButton);

		LuckyRename.resolveRedirectsButton = new OO.ui.ButtonWidget( {
			id: 'resolveRedirectsButton',
				label: LuckyRename.msgs.resolveRedirectsButtonlabel,
				flags: [ 'primary', 'progressive' ],
		} );
		LuckyRename.resolveRedirectsButton.on( 'click', LuckyRename.resolveRedirects);
	
		if ( (LuckyRename.settings.StoredLeaveRedir && LuckyRename.settings.remember) || LuckyRename.settings.noredir ) { //user either enabled memorize inputs and didn't have leave redirect checked last time they used it or they have enabled "Uncheck "leave redirect" by default"
			LuckyRename.LeaveRedirectCheckSelectedPreset = false;
		} else {
			LuckyRename.LeaveRedirectCheckSelectedPreset = true;
		}

		LuckyRename.LeaveRedirectCheck = new OO.ui.CheckboxInputWidget( {
			id: 'LeaveRedirectCheck',
				selected: LuckyRename.LeaveRedirectCheckSelectedPreset,
				//text: '-----Leave redirect',
		} );
		if ( LuckyRename.UserCanMoveFiles() ) {
			LuckyRename.LeaveRedirectLabel = LuckyRename.msgs.leaveredirect;
		} else {
			LuckyRename.LeaveRedirectLabel = LuckyRename.msgs.preserveredirect;
		}
		LuckyRename.LeaveRedirectCheckWithLabel = new OO.ui.FieldLayout( LuckyRename.LeaveRedirectCheck, { label: LuckyRename.LeaveRedirectLabel, align: 'inline' } );
	
		LuckyRename.OriginalFilename = new OO.ui.TextInputWidget( { // would allow changing what is considered the source
			value: mw.config.get('wgPageName').replace(/_/g, ' '),
			id: 'OriginalFilename',
			label: '',
			classes: [ 'LuckyRenameInvisible' ]
		} );
	
		LuckyRename.NewFilename = new OO.ui.TextInputWidget( {
			value: '',
			id: 'NewFilename',
			label: ' ',
	 	} );

		LuckyRename.LuckyUseArticleTitle = new OO.ui.TextInputWidget( {
			value: '',
			id: 'LuckyUseArticleTitle',
			label: LuckyRename.msgs.luckycustomarticlelabel,
	 	} );

		LuckyRename.MoveReason = new OO.ui.TextInputWidget( {
			value: LuckyRename.MoveReasonSuggested,
			id: 'MoveReason',
			label: LuckyRename.msgs.MoveReason,
		} );
	
//RadioSelectInputWidget also possible
		LuckyRename.MoveReasonSelect = new OO.ui.DropdownInputWidget( {
			options: [
			{ data: '', label: LuckyRename.msgs.defaultreasons } ,
			{ data: LuckyRename.msgs.fnc1, label: LuckyRename.msgs.fnclabel1 },
			{ data: LuckyRename.msgs.fnc2, label: LuckyRename.msgs.fnclabel2 },
			{ data: LuckyRename.msgs.fnc3, label: LuckyRename.msgs.fnclabel3 },
			{ data: LuckyRename.msgs.fnc4, label: LuckyRename.msgs.fnclabel4 },
			{ data: LuckyRename.msgs.fnc5, label: LuckyRename.msgs.fnclabel5 },
			{ data: LuckyRename.msgs.fnc6, label: LuckyRename.msgs.fnclabel6 },
			{ data: LuckyRename.msgs.fnc7, label: LuckyRename.msgs.fnclabel7 },
			{ data: LuckyRename.msgs.fnc8, label: LuckyRename.msgs.fnclabel8 },
			{ data: LuckyRename.msgs.fnc9, label: LuckyRename.msgs.fnclabel9 }
			],
			label: 'Pick a reason',
			value: '',
			id: 'MoveReasonSelect',
		} );
		LuckyRename.ProgressBar = new OO.ui.ProgressBarWidget( {
			progress: 0,
			id: 'LRprogress'
		} );
		LuckyRename.settingsLink = new OO.ui.ButtonWidget( {
			framed: false,
			//icon: 'settings', //does not work on enwiki https://phabricator.wikimedia.org/T296505
			label: '⚙',
			id: 'LuckyRenameSettingToggle',
			invisibleLabel: false,
			title: LuckyRename.msgs.settings,
			classes: [ 'LuckyRenameFloatRight LuckyRenameHalfOpacity LuckyRename2Em' ]
		} );

		LuckyRename.toggleSettings = function() {
			LuckyRename.SettingsDisplayStatus = document.getElementById('LuckyRenameSettingsContainer').classList[0];
			if ( LuckyRename.SettingsDisplayStatus == 'LuckyRenameInvisible' ) {
				document.getElementById('LuckyRenameSettingsContainer').classList = [ 'LuckyRenameVisible' ];
				$(document.getElementById('LuckyRenameSettingToggle')).removeClass('LuckyRenameHalfOpacity');
			} else if ( LuckyRename.SettingsDisplayStatus == 'LuckyRenameVisible' ) {
				document.getElementById('LuckyRenameSettingsContainer').classList = [ 'LuckyRenameInvisible' ];
				$(document.getElementById('LuckyRenameSettingToggle')).addClass('LuckyRenameHalfOpacity');
			}
		};

		LuckyRename.settingDebug = new OO.ui.CheckboxInputWidget( {
			id: 'LuckyRename.settingDebug',
				selected: false,
		} );
		LuckyRename.settingDebugWithLabel = new OO.ui.FieldLayout( LuckyRename.settingDebug, { label: LuckyRename.msgs.settingdebug, align: 'inline' } );
		LuckyRename.settingDebugUI = new OO.ui.CheckboxInputWidget( {
			id: 'LuckyRename.settingDebugUI',
				selected: false,
		} );
		LuckyRename.settingDebugUIWithLabel = new OO.ui.FieldLayout( LuckyRename.settingDebugUI, { label: LuckyRename.msgs.settingdebugui, align: 'inline' } );
		LuckyRename.settingRememberInputs = new OO.ui.CheckboxInputWidget( {
			id: 'LuckyRenameSettingRememberInputs',
				selected: false,
		} );
		LuckyRename.settingRememberInputsWithLabel = new OO.ui.FieldLayout( LuckyRename.settingRememberInputs, { label: LuckyRename.msgs.settingrememberinputs, align: 'inline' } );
		LuckyRename.settingUncheckLeaveRedirect = new OO.ui.CheckboxInputWidget( {
			id: 'LuckyRename.settingUncheckLeaveRedirect',
				selected: false,
		} );
		LuckyRename.settingUncheckLeaveRedirectLabel = new OO.ui.HtmlSnippet( LuckyRename.msgs.settinguncheckleaveredirect );

		if ( LuckyRename.UserCanMoveFiles() ) {
			LuckyRename.settingUncheckLeaveRedirectWithLabelHidden = "";
		} else {
			LuckyRename.settingUncheckLeaveRedirectWithLabelHidden = "LuckyRenameInvisible";
		}
		LuckyRename.settingUncheckLeaveRedirectWithLabel = new OO.ui.FieldLayout( LuckyRename.settingUncheckLeaveRedirect, { label: LuckyRename.settingUncheckLeaveRedirectLabel, align: 'inline', classes: [ LuckyRename.settingUncheckLeaveRedirectWithLabelHidden ] } );

		LuckyRename.saveSettingsToLS = function() {
			LuckyRename.saveSettings(); //store data into LS
			LuckyRename.toggleSettings();
		};
		LuckyRename.deleteSettingsLS = function() {
			mw.storage.delete('LuckyRenameSettings1');
			LuckyRename.toggleSettings();
		};

		LuckyRename.saveLS = new OO.ui.ButtonWidget( {
			label: LuckyRename.msgs.saveprefs,
			flags: [ 'primary', 'progressive' ],
		} );
		LuckyRename.saveLS.on( 'click', LuckyRename.saveSettingsToLS);

		LuckyRename.deleteLS = new OO.ui.ButtonWidget( {
			label: LuckyRename.msgs.deleteLS,
			flags: [ 'primary', 'destructive' ],
		} );
		LuckyRename.deleteLS.on( 'click', LuckyRename.deleteSettingsLS);

		LuckyRename.settingsButtonBar = new OO.ui.HorizontalLayout( {
			items: [
				LuckyRename.saveLS,
				LuckyRename.deleteLS
			],
			classes: [ 'LuckyRenameButtonBarPadding' ]
		} );
		LuckyRename.LuckyRenameSettingsContainer = new OO.ui.FormLayout( {
			items: [
				LuckyRename.settingDebugWithLabel,
				LuckyRename.settingDebugUIWithLabel,
				LuckyRename.settingRememberInputsWithLabel,
				LuckyRename.settingUncheckLeaveRedirectWithLabel,
				LuckyRename.settingsButtonBar
			],
			id: 'LuckyRenameSettingsContainer',
			classes: [ 'LuckyRenameInvisible' ]
		} );

		LuckyRename.loadSettings = function() {
			LuckyRename.settings = JSON.parse(mw.storage.get('LuckyRenameSettings1')||'{}');
			if ( LuckyRename.settings.debugUI ) {
				LuckyRename.settingDebugUI.setSelected(true);
			}
			if ( LuckyRename.settings.noredir ) {
				LuckyRename.settingUncheckLeaveRedirect.setSelected(true);
			}
			if ( LuckyRename.settings.remember ) {
				LuckyRename.settingRememberInputs.setSelected(true);
				LuckyRename.StoredMoveReason = atob(LuckyRename.settings.MoveReason);
			}
			if ( LuckyRename.settings.debug ) {
				LuckyRename.settingDebug.setSelected(true);
			}
		};
		LuckyRename.saveSettings = function() {
			if ( LuckyRename.settingRememberInputs.isSelected() && LuckyRename.MoveReasonSelect.getValue() != "" ) {
				LuckyRename.currentMoveReason = LuckyRename.MoveReasonSelect.getValue() + ', ' + LuckyRename.MoveReason.getValue();
			} else if ( LuckyRename.settingRememberInputs.isSelected() ) {
				LuckyRename.currentMoveReason = LuckyRename.MoveReason.getValue();
			} else { //don't store a possibly lengthy string if the user doesn't have settingRememberInputs enabled anyway, they won't use it
				LuckyRename.currentMoveReason = "";
			}
			LuckyRename.currentMoveReason = btoa(LuckyRename.currentMoveReason); //btoa = base64 encode
			LuckyRename.settings.MoveReason = LuckyRename.currentMoveReason;
			LuckyRename.settings.noredir = LuckyRename.settingUncheckLeaveRedirect.isSelected();
			LuckyRename.settings.debugUI = LuckyRename.settingDebugUI.isSelected();
			LuckyRename.settings.debug = LuckyRename.settingDebug.isSelected();
			LuckyRename.settings.remember = LuckyRename.settingRememberInputs.isSelected();
			LuckyRename.settings.StoredLeaveRedir = LuckyRename.LeaveRedirectCheck.isSelected();
			mw.storage.set('LuckyRenameSettings1',JSON.stringify(LuckyRename.settings));
			LuckyRename.reportDebugMsg('wrote settings to LocalStorage');
			LuckyRename.loadSettings();
		};
		LuckyRename.settingsLink.on( 'click', LuckyRename.toggleSettings);
		LuckyRename.scriptInfoLink = new OO.ui.LabelWidget( {
			label: new OO.ui.HtmlSnippet( LuckyRename.msgs.creditlink ),
			classes: [ 'LuckyRenameFloatRight' ]
		} );
		if ( LuckyRename.settings.debug ) {
			LuckyRename.buttonBar = new OO.ui.HorizontalLayout( {
				items: [
					LuckyRename.renameButton,
					LuckyRename.declineButton,
					LuckyRename.luckyButton,
					LuckyRename.overrideButton,
					LuckyRename.LeaveRedirectCheckWithLabel,
					LuckyRename.scriptInfoLink,
					LuckyRename.settingsLink,
					LuckyRename.requestButton,
				],
				classes: [ 'LuckyRenameButtonBarPadding' ]
			} );
			
		} else if ( LuckyRename.UserCanMoveFiles() ) {
			LuckyRename.buttonBar = new OO.ui.HorizontalLayout( {
				items: [
					LuckyRename.renameButton,
					LuckyRename.declineButton,
					LuckyRename.luckyButton,
					LuckyRename.overrideButton,
					LuckyRename.LeaveRedirectCheckWithLabel,
					LuckyRename.scriptInfoLink,
					LuckyRename.settingsLink,
				],
				classes: [ 'LuckyRenameButtonBarPadding' ]
			} );
		} else {
			LuckyRename.buttonBar = new OO.ui.HorizontalLayout( {
				items: [
					LuckyRename.requestButton,
					LuckyRename.declineButton,
					LuckyRename.luckyButton,
					LuckyRename.overrideButton,
					LuckyRename.scriptInfoLink,
					LuckyRename.settingsLink,
				],
				classes: [ 'LuckyRenameButtonBarPadding' ]
			} );
		}

	function CheckShadow() {
		if (LuckyRename.MoveReasonSelect.getValue() == LuckyRename.msgs.fncshadow) {
			LuckyRename.LeaveRedirectCheck.isSelected(false);
		}
	}
	LuckyRename.MoveReasonSelect.on( 'change', CheckShadow);
	LuckyRename.NewFilename.on( 'change', function(){LuckyRename.checkNewFilename();});
	LuckyRename.loadLuckyRename = function(int,filepageText) {

		if (typeof document.getElementsByClassName("media-move-reason")[0] != "undefined" && typeof filepageText == 'undefined') { 
			LuckyRename.api.get( {'action':'query','format':'json','export':'true','titles': mw.config.get('wgPageName')} ).then( function ( data ) {
				LuckyRename.reportDebugMsg('got move reason');
				LuckyRename.filepageText = LuckyRename.getWikitextFromExport(data.query.export['*']); 
				if ( LuckyRename.filepageText ) {
					LuckyRename.loadLuckyRename(int,LuckyRename.filepageText);
				} else {
					LuckyRename.reportDebugMsg('extraction of wikitext failed for '+mw.config.get('wgPageName'));
				}
			});
			return;
		}
		if ( document.getElementById('LuckyRename') == null ) {		
			$( '.fullMedia' ).append('<div style="background:AliceBlue;border:1px solid CornflowerBlue;padding:1em 1em 0.5em 1em;margin:0.5em;max-width:50em;" class="LuckyRename" id="LuckyRename">');
			$( '.LuckyRename' ).append(LuckyRename.OriginalFilename.$element);
			$( '.LuckyRename' ).append(LuckyRename.NewFilename.$element).append('<div style="height:0.5em"></div>');
			$( '.LuckyRename' ).append(LuckyRename.MoveReasonSelect.$element).append('<div style="height:0.5em"></div>');
			$( '.LuckyRename' ).append(LuckyRename.MoveReason.$element);
			$( '.LuckyRename' ).append(LuckyRename.buttonBar.$element);
			$( '.LuckyRename' ).append(LuckyRename.LuckyRenameSettingsContainer.$element);
			LuckyRename.loadSettings();
			if ( LuckyRename.settings.noredir || ( LuckyRename.settings.remember && !LuckyRename.settings.StoredLeaveRedir) ) { //uncheck "leave redirect" if the user configured uncheck by default in their settings or enabled memorized inputs and had it unchecked last time
				LuckyRename.LeaveRedirectCheck.setSelected(false);
			}
			LuckyRename.checkNewFilename();
	 		if ( ! LuckyRename.UserCanMoveFiles() || ( typeof document.getElementsByClassName("box-ShadowsCommons")[0] == "undefined" && typeof document.getElementsByClassName("media-move-suggestion")[0] == "undefined") ) { //disable decline button if the user isn't a file mover or there are no shadowscommons or rename media templates
				LuckyRename.declineButton.setDisabled(true);
			}
		}
		if ( typeof document.getElementsByClassName("mw-imagepage-linkstoimage-ns0")[0] == "undefined" ) { //image not used in article space, lucky suggestion not possible
			LuckyRename.disableLuckyButton();
		}
		if ( mw.config.get('wgArticleId') == 0 || typeof document.getElementsByClassName('sharedUploadNotice')[0] != "undefined") { //probably a file from Commons
			$( '.LuckyRename' ).append(LuckyRename.msgs.fileonsharedrepo + '<br />');
			LuckyRename.disableMoveButton();
		} else if ( mw.config.get('wgIsProbablyEditable') != true ) { //page is probably protected. actually doesn't guarantee it isn't move-protected
			LuckyRename.disableMoveButton();
			$( '.LuckyRename' ).append(LuckyRename.msgs.filemaybeprotected + '<br />');
		}
		$( '.luckyFMLink' )[0].hidden = true; //hide the four leaf clover that was clicked to load LuckyRename
		if (typeof document.getElementsByClassName("media-move-suggestion")[0] != "undefined") { 
			LuckyRename.NewFilenameSuggested = document.getElementsByClassName("media-move-suggestion")[0].innerText;
		} else {
			LuckyRename.NewFilenameSuggested = mw.config.get('wgPageName');
		}
		LuckyRename.NewFilenameSuggested = LuckyRename.NewFilenameSuggested.replace(/_/g, ' ').replace('File:', '').replace( /\.jpeg$/i, '.jpg').replace( /\.jpg$/i, '.jpg').replace( /\.png$/i, '.png').replace( /\.gif$/i, '.gif').replace( /\.tif$/i, '.tif').replace( /\.tiff$/i, '.tif').replace( /\.svg$/i, '.svg').replace(/\.(png|svg|jpg|jpeg|gif|tif|tiff)\.(png|svg|jpg|jpeg|gif|tif|tiff)/gi, '.$2').replace(/\.(png|svg|jpg|jpeg|gif|tif|tiff)\.(png|svg|jpg|jpeg|gif|tif|tiff)/gi, '.$2');
		LuckyRename.NewFilename.setValue(LuckyRename.NewFilenameSuggested);
		LuckyRename.checkNewFilename();

		if (typeof document.getElementsByClassName("media-move-reason")[0] != "undefined") { 
			LuckyRename.MoveReasonSuggested = filepageText;
			LuckyRename.MoveReasonSuggested = LuckyRename.MoveReasonSuggested.split("\n");
				LuckyRename.isRenameTemplate = function(line) {
					if ( line.search( LuckyRename.RenameTemplate ) != -1 ) {
					return true;
					}
				};
				if ( typeof LuckyRename.MoveReasonSuggested.filter( LuckyRename.isRenameTemplate )[0] != "undefined" ) {
				LuckyRename.MoveReasonSuggested = LuckyRename.MoveReasonSuggested.filter( LuckyRename.isRenameTemplate )[0].replace( LuckyRename.RenameTemplateReason , "$2");
			}
		}
		if (typeof document.getElementsByClassName("box-ShadowsCommons")[0] != "undefined") {
			LuckyRename.MoveReasonSuggested = LuckyRename.fncshadow;
		}
		if ( typeof LuckyRename.MoveReasonSuggested != "undefined" && LuckyRename.MoveReasonSuggested != "" ) {
			LuckyRename.MoveReasonPreload = LuckyRename.MoveReasonSuggested;
		} else if ( typeof LuckyRename.StoredMoveReason != "undefined" ) { //StoredMoveReason won't be defined if the user hasn't enabled memorize inputs
			LuckyRename.MoveReasonPreload = LuckyRename.StoredMoveReason;
		}
		if ( typeof LuckyRename.MoveReasonPreload != "undefined" ) {
			LuckyRename.MoveReason.setValue(LuckyRename.MoveReasonPreload); //first we load the suggestion, whatever it is, in the for loop it may get shortened and the FNC drop down reason selected if it's contained within
			for (int = 0; int < 20; int++) { //cycle through up to 20 FNC reasons (enwiki only has 9) to convert a suggested reason into a selected one
				if ( typeof LuckyRename.msgs['fnc' + int] != "undefined" && LuckyRename.msgs['fnc' + int] != "" && typeof LuckyRename.MoveReasonPreload != "undefined" ) {
					LuckyRename.MoveReasonPreloadEscapedRegExp = new RegExp(LuckyRename.escapeRegExp(LuckyRename.msgs['fnc' + int]));
					if ( LuckyRename.MoveReasonPreload.search(LuckyRename.MoveReasonPreloadEscapedRegExp) != -1 ) {
						LuckyRename.MoveReasonSelect.setValue(LuckyRename.msgs['fnc' + int]);
						LuckyRename.MoveReason.setValue(LuckyRename.MoveReason.getValue().replace(LuckyRename.msgs['fnc' + int], '').replace(/^, /, ''));
						CheckShadow();
					}
				}
			}
		}
		LuckyRename.checkNewFilename();
	}; //end function loadLuckyRename

	function addToolBarLink(label) {
		if ( ["vector","vector-2022"].includes(mw.config.get('skin')) ) {
			$('#right-navigation #p-views .vector-menu-content .vector-menu-content-list').append('<li class="ca-luckyrename vector-tab-noicon mw-list-item"><a>Lucky</a></li>');
			$('#ca-watch,#ca-unwatch')[0].parentElement.insertBefore($('.ca-luckyrename')[0],$('#ca-watch')[0]);
		} else if ( mw.config.get('skin') == "minerva") {
			$(document.getElementById('p-tb')).prepend('<li class="ca-luckyrename toggle-list-item"><a class="toggle-list-item__anchor menu__item--page-actions-overflow-info mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-page-actions-overflow-info mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-move"><span class="toggle-list-item__icon"><span class="toggle-list-item__label">Lucky</span></span></a></li>');
		} else if ( mw.config.get('skin') == "modern") {
			$(document.getElementById('p-cactions').children[1].children[0]).append('<li class="ca-luckyrename"><a class="collapsible">Lucky</a></li>');
		} else if ( mw.config.get('skin') == "monobook" ) {
			$(document.getElementById('p-cactions').children[1].children[0]).append('<li class="ca-luckyrename"><a class="collapsible">Lucky</a></li>');
		} else if ( mw.config.get('skin') == "timeless") {
			$(document.getElementById('p-cactions').children[1].children[0]).append('<li class="ca-luckyrename"><a class="collapsible">Lucky</a></li>');
		}
	}
	addToolBarLink('Lucky');
	$( '.fileInfo' ).append('<span class="luckyFMLink"><a style="font-size:1.5em;" class="ca-luckyrename"> 🍀</a></span>');
	$( '.ca-luckyrename' ).on( 'click', function(){LuckyRename.loadLuckyRename();});
 	if (typeof document.getElementsByClassName("media-move-suggestion")[0] != "undefined" || typeof document.getElementsByClassName("box-ShadowsCommons")[0] != "undefined") { //automatically load if there is a rename media template
		LuckyRename.loadLuckyRename();
	}	
} );
} //end only load on file pages
//</nowiki>