MediaWiki:Gadget-HotCat.js: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
Jagawana (bicara | kontrib)
k summary translate
perbarui skrip dari http://en.wikipedia.org/w/index.php?title=User:TheDJ/Gadget-HotCat.js&oldid=321175546
Baris 5:
*
* This version was forked from http://commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-HotCat.js&oldid=10204404
* In sync with version: http://commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-HotCat.js&oldid=19600669
* Major changes:
* - blacklist code is disabled.
* - all code for the uploadForm has been removed
* - autocommit is disabled
Baris 15:
* - tries to detect other categories and if possible, add to the end of them.
* - fixes a bug in the suggestion list with titles containing : character
* - Uses opensearch API to look for categories. Allows for case insensitive search.
* [[User:TheDJ]] 2008-03-12
* - Postfix blacklisting in addition to prefix blacklisting.
* [[User:TheDJ]] 2009-04-18
<source lang="javascript"><nowiki> */
var hotcat_running = 0 ;
Baris 34 ⟶ 36:
 
var hotcat_modify_blacklist = new Array (
" stubs"
) ;
 
var hotcat_cnames=["[Cc]ategory","[Kk]ategori"]; // namespaces and alias of category
// in chinese: categoryNames=["[Cc]ategory","分类","分類"];
 
addOnloadHook ( hotcat ) ;
Baris 58 ⟶ 63:
d3 = document.createElement ( "div" );
d3.id = "mw-normal-catlinks";
d3.innerHTML = '<a href="/wiki/Special:Categories" title="Special:Categories">KategoriCategories</a>: ';
visible_catlinks = d3;
 
Baris 96 ⟶ 101:
function hotcat_is_on_blacklist ( cat_title ) {
if ( !cat_title ) return 0 ;
cat_title = cat_title.ucFirst.replace( /Kategori:/gi, "" );
for ( var i = 0 ; i < hotcat_modify_blacklist.length ; i++ ) {
/* prefix */
if ( cat_title.substr ( 0 , hotcat_modify_blacklist[i].length ) == hotcat_modify_blacklist[i] ) return 1 ;
if ( cat_title.substr ( 0 , hotcat_modify_blacklist[i].length )
== hotcat_modify_blacklist[i] ) return 1 ;
/* postfix */
var postfix_len = cat_title.length - hotcat_modify_blacklist[i].length;
if ( postfix_len >= 0 && cat_title.substr ( postfix_len, hotcat_modify_blacklist[i].length )
== hotcat_modify_blacklist[i] ) return 1 ;
}
return 0 ;
Baris 106 ⟶ 116:
//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... weit anotheralso wayfails toif get thatthe category name
// contains "&" (because that is represented by &amp; in the XHTML both in the title and in
// name. Lupo, 2008-02-27
// the link's content (innerHTML). Extract the category name from the href instead:
var cat_title = span.firstChild.innerHTML; // without "Kategori:"
var cat_title = null;
var classes = span.firstChild.getAttribute ('class');
if (classes && classes.search (/\bnew\b/) >= 0) { // href="/w/index.php?title=...&action=edit"
cat_title = hotcatGetParamValue ('title', span.firstChild.href);
} else { // href="/wiki/..."
var re = new RegExp (wgArticlePath.replace (/\$1/, '(.*)'));
var matches = re.exec (span.firstChild.href);
if (matches && matches.length > 1)
cat_title = decodeURIComponent (matches[1]);
else
return;
}
// Strip namespace, replace _ by blank
cat_title = cat_title.substring (cat_title.indexOf (':') + 1).replace (/_/g, ' ');
 
var sep1 = document.createTextNode ( " " ) ;
var a1 = document.createTextNode ( "(-)" ) ;
Baris 122 ⟶ 146:
span.appendChild ( remove_link ) ;
 
if ( hotcat_is_on_blacklist ( cat_title ) ) return ;
// Disabled blacklist check TheDJ, 2008-02-28
// if ( hotcat_is_on_blacklist ( cat_title ) ) return ;
var mod_id = "hotcat_modify_" + i ;
var sep2 = document.createTextNode ( " " ) ;
Baris 134 ⟶ 157:
span.appendChild ( sep2 ) ;
span.appendChild ( modify_link ) ;
span.hotcat_name = cat_title; //Store the extracted category name in our own new property of the span DOM node
}
 
