Block and notify: topic block now takes a topic area and its pages Tags: Mobile edit Mobile web edit Advanced mobile edit |
Block and notify: topic ban is a notice only, using Uw-topicban Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
| Line 1: | Line 1: | ||
/* <nowiki> */ | /* <nowiki> */ | ||
/* Bharat Mitra: block and notify module (administrators only). | /* Bharat Mitra: block and notify module (administrators only). | ||
Full blocks | Full blocks and partial blocks (specific pages) place a real block. | ||
A topic ban places no block: like on Wikipedia it only posts the | |||
topic ban notice, and the user must comply or face a block. */ | |||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
| Line 15: | Line 16: | ||
[ 'full', 'Full block (whole site)' ], | [ 'full', 'Full block (whole site)' ], | ||
[ 'partial', 'Partial block (specific pages)' ], | [ 'partial', 'Partial block (specific pages)' ], | ||
[ 'topic', 'Topic | [ 'topic', 'Topic ban (notice only)' ] | ||
]; | ]; | ||
function pageList( v ) { | function pageList( v ) { | ||
| Line 34: | Line 35: | ||
throw new Error( 'Add at least one page for a partial block.' ); | throw new Error( 'Add at least one page for a partial block.' ); | ||
} | } | ||
if ( v.blockType === 'topic' | if ( v.blockType === 'topic' && !String( v.topic || '' ).trim() ) { | ||
throw new Error( 'Name the topic for a topic ban.' ); | |||
} | } | ||
} | } | ||
function areaText( v ) { | function areaText( v ) { | ||
return joinAnd( pageList( v ).map( function ( t ) { return '[[' + t + ']]'; } ) ); | return joinAnd( pageList( v ).map( function ( t ) { return '[[' + t + ']]'; } ) ); | ||
} | |||
var MONTHNAMES = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; | |||
function expiryDate( v ) { | |||
var d = new Date(); | |||
var m = /^(\d+) (hour|day|week|month|year)s?$/.exec( v.expiry || '' ); | |||
if ( !m ) { return ''; } | |||
var n = parseInt( m[ 1 ], 10 ); | |||
if ( m[ 2 ] === 'hour' ) { d.setHours( d.getHours() + n ); } | |||
else if ( m[ 2 ] === 'day' ) { d.setDate( d.getDate() + n ); } | |||
else if ( m[ 2 ] === 'week' ) { d.setDate( d.getDate() + 7 * n ); } | |||
else if ( m[ 2 ] === 'month' ) { d.setMonth( d.getMonth() + n ); } | |||
else if ( m[ 2 ] === 'year' ) { d.setFullYear( d.getFullYear() + n ); } | |||
return d.getDate() + ' ' + MONTHNAMES[ d.getMonth() ] + ' ' + d.getFullYear(); | |||
} | } | ||
function buildNotice( v ) { | function buildNotice( v ) { | ||
/* Full blocks use Template:Uw-block, partial | /* Full blocks use Template:Uw-block, partial blocks use | ||
Template:Uw-pblock. Both take |time= (duration text) or |indef=yes, | Template:Uw-pblock. Both take |time= (duration text) or |indef=yes, | ||
|reason=, |anon=yes, |notalk=yes and |sig=yes (adds the signature). */ | |reason=, |anon=yes, |notalk=yes and |sig=yes (adds the signature). | ||
A topic ban uses Template:Uw-topicban and is a notice only. */ | |||
var name = String( v.username || '' ).trim(); | var name = String( v.username || '' ).trim(); | ||
if ( v.blockType === 'topic' ) { | |||
var tb = '{{subst:Uw-topicban|topic=' + BM.core.cleanParam( v.topic ) + '|heading=yes'; | |||
if ( v.expiry !== 'indefinite' ) { tb += '|expiry=' + expiryDate( v ); } | |||
tb += '}}'; | |||
tb += '\n\'\'\'Reason:\'\'\' ' + BM.core.cleanParam( v.reason ) + ' ' + BM.SIG; | |||
return tb; | |||
} | |||
var tpl = ( v.blockType === 'full' ) ? 'uw-block' : 'uw-pblock'; | var tpl = ( v.blockType === 'full' ) ? 'uw-block' : 'uw-pblock'; | ||
var out = '{{subst:' + tpl; | var out = '{{subst:' + tpl; | ||
| Line 73: | Line 87: | ||
BM.core.dialog( { | BM.core.dialog( { | ||
title: 'Block and notify', | title: 'Block and notify', | ||
help: ' | help: 'Full and partial blocks are placed at once and the matching notice is posted on the user\'s talk page. A topic ban places no block: it only posts the topic ban notice, and the user must stay away from that topic or face a block. This action is for administrators only.', | ||
fields: [ | fields: [ | ||
{ name: 'username', label: 'User name', type: 'text', value: user, required: true }, | { name: 'username', label: 'User name', type: 'text', value: user, required: true }, | ||
| Line 80: | Line 94: | ||
{ name: 'reason', label: 'Reason', type: 'text', required: true }, | { name: 'reason', label: 'Reason', type: 'text', required: true }, | ||
{ name: 'topic', label: 'Topic (topic block only)', type: 'text', placeholder: 'For example: Indian history' }, | { name: 'topic', label: 'Topic (topic block only)', type: 'text', placeholder: 'For example: Indian history' }, | ||
{ name: 'pages', label: 'Pages (partial | { name: 'pages', label: 'Pages (partial block only)', type: 'textarea', rows: 3, placeholder: 'One page title per line' }, | ||
{ name: 'nocreate', label: 'Also block new account creation', type: 'checkbox', value: true }, | { name: 'nocreate', label: 'Also block new account creation', type: 'checkbox', value: true }, | ||
{ name: 'talkBlock', label: 'Also block editing their own talk page', type: 'checkbox', value: false } | { name: 'talkBlock', label: 'Also block editing their own talk page', type: 'checkbox', value: false } | ||
| Line 86: | Line 100: | ||
preview: function ( v ) { validate( v ); return buildNotice( v ); }, | preview: function ( v ) { validate( v ); return buildNotice( v ); }, | ||
previewTitle: function ( v ) { return 'User talk:' + String( v.username || '' ).trim(); }, | previewTitle: function ( v ) { return 'User talk:' + String( v.username || '' ).trim(); }, | ||
submitLabel: ' | submitLabel: 'Submit', | ||
success: ' | success: 'Done.', | ||
onSubmit: function ( v ) { | onSubmit: function ( v ) { | ||
validate( v ); | validate( v ); | ||
var name = String( v.username || '' ).trim(); | var name = String( v.username || '' ).trim(); | ||
if ( v.blockType === 'topic' ) { | |||
return BM.core.appendUnderMonth( 'User talk:' + name, buildNotice( v ), | |||
'Topic ban notice (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ).catch( function () { | |||
throw new Error( 'The topic ban notice could not be sent.' ); | |||
} ); | |||
} | |||
var params = { | var params = { | ||
action: 'block', user: name, expiry: v.expiry, | action: 'block', user: name, expiry: v.expiry, | ||
reason: BM.core.cleanParam( v.reason ), format: 'json' | reason: BM.core.cleanParam( v.reason ), format: 'json' | ||
}; | }; | ||
if ( v.blockType === 'partial | if ( v.blockType === 'partial' ) { | ||
params.partial = 1; | params.partial = 1; | ||
params.pagerestrictions = pageList( v ).join( '|' ); | params.pagerestrictions = pageList( v ).join( '|' ); | ||
Revision as of 10:53, 15 November 2026
/* <nowiki> */
/* Bharat Mitra: block and notify module (administrators only).
Full blocks and partial blocks (specific pages) place a real block.
A topic ban places no block: like on Wikipedia it only posts the
topic ban notice, and the user must comply or face a block. */
( function () {
'use strict';
var BM = window.BharatMitra;
var EXPIRIES = [
[ '1 hour', '1 hour' ], [ '1 day', '1 day' ], [ '3 days', '3 days' ],
[ '1 week', '1 week' ], [ '2 weeks', '2 weeks' ], [ '1 month', '1 month' ],
[ '3 months', '3 months' ], [ '6 months', '6 months' ], [ '1 year', '1 year' ],
[ 'indefinite', 'Indefinite' ]
];
var TYPES = [
[ 'full', 'Full block (whole site)' ],
[ 'partial', 'Partial block (specific pages)' ],
[ 'topic', 'Topic ban (notice only)' ]
];
function pageList( v ) {
var seen = {}, out = [];
String( v.pages || '' ).split( /\r?\n/ ).forEach( function ( line ) {
var t = line.replace( /\|/g, '' ).replace( /\s+/g, ' ' ).trim();
if ( t && !seen[ t ] ) { seen[ t ] = 1; out.push( t ); }
} );
return out;
}
function joinAnd( list ) {
if ( list.length <= 1 ) { return list.join( '' ); }
if ( list.length === 2 ) { return list[ 0 ] + ' and ' + list[ 1 ]; }
return list.slice( 0, -1 ).join( ', ' ) + ' and ' + list[ list.length - 1 ];
}
function validate( v ) {
if ( v.blockType === 'partial' && !pageList( v ).length ) {
throw new Error( 'Add at least one page for a partial block.' );
}
if ( v.blockType === 'topic' && !String( v.topic || '' ).trim() ) {
throw new Error( 'Name the topic for a topic ban.' );
}
}
function areaText( v ) {
return joinAnd( pageList( v ).map( function ( t ) { return '[[' + t + ']]'; } ) );
}
var MONTHNAMES = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
function expiryDate( v ) {
var d = new Date();
var m = /^(\d+) (hour|day|week|month|year)s?$/.exec( v.expiry || '' );
if ( !m ) { return ''; }
var n = parseInt( m[ 1 ], 10 );
if ( m[ 2 ] === 'hour' ) { d.setHours( d.getHours() + n ); }
else if ( m[ 2 ] === 'day' ) { d.setDate( d.getDate() + n ); }
else if ( m[ 2 ] === 'week' ) { d.setDate( d.getDate() + 7 * n ); }
else if ( m[ 2 ] === 'month' ) { d.setMonth( d.getMonth() + n ); }
else if ( m[ 2 ] === 'year' ) { d.setFullYear( d.getFullYear() + n ); }
return d.getDate() + ' ' + MONTHNAMES[ d.getMonth() ] + ' ' + d.getFullYear();
}
function buildNotice( v ) {
/* Full blocks use Template:Uw-block, partial blocks use
Template:Uw-pblock. Both take |time= (duration text) or |indef=yes,
|reason=, |anon=yes, |notalk=yes and |sig=yes (adds the signature).
A topic ban uses Template:Uw-topicban and is a notice only. */
var name = String( v.username || '' ).trim();
if ( v.blockType === 'topic' ) {
var tb = '{{subst:Uw-topicban|topic=' + BM.core.cleanParam( v.topic ) + '|heading=yes';
if ( v.expiry !== 'indefinite' ) { tb += '|expiry=' + expiryDate( v ); }
tb += '}}';
tb += '\n\'\'\'Reason:\'\'\' ' + BM.core.cleanParam( v.reason ) + ' ' + BM.SIG;
return tb;
}
var tpl = ( v.blockType === 'full' ) ? 'uw-block' : 'uw-pblock';
var out = '{{subst:' + tpl;
if ( v.expiry === 'indefinite' ) {
out += '|indef=yes';
} else {
out += '|time=' + v.expiry;
}
if ( tpl === 'uw-pblock' ) { out += '|area=' + areaText( v ); }
out += '|reason=' + BM.core.cleanParam( v.reason );
if ( mw.util.isIPAddress( name ) ) { out += '|anon=yes'; }
if ( v.talkBlock ) { out += '|notalk=yes'; }
out += '|sig=yes}}';
return out;
}
BM.register( 'block', {
open: function () {
var user = BM.targetUser() || '';
BM.core.dialog( {
title: 'Block and notify',
help: 'Full and partial blocks are placed at once and the matching notice is posted on the user\'s talk page. A topic ban places no block: it only posts the topic ban notice, and the user must stay away from that topic or face a block. This action is for administrators only.',
fields: [
{ name: 'username', label: 'User name', type: 'text', value: user, required: true },
{ name: 'blockType', label: 'Block type', type: 'select', required: true, options: TYPES, value: 'full' },
{ name: 'expiry', label: 'Duration', type: 'select', required: true, options: EXPIRIES, value: '1 week' },
{ name: 'reason', label: 'Reason', type: 'text', required: true },
{ name: 'topic', label: 'Topic (topic block only)', type: 'text', placeholder: 'For example: Indian history' },
{ name: 'pages', label: 'Pages (partial block only)', type: 'textarea', rows: 3, placeholder: 'One page title per line' },
{ name: 'nocreate', label: 'Also block new account creation', type: 'checkbox', value: true },
{ name: 'talkBlock', label: 'Also block editing their own talk page', type: 'checkbox', value: false }
],
preview: function ( v ) { validate( v ); return buildNotice( v ); },
previewTitle: function ( v ) { return 'User talk:' + String( v.username || '' ).trim(); },
submitLabel: 'Submit',
success: 'Done.',
onSubmit: function ( v ) {
validate( v );
var name = String( v.username || '' ).trim();
if ( v.blockType === 'topic' ) {
return BM.core.appendUnderMonth( 'User talk:' + name, buildNotice( v ),
'Topic ban notice (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ).catch( function () {
throw new Error( 'The topic ban notice could not be sent.' );
} );
}
var params = {
action: 'block', user: name, expiry: v.expiry,
reason: BM.core.cleanParam( v.reason ), format: 'json'
};
if ( v.blockType === 'partial' ) {
params.partial = 1;
params.pagerestrictions = pageList( v ).join( '|' );
}
if ( v.nocreate ) { params.nocreate = 1; }
if ( !v.talkBlock ) { params.allowusertalk = 1; }
return BM.core.api().postWithToken( 'block', params ).then( function ( r ) {
if ( !( r && r.block ) ) { throw new Error( 'The block could not be applied.' ); }
var block = buildNotice( v );
return BM.core.appendUnderMonth( 'User talk:' + name, block,
'Block notice (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ).catch( function () {
throw new Error( 'The block was applied, but the notice could not be sent.' );
} );
} );
}
} );
}
} );
}() );
/* </nowiki> */