-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
267 lines (234 loc) · 7.15 KB
/
update.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
var nodemw = require('nodemw'),
mysql = require('mysql');
(function(bot) {
// pass configuration object
var client = new bot('.node-bot.config.json'),
modes = [{
cat: 'Pages to be updated by UploadStatsBot - top file revisions',
regexp: /\{\{\s*(?:[Tt]emplate\:)?[Uu]ploadStats\/alive\s*\}\}/,
template: '{{UploadStats/alive}}',
queries: [
'SELECT count(*) AS count FROM image INNER JOIN actor_image ON img_actor = actor_id WHERE actor_name=? ORDER BY img_timestamp DESC;'
]
}, {
cat: 'Pages to be updated by UploadStatsBot - all alive',
regexp: /\{\{\s*(?:[Tt]emplate\:)?[Uu]ploadStats\/all[ _]alive\s*\}\}/,
template: '{{UploadStats/all alive}}',
queries: [
'SELECT count(*) AS count FROM image INNER JOIN actor_image ON img_actor = actor_id WHERE actor_name=? ORDER BY img_timestamp DESC;',
'SELECT count(*) AS count FROM oldimage INNER JOIN actor_oldimage ON oi_actor = actor_id WHERE actor_name=? ORDER BY oi_timestamp DESC;'
]
}, {
cat: 'Pages to be updated by UploadStatsBot - deleted',
regexp: /\{\{\s*(?:[Tt]emplate\:)?[Uu]ploadStats\/deleted\s*\}\}/,
template: '{{UploadStats/deleted}}',
queries: [
'SELECT count(*) AS count FROM filearchive INNER JOIN actor_filearchive ON fa_actor = actor_id WHERE actor_name=? ORDER BY fa_timestamp DESC;'
]
}, {
cat: 'Pages to be updated by UploadStatsBot - edits',
regexp: /\{\{\s*(?:[Tt]emplate\:)?[Uu]ploadStats\/edits\s*\}\}/,
template: '{{UploadStats/edits}}',
queries: [
'SELECT user_editcount AS count FROM user WHERE user_name=?;'
]
}],
currentMode,
updateBot;
updateBot = {
version: '0.0.0.4',
config: {},
uploadCountCache: {},
nextMode: function() {
var updater = this;
if (!modes.length) return false;
currentMode = modes.pop();
updater.uploadCountCache[currentMode.template] = {};
updater.fetchPages();
return true;
},
launch: function() {
var updater = this;
console.log('Hi. This is upload updater bot.');
updater.logOut(function() {
updater.establishDBConnection(function() {
client.logIn(function() {
// Make the server creating an editToken for our session.
// If we do that later while processing multiple pages, the sever
// would create a lot of different tokens due to replecation lag.
setTimeout(function() {
client.api.call({
action: 'query',
meta: 'tokens'
}, function(r) {
setTimeout(function() {
updater.nextMode();
}, 1000);
});
}, 1000);
});
});
});
// Kill myself if running too long
setTimeout(function() {
updater.logOut();
process.exit(1);
}, 43200000);
},
pages: [],
pendigPages: 0,
pendingEdits: 0,
dbCredentials: {
pass: '',
user: ''
},
setPassAndUserName: function() {
// Set the dbCredentials
console.log('Reading passwords.');
var fs = require('fs'),
cred = fs.readFileSync('./replica.my.cnf', {
encoding: 'utf8'
}),
arr = cred.split('\n'),
l, i, line;
for (i = 0, l = arr.length; i < l; ++i) {
line = arr[i];
if (/user\s*\=/.test(line)) {
this.dbCredentials.user = line.replace(/^\s*user\s*=\s*'(.+)'.*/, '$1');
} else if (/password\s*\=/.test(line)) {
this.dbCredentials.pass = line.replace(/^\s*pass(?:word)?\s*=\s*'(.+)'.*/, '$1');
}
}
},
establishDBConnection: function( cb ) {
var updater = this;
this.setPassAndUserName();
var connection = mysql.createConnection({
host : 'commonswiki.analytics.db.svc.eqiad.wmflabs',
database : 'commonswiki_p',
user : this.dbCredentials.user,
password : this.dbCredentials.pass
});
connection.connect(function(err) {
if (err) {
console.log(err);
} else {
console.log('Connected to DB as user ' + updater.dbCredentials.user + '.');
updater.connection = connection;
cb();
}
});
},
getUploadCount: function( username, callback ) {
var updater = this,
result = updater.uploadCountCache[currentMode.template][username];
if ( result !== undefined ) return callback( result );
console.log('Running SQL queries.');
result = 0;
var decrementAndContinue = function( count ) {
pending--;
result += count;
if (0 === pending) {
updater.uploadCountCache[currentMode.template][username] = result;
callback(result);
}
};
var i, l,
pending = 0;
for (var i = 0, l = currentMode.queries.length; i < l; ++i) {
pending++;
this.connection.query(currentMode.queries[i], username, function(err, result) {
if (!err) {
var result = result[0].count;
}
result = result || 0;
decrementAndContinue( result );
});
}
},
fetchPages: function() {
var updater = this;
client.getPagesInCategory(currentMode.cat, function(data) {
var i, l, d, pgId;
for (i = 0, l = data.length; i < l; ++i) {
d = data[i];
if ( d.ns === 2 ) {
updater.pages.push( pgId = data[i].pageid );
updater.processPage( pgId, data[i].title );
}
}
updater.maybeExit();
});
},
maybeCloseDBConnecton: function() {
if (this.pendingEdits === 0 && this.pendigPages === 0) {
console.log('Connection is being closed. Please stay away from the database.');
this.connection.destroy();
console.log('Connection closed.');
}
},
maybeExit: function() {
var updater = this;
if ( updater.pendigPages !== 0 ) return;
if ( updater.pendingEdits !== 0 ) return;
if ( updater.exiting ) return;
if ( updater.nextMode() ) return;
updater.maybeCloseDBConnecton();
updater.exiting = true;
console.info('Bye bye!');
setTimeout(function() {
updater.logOut(function() {
process.exit(0);
});
}, 1000);
},
logOut: function( callback ) {
client.api.call({
action: 'query',
meta: 'tokens'
}, function(r) {
console.log('Got a token');
console.log('Token: ' + r.tokens.csrftoken);
client.api.call({
action: 'logout',
token: r.tokens.csrftoken
}, callback || function(){}, 'POST');
});
},
processPage: function(pgId, pgName) {
var updater = this;
if (!pgId) return;
updater.pendigPages++;
client.getArticle(pgId, function(data) {
updater.pendigPages--;
console.log('Okay, got page contents for ' + pgName);
if (data.length > 75 || !currentMode.regexp.test(data)) {
// Do not vandalize pages.
return updater.maybeExit();
}
console.log(pgName + ' has valid content.');
// Fetch upload count of the user
var username = pgName.replace(/^[^:]+?\:([^\/]+).+/, '$1');
updater.pendingEdits++;
updater.getUploadCount( username, function(uploadCount) {
console.log('And ' + username + ' has uploaded ' + uploadCount + ' files that are alive.');
var newContent = currentMode.template + '<onlyinclude>' + uploadCount + '</onlyinclude>';
if (data === newContent) {
updater.pendingEdits--;
console.log('Skipped ' + pgName + '.');
updater.maybeExit();
return;
}
client.edit(pgName, newContent, 'Bot: Updating statistics. New value:' + uploadCount + '. Bot version:' + updater.version, function() {
updater.pendingEdits--;
console.log('Editing ' + pgName + ': Okay.');
updater.maybeExit();
});
});
updater.maybeExit();
});
updater.maybeExit();
}
};
updateBot.launch();
}(nodemw));