Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* <nowiki> */
/* Bharat Mitra: deletion discussion module (Articles for deletion) */
( function () {
	'use strict';
	var BM = window.BharatMitra;
	var AFD = 'Bharatpedia:Articles for deletion';
	var MONTHS = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
	function ordinal( n ) {
		var s = [ 'th', 'st', 'nd', 'rd' ], v = n % 100;
		return n + ( s[ ( v - 20 ) % 10 ] || s[ v ] || s[ 0 ] );
	}
	function discussionName( title, n ) {
		return title + ( n && n > 1 ? ' (' + ordinal( n ) + ' nomination)' : '' );
	}
	function discussionTitle( title, n ) {
		return AFD + '/' + discussionName( title, n );
	}
	function buildBanner( name ) {
		/* For a renomination, the discussion page name (numbered suffix
		   included) goes into the banner, as the afd template itself directs. */
		return name ? '{{subst:afd|' + name + '}}' : '{{subst:afd}}';
	}
	function findDiscussion( title ) {
		/* First free discussion page name: the plain name, then
		   (2nd nomination), (3rd nomination), and so on. This is what lets
		   an article be nominated again after an earlier discussion closed. */
		var n = 1;
		function step() {
			var disc = discussionTitle( title, n );
			return BM.core.getPage( disc ).then( function ( p ) {
				if ( !p.exists ) {
					return { n: n, disc: disc, prev: n > 1 ? discussionTitle( title, n - 1 ) : null };
				}
				n++;
				if ( n > 20 ) { throw new Error( 'Too many previous deletion discussions for this page.' ); }
				return step();
			} );
		}
		return step();
	}
	function logTitle() {
		var d = new Date();
		return AFD + '/Log/' + d.getFullYear() + ' ' + MONTHS[ d.getMonth() ] + ' ' + d.getDate();
	}
	function createDiscussion( disc, title, reason, prev ) {
		var statement = reason;
		if ( prev ) {
			statement = 'This is a renomination. The previous discussion is at [[' + prev + ']]. ' + statement;
		}
		var text = '{{subst:afd2|pg=' + BM.core.cleanParam( title ) +
			'|text=' + statement + ' ' + BM.SIG + '}}';
		return BM.core.savePage( disc, text,
			'Deletion discussion nomination (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' );
	}
	function addToLog( name ) {
		/* appendToPage creates the log page holding just this entry if it is missing.
		   afd3 needs the discussion page name, numbered suffix included. */
		return BM.core.appendToPage( logTitle(), '{{subst:afd3|pg=' + BM.core.cleanParam( name ) + '}}',
			'Added a deletion nomination to the log (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' );
	}
	function notifyBlock( title, disc ) {
		return '== Nomination of [[:' + title + ']] for deletion ==\n' +
			'Hello. The page [[:' + title + ']] has been nominated for deletion. ' +
			'You are welcome to take part in the discussion at [[' + disc + ']] ' +
			'and to improve the page in the meantime. Please do not remove the deletion notice ' +
			'from the page while the discussion is in progress.\n' + BM.SIG;
	}
	BM.register( 'xfd', {
		open: function () {
			var title = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
			BM.core.getConfig().then( function ( cfg ) {
				BM.core.dialog( {
					title: 'Put in deletion discussion',
					help: 'Page: ' + title + '. A deletion notice will be placed on the page, a discussion page will be created under Bharatpedia:Articles for deletion, the nomination will be added to today\'s log, and the creator will be notified.',
					fields: [
						{ name: 'reason', label: 'Reason for the discussion', type: 'textarea', rows: 3, required: true },
						{ name: 'notify', label: 'Notify the creator', type: 'checkbox', value: cfg.speedyNotify !== false }
					],
					preview: function () {
						return '\'\'A deletion discussion banner will be placed at the top of this page. ' +
							'A discussion page will be created under [[Bharatpedia:Articles for deletion]] ' +
							'(with a numbered name such as "(2nd nomination)" if the page has been nominated before) ' +
							'and the nomination will be added to today\'s log.\'\'';
					},
					previewTitle: function () { return title; },
					submitLabel: 'Start the discussion',
					success: 'The page has been nominated for deletion.',
					onSubmit: function ( v ) {
						var reason = BM.core.cleanParam( v.reason );
						return BM.core.getPage( title ).then( function ( p ) {
							if ( !p.exists ) { throw new Error( 'This page does not exist.' ); }
							if ( /\{\{\s*(subst:)?(afd|Article for deletion)/i.test( p.content ) ) {
								throw new Error( 'This page already has a deletion discussion notice.' );
							}
							return findDiscussion( title ).then( function ( found ) {
								var name = discussionName( title, found.n );
								var banner = buildBanner( found.n > 1 ? name : null );
								var nl = p.content.indexOf( '\n' );
								var firstLine = nl === -1 ? p.content : p.content.slice( 0, nl );
								var newContent;
								if ( /^\s*#\s*REDIRECT/i.test( firstLine ) && nl !== -1 ) {
									newContent = firstLine + '\n' + banner + p.content.slice( nl );
								} else {
									newContent = banner + '\n' + p.content;
								}
								return BM.core.savePage( title, newContent,
									'Nominated for deletion discussion (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])', p.revid ).then( function () {
									return createDiscussion( found.disc, title, reason, found.prev ).then( function () {
										return addToLog( name ).then( function () {
											if ( !v.notify ) { return null; }
											return BM.core.notifyCreator( title, notifyBlock( title, found.disc ),
												'Deletion discussion notice (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ).then( function ( status ) {
												if ( status === 'skipped' || status === 'failed' ) {
													mw.notify( 'The page has been nominated, but the creator could not be notified (no local account for the creator).', { type: 'warn' } );
												}
												return null;
											} );
										} );
									} );
								} );
							} );
						} );
					}
				} );
			} );
		}
	} );
}() );
/* </nowiki> */