User:Mabdul/fiximages.js

//////////////////////////////////////////////// // To use that script add following line to your Special:MyPage/skin.js (without the //): // importScript('User:Mabdul/fiximages.js'); //fiximages and Nowcommons helper //////////////////////////////////////////////// // Version 0.7xxx // mabdul: XX June 2012 // Roadmap: 0.8:  remove //         0.9:   nowcommons ok //          0.9.5: cleanup of the code, maybe redirect searches //////////////////////////////////////////////// // script parts, ideas, techniques, etc. stolen from: // * w:de:Benutzer:DerHexer/fixlinks.js // * w:en:User:MGA73/nowcommonsreview.js // * w:en:WP:AFCH //////////////////////////////////////////////// //global variable var fiximages_PageName = wgPageName.replace(/_/g, ' '); var fiximages_AJAXnumber = 0;

// token functions function fiximages_getToken(show){ if(show){ document.getElementById('fiximages_status').innerHTML += 'Getting token'; }	var req = sajax_init_object; req.open("GET", wgScriptPath + "/api.php?action=query&prop=info&indexpageids=1&intoken=edit&format=json&titles="+encodeURIComponent(fiximages_PageName), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; token = response['query']['pages'][pageid]['edittoken']; delete req; if (show) { document.getElementById('fiximages_gettoken').innerHTML = 'Got token'; }	return token; }// end token // HTML stuff function fiximages_escapeHtmlChars(original){ return original .replace(/&/g, "&amp;") .replace(//g, "&gt;") .replace(/"/g, "&quot;")     .replace(/'/g, "&#039;"); }//end HTML

//main function function fiximagesimages{ if (!wfSupportsAjax){ jsMsg(' Your browser does not seem to support AJAX, which is required for this version of Fix Images - Depending on your needs try one of the mentioned applications in the source code header. '); return; }	if(typeof(fiximages_advert) == 'undefined') fiximages_advert = ' (fiximages)'; form = ''+ ''+ ''+ ''; //check if this script is run at enwp (or secure...) or not if((wgServer == '//en.wikipedia.org')||((wgServer == '//secure.wikimedia.org') && (wgScriptPath == '/wikipedia/en/w'))) {	form += ''; }	else {	form += ''; }	form += ''+ ' '; jsMsg(form); } function fiximages_prompt(type){ var target = document.getElementById('fiximages_target').value; if(type == 'cancel'){ jsMsg(' '); }		else if(type == 'replace'){ var text = ' Replacing '+fiximages_PageName+' '+ 'Replace link with: '+ ' additional reason for the edit summary: ';			document.getElementById('fiximages_extra').innerHTML += text + ''; document.getElementById('fiximages_extra').innerHTML += '<ul id="fiximages_status"></ul><ul id="fiximages_finish"></ul>'; }		else if(type == 'remove'){ var text = ' Removing links to '+fiximages_PageName+' '+ '<label for="fiximages_reason">additional reason for the edit summary: <input type="text" id="fiximages_reason" name="fiximages_reason" value="" />'; document.getElementById('fiximages_extra').innerHTML += text + '<input type="button" id="fiximages_prompt_button" name="fiximages_prompt_button" value="Action!" onclick="fiximages_act(\''+type+'\')" />'; }		else if(type == 'add'){ //test if the template is already placed on that page document.getElementById('fiximages_extra').innerHTML += '<ul id="fiximages_status"></ul><ul id="fiximages_finish"></ul>'; var token = fiximages_getToken(true); var pagetext = fiximages_getPageText(fiximages_PageName); var find_template=/(\{\{nowcommons|\{\{now commons)/gi; if(pagetext.match(find_template)){ var text = 'This file (<a href="'+wgArticlePath.replace("$1", encodeURI(fiximages_PageName))+'" title="'+fiximages_PageName+'">'+fiximages_PageName+'</a>) transcludes already a \{\{<a href="'+wgArticlePath.replace("$1", 'Template:Nowcommons')+'" title="Template:Nowcommons">Nowcommons</a>\}\} template!'; document.getElementById('fiximages_extra').innerHTML += text; //check if the MtWC template is on that page and if, remove it			var search_for = /(\{\{(Copy|Move) to Wikimedia Commons\}\})/gi; if(pagetext.match(search_for)){ document.getElementById('fiximages_finish').innerHTML += '<span id="fiximages_finished_wrapper"><span id="fiximages_finished_main" style="display:none"><li id="fiximages_done">Done (<a href="'+wgArticlePath.replace("$1", encodeURI(fiximages_PageName))+'?action=purge" title="'+fiximages_PageName+'">Reload page</a>)</li> '; pagetext = pagetext.replace(search_for, ''); //check for the filename for the edit summary var already_at_commons = str.match(/\{\{nowcommons([\s]*?)([^\}]*?)([\s]*?)\}\}/gi); var temp1 = already_at_commons[0].replace(/\{\{Nowcommons([\s]*?)(\||\})/i, ''); if(temp1 == '\}'){ already_at_commons=fiximages_PageName; }				else{ var temp2 = temp1.replace(/\|([\s]*?)([^\}]*?)([\s]*?)\}\}/i, ''); var i = 1; var test = temp2.slice(temp2.length-i, (temp2.length-(i+1))); var temp3; while(test == ' ') {						test = temp2.slice(temp2.length-i, (temp2.length-(i+1))); i++; }					if((test =='\}')||(test =='\|')){ temp3 = temp2.slice(0, (temp2.length-2)); }					already_at_commons=temp3; }				summary = 'Removed an unnecessary \{\{Copy to Wikimedia Commons}}, file is already at Commons as \[\[' + already_at_commons + '\]\].'; document.getElementById('fiximages_extra').innerHTML += '<ul id="fiximages_status"></ul><ul id="fiximages_finish"></ul>'; fiximages_editPage(fiximages_PageName, pagetext, token, summary); } //end MtWC removal }		else {			var text = ' Add \'now commons\' to ' + fiximages_PageName + ' '+ '<label for="fiximages_target">If the file has another name at Commons, otherwise leave this textfield empty: <input type="text" id="fiximages_target" name="fiximages_target" value="File:" /> '+ '<label for="fiximages_optional_replace">Automatically replacement of the local file with the Commons file (only if labeled differently): <input type="checkbox" name="fiximages_optional_replace" id="fiximages_optional_replace" onclick="fiximages_trigger" checked="checked" /> '+ '<label for="fiximages_reason"> additional reason for the edit summary: <input type="text" id="fiximages_reason" name="fiximages_reason" value="This file was moved to Commons." />';			document.getElementById('fiximages_extra').innerHTML += text + '<input type="button" id="fiximages_prompt_button" name="fiximages_prompt_button" value="Action!" onclick="fiximages_act(\''+type+'\')" />'; }	} }

