-
Notifications
You must be signed in to change notification settings - Fork 0
/
marketshare.js
219 lines (202 loc) · 5.46 KB
/
marketshare.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
var nodemw = require('nodemw'),
mysql = require('mysql');
(function(bot) {
// pass configuration object
var client = new bot('.node-bot.config.json'),
tools = [{
name: 'bigChunkedUpload dot JS',
signature: '%bigChunkedUpload.js%'
}, {
name: 'Upload Wizard (WMF)',
signature: '%with UploadWizard%'
}, {
name: 'VicuñaUploader',
signature: '%Vicu__aUploader%'
}, {
name: 'Videoconvert upload from toollabs',
signature: '_ideoconvert % toollabs'
}, {
name: 'GLAM wiki toolset',
signature: '%GWToolset%'
}, {
name: 'video2commons',
signature: 'Imported media from %'
}],
updateBot;
updateBot = {
version: '0.0.0.1',
config: {
query: 'SELECT count(*) AS count FROM image '
+ 'WHERE `img_size` > 209715200 '
+ 'AND `img_timestamp` > 20150301000000 '
+ 'AND `img_description` LIKE ? '
+ 'ORDER BY `img_timestamp` DESC;',
totalQuery: 'SELECT count(*) AS count FROM image '
+ 'WHERE `img_size` > 209715200 '
+ 'AND `img_timestamp` > 20150301000000 '
+ 'ORDER BY `img_timestamp` DESC;',
},
nextTool: function() {
var updater = this;
if (!tools.length) return false;
updater.updateTotal();
updater.update( tools.pop() );
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.nextTool();
}, 1000);
});
}, 1000);
});
});
});
// Kill myself if running too long
setTimeout(function() {
updater.logOut();
process.exit(1);
}, 360000);
},
pages: [],
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.labsdb',
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();
}
});
},
updatePageWith: function( value, toolName, callback ) {
var updater = this;
console.log( 'Updating page for ' + toolName + ': ' + value );
client.edit('User:Rillke/bigChunkedUpload.js/share/' + toolName, '<onlyinclude>' + value + '</onlyinclude>', 'Bot: Updating statistics. New value:' + value + '. Bot version:' + updater.version, callback);
},
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.pendingEdits !== 0 ) return;
if ( updater.exiting ) return;
if ( updater.nextTool() ) 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');
});
},
getShare: function( toolSignature, callback ) {
console.log('Running SQL query.');
if ( toolSignature ) {
this.connection.query(this.config.query, toolSignature, function(err, result) {
var result;
if (!err) {
result = result[0].count;
}
result = result || 0;
callback( result );
});
} else {
this.connection.query(this.config.totalQuery, function(err, result) {
var result;
if (!err) {
result = result[0].count;
}
result = result || 0;
callback( result );
});
}
},
update: function( tool ) {
var updater = this;
updater.pendingEdits++;
updater.getShare( tool.signature, function( value ) {
updater.updatePageWith( value, tool.name, function() {
updater.pendingEdits--;
console.log( 'Editing ' + tool.name + ': Okay.' );
updater.maybeExit();
} );
} );
},
updateTotal: function() {
var updater = this;
updater.pendingEdits++;
updater.getShare( '', function( value ) {
updater.updatePageWith( value, 'total', function() {
updater.pendingEdits--;
console.log( 'Editing total: Okay.' );
updater.maybeExit();
} );
} );
}
};
updateBot.launch();
}(nodemw));