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

decodeURIComponent instead of decodeURI!!
MW 1.13α fix for redlinks; use encodeURI to create links in just_add; check_action rewritten as outlined on talk; disabled HotCat on preview pages. Passing now categoy name without "Category:"
שורה 1: שורה 1:
//<pre><nowiki>
//<source lang="javascript">
addOnloadHook ( hotcat ) ;
addOnloadHook ( hotcat ) ;


שורה 26: שורה 26:
   for ( var i = 0 ; i < cats.length ; i++ ) {
   for ( var i = 0 ; i < cats.length ; i++ ) {
     var a = cats[i].getElementsByTagName("a")[0] ;
     var a = cats[i].getElementsByTagName("a")[0] ;
     if ( a.getAttribute ( "title" ) != text ) continue ;
     if ( a.innerHTML != text ) continue ;
     cats[i].parentNode.removeChild ( cats[i].nextSibling ) ;
     cats[i].parentNode.removeChild ( cats[i].nextSibling ) ;
     cats[i].parentNode.removeChild ( cats[i] ) ;
     cats[i].parentNode.removeChild ( cats[i] ) ;
שורה 135: שורה 135:
   if ( catline == null || typeof catline == 'undefined' ) return ;
   if ( catline == null || typeof catline == 'undefined' ) return ;


   // do not add interface to protected pages, if user has no edit permission
   // Do not add interface to protected pages, if user has no edit permission
   if( document.getElementById('ca-viewsource' ) != null ) return;  
  // Also disable it on preview pages: on a prview, we *are* already editing,
  // and HotCat must not open the page for editing a second time. Lupo, 2008-02-27
   if(   document.getElementById('ca-viewsource' ) != null
      || wgAction == 'submit') return;  


   hotcat_modify_existing ( catline ) ;
   hotcat_modify_existing ( catline ) ;
שורה 171: שורה 174:


function hotcat_modify_span ( span , i ) {
function hotcat_modify_span ( span , i ) {
   var cat_title = span.firstChild.getAttribute ( "title" ) ;
   //var cat_title = span.firstChild.getAttribute ( "title" ) ;
  // This fails with MW 1.13alpha if the category is a redlink, because MW 1.13alpha appends
  // [[MediaWiki:Red-link-title]] to the category name... we another way to get that category
  // name. Lupo, 2008-02-27
  var cat_title = span.firstChild.innerHTML; // without "Category:"
 
   var sep1 = document.createTextNode ( " " ) ;
   var sep1 = document.createTextNode ( " " ) ;
   var a1 = document.createTextNode ( "(-)" ) ;
   var a1 = document.createTextNode ( "(-)" ) ;
שורה 220: שורה 228:
}
}


// New. Code by Lupo & Superm401, added by Lupo, 2008-02-2007
function hotcat_find_category (wikitext, category)
{
  var cat_name  = category.replace(/([\\\^\$\.\?\*\+\(\)])/g, "\\$1");
  var initial  = cat_name.substr (0, 1);
  var cat_regex = new RegExp ("\\[\\[\\s*[Cc]ategory\\s*:\\s*"
                              + (initial == "\\"
                                ? initial
                                : "[" + initial.toUpperCase() + initial.toLowerCase() + "]")
                              + cat_name.substring (1).replace (/[ _]/g, "[ _]")
                              + "\\s*(\\|.*?)?\\]\\]", "g"
                            );
  var result = new Array ();
  var curr_match  = null;
  while ((curr_match = cat_regex.exec (wikitext)) != null) {
    result [result.length] = {match : curr_match};
  }
  return result; // An array containing all matches, with positions, in result[i].match
}
// Rewritten (nearly) from scratch. Lupo, 2008-02-27
function hotcat_check_action () {
function hotcat_check_action () {
   var ret = 0 ;
   var ret = 0;
  if (wgAction != 'edit') return ret;
   if ( wgAction != "edit" ) return ret ; // Not an edit page, so no business...
   if ( wgAction != "edit" ) return ret ; // Not an edit page, so no business...
   var summary = new Array () ;
   var summary = new Array () ;
   var t = document.editform.wpTextbox1.value ;
   var t = document.editform.wpTextbox1.value ;
   var prevent_autocommit = 0 ;
   var errors = false;
  if ( typeof hotcat_no_autocommit != "undefined" && hotcat_no_autocommit != 0 ) prevent_autocommit = 1 ;
//  if ( hotcat_no_autocommit ) prevent_autocommit = 1 ;


   // Remove existing category?
   var cat_rm  = hotcatGetParamValue ('hotcat_removecat');
   var hrc = hotcatGetParamValue('hotcat_removecat') ;
   var cat_add = hotcatGetParamValue ('hotcat_newcat');
   if ( typeof hrc != "undefined" && hrc != null && hrc != "" ) {
  var cat_key = null;
     var tl = t.toLowerCase() ;
   if (cat_rm != null && cat_rm.length > 0) {
    var hrcl = "[[" + hrc.toLowerCase() ;
     var matches = hotcat_find_category (t, cat_rm);
    var tn = tl.split ( hrcl ) ;
     if (!matches || matches.length == 0) {
     if ( tn.length == 2 ) { // Found one occurrence of the category - good!
       alert ('Category "' + cat_rm + '" not found; maybe it is in a template?');
       var l1 = tn.shift().length ;
       errors = true;
       var t1 = t.substr ( 0 , l1 ) ;
    } else if (matches.length > 1) {
      var t2 = t.substr ( l1 + hrcl.length ) ;
       alert ('Category "' + cat_rm
       t2 = t2.split("]]") ;
            + "\" found several times; don't know which occurrence to remove.");
      t2.shift() ;
       errors = true;
      t2 = t2.join ( "]]" ) ;
      t = t1.replace(/\s+$/,"") + "\n" + t2.replace(/^\s+/,"") ; // Trying to remove whitespace
      summary.push ( "Removed \"" + hrc + "\"" ) ;
       ret = 1 ;
     } else {
     } else {
       alert ( "Did not find a unique occurrence of \"" + hrc + "\" - maybe it is in a template?" ) ;
       if (cat_add != null && cat_add.length > 0 && matches[0].match.length > 1)
       prevent_autocommit = 1 ;
        cat_key = matches[0].match[1]; // Remember the category key, if any.
      var t1 = t.substring (0, matches[0].match.index);
      var t2 = t.substring (matches[0].match.index + matches[0].match[0].length);
      // Remove whitespace (properly): strip whitespace, but only up to the next line feed.
      // If we then have two linefeeds in a row, remove one. Otherwise, if we have two non-
      // whitespace characters, insert a blank.
      var i = t1.length - 1;
      while (i >= 0 && t1.charAt (i) != '\n' && t1.substr (i, 1).search (/\s/) >= 0) i--;
      var j = 0;
      while (j < t2.length && t2.charAt (j) != '\n' && t1.substr (j, 1).search (/\s/) >= 0) j++;
      if (i >= 0 && t1.charAt (i) == '\n' && j < t2.length && t2.charAt (j) == '\n')
        i--;
      if (i >= 0) t1 = t1.substring (0, i+1); else t1 = "";
      if (j < t2.length) t2 = t2.substring (j); else t2 = "";
      if (t1.length > 0 && t1.substring (t1.length - 1).search (/\S/) >= 0
          && t2.length > 0 && t2.substr (0, 1).search (/\S/) >= 0)
        t1 = t1 + ' ';
      t = t1 + t2;
      summary.push ( "Removed category \"" + cat_rm + "\"" ) ;
       ret = 1;
     }
     }
   }
   }
 
   if (cat_add != null && cat_add.length > 0) {
   // Add new category?
    var matches = hotcat_find_category (t, cat_add);
  var hnc = hotcatGetParamValue('hotcat_newcat') ;
    if (matches && matches.length > 0) {
  if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) {
      alert ('Category "' + cat_add + '" already exists; not added.');
    var txt = "[[Category:" + hnc + "]]" ;
      error = true;
     t = t + '\n' + txt ;
     } else {
    summary.push ( "Quick-adding category \"" + hnc + "\"" ) ;
      if (t.charAt (t.length - 1) != '\n') t = t + '\n';
    var t2 = t.split("{{uncat}}").join("") ;
      t = t + '\[\[Category:' + cat_add + (cat_key != null ? cat_key : "") + '\]\]\n';
    t2 = t2.split("{{Uncat}}").join("") ;
      summary.push ( "Quick-adding category \"" + cat_add + "\"" ) ;
    if ( t2 != t ) {
      var t2 = t.replace(/\{\{[Uu]ncat\}\}/g, ""); // Remove "uncat" templates
      t = t2 ;
      if (t2.length != t.length) {
      summary.push ( "removed {{uncat}}" ) ;
        t = t2;
        summary.push ( "removed {{uncat}}" ) ;
      }
      ret = 1;
     }
     }
    ret = 1 ;
   }
   }
 
   if (ret) {
   if ( ret ) {
     document.editform.wpTextbox1.value = t ;
     document.editform.wpTextbox1.value = t ;
 
     document.editform.wpSummary.value = summary.join( "; " )
     document.editform.wpSummary.value = summary.join( "; " ) + " (using [[MediaWiki:Gadget-HotCat.js|HotCat.js]])" ;
                                      + " (using [[MediaWiki:Gadget-HotCat.js|HotCat.js]])" ;
     document.editform.wpMinoredit.checked = true ;
     document.editform.wpMinoredit.checked = true ;
     if ( !prevent_autocommit ) {
     if (!errors) {
       document.getElementById("bodyContent").style.display = "none" ; // Hiding the entire edit section so as not to tempt the user into editing...
       // Hide the entire edit section so as not to tempt the user into editing...
       document.editform.wpSave.click();
      document.getElementById("bodyContent").style.display = "none" ;
       document.editform.submit ();
     }
     }
   }
   }
 
   return ret;
  // This is the end, my friend, the end...
   return ret ;
}
}


שורה 298: שורה 341:
   var link = document.getElementById ( link_id ) ;
   var link = document.getElementById ( link_id ) ;
   var span = link.parentNode ;
   var span = link.parentNode ;
   var catname = span.firstChild.firstChild.data ;
   var catname = span.firstChild.innerHTML; // was .firstChild.data. Just for consistency
    
    
   while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
   while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
שורה 324: שורה 367:
    
    
   if (!hotcat_nosuggestions) {
   if (!hotcat_nosuggestions) {
  // Only do this if we may actually use XMLHttp...
    // Only do this if we may actually use XMLHttp...
     list = document.createElement ( "select" ) ;
     list = document.createElement ( "select" ) ;
     list.id = "hotcat_list" ;
     list.id = "hotcat_list" ;
שורה 392: שורה 435:
   }
   }


  // Special:Upload ?
   if (hotcat_upload) {
   if ( hotcat_upload ) {
     hotcat_just_add (v) ; // Close the form
     hotcat_just_add ( text.value ) ;
     return ;
     return ;
   }
   }
   var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
   var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
   var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ;
   var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ;
שורה 403: שורה 444:
   // Editing existing?
   // Editing existing?
   var span = text.parentNode.parentNode ; // span.form.text
   var span = text.parentNode.parentNode ; // span.form.text
   if ( span.id != "hotcat_add" ) { // Not plain "addition"
   if ( span.id != "hotcat_add" ) { // Not plain "addition"  
    var cat_title = span.firstChild.getAttribute ( "title" ) ;
     url += '&hotcat_removecat=' + span.firstChild.innerHTML;
     url += '&hotcat_removecat=' + cat_title ;
   }
   }
   document.location = url ;
   document.location = url ;
}
}
שורה 419: שורה 458:
   while ( span.firstChild ) span.removeChild ( span.firstChild ) ;
   while ( span.firstChild ) span.removeChild ( span.firstChild ) ;
   var na = document.createElement ( "a" ) ;
   var na = document.createElement ( "a" ) ;
   na.href = wgArticlePath.split("$1").join("Category:" + encodeURIComponent( text )) ;
   na.href = wgArticlePath.split("$1").join("Category:" + encodeURI (text)) ;
   na.appendChild ( document.createTextNode ( text ) ) ;
   na.appendChild ( document.createTextNode ( text ) ) ;
   na.setAttribute ( "title" , text ) ;
   na.setAttribute ( "title" , "Category:" + text ) ;
   span.appendChild ( na ) ;
   span.appendChild ( na ) ;
   var catline = getElementsByClassName ( document , "p" , "catlinks" ) [0] ;
   var catline = getElementsByClassName ( document , "p" , "catlinks" ) [0] ;
