Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
InterwikiLinksTable
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
12 / 12
19
100.00% covered (success)
100.00%
1 / 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%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 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%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 isExisting
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isInNewSet
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 insertLink
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 deleteLink
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 virtualDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Deferred\LinksUpdate;
4
5use MediaWiki\Parser\ParserOutput;
6use MediaWiki\Parser\ParserOutputLinkTypes;
7
8/**
9 * iwlinks
10 *
11 * Link ID format: string[]
12 *    - 0: Interwiki prefix
13 *    - 1: Foreign title
14 *
15 * @since 1.38
16 */
17class InterwikiLinksTable extends LinksTable {
18    /** @var array */
19    private $newLinks = [];
20
21    /** @var array|null */
22    private $existingLinks;
23
24    public function setParserOutput( ParserOutput $parserOutput ) {
25        $this->newLinks = [];
26        foreach (
27            $parserOutput->getLinkList( ParserOutputLinkTypes::INTERWIKI )
28            as [ 'link' => $link ]
29        ) {
30            $this->newLinks[$link->getInterwiki()][$link->getDBkey()] = 1;
31        }
32    }
33
34    /** @inheritDoc */
35    protected function getTableName() {
36        return 'iwlinks';
37    }
38
39    /** @inheritDoc */
40    protected function getFromField() {
41        return 'iwl_from';
42    }
43
44    /** @inheritDoc */
45    protected function getExistingFields() {
46        return [ 'iwl_prefix', 'iwl_title' ];
47    }
48
49    /** @inheritDoc */
50    protected function getNewLinkIDs() {
51        foreach ( $this->newLinks as $prefix => $links ) {
52            foreach ( $links as $title => $unused ) {
53                yield [ (string)$prefix, (string)$title ];
54            }
55        }
56    }
57
58    /**
59     * Get the existing links as a 2-d array, with the prefix in the first key,
60     * the title in the second key, and the value arbitrary.
61     *
62     * @return array|null
63     */
64    private function getExistingLinks() {
65        if ( $this->existingLinks === null ) {
66            $this->existingLinks = [];
67            foreach ( $this->fetchExistingRows() as $row ) {
68                $this->existingLinks[$row->iwl_prefix][$row->iwl_title] = true;
69            }
70        }
71        return $this->existingLinks;
72    }
73
74    /** @inheritDoc */
75    protected function getExistingLinkIDs() {
76        foreach ( $this->getExistingLinks() as $prefix => $links ) {
77            foreach ( $links as $title => $unused ) {
78                yield [ (string)$prefix, (string)$title ];
79            }
80        }
81    }
82
83    /** @inheritDoc */
84    protected function isExisting( $linkId ) {
85        $links = $this->getExistingLinks();
86        [ $prefix, $title ] = $linkId;
87        return isset( $links[$prefix][$title] );
88    }
89
90    /** @inheritDoc */
91    protected function isInNewSet( $linkId ) {
92        [ $prefix, $title ] = $linkId;
93        return isset( $this->newLinks[$prefix][$title] );
94    }
95
96    /** @inheritDoc */
97    protected function insertLink( $linkId ) {
98        [ $prefix, $title ] = $linkId;
99        $this->insertRow( [
100            'iwl_prefix' => $prefix,
101            'iwl_title' => $title
102        ] );
103    }
104
105    /** @inheritDoc */
106    protected function deleteLink( $linkId ) {
107        [ $prefix, $title ] = $linkId;
108        $this->deleteRow( [
109            'iwl_prefix' => $prefix,
110            'iwl_title' => $title
111        ] );
112    }
113
114    /** @inheritDoc */
115    protected function virtualDomain() {
116        return self::VIRTUAL_DOMAIN;
117    }
118}