לדלג לתוכן

מדיה ויקי:Gadget-Cat-a-lot.js: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
use doc ready
שורה 1: שורה 1:
// <source lang="javascript">
//<pre><nowiki>
 
//Install by copying the following line to your monobook.js user sub-page:
// document.write('<script type="text/javascript" src="http://commons.wikimedia.org/w/index.php?title=MediaWiki:Cat-a-lot.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');
 
// Variables
// Variables
var cat_a_lot_mark_color = "#3399FF" ;
var cat_a_lot_mark_color = "#3399FF" ;
var cat_a_lot_api_url = wgScriptPath + "/api.php" ;
var cat_a_lot_api_url = "/w/api.php" ;
var cat_a_lot_edit_url = wgScript + "?title=" ;
var cat_a_lot_query_url = "/w/query.php" ;
var cat_a_lot_divtype = 'div' ;
var cat_a_lot_edit_url = "/w/index.php?title=" ;
var cat_a_lot_classname = 'gallerytext' ;
 
var cat_a_lot_searchmode = false ;
var cat_a_lot_parent;


String.prototype.trim = function () {
String.prototype.trim = function () {
   return this.replace (/^\s+|\s+$/g, "");
   var s = this ;
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
}


שורה 24: שורה 33:
   return this.split(s).join(r);
   return this.split(s).join(r);
}
}


if ( wgNamespaceNumber == 14 ) {
if ( wgNamespaceNumber == 14 ) {
  $j(document).ready ( init_cat_a_lot ) ;
//  document.addEventListener("DOMContentLoaded", init_cat_a_lot, false);
  addOnloadHook ( init_cat_a_lot ) ;
}
}
if ( wgNamespaceNumber == -1 && wgCanonicalSpecialPageName == "Search" ) {
  cat_a_lot_divtype = 'table' ;
  cat_a_lot_classname = 'searchResultImage' ;
  cat_a_lot_searchmode = true ;
  $j(document).ready ( init_cat_a_lot ) ;
}


// Add this to URLs to get around cache issues
// Add this to URLs to get around cache issues
שורה 45: שורה 46:


function init_cat_a_lot () {
function init_cat_a_lot () {
  cat_a_lot_parent = document.getElementById( "mw-category-media" ) || getElementsByClassName( document, "ul", "mw-search-results" )[0];
  if( !cat_a_lot_parent) return;
   var c1_div = document.getElementById("column-one") ;
   var c1_div = document.getElementById("column-one") ;
  if (!c1_div) c1_div = document.getElementById("mw-panel");
   c1_div.innerHTML += '<div id="cat_a_lot" style="z-index:9999;display:block;background-color:white; width:108px height:145px; position:fixed; bottom:0; right:0;">' +  
  if (!c1_div) return;
   c1_div.innerHTML += '<div id="cat_a_lot" style="z-index:9999;display:block;background-color:white; position:fixed; bottom:0; right:0;">' +  
     '<div class="cat_a_lot_data" id="cat_a_lot_data" style="display:none;border:1px solid black">' +
     '<div class="cat_a_lot_data" id="cat_a_lot_data" style="display:none;border:1px solid black">' +


שורה 57: שורה 53:
     '<form name="cat_a_lot_searchcat" method="post" onSubmit="return false;">' +  
     '<form name="cat_a_lot_searchcat" method="post" onSubmit="return false;">' +  
     '<input type="text" input="8" name="cat_a_lot_searchcatname" />' +
     '<input type="text" input="8" name="cat_a_lot_searchcatname" />' +
     '<input type="submit" name="cat_a_lot_go" value="Go" onclick="cat_a_lot_search_cat();"/>' +
     '<input type="button" name="cat_a_lot_go" value="Go" onclick="cat_a_lot_search_cat();"/>' +
     '</form>' +
     '</form>' +
     '</div>' +
     '</div>' +
שורה 81: שורה 77:
     '<form><input id="cat_a_lot_mode" type="checkbox" onchange="run_cat_a_lot();"/><label for="cat_a_lot_mode">Cat-a-lot mode</label></form>' +
     '<form><input id="cat_a_lot_mode" type="checkbox" onchange="run_cat_a_lot();"/><label for="cat_a_lot_mode">Cat-a-lot mode</label></form>' +
     '</div></div>' ;
     '</div></div>' ;
if( skin == "vector" )
  appendCSS( "#cat_a_lot {font-size: .75em};");
}
}


