MediaWiki:MassEdit.js

function doMassEdit { document.getElementById("wpMassEditSubmit").disabled = true; var articles = document.getElementById("wpMassEditPages").value.split("\n"); if(articles.length == 0) return; var newtext = document.getElementById("wpMassEditText1").value; var text2 = document.getElementById("wpMassEditText2").value; var wpMassEditType = document.getElementById("wpMassEditType").value, wpMassEditSummary = document.getElementById("wpMassEditSummary").value, edited = 0, failed = new Array, error = new Array; var wpMassEditMinor = document.getElementById("wpMassEditMinor").checked; var wpMassEditWatch = document.getElementById("wpMassEditWatch").checked; for(i=0;i 0) { var req = new XMLHttpRequest; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?format=json&action=query&prop=info&intoken=edit&titles=" + encodeURIComponent(article), false); req.send(null); var response = eval("(" + req.responseText + ")").query.pages; for(var index in response) { var info = response[index]; var edittoken = info.edittoken; var postdata = "format=json" + "&action=edit" + "&title=" + encodeURIComponent(article) + "&summary=" + encodeURIComponent(wpMassEditSummary) + "&token=" + encodeURIComponent(edittoken); if(wpMassEditMinor) postdata += "&minor=1"; else postdata += "&notminor=1"; if( wpMassEditWatch) postdata += "&watch=1"; else postdata += "&notwatch=1"; if(wpMassEditType == "prepend"){ postdata += "&prependtext=" + encodeURIComponent(newtext + "\n"); }		else if(wpMassEditType == "append"){ postdata += "&appendtext=" + encodeURIComponent("\n" + newtext); }		else if(wpMassEditType == "replacepage"){ postdata += "&text=" + encodeURIComponent(newtext); }		else if(wpMassEditType == "replacetext" || wpMassEditType == "bothpend"){ var pagetext = gettext(article); if(wpMassEditType == "replacetextg"){ newtext = newtext.replace(/([.*+?|{}\[\]\\])/g, "\\$1"); pagetext = pagetext.replace(new RegExp(newtext, "g"), text2); }			else if(wpMassEditType == "replacetext"){ pagetext = pagetext.replace(newtext, text2); }			else pagetext = newtext + pagetext + text2; postdata += "&text=" + encodeURIComponent(pagetext); }               var req = new XMLHttpRequest; req.open("POST", mw.config.get('wgScriptPath') + "/api.php", false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", postdata.length); req.send(postdata); if(eval("(" + req.responseText + ")").edit ) { //If edited, update the count and the button. edited++; document.getElementById("wpMassEditSubmit").value = "(" + edited + ")"; } else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array. failed.push(article); error.push(eval("(" + req.responseText + ")").error.info); }           }        }        if(!articles[i+1]) { document.getElementById("wpMassEditSubmit").value = "Done (" + edited + ")"; if(failed.length > 0) { var linkedList = ""; for(x=0; x" + failed[x] + ": " + error[x] + ""; //Links the titles in the "failed" array }               document.getElementById("wpMassEditFailedContainer").innerHTML += ' Failed edits:' + linkedList + ''; }       }    } }

function gettext(title){ var req = new XMLHttpRequest; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; if (pageid == "-1") { delete req; return ''; }	var pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; return pagetext; } function masseditform { var bodyContent = (mw.config.get( 'skin' ) == "cologneblue" ? "article" : "mw-content-text"); document.getElementsByTagName("h1")[0].textContent = "Mass edit"; document.title = "Mass edit"; document.getElementById(bodyContent).innerHTML = 'Sitename

'       + '' + 'Please be careful!' + ' '       + '

'           + 'Pages to edit (each page on its own line): '               + ' ' + 'Text 1: '               + ' ' + 'Text 2: '               + ' ' + '

'               + '  Type of edit: ' + ' ' + ' Add text 1 at the beginning of the page ' + ' Add text 1 at the end of the page ' + ' Add text 1 at the beginning of the page, text 2 at the end of the page ' + ' The first time "Text 1" appears, replace it with text 2 ' + 'Replace "Text 1" with text 2 every time it appears ' + ' Replace the content of the page with text 1 ' + ' '            + '  Summary of changes: ' + '  ' + ' Mark changes as minor: ' + '  ' + ' Watch these pages: ' + ' <input type="checkbox" id="wpMassEditWatch" name="wpMassEditWatch"/> ' + ' <input type="button" id="wpMassEditSubmit" name="wpMassEditSubmit" value="Edit" onclick="doMassEdit" /> ' + ' '; } var massedit_whitelist = ["Sven Manguard", "DeltaQuad.alt", "T. Canens"]; if(mw.config.get( 'wgNamespaceNumber' ) == -1 && (mw.config.get( 'wgPageName' ) == "Special:Massedit" || mw.config.get( 'wgPageName' ) == "Special:MassEdit") && ((/autopatrolled/.test(mw.config.get( 'wgUserGroups' )) || /sysop/.test(mw.config.get( 'wgUserGroups' )) || /bot/.test(mw.config.get( 'wgUserGroups' ))) || massedit_whitelist.indexOf(mw.config.get( 'wgUserName' )) != -1 )) $(masseditform);