Uzanto:Dtrebbien/movuKategorion.js

Notu: Post konservado vi forviŝu la kaŝmemoron de via foliumilo por vidi la ŝanĝojn : Mozilo: alklaku Reŝarĝi (aŭ Stir-Shift-R), IE / Opera: Stir-F5, Safari: Cmd-R, Konqueror Stir-R.

if(!RegExp.escape)
// http://simonwillison.net/2006/Jan/20/escape/
RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      /*'/',*/ '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
};

function movuKategorion(novaTitolo)
{
   if(novaTitolo == null || novaTitolo == "")
   {
      novaTitolo = window.prompt("Kio estas la nova kategoria titolo?")
   }

   if(novaTitolo != null && novaTitolo != "")
   {
      window.status = "Kalkuli la nombron da paĝoj en la kategorio...";
      var xhr = sajax_init_object();
      xhr.open("GET", mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php"+
         "?action=query&format=xml&list=categorymembers&cmlimit=12&cmprop=title&cmtitle=Kategorio:"+encodeURIComponent(wgTitle), false);
      xhr.send(null);
      var cms = xhr.responseXML.getElementsByTagName("cm");
      window.status = "";
      if(window.confirm("Estas almenaŭ "+cms.length+" paĝoj en `Kategorio:"+wgTitle+"`. Ĉu vi estas certa, ke vi volas movi la kategorion al `Kategorio:"+novaTitolo+"`?"))
      {
         for(var i = 0; i < cms.length; i++)
         {
            var cm = cms[i];
            var titolo = cm.getAttribute("title");
            
            window.status = "Modifi "+titolo+"...";
            
            // akiri "edit token"
            xhr.abort();
            xhr.open("GET", mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php"+
               "?action=query&format=xml&prop=info&intoken=edit&titles="+encodeURIComponent(titolo), false);
            xhr.send(null);
            var editToken = xhr.responseXML.getElementsByTagName("page")[0].getAttribute("edittoken");
            
            xhr.abort();
            xhr.open("GET", mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/index.php?action=raw&title="+encodeURIComponent(titolo), false);
            xhr.send(null);
            var vikiteksto = xhr.responseText;
            
            var re = new RegExp("\\[\\[\\s*[Kk]ategorio:"+RegExp.escape(wgTitle)+"\\s*((|[^\\]]*)?)\\s*\\]\\]");
            vikiteksto = vikiteksto.replace(re, "[[Kategorio:"+novaTitolo+"$1]]");
            
            var korpo = "text="+encodeURIComponent(vikiteksto);
            xhr.abort();
            xhr.open("POST", mw.config.get('wgServer')+mw.config.get('wgScriptPath')+"/api.php"+
               "?action=edit&format=xml&token="+encodeURIComponent(editToken)+
               "&title="+encodeURIComponent(titolo)+
               "&minor=1&bot=1&nocreate=1"+
               "&summary="+encodeURIComponent("-[[:Kategorio:"+wgTitle+"]], +[[:Kategorio:"+novaTitolo+"]]"), false);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.setRequestHeader("Content-Length", korpo.length);
            xhr.send(korpo);
            
            var edits = xhr.responseXML.getElementsByTagName("edit");
            if(edits.length <= 0 || edits[0].getAttribute("result") != "Success")
            {
               window.alert("Malsukcesis redakti `"+titolo+"`");
               break;
            }
         }
         
         window.status = "";
         window.alert("Fininta.");
      }
   }
}

$(function() {
   if(wgNamespaceNumber == 14) // Kategorio:
   {
      mw.util.addPortletLink("p-cactions", "javascript:movuKategorion()",
         "movu", "ca-move", "Alinomigi tiun ĉi kategorion",
         null, document.getElementById("ca-history"));
   }
});