Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
ArticleCompile | |
0.00% |
0 / 38 |
|
0.00% |
0 / 7 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
compile | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getMetadata | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
processEstimatedCount | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
fillInZeroCount | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getWikiPageByPageId | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getContentByPageId | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
getParserOutputByPageId | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\PageTriage\ArticleCompile; |
4 | |
5 | use Content; |
6 | use IDBAccessObject; |
7 | use MediaWiki\Deferred\LinksUpdate\LinksUpdate; |
8 | use MediaWiki\Extension\PageTriage\PageTriageUtil; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Parser\ParserOutput; |
11 | use MediaWiki\Revision\SlotRecord; |
12 | use Wikimedia\Rdbms\IDatabase; |
13 | use WikiPage; |
14 | |
15 | /** |
16 | * The abstract class extended in each ArticleCompile, used by ArticleCompileProcessor. |
17 | */ |
18 | abstract class ArticleCompile { |
19 | /** @var int[] List of page IDs */ |
20 | protected $mPageId; |
21 | |
22 | /** @var array */ |
23 | protected $metadata; |
24 | |
25 | /** @var WikiPage[] */ |
26 | protected $articles; |
27 | |
28 | /** @var LinksUpdate[] */ |
29 | protected $linksUpdates; |
30 | |
31 | /** @var IDatabase */ |
32 | protected $db; |
33 | |
34 | /** @var int Either DB_PRIMARY or DB_REPLICA */ |
35 | protected $componentDb; |
36 | |
37 | /** |
38 | * @param int[] $pageId |
39 | * @param int $componentDb |
40 | * @param WikiPage[] $articles |
41 | * @param LinksUpdate[] $linksUpdates |
42 | */ |
43 | public function __construct( |
44 | array $pageId, $componentDb, array $articles, array $linksUpdates |
45 | ) { |
46 | $this->mPageId = $pageId; |
47 | $this->metadata = array_fill_keys( $pageId, [] ); |
48 | $this->articles = $articles; |
49 | $this->linksUpdates = $linksUpdates; |
50 | |
51 | if ( $componentDb == DB_PRIMARY ) { |
52 | $this->db = PageTriageUtil::getPrimaryConnection(); |
53 | } else { |
54 | $this->db = PageTriageUtil::getReplicaConnection(); |
55 | } |
56 | |
57 | $this->componentDb = $componentDb; |
58 | } |
59 | |
60 | abstract public function compile(); |
61 | |
62 | /** |
63 | * @return array |
64 | */ |
65 | public function getMetadata() { |
66 | return $this->metadata; |
67 | } |
68 | |
69 | /** |
70 | * Provide an estimated count for an item, for example: if $maxNumToProcess is |
71 | * 100 and the result is greater than 100, then the result should be 100+ |
72 | * @param int $pageId page id |
73 | * @param int $record Number of rows in query |
74 | * @param int $maxNumToProcess max number to process/display |
75 | * @param string $indexName the array index name to be saved |
76 | */ |
77 | protected function processEstimatedCount( int $pageId, int $record, int $maxNumToProcess, string $indexName ) { |
78 | if ( $record > $maxNumToProcess ) { |
79 | $this->metadata[$pageId][$indexName] = $maxNumToProcess . '+'; |
80 | } else { |
81 | $this->metadata[$pageId][$indexName] = $record; |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * Fill in zero for page with no estimated count |
87 | * @param string $indexName the array index name for the count |
88 | */ |
89 | protected function fillInZeroCount( $indexName ) { |
90 | foreach ( $this->mPageId as $pageId ) { |
91 | if ( !isset( $this->metadata[$pageId][$indexName] ) ) { |
92 | $this->metadata[$pageId][$indexName] = '0'; |
93 | } |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * @param int $pageId |
99 | * |
100 | * @return WikiPage|null |
101 | */ |
102 | protected function getWikiPageByPageId( $pageId ): ?WikiPage { |
103 | // Try if there is an up-to-date wikipage object from article save |
104 | // else try to create a new one, this is important for replication delay |
105 | if ( isset( $this->articles[$pageId] ) ) { |
106 | $article = $this->articles[$pageId]; |
107 | } else { |
108 | if ( $this->componentDb === DB_PRIMARY ) { |
109 | $from = IDBAccessObject::READ_LATEST; |
110 | } else { |
111 | $from = IDBAccessObject::READ_NORMAL; |
112 | } |
113 | $article = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $pageId, $from ); |
114 | } |
115 | |
116 | return $article; |
117 | } |
118 | |
119 | /** |
120 | * @param int $pageId |
121 | * |
122 | * @return Content|null |
123 | */ |
124 | protected function getContentByPageId( $pageId ) { |
125 | // Prefer a preregistered Article, then a preregistered LinksUpdate |
126 | if ( isset( $this->articles[$pageId] ) ) { |
127 | return $this->articles[$pageId]->getContent(); |
128 | } |
129 | if ( isset( $this->linksUpdates[$pageId] ) ) { |
130 | $revRecord = $this->linksUpdates[$pageId]->getRevisionRecord(); |
131 | if ( $revRecord ) { |
132 | return $revRecord->getContent( SlotRecord::MAIN ); |
133 | } |
134 | } |
135 | // Fall back on creating a new WikiPage object and fetching from the DB |
136 | $wikiPage = $this->getWikiPageByPageId( $pageId ); |
137 | |
138 | return $wikiPage ? $wikiPage->getContent() : null; |
139 | } |
140 | |
141 | /** |
142 | * @param int $pageId |
143 | * @return bool|ParserOutput|null |
144 | */ |
145 | protected function getParserOutputByPageId( $pageId ) { |
146 | // Prefer a preregistered LinksUpdate |
147 | if ( isset( $this->linksUpdates[$pageId] ) ) { |
148 | return $this->linksUpdates[$pageId]->getParserOutput(); |
149 | } |
150 | // Fall back on WikiPage |
151 | $wikiPage = $this->getWikiPageByPageId( $pageId ); |
152 | if ( !$wikiPage ) { |
153 | return null; |
154 | } |
155 | |
156 | return $wikiPage->getParserOutput( |
157 | $wikiPage->makeParserOptions( 'canonical' ) |
158 | ); |
159 | } |
160 | } |