مدیاویکی:Gadget-HotCat.js: تفاوت میان نسخه‌ها

بدون خلاصۀ ویرایش
بدون خلاصۀ ویرایش
 
(۵ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد)
خط ۱: خط ۱:
/**
/**
HotCat V2.41
HotCat V2.43


Ajax-based simple Category manager. Allows adding/removing/changing categories on a page view.
Ajax-based simple Category manager. Allows adding/removing/changing categories on a page view.
خط ۲۷: خط ۲۷:
// Don't use mw.config.get() as that takes a copy of the config, and so doesn't
// Don't use mw.config.get() as that takes a copy of the config, and so doesn't
// account for values changing, e.g. wgCurRevisionId after a VE edit
// account for values changing, e.g. wgCurRevisionId after a VE edit
var conf = mw.config.values;
var
        conf = mw.config.values,
        // when running on mobile domain - do not use wgServer.
        wgServer = window.location.host.indexOf('.m.') > -1 ?
            '//' + window.location.host : mw.config.get( 'wgServer' );


// Guard against double inclusions (in old IE/Opera element ids become window properties)
// Guard against double inclusions (in old IE/Opera element ids become window properties)
خط ۱۴۷: خط ۱۵۱:
parentcat: 'Parent categories'
parentcat: 'Parent categories'
},
},
// Set to false if your wiki has case-sensitive page names. MediaWiki has two modes: either the // any items, but that contains links to other categories where stuff should be categorized. If you don't have


// ("case-sensitive"; Category:aa !== Category:Aa). It doesn't currently have a fully case-insensitive mode
// Override the decision of whether HotCat should help users by automatically
// (which would mean Category:aa === Category:Aa === Category:AA === Category:aA)
// capitalising the title in the user input text if the wiki has case-sensitive page names.
// HotCat tries to set this correctly automatically using an API query. It's still a good idea to manually
// Basically, this will make an API query to check the MediaWiki configuration and HotCat then sets
// configure it correctly; either directly here if you copied HotCat, or in the local configuration file
// this to true for most wikis, and to false on Wiktionary.
// MediaWiki:Gadget-HotCat.js/local_defaults if you hotlink to the Commons-version, to ensure it is set even
//  
// if that API query should fail for some strange reason.
// You can set this directly if there is a problem with it. For example, Georgian Wikipedia (kawiki),
capitalizePageNames: true,
// is known to have different capitalisation logic between MediaWiki PHP and JavaScript. As such, automatic
// case changes in JavaScript by HotCat would be wrong.
capitalizePageNames: null,
// If upload_disabled is true, HotCat will not be used on the Upload form.
// If upload_disabled is true, HotCat will not be used on the Upload form.
upload_disabled: false,
upload_disabled: false,
خط ۱۸۶: خط ۱۹۱:
use_up_down: true,
use_up_down: true,
// Default list size
// Default list size
listSize: 5,
listSize: 10,
// If true, single category changes are marked as minor edits. If false, they're not.
// If true, single category changes are marked as minor edits. If false, they're not.
single_minor: true,
single_minor: true,
خط ۲۶۰: خط ۲۶۵:


function loadJS( page, callback ) {
function loadJS( page, callback ) {
load( conf.wgServer + conf.wgScript + '?title=' + encodeURIComponent( page ) + '&action=raw&ctype=text/javascript', callback );
load( wgServer + conf.wgScript + '?title=' + encodeURIComponent( page ) + '&action=raw&ctype=text/javascript', callback );
}
}


function loadURI( href, callback ) {
function loadURI( href, callback ) {
var url = href;
var url = href;
if ( url.substring( 0, 2 ) === '//' ) url = window.location.protocol + url; else if ( url.substring( 0, 1 ) === '/' ) url = conf.wgServer + url;
if ( url.substring( 0, 2 ) === '//' ) url = window.location.protocol + url; else if ( url.substring( 0, 1 ) === '/' ) url = wgServer + url;


load( url, callback );
load( url, callback );
خط ۲۸۳: خط ۲۸۸:


// Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
// Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
if ( window.hotcat_translations_from_commons && conf.wgServer.indexOf( '//commons' ) < 0 ) {
if ( window.hotcat_translations_from_commons && wgServer.indexOf( '//commons' ) < 0 ) {
loadURI( '//commons.wikimedia.org/w/index.php?title=' +
loadURI( '//commons.wikimedia.org/w/index.php?title=' +
'MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage +
'MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage +
خط ۳۷۹: خط ۳۸۴:


var script = conf.wgScript + '?';
var script = conf.wgScript + '?';
if ( href.indexOf( script ) === 0 || href.indexOf( conf.wgServer + script ) === 0 || conf.wgServer.substring( 0, 2 ) === '//' && href.indexOf( document.location.protocol + conf.wgServer + script ) === 0 ) {
if ( href.indexOf( script ) === 0 || href.indexOf( wgServer + script ) === 0 || wgServer.substring( 0, 2 ) === '//' && href.indexOf( document.location.protocol + wgServer + script ) === 0 ) {
// href="/w/index.php?title=..."
// href="/w/index.php?title=..."
return param( 'title', href );
return param( 'title', href );
خط ۳۸۵: خط ۳۹۰:
// href="/wiki/..."
// href="/wiki/..."
var prefix = conf.wgArticlePath.replace( '$1', '' );
var prefix = conf.wgArticlePath.replace( '$1', '' );
if ( href.indexOf( prefix ) ) prefix = conf.wgServer + prefix; // Fully expanded URL?
if ( href.indexOf( prefix ) ) prefix = wgServer + prefix; // Fully expanded URL?


if ( href.indexOf( prefix ) && prefix.substring( 0, 2 ) === '//' ) prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
if ( href.indexOf( prefix ) && prefix.substring( 0, 2 ) === '//' ) prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
خط ۷۲۲: خط ۷۲۷:
if ( page.starttimestamp ) startTime = page.starttimestamp.replace( /\D/g, '' );
if ( page.starttimestamp ) startTime = page.starttimestamp.replace( /\D/g, '' );
pageWatched = typeof page.watched === 'string';
pageWatched = typeof page.watched === 'string';
editToken = page.edittoken;
if ( json.query.tokens ) editToken = json.query.tokens.csrftoken;
if ( page.langlinks && ( !json[ 'query-continue' ] || !json[ 'query-continue' ].langlinks ) ) {
if ( page.langlinks && ( !json[ 'query-continue' ] || !json[ 'query-continue' ].langlinks ) ) {
// We have interlanguage links, and we got them all.
// We have interlanguage links, and we got them all.
خط ۷۳۳: خط ۷۳۸:
// Siteinfo
// Siteinfo
if ( json.query.general ) {
if ( json.query.general ) {
// ResourceLoader's JSParser doesn't like .case, so override eslint.
// eslint-disable-next-line dot-notation
HC.capitalizePageNames = ( json.query.general[ 'case' ] === 'first-letter' );
if ( json.query.general.time && !startTime ) startTime = json.query.general.time.replace( /\D/g, '' );
if ( json.query.general.time && !startTime ) startTime = json.query.general.time.replace( /\D/g, '' );
if ( HC.capitalizePageNames === null ) {
// ResourceLoader's JSParser doesn't like .case, so override eslint.
// eslint-disable-next-line dot-notation
HC.capitalizePageNames = ( json.query.general[ 'case' ] === 'first-letter' );
}
}
}
serverTime = startTime;
serverTime = startTime;
خط ۷۶۹: خط ۷۷۷:
// Must use Ajax here to get the user options and the edit token.
// Must use Ajax here to get the user options and the edit token.
$.getJSON(
$.getJSON(
conf.wgServer + conf.wgScriptPath + '/api.php?' +
wgServer + conf.wgScriptPath + '/api.php?' +
'format=json&action=query&rawcontinue=&titles=' + encodeURIComponent( conf.wgPageName ) +
'format=json&action=query&rawcontinue=&titles=' + encodeURIComponent( conf.wgPageName ) +
'&prop=info%7Crevisions%7Clanglinks&inprop=watched&intoken=edit&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500' +
'&prop=info%7Crevisions%7Clanglinks&inprop=watched&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500' +
'&rvlimit=2&rvdir=newer&rvstartid=' + conf.wgCurRevisionId + '&meta=siteinfo%7Cuserinfo&uiprop=options',
'&rvlimit=2&rvdir=newer&rvstartid=' + conf.wgCurRevisionId + '&meta=siteinfo%7Cuserinfo%7Ctokens&type=csrf&uiprop=options',
function ( json ) {
function ( json ) {
setPage( json );
setPage( json );
خط ۹۱۳: خط ۹۲۱:
var shortSummary = [];
var shortSummary = [];
// Deleted
// Deleted
for ( i = 0; i < deleted.length; i++ ) summary.push( '-' + substitute( HC.messages.short_catchange, [ null, deleted[ i ] ] ) );
for ( i = 0; i < deleted.length; i++ ) summary.push( '' + substitute( HC.messages.short_catchange, [ null, deleted[ i ] ] ) );


if ( deleted.length === 1 ) shortSummary.push( '-' + substitute( HC.messages.short_catchange, [ null, deleted[ 0 ] ] ) ); else if ( deleted.length ) shortSummary.push( '- ' + multiChangeMsg( deleted.length ) );
if ( deleted.length === 1 ) shortSummary.push( '' + substitute( HC.messages.short_catchange, [ null, deleted[ 0 ] ] ) ); else if ( deleted.length ) shortSummary.push( '' + multiChangeMsg( deleted.length ) );


// Added
// Added
خط ۱٬۰۵۶: خط ۱٬۰۶۴:
if ( i + 1 < toResolve.length ) args += '%7C';
if ( i + 1 < toResolve.length ) args += '%7C';
}
}
$.getJSON( conf.wgServer + conf.wgScriptPath + '/api.php?' + args,
$.getJSON( wgServer + conf.wgScriptPath + '/api.php?' + args,
function ( json ) {
function ( json ) {
resolveRedirects( toResolve, json );
resolveRedirects( toResolve, json );
خط ۱٬۳۶۴: خط ۱٬۳۷۲:
after.parentNode.insertBefore( span, after.nextSibling );
after.parentNode.insertBefore( span, after.nextSibling );
after = after.nextSibling;
after = after.nextSibling;
} else {
} else if (line) {
line.appendChild( span );
line.appendChild( span );
}
}
} else if ( line.firstChild ) {
} else if ( line && line.firstChild ) {
span.appendChild( make( ' ', true ) );
span.appendChild( make( ' ', true ) );
line.appendChild( span );
line.appendChild( span );
خط ۱٬۳۸۵: خط ۱٬۳۹۳:


span.appendChild( this.linkSpan );
span.appendChild( this.linkSpan );
if ( after ) after.parentNode.insertBefore( span, after.nextSibling ); else line.appendChild( span );
if ( after ) {
after.parentNode.insertBefore( span, after.nextSibling );
} else if ( line ) {
line.appendChild( span );
}


this.normalLinks = null;
this.normalLinks = null;
خط ۱٬۷۰۴: خط ۱٬۷۱۶:


display: function ( evt ) {
display: function ( evt ) {
if ( this.isAddCategory && !onUpload ) {
if ( this.isAddCategory && !onUpload && this.line ) {
// eslint-disable-next-line no-new
// eslint-disable-next-line no-new
new CategoryEditor( this.line, null, this.span, true ); // Create a new one
new CategoryEditor( this.line, null, this.span, true ); // Create a new one
خط ۱٬۸۱۲: خط ۱٬۸۲۴:
if ( next ) next.parentNode.removeChild( next );
if ( next ) next.parentNode.removeChild( next );
}
}
this.span.parentNode.removeChild( this.span );
if (this.span && this.span.parentNode) {
this.span.parentNode.removeChild( this.span );
}
for ( var i = 0; i < editors.length; i++ ) {
for ( var i = 0; i < editors.length; i++ ) {
if ( editors[ i ] === this ) {
if ( editors[ i ] === this ) {
خط ۱٬۹۳۱: خط ۱٬۹۴۵:
this.catLink.style.display = '';
this.catLink.style.display = '';
if ( this.isAddCategory ) {
if ( this.isAddCategory ) {
if ( onUpload ) {
if ( onUpload && this.line ) {
// eslint-disable-next-line no-new
// eslint-disable-next-line no-new
new CategoryEditor( this.line, null, this.span, true ); // Create a new one
new CategoryEditor( this.line, null, this.span, true ); // Create a new one
خط ۱٬۹۸۲: خط ۱٬۹۹۶:
return;
return;
}
}
if ( commitButton || onUpload ) {
this.close();
this.close();
if ( !commitButton && !onUpload ) {
} else {
this.close();
var self = this;
var self = this;
initiateEdit( function ( failure ) {
initiateEdit( function ( failure ) {
خط ۲٬۰۷۶: خط ۲٬۰۸۸:
var re = new RegExp( '^(' + HC.category_regexp + '):' );
var re = new RegExp( '^(' + HC.category_regexp + '):' );
if ( re.test( v ) ) v = v.substring( v.indexOf( ':' ) + 1 ).replace( /^(\s|_)+/, '' );
if ( re.test( v ) ) v = v.substring( v.indexOf( ':' ) + 1 ).replace( /^(\s|_)+/, '' );
 
v = v.replace(/\u200E$/, ''); // Trim ending left-to-right mark
if ( HC.capitalizePageNames ) v = capitalize( v );
if ( HC.capitalizePageNames ) v = capitalize( v );


// Only update the input field if there is a difference. Various browsers otherwise
// Update the input field if needed, manually restoring the cursor afterwards
// reset the selection and cursor position after each value re-assignment.
// (but not the whole selection, so that further typing does not overwrite anything)
if ( this.text.value !== null && this.text.value !== v ) this.text.value = v;
if ( this.text.value !== null && this.text.value !== v ) {
var cursorPos = this.text.selectionStart;
this.text.value = v;
this.text.setSelectionRange( cursorPos, cursorPos );
}
},
},


خط ۲٬۱۹۳: خط ۲٬۲۰۹:
for ( var j = 0; j < engines.length; j++ ) {
for ( var j = 0; j < engines.length; j++ ) {
var engine = suggestionEngines[ engines[ j ] ];
var engine = suggestionEngines[ engines[ j ] ];
var url = conf.wgServer + conf.wgScriptPath + engine.uri.replace( /\$1/g, encodeURIComponent( cleanKey ) );
var url = wgServer + conf.wgScriptPath + engine.uri.replace( /\$1/g, encodeURIComponent( cleanKey ) );
this.makeCall( url, cb, engine, v, cleanKey );
this.makeCall( url, cb, engine, v, cleanKey );
}
}
خط ۲٬۶۵۶: خط ۲٬۶۷۲:


function initialize() {
function initialize() {
// User configurations. Do this here, called from the onload handler, so that users can
// User configurations: Do this here, called from the onload handler, so that users can
// override it easily in their own user script files by just declaring variables. JSconfig
// override it easily in their own user script files by just declaring variables. JSconfig
// is some feature used at Wikimedia Commons.
// is some feature used at Wikimedia Commons.
خط ۲٬۶۶۷: خط ۲٬۶۸۳:
!!window.hotcat_no_autocommit : ( config.HotCatNoAutoCommit !== undefined ?
!!window.hotcat_no_autocommit : ( config.HotCatNoAutoCommit !== undefined ?
config.HotCatNoAutoCommit :
config.HotCatNoAutoCommit :
HC.no_autocommit ) );
// On talk namespace default autocommit off
( conf.wgNamespaceNumber % 2 ?
true : HC.no_autocommit ) ) );
HC.del_needs_diff = ( window.hotcat_del_needs_diff !== undefined ?
HC.del_needs_diff = ( window.hotcat_del_needs_diff !== undefined ?
!!window.hotcat_del_needs_diff :
!!window.hotcat_del_needs_diff :
خط ۲٬۸۹۳: خط ۲٬۹۱۱:
// Change the onsubmit handler
// Change the onsubmit handler
var form = document.getElementById( 'upload' ) || document.getElementById( 'mw-upload-form' );
var form = document.getElementById( 'upload' ) || document.getElementById( 'mw-upload-form' );
if ( form ) {
if ( form && ip && ip.insertRow ) {
var newRow = ip.insertRow( -1 );
var newRow = ip.insertRow( -1 );
newRow.appendChild( labelCell );
newRow.appendChild( labelCell );
خط ۲٬۹۶۹: خط ۲٬۹۸۷:
var result = $( scope ).find( tag + '.' + className );
var result = $( scope ).find( tag + '.' + className );
return ( result && result.length ) ? result[ 0 ] : null;
return ( result && result.length ) ? result[ 0 ] : null;
}
function errorAMC() {
alert( 'An error occurred. Unable to setup HotCat' );
}
function enableAMC() {
var api = new mw.Api();
return api.saveOption( 'mf_amc_optin', '1' ).then( function ( r ) {
if ( !r || r.options !== 'success' ) {
errorAMC();
return;
}
if ( window.confirm( 'Please reload your page to use hotcat.' ) ) {
location.reload();
}
}, function () {
errorAMC();
} );
}
function showWarning( text ) {
var warning = document.createElement( 'div' );
warning.setAttribute( 'style', 'padding: 20px; background: orange; color: #333; font-weight: bold; margin-top: 20px;' );
warning.textContent = text;
var btn = document.createElement( 'button' );
btn.classList.add( 'mw-ui-button', 'cdx-button' );
btn.style.display = 'block';
btn.textContent = 'Enable HotCat and AMC mode on this page';
btn.addEventListener( 'click', function () {
enableAMC();
} );
warning.appendChild( btn );
document.getElementById( 'mw-content-text' ).appendChild( warning );
}
}


خط ۲٬۹۸۶: خط ۳٬۰۳۸:
var hiddenCats = document.getElementById( 'mw-hidden-catlinks' );
var hiddenCats = document.getElementById( 'mw-hidden-catlinks' );
if ( !catLine ) {
if ( !catLine ) {
// Workaround for T24660
if ( mw.config.get('skin') === 'minerva' ) {
if ( document.body.classList.contains('mw-mf-amc-disabled') ) {
showWarning( 'HotCat requires AMC mode.' );
}
}
var footer = null;
var footer = null;
if ( !hiddenCats ) {
if ( !hiddenCats ) {
خط ۳٬۰۳۰: خط ۳٬۰۸۹:
for ( i = 0; i < copyCats.length; i++ ) {
for ( i = 0; i < copyCats.length; i++ ) {
var test = isOnPage( copyCats[ i ] );
var test = isOnPage( copyCats[ i ] );
if ( test !== null && test.match !== null ) {
if ( test !== null && test.match !== null && line ) {
// eslint-disable-next-line no-new
// eslint-disable-next-line no-new
new CategoryEditor( line, copyCats[ i ], test.title, test.match[ 2 ], is_hidden );
new CategoryEditor( line, copyCats[ i ], test.title, test.match[ 2 ], is_hidden );
خط ۳٬۰۸۰: خط ۳٬۱۳۹:
conf.wgScript + '?title=' + encodeURIComponent( conf.wgPageName ) + '&action=submit">' +
conf.wgScript + '?title=' + encodeURIComponent( conf.wgPageName ) + '&action=submit">' +
'<input type="hidden" name="wpTextbox1">' +
'<input type="hidden" name="wpTextbox1">' +
'<input type="hidden" name="model" value="wikitext">' +
'<input type="hidden" name="model" value="' + conf.wgPageContentModel + '">' +
'<input type="hidden" name="format" value="text/x-wiki">' +
'<input type="hidden" name="format" value="text/x-wiki">' +
'<input type="hidden" name="wpSummary" value="">' +
'<input type="hidden" name="wpSummary" value="">' +
خط ۳٬۰۹۰: خط ۳٬۱۴۹:
'<input type="hidden" name="wpDiff" value="wpDiff">' +
'<input type="hidden" name="wpDiff" value="wpDiff">' +
'<input type="hidden" name="oldid" value="0">' +
'<input type="hidden" name="oldid" value="0">' +
'<input type="hidden" name="wpIgnoreBlankSummary" value="1">' +
'<input type="submit" name="hcCommit" value="hcCommit">' +
'<input type="submit" name="hcCommit" value="hcCommit">' +
'<input type="hidden" name="wpEditToken">' +
'<input type="hidden" name="wpEditToken">' +
خط ۳٬۱۰۸: خط ۳٬۱۶۸:
setup( createCommitForm );
setup( createCommitForm );
} else {
} else {
var url = conf.wgServer + conf.wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&rawcontinue=&titles=' +
var url = wgServer + conf.wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&rawcontinue=&titles=' +
encodeURIComponent( conf.wgPageName ) +
encodeURIComponent( conf.wgPageName ) +
'&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid=' +
'&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid=' +
خط ۳٬۲۲۶: خط ۳٬۲۸۶:
// Reload HotCat after (VE) edits (bug T103285)
// Reload HotCat after (VE) edits (bug T103285)
mw.hook( 'postEdit' ).add( function () {
mw.hook( 'postEdit' ).add( function () {
// Reset HotCat in case this is a soft reload (VE edit)
// Reset HotCat in case this is a soft reload (e.g. VisualEditor edit), unless the categories
// were not re-rendered and our interface is still there (e.g. DiscussionTools edit)
if ( document.querySelector( '#catlinks .hotcatlink' ) ) {
return;
}
catLine = null;
catLine = null;
editors = [];
editors = [];