-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlaggedCatCheck.php
270 lines (227 loc) · 7.78 KB
/
FlaggedCatCheck.php
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
#!/usr/bin/php
<?php
require_once('phpWBF.php');
require_once('database.phpWBF.php');
class hgzFlaggedCatCheck {
protected $mw;
protected $db;
protected $customers = [];
protected $config = [];
public function __construct() {
$this->mw = new phpWBF( 'de.wikipedia.org' );
$this->db = new phpWBFdatabase();
$this->mw->loginProfile( 'Hgzbot' );
$this->run();
}
public function __destruct() {
unset( $this->mw );
}
protected function getConfig() {
$json = $this->mw->mwRawGet(
'User:Hgzbot/Service/FlaggedCatCheck/config.json',
'application/json'
);
$this->config = json_decode( $json, true );
}
protected function getCustomers() {
// get transclusions of template
try {
$embd = $this->mw->getEmbeddingsContent(
$this->config['config']['transcludes'],
[]
);
} catch (Exception $e) {
throw new Exception( 'Error: No transclusions of template found.' );
}
foreach ($embd as $item) {
$text = $item['text'];
// categories to use
$match = [];
if ( preg_match( '/HGZ_FCC_CATEGORIES\s*\=\s*([^\n\|]+)/', $text, $match ) == 1 ) {
$cnfCatText = $match[1];
} else {
$this->mw->log( 'ERROR: Customer ' . $item['title'] . ': category pattern not found' );
continue;
}
// frequency
$match = [];
if ( preg_match( '/HGZ_FCC_FREQUENCY\s*\=\s*(\d{1,})/', $text, $match ) == 1 ) {
$cnfFrequency = $match[1];
} else {
$cnfFrequency = '24';
}
// last visit
$match = [];
if ( preg_match( '/HGZ_FCC_LASTVISIT\s*\=\s*(\d{10})/', $text, $match ) == 1 ) {
$cnfLastVisit = $match[1] . '00';
} else {
$cnfLastVisit = '200000000000';
}
// check last visited/frequency
if ( !isset( $_SERVER['argv'][1] ) || $_SERVER['argv'][1] !== 'forceupdate' ) {
$dtNow = time();
$dtLast = DateTime::createFromFormat( 'YmdHi', $cnfLastVisit );
$dtNext = strtotime( '+ ' . $cnfFrequency . ' hours', $dtLast->getTimestamp() );
if ( $dtNow <= $dtNext ) {
continue;
}
}
// split categories
$cnfCategories = [];
$match = [];
if ( preg_match_all( '/([^\n\|\{#]+)(\{(\d{1,2})\})?\#?/', $cnfCatText, $match, PREG_SET_ORDER ) > 0 ) {
foreach ( $match as $m ) {
if ( !isset( $m[1] ) ) {
continue;
}
$cnfCategories[] = str_replace( ' ', '_', $m[1] );
}
} else {
$this->mw->log( 'ERROR: Customer ' . $item['title'] . ': invalid category pattern' );
continue;
}
$this->customers[] = [
'title' => $item['title'],
'frequency' => $cnfFrequency,
'last' => $cnfLastVisit,
'categories' => $cnfCategories
];
}
$this->mw->log( 'Found ' . count($this->customers) . ' customers.' );
}
protected function processCustomers() {
$limit = $this->config['config']['maxEntries'];
foreach ( $this->customers as $customer ) {
$pages = [];
$output = "\r\n";
$count = 0;
$this->mw->log( 'Processing ' . $customer['title'] . '... pages from categories...' );
foreach ( $customer['categories'] as $cat ) {
// unreviewed pages
$t1 = '';
$t1 .= 'WITH RECURSIVE Cat AS (';
$t1 .= ' SELECT page_title, page_id';
$t1 .= ' FROM page';
$t1 .= ' WHERE page_title = ?';
$t1 .= ' AND page_namespace = 14';
$t1 .= ' UNION';
$t1 .= ' SELECT Subcat.page_title, Subcat.page_id';
$t1 .= ' FROM page AS Subcat, categorylinks, Cat';
$t1 .= ' WHERE Subcat.page_namespace = 14';
$t1 .= ' AND cl_from = Subcat.page_id';
$t1 .= ' AND cl_to = Cat.page_title';
$t1 .= ' AND cl_type = "subcat"';
$t1 .= ')';
$t1 .= 'SELECT DISTINCT Art.page_title AS "page"';
$t1 .= ' FROM Cat';
$t1 .= ' INNER JOIN categorylinks AS Catlinks ON Cat.page_title = Catlinks.cl_to';
$t1 .= ' INNER JOIN page AS Art ON Catlinks.cl_from = Art.page_id';
$t1 .= ' WHERE Art.page_namespace = 0';
$t1 .= ' AND NOT EXISTS (';
$t1 .= ' SELECT fp_page_id FROM flaggedpages WHERE fp_page_id = Art.page_id';
$t1 .= ' )';
$q1 = $this->db->executeDBQuery( $t1, 's', $cat );
$r1 = phpWBFdatabase::fetchDBQueryResult( $q1 );
foreach ( $r1 as $l1 ) {
$add = [];
$add['title'] = str_replace( '_', ' ', $l1['page'] );
$add['since'] = false;
$pages[] = $add;
}
$q1->close();
// pending changes
$t2 = '';
$t2 .= 'WITH RECURSIVE Cat AS (';
$t2 .= ' SELECT page_title, page_id';
$t2 .= ' FROM page';
$t2 .= ' WHERE page_title = ?';
$t2 .= ' AND page_namespace = 14';
$t2 .= ' UNION';
$t2 .= ' SELECT Subcat.page_title, Subcat.page_id';
$t2 .= ' FROM page AS Subcat, categorylinks, Cat';
$t2 .= ' WHERE Subcat.page_namespace = 14';
$t2 .= ' AND cl_from = Subcat.page_id';
$t2 .= ' AND cl_to = Cat.page_title';
$t2 .= ' AND cl_type = "subcat"';
$t2 .= ')';
$t2 .= 'SELECT DISTINCT Art.page_title AS "page", Flag.fp_pending_since AS "since"';
$t2 .= ' FROM Cat';
$t2 .= ' INNER JOIN categorylinks AS Catlinks ON Cat.page_title = Catlinks.cl_to';
$t2 .= ' INNER JOIN page AS Art ON Catlinks.cl_from = Art.page_id';
$t2 .= ' INNER JOIN flaggedpages AS Flag ON Flag.fp_page_id = Art.page_id';
$t2 .= ' WHERE Art.page_namespace = 0';
$t2 .= ' AND Flag.fp_pending_since IS NOT NULL';
$t2 .= ' ORDER BY since';
$q2 = $this->db->executeDBQuery( $t2, 's', $cat );
$r2 = phpWBFdatabase::fetchDBQueryResult( $q2 );
foreach ( $r2 as $l2 ) {
$add = [];
$add['title'] = str_replace( '_', ' ', $l2['page'] );
$add['since'] = $l2['since'];
$pages[] = $add;
}
$q2->close();
}
$pages = $this->mw->uniqueMultidimArray($pages, 'title');
$pages = $this->mw->sortMultidimArray($pages, 'since', SORT_ASC);
$this->mw->log( 'Processing ' . $customer['title'] . '... get output...' );
foreach ( $pages as $page ) {
$output .= str_replace( '$1', $page['title'], $this->config['config']['outputPage'] ) . ' ';
if ($page['since'] === false) {
$output .= $this->config['config']['outputUnreviewed'];
} else {
$since = DateTime::createFromFormat( 'YmdHis', $page['since'] );
$now = new DateTime();
$diff = $since->diff( $now );
$text = $diff->format( $this->config['config']['pendingSinceFormat'] );
$output .= str_replace(
['$1', '$2'],
[$page['title'], $text],
$this->config['config']['outputPending']
);
}
$output .= "\r\n";
$count++;
if ( $count == $limit ) {
break;
}
}
$this->mw->log( 'Processing ' . $customer['title'] . '... update page...' );
$newtext = $this->mw->getWikitext( $customer['title'] );
$newtext = preg_replace(
'/<!--\s*HGZ_FCC_START\s*-->(.+)<!--\s*HGZ_FCC_END\s*-->/s', '<!-- HGZ_FCC_START -->' . $output . '<!-- HGZ_FCC_END -->',
$newtext,
1
);
$newtext = preg_replace(
'/HGZ_FCC_LASTVISIT\s*\=\s*([^\n\|\}])+/', 'HGZ_FCC_LASTVISIT = ' . date('YmdHi', time()),
$newtext,
1
);
$this->mw->editPage(
$customer['title'],
$newtext,
str_replace(
['$1', '$2'],
[$count, count( $pages )],
$this->config['config']['summary']
)
);
unset( $pages );
}
}
protected function run() {
$this->getConfig();
if ( $this->config['enabled'] === true ) {
$this->db->connectWMCSReplica(
$this->config['connect']['cluster'],
$this->config['connect']['database']
);
$this->getCustomers();
$this->processCustomers();
}
$this->mw->logoutUser();
}
}
$instance = new HgzFlaggedCatCheck();
?>