(function($) {

/*

Twinkle.batchundelete = function twinklebatchundelete {
	if (!Morebits.userIsSysop || !mw.config.get('wgArticleId') || (
		mw.config.get('wgNamespaceNumber') !== mw.config.get('wgNamespaceIds').user &&
		mw.config.get('wgNamespaceNumber') !== mw.config.get('wgNamespaceIds').project)) {
		return;
	Twinkle.addPortletLink;

Twinkle.batchundelete.callback = function twinklebatchundeleteCallback {
	var Window = new Morebits.simpleWindow;
	Window.setScriptName('트윙클');
	Window.setTitle('일괄 복구');
	Window.addFooterLink;

	var form = new Morebits.quickForm(Twinkle.batchundelete.callback.evaluate);
	form.append({
		type: 'checkbox',
		list: [
				label: '존재하는 경우 삭제된 문서의 토론 문서를 복구합니다',
				name: 'undel_talk',
				value: 'undel_talk',
				checked: true
		]
	form.append({
		type: 'input',
		name: 'reason',
		label: '이유: ',
		size: 60

	var statusdiv = document.createElement('div');
	statusdiv.style.padding = '15px'; // just so it doesn't look broken
	Window.setContent(statusdiv);
	Morebits.status.init(statusdiv);
	Window.display;

	var query = {
		'action': 'query',
		'generator': 'links',
		'prop': 'info',
		'inprop': 'protection',
		'titles': mw.config.get('wgPageName'),
		'gpllimit': Twinkle.getPref('batchMax')
	var statelem = new Morebits.status('문서 목록을 가져오고 있습니다');
	var wikipedia_api = new Morebits.wiki.api('불러오는 중...', query, function(apiobj) {
		var xml = apiobj.responseXML;
		var $pages = $(xml).find('page[missing]');
		var list = [];
		$pages.each(function {
			var $page = $(page);
			var title = $page.attr('title');
			var $editprot = $page.find('pr[type="create"][level="sysop"]');
			var isProtected = $editprot.length > 0;

			list.push({
				label: title + (isProtected ? ' (fully create protected' +
					($editprot.attr('expiry') === 'infinity' ? ' indefinitely' : ', expires ' + new Morebits.date($editprot.attr('expiry')).calendar('utc') + ' (UTC)') + ')' : ''),
				value: title,
				checked: true,
				style: isProtected ? 'color:red' : ''
		apiobj.params.form.append;
		apiobj.params.form.append({
			type: 'button',
			label: '모두 선택',
			event: function(e) {
				$(Morebits.quickForm.getElements).prop;
		apiobj.params.form.append({
			type: 'button',
			label: '모두 선택 해제',
			event: function(e) {
				$(Morebits.quickForm.getElements).prop;
		apiobj.params.form.append({
			type: 'checkbox',
			name: 'pages',
			shiftClickSupport: true,
			list: list
		apiobj.params.form.append({ type: 'submit' });

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

	wikipedia_api.params = { form: form, Window: Window };
	wikipedia_api.post;

Twinkle.batchundelete.callback.evaluate = function(event) {
	Morebits.wiki.actionCompleted.notice = '일괄 복구가 완료도었습니다';

	var numProtected = Morebits.quickForm.getElements.filter(function(element) {
		return element.checked && element.nextElementSibling.style.color === 'red';
	if (numProtected > 0 && !confirm('생성 보호된 문서 ' + numProtected + '개를 복구하려고 합니다. 계속하시겠습니까?')) {
		return;

	var input = Morebits.quickForm.getInputData(event.target);

	if (!input.reason) {
		alert('이유를 지정해야 합니다!');
		return;
	Morebits.simpleWindow.setButtonsEnabled(false);
	Morebits.status.init(event.target);

	if (!input.pages || !input.pages.length) {
		Morebits.status.error;
		return;

	var pageUndeleter = new Morebits.batchOperation('문서 복구 중');
	pageUndeleter.setOption('chunkSize', Twinkle.getPref('batchChunks'));
	pageUndeleter.setOption;
	pageUndeleter.setPageList(input.pages);
	pageUndeleter.run(function(pageName) {
		var params = {
			page: pageName,
			undel_talk: input.undel_talk,
			reason: input.reason,
			pageUndeleter: pageUndeleter

		var wikipedia_page = new Morebits.wiki.page;
		wikipedia_page.setCallbackParameters(params);
		wikipedia_page.setEditSummary(input.reason + Twinkle.getPref('deletionSummaryAd'));
		wikipedia_page.suppressProtectWarning;
		wikipedia_page.setMaxRetries(3); // temporary increase from 2 to make batchundelete more likely to succeed phab:T222402 #613
		wikipedia_page.undeletePage;

Twinkle.batchundelete.callbacks = {
	// this stupid parameter name is a temporary thing until I implement an overhaul
	// of Morebits.wiki.* callback parameters
	doExtras: function(thingWithParameters) {
		var params = thingWithParameters.parent ? thingWithParameters.parent.getCallbackParameters :
			thingWithParameters.getCallbackParameters;
		// the initial batch operation's job is to delete the page, and that has
		// succeeded by now
		params.pageUndeleter.workerSuccess(thingWithParameters);

		var query, wikipedia_api;

		if (params.undel_talk) {
			var talkpagename = new mw.Title(params.page).getTalkPage.getPrefixedText;
			if (talkpagename !== params.page) {
				query = {
					'action': 'query',
					'prop': 'deletedrevisions',
					'drvprop': 'ids',
					'drvlimit': 1,
					'titles': talkpagename
				wikipedia_api = new Morebits.wiki.api;
				wikipedia_api.params = params;
				wikipedia_api.params.talkPage = talkpagename;
				wikipedia_api.post;
	undeleteTalk: function(apiobj) {
		var xml = apiobj.responseXML;
		var exists = $(xml).find('page:not([missing])').length > 0;
		var delrevs = $(xml).find('rev').attr('revid');

		if (exists || !delrevs) {
			// page exists or has no deleted revisions; forget about it
			return;

		var page = new Morebits.wiki.page;
		page.setEditSummary('토론 문서 복구: "' + apiobj.params.page + '"' + Twinkle.getPref('deletionSummaryAd'));
		page.undeletePage;

Twinkle.addInitCallback;