Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.67% |
13 / 15 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TitleLinksTable | |
86.67% |
13 / 15 |
|
50.00% |
2 / 4 |
6.09 | |
0.00% |
0 / 1 |
| makePageReferenceValue | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getTargetIdField | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| makeTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deduplicateLinkIds | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getDeduplicatedLinkIds | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getPageReferenceIterator | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| fetchExistingRows | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Deferred\LinksUpdate; |
| 4 | |
| 5 | use MediaWiki\Page\PageReferenceValue; |
| 6 | use MediaWiki\Title\Title; |
| 7 | use Wikimedia\Rdbms\IResultWrapper; |
| 8 | |
| 9 | /** |
| 10 | * An abstract base class for tables that link to local titles. |
| 11 | * |
| 12 | * @stable to extend |
| 13 | * @since 1.38 |
| 14 | */ |
| 15 | abstract class TitleLinksTable extends LinksTable { |
| 16 | /** |
| 17 | * Convert a link ID to a PageReferenceValue |
| 18 | * |
| 19 | * @param mixed $linkId |
| 20 | * @return PageReferenceValue |
| 21 | */ |
| 22 | abstract protected function makePageReferenceValue( $linkId ): PageReferenceValue; |
| 23 | |
| 24 | /** |
| 25 | * Get the link target id (DB key) field name |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | abstract protected function getTargetIdField(); |
| 30 | |
| 31 | /** |
| 32 | * Convert a link ID to a Title |
| 33 | * |
| 34 | * @stable to override |
| 35 | * @param mixed $linkId |
| 36 | * @return Title |
| 37 | */ |
| 38 | protected function makeTitle( $linkId ): Title { |
| 39 | return Title::newFromPageReference( $this->makePageReferenceValue( $linkId ) ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Given an iterator over link IDs, remove links which go to the same |
| 44 | * title, leaving only one link per title. |
| 45 | * |
| 46 | * @param iterable<mixed> $linkIds |
| 47 | * @return iterable<mixed> |
| 48 | */ |
| 49 | abstract protected function deduplicateLinkIds( $linkIds ); |
| 50 | |
| 51 | /** |
| 52 | * Get link IDs for a given set type, filtering out duplicate links to the |
| 53 | * same title. |
| 54 | * |
| 55 | * @param int $setType |
| 56 | * @return iterable<mixed> |
| 57 | */ |
| 58 | protected function getDeduplicatedLinkIds( $setType ) { |
| 59 | $linkIds = $this->getLinkIDs( $setType ); |
| 60 | // Only the CHANGED set type should have duplicates |
| 61 | if ( $setType === self::CHANGED ) { |
| 62 | $linkIds = $this->deduplicateLinkIds( $linkIds ); |
| 63 | } |
| 64 | return $linkIds; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get a link set as an iterator over PageReferenceValue objects. |
| 69 | * |
| 70 | * @param int $setType |
| 71 | * @return iterable<PageReferenceValue> |
| 72 | * @phan-return \Traversable |
| 73 | */ |
| 74 | public function getPageReferenceIterator( $setType ) { |
| 75 | $linkIds = $this->getDeduplicatedLinkIds( $setType ); |
| 76 | foreach ( $linkIds as $linkId ) { |
| 77 | yield $this->makePageReferenceValue( $linkId ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** @inheritDoc */ |
| 82 | protected function fetchExistingRows(): IResultWrapper { |
| 83 | return $this->getReplicaDB()->newSelectQueryBuilder() |
| 84 | ->select( $this->getExistingFields() ) |
| 85 | ->from( $this->getTableName() ) |
| 86 | ->join( 'linktarget', null, [ $this->getTargetIdField() . '=lt_id' ] ) |
| 87 | ->where( $this->getFromConds() ) |
| 88 | ->caller( __METHOD__ ) |
| 89 | ->fetchResultSet(); |
| 90 | } |
| 91 | } |