-
Notifications
You must be signed in to change notification settings - Fork 3
/
SubpageMover.js
129 lines (105 loc) · 3.79 KB
/
SubpageMover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*** Subpage Mover ***/
// Easily move the subpages of a page with a single click
// Documentation at [[en:w:User:BrandonXLF/SubpageMover]]
// By [[en:w:User:BrandonXLF]]
$(function() {
function Log() {
$('#moveSubpages-log').remove();
this.el = $('<div>')
.appendTo($('#movepage'))
.append('<br><hr>')
.attr('id', 'moveSubpages-log');
}
Log.prototype.log = function(html, color) {
this.el.append($('<p>').append(html).css('color', color));
};
Log.prototype.createLink = function(page) {
return $('<a>').attr('href', mw.util.getUrl(page)).text(page);
};
Log.prototype.logError = function(html) {
this.log(html, '#d33');
};
Log.prototype.logSuccess = function(html) {
this.log(html, '#14866d');
};
Log.prototype.logMoveError = function(from, to, reasonHtml) {
this.logError(['Failed to move page ', this.createLink(from), ' to ', this.createLink(to), '. Reason: ', reasonHtml]);
};
Log.prototype.logMoveSuccess = function(from, to) {
this.logSuccess(['Successfully moved page ', this.createLink(from), ' to ', this.createLink(to), '.']);
};
function movePage(from, to, params, log, onSuccess) {
$.post(mw.config.get('wgScriptPath') + '/api.php', $.extend({
action: 'move',
from: from,
to: to,
token: mw.user.tokens.get('csrfToken'),
format: 'json',
formatversion: '2',
uselang: 'user',
errorformat: 'html',
errorlang: 'uselang'
}, params)).done(function(response) {
if (response.errors) {
log.logMoveError(from, to, response.errors[0].html);
return;
}
log.logMoveSuccess(response.move.from, response.move.to);
if (response.move['talkmove-errors']) {
var talkFrom = (from.match(':') ? from.replace(':', ' talk:') : 'Talk:') + from,
talkTo = (to.match(':') ? to.replace(':', ' talk:') : 'Talk:') + to;
log.logMoveError(talkFrom, talkTo, response.move['talkmove-errors'][0].html);
return;
}
if (response.move.talkfrom) {
log.logMoveSuccess(response.move.talkfrom, response.move.talkto);
}
onSuccess && onSuccess();
});
}
function moveSubpages() {
$('#moveSubpages-log').remove();
var log = new Log();
if (mw.config.get('wgUserGroups').indexOf('extendedconfirmed') === -1) {
log.log('You must be at least extended confirmed.', 'red');
return;
}
var fromTitle = new mw.Title($('input[name="wpOldTitle"]').val()),
toTitle = mw.Title.makeTitle($('select[name="wpNewTitleNs"]').val(), $('input[name="wpNewTitleMain"]').val()),
params = {
reason: $('input[name="wpReason"]').val(),
movetalk: $('input[name="wpMovetalk"]').prop('checked') ? true : undefined,
noredirect: $('[name="wpLeaveRedirect"]').prop('checked') === false ? true : undefined,
watchlist: $('input[name="wpWatch"]').prop('checked') ? 'watch' : 'nochange',
};
if (!toTitle) {
log.logError('New title is an invalid page!');
return;
}
$.get(mw.config.get('wgScriptPath') + '/api.php', {
action: 'query',
list: 'allpages',
apprefix: fromTitle.getMainText() + '/',
apnamespace: fromTitle.getNamespaceId(),
pslimit: '500',
format: 'json',
formatversion: '2'
}).done(function(res) {
movePage(fromTitle.getPrefixedText(), toTitle.getPrefixedText(), params, log, function() {
var prefixRegex = new RegExp('^' + mw.util.escapeRegExp(fromTitle.getPrefixedText()));
res.query.allpages.forEach(function(info) {
if (info.title === fromTitle) return;
movePage(info.title, info.title.replace(prefixRegex, toTitle.getPrefixedText()), params, log);
});
});
});
}
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());
}
});