「MediaWiki:Gadget-twinkleunlink.js」:修訂間差異

出自竹園Wiki
跳至導覽 跳至搜尋
(已匯入 1 筆修訂:Twinkle)
(繁簡轉換)
 
行 20: 行 20:
}
}
if( Morebits.userIsInGroup( 'sysop' ) ) {
if( Morebits.userIsInGroup( 'sysop' ) ) {
Twinkle.addPortletLink( Twinkle.unlink.callback, "入", "tw-unlink", "取消到本链接" );
Twinkle.addPortletLink( Twinkle.unlink.callback, "入", "tw-unlink", "取消到本連結" );
}
}
};
};
行 40: 行 40:
Twinkle.unlink.callback = function(presetReason) {
Twinkle.unlink.callback = function(presetReason) {
var Window = new Morebits.simpleWindow( 600, 440 );
var Window = new Morebits.simpleWindow( 600, 440 );
Window.setTitle( "取消入" + (mw.config.get('wgNamespaceNumber') === 6 ? "和文件使用" : "") );
Window.setTitle( "取消入" + (mw.config.get('wgNamespaceNumber') === 6 ? "和文件使用" : "") );
Window.setScriptName( "Twinkle" );
Window.setScriptName( "Twinkle" );
Window.addFooterLink( "Twinkle助", "WP:TW/DOC#unlink" );
Window.addFooterLink( "Twinkle助", "WP:TW/DOC#unlink" );


var form = new Morebits.quickForm( Twinkle.unlink.callback.evaluate );
var form = new Morebits.quickForm( Twinkle.unlink.callback.evaluate );


// prepend some basic documentation
// prepend some basic documentation
var node1 = Morebits.htmlNode("code", "[[" + Morebits.pageNameNorm + "|链接文本]]")
var node1 = Morebits.htmlNode("code", "[[" + Morebits.pageNameNorm + "|連結文本]]")
var node2 = Morebits.htmlNode("code", "链接文本");
var node2 = Morebits.htmlNode("code", "連結文本");
node1.style.fontFamily = node2.style.fontFamily = "monospace";
node1.style.fontFamily = node2.style.fontFamily = "monospace";
node1.style.fontStyle = node2.style.fontStyle = "normal";
node1.style.fontStyle = node2.style.fontStyle = "normal";
行 55: 行 55:
style: 'margin-bottom: 0.5em',
style: 'margin-bottom: 0.5em',
label: [
label: [
'这个工具可以取消所有指向该页链接(“链”)' +
'這個工具可以取消所有指向該頁連結(「鏈」)' +
(mw.config.get('wgNamespaceNumber') === 6 ? ",和/或通加入<!-- -->注释标记隐藏所有此文件的使用" : "") +
(mw.config.get('wgNamespaceNumber') === 6 ? ",和/或通加入<!-- -->注釋標記隱藏所有此文件的使用" : "") +
"。比如,",
"。比如,",
node1,
node1,
"将会变成",
"將會變成",
node2,
node2,
"。小心使用。"
"。小心使用。"
]
]
} );
} );
行 97: 行 97:
};
};
}
}
var wikipedia_api = new Morebits.wiki.api( '抓取入', query, Twinkle.unlink.callbacks.display.backlinks );
var wikipedia_api = new Morebits.wiki.api( '抓取入', query, Twinkle.unlink.callbacks.display.backlinks );
wikipedia_api.params = { form: form, Window: Window, image: mw.config.get('wgNamespaceNumber') === 6 };
wikipedia_api.params = { form: form, Window: Window, image: mw.config.get('wgNamespaceNumber') === 6 };
wikipedia_api.post();
wikipedia_api.post();
行 104: 行 104:
root.style.padding = '15px'; // just so it doesn't look broken
root.style.padding = '15px'; // just so it doesn't look broken
Morebits.status.init( root );
Morebits.status.init( root );
wikipedia_api.statelem.status( "入中…" );
wikipedia_api.statelem.status( "入中…" );
Window.setContent( root );
Window.setContent( root );
Window.display();
Window.display();
行 112: 行 112:
var reason = event.target.reason.value;
var reason = event.target.reason.value;
if (!reason) {
if (!reason) {
alert("您必指定取消入的理由。");
alert("您必指定取消入的理由。");
return;
return;
}
}
行 129: 行 129:
var pages = Morebits.array.uniq(backlinks.concat(imageusage));
var pages = Morebits.array.uniq(backlinks.concat(imageusage));


