ব্যবহারকারী:SMA/পুনঃনামকরণ২.js

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
/*
    This is userscirpt is usefull for Mass pages Move.
	
    @Author [[User: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 ( $.inArray( 'sysop', mw.config.get('wgUserGroups') ) !== -1 ) {
            notleaveRedirect = new OO.ui.FieldLayout( 
                notleaveRedirectInside = new OO.ui.CheckboxInputWidget( { 
                    selected: false 
                    
                } ), { 
                label: 'Don\'t leave the redirect 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 sever 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><b>" + data.move.from + "</b> কে <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() + "";
            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 ( $.inArray( 'sysop', mw.config.get('wgUserGroups') ) !== -1 ) {
                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);     
                }
            });
        });
    }

    // On every page
    $.when(mw.loader.using('mediawiki.util'), $.ready).then(function () {
        mw.util.addPortletLink(
        'p-tb',
        mw.util.getUrl('Special:BlankPage/MassMover'),
        'MassMover'
        );
    });

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