Wikipedia:专题/提升为命名空间/转移脚本.js

var page_data={
	//"Wikipedia:ACG专题": "专题:ACG",
	"User_talk:A2569875/API測試":"U:A2569875/測試API",
	"User_talk:A2569875/API測試2":"U:A2569875/測試2API",
};//屆時會替換為專題頁面表

//設定值
var interval=1500;
var change_redirect_reason="Javascript呼叫API:跨名字空間移動測試之重定向調整測試(第四次;含重定向控制)";
var move_page_reason="Javascript呼叫API:跨名字空間移動測試之移動測試(第四次;含重定向控制)";

//函數
function move_all_pages(page_data){
	var api = new mw.Api(),final_page={};
	var check_pages=[],page_list=[];
	Object.keys(page_data).map(function(objectKey, index) {
		if(typeof(page_data[objectKey])==typeof(''))check_pages.push(objectKey);
	});
	var change_redirect=function(page,target){
		console.log(`修改重新導向${page}目標頁到${target}`);
		api.get({
			action: 'parse',
			page: page,
			prop: 'wikitext',
			formatversion: 2,
			format: 'json'
		}).done( ((edit_page,edit_target) => (data => {
			api.postWithToken( 'csrf', {
				action: 'edit',
				title: edit_page,
				summary: `修改重新導向[[${edit_page}]]目標頁到[[${edit_target}]]:${change_redirect_reason}`,
				text: data.parse.wikitext.replace(/^(#[^\[\]]+\s*)\[\[([^\[\]]+)\]\]/,'$1[['+edit_target+']]'),
				format: 'json'
			} ).done( function ( data ) {

			} );
		}))(page,target) ) ;
	};
	
	var check_link_count=function(check_page, true_callback){
		api.get( {
			action: 'query',
			format: 'json',
			list: 'backlinks',
			redirects:true,
			converttitles:false,
			bllimit: 500,
			blfilterredir:'nonredirects',
			bltitle: check_page
		} ).done( ((callback_func)=>((link_count_data)=>{
			callback_func(parseInt((link_count_data['continue']||{blcontinue:`1|${link_count_data.query.backlinks.length}`}).blcontinue.split('|')[1])<100);
			console.log(link_count_data);
		}))(true_callback));
	};
	
	var move_page=function(page,target){
		console.log(`移動頁面${page}${page_data[page]}`);
		check_link_count(page,((move_page,move_target)=>( (in_noredirect)=>{
			window.setTimeout( ((edit_page,edit_target,noredirect) => ( ()=>{
				var param={
					action: 'move',
					from: page,
					to: edit_target,
					reason: `移動頁面[[${edit_page}]]到[[${page_data[edit_page]}]]:${move_page_reason}`,
					movetalk: '1',
					format: 'json'
				};
				//if(noredirect)param.noredirect='1';
				api.postWithToken('csrf',param).done( function ( data ) {

				});
			}))(move_page,move_target,in_noredirect), 200);
		} ))(page,target));
	};
	var max_count=check_pages.length;
	var get_all_subpage=function(get_all_subpages,complete){
		if(check_pages.length<=0){
			complete();
		}else{
			var pr_d=1-check_pages.length/max_count;
			var find_title=check_pages.pop();
			console.log(`searching from ${find_title}...(${Math.floor(pr_d*10000)/100}%)`);
			move_page(find_title,page_data[find_title]);
			api.get( {
				action: 'query',
				format: 'json',
				list: 'backlinks',
				blnamespace: 4,
				redirects:true,
				converttitles:false,
				bllimit: 500,
				blfilterredir:'redirects',
				bltitle: find_title
				
			} ).done( ((org_title) => (function ( data ) {
				var pages = data.query.backlinks;
				console.log(`頁面共有${pages.length}個重新導向...`);
				for (var page in pages ) {
					var pjName=pages[page].title;
					change_redirect(pjName,page_data[org_title]);
				}
				window.setTimeout(( () => get_all_subpages(get_all_subpages,complete) ), interval);
			}))(find_title) );
		}
	};
	get_all_subpage(get_all_subpage,function(){
		console.log(`完成。`);
	});
}
var _addText="{{Historical}}{{see also|Wikipedia:专题/提升为命名空间/转移表}}";