Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.49% covered (warning)
86.49%
32 / 37
82.35% covered (warning)
82.35%
14 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageLinksTable
86.49% covered (warning)
86.49%
32 / 37
82.35% covered (warning)
82.35%
14 / 17
24.31
0.00% covered (danger)
0.00%
0 / 1
 setParserOutput
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getTableName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFromField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExistingFields
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNewLinkIDs
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getExistingLinks
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getExistingLinkIDs
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isExisting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isInNewSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needForcedLinkRefresh
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 insertLink
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 deleteLink
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makePageReferenceValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deduplicateLinkIds
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 finishUpdate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 invalidateImageDescriptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Deferred\LinksUpdate;
4
5use MediaWiki\DAO\WikiAwareEntity;
6use MediaWiki\Page\PageReferenceValue;
7use MediaWiki\Parser\ParserOutput;
8use MediaWiki\Parser\ParserOutputLinkTypes;
9use MediaWiki\Title\Title;
10use PurgeJobUtils;
11
12/**
13 * imagelinks
14 *
15 * Link ID format: string image name
16 *
17 * @since 1.38
18 */
19class ImageLinksTable extends TitleLinksTable {
20    /**
21     * @var array New links with the name in the key, value arbitrary
22     */
23    private $newLinks;
24
25    /**
26     * @var array Existing links with the name in the key, value arbitrary
27     */
28    private $existingLinks;
29
30    public function setParserOutput( ParserOutput $parserOutput ) {
31        // Convert the format of the local links
32        $this->newLinks = [];
33        foreach (
34            $parserOutput->getLinkList( ParserOutputLinkTypes::MEDIA )
35            as [ 'link' => $link ]
36        ) {
37            $this->newLinks[$link->getDBkey()] = 1;
38        }
39    }
40
41    protected function getTableName() {
42        return 'imagelinks';
43    }
44
45    protected function getFromField() {
46        return 'il_from';
47    }
48
49    protected function getExistingFields() {
50        return [ 'il_to' ];
51    }
52
53    protected function getNewLinkIDs() {
54        foreach ( $this->newLinks as $link => $unused ) {
55            yield (string)$link;
56        }
57    }
58
59    /**
60     * Get existing links with the name in the key, value arbitrary.
61     *
62     * @return array
63     */
64    private function getExistingLinks() {
65        if ( $this->existingLinks === null ) {
66            $this->existingLinks = [];
67            foreach ( $this->fetchExistingRows() as $row ) {
68                $this->existingLinks[$row->il_to] = true;
69            }
70        }
71        return $this->existingLinks;
72    }
73
74    protected function getExistingLinkIDs() {
75        foreach ( $this->getExistingLinks() as $link => $unused ) {
76            yield (string)$link;
77        }
78    }
79
80    protected function isExisting( $linkId ) {
81        return \array_key_exists( $linkId, $this->getExistingLinks() );
82    }
83
84    protected function isInNewSet( $linkId ) {
85        return \array_key_exists( $linkId, $this->newLinks );
86    }
87
88    protected function needForcedLinkRefresh() {
89        return $this->isCrossNamespaceMove();
90    }
91
92    protected function insertLink( $linkId ) {
93        $this->insertRow( [
94            'il_from_namespace' => $this->getSourcePage()->getNamespace(),
95            'il_to' => $linkId
96        ] );
97    }
98
99    protected function deleteLink( $linkId ) {
100        $this->deleteRow( [ 'il_to' => $linkId ] );
101    }
102
103    protected function makePageReferenceValue( $linkId ): PageReferenceValue {
104        return new PageReferenceValue( NS_FILE, $linkId, WikiAwareEntity::LOCAL );
105    }
106
107    protected function makeTitle( $linkId ): Title {
108        return Title::makeTitle( NS_FILE, $linkId );
109    }
110
111    protected function deduplicateLinkIds( $linkIds ) {
112        if ( !is_array( $linkIds ) ) {
113            $linkIds = iterator_to_array( $linkIds );
114        }
115        return array_unique( $linkIds );
116    }
117
118    protected function finishUpdate() {
119        // A update of namespace on cross namespace move is detected as insert + delete,
120        // but the updates are not needed there.
121        $allInsertedLinks = array_column( $this->insertedLinks, 0 );
122        $allDeletedLinks = array_column( $this->deletedLinks, 0 );
123        $insertedLinks = array_diff( $allInsertedLinks, $allDeletedLinks );
124        $deletedLinks = array_diff( $allDeletedLinks, $allInsertedLinks );
125
126        $this->invalidateImageDescriptions( $insertedLinks, $deletedLinks );
127    }
128
129    /**
130     * Invalidate all image description pages which had links added or removed
131     * @param array $insertedLinks
132     * @param array $deletedLinks
133     */
134    private function invalidateImageDescriptions( array $insertedLinks, array $deletedLinks ) {
135        PurgeJobUtils::invalidatePages(
136            $this->getDB(), NS_FILE,
137            array_merge( $insertedLinks, $deletedLinks ) );
138    }
139}