function cat_a_lot_get_marked_labels () {
function cat_a_lot_get_marked_labels () {
   var ret = new Array () ;
   var ret = new Array () ;
   var divs = getElementsByClassName( cat_a_lot_parent, cat_a_lot_divtype, cat_a_lot_classname);
   var divs = document.getElementsByTagName ( "div" ) ;
   for ( var i = 0 ; i < divs.length ; i++ ) {
   for ( i = 0 ; i < divs.length ; i++ ) {
    if ( divs[i].className != "gallerytext" ) continue ;
     if ( divs[i].style.backgroundColor == "" ) continue ;
     if ( divs[i].style.backgroundColor == "" ) continue ;
     var files = divs[i].getElementsByTagName ( "a" ) ;
     var files = divs[i].getElementsByTagName ( "a" ) ;
    var t = cat_a_lot_gettitle(files) ;
     ret.push ( files[0].title ) ;
     ret.push ( t ) ;
   }
   }
   return ret ;
   return ret ;
שורה 112: שורה 106:


function cat_a_lot_alter_label ( title , mode ) {
function cat_a_lot_alter_label ( title , mode ) {
   var divs = getElementsByClassName( cat_a_lot_parent, cat_a_lot_divtype, cat_a_lot_classname ) ;
   var divs = document.getElementsByTagName ( "div" ) ;
   for ( var i = 0 ; i < divs.length ; i++ ) {
   for ( i = 0 ; i < divs.length ; i++ ) {
    if ( divs[i].className != "gallerytext" ) continue ;
     var files = divs[i].getElementsByTagName ( "a" ) ;
     var files = divs[i].getElementsByTagName ( "a" ) ;
     if ( files.length == 0 ) continue ;
     if ( files.length == 0 ) continue ;
     if ( cat_a_lot_gettitle(files) != title ) continue ;
     if ( files[0].title != title ) continue ;


     if ( mode == "move" ) {
     if ( mode == "move" ) {
       divs[i].innerHTML += "<div style=\"width:100%;color:white;background-color:red;text-align:center;\">Moved to new category</div>" ;
       divs[i].innerHTML = "<div style='width:100%;color:white;background-color:red;text-align:center'>Moved to new category</div>" ;
       divs[i].style.backgroundColor = "#FF0000" ;
       divs[i].style.backgroundColor = "#FF0000" ;
     } else if ( mode == "remove" ) {
     } else if ( mode == "remove" ) {
       divs[i].innerHTML += "<div style=\"width:100%;color:white;background-color:red;text-align:center;\">Removed from category</div>" ;
       divs[i].innerHTML = "<div style='width:100%;color:white;background-color:red;text-align:center'>Removed from category</div>" ;
       divs[i].style.backgroundColor = "#FF0000" ;
       divs[i].style.backgroundColor = "#FF0000" ;
     } else if ( mode == "copy" ) {
     } else if ( mode == "copy" ) {
       divs[i].innerHTML += "<div style=\"width:100%;font-weight:bold;\">Copied to new category!</div>" ;
       divs[i].innerHTML += "<br/><b>Copied to new category!</b>" ;
     }
     }


שורה 141: שורה 136:
function cat_a_lot_fix_quotes ( text , quote ) {
function cat_a_lot_fix_quotes ( text , quote ) {
   return text.split(quote).join("\\"+quote) ;
   return text.split(quote).join("\\"+quote) ;
}
function cat_a_lot_gettitle( files ) {
  var ret = "";
  if( files.length > 0 ) {
      var flag = ( cat_a_lot_searchmode && files.length > 1 ) ? 1 : 0;
      ret = files[flag].title;
      if( files[flag].originalTitle ) // navpopups mode
        ret = files[flag].originalTitle;
  }
  return ret;
}
}


function cat_a_lot_mark_all ( how ) {
function cat_a_lot_mark_all ( how ) {
   var divs = getElementsByClassName( cat_a_lot_parent, cat_a_lot_divtype, cat_a_lot_classname ) ;
   var divs = document.getElementsByTagName ( "div" ) ;
   for ( var i = 0 ; i < divs.length ; i++ ) {
   for ( i = 0 ; i < divs.length ; i++ ) {
    if ( divs[i].className != "gallerytext" ) continue ;
     var files = divs[i].getElementsByTagName ( "a" ) ;
     var files = divs[i].getElementsByTagName ( "a" ) ;
     if ( files.length == 0 ) continue ;
     if ( files.length == 0 ) continue ;
שורה 162: שורה 147:
     divs[i].style.backgroundColor = "" ; // Deselect
     divs[i].style.backgroundColor = "" ; // Deselect
     if ( how == 1 ) { // Activate
     if ( how == 1 ) { // Activate
      var title = cat_a_lot_gettitle( files );
       eval("divs[i].onclick = function () { cat_a_lot_on_click_label('" + cat_a_lot_fix_quotes ( files[0].title , "'" ) + "'); };") ;
      if ( cat_a_lot_searchmode )
        divs[i] = divs[i].getElementsByTagName('div')[0] ;
 
       eval("divs[i].onclick = function () { cat_a_lot_on_click_label('" + cat_a_lot_fix_quotes ( title , "'" ) + "'); };") ;
     } else if ( how == 2 ) { // Select
     } else if ( how == 2 ) { // Select
       divs[i].style.backgroundColor = cat_a_lot_mark_color ;
       divs[i].style.backgroundColor = cat_a_lot_mark_color ;
שורה 177: שורה 158:


function cat_a_lot_get_subcategories () {
function cat_a_lot_get_subcategories () {
   var url = cat_a_lot_api_url + "?action=query&format=xml&list=categorymembers&cmnamespace=14&cmlimit=50&cmtitle=Category:" + encodeURIComponent (cat_a_lot_current_category) + cat_a_lot_dummy_time() ;
   var url = cat_a_lot_query_url + "?what=category&format=xml&cpnamespace=14&cptitle=" + cat_a_lot_current_category + cat_a_lot_dummy_time() ;
   var xmlreq = sajax_init_object() ;
   var xmlreq = new XMLHttpRequest() ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.send ( null ) ;
   xmlreq.send ( null ) ;
   xml = xmlreq.responseXML ;
   xml = xmlreq.responseXML ;
   var cls = xml.getElementsByTagName('cm') ;
   var cls = xml.getElementsByTagName('title') ;
   var ret = new Array () ;
   var ret = new Array () ;
   for ( var i = 0 ; i < cls.length ; i++ ) {
   for ( var i = 0 ; i < cls.length ; i++ ) {
    if (cls[i].getAttribute('ns') != 14) continue;  // XXX: partial workaround for MW API bug (cmnamespace parameter doesn't work)
     var title = cls[i].firstChild.data ;
     var title = cls[i].getAttribute('title');
     title = title.split(":",2)[1] ;
     title = title.split(":",2)[1] ;
     ret.push ( title ) ;
     ret.push ( title ) ;
שורה 194: שורה 174:


function cat_a_lot_get_parent_categories () {
function cat_a_lot_get_parent_categories () {
   var url = cat_a_lot_api_url + "?action=query&format=xml&prop=categories&titles=Category:" + encodeURIComponent (cat_a_lot_current_category) + cat_a_lot_dummy_time() ;
   var url = cat_a_lot_api_url + "?action=query&format=xml&prop=categories&titles=Category:" + cat_a_lot_current_category + cat_a_lot_dummy_time() ;
   var xmlreq = sajax_init_object() ;
   var xmlreq = new XMLHttpRequest() ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.send ( null ) ;
   xmlreq.send ( null ) ;
שורה 210: שורה 190:


function cat_a_lot_replace_category ( text , sourcecat , targetcat , catname , ending ) {
function cat_a_lot_replace_category ( text , sourcecat , targetcat , catname , ending ) {
//  alert ( "[[" + catname + ":" + sourcecat + ending + " : " + "[[" + "Category:" + targetcat + ending ) ;
//  alert ( "[[" + catname + ":" + sourcecat + ending + " : " + "[[Category:" + targetcat + ending ) ;
   return text.replaceAll ( "[[" + catname + ":" + sourcecat + ending , "[[" + "Category:" + targetcat + ending ) ;
   return text.replaceAll ( "[[" + catname + ":" + sourcecat + ending , "[[Category:" + targetcat + ending ) ;
}
}


שורה 226: שורה 206:
function cat_a_lot_get_article_timestamp ( title ) {
function cat_a_lot_get_article_timestamp ( title ) {
   var ret ;
   var ret ;
   var url = cat_a_lot_api_url + '?action=query&format=xml&prop=revisions&rvprop=timestamp&titles=' + title ;
   var url = cat_a_lot_query_url + '?format=xml&what=revisions&rvlimit=1&rvcomments&titles=' + title ;


   var xmlreq = sajax_init_object() ;
   var xmlreq = new XMLHttpRequest() ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.send ( null ) ;
   xmlreq.send ( null ) ;
   var xml = xmlreq.responseXML ;
   var xml = xmlreq.responseXML ;
   var rev = xml.getElementsByTagName('rev') ;
   var rev = xml.getElementsByTagName('rv') ;
   if ( rev.length == 0 ) return cat_a_lot_get_current_utc_timestamp () ;
   if ( rev.length == 0 ) return cat_a_lot_get_current_utc_timestamp () ;


   ret = rev.item(0).getAttribute('timestamp') ;
   ret = rev.item(0).getAttribute('timestamp') ;
   ret = cat_a_lot_fix_time ( ret ) ;
   return cat_a_lot_fix_time ( ret ) ;
  return ret
}
}


שורה 247: שורה 226:
   while ( parts.length > 0 ) {
   while ( parts.length > 0 ) {
     var s = parts.shift () ;
     var s = parts.shift () ;
     if ( ending == "|" ) {
     if ( ending == "|" ) s = s.split("]]",2).pop () ;
      var endOfLink = s.indexOf ("]]");
      if (endOfLink >= 0) s = s.substring (endOfLink + 2);
    }
     text += s ;
     text += s ;
   }
   }
שורה 258: שורה 234:
function cat_a_lot_edit_move ( filename , targetcat , mode ) {
function cat_a_lot_edit_move ( filename , targetcat , mode ) {
   var sourcecat = wgTitle ;
   var sourcecat = wgTitle ;
   var url = cat_a_lot_edit_url + encodeURIComponent (filename) + "&action=raw" + cat_a_lot_dummy_time() ;
   var url = cat_a_lot_edit_url + filename + "&action=raw" + cat_a_lot_dummy_time() ;
   var xmlreq = sajax_init_object() ;
   var xmlreq = new XMLHttpRequest() ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.open ( "GET" , url , false ) ;
   xmlreq.send ( null ) ;
   xmlreq.send ( null ) ;
שורה 276: שורה 252:
   var text = otext ;
   var text = otext ;
   if ( mode == 'copy' ) {
   if ( mode == 'copy' ) {
     text += "\n[[" + "Category:" + targetcat + "]]\n" ;
     text += "\n[[Category:" + targetcat + "]]\n" ;
   } else if ( mode == 'move' ) {
   } else if ( mode == 'move' ) {
     text = cat_a_lot_replace_category ( text , sourcecat.ucFirst() , targetcat , "category" , "]]" ) ;
     text = cat_a_lot_replace_category ( text , sourcecat.ucFirst() , targetcat , "category" , "]]" ) ;
שורה 307: שורה 283:


   var comment ;
   var comment ;
   if ( mode == "move" ) comment = "Moving from [[" + "Category:" + sourcecat + "]] to [[" + "Category:" + targetcat + "]]" ;
   if ( mode == "move" ) comment = "Moving from [[Category:" + sourcecat + "]] to [[Category:" + targetcat + "]]" ;
   if ( mode == "copy" ) comment = "Copying from [[" + "Category:" + sourcecat + "]] to [[" + "Category:" + targetcat + "]]" ;
   if ( mode == "copy" ) comment = "Copying from [[Category:" + sourcecat + "]] to [[Category:" + targetcat + "]]" ;
   if ( mode == "remove" ) comment = "Removing from [[" + "Category:" + sourcecat + "]]" ;
   if ( mode == "remove" ) comment = "Removing from [[Category:" + sourcecat + "]]" ;
   url = cat_a_lot_edit_url + encodeURIComponent(filename) + "&action=edit" ;
   url = cat_a_lot_edit_url + filename + "&action=edit" ;
   document.cat_a_lot_edit.action = url ;
   document.cat_a_lot_edit.action = url ;
   document.cat_a_lot_edit.wpSummary.value = comment ;
   document.cat_a_lot_edit.wpSummary.value = comment ;
שורה 347: שורה 323:


function cat_a_lot_move_here ( targetcat ) {
function cat_a_lot_move_here ( targetcat ) {
   cat_a_lot_do_something ( cat_a_lot_decode (targetcat) , 'move' ) ;
   cat_a_lot_do_something ( targetcat , 'move' ) ;
}
}


function cat_a_lot_copy_here ( targetcat ) {
function cat_a_lot_copy_here ( targetcat ) {
   cat_a_lot_do_something ( cat_a_lot_decode (targetcat) , 'copy' ) ;
   cat_a_lot_do_something ( targetcat , 'copy' ) ;
}
}


שורה 367: שורה 343:
     cat_a_lot_edit_move ( files[i] , targetcat , mode ) ;
     cat_a_lot_edit_move ( files[i] , targetcat , mode ) ;
   }
   }
}
function cat_a_lot_htmlize (str) {
  return str.replace (/&/g,    '&amp;')
            .replace (/\xa0/g, '&nbsp;')
            .replace (/</g,    '&lt;')
            .replace (/>/g,    '&gt;')
            .replace (/\"/g,  '&quot;') // " // Fix syntax coloring
            .replace (/\'/g,  '&apos;') // ' // Fix syntax coloring
  ;
}
function cat_a_lot_encode (str) {
  return encodeURIComponent (str).replace (/'/g, '%27').replace (/"/g, '%22');
}
function cat_a_lot_decode (str) {
  return decodeURIComponent (str.replace (/%27/g, "'").replace (/%22/g, '"'));
}
}


שורה 392: שורה 350:
   for ( var i = 0 ; i < list.length ; i++ ) {
   for ( var i = 0 ; i < list.length ; i++ ) {
     ret += "<tr style='border:1px solid #AAAAAA'><td>" + symbol + "</td><td>" ;
     ret += "<tr style='border:1px solid #AAAAAA'><td>" + symbol + "</td><td>" ;
     ret += '<a href="#" onclick ="cat_a_lot_update_cats(\'' + cat_a_lot_encode (list[i]) + '\', true)">' + cat_a_lot_htmlize (list[i]) + '</a>' ;
     ret += '<a href="#" onclick ="cat_a_lot_update_cats(\'' + list[i] + '\')">' + list[i] + '</a>' ;
     ret += "</td><td>" ;
     ret += "</td><td>" ;
     if ( list[i] != wgTitle ) { // Can't move to source category
     if ( list[i] != wgTitle ) { // Can't move to source category
       ret += '<a href="#" onclick ="cat_a_lot_move_here(\'' + cat_a_lot_encode (list[i]) + '\')"><b>Move</b></a> ' ;
       ret += '<a href="#" onclick ="cat_a_lot_move_here(\'' + list[i] + '\')"><b>Move</b></a> ' ;
       ret += '<a href="#" onclick ="cat_a_lot_copy_here(\'' + cat_a_lot_encode (list[i]) + '\')"><b>Copy</b></a>' ;
       ret += '<a href="#" onclick ="cat_a_lot_copy_here(\'' + list[i] + '\')"><b>Copy</b></a>' ;
     }
     }
     ret += "</td></tr>" ;
     ret += "</td></tr>" ;
שורה 420: שורה 378:
}
}


function cat_a_lot_update_cats ( newcat, encoded ) {
function cat_a_lot_update_cats ( newcat ) {
   document.body.style.cursor = 'wait' ;
   document.body.style.cursor = 'wait' ;
  if (encoded) newcat = cat_a_lot_decode (newcat);
   cat_a_lot_current_category = newcat ;
   cat_a_lot_current_category = newcat ;
   catlist = document.getElementById("cat_a_lot_category_list") ;
   catlist = document.getElementById("cat_a_lot_category_list") ;
שורה 443: שורה 400:
     cat_a_lot_mark_all ( 1 ) ;
     cat_a_lot_mark_all ( 1 ) ;
     cat_a_lot_data.style.display = "block" ;
     cat_a_lot_data.style.display = "block" ;
     if ( cat_a_lot_searchmode ) cat_a_lot_update_cats ( "Pictures and images" ) ;
     cat_a_lot_update_cats ( wgTitle ) ;
    else cat_a_lot_update_cats ( wgTitle ) ;
   }
   }
}
}
// </source>
 
//</nowiki></pre>

גרסה מ־11:57, 6 בנובמבר 2010

//<pre><nowiki>

//Install by copying the following line to your monobook.js user sub-page:
// document.write('<script type="text/javascript" src="http://commons.wikimedia.org/w/index.php?title=MediaWiki:Cat-a-lot.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');

// Variables
var cat_a_lot_mark_color = "#3399FF" ;
var cat_a_lot_api_url = "/w/api.php" ;
var cat_a_lot_query_url = "/w/query.php" ;
var cat_a_lot_edit_url = "/w/index.php?title=" ;


String.prototype.trim = function () {
  var s = this ;
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

String.prototype.lcFirst = function () {
   return this.substr(0,1).toLowerCase() + this.substr(1,this.length);
}

String.prototype.replaceAll = function ( s , r ) {
   return this.split(s).join(r);
}

if ( wgNamespaceNumber == 14 ) {
//  document.addEventListener("DOMContentLoaded", init_cat_a_lot, false);
  addOnloadHook ( init_cat_a_lot ) ;
}

// Add this to URLs to get around cache issues
function cat_a_lot_dummy_time() {
  var t = new Date () ;
  return "&dummytime=" + t.getTime() ;
}

function init_cat_a_lot () {
  var c1_div = document.getElementById("column-one") ;
  c1_div.innerHTML += '<div id="cat_a_lot" style="z-index:9999;display:block;background-color:white; width:108px height:145px; position:fixed; bottom:0; right:0;">' + 
    '<div class="cat_a_lot_data" id="cat_a_lot_data" style="display:none;border:1px solid black">' +

    '<div style="border-bottom:1px solid black">' +
    '<form name="cat_a_lot_searchcat" method="post" onSubmit="return false;">' + 
    '<input type="text" input="8" name="cat_a_lot_searchcatname" />' +
    '<input type="button" name="cat_a_lot_go" value="Go" onclick="cat_a_lot_search_cat();"/>' +
    '</form>' +
    '</div>' +

    '<div class="cat_a_lot_category_list" id="cat_a_lot_category_list" style="border-bottom:1px solid black;max-height:500px;overflow:auto"> </div>' +
    '<div class="cat_a_lot_mark_counter" id="cat_a_lot_mark_counter"> </div>' +
    '<div class="cat_a_lot_selections" id="cat_a_lot_selections" style="border-top:1px solid black">Select ' +
    '<a href="#" onClick="cat_a_lot_mark_all(' + "'2'" + ');">all</a> / ' +
    '<a href="#" onClick="cat_a_lot_mark_all(' + "'3'" + ');">none</a>' +
    '</div>' +
    '<div>' +
    '<a href="#" onclick ="cat_a_lot_remove()"><b>Remove from this category</b></a>' +
    '</div>' +
    '<form name="cat_a_lot_edit" enctype="multipart/form-data" method="post" target="_blank">' + 
    '<input type="hidden" name="wpTextbox1" />' +
    '<input type="hidden" name="wpSummary" />' +
    '<input type="hidden" name="wpDiff" value="wpDiff" />' +
    '<input type="hidden" name="wpStarttime" />' +
    '<input type="hidden" name="wpEdittime" />' +
    '</form>' +
    '</div>' +
    '<div id="cat_a_lot_head" class="pBody">' +
    '<form><input id="cat_a_lot_mode" type="checkbox" onchange="run_cat_a_lot();"/><label for="cat_a_lot_mode">Cat-a-lot mode</label></form>' +
    '</div></div>' ;
}

function cat_a_lot_get_marked_labels () {
  var ret = new Array () ;
  var divs = document.getElementsByTagName ( "div" ) ;
  for ( i = 0 ; i < divs.length ; i++ ) {
    if ( divs[i].className != "gallerytext" ) continue ;
    if ( divs[i].style.backgroundColor == "" ) continue ;
    var files = divs[i].getElementsByTagName ( "a" ) ;
    ret.push ( files[0].title ) ;
  }
  return ret ;
}

function cat_a_lot_update_selection_counter () {
  var cat_a_lot_mark_counter = document.getElementById("cat_a_lot_mark_counter") ;
  var l = cat_a_lot_get_marked_labels () ;
  cat_a_lot_mark_counter.innerHTML = l.length + " files selected." ;
}

function cat_a_lot_on_click_label ( title ) {
  cat_a_lot_alter_label ( title , "onclick" ) ;
}

function cat_a_lot_invalidate_label ( title , mode ) {
  cat_a_lot_alter_label ( title , mode ) ;
}

function cat_a_lot_alter_label ( title , mode ) {
  var divs = document.getElementsByTagName ( "div" ) ;
  for ( i = 0 ; i < divs.length ; i++ ) {
    if ( divs[i].className != "gallerytext" ) continue ;
    var files = divs[i].getElementsByTagName ( "a" ) ;
    if ( files.length == 0 ) continue ;
    if ( files[0].title != title ) continue ;

    if ( mode == "move" ) {
      divs[i].innerHTML = "<div style='width:100%;color:white;background-color:red;text-align:center'>Moved to new category</div>" ;
      divs[i].style.backgroundColor = "#FF0000" ;
    } else if ( mode == "remove" ) {
      divs[i].innerHTML = "<div style='width:100%;color:white;background-color:red;text-align:center'>Removed from category</div>" ;
      divs[i].style.backgroundColor = "#FF0000" ;
    } else if ( mode == "copy" ) {
      divs[i].innerHTML += "<br/><b>Copied to new category!</b>" ;
    }

    // Clicked on this element
    if ( divs[i].style.backgroundColor != "" ) {
      divs[i].style.backgroundColor = "" ;
    } else {
      divs[i].style.backgroundColor = cat_a_lot_mark_color ;
    }
    cat_a_lot_update_selection_counter () ;
    return ;
  }
}

function cat_a_lot_fix_quotes ( text , quote ) {
  return text.split(quote).join("\\"+quote) ;
}

function cat_a_lot_mark_all ( how ) {
  var divs = document.getElementsByTagName ( "div" ) ;
  for ( i = 0 ; i < divs.length ; i++ ) {
    if ( divs[i].className != "gallerytext" ) continue ;
    var files = divs[i].getElementsByTagName ( "a" ) ;
    if ( files.length == 0 ) continue ;

    divs[i].style.backgroundColor = "" ; // Deselect
    if ( how == 1 ) { // Activate
      eval("divs[i].onclick = function () { cat_a_lot_on_click_label('" + cat_a_lot_fix_quotes ( files[0].title , "'" ) + "'); };") ;
    } else if ( how == 2 ) { // Select
      divs[i].style.backgroundColor = cat_a_lot_mark_color ;
    } else if ( how == 0 ) { // Deactivate
      divs[i].onclick = "" ;
    }
  }
  cat_a_lot_update_selection_counter () ;
}

function cat_a_lot_get_subcategories () {
  var url = cat_a_lot_query_url + "?what=category&format=xml&cpnamespace=14&cptitle=" + cat_a_lot_current_category + cat_a_lot_dummy_time() ;
  var xmlreq = new XMLHttpRequest() ;
  xmlreq.open ( "GET" , url , false ) ;
  xmlreq.send ( null ) ;
  xml = xmlreq.responseXML ;
  var cls = xml.getElementsByTagName('title') ;
  var ret = new Array () ;
  for ( var i = 0 ; i < cls.length ; i++ ) {
    var title = cls[i].firstChild.data ;
    title = title.split(":",2)[1] ;
    ret.push ( title ) ;
  }
  return ret ;
}

function cat_a_lot_get_parent_categories () {
  var url = cat_a_lot_api_url + "?action=query&format=xml&prop=categories&titles=Category:" + cat_a_lot_current_category + cat_a_lot_dummy_time() ;
  var xmlreq = new XMLHttpRequest() ;
  xmlreq.open ( "GET" , url , false ) ;
  xmlreq.send ( null ) ;
  xml = xmlreq.responseXML ;
  var cls = xml.getElementsByTagName('cl') ;
  var ret = new Array () ;
  for ( var i = 0 ; i < cls.length ; i++ ) {
    var title = cls[i].attributes.getNamedItem("title").value ;
    title = title.split(":",2)[1] ;
    ret.push ( title ) ;
  }
  return ret ;
}

function cat_a_lot_replace_category ( text , sourcecat , targetcat , catname , ending ) {
//  alert ( "[[" + catname + ":" + sourcecat + ending + " : " + "[[Category:" + targetcat + ending ) ;
  return text.replaceAll ( "[[" + catname + ":" + sourcecat + ending , "[[Category:" + targetcat + ending ) ;
}


function cat_a_lot_fix_time ( time ) {
  time = time.replace ( /:/g , "" ) ;
  time = time.replace ( / /g , "" ) ;
  time = time.replace ( /-/g , "" ) ;
  time = time.replace ( /Z/g , "" ) ;
  time = time.replace ( /T/g , "" ) ;
  return time ;
}

function cat_a_lot_get_article_timestamp ( title ) {
  var ret ;
  var url = cat_a_lot_query_url + '?format=xml&what=revisions&rvlimit=1&rvcomments&titles=' + title ;

  var xmlreq = new XMLHttpRequest() ;
  xmlreq.open ( "GET" , url , false ) ;
  xmlreq.send ( null ) ;
  var xml = xmlreq.responseXML ;
  var rev = xml.getElementsByTagName('rv') ;
  if ( rev.length == 0 ) return cat_a_lot_get_current_utc_timestamp () ;

  ret = rev.item(0).getAttribute('timestamp') ;
  return cat_a_lot_fix_time ( ret ) ;
}

function cat_a_lot_remove_category ( text , sourcecat , cat , ending ) {
  text = " " + text ;
  var parts = text.split ( "[[" + cat + ":" + sourcecat + ending ) ;
  if ( parts.length == 1 ) return text.trim() ; // Not found
  text = parts.shift () ;
  while ( parts.length > 0 ) {
    var s = parts.shift () ;
    if ( ending == "|" ) s = s.split("]]",2).pop () ;
    text += s ;
  }
  return text.trim() ;
}

function cat_a_lot_edit_move ( filename , targetcat , mode ) {
  var sourcecat = wgTitle ;
  var url = cat_a_lot_edit_url + filename + "&action=raw" + cat_a_lot_dummy_time() ;
  var xmlreq = new XMLHttpRequest() ;
  xmlreq.open ( "GET" , url , false ) ;
  xmlreq.send ( null ) ;
  var otext = xmlreq.responseText.trim() ;

  // Check if that file is already in that category
  if ( mode != "remove" ) {
    if ( otext.indexOf ( "ategory:" + targetcat.ucFirst() ) != -1 ||
         otext.indexOf ( "ategory:" + targetcat.lcFirst() ) != -1 ) {
      alert ( filename + " is already in category " + targetcat + " - skipping." ) ;
      return ;
    }
  }

  // Fix text
  var text = otext ;
  if ( mode == 'copy' ) {
    text += "\n[[Category:" + targetcat + "]]\n" ;
  } else if ( mode == 'move' ) {
    text = cat_a_lot_replace_category ( text , sourcecat.ucFirst() , targetcat , "category" , "]]" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.lcFirst() , targetcat , "category" , "]]" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.ucFirst() , targetcat , "Category" , "]]" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.lcFirst() , targetcat , "Category" , "]]" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.ucFirst() , targetcat , "category" , "|" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.lcFirst() , targetcat , "category" , "|" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.ucFirst() , targetcat , "Category" , "|" ) ;
    text = cat_a_lot_replace_category ( text , sourcecat.lcFirst() , targetcat , "Category" , "|" ) ;
  } else if ( mode == 'remove' ) {
    text = cat_a_lot_remove_category ( text , sourcecat.ucFirst() , "category" , "]]" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.lcFirst() , "category" , "]]" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.ucFirst() , "category" , "|" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.lcFirst() , "category" , "|" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.ucFirst() , "Category" , "]]" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.lcFirst() , "Category" , "]]" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.ucFirst() , "Category" , "|" ) ;
    text = cat_a_lot_remove_category ( text , sourcecat.lcFirst() , "Category" , "|" ) ;
  }

  if ( text == otext ) {
    alert ( "Could not find an occurrence of category " + sourcecat + " in description of file " + filename + " - skipping." ) ;
    return ;
  }

  var time = new Date () ;
  var last_version_timestamp = cat_a_lot_get_article_timestamp ( filename ) ;
  var starttime = cat_a_lot_get_current_utc_timestamp () ;

  var comment ;
  if ( mode == "move" ) comment = "Moving from [[Category:" + sourcecat + "]] to [[Category:" + targetcat + "]]" ;
  if ( mode == "copy" ) comment = "Copying from [[Category:" + sourcecat + "]] to [[Category:" + targetcat + "]]" ;
  if ( mode == "remove" ) comment = "Removing from [[Category:" + sourcecat + "]]" ;
  url = cat_a_lot_edit_url + filename + "&action=edit" ;
  document.cat_a_lot_edit.action = url ;
  document.cat_a_lot_edit.wpSummary.value = comment ;
  document.cat_a_lot_edit.wpTextbox1.value = text ;
  document.cat_a_lot_edit.wpStarttime.value = starttime ;
  document.cat_a_lot_edit.wpEdittime.value = last_version_timestamp ;
  document.cat_a_lot_edit.submit () ;

  cat_a_lot_invalidate_label ( filename , mode ) ;
}

// Generates current timestamp
function cat_a_lot_get_current_utc_timestamp () {
  var today=new Date() ; 

  var y  = "00" + today.getUTCFullYear() ; 
  var mo = today.getUTCMonth() + 1 ; 
  var d  = "00" + today.getUTCDate() ; 
  mo = "00" + mo ;

  var h = "00" + today.getUTCHours() ; 
  var m = "00" + today.getUTCMinutes() ;
  var s = "00" + today.getUTCSeconds() ;

  var ret = "" ;
  ret +=  y.substr (  y.length - 4 ) ;
  ret += mo.substr ( mo.length - 2 ) ;
  ret +=  d.substr (  d.length - 2 ) ;
  ret +=  h.substr (  h.length - 2 ) ;
  ret +=  m.substr (  m.length - 2 ) ;
  ret +=  s.substr (  s.length - 2 ) ;
  return ret ;
}



function cat_a_lot_move_here ( targetcat ) {
  cat_a_lot_do_something ( targetcat , 'move' ) ;
}

function cat_a_lot_copy_here ( targetcat ) {
  cat_a_lot_do_something ( targetcat , 'copy' ) ;
}

function cat_a_lot_remove () {
  cat_a_lot_do_something ( '' , 'remove' ) ;
}

function cat_a_lot_do_something ( targetcat , mode ) {
  var files = cat_a_lot_get_marked_labels () ;
  if ( files.length == 0 ) {
    alert ( "No files selected." ) ;
    return ;
  }
  for ( var i = 0 ; i < files.length ; i++ ) {
    cat_a_lot_edit_move ( files[i] , targetcat , mode ) ;
  }
}

function cat_a_lot_get_cat_links ( symbol , list ) {
  var ret = "" ;
  list.sort () ;
  for ( var i = 0 ; i < list.length ; i++ ) {
    ret += "<tr style='border:1px solid #AAAAAA'><td>" + symbol + "</td><td>" ;
    ret += '<a href="#" onclick ="cat_a_lot_update_cats(\'' + list[i] + '\')">' + list[i] + '</a>' ;
    ret += "</td><td>" ;
    if ( list[i] != wgTitle ) { // Can't move to source category
      ret += '<a href="#" onclick ="cat_a_lot_move_here(\'' + list[i] + '\')"><b>Move</b></a> ' ;
      ret += '<a href="#" onclick ="cat_a_lot_copy_here(\'' + list[i] + '\')"><b>Copy</b></a>' ;
    }
    ret += "</td></tr>" ;
  }
  return ret ;
}

function cat_a_lot_show_category_list () {
  var parcats = cat_a_lot_get_parent_categories () ;
  var subcats = cat_a_lot_get_subcategories () ;
  var thiscat = new Array () ;
  thiscat.push ( cat_a_lot_current_category ) ;
  
  var out = "<table border='0'>" ;
  out += cat_a_lot_get_cat_links ( "&uarr;" , parcats ) ;
  out += cat_a_lot_get_cat_links ( "&rarr;" , thiscat ) ;
  out += cat_a_lot_get_cat_links ( "&darr;" , subcats ) ;
  out += "</table>" ;

  catlist = document.getElementById("cat_a_lot_category_list") ;
  catlist.innerHTML = out ;
  document.body.style.cursor = 'auto' ;
}

function cat_a_lot_update_cats ( newcat ) {
  document.body.style.cursor = 'wait' ;
  cat_a_lot_current_category = newcat ;
  catlist = document.getElementById("cat_a_lot_category_list") ;
  catlist.innerHTML = '<div style="background-color:red;color:white">Loading...</div>' ;
  window.setTimeout("cat_a_lot_show_category_list()", 0);
}

function cat_a_lot_search_cat() {
  var newcat = document.cat_a_lot_searchcat.cat_a_lot_searchcatname.value ;
  cat_a_lot_update_cats ( newcat ) ;
}

function run_cat_a_lot () {
  cat_a_lot_data = document.getElementById("cat_a_lot_data") ;
  cat_a_lot_mode = document.getElementById("cat_a_lot_mode").checked ;
  if ( cat_a_lot_mode == false ) {
    cat_a_lot_data.style.display = "none" ;
    cat_a_lot_mark_all ( 0 ) ;
  } else {
    cat_a_lot_mark_all ( 1 ) ;
    cat_a_lot_data.style.display = "block" ;
    cat_a_lot_update_cats ( wgTitle ) ;
  }
}

//</nowiki></pre>