MediaWiki:Gadget-Massblock.js: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m docfix |
+opzione per bloccare la talk anche qui |
||
(13 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 10:
* see [[:it:MediaWiki:Gadget-Massblock-it.js]]
*
* Memo: only then() can return a new promise object, done() and fail() can't.
*
* @author (i.e. blame him) [[:it:User:Daimona Eaytoy]]
*/
( function( mw, $ ) {
'use strict';
// Default messages
mw.messages.set( {
'massblock-toolbar-text': 'Massblock',
Line 28 ⟶ 23:
'massblock-page-title': 'Mass-blocking tool',
'massblock-abuse-disclaimer': 'If you abuse this tool, it\'s your fault, not mine.',
'massblock-blockusers': 'Users to block (one on each line, please):',
'massblock-talkmsg': 'Replace talk page with, or text to add if the expiry is not infinite (leave blank to leave no message):',
Line 58 ⟶ 52:
} );
// OOUI element
var submitBtn,
// Ideally this would use $wgBlockAllowsUTEdit, but it's not available in JS.
blockAllowsTalkEdit = true,
mwapi = new mw.Api();
var ErrorHandler = {
errors: {},
/**
* Process an error in any part of the process
Line 114 ⟶ 71:
* which has no hyphen and no page.
*/
var obj = { action: e };
if ( !this.errors[ user ] ) {
this.errors[ user ] = [ obj ];
} else {
this.errors[ user ].push( obj );
}
}
getCodesForUser: function( user ) {
var cb = function( el ) {
var key = Object.keys( el )[ 0 ],
action = key.split( "-" )[ 0 ],
link = '//mediawiki.org/wiki/API:' + action + '#Possible_errors';
return key + ': <code style="color:red">' +
el[ key ] + '</code> (<a href="' + link + '">' +
msg( 'failure-help' ) + '</a>)';
};
return this.errors[ user ].map( cb );
}
};
var FormData = {
init: function() {
this.dropdownReason = $( "#wpMassBlockReasons select :selected" ).val().trim();
this.otherReason = $( "#wpMassBlockReason input" ).val().trim();
this.anononly = $( "#wpMassBlockAnononly input" ).prop( 'checked' );
this.nocreate = $( "#wpMassBlockNocreate input" ).prop( 'checked' );
this.noEmail = $( "#wpMassBlockEmail input" ).prop( 'checked' );
this.autoblock = $( "#wpMassBlockAutoblock input" ).prop( 'checked' );
this.talkpageBlocked = blockAllowsTalkEdit ? $( "#wpMassBlockTalkpage input" ).prop( 'checked' ) : true;
this.reblock = $( "#wpMassBlockReblock input" ).prop( 'checked' );
this.talkMessage = $( "#wpMassBlockMessage textarea" ).val().trim();
this.expiry = $( "#wpMassBlockExpiry input" ).val().trim();
this.talkSummary = $( "#wpMassBlockSummaryTalk input" ).val().trim();
this.upSummary = $( "#wpMassBlockSummaryUser input" ).val().trim();
this.protectReason = $( "#wpMassBlockProtectReason input" ).val().trim();
this.isInfty = isInfinity( this.expiry );
//
this.protectTalk = $( "#wpMassBlockProtectTalk input" ).prop( 'checked' ) && this.isInfty;
this.protectUser = $( "#wpMassBlockProtectUser input" ).prop( 'checked' ) && this.isInfty;
this.upMessage = this.isInfty ? $( "#wpMassBlockTag textarea" ).val().trim() : '';
};
var Main = {
blocked: 0,
talkpageedited: 0,
userpageedited: 0,
talkpageprotected: 0,
userpageprotected: 0,
_protect: function( page, exists, counter, user ) {
var prType = exists ? 'edit=sysop|move=sysop' : 'create=sysop';
return doProtectPage( page, 'edit=sysop|move=sysop' )
.done( function() {
} )
ErrorHandler.add( e, user, "protect-talk" );
} )
. },
editTalk: function( user ) {
var talkPage = getTalkTitle( user );
return doEditPage( talkPage, FormData.talkMessage, FormData.talkSummary, !FormData.isInfty )
if ( FormData.protectTalk ) {
return this._protect( talkPage, true, 'talkpageprotected', user );
}
}.bind( this ),
}
);
},
protectTalk: function( user ) {
action: 'query',
titles: talkPage
} )
.then(
var exists = Object.keys( data.query.pages )[ 0 ] !== -1;
return this._protect( talkPage, exists, 'talkpageprotected', user );
ErrorHandler.add( e, user, "query-talk" );
}
var userPage = getUPTitle( user );
doEditPage( userPage, FormData.upMessage, FormData.upSummary )
this.userpageedited++;
if ( FormData.protectUser ) {
return this._protect( userPage, true, 'userpageprotected', user );
}
}.bind( this ),
}
);
},
protectUP: function( user ) {
return mwapi.get( {
action: 'query',
titles: userPage
} )
.then(
var exists = Object.keys( data.query.pages )[ 0 ] !== -1;
return this._protect( userPage, exists, 'userpageprotected', user );
ErrorHandler.add( e, user, "query-user" );
}
getAlertText: function() {
return msg( 'result-alert' )
.replace( '$1', this.blocked )
.replace( '$2', this.talkpageedited )
.replace( '$3', this.userpageedited )
.replace( '$4', this.talkpageprotected )
.replace( '$5', this.userpageprotected );
}
};
function getTalkTitle( user ) {
return 'User talk:' + user;
}
function getUPTitle( user ) {
return 'User:' + user;
}
/**
* The post-block handler. Performs edits and protections.
*
* @param {string} user
* @return {Promise} A promise which is resolved after all actions are done
* for all pages. This will never be rejected.
*/
function successHandler( user ) {
// @var {Promise} These track the state for all actions (edit and protect)
// on every page. They are resolved as soon as a page is processed, with or
// without a failure. This way the final $.when will resolve after all promises
// have been processed, and not after the first rejection.
var talkDone, userDone;
Main.blocked++;
if ( FormData.talkMessage !== "" ) {
talkDone = Main.editTalk( user );
} else if ( FormData.protectTalk ) {
talkDone = Main.protectTalk( user );
} else {
talkDone = $.when();
}
if ( FormData.upMessage !== "" ) {
userDone = Main.editUP( user );
} else if ( FormData.protectUser ) {
userDone = Main.protectUP( user );
} else {
userDone = $.when();
}
return $.when( talkDone, userDone );
}
/**
* Main form processing routine, called on form submit
*/
function doMassBlock() {
var users = $( "#wpMassBlockUsers textarea" ).val().split( "\n" );
users = users
// First trim everything
.map( function( s ) {
return s.trim();
} )
// Then remove blanks and duplicates
.filter( function( el, index, me ) {
return el !== '' && me.indexOf( el ) === index;
} );
if ( users.length === 0 ) {
// Easy
return;
}
submitBtn.setDisabled( true );
FormData.init();
// Array of Promises, one for each user. Each one is resolved after the user
Line 280 ⟶ 288:
deferreds.push(
doBlock( user )
.then(
function(
return successHandler(
},
function( e ) {
// Return so that this counts as resolved and won't leave
// other promises unresolved.
return $.when();
}
)
);
} );
$.when.apply( $, deferreds ).always(
}
/**
* Executed after all users have been processed.
*/
function doPostBlockActions(
var errors = ErrorHandler.errors;
if ( Object.keys( errors ).length > 0 ) {
var linkedList =
for ( var user in errors ) {
var codes =
linkedList +=
"<li><a href=\"" + mw.config.get( 'wgScript' ) + "?title=Special:Contributions/" + encodeURIComponent( user ) + "\"> user + "</a>: " + codes.join( "; " ) + "</li>";
}
$( "#wpMassBlockFailedContainer" ).html(
Line 331 ⟶ 323:
);
}
OO.ui.alert( Main.getAlertText() );
}
/**
* Perform a single block via API
*
* @param {string} user The user to block
* @return {Promise}
*/
function doBlock( user ) {
return mwapi.postWithToken( "csrf", {
action: 'block',
allowusertalk: !FormData.talkpageBlocked,
autoblock: FormData.autoblock,
nocreate: FormData.nocreate,
expiry: FormData.expiry === "" ? "indefinite" : FormData.expiry,
anononly: FormData.anononly,
noemail: FormData.noEmail,
reblock: FormData.reblock,
reason: FormData.dropdownReason === "other" ? FormData.otherReason : FormData.dropdownReason + ( FormData.otherReason ? ": " + FormData.otherReason : "" ),
user: user
} );
}
Line 355 ⟶ 370:
params.text = text;
}
return
}
Line 366 ⟶ 381:
*/
function doProtectPage( title, protections ) {
return
action: 'protect',
title: title,
protections: protections,
reason:
watchlist: 'nochange'
} );
Line 393 ⟶ 408:
talkTextField, userTextField, talkProtectCb, talkSummaryField,
userSummaryField, userProtectCb, protectReasonField;
$( "h1" ).first().html( msg( 'page-title' ) );
Line 412 ⟶ 424:
} );
var
label: msg( '
items: [
new OO.ui.
rows: 10,
} )
]
Line 574 ⟶ 569:
} );
!isInfinity( $( '#wpMassBlockExpiry input' ).val().trim() );
userSummaryField.setDisabled( disabled );
};
userTextField.on( 'change', toggleUserTextField );
talkProtectCb =
new OO.ui.CheckboxInputWidget( {
id: 'wpMassBlockProtectTalk'
} );
userProtectCb =
new OO.ui.CheckboxInputWidget( {
id: 'wpMassBlockProtectUser'
} );
Line 594 ⟶ 591:
maxLength: 255,
id: 'wpMassBlockProtectReason',
value: msg( 'protect-reason-default' ),
disabled: true
} );
Line 601 ⟶ 599:
*/
var toggleProtectionFields = function() {
if (
!( talkProtectCb.isSelected() || userProtectCb.isSelected() ) ||
!isInfinity( $( '#wpMassBlockExpiry input' ).val().trim() )
) {
protectReasonField.setDisabled( true );
} else {
Line 614 ⟶ 615:
label: msg( 'further-options-label' ),
items: [
new OO.ui.HorizontalLayout( {
items: [
new OO.ui.FieldLayout(
talkTextField,
{
label: msg( 'talkmsg' ),
align: 'top',
id: 'wpMassBlockMessage',
classes: [ 'massblock-horiz-element' ]
}
),
new OO.ui.FieldLayout(
userTextField,
{
label: msg( 'upmsg' ),
align: 'top',
id: 'wpMassBlockTag',
classes: [ 'massblock-horiz-element' ]
}
)
]
} ),
new OO.ui.FieldLayout( talkSummaryField, {
label: msg( 'talksummary' )
Line 639 ⟶ 662:
userTextField,
talkProtectCb,
];
Line 647 ⟶ 668:
disableEls[ el ].setDisabled( !enable );
}
toggleProtectionFields();
toggleUserTextField();
} );
Line 660 ⟶ 684:
.on( 'click', doMassBlock );
form.addItems( [
Line 683 ⟶ 707:
( /sysop/ ).test( mw.config.get( "wgUserGroups" ) )
) {
var style ='.massblock-horiz-element{ width: 40%; }';
mw.util.addCSS( style );
mw.loader.using( [ 'mediawiki.jqueryMsg', 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows' ], $.ready )
.done( function loadMsg() {
.done( massblockform )
.fail( function ( e ) {
Line 695 ⟶ 721:
} );
} else {
mw.util.addPortletLink( 'p-tb', mw.
}
} );
|