-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmysql_functions.php
More file actions
299 lines (278 loc) · 11.5 KB
/
Copy pathmysql_functions.php
File metadata and controls
299 lines (278 loc) · 11.5 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace CluebotNG;
use mysqli_sql_exception;
/*
* Copyright (C) 2015 Jacobi Carter and Chris Breneman
*
* This file is part of ClueBot NG.
*
* ClueBot NG is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* ClueBot NG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ClueBot NG. If not, see <http://www.gnu.org/licenses/>.
*/
class ReplicaDb
{
private static function connect($exclude_users = [])
{
global $logger;
if (count(Config::$mw_mysql_credentials) > 0) {
$candidate_credentials = array_filter(
Config::$mw_mysql_credentials,
fn($cred) => !in_array($cred['user'], $exclude_users, true)
);
if (empty($candidate_credentials)) {
$logger->error("ran out of database credentials");
Metrics::increment('bot_mysql_mw_credentials_exhausted_total');
die();
}
$selected = $candidate_credentials[array_rand($candidate_credentials)];
$mw_mysql_user = $selected['user'];
$mw_mysql_pass = $selected['pass'];
} elseif (!empty($exclude_users)) {
$logger->error("ran out of database credentials");
die();
} else {
$mw_mysql_user = Config::$mw_mysql_user;
$mw_mysql_pass = Config::$mw_mysql_pass;
}
try {
$mw_mysql = mysqli_init();
$mw_mysql->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
$mw_mysql->real_connect(
Config::$mw_mysql_host,
$mw_mysql_user,
$mw_mysql_pass,
Config::$mw_mysql_db,
Config::$mw_mysql_port
);
} catch (mysqli_sql_exception $e) {
if ($e->getCode() == 1226) {
$logger->debug("ran out of database connections for " . $mw_mysql_user);
Metrics::increment('bot_mysql_mw_credential_conn_limit_total', [$mw_mysql_user]);
Metrics::increment('bot_mysql_mw_connection_retries_total');
usleep(5000);
$exclude_users[] = $mw_mysql_user;
return self::connect($exclude_users);
}
die('replica mysql error: ' . $e->getMessage());
}
if (!$mw_mysql) {
die('replica mysql error: ' . mysqli_connect_error());
}
mysqli_select_db($mw_mysql, Config::$mw_mysql_db);
return $mw_mysql;
}
private static function runQuery($mw_mysql, string $identifier, string $context, string $sql)
{
global $logger;
try {
$res = mysqli_query($mw_mysql, $sql);
if ($res === false) {
$logger->warning("$identifier query returned no data for $context: " . mysqli_error($mw_mysql));
Metrics::increment('bot_mysql_mw_query_failures_total', [$identifier, 'no_data']);
return null;
}
Metrics::increment('bot_mysql_mw_query_total', [$identifier]);
return $res;
} catch (mysqli_sql_exception $e) {
if ($e->getCode() == 1969) {
$logger->warning("$identifier query timed out for $context");
Metrics::increment('bot_mysql_mw_query_failures_total', [$identifier, 'timeout']);
} else {
$logger->error("$identifier query returned an error for $context: " . $e->getMessage());
Metrics::increment('bot_mysql_mw_query_failures_total', [$identifier, 'error']);
}
return null;
}
}
private static function parseMwTimestamp(string $timestamp): int
{
return \DateTime::createFromFormat('YmdHis', $timestamp, new \DateTimeZone('UTC'))->getTimestamp();
}
private static function getPageMetadata($mw_mysql, $nsid, $title)
{
$res = self::runQuery(
$mw_mysql,
'page_metadata',
"$title ($nsid)",
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT `rev_timestamp`, `actor_name` FROM `revision`' .
' JOIN `actor_revision` ON `actor_id` = `rev_actor`' .
' WHERE `rev_id` = (' .
' SELECT MIN(`rev_id`) FROM `revision`' .
' JOIN `page` ON `page_id` = `rev_page`' .
' WHERE' .
' `page_namespace` = "' . mysqli_real_escape_string($mw_mysql, $nsid) . '"' .
' AND ' .
' `page_title` = "' . mysqli_real_escape_string($mw_mysql, $title) . '"' .
')'
);
$d = $res !== null ? mysqli_fetch_assoc($res) : null;
if ($d === null) {
return ['page_made_time' => null, 'creator' => null];
}
return [
'page_made_time' => self::parseMwTimestamp($d['rev_timestamp']),
'creator' => $d['actor_name'],
];
}
private static function getPageRecentEdits($mw_mysql, $nsid, $title, $timestamp)
{
$res = self::runQuery(
$mw_mysql,
'page_recent_edits',
"$title ($nsid) > $timestamp",
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT COUNT(*) as count FROM `page`' .
' JOIN `revision` ON `rev_page` = `page_id`' .
' WHERE `page_namespace` = "' .
mysqli_real_escape_string($mw_mysql, $nsid) .
'" AND `page_title` = "' .
mysqli_real_escape_string($mw_mysql, $title) .
'" AND `rev_timestamp` > "' .
mysqli_real_escape_string($mw_mysql, gmdate('YmdHis', $timestamp)) . '"'
);
if ($res === null) {
return null;
}
$d = mysqli_fetch_assoc($res);
return $d !== null ? $d['count'] : null;
}
private static function getPageRecentReverts($mw_mysql, $nsid, $title, $timestamp)
{
$res = self::runQuery(
$mw_mysql,
'page_recent_reverts',
"$title ($nsid) > $timestamp",
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT COUNT(*) as count FROM `page`' .
' JOIN `revision` ON `rev_page` = `page_id`' .
' JOIN `comment` ON `rev_comment_id` = `comment_id`' .
" WHERE `page_namespace` = '" .
mysqli_real_escape_string($mw_mysql, $nsid) .
"' AND `page_title` = '" .
mysqli_real_escape_string($mw_mysql, $title) .
"' AND `rev_timestamp` > '" .
mysqli_real_escape_string($mw_mysql, gmdate('YmdHis', $timestamp)) .
"' AND `comment_text` LIKE 'Revert%'"
);
if ($res === null) {
return null;
}
$d = mysqli_fetch_assoc($res);
return $d !== null ? $d['count'] : null;
}
private static function getUserRegistration($mw_mysql, $user)
{
$res = self::runQuery(
$mw_mysql,
'user_registration',
$user,
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT `user_registration`, `user_editcount` FROM `user` WHERE `user_name` = "' .
mysqli_real_escape_string($mw_mysql, $user) . '"'
);
$d = $res !== null ? mysqli_fetch_assoc($res) : null;
if ($d === null) {
return ['user_reg_time' => null, 'user_edit_count' => null];
}
return [
'user_reg_time' => $d['user_registration'] ? self::parseMwTimestamp($d['user_registration']) : null,
'user_edit_count' => $d['user_editcount'],
];
}
private static function getUserRegistrationViaRevision($mw_mysql, $user)
{
$res = self::runQuery(
$mw_mysql,
'user_registration_via_revision',
$user,
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT `rev_timestamp` FROM `revision_userindex` ' .
' JOIN `actor_revision` ON `actor_id` = `rev_actor`' .
' WHERE `actor_name` = "' .
mysqli_real_escape_string($mw_mysql, $user) . '" ORDER BY `rev_timestamp` LIMIT 0,1'
);
if ($res === null) {
return null;
}
$d = mysqli_fetch_assoc($res);
return $d !== null ? self::parseMwTimestamp($d['rev_timestamp']) : null;
}
private static function getUserWarningsCount($mw_mysql, $userPage)
{
$res = self::runQuery(
$mw_mysql,
'user_warnings_count',
$userPage,
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT COUNT(*) as count FROM `page`' .
' JOIN `revision` ON `rev_page` = `page_id`' .
' JOIN `comment` ON `rev_comment_id` = `comment_id`' .
" WHERE `page_namespace` = 3 AND `page_title` = '" .
mysqli_real_escape_string($mw_mysql, $userPage) .
"' AND (`comment_text` LIKE '%warning%' OR `comment_text`" .
" LIKE 'General note: Nonconstructive%')"
);
if ($res === null) {
return null;
}
$d = mysqli_fetch_assoc($res);
return $d !== null ? $d['count'] : null;
}
private static function getUserDistinctPages($mw_mysql, $userPage)
{
$res = self::runQuery(
$mw_mysql,
'user_distinct_pages',
$userPage,
'SET STATEMENT max_statement_time=120 FOR ' .
'SELECT count(distinct rev_page) AS count FROM' .
' `revision_userindex` JOIN `actor_revision` ON `actor_id` = `rev_actor`' .
" WHERE `actor_name` = '" . mysqli_real_escape_string($mw_mysql, $userPage) . "'"
);
if ($res === null) {
return null;
}
$d = mysqli_fetch_assoc($res);
return $d !== null ? $d['count'] : null;
}
public static function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
{
$mw_mysql = self::connect();
$userPage = str_replace(' ', '_', $user);
$title = str_replace(' ', '_', $title);
$pageMetadata = self::getPageMetadata($mw_mysql, $nsid, $title);
if ($pageMetadata['page_made_time'] === null) {
mysqli_close($mw_mysql);
return null;
}
$userRegistration = self::getUserRegistration($mw_mysql, $user);
if ($userRegistration['user_reg_time'] === null) {
$userRegistration['user_reg_time'] = self::getUserRegistrationViaRevision($mw_mysql, $user);
}
$data = [
'common' => [
'creator' => $pageMetadata['creator'],
'page_made_time' => $pageMetadata['page_made_time'],
'num_recent_edits' => self::getPageRecentEdits($mw_mysql, $nsid, $title, $timestamp),
'num_recent_reversions' => self::getPageRecentReverts($mw_mysql, $nsid, $title, $timestamp),
],
'user_reg_time' => $userRegistration['user_reg_time'],
'user_warns' => self::getUserWarningsCount($mw_mysql, $userPage),
'user_edit_count' => $userRegistration['user_edit_count'],
'user_distinct_pages' => self::getUserDistinctPages($mw_mysql, $userPage),
];
mysqli_close($mw_mysql);
return $data;
}
}