Warn: add 35 warning types, including paid editing and disruptive editing
No edit summary
Line 131: Line 131:
} );
} );
},
},
/* Send one warning with no dialog: the type is given, the level is
/* The levels a warning type offers: an array of level strings, or
  chosen automatically. Resolves with the plan that was sent. */
  null for a single standard notice. Used by the revert module to
sendAuto: function ( user, typeKey, article ) {
  fill its warning level list. */
levelsFor: function ( typeKey ) {
var t = TYPEMAP[ typeKey ];
return t ? t.levels : null;
},
/* Send one warning with no dialog: the type is given, and the level
  is chosen automatically unless a level is passed. Resolves with
  the plan that was sent. */
sendAuto: function ( user, typeKey, article, levelChoice ) {
var t = TYPEMAP[ typeKey ] || TYPEMAP.vandalism;
var t = TYPEMAP[ typeKey ] || TYPEMAP.vandalism;
var talk = 'User talk:' + user;
var talk = 'User talk:' + user;
return BM.core.getPage( talk ).then( function ( page ) {
return BM.core.getPage( talk ).then( function ( page ) {
var plan = planFor( t, analyze( page.content ), article );
var plan;
if ( levelChoice && levelChoice !== 'auto' && t.levels ) {
var lvl = clampLevel( t, levelChoice );
plan = { type: t.key, label: t.label, level: lvl, block: buildBlock( t, lvl, article, '' ) };
} else {
plan = planFor( t, analyze( page.content ), article );
}
return BM.core.appendUnderMonth( talk, plan.block,
return BM.core.appendUnderMonth( talk, plan.block,
'Warning issued (' + plan.label + ( plan.level ? ', level ' + plan.level : '' ) + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' )
'Warning issued (' + plan.label + ( plan.level ? ', level ' + plan.level : '' ) + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' )

Revision as of 23:50, 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 },
		{ key: 'paid', label: 'Paid editing', base: 'uw-paid', levels: [ '1', '2', '3', '4' ] },
		{ key: 'disruptive', label: 'Disruptive editing', base: 'uw-disruptive', levels: [ '1', '2', '3', '4' ] },
		{ key: 'spam', label: 'Spam', base: 'uw-spam', levels: [ '1', '2', '3', '4', '4im' ] },
		{ key: 'delete', label: 'Removing content', base: 'uw-delete', levels: [ '1', '2', '3', '4', '4im' ] },
		{ key: 'generic', label: 'Generic disruptive', base: 'uw-generic', levels: [ '1', '2', '3', '4' ] },
		{ key: 'error', label: 'Factual errors', base: 'uw-error', levels: [ '1', '2', '3', '4' ] },
		{ key: 'subtle', label: 'Subtle vandalism', base: 'uw-subtle', levels: [ '1', '2', '3', '4' ] },
		{ key: 'attempt', label: 'Attempted disruption', base: 'uw-attempt', levels: [ '1', '2', '3', '4' ] },
		{ key: 'joke', label: 'Joke edits', base: 'uw-joke', levels: [ '1', '2', '3', '4' ] },
		{ key: 'create', label: 'Inappropriate pages', base: 'uw-create', levels: [ '1', '2', '3', '4' ] },
		{ key: 'speedy', label: 'Speedy deletion misuse', base: 'uw-speedy', levels: [ '1', '2', '3', '4' ] },
		{ key: 'afd', label: 'Deletion discussion misuse', base: 'uw-afd', levels: [ '1', '2', '3', '4' ] },
		{ key: 'tdel', label: 'Removing deletion tags', base: 'uw-tdel', levels: [ '1', '2', '3', '4' ] },
		{ key: 'blpprod', label: 'Removing BLP prod tags', base: 'uw-blpprod', levels: [ '1', '2', '3', '4' ] },
		{ key: 'tpv', label: 'Tampering with posts', base: 'uw-tpv', levels: [ '1', '2', '3', '4' ] },
		{ key: 'genre', label: 'Genre changes', base: 'uw-genre', levels: [ '1', '2', '3', '4' ] },
		{ key: 'idt', label: 'Identity fields', base: 'uw-idt', levels: [ '1', '2', '3', '4' ] },
		{ key: 'image', label: 'Image policy', base: 'uw-image', levels: [ '1', '2', '3', '4' ] },
		{ key: 'upload', label: 'Problem uploads', base: 'uw-upload', levels: [ '1', '2', '3', '4' ] },
		{ key: 'mos', label: 'Manual of Style', base: 'uw-mos', levels: [ '1', '2', '3', '4' ] },
		{ key: 'move', label: 'Page moves', base: 'uw-move', levels: [ '1', '2', '3', '4' ] },
		{ key: 'nor', label: 'Original research', base: 'uw-nor', levels: [ '1', '2', '3', '4' ] },
		{ key: 'notcensored', label: 'Removing content as offensive', base: 'uw-notcensored', levels: [ '1', '2', '3', '4' ] },
		{ key: 'own', label: 'Article ownership', base: 'uw-own', levels: [ '1', '2', '3', '4' ] },
		{ key: 'biog', label: 'Biography violations', base: 'uw-biog', levels: [ '1', '2', '3', '4' ] },
		{ key: 'defamatory', label: 'Defamatory content', base: 'uw-defamatory', levels: [ '1', '2', '3', '4' ] },
		{ key: 'agf', label: 'Failure to assume good faith', base: 'uw-agf', levels: [ '1', '2', '3', '4' ] },
		{ key: 'chat', label: 'Off-topic talk', base: 'uw-chat', levels: [ '1', '2', '3', '4' ] },
		{ key: 'tempabuse', label: 'Template abuse', base: 'uw-tempabuse', levels: [ '1', '2', '3', '4' ] },
		{ key: 'coi', label: 'Conflict of interest', base: 'uw-coi', levels: [ '1', '2', '3' ] },
		{ key: 'editwarring', label: 'Edit warring (3RR)', base: 'uw-3rr', levels: null },
		{ key: 'editwar', label: 'Edit warring', base: 'uw-ew', levels: null },
		{ key: 'hoax', label: 'Hoax', base: 'uw-hoax', levels: null },
		{ key: 'legal', label: 'Legal threats', base: 'uw-legal', levels: null },
		{ key: 'attack', label: 'Attack warning', base: 'uw-attack', 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;
			} );
		},
		/* The levels a warning type offers: an array of level strings, or
		   null for a single standard notice. Used by the revert module to
		   fill its warning level list. */
		levelsFor: function ( typeKey ) {
			var t = TYPEMAP[ typeKey ];
			return t ? t.levels : null;
		},
		/* Send one warning with no dialog: the type is given, and the level
		   is chosen automatically unless a level is passed. Resolves with
		   the plan that was sent. */
		sendAuto: function ( user, typeKey, article, levelChoice ) {
			var t = TYPEMAP[ typeKey ] || TYPEMAP.vandalism;
			var talk = 'User talk:' + user;
			return BM.core.getPage( talk ).then( function ( page ) {
				var plan;
				if ( levelChoice && levelChoice !== 'auto' && t.levels ) {
					var lvl = clampLevel( t, levelChoice );
					plan = { type: t.key, label: t.label, level: lvl, block: buildBlock( t, lvl, article, '' ) };
				} else {
					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> */