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 buildBanner() {
		return '{{subst:afd}}';
	}
	function discussionTitle( title ) {
		return AFD + '/' + title;
	}
	function logTitle() {
		var d = new Date();
		return AFD + '/Log/' + d.getFullYear() + ' ' + MONTHS[ d.getMonth() ] + ' ' + d.getDate();
	}
	function createDiscussion( title, reason ) {
		var disc = discussionTitle( title );
		return BM.core.getPage( disc ).then( function ( p ) {
			if ( p.exists ) {
				throw new Error( 'A deletion discussion for this page already exists.' );
			}
			var text = '{{subst:afd2|pg=' + BM.core.cleanParam( title ) +
				'|text=' + reason + ' ' + BM.SIG + '}}';
			return BM.core.savePage( disc, text,
				'Bharat Mitra: deletion discussion nomination' );
		} );
	}
	function addToLog( title ) {
		/* appendToPage creates the log page holding just this entry if it is missing */
		return BM.core.appendToPage( logTitle(), '{{subst:afd3|pg=' + BM.core.cleanParam( title ) + '}}',
			'Bharat Mitra: added a deletion nomination to the log' );
	}
	function notifyBlock( title ) {
		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 [[' + discussionTitle( title ) + ']] ' +
			'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 buildBanner(); },
					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.' );
							}
							var banner = buildBanner();
							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,
								'Bharat Mitra: nominated for deletion discussion', p.revid ).then( function () {
								return createDiscussion( title, reason ).then( function () {
									return addToLog( title ).then( function () {
										if ( !v.notify ) { return null; }
										return BM.core.firstEditor( title ).then( function ( creator ) {
											if ( !creator || creator === mw.config.get( 'wgUserName' ) ) { return null; }
											return BM.core.appendUnderMonth( 'User talk:' + creator, notifyBlock( title ),
												'Bharat Mitra: deletion discussion notice' ).catch( function () {
												throw new Error( 'The nomination was made, but the creator could not be notified.' );
											} );
										} );
									} );
								} );
							} );
						} );
					}
				} );
			} );
		}
	} );
}() );
/* </nowiki> */