User:Arbitrarily0/filemove.js

/* Ajax file move module, version [0.0.4b]


 * All credit to User:Splarka, originally from http://en.wikipedia.org/wiki/User:Splarka/ajaxfilemove.js



var sfmMoveToken = ''; var sfmNewTitle = ''; if(wgNamespaceNumber == 6 && wgCurRevisionId) { addOnloadHook(function {   var move = document.getElementById('ca-move');    if(!move) return    var sm = addPortletLink('p-cactions','javascript:sfmInit','speedy move','ca-gonzales','Speed move this File','i',move);  }); }

function sfmInit { var theext = wgPageName.split('.')[wgPageName.split('.').length-1]; var theextext = theext.toLowerCase; var def = wgCanonicalNamespace + ':TITLE.' + theextext; var suggest = getElementsByClassName(document,'span','media-move-suggestion'); if(suggest.length > 0) def = getInnerText(suggest[0]) var newtitle = prompt('Move file to page:',def); if(!newtitle) return if(newtitle.toLowerCase.indexOf(wgCanonicalNamespace.toLowerCase + ':') != 0) { alert('You can\'t move files to other namespaces'); return; } var oldext = wgPageName.split('.')[wgPageName.split('.').length-1]; var newext = newtitle.split('.')[newtitle.split('.').length-1]; if(oldext.toLowerCase != newext.toLowerCase) { var conf = confirm('Warning: you appear to be changing this file\'s extension from "' + oldext + '" to "' + newext + '".\nWhile this is works sometimes for very old files uploaded with the wrong\nextension, this is rare. Are you really sure you want to try that?'); if(!conf) return }

var bar = document.getElementById('contentSub') || document.getElementById('topbar'); var out = document.createElement('pre'); out.setAttribute('id','sfm-output'); appendCSS('#sfm-output {border:1px solid black !important;padding:.5em;overflow:auto;font-size:120%}'); bar.appendChild(out);

var badchars = /([#<>\[\]{}|\/]|\:.*\:)/; if(newtitle == wgPageName || newtitle.indexOf(wgCanonicalNamespace + ':') != 0 || badchars.test(newtitle)) { out.appendChild(document.createTextNode('! That seems like a bad title to me: ' + newtitle + '')); return; }

out.appendChild(document.createTextNode('* Input accepted, fetching edit/move tokens and page text...\n')); document.getElementById('ca-gonzales').style.display = 'none';

var url = wgScriptPath + '/api.php?action=query&prop=info|revisions&rvprop=content|timestamp&indexpageids=1&intoken=edit|move&format=json&titles=' + encodeURIComponent(mw.config.get('wgPageName')); var req = sajax_init_object; req.open('GET', url, true); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { eval("sfmTokens(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')"); } }  sfmNewTitle = newtitle; req.send(null); }

function sfmTokens(obj,txt) { //http://test.wikipedia.org/w/api.php?action=query&prop=info|revisions&rvprop=content|timestamp&indexpageids=1&intoken=edit|move&format=jsonfm&titles=File:Test.png var out = document.getElementById('sfm-output'); if(obj['error']) { out.appendChild(document.createTextNode('! Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info'] + '\n')); return; } if(!obj['query'] || !obj['query']['pageids'] || !obj['query']['pages'][obj['query']['pageids'][0]] || !obj['query']['pages'][obj['query']['pageids'][0]]['movetoken'] || !obj['query']['pages'][obj['query']['pageids'][0]]['edittoken'] || !obj['query']['pages'][obj['query']['pageids'][0]]['revisions'][0]['timestamp'] || !obj['query']['pages'][obj['query']['pageids'][0]]['revisions'][0]['*']) { out.appendChild(document.createTextNode('? Unexpected response: ' + txt + '\n')); return; } if(obj['query']['pages'][obj['query']['pageids'][0]]['redirect']) { out.appendChild(document.createTextNode('! This file is apparently a redirect, and should not be moved again.\n')); return; } var edittoken = obj['query']['pages'][obj['query']['pageids'][0]]['edittoken']; var movetoken = obj['query']['pages'][obj['query']['pageids'][0]]['movetoken']; var pagetext = obj['query']['pages'][obj['query']['pageids'][0]]['revisions'][0]['*']; var timestamp = obj['query']['pages'][obj['query']['pageids'][0]]['revisions'][0]['timestamp']; var startstamp = obj['query']['pages'][obj['query']['pageids'][0]]['starttimestamp'];

out.appendChild(document.createTextNode('* Got tokens and text, searching for templates...\n')); var templates = /\{\{[\s\n\t]*(ifr|rename[ _]*media|rename[ _]*image|ImageRename|rename)[\s\n\t]*(\|[^\}]*|)\}\}/ig; var found = templates.test(pagetext); if(!found) { out.appendChild(document.createTextNode('! No automatically removable templates found, please continue manually.')); return; } out.appendChild(document.createTextNode('* Move template(s) found and removed, attempting edit...\n')); pagetext = pagetext.replace(templates,'');

var params = 'action=edit&format=json&title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&text=' + encodeURIComponent(pagetext) + '&token=' + encodeURIComponent(edittoken) + '&summary=' + encodeURIComponent('closed rename request') + '&minor=1&basetimestamp=' + timestamp.replace(/[^\d]/g,''); if(startstamp) params += '&starttimestamp=' + startstamp.replace(/[^\d]/g,'') var url = wgScriptPath + '/api.php'; var req = sajax_init_object; 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) { eval("sfmEdit(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')"); } }  sfmMoveToken = movetoken; req.send(params); }

function sfmEdit(obj,txt) { var out = document.getElementById('sfm-output'); if(obj['error']) { out.appendChild(document.createTextNode('! Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info'] + '\n* Please finish manually.')); return; } else if(obj['edit'] && obj['edit']['result']) { out.appendChild(document.createTextNode('* Edit returned as "' + obj['edit']['result'] + '", attempting to move page to ' + sfmNewTitle + '... \n')); } else { out.appendChild(document.createTextNode('? Unexpected response: ' + txt + '\n* Please finish manually.')); return; }

var params = 'action=move&format=json&token=' + encodeURIComponent(sfmMoveToken) + '&movetalk=1&from=' + encodeURIComponent(mw.config.get('wgPageName')) + '&to=' + encodeURIComponent(sfmNewTitle) + '&reason=' + encodeURIComponent('more suitable name');

var url = wgScriptPath + '/api.php'; var req = sajax_init_object; 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) { eval("sfmMove(" + req.responseText + ",'" + req.responseText.replace(/\'/g,"`") + "')"); } }  req.send(params); }

function sfmMove(obj,txt) { var out = document.getElementById('sfm-output'); if(obj['error']) { out.appendChild(document.createTextNode('* Api error: ' + obj['error']['code'] + ' - ' + obj['error']['info'] + '\n* Please finish manually.')); } else if(obj['move'] && obj['move']['from'] && obj['move']['to']) { out.appendChild(document.createTextNode('* File ' + obj['move']['from'] + ' moved to ' + obj['move']['to'] + '\n* Be sure to ')); var a = document.createElement('a'); a.setAttribute('href',wgScript + '?title=Special:WhatLinksHere&hidelinks=1&hidetrans=1&target=' + encodeURIComponent(obj['move']['to'])); a.appendChild(document.createTextNode('Check for double redirects!')); out.appendChild(a); } else { out.appendChild(document.createTextNode('? Unexpected response: ' + txt + '\n* Please finish manually.')); } }