MediaWiki:Gadget-QuoteSourceRedirects.js

From Bodhicitta
Revision as of 12:26, 9 September 2026 by Jeremi (talk | contribs) (Approve buttons for the QuoteSource redirect list)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

MediaWiki:Gadget-QuoteSourceRedirects.js

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.
/**
 * Approve buttons for [[QuoteSource redirects to create]].
 *
 * Each suggested row carries a .qs-approve span with data-from (the unresolved
 * QuoteSource value) and data-to (the target page). Clicking creates a redirect
 * from one to the other through the API — it does not touch any segment.
 *
 * Deliberately createonly: if a page already exists at that title the edit is
 * refused rather than overwriting it, so an approval can never clobber real
 * content. The row reports what happened either way.
 *
 * Only loaded on that page, and only for users who can edit — the buttons are
 * not rendered at all otherwise.
 */
( function () {
	'use strict';

	if ( mw.config.get( 'wgPageName' ) !== 'QuoteSource_redirects_to_create' ) {
		return;
	}

	$( function () {
		var $rows = $( '.qs-approve' );
		if ( !$rows.length ) {
			return;
		}

		var api = new mw.Api();

		// Replaces the button with a result. The button is gone by then, so the
		// row cannot be double-submitted.
		function setState( $cell, cls, text ) {
			$cell.removeClass( 'qs-pending qs-done qs-failed' ).addClass( cls ).text( text );
		}

		function approve( $cell ) {
			var from = $cell.data( 'from' );
			var to = $cell.data( 'to' );
			if ( !from || !to ) {
				return;
			}

			setState( $cell, 'qs-pending', 'creating…' );

			api.postWithToken( 'csrf', {
				action: 'edit',
				title: from,
				text: '#REDIRECT [[' + to + ']]',
				summary: 'Redirect for a QuoteSource value used in the translation memory',
				createonly: 1
			} ).done( function () {
				setState( $cell, 'qs-done', '✓ created' );
			} ).fail( function ( code, res ) {
				var why = code;
				if ( code === 'articleexists' ) {
					why = 'page already exists';
				} else if ( res && res.error && res.error.info ) {
					why = res.error.info;
				}
				setState( $cell, 'qs-failed', '✗ ' + why );
			} );
		}

		$rows.each( function () {
			var $cell = $( this );
			$( '<button>' )
				.addClass( 'qs-approve-btn' )
				.text( 'Create redirect' )
				.on( 'click', function () {
					$( this ).prop( 'disabled', true );
					approve( $cell );
				} )
				.appendTo( $cell );
		} );

		// Editing a target in place: the suggestion is a guess, and the row is
		// useless if the only way to correct it is to edit 67KB of wikitext.
		$( '.qs-table' ).on( 'click', '.qs-target-edit', function () {
			var $link = $( this );
			var $cell = $link.closest( 'td' );
			var $approve = $cell.closest( 'tr' ).find( '.qs-approve' );
			var current = $approve.data( 'to' ) || '';
			var value = window.prompt(
				'Redirect target for:\n\n' + ( $approve.data( 'from' ) || '' ),
				current
			);
			if ( value === null ) {
				return;
			}
			value = $.trim( value );
			if ( !value ) {
				return;
			}
			$approve.data( 'to', value );
			$cell.find( '.qs-target-label' ).text( value );
			// Re-arm the row: a changed target means the previous result, whether
			// it succeeded or failed, no longer describes what the button does.
			$approve.removeClass( 'qs-pending qs-done qs-failed' ).empty();
			$( '<button>' )
				.addClass( 'qs-approve-btn' )
				.text( 'Create redirect' )
				.on( 'click', function () {
					$( this ).prop( 'disabled', true );
					approve( $approve );
				} )
				.appendTo( $approve );
		} );
	} );
}() );