MediaWiki:Gadget-quickdel.js

From Celeste Wiki
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// * @name		Quickdel.js
// * @dependencies	jquery.dialog, mediawiki
// * @source
// * @revision		2.1.0
// * @revision date	Jan 19, 2021
// * @author:		Celeste (https://celeste.ink/wiki/User:Celeste)
// * @license		LGPL V3 OR LATER
// * @comment		Allow quick adding of pages to [[CW:QDEL]]
// * @comment		Original by [[User:Spang]] and [[User:Lyrithya]] (FU SPANG!)

//<nowiki>
;( function( $ ) {
	var boxId = '#qdel-box',
		comment = '',
		pageToqdel,
		pageName = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ),
		hasHistory = !!$( '#ca-history' ).length,
		isRedirect = !!$( '#contentSub' ).text().match( /^redirect page$/i ),
		isRcOrNew = !!pageName.match( /Special:(NewPages|RecentChanges)/i ),
		scriptPath = mw.config.get( 'wgScriptPath' ) + '/api.php',
		// fix for IE instances with no open console
		console = window.console || { log: function() { } },
		savePage = 'Celeste Wiki:Quickdel';

	// $( document ).ready() equivalent
	// This can be done before the actual function declaration because of "hoisting"
	$( init );

	function init() {
		if ( hasHistory ) {
			addControl();
		} else if ( isRcOrNew ) {
			addLinks();
		}

		// add a handler to make the input-box execute on ENTER press
		$( document ).on( 'keydown', '#qdel-comment', function( e ) {
			if ( e.which == 13 ) {
				$( boxId ).trigger( 'qdel:ok' );
			}
		} );
	}

	function addControl() {
		$( mw.util.addPortletLink( 'p-cactions', '#', 'QDEL', 't-qdel', 'Add page to Quickdel' ) )
			.click( function( e ) { run( e, pageName ); } );
	}

	function addLinks() {
		$( '#mw-content-text li, #mw-content-text table' ).each( function() {
			var $t = $( this ),
			title = $t.find( 'a.mw-changeslist-title, a.mw-newpages-pagename' ).attr( 'title' ),
			link = $( '<a/>', {
				href: 'javascript:void(0)',
				text: 'qdel',
				click: function( e ) { run( e, title ); }
			} );
			if ( pageName.match( /Special:NewPages/i ) ) {
				$t.find( '.mw-newpages-history' ).after( ')' ).after( link ).after( ' (' );
			} else {
				$t.find( 'a[href*="action=history"]' ).after( link ).after( ' | ' );
			}
		} );
	}

	function run( e, title ) {
		pageToqdel = title;
		e.preventDefault();
		$( '#qdel-box' ).dialog( 'close' );
		$( '<div id="qdel-box"></div>' )
			.append( '<p>Add comment:</p><input type="text" id="qdel-comment" placeholder="optional"/>' )
			.dialog( {
				buttons: [
					{ text: 'Go', click: function() { $( this ).trigger( 'qdel:ok' ); } },
					{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }
				],
				closeOnEscape: false,
				modal: true,
				close: function() {
					$( this ).dialog( 'destroy' ).remove();
				},
				title: 'Quick-deletion request (QDEL)'
			} )
			.bind( 'qdel:ok', fetch );
	}

	function fetch() {
		comment = $( '#qdel-comment' ).val();
		$( boxId ).empty()
			.css( 'font-size', '80%' )
			.dialog( 'option', 'title', 'QDELing, please wait...' )
			.dialog( 'option', 'buttons', [] );
		output( 'info', 'Loading', 'QDEL page...' );
		$.get( scriptPath, {
			'action': 'query',
			'titles': savePage,
			'prop': 'revisions|info|links',
			'pllimit': 500,
			'rvprop': 'content',
			'rvlimit': 1,
			'rvsection': 1,
			'intoken': 'edit',
			'format': 'xml'
		} )
		.done( fetchDone )
		.fail( fetchFail );
	}

	function isListed( $xml, title ) {
		var listed = false;
		$xml.find( 'pl' ).each( function() {
			if ( $( this ).attr( 'title' ) == title ) {
				listed = true;
			}
		} );
		return listed;
	}

	function fetchDone( xml ) {
		var $xml = $( xml ),
			sectionText,
			line = ' ' + ( isRedirect ? '{{redirect|' + pageToqdel + '}}' : '[[:' + pageToqdel + ']]' )
				+ ( comment ? ' &#8212; ' + comment : '' );
		output( 'info', 'Checking', 'QDEL for selected page...' );
		if ( isListed( $xml, pageToqdel ) ) {
			output( 'fail', 'Page', 'already on QDEL!' );
			addBackButtons();
			return;
		}
		output( 'info', 'Page', 'not listed, adding it now...' );
		sectionText = $xml.find( 'rev:first' ).text().replace( /==(\n|$)/, '==\n' + line + '\n' );
		$.post( scriptPath, {
			'action': 'edit',
			'title': savePage,
			'section': 1,
			'token': $xml.find( 'page' ).attr( 'edittoken' ),
			'summary': '+[[' + pageToqdel + ']] - via [[MediaWiki:Gadget-quickdel.js|QDEL gadget]]',
			'text': sectionText,
			'watchlist': 'nochange',
			'format': 'xml'
		} )
			.done( editDone )
			.fail( editFail );
	}

	function fetchFail( error ) {
		output( 'fail', 'Couldn\'t', 'connect to QDEL!' );
		addBackButtons();
		console.log( error );
	}

	function editDone( xml ) {
		var $xml = $( xml );
		if ( $xml.find( 'edit[result="Success"]' ).length ) {
			output( 'win', 'Saved!' );
		} else {
			var $error = $xml.find( 'error' );
			output( 'fail', 'Saving failed', 
				'Error: ' + $error.attr( 'code' ) + ' - ' + $error.attr( 'info' ) );
		}
		addBackButtons();
	}

	function editFail() {
		output( 'fail', 'Connection failed' );
		addBackButtons();
	}

	function addBackButtons() {
		$( boxId ).dialog( 'option', { buttons: [
			{
				text: 'Go to QDEL',
				click: function() {
					$( boxId ).dialog( 'close' );
					document.location.href = mw.util.wikiUrlencode( savePage );
				}
			},
			{ text: 'Close', click: function() { $( boxId ).dialog( 'close' ); } }
		] } );
	}

	function output( type, label, text ) {
		var indicator = type == 'fail' ? '&#160;x ' : type == 'win' ? '&#160;√ ' : '&#160;► ',
			str = '<p>' + indicator + '<b>' + ( label || '' ) + '</b> ' + ( text || '' ) + '</p>';
		$( boxId ).append( str )
			.css( 'color', ( type == 'fail' ? 'red' : type == 'win' ? 'green' : 'black' ) );
	}
})( jQuery );