User:Flatscan WP/interwikiImported.js

const interwikiImported_regexpWikilink = new RegExp('\\[\\[(.*?)(#.*?)?(\\|.*?)?\\]\\]', 'g'); const interwikiImported_regexpCategory = new RegExp('^Category:'); const interwikiImported_defaultSource = 'wikipedia:';

var interwikiImported_done = false;

function interwikiImported_init { if (!wfSupportsAjax) { jsMsg(' Your browser does not seem to support AJAX, which is required for the interwikiImported script. '); return; }

form = ''+ ' Automatic interwiki conversion for imported page '+ 'Source interwiki: '+ ' ';

jsMsg(form); }

function interwikiImported_loadAndCommit { document.getElementById('interwikiImported_button_commit').disabled=true;

pageText = interwikiImported_getPageText(wgPageName); prefix = document.getElementById('interwikiImported_prefix').value; var linksToReplace = new Array;

while (true) { match = interwikiImported_regexpWikilink.exec(pageText); if (match == null) break; replace = interwikiImported_checkAndAddPrefix(prefix, match); if (replace != null) { linksToReplace.push([ match[0], replace ]); } }

for (i = 0; i < linksToReplace.length; i++) { link = linksToReplace[i]; pageText = pageText.replace(link[0], link[1]); }

es = 'Adding prefix ' + prefix + ' where pages do not exist';

token = interwikiImported_getToken; interwikiImported_commitEdit(wgPageName, token, es, '&notminor=1&text=' + encodeURIComponent(pageText));

interwikiImported_done = true; jsMsg('Wikilinks converted.');

// force reload location.reload; }

function interwikiImported_checkAndAddPrefix(prefix, matchArray) { // 0: whole, including // 1: page name // 2: following # // 3: following | if (matchArray[1] == '') { // local section link return null; } else if (interwikiImported_regexpCategory.test(matchArray[1])) { // Category: return null; } if (interwikiImported_getPageExists(matchArray[1])) { // linked page exists locally return null; } link = prefix + matchArray[1]; if (matchArray[2] != undefined) { link += matchArray[2]; } if (matchArray[3] != undefined) { display = matchArray[3]; } else { // article name sans prefix display = '|' + matchArray[1]; } return  + link + display + ; }

function interwikiImported_getPageText(pageName) { var req = sajax_init_object; req.open("GET", wgScriptPath + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(pageName), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; if (pageid == "-1") { delete req; return ''; } pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; delete req; return pagetext; }

function interwikiImported_getPageExists(pageName) { var req = sajax_init_object; req.open("GET", wgScriptPath + "/api.php?action=query&format=json&indexpageids=1&titles="+encodeURIComponent(pageName), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; delete req; return pageid > 0; }

function interwikiImported_getToken { var req = sajax_init_object; req.open("GET", wgScriptPath + "/api.php?action=query&prop=info&indexpageids=1&intoken=edit&format=json&titles="+encodeURIComponent(wgPageName), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; token = response['query']['pages'][pageid]['edittoken']; delete req; return token; }

function interwikiImported_commitEdit(title, token, summary, textParams) { success = false; var req = sajax_init_object; var params = "action=edit&format=json&token="+encodeURIComponent(token)+"&title="+encodeURIComponent(title)+"&summary="+encodeURIComponent(summary)+textParams; 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") { success = true; } else { alert('Failure on ' + title + '; Error info:' +response['error']['code'] + ' : ' + response['error']['info']); }     }      catch(err) { alert('Failure on ' + title + '; Unspecified error.'); }     delete req; } }  req.send(params); delete req; return success; }

function interwikiImported_addLink { addPortletLink("p-cactions", "javascript:interwikiImported_init", "Interwiki", "ca-interwikiImported", "Automatic interwiki conversion for imported page"); } addOnloadHook(interwikiImported_addLink);