function fiximages_act(action) { //placing the nowcommons template if(action == 'add') { document.getElementById('fiximages_finish').innerHTML += '<span id="fiximages_finished_wrapper"><span id="fiximages_finished_main"><li id="fiximages_done">Done (<a href="'+wgArticlePath.replace("$1", encodeURI(fiximages_PageName))+'?action=purge" title="'+fiximages_PageName+'">Reload page</a>)</li> '; var token = fiximages_getToken(true); var text = fiximages_getPageText(fiximages_PageName); if((target == '') || (target == 'File:') || (target == fiximages_PageName) || (target == wgCanonicalNamespace+':')) {			text='\{\{Nowcommons\}\}\n'+text; target = fiximages_PageName; }		else {		text='\{\{Nowcommons|'+target+'\}\}\n'+text; }		summary = 'This file is available at Commons: ' + target + ''; //Template:Copy/Move to Wikimedia Commons removal var search_for = /(\{\{(Copy|Move) to Wikimedia Commons\}\})/g; if(search_for.test(text)){ text = text.replace(search_for, ''); summary += '; removed \{\{Copy to Wikimedia Commons}}'; }		document.getElementById('fiximages_extra').innerHTML += '<ul id="fiximages_status"></ul><ul id="fiximages_finish"></ul>'; fiximages_editPage(fiximages_PageName, text, token, summary); var do_replace = document.getElementById("fiximages_optional_replace").checked; if(do_replace && ((target != '') || (target != 'File:') || (target != fiximages_PageName) || (target != wgCanonicalNamespace+':'))){ fiximages_act('replace'); }	}//end action== add else if(action == 'replace'){ var target = document.getElementById('fiximages_target').value; if((target == '') || (target == 'File:') || (target == 'Image:') || (target == fiximages_PageName) || (target == wgCanonicalNamespace+':')) {			document.getElementById('fiximages_status').innerHTML += '<li id="fiximages_get'+escape(fiximages_PageName)+'">There is nothing to replace! (<a href="'+wgArticlePath.replace("$1", encodeURI(fiximages_PageName))+'?action=purge" title="'+fiximages_PageName+'">Reload page</a>)</li>'; }		else{ document.getElementById('fiximages_finish').innerHTML += '<span id="fiximages_finished_wrapper"><span id="fiximages_finished_main" style="display:none"><li id="fiximages_done">Done (<a href="'+wgArticlePath.replace("$1", encodeURI(fiximages_PageName))+'?action=purge" title="'+fiximages_PageName+'">Reload page</a>)</li> '; document.getElementById('fiximages_status').innerHTML += '<li id="fiximages_get'+escape(fiximages_PageName)+'">Getting links to <a href="'+wgArticlePath.replace("$1", encodeURI(fiximages_PageName))+'" title="'+fiximages_PageName+'">'+fiximages_PageName+'</a></li>'; var req = sajax_init_object; var params = '/api.php?action=query&format=json&list=imageusage&iulimit=500&iutitle='+ encodeURIComponent(mw.config.get('wgPageName')); req.open("GET", wgScriptPath + params, false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.send(params); var response = eval('(' + req.responseText + ')'); var imageusage = []; for(var i=0; i<response['query']['imageusage'].length; i++){ imageusage.push(response['query']['imageusage'][i].title) }			document.getElementById('fiximages_status').innerHTML += '<li id="fiximages_get'+escape(fiximages_PageName)+'">Replacing '+ imageusage.length +' link(s).</li>'; for(var i=0; i<imageusage.length; i++){ var text = fiximages_getPageText(imageusage[i]); var newpage = target.slice(4); var oldpage = wgPageName.slice(4); text=text.replace(oldpage, newpage); oldpage=oldpage.replace('_', ' '); text=text.replace(oldpage, newpage); var summary = 'File replacement: \[\[' + fiximages_PageName + '\]\] → \[\[' + target + '\]\]'; if(fiximages_getPageText(fiximages_reason)) summary += '; ' + fiximages_getPageText(fiximages_reason); var token = fiximages_getToken(true); fiximages_editPage(imageusage[i], text, token, summary); document.getElementById('fiximages_get'+escape(imageusage[i])).innerHTML = 'Replaced image on page \#'+ (i+1) +': <a href="'+wgArticlePath.replace("$1", encodeURI(imageusage[i]))+'" title="'+imageusage[i]+'">'+imageusage[i]+'</a>.'; }//end for //test //		var text = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&format=xml&list=backlinks&bllimit=500&blfilterredir=nonredirects&blnamespace=0|4|6|10|12|14|100&bltitle='+movefrom; //		req2.open("GET", text, false); //		req2.send(""); //		var linklist1 = req2.responseXML.getElementsByTagName("bl");

}//end if target=='' } // end replace }//end fiximages_act

