No edit summary |
Block and notify: add partial and topic block types with matching notices Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
| Line 1: | Line 1: | ||
/* <nowiki> */ | /* <nowiki> */ | ||
/* Bharat Mitra: block module (administrators only) */ | /* Bharat Mitra: block and notify module (administrators only). | ||
Full blocks, partial blocks (specific pages) and topic blocks | |||
(namespaces), each with the matching talk page notice. */ | |||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
var BM = window.BharatMitra; | var BM = window.BharatMitra; | ||
var EXPIRIES = [ | var EXPIRIES = [ | ||
[ '1 hour', '1 hour' ], [ '1 day', '1 day' ], [ '1 week', '1 week' ], | [ '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 block (namespaces)' ] | |||
]; | |||
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 namespaceList( v ) { | |||
var ids = mw.config.get( 'wgNamespaceIds' ) || {}; | |||
var names = [], nsids = []; | |||
String( v.namespaces || '' ).split( ',' ).forEach( function ( part ) { | |||
var name = part.replace( /\s+/g, ' ' ).trim(); | |||
if ( !name ) { return; } | |||
if ( /^\d+$/.test( name ) ) { | |||
nsids.push( name ); names.push( 'namespace ' + name ); return; | |||
} | |||
var key = name.toLowerCase().replace( /\s+/g, '_' ); | |||
if ( key === 'article' || key === 'main' || key === '(article)' ) { key = ''; } | |||
if ( !( key in ids ) ) { | |||
throw new Error( 'Unknown namespace: ' + name ); | |||
} | |||
nsids.push( String( ids[ key ] ) ); names.push( name ); | |||
} ); | |||
return { ids: nsids, names: names }; | |||
} | |||
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' && !namespaceList( v ).ids.length ) { | |||
throw new Error( 'Add at least one namespace for a topic block.' ); | |||
} | |||
} | |||
function areaText( v ) { | |||
if ( v.blockType === 'partial' ) { | |||
return joinAnd( pageList( v ).map( function ( t ) { return '[[' + t + ']]'; } ) ); | |||
} | |||
var ns = namespaceList( v ); | |||
if ( ns.names.length === 1 ) { return 'the ' + ns.names[ 0 ] + ' namespace'; } | |||
return 'the ' + joinAnd( ns.names ) + ' namespaces'; | |||
} | |||
function buildNotice( v ) { | function buildNotice( v ) { | ||
/* Template:Uw-block | /* Full blocks use Template:Uw-block, partial and topic blocks use | ||
Template:Uw-pblock. Both take |time= (duration text) or |indef=yes, | |||
|reason=, |anon=yes, |notalk=yes and |sig=yes (adds the signature). */ | |||
var name = String( v.username || '' ).trim(); | var name = String( v.username || '' ).trim(); | ||
var out = '{{subst: | var tpl = ( v.blockType === 'full' ) ? 'uw-block' : 'uw-pblock'; | ||
var out = '{{subst:' + tpl; | |||
if ( v.expiry === 'indefinite' ) { | if ( v.expiry === 'indefinite' ) { | ||
out += '|indef=yes'; | out += '|indef=yes'; | ||
| Line 18: | Line 76: | ||
out += '|time=' + v.expiry; | out += '|time=' + v.expiry; | ||
} | } | ||
if ( tpl === 'uw-pblock' ) { out += '|area=' + areaText( v ); } | |||
out += '|reason=' + BM.core.cleanParam( v.reason ); | out += '|reason=' + BM.core.cleanParam( v.reason ); | ||
if ( mw.util.isIPAddress( name ) ) { out += '|anon=yes'; } | if ( mw.util.isIPAddress( name ) ) { out += '|anon=yes'; } | ||
if ( v.talkBlock ) { out += '|notalk=yes'; } | |||
out += '|sig=yes}}'; | out += '|sig=yes}}'; | ||
return out; | return out; | ||
| Line 27: | Line 87: | ||
var user = BM.targetUser() || ''; | var user = BM.targetUser() || ''; | ||
BM.core.dialog( { | BM.core.dialog( { | ||
title: 'Block | title: 'Block and notify', | ||
help: ' | help: 'Block this user and automatically post the matching notice on their talk page: a full block notice, a partial block notice, or a topic block notice. 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 }, | ||
{ 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: 'expiry', label: 'Duration', type: 'select', required: true, options: EXPIRIES, value: '1 week' }, | ||
{ name: 'reason', label: 'Reason', type: 'text', required: true }, | { name: 'reason', label: 'Reason', type: 'text', required: true }, | ||
{ name: 'pages', label: 'Pages (partial block only)', type: 'textarea', rows: 3, placeholder: 'One page title per line' }, | |||
{ name: 'namespaces', label: 'Namespaces (topic block only)', type: 'text', placeholder: 'Comma-separated, for example: Article, User talk' }, | |||
{ 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 } | ||
], | ], | ||
preview: function ( 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: 'Block', | submitLabel: 'Block', | ||
success: 'Block applied and notice sent.', | success: 'Block applied and notice sent.', | ||
onSubmit: function ( v ) { | onSubmit: function ( v ) { | ||
validate( v ); | |||
var name = String( v.username || '' ).trim(); | var name = String( v.username || '' ).trim(); | ||
var params = { | var params = { | ||
| Line 46: | Line 110: | ||
reason: BM.core.cleanParam( v.reason ), format: 'json' | reason: BM.core.cleanParam( v.reason ), format: 'json' | ||
}; | }; | ||
if ( v.blockType === 'partial' ) { | |||
params.partial = 1; | |||
params.pagerestrictions = pageList( v ).join( '|' ); | |||
} else if ( v.blockType === 'topic' ) { | |||
params.partial = 1; | |||
params.namespacerestrictions = namespaceList( v ).ids.join( '|' ); | |||
} | |||
if ( v.nocreate ) { params.nocreate = 1; } | if ( v.nocreate ) { params.nocreate = 1; } | ||
if ( !v.talkBlock ) { params.allowusertalk = 1; } | if ( !v.talkBlock ) { params.allowusertalk = 1; } | ||
Revision as of 10:32, 15 November 2026
/* <nowiki> */
/* Bharat Mitra: block and notify module (administrators only).
Full blocks, partial blocks (specific pages) and topic blocks
(namespaces), each with the matching talk page notice. */
( 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 block (namespaces)' ]
];
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 namespaceList( v ) {
var ids = mw.config.get( 'wgNamespaceIds' ) || {};
var names = [], nsids = [];
String( v.namespaces || '' ).split( ',' ).forEach( function ( part ) {
var name = part.replace( /\s+/g, ' ' ).trim();
if ( !name ) { return; }
if ( /^\d+$/.test( name ) ) {
nsids.push( name ); names.push( 'namespace ' + name ); return;
}
var key = name.toLowerCase().replace( /\s+/g, '_' );
if ( key === 'article' || key === 'main' || key === '(article)' ) { key = ''; }
if ( !( key in ids ) ) {
throw new Error( 'Unknown namespace: ' + name );
}
nsids.push( String( ids[ key ] ) ); names.push( name );
} );
return { ids: nsids, names: names };
}
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' && !namespaceList( v ).ids.length ) {
throw new Error( 'Add at least one namespace for a topic block.' );
}
}
function areaText( v ) {
if ( v.blockType === 'partial' ) {
return joinAnd( pageList( v ).map( function ( t ) { return '[[' + t + ']]'; } ) );
}
var ns = namespaceList( v );
if ( ns.names.length === 1 ) { return 'the ' + ns.names[ 0 ] + ' namespace'; }
return 'the ' + joinAnd( ns.names ) + ' namespaces';
}
function buildNotice( v ) {
/* Full blocks use Template:Uw-block, partial and topic blocks use
Template:Uw-pblock. Both take |time= (duration text) or |indef=yes,
|reason=, |anon=yes, |notalk=yes and |sig=yes (adds the signature). */
var name = String( v.username || '' ).trim();
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: 'Block this user and automatically post the matching notice on their talk page: a full block notice, a partial block notice, or a topic block notice. 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: 'pages', label: 'Pages (partial block only)', type: 'textarea', rows: 3, placeholder: 'One page title per line' },
{ name: 'namespaces', label: 'Namespaces (topic block only)', type: 'text', placeholder: 'Comma-separated, for example: Article, User talk' },
{ 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: 'Block',
success: 'Block applied and notice sent.',
onSubmit: function ( v ) {
validate( v );
var name = String( v.username || '' ).trim();
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( '|' );
} else if ( v.blockType === 'topic' ) {
params.partial = 1;
params.namespacerestrictions = namespaceList( v ).ids.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> */