-
Notifications
You must be signed in to change notification settings - Fork 6
/
PageTriageUtil.php
669 lines (597 loc) · 17.8 KB
/
PageTriageUtil.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
namespace MediaWiki\Extension\PageTriage;
use EchoEvent;
use Exception;
use ExtensionRegistry;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Extension\PageTriage\Api\ApiPageTriageList;
use MediaWiki\MediaWikiServices;
use ORES\Hooks\Helpers;
use RequestContext;
use Title;
use User;
use Wikimedia\ParamValidator\ParamValidator;
use WikiPage;
/**
* Utility class for PageTriage
*/
class PageTriageUtil {
/**
* Get whether or not a page needs triaging
*
* @param WikiPage $page
*
* @throws Exception
* @return mixed null if the page is not in the triage system,
* otherwise whether or not the page is unreviewed.
* Return convention is this way so that null and false are equivalent
* with a straight boolean test.
*/
public static function doesPageNeedTriage( WikiPage $page ) {
$pageId = $page->getId();
$dbr = wfGetDB( DB_REPLICA );
$row = $dbr->selectRow( 'pagetriage_page', 'ptrp_reviewed',
[ 'ptrp_page_id' => $pageId ],
__METHOD__
);
if ( !$row ) {
return null;
}
return !(bool)$row->ptrp_reviewed;
}
/**
* Get the IDs of applicable PageTriage namespaces.
* @return int[]
*/
public static function getNamespaces() {
$config = MediaWikiServices::getInstance()->getMainConfig();
$pageTriageDraftNamespaceId = $config->get( 'PageTriageDraftNamespaceId' );
$pageTriageNamespaces = $config->get( 'PageTriageNamespaces' );
// Add the Draft namespace if configured.
if ( $pageTriageDraftNamespaceId
&& !in_array( $pageTriageDraftNamespaceId, $pageTriageNamespaces )
) {
$pageTriageNamespaces[] = $pageTriageDraftNamespaceId;
}
return $pageTriageNamespaces;
}
/**
* Validate a page namespace ID.
* @param int $namespace The namespace ID to validate.
* @return int The provided namespace if valid, otherwise 0 (main namespace).
*/
public static function validatePageNamespace( $namespace ) {
$pageTriageNamespaces = static::getNamespaces();
if ( !in_array( $namespace, $pageTriageNamespaces ) ) {
$namespace = NS_MAIN;
}
return (int)$namespace;
}
/**
* Get a list of stat for unreviewed articles
* @param int $namespace
* @return array
*
* @todo - Limit the number of records by a timestamp filter, maybe 30 days etc,
* depends on the time the triage queue should look back for listview
*/
public static function getUnreviewedArticleStat( $namespace = 0 ) {
$namespace = self::validatePageNamespace( $namespace );
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$fname = __METHOD__;
return $cache->getWithSetCallback(
$cache->makeKey( 'pagetriage-unreviewed-pages-stat', $namespace ),
10 * $cache::TTL_MINUTE,
static function () use ( $namespace, $fname ) {
$dbr = wfGetDB( DB_REPLICA );
$table = [ 'pagetriage_page', 'page' ];
$conds = [
'ptrp_reviewed' => 0,
'page_id = ptrp_page_id',
// remove redirect from the unreviewd number per bug40540
'page_is_redirect' => 0,
'page_namespace' => $namespace
];
$res = $dbr->selectRow(
$table,
[ 'COUNT(ptrp_page_id) AS total', 'MIN(ptrp_created) AS oldest' ],
$conds,
$fname
);
$data = [ 'count' => 0, 'oldest' => '' ];
if ( $res ) {
$data['count'] = (int)$res->total;
$data['oldest'] = $res->oldest;
}
return $data;
},
[ 'version' => PageTriage::CACHE_VERSION ]
);
}
/**
* Get the number of pages based on the selected filters.
* @param array $filters Associative array of filter names/values.
* See ApiPageTriageStats->getAllowedParams() for possible values,
* which are the same that the ApiPageTriageList endpoint accepts.
* @return int Number of pages based on the selected filters
*/
public static function getArticleFilterStat( $filters ) {
if ( !isset( $filters['showreviewed'] ) && !isset( $filters['showunreviewed'] ) ) {
$filters['showunreviewed'] = 'showunreviewed';
}
return ApiPageTriageList::getPageIds( $filters, true );
}
/**
* Get number of reviewed articles in the past week
* @param int $namespace Namespace number
* @return array Stats to be returned
*/
public static function getReviewedArticleStat( $namespace = 0 ) {
$namespace = self::validatePageNamespace( $namespace );
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$fname = __METHOD__;
return $cache->getWithSetCallback(
$cache->makeKey( 'pagetriage-reviewed-pages-stat', $namespace ),
10 * $cache::TTL_MINUTE,
static function () use ( $namespace, $fname ) {
$time = (int)wfTimestamp( TS_UNIX ) - 7 * 24 * 60 * 60;
$dbr = wfGetDB( DB_REPLICA );
$table = [ 'pagetriage_page', 'page' ];
$conds = [
'ptrp_reviewed' => 1,
'page_id = ptrp_page_id',
'page_namespace' => $namespace,
'ptrp_reviewed_updated > ' . $dbr->addQuotes( $dbr->timestamp( $time ) )
];
$res = $dbr->selectRow(
$table,
[ 'COUNT(ptrp_page_id) AS reviewed_count' ],
$conds,
$fname
);
$data = [ 'reviewed_count' => 0 ];
if ( $res ) {
$data['reviewed_count'] = (int)$res->reviewed_count;
}
return $data;
},
[ 'version' => PageTriage::CACHE_VERSION ]
);
}
/**
* Get top page triagers in various time frame
* @param string $time time to look back for top triagers, possible values include
* last-day, last-week, last-month
* @return array
*/
public static function getTopTriagers( $time = 'last-week' ) {
$now = (int)wfTimestamp( TS_UNIX );
// times to look back for top trigers and expiration time in cache
$timeFrame = [
'last-day' => [ 'ts' => $now - 24 * 60 * 60, 'expire' => 60 * 60 ],
'last-week' => [ 'ts' => $now - 7 * 24 * 60 * 60, 'expire' => 24 * 60 * 60 ],
'last-month' => [ 'ts' => $now - 30 * 24 * 60 * 60, 'expire' => 24 * 60 * 60 ],
];
if ( !isset( $timeFrame[$time] ) ) {
$time = 'last-day';
}
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$fname = __METHOD__;
return $cache->getWithSetCallback(
$cache->makeKey( 'pagetriage-top-triager', $time ),
$timeFrame[$time]['expire'],
static function () use ( $timeFrame, $time, $fname ) {
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
[ 'pagetriage_log', 'user' ],
[ 'user_name', 'user_id', 'COUNT(ptrl_id) AS num' ],
[
'user_id = ptrl_user_id',
'ptrl_reviewed' => 1, // only reviewed status
'ptrl_timestamp > ' .
$dbr->addQuotes( $dbr->timestamp( $timeFrame[$time]['ts'] ) )
],
$fname,
[ 'GROUP BY' => [ 'user_name', 'user_id' ], 'ORDER BY' => 'num DESC', 'LIMIT' => 50 ]
);
return iterator_to_array( $res );
},
[ 'version' => PageTriage::CACHE_VERSION ]
);
}
/**
* returns the cache key for user status
* @param string $userName
* @return string
*/
public static function userStatusKey( $userName ) {
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
return $cache->makeKey(
'pagetriage-user-page-status',
sha1( $userName ),
PageTriage::CACHE_VERSION
);
}
/**
* Check the existance of user page and talk page for a list of users
* @param array $users contains user_name db keys
* @return array
*/
public static function pageStatusForUser( $users ) {
$return = [];
$title = [];
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
foreach ( $users as $user ) {
$user = (array)$user;
$searchKey = [ 'user_name', 'reviewer' ];
foreach ( $searchKey as $val ) {
if ( !isset( $user[$val] ) || !$user[$val] ) {
continue;
}
$data = $cache->get( self::userStatusKey( $user[$val] ) );
// data is in memcache
if ( $data !== false ) {
foreach ( $data as $pageKey => $status ) {
if ( $status === 1 ) {
$return[$pageKey] = $status;
}
}
// data is not in memcache and will be checked against database
} else {
$u = Title::newFromText( $user[$val], NS_USER );
if ( $u ) {
if ( isset( $title[$u->getDBkey()] ) ) {
continue;
}
$t = Title::makeTitle( NS_USER_TALK, $u->getDBkey() );
// store the data in $title, 'u' is for user page, 't' is for talk page
$title[$u->getDBkey()] = [ 'user_name' => $user[$val], 'u' => $u, 't' => $t ];
}
}
}
}
if ( $title ) {
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
[ 'page' ],
[ 'page_namespace', 'page_title' ],
[
'page_title' => array_map( 'strval', array_keys( $title ) ),
'page_namespace' => [ NS_USER, NS_USER_TALK ]
],
__METHOD__
);
$dataToCache = [];
// if there is result from the database, that means the page exists, set it to the
// cache array with value 1
foreach ( $res as $row ) {
$user = $title[$row->page_title];
if ( $row->page_namespace == NS_USER ) {
$dataToCache[$user['user_name']][$user['u']->getPrefixedDBkey()] = 1;
} else {
$dataToCache[$user['user_name']][$user['t']->getPrefixedDBkey()] = 1;
}
}
// Loop through the original $title array, set the data not in db result with value 0
// then save the cache value to memcache for next time use
foreach ( $title as $key => $value ) {
if ( !isset( $dataToCache[$value['user_name']][$value['u']->getPrefixedDBkey()] ) ) {
$dataToCache[$value['user_name']][$value['u']->getPrefixedDBkey()] = 0;
} else {
$return[$value['u']->getPrefixedDBkey()] = 1;
}
if ( !isset( $dataToCache[$value['user_name']][$value['t']->getPrefixedDBkey()] ) ) {
$dataToCache[$value['user_name']][$value['t']->getPrefixedDBkey()] = 0;
} else {
$return[$value['t']->getPrefixedDBkey()] = 1;
}
$cache->set(
self::userStatusKey( $value['user_name'] ),
$dataToCache[$value['user_name']],
3600
);
}
}
return $return;
}
/**
* Update user metadata when a user's block status is updated
* @param DatabaseBlock $block block object
* @param int $status 1/0
*/
public static function updateMetadataOnBlockChange( $block, $status = 1 ) {
// do instant update if the number of page to be updated is less or equal to
// the number below, otherwise, delay this to the cron
$maxNumToProcess = 500;
$tags = ArticleMetadata::getValidTags();
if ( !$tags ) {
return;
}
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
[ 'pagetriage_page_tags' ],
[ 'ptrpt_page_id' ],
[ 'ptrpt_tag_id' => $tags['user_name'], 'ptrpt_value' => $block->getTargetName() ],
__METHOD__,
[ 'LIMIT' => $maxNumToProcess + 1 ]
);
if ( $res->numRows() > $maxNumToProcess ) {
return;
}
$pageIds = [];
foreach ( $res as $row ) {
$pageIds[] = $row->ptrpt_page_id;
}
if ( !$pageIds ) {
return;
}
$dbw = wfGetDB( DB_PRIMARY );
$dbw->startAtomic( __METHOD__ );
$dbw->update(
'pagetriage_page_tags',
[ 'ptrpt_value' => $status ],
[ 'ptrpt_page_id' => $pageIds, 'ptrpt_tag_id' => $tags['user_block_status'] ],
__METHOD__
);
PageTriage::bulkSetTagsUpdated( $pageIds );
$dbw->endAtomic( __METHOD__ );
$metadata = new ArticleMetadata( $pageIds );
$metadata->flushMetadataFromCache();
}
/**
* Attempt to create an Echo notification event for
* 1. 'Mark as Reviewed' curation flyout
* 2. 'Mark as Patrolled' from Special:NewPages
* 3. 'Add maintenance tag' curation flyout
* 4. 'Add deletion tag' curation flyout
*
* @param Title $title
* @param User $user
* @param string $type notification type
* @param array|null $extra
*/
public static function createNotificationEvent( Title $title, $user, $type, $extra = null ) {
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) ) {
return;
}
$params = [
'type' => $type,
'title' => $title,
'agent' => $user,
];
if ( $extra ) {
$extra['note'] = self::truncateLongText( $extra['note'] );
$params['extra'] = $extra;
}
EchoEvent::create( $params );
}
/**
* @param string $text The text to truncate.
* @param int $length Maximum number of characters.
* @param string $ellipsis String to append to the end of truncated text.
* @return string
*/
public static function truncateLongText( $text, $length = 150, $ellipsis = '...' ) {
if ( !is_string( $text ) ) {
return $text;
}
return RequestContext::getMain()->getLanguage()->truncateForVisual( $text, $length, $ellipsis );
}
/**
* Get an array of ORES articlequality API parameters.
*
* @return array
*/
private static function getOresArticleQualityApiParams() {
return [
'show_predicted_class_stub' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_class_start' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_class_c' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_class_b' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_class_good' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_class_featured' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
];
}
/**
* Get an array of ORES draftquality API parameters.
*
* @return array
*/
private static function getOresDraftQualityApiParams() {
return [
'show_predicted_issues_vandalism' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_issues_spam' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_issues_attack' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
'show_predicted_issues_none' => [
ParamValidator::PARAM_TYPE => 'boolean'
],
];
}
/**
* Get an array of ORES API parameters.
*
* These are used in both NPP and AFC contexts.
*
* @return array
*/
public static function getOresApiParams() {
return self::getOresArticleQualityApiParams() + self::getOresDraftQualityApiParams();
}
/**
* Get array of common API parameters, for use with getAllowedParams().
*
* @return array
*/
public static function getCommonApiParams() {
return [
'showbots' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'showredirs' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'showothers' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'showreviewed' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'showunreviewed' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'showdeleted' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'namespace' => [
ParamValidator::PARAM_TYPE => 'integer',
],
'afc_state' => [
ParamValidator::PARAM_TYPE => 'integer',
],
'no_category' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'unreferenced' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'no_inbound_links' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'recreated' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'non_autoconfirmed_users' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'learners' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'blocked_users' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
'username' => [
ParamValidator::PARAM_TYPE => 'user',
],
'date_range_from' => [
ParamValidator::PARAM_TYPE => 'timestamp',
],
'date_range_to' => [
ParamValidator::PARAM_TYPE => 'timestamp',
],
];
}
/**
* Helper method to check if the API call includes ORES articlequality parameters.
*
* @param array $opts
* @return bool
*/
public static function isOresArticleQualityQuery( $opts ) {
return self::queryContains( $opts, self::getOresArticleQualityApiParams() );
}
/**
* Helper method to check if the API call includes ORES draftquality parameters.
*
* @param array $opts
* @return bool
*/
public static function isOresDraftQualityQuery( $opts ) {
return self::queryContains( $opts, self::getOresDraftQualityApiParams() );
}
/**
* Helper method to check if $opts contains some of the parameters in $params.
*
* @param array $opts Selected parameters from API request
* @param array $params
* @return bool
*/
private static function queryContains( $opts, $params ) {
$params = array_keys( $params );
foreach ( $params as $key ) {
if ( isset( $opts[ $key ] ) && $opts[ $key ] ) {
return true;
}
}
return false;
}
/**
* Convert ORES param names to class names.
*
* @param string $model Which model to convert names for ('articlequality' or 'draftquality')
* @param array $opts Selected parameters
* @return array Corresponding ORES class names
*/
public static function mapOresParamsToClassNames( $model, $opts ) {
$paramsToClassesMap = [
'articlequality' => [
'show_predicted_class_stub' => 'Stub',
'show_predicted_class_start' => 'Start',
'show_predicted_class_c' => 'C',
'show_predicted_class_b' => 'B',
'show_predicted_class_good' => 'GA',
'show_predicted_class_featured' => 'FA',
],
'draftquality' => [
'show_predicted_issues_vandalism' => 'vandalism',
'show_predicted_issues_spam' => 'spam',
'show_predicted_issues_attack' => 'attack',
'show_predicted_issues_none' => 'OK',
],
];
$result = [];
foreach ( $paramsToClassesMap[ $model ] as $param => $className ) {
if ( isset( $opts[ $param ] ) && $opts[ $param ] ) {
$result[] = $className;
}
}
return $result;
}
/**
* Check if the ORES extension is present and configured
* correctly for PageTriage to integrate with it.
*
* @return bool
*/
public static function oresIsAvailable() {
return ExtensionRegistry::getInstance()->isLoaded( 'ORES' ) &&
Helpers::isModelEnabled( 'articlequality' ) &&
Helpers::isModelEnabled( 'draftquality' );
}
/**
* @return array The copyvio filter parameter
*/
public static function getCopyvioApiParam() {
return [
'show_predicted_issues_copyvio' => [
ParamValidator::PARAM_TYPE => 'boolean',
],
];
}
/**
* Check if $opts contain the copyvio filter parameter
*
* @param array $opts
* @return bool
*/
public static function isCopyvioQuery( $opts ) {
return $opts[ 'show_predicted_issues_copyvio' ] ?? false;
}
}