مدیاویکی:Gadget-HotCat.js: تفاوت میان نسخهها
بدون خلاصۀ ویرایش |
بدون خلاصۀ ویرایش |
||
(۵ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد) | |||
خط ۱: | خط ۱: | ||
/** | /** | ||
HotCat V2. | 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' | ||
}, | }, | ||
// | // Override the decision of whether HotCat should help users by automatically | ||
// | // capitalising the title in the user input text if the wiki has case-sensitive page names. | ||
// | // Basically, this will make an API query to check the MediaWiki configuration and HotCat then sets | ||
// | // this to true for most wikis, and to false on Wiktionary. | ||
// MediaWiki | // | ||
// | // You can set this directly if there is a problem with it. For example, Georgian Wikipedia (kawiki), | ||
capitalizePageNames: | // 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: | 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( | 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 = | 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 && | 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( | 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 = | 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 = | 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 ) { | ||
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( | ||
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 | '&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( ' | for ( i = 0; i < deleted.length; i++ ) summary.push( '−' + substitute( HC.messages.short_catchange, [ null, deleted[ i ] ] ) ); | ||
if ( deleted.length === 1 ) shortSummary.push( ' | 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( | $.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; | ||
} | } | ||
this.close(); | |||
if ( !commitButton && !onUpload ) { | |||
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 ); | ||
// | // Update the input field if needed, manually restoring the cursor afterwards | ||
// | // (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 = | 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 | // 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=" | '<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 = | 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 ( | // 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 = []; |