Add planAll and sendAuto for automatic warnings |
Fix automatic warning levels: read substituted warning markers so the level escalates |
||
| Line 24: | Line 24: | ||
function analyze( content ) { | function analyze( content ) { | ||
var found = []; | var found = []; | ||
var | var text = content || ''; | ||
var m; | function collect( re ) { | ||
var m; | |||
while ( ( m = re.exec( text ) ) !== null ) { | |||
var t = BASEMAP[ m[ 1 ].toLowerCase() ]; | |||
if ( !t ) { continue; } | |||
found.push( { type: t.key, level: ( m[ 2 ] || '' ).toLowerCase(), pos: m.index } ); | |||
} | |||
} | } | ||
/* Warnings placed as template calls, not yet substituted or | |||
placed without subst. */ | |||
collect( /\{\{(?:subst:)?(uw-[a-z0-9-]+?)(4im|[1-4])?(?:\||\}\})/gi ); | |||
/* A substituted warning leaves no template call behind in the | |||
saved text. The warning templates leave a marker comment in | |||
their output, for example <!-- Template:uw-vandalism2 -->, | |||
and that marker is the only record of the type and level | |||
that was issued. Reading only template calls missed every | |||
substituted warning, so the suggested level always fell | |||
back to the first level. */ | |||
collect( /<!--\s*Template:(uw-[a-z0-9-]+?)(4im|[1-4])?\s*-->/gi ); | |||
found.sort( function ( a, b ) { return a.pos - b.pos; } ); | |||
return found; | return found; | ||
} | } | ||
Revision as of 20:21, 11 November 2026
/* <nowiki> */
/* Bharat Mitra: warn module */
( function () {
'use strict';
var BM = window.BharatMitra;
/* Warning types: base template and the levels it offers.
levels: null means a single notice with no level. */
var TYPES = [
{ key: 'vandalism', label: 'Vandalism', base: 'uw-vandalism', levels: [ '1', '2', '3', '4', '4im' ] },
{ key: 'advertising', label: 'Advertising', base: 'uw-advert', levels: [ '1', '2', '3', '4', '4im' ] },
{ key: 'unsourced', label: 'Unsourced', base: 'uw-unsourced', levels: [ '1', '2', '3', '4' ] },
{ key: 'npa', label: 'Personal attacks', base: 'uw-npa', levels: [ '1', '2', '3', '4' ] },
{ key: 'harassment', label: 'Harassment', base: 'uw-harass', levels: [ '1', '2', '3', '4', '4im' ] },
{ key: 'copyright', label: 'Copyright', base: 'uw-copyright', levels: null },
{ key: 'test', label: 'Test edits', base: 'uw-test', levels: [ '1', '2', '3' ] },
{ key: 'npov', label: 'Point of view', base: 'uw-npov', levels: [ '1', '2', '3', '4' ] },
{ key: 'blanking', label: 'Blanking', base: 'uw-blank', levels: null },
{ key: 'username', label: 'Username', base: 'uw-username', levels: null }
];
var TYPEMAP = {}, BASEMAP = {};
TYPES.forEach( function ( t ) { TYPEMAP[ t.key ] = t; BASEMAP[ t.base ] = t; } );
var RANK = { '1': 1, '2': 2, '3': 3, '4': 4, '4im': 5 };
function analyze( content ) {
var found = [];
var text = content || '';
function collect( re ) {
var m;
while ( ( m = re.exec( text ) ) !== null ) {
var t = BASEMAP[ m[ 1 ].toLowerCase() ];
if ( !t ) { continue; }
found.push( { type: t.key, level: ( m[ 2 ] || '' ).toLowerCase(), pos: m.index } );
}
}
/* Warnings placed as template calls, not yet substituted or
placed without subst. */
collect( /\{\{(?:subst:)?(uw-[a-z0-9-]+?)(4im|[1-4])?(?:\||\}\})/gi );
/* A substituted warning leaves no template call behind in the
saved text. The warning templates leave a marker comment in
their output, for example <!-- Template:uw-vandalism2 -->,
and that marker is the only record of the type and level
that was issued. Reading only template calls missed every
substituted warning, so the suggested level always fell
back to the first level. */
collect( /<!--\s*Template:(uw-[a-z0-9-]+?)(4im|[1-4])?\s*-->/gi );
found.sort( function ( a, b ) { return a.pos - b.pos; } );
return found;
}
function suggestFor( t, found ) {
if ( !t.levels ) { return ''; }
var max = '';
found.forEach( function ( f ) {
if ( f.type !== t.key || !f.level ) { return; }
if ( !max || RANK[ f.level ] > RANK[ max ] ) { max = f.level; }
} );
if ( !max ) { return t.levels[ 0 ]; }
var idx = t.levels.indexOf( max );
if ( idx === -1 ) { return t.levels[ t.levels.length - 1 ]; }
return t.levels[ Math.min( idx + 1, t.levels.length - 1 ) ];
}
function clampLevel( t, level ) {
if ( !t.levels ) { return ''; }
if ( t.levels.indexOf( level ) !== -1 ) { return level; }
var best = t.levels[ 0 ];
t.levels.forEach( function ( l ) {
if ( RANK[ l ] <= ( RANK[ level ] || 0 ) ) { best = l; }
} );
return best;
}
function buildBlock( t, levelSuffix, article, comment ) {
var out = '{{subst:' + t.base + levelSuffix;
var a = BM.core.cleanParam( article );
var c = BM.core.cleanParam( comment );
if ( a ) { out += '|' + a; }
if ( c ) { out += '|' + c; }
out += '}}';
return out + BM.SIG;
}
/* Automatic warning plan for one type: the level is the next level of
that type based on the warnings already on the talk page, exactly as
the warning dialog chooses it with its Automatic setting. */
function planFor( t, found, article ) {
var lvl = t.levels ? suggestFor( t, found ) : '';
return { type: t.key, label: t.label, level: lvl, block: buildBlock( t, lvl, article, '' ) };
}
BM.register( 'warn', {
/* Plans for every warning type for a user, from one talk page read.
Used by the revert module to show and send an automatic warning. */
planAll: function ( user, article ) {
return BM.core.getPage( 'User talk:' + user ).then( function ( page ) {
var found = analyze( page.content );
var out = {};
TYPES.forEach( function ( t ) { out[ t.key ] = planFor( t, found, article ); } );
return out;
} );
},
/* Send one warning with no dialog: the type is given, the level is
chosen automatically. Resolves with the plan that was sent. */
sendAuto: function ( user, typeKey, article ) {
var t = TYPEMAP[ typeKey ] || TYPEMAP.vandalism;
var talk = 'User talk:' + user;
return BM.core.getPage( talk ).then( function ( page ) {
var plan = planFor( t, analyze( page.content ), article );
return BM.core.appendUnderMonth( talk, plan.block,
'Warning issued (' + plan.label + ( plan.level ? ', level ' + plan.level : '' ) + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' )
.then( function () { return plan; } );
} );
},
open: function () {
var user = BM.targetUser();
if ( !user ) { mw.notify( 'This option is only available on user pages.', { type: 'error' } ); return; }
var talk = 'User talk:' + user;
$.when( BM.core.getConfig(), BM.core.getPage( talk ) ).then( function ( cfg, page ) {
var found = analyze( page.content );
var last = found.length ? found[ found.length - 1 ] : null;
var suggested = {};
TYPES.forEach( function ( t ) { suggested[ t.key ] = suggestFor( t, found ); } );
var info;
if ( last ) {
var lt = TYPEMAP[ last.type ];
info = 'Last warning found: ' + lt.label +
( lt.levels ? ' (level ' + ( last.level || '1' ) + ')' : ' (standard notice)' ) +
'. With the level set to automatic, the next level of the same type will be chosen.';
} else {
info = 'No earlier warnings were found on this talk page.';
}
var ns = mw.config.get( 'wgNamespaceNumber' );
var articleDefault = ( ns === 0 ) ? mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ) : '';
function resolveLevel( typeKey, choice ) {
var t = TYPEMAP[ typeKey ];
if ( !t || !t.levels ) { return ''; }
if ( choice === 'auto' ) { return suggested[ typeKey ]; }
return clampLevel( t, choice );
}
BM.core.dialog( {
title: 'Warn user: ' + user,
help: 'The warning will be added under this month\'s section. A preview is always shown before sending.',
info: info,
fields: [
{ name: 'type', label: 'Type of warning', type: 'select', required: true,
options: TYPES.map( function ( t ) { return [ t.key, t.label ]; } ),
value: last ? last.type : 'vandalism' },
{ name: 'level', label: 'Level', type: 'select', required: true,
options: [ [ 'auto', 'Automatic (based on previous warnings)' ],
[ '1', 'Level 1' ], [ '2', 'Level 2' ], [ '3', 'Level 3' ],
[ '4', 'Level 4' ], [ '4im', 'Level 4im' ] ],
value: cfg.warnLevel || 'auto' },
{ name: 'article', label: 'Related page (optional)', type: 'text', value: articleDefault },
{ name: 'comment', label: 'Your comment (optional)', type: 'textarea', rows: 3 }
],
preview: function ( v ) {
var t = TYPEMAP[ v.type ];
return buildBlock( t, resolveLevel( v.type, v.level ), v.article, v.comment );
},
previewTitle: function () { return talk; },
submitLabel: 'Send warning',
success: 'Warning added.',
onSubmit: function ( v ) {
var t = TYPEMAP[ v.type ];
var lvl = resolveLevel( v.type, v.level );
var block = buildBlock( t, lvl, v.article, v.comment );
return BM.core.appendUnderMonth( talk, block,
'Warning issued (' + t.label + ( lvl ? ', level ' + lvl : '' ) + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' );
}
} );
/* The level list must only offer the chosen type's levels */
var $type = $( 'select[data-bm-field="type"]' );
var $level = $( 'select[data-bm-field="level"]' );
function syncLevels() {
var t = TYPEMAP[ $type.val() ];
if ( !t ) { return; }
var cur = $level.val();
$level.empty();
if ( !t.levels ) {
$level.append( $( '<option>' ).attr( 'value', 'single' ).text( 'Standard notice' ) );
$level.val( 'single' );
return;
}
$level.append( $( '<option>' ).attr( 'value', 'auto' )
.text( 'Automatic (based on previous warnings)' ) );
t.levels.forEach( function ( l ) {
$level.append( $( '<option>' ).attr( 'value', l ).text( 'Level ' + l ) );
} );
var want = ( cur && cur !== 'single' ) ? cur : ( cfg.warnLevel || 'auto' );
if ( want !== 'auto' ) { want = clampLevel( t, want ); }
$level.val( want );
}
$type.on( 'change', syncLevels );
syncLevels();
} ).catch( function () {
mw.notify( 'The talk page could not be read.', { type: 'error' } );
} );
}
} );
}() );
/* </nowiki> */