-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocess_functions.php
More file actions
221 lines (200 loc) · 8.76 KB
/
Copy pathprocess_functions.php
File metadata and controls
221 lines (200 loc) · 8.76 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
<?php
namespace CluebotNG;
/*
* 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 Process
{
private static $pendingChanges = [];
public static function pendingChangesTotal()
{
return count(self::$pendingChanges);
}
public static function processEdit($change)
{
global $logger;
Metrics::increment('bot_edits_received_total');
// Reload config from our 'special' pages
switch ($change['namespace'] . $change['title']) {
case 'User:' . Config::$user . '/Run':
$logger->info('Reloading /Run', ['revision_id' => $change['revid']]);
refreshRunFlag();
break;
case 'User:' . Config::$user . '/Optin':
$logger->info('Reloading /Optin', ['revision_id' => $change['revid']]);
Globals::$optin = Api::$q->getpage('User:' . Config::$user . '/Optin');
break;
case 'User:' . Config::$user . '/AngryOptin':
$logger->info('Reloading /AngryOptin', ['revision_id' => $change['revid']]);
Globals::$aoptin = Api::$q->getpage('User:' . Config::$user . '/AngryOptin');
break;
}
// Check this is an allowed namespace (same as for IRC)
if (
$change['namespace'] != 'Main:' and
!preg_match(
'/\* \[\[(' . preg_quote($change['namespace'] . $change['title'], '/') .
')\]\] \- .*/i',
Globals::$optin
)
) {
$logger->debug('Skipping due to namespace', ['revision_id' => $change['revid']]);
Metrics::increment('bot_edits_skipped_namespace_total', [$change['namespace']]);
return;
}
// Re-authenticate if required
if ((time() - Globals::$atime) >= 600) {
if (!Api::$a->loggedin()) {
$logger->warning('Lost authentication');
if (!Api::$a->login(Config::$user, Config::$pass)) {
$logger->error('Failed to re-authenticate');
die(); // Before we fork, this is the parent
}
}
Globals::$atime = time();
}
// Start actually processing things
$logger->info('Processing: ' . $change['namespace'] . $change['title'], ['revision_id' => $change['revid']]);
// Ignore new articles
if (in_array('N', $change['flags'])) {
$logger->info('Skipping: New article', ['revision_id' => $change['revid']]);
Metrics::increment('bot_edits_skipped_new_article_total');
return;
}
// Ignore whitelisted bots as early as possible - before starting to load any data
if (Action::isWhitelistedBot($change['user'])) {
$logger->info('Skipping: Bot whitelisted', ['revision_id' => $change['revid'], 'bot' => $change['user']]);
Metrics::increment('bot_edits_whitelisted_bot_total');
return;
}
self::$pendingChanges[] = $change;
self::dispatchPending();
}
public static function dispatchPending()
{
global $logger;
while (
!empty(self::$pendingChanges) &&
(Config::$max_forks <= 0 || count(Globals::$activeChildren) < Config::$max_forks)
) {
$change = array_shift(self::$pendingChanges);
$pid = pcntl_fork();
if ($pid == -1) {
$logger->error("Failed to fork");
die();
}
if ($pid != 0) {
// Parent
$logger->debug("Created fork with " . $pid);
Globals::$activeChildren[$pid] = true;
Metrics::set('bot_forks_total', count(Globals::$activeChildren));
continue;
}
// Child
$logger->debug("Fork started");
mt_srand();
Metrics::reset();
$change = parseFeedData($change);
if ($change === null) {
Metrics::increment('bot_edits_skipped_missing_data_total');
} else {
self::processEditThread($change);
}
$logger->debug("Fork finished");
// Avoid propagating shutdown signals from die() which cause curl's connection to get dropped
posix_kill(posix_getpid(), SIGKILL);
}
}
public static function processEditThread($change)
{
global $logger;
$score = null;
if (!isVandalism($change['all'], $score)) {
$logger->info('Skipping: Below threshold', ['revision_id' => $change['revid'], 'score' => $score]);
Metrics::increment('bot_edits_below_threshold_total');
Relay::publishEdit($change, $score, false, 'Below threshold');
return;
}
if (Action::isWhitelistedUser($change['user'])) {
$logger->info(
'Skipping: User whitelisted',
['revision_id' => $change['revid'], 'score' => $score, 'user' => $change['user']]
);
Metrics::increment('bot_edits_whitelisted_user_total');
Relay::publishEdit($change, $score, false, 'User whitelisted');
return;
}
Metrics::increment('bot_edits_vandalism_detected_total');
$reason = 'ANN scored at ' . $score;
$heuristic = '';
$diff = 'https://en.wikipedia.org/w/index.php' .
'?title=' . urlencode($change['title']) .
'&diff=' . urlencode($change['revid']) .
'&oldid=' . urlencode($change['old_revid']);
$report = '[[' . str_replace('File:', ':File:', $change['title']) . ']] was '
. '[' . $diff . ' changed] by '
. '[[Special:Contributions/' . $change['user'] . '|' . $change['user'] . ']] '
. '[[User:' . $change['user'] . '|(u)]] '
. '[[User talk:' . $change['user'] . '|(t)]] '
. $reason . ' on ' . gmdate('c');
$ircreport = "\x0315[[\x0307" . $change['title'] . "\x0315]] by \"\x0303" . $change['user'] .
"\x0315\" (\x0312 " . $change['url'] . " \x0315) \x0306" . $score . "\x0315 (";
$change['mysqlid'] = Db::detectedVandalism(
$change['user'],
$change['title'],
$heuristic,
$reason,
$change['url'],
$change['old_revid'],
$change['revid']
);
list($shouldRevert, $revertReason) = Action::shouldRevert($change);
Metrics::increment('bot_revert_decisions_total', [$shouldRevert ? 'yes' : 'no', $revertReason]);
if ($shouldRevert) {
$logger->notice(
'Reverting: ' . $revertReason,
['revision_id' => $change['revid'], 'score' => $score, 'user' => $change['user']]
);
Metrics::increment('bot_reverts_attempted_total');
$rbret = Action::doRevert($change, $score);
if ($rbret !== false) {
Metrics::increment('bot_reverts_succeeded_total');
Relay::publishEdit($change, $score, true, $revertReason);
Action::doWarn($change, $report);
Db::vandalismReverted($change['mysqlid']);
} else {
$rv2 = Api::$a->revisions($change['title'], 1);
if (!empty($rv2) && $change['user'] != $rv2[0]['user']) {
$logger->notice(
'Revert Beaten',
['revision_id' => $change['revid'], 'score' => $score, 'beaten_by' => $rv2[0]['user']]
);
Metrics::increment('bot_reverts_beaten_total');
Relay::publishEdit($change, $score, false, 'Beaten by ' . $rv2[0]['user']);
Db::vandalismRevertBeaten($change['mysqlid'], $change['title'], $rv2[0]['user'], $change['url']);
}
}
} else {
$logger->notice(
'Not Reverting: ' . $revertReason,
['revision_id' => $change['revid'], 'score' => $score, 'user' => $change['user']]
);
Relay::publishEdit($change, $score, false, $revertReason);
}
}
}