User:NguoiDungKhongDinhDanh/SubpagesMover.js

From Test Wiki
Revision as of 14:00, 10 November 2021 by NguoiDungKhongDinhDanh (talk | contribs) (Created page with "→‎** Subpage Mover **: // Easily move the subpages of a page with a single click // Documention at w:en:User:BrandonXLF/SubpageMover // By User:BrandonXLF $(functi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*** Subpage Mover ***/

// Easily move the subpages of a page with a single click
// Documention at [[:w:en:User:BrandonXLF/SubpageMover]]
// By [[User:BrandonXLF]]

$(function() {
	function moveSubpages() {
		var allowed = false;

		$.each(mw.config.get('wgUserGroups'), function(i, v) {
			if ((v == 'extendedconfirmed') || (v == 'sysop')) {
				allowed = true;

				var from = $('input[name=wpOldTitle]').val(),
					to_ns = mw.config.get('wgFormattedNamespaces')[$('select[name=wpNewTitleNs]').val()].replace(' ', '_'),
					to_page = $('input[name=wpNewTitleMain]').val(),
					to = (to_ns === '' ? to_page : to_ns + ':' + to_page),
					reason = $('input[name=wpReason]').val(),
					talk = $('input[name=wpMovetalk]').prop('checked') ? 'yes' : void 0,
					watch = $('input[name=wpWatch]').prop('checked') ? 'watch' : void 0;

				$.get(mw.config.get('wgScriptPath') + '/api.php', {
					action: 'query',
					list: 'prefixsearch',
					pssearch: from + '/',
					pslimit: '500',
					format: 'json'
				}).done(function(apiQuery) {
					$('#moveSubpages-log').remove();

					var log = $('<span>')
						.appendTo($('#movepage'))
						.append('<br /><hr />')
						.attr('id', 'moveSubpages-log');

					function movePage(from, to, noerror) {
						$.post(mw.config.get('wgScriptPath') + '/api.php', {
							action: 'move',
							from: from,
							to: to,
							reason: reason,
							movetalk: talk,
							watchlist: watch,
							token: mw.user.tokens.get('csrfToken'),
							format: 'json'
						}).done(function(response) {
							if (response.move) {
								if (response.move['talkmove-errors']) {
									var talkpage = from.match(':') ? from.replace(':', ' talk:') : 'Talk:' + from;
									log.append($('<p>').text(talkpage + ' could not be moved.').css('color', 'red'));
								} else if (response.move.talkfrom) {
									log.append(
										'<p>Successfully moved ' +
										response.move.talkfrom +
										' to ' +
										response.move.talkto +
										'.</p>'
									).css('color', 'green');
								}
							}

							if (response.error) {
								log.append($('<p>').text(from + ' could not be moved.').css('color', 'red'));
								log.append($('<p>').append('&bull; Reason: ' + response.error.info + '</li>').css('color', 'red'));
							} else {
								log.append('<p>Successfully moved ' + response.move.from + ' to ' + response.move.to + '.</p>').css('color', 'green');
								noerror();
							}
						});
					}

					movePage(from, to, function() {
						apiQuery.query.prefixsearch.forEach(function(info) {
							if (info.title === from) return;
							movePage(info.title, info.title.replace(from, to));
						});
					});
				});
			}
		});

		if (!allowed) {
			mw.notify('You must be at least extended confimed.', {title: 'Cannot move page and subpages', type: 'error'});
		}
	}

	if (mw.config.get('wgDBname') == 'viwiki') {
		if (mw.config.get('wgCanonicalSpecialPageName') == 'MovePage') {
			new OO.ui.ButtonWidget({
				label: 'Di chuyển trang và trang con',
				flags: ['primary', 'progressive']
			}).$element
				.on('click', moveSubpages)
				.appendTo($('button[name=wpMove]').parent().parent());
		}
	}
	else {
		if (window.location.href.match('Special:MovePage') && ! $('p:contains(\'This page has no subpages.\')')[0]) {
			new OO.ui.ButtonWidget({
				label: 'Move page and subpages',
				flags: ['primary', 'progressive']
			}).$element
				.on('click', moveSubpages)
				.appendTo($('button[name=wpMove]').parent().parent());
		}
	}
});