Jump to content

User:Amire80/common.js

From translatewiki.net

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.
$( function () {
	"use strict";

	var wgCanonicalSpecialPageName = mw.config.get( 'wgCanonicalSpecialPageName' ),
		tokenParams = {
			action: 'query',
			meta: 'tokens',
			type: 'csrf',
			format: 'json'
		};

	function searchTranslationsMakeEditableLinks() {
		var $trigger = $( '<a>' ).text( 'Make editable links' ),
			$results = $( '.results' ),
			$messages = $results.find( '.tux-message' );

		$trigger.click( function () {
			$messages.each( function () {
				var $editLink = $( this ).find( '.tux-edit' ),
					$newEditLink = $editLink.clone(),
					$newEditA = $newEditLink.find( 'a' ),
					newUri = new mw.Uri( $newEditA.attr( 'href' ) );

				newUri.query.language = 'he';
				$newEditA.attr( 'href', newUri.toString() );
				$editLink.after( $newEditLink );
			} );
		} );

		$results.before( $trigger );
	}

	function languageStatsHideNonMediaWiki() {
		var $hideTrigger,
			mwGroupsRe = /(^core$)|(^pagecontentservice$)|(^mediawiki-)|(^ext-)|(^mwgerrit-)|(^mwgitlab-)|(^mwgithub-)|(^mwgithubskin-)|(^mwbitbucketskin-)|(^jquery-uls)/,
			$statsTable = $( 'table.statstable' );

		if ( $statsTable.length === 0 ) {
			return;
		}

		$statsTable.attr( 'id', 'theTable' );

		$hideTrigger = $( '<a>' )
			.text( 'Hide non-MediaWiki' )
			.attr( 'href', '#theTable' )
			.on( 'click', function () {
				// Expand all
				$( 'p.groupexpander-all a' ).click();

				// Hide the non-MediaWiki things
				$statsTable.find( 'tbody tr' ).each( function () {
					var $row = $( this );

					if ( !( $row.hasClass( 'FileBasedMessageGroup' ) || $row.hasClass( 'MediaWikiExtensionMessageGroup' ) ) ||
						$row.data( 'groupid' ).match( mwGroupsRe ) === null
					) {
						$row.hide();
					}
				} );

				// Sort the table by "Untranslated" column
				$( $( '#theTable thead tr th' ).get( 2 ) ).click();
			} );

		$statsTable.before( $hideTrigger );
	}

	function translationsDeleteDuplicates() {
		var deleteHandler,
			$deleteButton = $(),
			englishText = '',
			languageCode = '',
			language = {},
			title = '',
			$languageLinks = $(),
			$historyLink = $(),
			$rows = $( 'table.mw-sp-translate-table tbody' ).find( 'tr' );

		const titleRe = /(^.+:.+):/;

		deleteHandler = function ( e ) {
			var api = new mw.Api(),
				$a = $( this ),
				title = $a.attr( 'title' );

			api.get( tokenParams ).done( function ( data ) {
				var deleteParams = {
					action: "delete",
					format: "json",
					title: title,
					watchlist: "watch",
					reason: "identical to English source",
					token: data.query.tokens.csrftoken
				};

				api.post( deleteParams )
					.done( function ( data ) {
						console.log( 'deleted ' + title );
						$a.text( 'deleted ' + title );
					} )
					.fail( function ( data ) {
						console.log( 'failed to delete ' + title );
						$a.text( 'failed to delete ' + title );
					} );
			} );
		};

		$rows.each( function ( index, element ) {
			var $languageCell = $(),
				$contentCell = $(),
				languageMatchResult = '',
				$cells = $( this ).find( 'td' );

			const languageCodeRe = /\(([^(]+)\)$/;

			if ( $cells.length !== 2 ) {
				return;
			}

			$languageCell = $( $cells.get( 0 ) );
			$contentCell = $( $cells.get( 1 ) );

			languageMatchResult = $languageCell.text().match( languageCodeRe );
			if ( languageMatchResult === null
				|| languageMatchResult.length !== 2
			) {
				return;
			}

			languageCode = languageMatchResult[1];
			language[languageCode] = {
				language: $languageCell,
				content: $contentCell
			};
		} );

		englishText = language.en.content.text();

		for ( languageCode in language ) {
			if ( [ 'en', 'qqq' ].includes( languageCode ) ) {
				continue;
			}

			if ( language[languageCode].content.text() === englishText ) {
				$languageLinks = language[languageCode].language.find( 'a' );
				$historyLink = $( $languageLinks.get( 0 ) );

				title = $historyLink.attr( 'title' ).match( titleRe )[1];

				$deleteButton = $( '<a>' )
					.attr( {
						title: title
					} )
					.text( 'delete ' + title )
					.click( deleteHandler );

				$historyLink.before( $deleteButton );
			}
		}
	}

	function migrateLQT() {
		// WIP
		// LiquidThreads migration script.
		// Based on [[FAQ#Talk_page_discussion_systems]]
		var $trigger = $( '<a>' ).text( 'Migrate LiquidThreads' ),
			$options = $( '.lqt-talkpage-header' ).find( '.lqt_view_options' ),
			summary = 'Archiving LiquidThreads';

		$options.after( '<strong>' ).after( $trigger );

		$trigger.on( 'click', function () {
			$.when( mw.loader.using( 'mediawiki.api' ), $.ready ).then( function () {
				var triggerText,
					fromTitle = mw.config.get( 'wgPageName' ),
					toTitle = fromTitle + '/LiquidThreads',
					api = new mw.Api();

					api.get( tokenParams ).done( function ( data ) {
						var moveParams = {
							action: 'move',
							from: fromTitle,
							to: toTitle,
							reason: summary,
							noredirect: '0',
							token: data.query.tokens.csrftoken
						};

						triggerText = $trigger.text();
						api.post( moveParams )
							.done( function ( data ) { // Original move succeeded
								$trigger.text( triggerText + ' ... Moved from ' + fromTitle + ' to ' + toTitle );

								setTimeout( function() { // Waiting for LQT weirdness to happen in the background after original move
									api.get( tokenParams ).done( function ( data ) {
										var editParams = {
											action: 'edit',
											title: fromTitle,
											text: '{{HadLiquidThreads}}',
											summary: summary,
											createonly: 1,
											token: data.query.tokens.csrftoken
										};

										triggerText = $trigger.text();
										api.post( editParams ) // Try to create non-LQT
											.done( function ( data ) { // Creating non-LQT succeeded
												$trigger.text( triggerText + ' ... Edited ' + fromTitle );

												setTimeout( function() { // Waiting for LQT weirdness after creating non-LQT
													api.get( tokenParams ).done( function ( data ) {
														var prependParams = {
															action: 'edit',
															title: toTitle,
															prependtext: "{{ArchivedLiquidThreads}}\n\n",
															summary: summary,
															token: data.query.tokens.csrftoken
														};

														api.post( prependParams ) // Try to prepend LQT archive
															.done( function ( data ) { // Prepending LQT archive succeeded
																var purgeParams = {
																	action: 'purge',
																	titles: [ fromTitle, toTitle ],
																};

																$trigger.text( triggerText + ' ... Edited ' + toTitle + '.' );

																triggerText = $trigger.text();

																api.post( purgeParams )
																	.done( function ( data ) { // Purging succeeded
																		$trigger.text( triggerText + ' That is it, purged the pages. It probably worked! Refresh me!' );
																	} )
																	.fail( function ( data ) { // Purging failed
																		$trigger.text( triggerText + ' Something was wrong with purging the pages, but it probably worked anyway. Refresh me!' );
																	} );
															} )
															.fail( function ( data ) { // Prepending LQT archive failed
																$trigger.text( triggerText + ' ... Failed to edit ' + toTitle );
															} ); // End prepending
													} ); // End token for prepending
												}, 1000 ); // End waiting for LQT weirdness after creating non-LQT
											} )
											.fail( function ( data ) { // Creating non-LQT failed
												$trigger.text( triggerText + ' ... Failed to edit ' + fromTitle );
											} ); // End creating non-LQT
									} );
								}, 1000 );
							} )
							.fail( function ( data ) { // Original move failed
								$trigger.text( triggerText + ' ... Failed to move ' + fromTitle + ' to ' + toTitle );
							} );
						} );
				} );
		} );
	}

	if ( $( '.lqt-talkpage-header' ).length ) {
		migrateLQT();
	}

	if ( wgCanonicalSpecialPageName === 'SearchTranslations' ) {
		searchTranslationsMakeEditableLinks();
	}

	if ( wgCanonicalSpecialPageName === 'LanguageStats' ) {
		languageStatsHideNonMediaWiki();
	}

	if ( wgCanonicalSpecialPageName === 'Translations' ) {
		translationsDeleteDuplicates();
	}
} );