function fiximages_editPage(title, newtext, token, summary) { summary += fiximages_advert; document.getElementById('fiximages_finished_wrapper').innerHTML = '<span id="fiximages_AJAX_finished_'+fiximages_AJAXnumber+'" style="display:none">' + document.getElementById('fiximages_finished_wrapper').innerHTML + ' '; var func_id = fiximages_AJAXnumber; fiximages_AJAXnumber++; document.getElementById('fiximages_status').innerHTML += '<li id="fiximages_edit'+escape(title)+'">Editing <a href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a></li>'; var req = sajax_init_object; var params = "action=edit&format=json&token="+encodeURIComponent(token)+"&title="+encodeURIComponent(title)+"&text="+encodeURIComponent(newtext)+"&notminor=1&summary="+encodeURIComponent(summary); url = wgScriptPath + "/api.php"; req.open("POST", url, true); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { response = eval('(' + req.responseText + ')'); try { if (response['edit']['result'] == "Success") { document.getElementById('fiximages_edit'+escape(title)).innerHTML = 'Saved <a href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a>'; } else { document.getElementById('fiximages_edit'+escape(title)).innerHTML = ' Edit failed on <a href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a>. Error info:' +response['error']['code'] + ' : ' + response['error']['info']; }				}				catch(err) { document.getElementById('fiximages_edit'+escape(title)).innerHTML = ' Edit failed on <a href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a> '; }				document.getElementById('fiximages_AJAX_finished_'+func_id).style.display = ''; delete req; }		};		req.send(params); }

function fiximages_getPageText(title) { document.getElementById('fiximages_status').innerHTML += '<li id="fiximages_get'+escape(title)+'">Getting <a href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a></li>'; var req = sajax_init_object; var params = "action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles=" + encodeURIComponent(title); req.open("POST", wgScriptPath + "/api.php", false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.send(params); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; if (pageid == "-1") { document.getElementById('fiximages_get'+escape(title)).innerHTML = '<a class="new" href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a> does not exist'; delete req; return ''; }	pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; delete req; document.getElementById('fiximages_get'+escape(title)).innerHTML = 'Got source code of <a href="'+wgArticlePath.replace("$1", encodeURI(title))+'" title="'+title+'">'+title+'</a>'; return pagetext; }

// add Link "fiximages" to the top if(wgNamespaceNumber==6 || wgCanonicalSpecialPageName == "Whatlinkshere") {  $(function   {     mw.util.addPortletLink('p-cactions', "javascript:fiximagesimages;", "fiximages", 't-fiximages', 'fix Links', '',  document.getElementById('t-whatlinkshere'));  }); }

//function to disable the textbox function fiximages_trigger{ if(fiximages_optional_replace.checked){ fiximages_reason.disabled=false; }else{ fiximages_reason.disabled=true; } }