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 MediaWiki\Content\Content; |
| 6 | use MediaWiki\Deferred\LinksUpdate\LinksUpdate; |
| 7 | use MediaWiki\Extension\PageTriage\PageTriageUtil; |
| 8 | use MediaWiki\MediaWikiServices; |
| 9 | use MediaWiki\Page\WikiPage; |
| 10 | use MediaWiki\Parser\ParserOutput; |
| 11 | use MediaWiki\Revision\SlotRecord; |
| 12 | use Wikimedia\Rdbms\IDatabase; |
| 13 | use Wikimedia\Rdbms\IDBAccessObject; |
| 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 | /** @return bool */ |
| 61 | abstract public function compile(); |
| 62 | |
| 63 | /** |
| 64 | * @return array |
| 65 | */ |
| 66 | public function getMetadata() { |
| 67 | return $this->metadata; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Provide an estimated count for an item, for example: if $maxNumToProcess is |
| 72 | * 100 and the result is greater than 100, then the result should be 100+ |
| 73 | * @param int $pageId page id |
| 74 | * @param int $record Number of rows in query |
| 75 | * @param int $maxNumToProcess max number to process/display |
| 76 | * @param string $indexName the array index name to be saved |
| 77 | */ |
| 78 | protected function processEstimatedCount( int $pageId, int $record, int $maxNumToProcess, string $indexName ) { |
| 79 | if ( $record > $maxNumToProcess ) { |
| 80 | $this->metadata[$pageId][$indexName] = $maxNumToProcess . '+'; |
| 81 | } else { |
| 82 | $this->metadata[$pageId][$indexName] = $record; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Fill in zero for page with no estimated count |
| 88 | * @param string $indexName the array index name for the count |
| 89 | */ |
| 90 | protected function fillInZeroCount( $indexName ) { |
| 91 | foreach ( $this->mPageId as $pageId ) { |
| 92 | if ( !isset( $this->metadata[$pageId][$indexName] ) ) { |
| 93 | $this->metadata[$pageId][$indexName] = '0'; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param int $pageId |
| 100 | * |
| 101 | * @return WikiPage|null |
| 102 | */ |
| 103 | protected function getWikiPageByPageId( $pageId ): ?WikiPage { |
| 104 | // Try if there is an up-to-date wikipage object from article save |
| 105 | // else try to create a new one, this is important for replication delay |
| 106 | if ( isset( $this->articles[$pageId] ) ) { |
| 107 | $article = $this->articles[$pageId]; |
| 108 | } else { |
| 109 | if ( $this->componentDb === DB_PRIMARY ) { |
| 110 | $from = IDBAccessObject::READ_LATEST; |
| 111 | } else { |
| 112 | $from = IDBAccessObject::READ_NORMAL; |
| 113 | } |
| 114 | $article = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $pageId, $from ); |
| 115 | } |
| 116 | |
| 117 | return $article; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param int $pageId |
| 122 | * |
| 123 | * @return Content|null |
| 124 | */ |
| 125 | protected function getContentByPageId( $pageId ) { |
| 126 | // Prefer a preregistered Article, then a preregistered LinksUpdate |
| 127 | if ( isset( $this->articles[$pageId] ) ) { |
| 128 | return $this->articles[$pageId]->getContent(); |
| 129 | } |
| 130 | if ( isset( $this->linksUpdates[$pageId] ) ) { |
| 131 | $revRecord = $this->linksUpdates[$pageId]->getRevisionRecord(); |
| 132 | if ( $revRecord ) { |
| 133 | return $revRecord->getContent( SlotRecord::MAIN ); |
| 134 | } |
| 135 | } |
| 136 | // Fall back on creating a new WikiPage object and fetching from the DB |
| 137 | $wikiPage = $this->getWikiPageByPageId( $pageId ); |
| 138 | |
| 139 | return $wikiPage ? $wikiPage->getContent() : null; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param int $pageId |
| 144 | * @return bool|ParserOutput|null |
| 145 | */ |
| 146 | protected function getParserOutputByPageId( $pageId ) { |
| 147 | // Prefer a preregistered LinksUpdate |
| 148 | if ( isset( $this->linksUpdates[$pageId] ) ) { |
| 149 | return $this->linksUpdates[$pageId]->getParserOutput(); |
| 150 | } |
| 151 | // Fall back on WikiPage |
| 152 | $wikiPage = $this->getWikiPageByPageId( $pageId ); |
| 153 | if ( !$wikiPage ) { |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | return $wikiPage->getParserOutput( |
| 158 | $wikiPage->makeParserOptions( 'canonical' ) |
| 159 | ); |
| 160 | } |
| 161 | } |