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