User:NguoiDungKhongDinhDanh/Script/massMover.js

From Meta, a Wikimedia project coordination wiki

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.
/*
	This is userscript is useful for Mass pages Move.
	
	@Author Jayprakash12345
	@Author from [[User:Legoktm/massrename.js]]
	@OwnBy [[meta:Indic-TechCom]]
*/

$( document ).ready( function() {
	function init() {
	$('#mw-content-text > p').remove();
	$('#firstHeading').text('MassMover');

		var listofPages = new OO.ui.MultilineTextInputWidget( {
				placeholder: 'List of Pages',
				autosize: true, 
				rows: 10
			} ),
			findInput = new OO.ui.TextInputWidget( { 
				placeholder: 'Find'
			} );
			replaceInput = new OO.ui.TextInputWidget( { 
				placeholder: 'Replace'
			} ),
			reasonInput = new OO.ui.TextInputWidget( { 
				placeholder: 'Reason'
			} ),
			treatAsRegex = new OO.ui.FieldLayout( 
				treatAsRegexInside = new OO.ui.CheckboxInputWidget( { 
					selected: false
					
				} ), { 
				label: 'Treat search string as a regular expression', 
				align: 'inline' 
			} ),
			notleaveRedirect = '',
			moveStart = new OO.ui.ButtonWidget( { 
				label: 'Start moving', 
				icon: 'alert',
				flags: [ 'primary', 'progressive' ]
			} ),
			cancelBtn = new OO.ui.ButtonWidget( {
				label: 'Cancel',
				flags: [ 'primary', 'destructive' ],
				href: 'https:' + mw.config.get( 'wgServer' )
			} ),
			moveLogHeading = $("<div>").hide();
		
		label1 = $('<p>').text('List of Pages:').css('font-weight','bold' );

		label2 = $('<p>').text('Find:').css('font-weight','bold' );

		label3 = $('<p>').text('Replace:').css('font-weight','bold' );

		label4 = $('<p>').text('Reason:').css('font-weight','bold' );

		if (/sysop|eliminator|bot|suppressredirect|filemover|extendedmover/.test(mw.config.get('wgUserGroups'))) {
			notleaveRedirect = new OO.ui.FieldLayout( 
				notleaveRedirectInside = new OO.ui.CheckboxInputWidget( { 
					selected: false
					
				} ), { 
				label: 'Don\'t leave redirects behind (Be careful)', 
				align: 'inline' 
			} );
		}
		$( '#mw-content-text' ).append( 
			label1, listofPages.$element, 
			label2, findInput.$element,
			label3, replaceInput.$element,
			label4, reasonInput.$element,
			treatAsRegex.$element,
			notleaveRedirect.$element,
			'<br/>',
			moveStart.$element,
			cancelBtn.$element,
			'<br/>',
			moveLogHeading
		);

		// Thanks [[User:Legoktm]], Stolen from [[User:Legoktm/massrename.js]]
		function rename_file( old, newname, reason, noRedirect, callback ) {
			( new mw.Api() ).postWithToken ( 'csrf', {
				action: 'move',
				from: old,
				to: newname,
				reason: reason,
				movetalk: 1,
				noredirect: noRedirect
			}, {
				async: false // Don't run parallel requests, be nice to server kittens!
			})
			.done( callback )
			.fail( callback );
		}
		
		function escapeRegExp(str) {
			// From MDN
			return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
		}
		
		function missingAlertMsg ( str ) {
			return alert( "Did not find " + str + " :(" );
		}

		function responeHanddler( data ) {
			var orderedList = $("<ul>").appendTo( moveLogHeading );
			
			/*
			// If source page is not exist
			if ( data === "missingtitle") {
				orderedList.append( "<li> Page <b>" + pageToMoveByMassMover + "</b> not found. </li>" );
			}
			
			// If old name and new name are same
			if ( data === "selfmove") {
				orderedList.append( "<li><b>" + pageToMoveByMassMover + "</b> no changes made. </li>" );
			}
			*/
			
			if( data.move ) {
				orderedList.append( "<li>Moved <b>" + data.move.from + "</b> to <b>" + data.move.to + "</b>.</li>" );
			}
		}
		
		moveStart.on( 'click', function() {
			pagesList = listofPages.getValue().replace(/^\s*[\r\n]/gm, '').split("\n");

			var find = findInput.getValue().trim();
			var replace = replaceInput.getValue().trim();
			var reason = reasonInput.getValue().trim() + " (By [[meta:Indic-TechCom/Tools|MassMover]])";
			var noRedirect = false;
			var AsRegex = treatAsRegexInside.isSelected();
			
			if( pagesList[0].trim() !== "" && find !== "" && replace !== "" ) {
				moveLogHeading.empty();
				$("<h1>").wrapInner( "<span class='mw-headline'>Move log</span>").appendTo( moveLogHeading );
				moveLogHeading.show();
			} else {
				missingAlertMsg( "any source page" );
				return;
			}
			
			if ( find === "" ) {
				missingAlertMsg( "\'find\' string" );
				return;
			}
			
			if ( replace === "" ) {
				missingAlertMsg( "replace string" );
				return;
			}
			
			if (/sysop|eliminator|bot|suppressredirect|filemover|extendedmover/.test(mw.config.get('wgUserGroups'))) {
				noRedirect = notleaveRedirectInside.isSelected();
			}
			
			pagesList.forEach( function(page){
				
				// Lengthy name to aviod override in the global variable
				window.pageToMoveByMassMover = page.trim();
				
				// Simple find/replace without Regex
				if ( !AsRegex ) {
					find = escapeRegExp(find);
					newPagename = pageToMoveByMassMover.replace( find, replace );
					rename_file( pageToMoveByMassMover, newPagename, reason, noRedirect, responeHanddler );
				}
				// Find/replace with Regex
				else {
					
					// Check whether the Regex is correct or not
					try {
						find = new RegExp( find, 'gi' );
					} catch (e) {
						alert( "Regex Error: " + e );
						
						// Hack to break the loop
						pagesList.length = 0;
						return;
					}
					newPagename = pageToMoveByMassMover.replace( find, replace );
					rename_file( pageToMoveByMassMover, newPagename, reason, noRedirect, responeHanddler);
				}
			});
		});
	}

	if ( mw.config.get('wgCanonicalSpecialPageName') === 'Blankpage' && mw.config.get('wgTitle').split('/', 2)[1] === 'MassMove' ) {
		
		$.when(mw.loader.using('oojs-ui-core'), $.ready).then(function () {
			init();
		});
	}
});