Baris 194 ⟶ 218:
if (!node) return false;
// Get the category name from the original link to the category
var cat_title = node.parentNode.firstChild.innerHTMLhotcat_name;
var editlk = wgServer + wgScript + '?title=' + encodeURIComponent (wgPageName) + '&action=edit';
var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
if ((hotcat_evtkeys (evt) & 1) || (hotcat_evtkeys (evt) & 4 )) // CTRL or ALT pressed?
editlk = editlk + '&hotcat_nocommit=1';
Baris 205 ⟶ 229:
 
function hotcatGetParamValue(paramName, h) {
if (typeof( h) == 'undefined' ) { h = document.location.href; }
var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
var m=cmdRe.exec(h);
if (m) {
try {
return decodeURIComponent(m[1]);
} catch (someError) {}
}
return null;
}
 
Baris 221 ⟶ 245:
var cat_name = category.replace(/([\\\^\$\.\?\*\+\(\)])/g, "\\$1");
var initial = cat_name.substr (0, 1);
var cat_regex = new RegExp ("\\[\\[\\s*[Kk]ategori(?:" + hotcat_cnames.join("|") + ")\\s*:\\s*"
+ (initial == "\\"
? initial
Baris 239 ⟶ 263:
function hotcat_find_ins ( wikitext )
{
var re = /new RegExp("\\[\\[\\s*(?:Kategori" + hotcat_cnames.join("|") + ")\\s*:\[^\\]\]+\\]\\]/", "ig" );
var index = -1;
while( re.exec(wikitext) != null ) index = re.lastIndex;
Baris 262 ⟶ 286:
var cat_rm = hotcatGetParamValue ('hotcat_removecat');
var cat_add = hotcatGetParamValue ('hotcat_newcat');
var comment = hotcatGetParamValue ('hotcat_comment') || "";
var cat_key = null;
var cat_key = hotcatGetParamValue ('hotcat_sortkey');
if (cat_key != null) cat_key = '|' + cat_key;
 
if (cat_rm != null && cat_rm.length > 0) {
var matches = hotcat_find_category (t, cat_rm);
if (!matches || matches.length == 0) {
alert ('KategoriCategory "' + cat_rm + '" nottidak foundditemukan; maybemungkin itdari is in a templatetemplat?');
prevent_autocommit = 1;
} else if (matches.length > 1) {
alert ('KategoriCategory "' + cat_rm
+ "\" foundditemukan severalbeberapa timeskali; don'ttidak knowyakin whichyang occurrencemana toyang removeharus dibuang.");
prevent_autocommit = 1;
} else {
Baris 292 ⟶ 319:
t1 = t1 + ' ';
t = t1 + t2;
summary.push ( "RemovedMembuang categorykategori \[\[:Category:" + cat_rm + "|" + cat_rm + "\"]\]" ) ;
ret = 1;
}
Baris 299 ⟶ 326:
var matches = hotcat_find_category (t, cat_add);
if (matches && matches.length > 0) {
alert ('Category "' + cat_add + '" alreadysudah existsada; nottidak addedditambahkan.');
prevent_autocommit = 1;
} else {
var insertionpoint = hotcat_find_ins( t );
var newcatstring = '\n\[\[KategoriCategory:' + cat_add + (cat_key != null ? cat_key : "") + '\]\]';
if( insertionpoint > -1 ) {
t = t.substring(0, insertionpoint ) + newcatstring + t.substring( insertionpoint );
Baris 309 ⟶ 336:
t = t + newcatstring;
}
summary.push ( "MenambahMenambahkan kategori \[\[:Category:" + cat_add + "|" + cat_add + "\"]\]" )+ comment);
var t2 = t.replace(/\{\{[Uu]ncategorized[^}]*\}\}/g, ""); // Remove "uncategorized" template
if (t2.length != t.length) {
Baris 321 ⟶ 348:
document.editform.wpTextbox1.value = t ;
document.editform.wpSummary.value = summary.join( "; " )
+ " (menggunakanusing [[WP:HOTCAT|HotCat]])" ;
document.editform.wpMinoredit.checked = true ;
if (!prevent_autocommit) {
// Hide the entire edit section so as not to tempt the user into editing...
var bodyContentId = document.getElementById("bodyContent") //monobook skin
|| document.getElementById("mw_contentholder"); // modern skin
|| document.getElementById ("article"); // classic skin
bodyContentId.style.display = "none";
document.editform.submit();
Baris 351 ⟶ 380:
var link = document.getElementById ( link_id ) ;
var span = link.parentNode ;
var catname = span.firstChild.innerHTMLhotcat_name; // was .firstChild.data. Just for consistency
 
while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
span.firstChild.style.display = "none" ;
Baris 437 ⟶ 466:
function hotcat_ok (nocommit) {
var text = document.getElementById ( "hotcat_text" ) ;
var v = text.value || "";
v = v.replace(/_/g, ' ').replace(/^\s\s*/, '').replace(/\s\s*$/, ''); // Trim leading and trailing blanks
 
// Empty category ?
if ( !v == "" ) {
hotcat_cancel() ;
return ;
} else if ( hotcat_is_on_blacklist(v) ) {
alert( 'This type of category needs to be added using a template' );
return;
}
 
// Get the links and the categories of the chosen category page
var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
var url = wgServer + wgScriptPath + '/api.php?action=query&titles='
+ encodeURIComponent ('Category:' + v)
+ '&prop=info|links|categories&plnamespace=14&format=json&callback=hotcat_json_resolve';
var request = sajax_init_object() ;
if (request == null) {
//Oops! We don't have XMLHttp...
hotcat_nosuggestions = true;
hotcat_closeform (nocommit);
hotcat_running = 0;
return;
}
request.open ('GET', url, true);
request.onreadystatechange =
function () {
if (request.readyState != 4) return;
if (request.status != 200) {
hotcat_closeform (nocommit);
} else {
var do_submit = eval (request.responseText);
var txt = document.getElementById ('hotcat_text');
if (do_submit) {
hotcat_closeform (
nocommit
,(txt && txt.value != v) ? " (redirect \[\[:Category:" + v + "|" + v + "\]\] resolved)" : null
);
}
}
};
request.setRequestHeader ('Pragma', 'cache=yes');
request.setRequestHeader ('Cache-Control', 'no-transform');
request.send (null);
}
 
function hotcat_json_resolve (params)
{
function resolve (page)
{
var cats = page.categories;
var is_dab = false;
var is_redir = typeof (page.redirect) == 'string'; // Hard redirect?
if (!is_redir && cats) {
for (var c = 0; c < cats.length; c++) {
var cat = cats[c]["title"];
if (cat) cat = cat.substring (cat.indexOf (':') + 1); // Strip namespace prefix
if (cat == 'Disambiguation') {
is_dab = true; break;
} else if ( /.*soft.redirected.categories.*/.test( cat ) ) {
is_redir = true; break;
}
}
}
if (!is_redir && !is_dab) return true;
var lks = page.links;
var titles = new Array ();
for (i = 0; i < lks.length; i++) {
if ( lks[i]["ns"] == 14 // Category namespace
&& lks[i]["title"] && lks[i]["title"].length > 0) { // Name not empty
// Internal link to existing thingy. Extract the page name.
var match = lks[i]["title"];
// Remove the category prefix
match = match.substring (match.indexOf (':') + 1);
titles.push (match);
if (is_redir) break;
}
}
if (titles.length > 1) {
// Disambiguation page
hotcat_show_suggestions (titles);
return false;
} else if (titles.length == 1) {
var text = document.getElementById ("hotcat_text");
if (text) text.value = titles[0];
}
return true;
} // end local function resolve
 
// We should have at most one page here
for (var page in params.query.pages) return resolve (params.query.pages[page]);
return true; // In case we have none.
}
 
function hotcat_closeform (nocommit, comment)
{
var text = document.getElementById ( "hotcat_text" ) ;
var v = text.value || "";
v = v.replace(/_/g, ' ').replace(/^\s\s*/, '').replace(/\s\s*$/, ''); // Trim leading and trailing blanks
if (!v // Empty
|| wgNamespaceNumber == 14 && v == wgTitle // Self-reference
|| text.parentNode.parentNode.id != 'hotcat_add' // Modifying, but
&& text.parentNode.parentNode.hotcat_name == v) // name unchanged
{
hotcat_cancel ();
return;
}
var editlk = wgServer + wgScript + '?title=' + encodeURIComponent (wgPageName) + '&action=edit';
var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ;
 
Baris 451 ⟶ 580:
var span = text.parentNode.parentNode ; // span.form.text
if ( span.id != "hotcat_add" ) { // Not plain "addition"
url += '&hotcat_removecat=' + encodeURIComponent (span.firstChild.innerHTMLhotcat_name);
}
if (nocommit) url = url + '&hotcat_nocommit=1';
if (comment) url = url + '&hotcat_comment=' + encodeURIComponent (comment);
// Make the list disappear:
var list = document.getElementById ( "hotcat_list" ) ;
if (list) list.style.display = 'none';
document.location = url ;
}
Baris 465 ⟶ 599:
while ( span.firstChild ) span.removeChild ( span.firstChild ) ;
var na = document.createElement ( "a" ) ;
na.href = wgArticlePath.split("$1").join("KategoriCategory:" + encodeURI (text)) ;
na.appendChild ( document.createTextNode ( text ) ) ;
na.setAttribute ( "title" , "KategoriCategory:" + text ) ;
span.appendChild ( na ) ;
var catline = getElementsByClassName ( document , "p" , "catlinks" ) [0] ;
Baris 514 ⟶ 648:
 
if ( v != "" ) {
var url = wgServer + wgScriptPathwgMWSuggestTemplate.replace("{namespaces}","14")
.replace("{dbname}",wgDBname)
+ "/api.php?format=xml&action=query&list=allpages&apfilterredir=nonredirects&apnamespace=14&apfrom="
+ .replace("{searchTerms}",encodeURIComponent( v ) );
var request = sajax_init_object() ;
if (request == null) {
Baris 527 ⟶ 661:
hotcat_running = 0;
return;
}
request.open('GET', url, true);
request.onreadystatechange =
function () {
if (request.readyState == 4) {
try var xml = request.responseXML ;{
if eval( xml"var queryResult=="+ nullrequest.responseText ) return ;
} catch var pages = xml.getElementsByTagName( "p"someError ) ;{
varif( titlesconsole =&& newconsole.log Array () ;
for console.log( var"Oh idear, =our 0JSON ;query iwent <down pages.lengththe ;drain?\nError: " i++someError ) {;
return;
// Strip "Category:" but don't break on titles like "Category:Space: 2001 a space odyssey"
var s = pages[i].getAttribute("title").replace( /Kategori:/gi, "" );
if ( s.substr ( 0 , hotcat_last_v.length ) != hotcat_last_v ) break ;
titles.push ( s ) ;
}
hotcat_show_suggestions ( titles ) ;
}
var pages = queryResult[1]; // results are *with* namespace here
var titles = new Array();
for ( var i = 0 ; pages && i < pages.length ; i++ ) {
// Remove the namespace. No hardcoding of 'Category:', please, other Wikis may have
// local names ("Kategorie:" on de-WP, for instance). Also don't break on category
// names containing a colon
var s = pages[i].substring (pages[i].indexOf (':') + 1);
if ( s.substr ( 0 , hotcat_last_v.length ).toLowerCase() != hotcat_last_v.toLowerCase() ) break ;
titles.push ( s ) ;
}
hotcat_show_suggestions ( titles ) ;
}
};
request.setRequestHeader ('Pragma', 'cache=yes');
Baris 585 ⟶ 727:
var nl = parseInt (text.offsetLeft) - 1 ;
var nt = parseInt (text.offsetTop) - listh ;
if (skin == 'nostalgia' || skin == 'cologneblue' || skin == 'standard') {
// These three skins have the category line at the top of the page. Make the suggestions
// appear *below* out input field.
nt = parseInt (text.offsetTop) + parseInt (text.offsetHeight) + 3;
}
list.style.top = nt + "px" ;
list.style.width = text.offsetWidth + "px" ;
Baris 600 ⟶ 747:
icon.src = hotcat_exists_yes ;
 
var nof_titles = titles.lenghtlength;
var first_title = titles.shift () ;
var v = text.value.ucFirst() ;
 
text.focus();
if ( first_title == v ) {
if( nof_titles == 1 ) {
// Only one result, and it's the same as whatever is in the input box: makes no sense
// to show the list. But make sure the text field has the focus !
text.focus();
list.style.display = "none";
}