שורה 527: שורה 566:
   // Lupo, 2008-01-20
   // Lupo, 2008-01-20
   list.size = (titles.length > 5 ? 5 : titles.length) ;
   list.size = (titles.length > 5 ? 5 : titles.length) ;
  // Avoid list height 1: double-click doesn't work in FF. Lupo, 2008-02-27
  if (list.size == 1) list.size = 2;
   list.style.align = "left" ;
   list.style.align = "left" ;
   list.style.zIndex = 5 ;
   list.style.zIndex = 5 ;
שורה 534: שורה 575:
   // Lupo, 2008-01-20
   // Lupo, 2008-01-20
   var listh = list.size * 20;
   var listh = list.size * 20;
   var nl = parseInt (text.parentNode.offsetLeft) - 1 ;
   var nl = parseInt (text.offsetLeft) - 1 ;
   var nt = parseInt (text.offsetTop) - listh ;
   var nt = parseInt (text.offsetTop) - listh ;
   list.style.top = nt + "px" ;
   list.style.top = nt + "px" ;
שורה 625: שורה 666:
     if (cats[i].length > 0) {
     if (cats[i].length > 0) {
       var lk = document.createElement ('a');
       var lk = document.createElement ('a');
       lk.href = wgArticlePath.split ('$1').join ('Category:' + encodeURIComponent (cats[i]));
       lk.href = wgArticlePath.split ('$1').join ('Category:' + encodeURI (cats[i]));
       lk.appendChild (document.createTextNode (cats[i]));
       lk.appendChild (document.createTextNode (cats[i]));
       lk.setAttribute ('title', cats[i]);
       lk.setAttribute ('title', cats[i]);
שורה 638: שורה 679:
}
}


 
//</source>
//</pre></nowiki>