var unlinker = new Morebits.batchOperation("取消入" + (imageusage ? "文件使用" : ""));
var unlinker = new Morebits.batchOperation("取消入" + (imageusage ? "文件使用" : ""));
unlinker.setOption("preserveIndividualStatusLines", true);
unlinker.setOption("preserveIndividualStatusLines", true);
unlinker.setPageList(pages);
unlinker.setPageList(pages);
var params = { reason: reason, unlinker: unlinker };
var params = { reason: reason, unlinker: unlinker };
unlinker.run(function(pageName) {
unlinker.run(function(pageName) {
var wikipedia_page = new Morebits.wiki.page(pageName, "在" + pageName + "中取消入");
var wikipedia_page = new Morebits.wiki.page(pageName, "在" + pageName + "中取消入");
wikipedia_page.setBotEdit(true); // unlink considered a floody operation
wikipedia_page.setBotEdit(true); // unlink considered a floody operation
var innerParams = $.extend({}, params);
var innerParams = $.extend({}, params);
行 171: 行 171:
apiobj.params.form.append( {
apiobj.params.form.append( {
type: 'div',
type: 'div',
label: "已选择的名字空:" + namespaces.join(', '),
label: "已選擇的名字空:" + namespaces.join(', '),
tooltip: "您可在Twinkle性中更改这个请参见[[WP:TWPREFS]]"
tooltip: "您可在Twinkle性中更改這個請參見[[WP:TWPREFS]]"
});
});
if ($(xmlDoc).find('query-continue').length) {
if ($(xmlDoc).find('query-continue').length) {
apiobj.params.form.append( {
apiobj.params.form.append( {
type: 'div',
type: 'div',
label: " " + list.length.toString() + " 文件使用。"
label: " " + list.length.toString() + " 文件使用。"
});
});
}
}
apiobj.params.form.append({
apiobj.params.form.append({
type: 'button',
type: 'button',
label: "全",
label: "全",
event: function(e) {
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, "imageusage")).prop('checked', true);
$(Morebits.quickForm.getElements(e.target.form, "imageusage")).prop('checked', true);
行 189: 行 189:
apiobj.params.form.append({
apiobj.params.form.append({
type: 'button',
type: 'button',
label: "全不",
label: "全不",
event: function(e) {
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, "imageusage")).prop('checked', false);
$(Morebits.quickForm.getElements(e.target.form, "imageusage")).prop('checked', false);
行 217: 行 217:
apiobj.params.form.append( {
apiobj.params.form.append( {
type: 'div',
type: 'div',
label: "已选择的名字空:" + namespaces.join(', '),
label: "已選擇的名字空:" + namespaces.join(', '),
tooltip: "您可在Twinkle性中更改这个请参见[[WP:TWPREFS]]"
tooltip: "您可在Twinkle性中更改這個請參見[[WP:TWPREFS]]"
});
});
if ($(xmlDoc).find('query-continue').length) {
if ($(xmlDoc).find('query-continue').length) {
apiobj.params.form.append( {
apiobj.params.form.append( {
type: 'div',
type: 'div',
label: " " + list.length.toString() + " 个链入。"
label: " " + list.length.toString() + " 個鏈入。"
});
});
}
}
apiobj.params.form.append({
apiobj.params.form.append({
type: 'button',
type: 'button',
label: "全",
label: "全",
event: function(e) {
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, "backlinks")).prop('checked', true);
$(Morebits.quickForm.getElements(e.target.form, "backlinks")).prop('checked', true);
行 235: 行 235:
apiobj.params.form.append({
apiobj.params.form.append({
type: 'button',
type: 'button',
label: "全不",
label: "全不",
event: function(e) {
event: function(e) {
$(Morebits.quickForm.getElements(e.target.form, "backlinks")).prop('checked', false);
$(Morebits.quickForm.getElements(e.target.form, "backlinks")).prop('checked', false);
行 249: 行 249:
else
else
{
{
apiobj.params.form.append( { type: 'div', label: '未找到入。' } );
apiobj.params.form.append( { type: 'div', label: '未找到入。' } );
}
}


行 275: 行 275:
// remove image usages
// remove image usages
if (params.doImageusage) {
if (params.doImageusage) {
wikiPage.commentOutImage(mw.config.get('wgTitle'), '注出');
wikiPage.commentOutImage(mw.config.get('wgTitle'), '注出');
text = wikiPage.getText();
text = wikiPage.getText();
// did we actually make any changes?
// did we actually make any changes?
行 281: 行 281:
warningString = "文件使用";
warningString = "文件使用";
} else {
} else {
summaryText = "注出文件使用";
summaryText = "注出文件使用";
oldtext = text;
oldtext = text;
}
}
行 292: 行 292:
// did we actually make any changes?
// did we actually make any changes?
if (text === oldtext) {
if (text === oldtext) {
warningString = (warningString ? "反或文件使用" : "反");
warningString = (warningString ? "反或文件使用" : "反");
} else {
} else {
summaryText = (summaryText ? (summaryText + " / ") : "") + "取消链接到";
summaryText = (summaryText ? (summaryText + " / ") : "") + "取消連結到";
oldtext = text;
oldtext = text;
}
}
行 301: 行 301:
if (warningString) {
if (warningString) {
// nothing to do!
// nothing to do!
pageobj.getStatusElement().error("未能在面上找到" + warningString + "。");
pageobj.getStatusElement().error("未能在面上找到" + warningString + "。");
params.unlinker.workerFailure(pageobj);
params.unlinker.workerFailure(pageobj);
return;
return;

於 2016年4月10日 (日) 15:27 的最新修訂

//<nowiki>
// vim: set noet sts=0 sw=8:


(function($){


/*
 ****************************************
 *** twinkleunlink.js: Unlink module
 ****************************************
 * Mode of invocation:     Tab ("Unlink")
 * Active on:              Non-special pages, except Wikipedia:Sandbox
 * Config directives in:   TwinkleConfig
 */

Twinkle.unlink = function twinkleunlink() {
	if( mw.config.get('wgNamespaceNumber') < 0 || mw.config.get('wgPageName') === 'Wikipedia:沙盒' ) {
		return;
	}
	if( Morebits.userIsInGroup( 'sysop' ) ) {
		Twinkle.addPortletLink( Twinkle.unlink.callback, "鏈入", "tw-unlink", "取消到本頁的連結" );
	}
};

Twinkle.unlink.getChecked2 = function twinkleunlinkGetChecked2( nodelist ) {
	if( !( nodelist instanceof NodeList ) && !( nodelist instanceof HTMLCollection ) ) {
		return nodelist.checked ? [ nodelist.values ] : [];
	}
	var result = [];
	for(var i  = 0; i < nodelist.length; ++i ) {
		if( nodelist[i].checked ) {
			result.push( nodelist[i].values );
		}
	}
	return result;
};

// the parameter is used when invoking unlink from admin speedy
Twinkle.unlink.callback = function(presetReason) {
	var Window = new Morebits.simpleWindow( 600, 440 );
	Window.setTitle( "取消鏈入" + (mw.config.get('wgNamespaceNumber') === 6 ? "和文件使用" : "") );
	Window.setScriptName( "Twinkle" );
	Window.addFooterLink( "Twinkle幫助", "WP:TW/DOC#unlink" );

	var form = new Morebits.quickForm( Twinkle.unlink.callback.evaluate );

	// prepend some basic documentation
	var node1 = Morebits.htmlNode("code", "[[" + Morebits.pageNameNorm + "|連結文本]]")
	var node2 = Morebits.htmlNode("code", "連結文本");
	node1.style.fontFamily = node2.style.fontFamily = "monospace";
	node1.style.fontStyle = node2.style.fontStyle = "normal";
	form.append( {
		type: 'div',
		style: 'margin-bottom: 0.5em',
		label: [
			'這個工具可以取消所有指向該頁的連結(「鏈入」)' +
				(mw.config.get('wgNamespaceNumber') === 6 ? ",和/或通過加入<!-- -->注釋標記隱藏所有對此文件的使用" : "") +
				"。比如,",
			node1,
			"將會變成",
			node2,
			"。請小心使用。"
		]
	} );

	form.append( {
		type: 'input',
		name: 'reason',
		label: '理由:',
		value: (presetReason ? presetReason : ''),
		size: 60
	} );

	var query;
	if(mw.config.get('wgNamespaceNumber') === 6) {  // File:
		query = {
			'action': 'query',
			'list': [ 'backlinks', 'imageusage' ],
			'bltitle': Morebits.pageNameNorm,
			'iutitle': Morebits.pageNameNorm,
			'bllimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
			'iulimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
			'blnamespace': Twinkle.getPref('unlinkNamespaces'),
			'iunamespace': Twinkle.getPref('unlinkNamespaces'),
			'rawcontinue': true
		};
	} else {
		query = {
			'action': 'query',
			'list': 'backlinks',
			'bltitle': Morebits.pageNameNorm,
			'blfilterredir': 'nonredirects',
			'bllimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
			'blnamespace': Twinkle.getPref('unlinkNamespaces'),
			'rawcontinue': true
		};
	}
	var wikipedia_api = new Morebits.wiki.api( '抓取鏈入', query, Twinkle.unlink.callbacks.display.backlinks );
	wikipedia_api.params = { form: form, Window: Window, image: mw.config.get('wgNamespaceNumber') === 6 };
	wikipedia_api.post();

	var root = document.createElement( 'div' );
	root.style.padding = '15px';  // just so it doesn't look broken
	Morebits.status.init( root );
	wikipedia_api.statelem.status( "載入中…" );
	Window.setContent( root );
	Window.display();
};

Twinkle.unlink.callback.evaluate = function twinkleunlinkCallbackEvaluate(event) {
	var reason = event.target.reason.value;
	if (!reason) {
		alert("您必須指定取消鏈入的理由。");
		return;
	}

	var backlinks = [], imageusage = [];
	if( event.target.backlinks ) {
		backlinks = Twinkle.unlink.getChecked2(event.target.backlinks);
	}
	if( event.target.imageusage ) {
		imageusage = Twinkle.unlink.getChecked2(event.target.imageusage);
	}

	Morebits.simpleWindow.setButtonsEnabled( false );
	Morebits.status.init( event.target );

	var pages = Morebits.array.uniq(backlinks.concat(imageusage));

	var unlinker = new Morebits.batchOperation("取消鏈入" + (imageusage ? "與文件使用" : ""));
	unlinker.setOption("preserveIndividualStatusLines", true);
	unlinker.setPageList(pages);
	var params = { reason: reason, unlinker: unlinker };
	unlinker.run(function(pageName) {
		var wikipedia_page = new Morebits.wiki.page(pageName, "在條目「" + pageName + "」中取消鏈入");
		wikipedia_page.setBotEdit(true);  // unlink considered a floody operation
		var innerParams = $.extend({}, params);
		innerParams.doBacklinks = backlinks && backlinks.indexOf(pageName) !== -1;
		innerParams.doImageusage = imageusage && imageusage.indexOf(pageName) !== -1;
		wikipedia_page.setCallbackParameters(innerParams);
		wikipedia_page.load(Twinkle.unlink.callbacks.unlinkBacklinks);
	});
};

Twinkle.unlink.callbacks = {
	display: {
		backlinks: function twinkleunlinkCallbackDisplayBacklinks(apiobj) {
			var xmlDoc = apiobj.responseXML;
			var havecontent = false;
			var list, namespaces, i;

			if( apiobj.params.image ) {
				var imageusage = $(xmlDoc).find('query imageusage iu');
				list = [];
				for ( i = 0; i < imageusage.length; ++i ) {
					var usagetitle = imageusage[i].getAttribute('title');
					list.push( { label: usagetitle, value: usagetitle, checked: true } );
				}
				if (!list.length)
				{
					apiobj.params.form.append( { type: 'div', label: '未找到文件使用。' } );
				}
				else
				{
					apiobj.params.form.append( { type:'header', label: '文件使用' } );
					namespaces = [];
					$.each(Twinkle.getPref('unlinkNamespaces'), function(k, v) {
						namespaces.push(Morebits.wikipedia.namespacesFriendly[v]);
					});
					apiobj.params.form.append( {
						type: 'div',
						label: "已選擇的名字空間:" + namespaces.join(', '),
						tooltip: "您可在Twinkle屬性中更改這個,請參見[[WP:TWPREFS]]"
					});
					if ($(xmlDoc).find('query-continue').length) {
						apiobj.params.form.append( {
							type: 'div',
							label: "顯示頭 " + list.length.toString() + " 個文件使用。"
						});
					}
					apiobj.params.form.append({
						type: 'button',
						label: "全選",
						event: function(e) {
							$(Morebits.quickForm.getElements(e.target.form, "imageusage")).prop('checked', true);
						}
					});
					apiobj.params.form.append({
						type: 'button',
						label: "全不選",
						event: function(e) {
							$(Morebits.quickForm.getElements(e.target.form, "imageusage")).prop('checked', false);
						}
					});
					apiobj.params.form.append({
						type: 'checkbox',
						name: 'imageusage',
						list: list
					});
					havecontent = true;
				}
			}

			var backlinks = $(xmlDoc).find('query backlinks bl');
			if( backlinks.length > 0 ) {
				list = [];
				for ( i = 0; i < backlinks.length; ++i ) {
					var title = backlinks[i].getAttribute('title');
					list.push( { label: title, value: title, checked: true } );
				}
				apiobj.params.form.append( { type:'header', label: 'Backlinks' } );
				namespaces = [];
				$.each(Twinkle.getPref('unlinkNamespaces'), function(k, v) {
					namespaces.push(Morebits.wikipedia.namespacesFriendly[v]);
				});
				apiobj.params.form.append( {
					type: 'div',
					label: "已選擇的名字空間:" + namespaces.join(', '),
					tooltip: "您可在Twinkle屬性中更改這個,請參見[[WP:TWPREFS]]"
				});
				if ($(xmlDoc).find('query-continue').length) {
					apiobj.params.form.append( {
						type: 'div',
						label: "顯示頭 " + list.length.toString() + " 個鏈入。"
					});
				}
				apiobj.params.form.append({
					type: 'button',
					label: "全選",
						event: function(e) {
							$(Morebits.quickForm.getElements(e.target.form, "backlinks")).prop('checked', true);
						}
				});
				apiobj.params.form.append({
					type: 'button',
					label: "全不選",
						event: function(e) {
							$(Morebits.quickForm.getElements(e.target.form, "backlinks")).prop('checked', false);
						}
				});
				apiobj.params.form.append({
					type: 'checkbox',
					name: 'backlinks',
					list: list
				});
				havecontent = true;
			}
			else
			{
				apiobj.params.form.append( { type: 'div', label: '未找到鏈入。' } );
			}

			if (havecontent) {
				apiobj.params.form.append( { type:'submit' } );
			}

			var result = apiobj.params.form.render();
			apiobj.params.Window.setContent( result );

			Morebits.checkboxShiftClickSupport($("input[name='imageusage']", result));
			Morebits.checkboxShiftClickSupport($("input[name='backlinks']", result));

		}
	},
	unlinkBacklinks: function twinkleunlinkCallbackUnlinkBacklinks(pageobj) {
		var oldtext = pageobj.getPageText();
		var params = pageobj.getCallbackParameters();
		var wikiPage = new Morebits.wikitext.page(oldtext);

		var removedBacklinks = false, removedImageusage = false;
		var summaryText = "", warningString = false;
		var text;

		// remove image usages
		if (params.doImageusage) {
			wikiPage.commentOutImage(mw.config.get('wgTitle'), '注釋出');
			text = wikiPage.getText();
			// did we actually make any changes?
			if (text === oldtext) {
				warningString = "文件使用";
			} else {
				summaryText = "注釋出文件使用";
				oldtext = text;
			}
		}

		// remove backlinks
		if (params.doBacklinks) {
			wikiPage.removeLink(Morebits.pageNameNorm);
			text = wikiPage.getText();
			// did we actually make any changes?
			if (text === oldtext) {
				warningString = (warningString ? "反鏈或文件使用" : "反鏈");
			} else {
				summaryText = (summaryText ? (summaryText + " / ") : "") + "取消連結到";
				oldtext = text;
			}
		}

		if (warningString) {
			// nothing to do!
			pageobj.getStatusElement().error("未能在頁面上找到" + warningString + "。");
			params.unlinker.workerFailure(pageobj);
			return;
		}

		pageobj.setPageText(text);
		pageobj.setEditSummary(summaryText + " \"" + Morebits.pageNameNorm + "\": " + params.reason + "." + Twinkle.getPref('summaryAd'));
		pageobj.setCreateOption('nocreate');
		pageobj.save(params.unlinker.workerSuccess, params.unlinker.workerFailure);
	}
};
})(jQuery);


//</nowiki>