-
Notifications
You must be signed in to change notification settings - Fork 6
/
ArticleMetadata.php
736 lines (624 loc) · 18.1 KB
/
ArticleMetadata.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<?php
/**
* Handles article metadata retrieval and saving to cache
*/
class ArticleMetadata {
protected $mPageId;
/**
* @param $pageId array - list of page id
* @param $validated bool - whether the page ids are validated
*/
public function __construct( array $pageId, $validated = true ) {
if ( $validated ) {
$this->mPageId = $pageId;
} else {
$this->mPageId = self::validatePageId( $pageId );
}
}
/**
* Delete all the metadata for an article
*
* @param $pageId - the page id to be deleted
*/
public function deleteMetadata() {
if ( $this->mPageId ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->delete(
'pagetriage_page_tags',
array( 'ptrpt_page_id' => $this->mPageId ),
__METHOD__,
array()
);
// also remove it from the cache
$this->flushMetadataFromCache();
}
return true;
}
/**
* Update the metadata in cache
* @param $update array - key => value pair for update
* @param $pageId int
*/
public function updateMetadataInCache( $update, $pageId = null ) {
global $wgMemc;
$keyPrefix = $this->memcKeyPrefix();
if ( $pageId ) {
$pageId = array( $pageId );
} else {
$pageId = $this->mPageId;
}
foreach ( $pageId as $val ) {
$data = $wgMemc->get( $keyPrefix . '-' . $val );
if ( $data !== false ) {
$wgMemc->replace( $keyPrefix . '-' . $val, array_merge( $data, $update ), 86400 );
}
}
}
/**
* Flush the metadata in cache
* @param $pageId - page id to be flushed, if null is provided, all
* page id in $this->mPageId will be flushed
*/
public function flushMetadataFromCache( $pageId = null ) {
global $wgMemc;
$keyPrefix = $this->memcKeyPrefix();
if ( is_null( $pageId ) ) {
foreach ( $this->mPageId as $pageId ) {
$wgMemc->delete( $keyPrefix . '-' . $pageId );
}
} else {
$wgMemc->delete( $keyPrefix . '-' . $pageId );
}
}
/**
* Set the metadata to cache
* @param $pageId int - page id
* @param $singleData mixed - data to be saved
*/
public function setMetadataToCache( $pageId, $singleData ) {
global $wgMemc;
$this->flushMetadataFromCache( $pageId );
$wgMemc->set( $this->memcKeyPrefix() . '-' . $pageId, $singleData, 86400 ); // 24 hours
}
/**
* Get the metadata from cache
* @param $pageId - the page id to get the cache data for, if null is provided
* all page id in $this->mPageId will be obtained
* @return array
*/
public function getMetadataFromCache( $pageId = null ) {
global $wgMemc;
$keyPrefix = $this->memcKeyPrefix();
if ( is_null( $pageId ) ) {
$metaData = array();
foreach ( $this->mPageId as $pageId ) {
$metaDataCache = $wgMemc->get( $keyPrefix . '-' . $pageId );
if ( $metaDataCache !== false ) {
$metaData[$pageId] = $metaDataCache;
}
}
return $metaData;
} else {
return $wgMemc->get( $keyPrefix . '-' . $pageId );
}
}
/**
* Return the prefix of memcache key for article metadata
* @return string
*/
protected function memcKeyPrefix() {
return wfMemcKey( 'article', 'metadata' );
}
/**
* Get the metadata for a single or list of articles
* @return array
*/
public function getMetadata() {
$articles = $this->mPageId;
$metaData = $this->getMetadataFromCache();
$articles = self::getPageWithoutMetadata( $articles, $metaData );
// Grab metadata from database after cache attempt
if ( $articles ) {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'pagetriage_page_tags', 'pagetriage_tags', 'pagetriage_page' ),
array( 'ptrpt_page_id', 'ptrt_tag_name', 'ptrpt_value', 'ptrp_reviewed', 'ptrp_created' ),
array( 'ptrpt_page_id' => $articles, 'ptrpt_tag_id = ptrt_tag_id', 'ptrpt_page_id = ptrp_page_id' ),
__METHOD__
);
$pageData = array();
foreach ( $res as $row ) {
$pageData[$row->ptrpt_page_id][$row->ptrt_tag_name] = $row->ptrpt_value;
if ( !isset( $pageData[$row->ptrpt_page_id]['creation_date'] ) ) {
$pageData[$row->ptrpt_page_id]['creation_date'] = $row->ptrp_created;
$pageData[$row->ptrpt_page_id]['patrol_status'] = $row->ptrp_reviewed;
}
}
$articles = self::getPageWithoutMetadata( $articles, $pageData );
// Compile the data if it is not available
if ( $articles ) {
$acp = ArticleCompileProcessor::newFromPageId( $articles );
if ( $acp ) {
$pageData += $acp->compileMetadata();
}
}
foreach ( $pageData as $pageId => $val ) {
$this->setMetadataToCache( $pageId, $val );
}
$metaData += $pageData;
}
return $metaData;
}
/**
* Get the pages without metadata yet
*/
private static function getPageWithoutMetadata( $articles, $data ) {
foreach ( $articles as $key => $pageId ) {
if ( isset( $data[$pageId] ) ) {
unset( $articles[$key] );
}
}
return $articles;
}
/**
* Return a list of valid metadata
* @return array
*/
public static function getValidTags() {
static $tags = array();
if ( count( $tags ) > 0 ) {
return $tags;
}
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'pagetriage_tags' ),
array( 'ptrt_tag_id', 'ptrt_tag_name' ),
array( ),
__METHOD__
);
foreach ( $res as $row ) {
$tags[$row->ptrt_tag_name] = $row->ptrt_tag_id;
}
return $tags;
}
/**
* Typecast the value in page id array to int and verify that it's
* in page triage queue
* @param $pageIds array
* @return array
*/
public static function validatePageId( array $pageIds ) {
static $cache = array();
$cleanUp = array();
foreach ( $pageIds as $key => $val ) {
$casted = intval( $val );
if ( $casted ) {
if ( isset( $cache[$casted] ) ) {
if ( $cache[$casted] ) {
$cleanUp[] = $casted;
}
unset( $pageIds[$key] );
} else {
$pageIds[$key] = $casted;
$cahce[$casted] = false;
}
} else {
unset( $pageIds[$key] );
}
}
if ( $pageIds ) {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'pagetriage_page' ),
array( 'ptrp_page_id' ),
array( 'ptrp_page_id' => $pageIds ),
__METHOD__
);
foreach ( $res as $row ) {
$cleanUp[] = $row->ptrp_page_id;
$cache[$row->ptrp_page_id] = true;
}
}
return $cleanUp;
}
}
/**
* Compiling metadata for articles
*/
class ArticleCompileProcessor {
protected $component;
protected $mPageId;
protected $metadata;
protected $defaultMode;
/**
* @param $pageId array - list of page id
*/
private function __construct( $pageId ) {
$this->mPageId = $pageId;
$this->component = array(
'BasicData' => 'off',
'LinkCount' => 'off',
'CategoryCount' => 'off',
'Snippet' => 'off',
'UserData' => 'off',
'DeletionTag' => 'off'
);
$this->metadata = array_fill_keys( $this->mPageId, array() );
$this->defaultMode = true;
}
/**
* Factory for creating an instance
* @param $pageId array
* @param $validated bool - whether page ids are validated
* @return ArticleCompileProcessor|false
*/
public static function newFromPageId( array $pageId, $validated = true ) {
if ( !$validated ) {
$pageId = ArticleMetadata::validatePageId( $pageId );
}
if ( $pageId ) {
return new ArticleCompileProcessor( $pageId );
} else {
return false;
}
}
/**
* Register a component to the processor for compiling
* @param $component string
*/
public function registerComponent( $component ) {
if ( isset( $this->component[$component] ) ) {
$this->component[$component] = 'on';
$this->defaultMode = false;
}
}
/**
* Wrapper function for compiling the data
* @return array
*/
public function compileMetadata() {
$this->prepare();
$this->process();
$this->save();
return $this->metadata;
}
/**
* Set up the data before compiling
*/
protected function prepare() {
if ( $this->defaultMode ) {
foreach ( $this->component as $key => $val ) {
$this->component[$key] = 'on';
}
} else {
// These two set of data are related
if ( $this->component['CategoryCount'] == 'on' || $this->component['DeletionTag'] == 'on' ) {
$this->component['CategoryCount'] = 'on';
$this->component['DeletionTag'] = 'on';
}
}
}
/**
* Compile all of the registered components in order
*/
protected function process() {
$completed = array();
foreach ( $this->component as $key => $val ) {
if ( $val === 'on' ) {
$compClass = 'ArticleCompile' . $key;
$comp = new $compClass( $this->mPageId );
if ( !$comp->compile() ) {
break;
}
foreach ( $comp->getMetadata() as $pageId => $row ) {
$this->metadata[$pageId] += $row;
}
$completed[] = $key;
}
}
// Subtract deletion tags from category count
if ( in_array( 'CategoryCount', $completed ) ) {
$deletionTags = ArticleCompileDeletionTag::getDeletionTags();
foreach ( $this->metadata as $pageId => $row ) {
foreach( $deletionTags as $val ) {
if ( $this->metadata[$pageId][$val] ) {
$this->metadata[$pageId]['category_count'] -= 1;
}
}
if ( $this->metadata[$pageId]['category_count'] < 0 ) {
$this->metadata[$pageId]['category_count'] = '0';
}
}
}
}
/**
* Save the compiling result to database as well as cache
*/
protected function save() {
$dbw = wfGetDB( DB_MASTER );
$tags = ArticleMetadata::getValidTags();
foreach ( $this->metadata as $pageId => $data ) {
//Flush cache so a new copy of cache will be generated
$ArticleMetadata = new ArticleMetadata( array( $pageId ) );
$ArticleMetadata->flushMetadataFromCache();
//Make sure either all or none metadata for a single page_id
$dbw->begin();
foreach ( $data as $key => $val) {
if ( isset( $tags[$key] ) ) {
$row = array (
'ptrpt_page_id' => $pageId,
'ptrpt_tag_id' => $tags[$key],
'ptrpt_value' => $val
);
}
$dbw->replace( 'pagetriage_page_tags', array( 'ptrpt_page_id', 'ptrpt_tag_id' ), $row, __METHOD__ );
}
$pt = new PageTriage( $pageId );
$row = array( 'ptrp_tags_updated' => $dbw->timestamp( wfTimestampNow() ) );
if ( isset( $data['deleted'] ) ) {
$row['ptrp_deleted'] = $data['deleted'] ? '1' : '0';
}
$pt->update( $row );
$dbw->commit();
}
}
}
/**
* The following are private classes used by ArticleCompileProcessor
*/
abstract class ArticleCompileInterface {
protected $mPageId;
protected $metadata;
/**
* @param $pageId array
*/
public function __construct( array $pageId ) {
$this->mPageId = $pageId;
$this->metadata = array_fill_keys( $pageId, array() );
}
public abstract function compile();
public function getMetadata() {
return $this->metadata;
}
}
/**
* Article page length, creation date, number of edit, title, article triage status
*/
class ArticleCompileBasicData extends ArticleCompileInterface {
public function __construct( $pageId ) {
parent::__construct( $pageId );
}
public function compile() {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array ( 'page', 'revision', 'pagetriage_page' ),
array (
'page_id', 'page_namespace', 'page_title', 'page_len',
'COUNT(rev_id) AS rev_count', 'ptrp_reviewed',
'MIN(rev_timestamp) AS creation_date'
),
array ( 'page_id' => $this->mPageId, 'page_id = rev_page', 'page_id = ptrp_page_id'),
__METHOD__,
array ( 'GROUP BY' => 'page_id' )
);
foreach ( $res as $row ) {
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
$this->metadata[$row->page_id]['page_len'] = $row->page_len;
$this->metadata[$row->page_id]['rev_count'] = $row->rev_count;
$this->metadata[$row->page_id]['title'] = $title->getPrefixedText();
// The following data won't be saved into metadata since they are not metadata tags
// just for saving into cache later
$this->metadata[$row->page_id]['creation_date'] = $row->creation_date;
$this->metadata[$row->page_id]['patrol_status'] = $row->ptrp_reviewed;
}
if ( count( $this->metadata) == 0 ) {
return false;
} else {
return true;
}
}
}
/**
* Article link count
*/
class ArticleCompileLinkCount extends ArticleCompileInterface {
public function __construct( $pageId ) {
parent::__construct( $pageId );
}
public function compile() {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'page', 'pagelinks' ),
array( 'page_id', 'COUNT(pl_from) AS linkcount' ),
array(
'page_id' => $this->mPageId,
'page_namespace = pl_namespace',
'page_title = pl_title'
),
__METHOD__,
array ( 'GROUP BY' => 'page_id' )
);
foreach ( $res as $row ) {
$this->metadata[$row->page_id]['linkcount'] = $row->linkcount;
}
foreach ( $this->mPageId as $pageId ) {
if ( !isset( $this->metadata[$pageId]['linkcount'] ) ) {
$this->metadata[$pageId]['linkcount'] = '0';
}
}
return true;
}
}
/**
* Article category count
*/
class ArticleCompileCategoryCount extends ArticleCompileInterface {
public function __construct( $pageId ) {
parent::__construct( $pageId );
}
public function compile() {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'page', 'categorylinks' ),
array( 'page_id', 'COUNT(cl_to) AS category_count' ),
array( 'page_id' => $this->mPageId, 'page_id = cl_from' ),
__METHOD__,
array ( 'GROUP BY' => 'page_id' )
);
foreach ( $res as $row ) {
$this->metadata[$row->page_id]['category_count'] = $row->category_count;
}
foreach ( $this->mPageId as $pageId ) {
if ( !isset( $this->metadata[$pageId]['category_count'] ) ) {
$this->metadata[$pageId]['category_count'] = '0';
}
}
return true;
}
}
/**
* Article snippet
*/
class ArticleCompileSnippet extends ArticleCompileInterface {
public function __construct( $pageId ) {
parent::__construct( $pageId );
}
public function compile() {
$dbr = wfGetDB( DB_SLAVE );
// Article snippet
$res = $dbr->select(
array( 'text', 'revision', 'page' ),
array( 'page_id', 'old_text' ),
array( 'page_id' => $this->mPageId, 'page_latest = rev_id', 'rev_text_id = old_id' ),
__METHOD__
);
foreach ( $res as $row ) {
$this->metadata[$row->page_id]['snippet'] = self::generateArticleSnippet( $row->old_text );
}
return true;
}
/**
* Generate article snippet for listview from article text
* @param $text string - page text
* @return string
*/
public static function generateArticleSnippet( $text ) {
global $wgLang;
$text = strip_tags( $text );
$attempt = 1;
$openCurPos = strpos($text, '{{');
$closeCurPos = strpos($text, '}}');
while( $openCurPos !== false && $closeCurPos !== false && $openCurPos < $closeCurPos ) {
// replace all templates with empty string
$text = preg_replace( '/\{\{[^\{]((?!\{\{).)*?\}\}/is', '', $text );
$openCurPos = strpos($text, '{{');
$closeCurPos = strpos($text, '}}');
$attempt++;
// only try 5 nested levels at max
if ( $attempt > 5 ) {
break;
}
}
$text = trim( strip_tags( MessageCache::singleton()->parse( $text )->getText() ) );
// strip out non-useful data for snippet
$text = str_replace( array('{', '}', '[edit]' ), '', $text );
return $wgLang->truncate( $text, 150 );
}
}
/**
* Article User data
*/
class ArticleCompileUserData extends ArticleCompileInterface {
public function __construct( $pageId ) {
parent::__construct( $pageId );
}
public function compile() {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'revision' ),
array( 'MIN(rev_id) AS rev_id' ),
array( 'rev_page' => $this->mPageId ),
__METHOD__,
array( 'GROUP BY' => 'rev_page' )
);
$revId = array();
foreach ( $res as $row ) {
$revId[] = $row->rev_id;
}
$res = $dbr->select(
array( 'revision', 'user', 'ipblocks' ),
array(
'rev_page AS page_id', 'user_id', 'user_name',
'user_real_name', 'user_registration', 'user_editcount',
'ipb_id', 'rev_user_text'
),
array( 'rev_id' => $revId ),
__METHOD__,
array(),
array( 'user' => array(
'LEFT JOIN', 'rev_user = user_id' ),
'ipblocks' => array( 'LEFT JOIN', 'rev_user = ipb_user AND rev_user_text = ipb_address' )
)
);
foreach ( $res as $row ) {
if ( $row->user_id ) {
$user = User::newFromRow( $row );
$this->metadata[$row->page_id]['user_id'] = $user->getId();
$this->metadata[$row->page_id]['user_name'] = $user->getName();
$this->metadata[$row->page_id]['user_editcount'] = $user->getEditCount();
$this->metadata[$row->page_id]['user_creation_date'] = wfTimestamp( TS_MW, $user->getRegistration() );
$this->metadata[$row->page_id]['user_autoconfirmed'] = $user->isAllowed( 'autoconfirmed' ) ? '1' : '0';
$this->metadata[$row->page_id]['user_bot'] = $user->isAllowed( 'bot' ) ? '1' : '0';
$this->metadata[$row->page_id]['user_block_status'] = $row->ipb_id ? '1' : '0';
} else {
$this->metadata[$row->page_id]['user_id'] = 0;
$this->metadata[$row->page_id]['user_name'] = $row->rev_user_text;
$this->metadata[$row->page_id]['user_autoconfirmed'] = '0';
$this->metadata[$row->page_id]['user_bot'] = '0';
$this->metadata[$row->page_id]['user_block_status'] = $row->ipb_id ? '1' : '0';
}
}
return true;
}
}
/**
* Article Deletion Tag
*/
class ArticleCompileDeletionTag extends ArticleCompileInterface {
public function __construct( $pageId ) {
$this->mPageId = $pageId;
$this->metadata = array_fill_keys( $this->mPageId, array( 'deleted' => '0' ) );
}
public static function getDeletionTags() {
return array (
'All_articles_proposed_for_deletion' => 'prod_status',
'BLP_articles_proposed_for_deletion' => 'blp_prod_status',
'Candidates_for_speedy_deletion' => 'csd_status',
'Articles_for_deletion' => 'afd_status'
);
}
public function compile() {
$dbr = wfGetDB( DB_SLAVE );
$deletionTags = self::getDeletionTags();
$res = $dbr->select(
array( 'categorylinks' ),
array( 'cl_from AS page_id', 'cl_to' ),
array( 'cl_from' => $this->mPageId, 'cl_to' => array_keys( $deletionTags ) ),
__METHOD__
);
foreach ( $res as $row ) {
$this->metadata[$row->page_id][$deletionTags[$row->cl_to]] = '1';
// This won't be saved to metadata, only for later reference
$this->metadata[$row->page_id]['deleted'] = '1';
}
// Fill in 0 for page not tagged with any of these status
foreach ( $this->mPageId as $pageId ) {
foreach ( $deletionTags as $status ) {
if ( !isset( $this->metadata[$pageId][$status] ) ) {
$this->metadata[$pageId][$status] = '0';
}
}
}
return true;
}
}