-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfobox-move.js
164 lines (159 loc) · 4.54 KB
/
Infobox-move.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var i = 0;
var ind = 0;
$("#js-test").click(function(){
getInfoboxPages();
});
function getInfoboxPages() {
getUserGroup();
if (userGroup.some(isBot) == false) return;
getToken();
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:Infobox_page',
cmlimit: 400,
cmprop: 'title',
cmcontinue: 'page|4348454d495354525920285245534541524348292f494e464f424f58|38720' //skip the pages that I botched (up to chemical plant)
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var categorymembers = data.query.categorymembers;
console.log('Found ' + categorymembers.length + ' infobox pages.');
setTimeout(function(){getPagesUsingInfobox(categorymembers, categorymembers[0].title)}, 500);
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}
function getPagesUsingInfobox(categorymembers, infoboxPage) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
list: 'embeddedin',
eititle: infoboxPage,
eilimit: 500,
eifilterredir: 'nonredirects',
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var embeddedin = data.query.embeddedin;
if (embeddedin.length == 0) {
console.log('No pages transclude ' + infoboxPage);
setTimeout(function(){moveInfoboxPage(infoboxPage, categorymembers)}, 500);
} else {
console.log(embeddedin.length + ' pages transclude ' + infoboxPage + '.');
setTimeout(function(){changePageUsingInfobox(infoboxPage, embeddedin, embeddedin[0].title, categorymembers)}, 500);
}
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}
function changePageUsingInfobox(infoboxPage, embeddedin, pageUsingInfobox, categorymembers) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: pageUsingInfobox,
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
var content = revisions[Object.keys(revisions)[2]];
setTimeout(function(){editPageUsingInfobox(infoboxPage, embeddedin, pageUsingInfobox, content, categorymembers)}, 200);
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}
function editPageUsingInfobox(infoboxPage, embeddedin, pageUsingInfobox, content, categorymembers) {
var searchString = "{{:" + infoboxPage + "}}";
var posi = infoboxPage.search("/infobox");
var newInfoboxPage = infoboxPage.slice(0,posi);
var replaceString = "{{:Infobox:" + newInfoboxPage + "}}";
var newContent = content.replace(searchString, replaceString)
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'edit',
title: pageUsingInfobox,
text: newContent,
token: globalToken,
summary: 'New infobox organization.',
bot: true,
nocreate: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log('Changed ' + pageUsingInfobox);
ind++;
if (ind + 1 > embeddedin.length) {
console.log("Changed all pages that transclude " + infoboxPage + ".");
ind = 0;
setTimeout(function(){moveInfoboxPage(infoboxPage, categorymembers)}, 500);
} else {
setTimeout(function(){changePageUsingInfobox(infoboxPage, embeddedin, embeddedin[ind].title, categorymembers)}, 500);
}
},
error: function( xhr ) {
alert('Failed to change ' + pageUsingInfobox);
}
});
}
function moveInfoboxPage(infoboxPage, categorymembers) {
var posi = infoboxPage.search("/infobox");
var newInfoboxPage = infoboxPage.slice(0,posi);
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'move',
token: globalToken,
from: infoboxPage,
to: 'Infobox:' + newInfoboxPage,
ignorewarnings: true,
reason: 'New infobox organization.',
noredirect: true,
movetalk: true,
movesubpages: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function(data) {
console.log('Moved ' + infoboxPage + ' to Infobox:' + newInfoboxPage + '.');
i++;
if (i + 1 > categorymembers.length) {
console.log("Job's done!");
i = 0;
return;
} else {
setTimeout(function(){getPagesUsingInfobox(categorymembers, categorymembers[i].title)}, 500);
}
},
error: function(xhr) {
alert( 'Error: Request failed. Could not move ' + infoboxPage + '.');
}
});
}