跳转到内容

User:Lt2818/massmove.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
	This is userscirpt is usefull for Mass pages Move.

	@Author [[User:Jayprakash12345]]
	@Author from [[User:Legoktm/massrename.js]]
	@OwnBy [[meta:Indic-TechCom]]
*/

function getLink(text, wikiPage, params) {
	wikiPage = wikiPage || text;
	var link = document.createElement('a');
	link.setAttribute('href', mw.util.getUrl(wikiPage, params));
	link.setAttribute('title', wikiPage);
	link.setAttribute('target', '_blank');
	link.textContent = text;
	return $(link).prop('outerHTML');
}

$( document ).ready( function() {
	var isSysop = ($.inArray( 'sysop', mw.config.get('wgUserGroups') ) !== -1);

	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'
		} ),
		notleaveRedirect = '',
		moveStart = new OO.ui.ButtonWidget( {
			label: 'Start Moving',
			icon: 'alert',
			flags: [ 'primary', 'progressive' ]
		} ),
		cancelBtn = new OO.ui.ButtonWidget( {
			label: 'Cancel',
			flags: [ 'primary', 'destructive' ],
			title: '取消尚未开始的任务'
		} ),
		moveLogHeading = $("<div>").hide();

		label1 = $('<p>').text('List of Pages:').css('font-weight','bold' );

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

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

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

		if ( isSysop ) {
			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,
			notleaveRedirect.$element,
			'<br/>',
			moveStart.$element,
			cancelBtn.$element,
			'<br/>',
			moveLogHeading
		);

		var stop = true;

		// Thanks [[User:Legoktm]], Stolen from [[User:Legoktm/massrename.js]]
		function rename_file( old, newname, reason, noRedirect, orderedList) {
			if (stop)
				return;
			var delay = 15;

			function ROUNDROBIN_step3() {
				( new mw.Api() ).postWithToken ( 'csrf', {
					action: 'move',
					from: "Draft:"+old,
					to: old,
					reason: "[[:en:WP:ROUNDROBIN]]",
					movetalk: 1,
					noredirect: true
				}, {
					async: false // Don't run parallel requests, be nice to sever kittens!
				})
				.done( function ( data ) {
					orderedList.append( "<li><b>" + old + "</b> moved to <b>" + newname + "</b> ROUNDROBIN. 请修复" + getLink(old) + "重定向目标,并检查" + getLink("Special:WhatLinksHere/Talk:" + old) + "</li>" );
				} )
				.fail( function ( data ) {
					if( data == "ratelimited" ) {
						orderedList.append( "<li>ROUNDROBIN_step3: <b>" + old + "</b> ratelimited. " + delay + "秒后将重试。</li>" );
						window.setTimeout(ROUNDROBIN_step3, 1000 * delay);
					} else {
						orderedList.append( "<li>ROUNDROBIN_step3: <b>" + old + "</b> " + data + ". 未知错误,请手工修复。</li>" );
					}
				});
			}

			function ROUNDROBIN_step2() {
				( new mw.Api() ).postWithToken ( 'csrf', {
					action: 'move',
					from: old,
					to: newname,
					reason: reason,
					movetalk: 1,
					noredirect: true
				}, {
					async: false // Don't run parallel requests, be nice to sever kittens!
				})
				.done(ROUNDROBIN_step3)
				.fail( function ( data ) {
					if( data == "ratelimited" ) {
						orderedList.append( "<li>ROUNDROBIN_step2: <b>" + old + "</b> ratelimited. " + delay + "秒后将重试。</li>" );
						window.setTimeout(ROUNDROBIN_step2, 1000 * delay);
					} else {
						orderedList.append( "<li>ROUNDROBIN_step2: <b>" + old + "</b> " + data + ". 未知错误,请手工修复。</li>" );
					}
				});
			}

			new mw.Api().get({
				action: 'query',
				format: 'json',
				titles: old,
				redirects: null,
				formatversion: 2
			}).done( function ( data ) {
				if(data.query.redirects){
					orderedList.append( "<li><b>" + getLink(old) + "</b>为重定向,未移动。</li>" );
					return;
				}
				( 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!
				})
				.always( function ( data ) {
					if( data.move ) {
						orderedList.append( "<li><b>" + data.move.from + "</b> moved to <b>" + data.move.to + "</b>.</li>" );
					} else if( data == "articleexists" ) {
						orderedList.append( "<li><b>" + getLink(newname) + "</b> articleexists. </li>" );
						if (isSysop)
							orderedList.append( "<li>作为管理员,您可点击" + getLink("此处", "Special:MovePage", {wpOldTitle: old, wpNewTitle: newname, wpReason: reason, wpMovetalk: 1}) + "删除页面再移动(请勾选“是的,刪除該頁面”)。</li>" );
						else if (mw.config.get("wgUserName") === "Lt2818")
							( new mw.Api() ).postWithToken ( 'csrf', {
								action: 'move',
								from: newname,
								to: "Draft:"+old,
								reason: "[[:en:WP:ROUNDROBIN]]",
								movetalk: 1,
								noredirect: true
							}, {
								async: false // Don't run parallel requests, be nice to sever kittens!
							})
							.done(ROUNDROBIN_step2)
							.fail( function ( data ) {
								orderedList.append( "<li><b>" + old + "</b> ROUNDROBIN失败,ROUNDROBIN未开始。错误信息:" + data + "</li>" );
							});
					} else if( data == "ratelimited" ) {
						orderedList.append( "<li><b>" + old + "</b> ratelimited. " + delay + "秒后将重试。</li>" );
						window.setTimeout(rename_file, 1000 * delay, old, newname, reason, noRedirect, orderedList);
					} else {
						orderedList.append( "<li><b>" + old + "</b> 移动出错:" + data + ".</li>" );
					}
				} );
			}).fail( function ( data ) {
				orderedList.append( "<li>查询<b>" + old + "</b>是否为重定向时失败:" + data + "</li>" );
			});
		}

		function escapeRegExp(str) {
			// From MDN
			return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
		}

		function missingAlertMsg ( str ) {
			return alert( "Did not find " + str + " :(" );
		}

		moveStart.on( 'click', function() {
			var 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;

			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;
			}

			// Check whether the Regex is correct or not
			try {
				find = new RegExp( find, 'gi' );
			} catch (e) {
				alert( "Regex Error: " + e );
				return;
			}

			if ( replace === "" ) {
				missingAlertMsg( "replace string" );
				return;
			}

			if ( $.inArray( 'sysop', mw.config.get('wgUserGroups') ) !== -1 ) {
				noRedirect = notleaveRedirectInside.isSelected();
			}

			stop = false;

			var ratelimitPerMinute = 8;
			var interval = pagesList.length > ratelimitPerMinute ? 60000 / (ratelimitPerMinute - 1) : 2000;
			var start = 0;
			pagesList.forEach( function(page){
			
				// Lengthy name to aviod override in the global variable
				var pageToMoveByMassMover = page.trim();

				newPagename = pageToMoveByMassMover.replace( find, replace );
				var orderedList = $("<ul>").appendTo( moveLogHeading );
				window.setTimeout(rename_file, start, pageToMoveByMassMover, newPagename, reason, noRedirect, orderedList);
				start += interval;
			});
		});
		cancelBtn.on( 'click', function() {
			stop = true;
		});
	}

	// 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();
		});
	}
});