Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.14% covered (success)
97.14%
34 / 35
90.91% covered (success)
90.91%
10 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
LangLinksTable
97.14% covered (success)
97.14%
34 / 35
90.91% covered (success)
90.91%
10 / 11
19
0.00% covered (danger)
0.00%
0 / 1
 setParserOutput
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isInNewSet
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 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%
3 / 3
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 * langlinks
10 *
11 * Link ID format: string[]
12 *   - 0: Language code
13 *   - 1: Foreign title
14 *
15 * @since 1.38
16 */
17class LangLinksTable extends LinksTable {
18    /** @var string[] */
19    private $newLinks = [];
20    /** @var string[]|null */
21    private $existingLinks;
22
23    public function setParserOutput( ParserOutput $parserOutput ) {
24        // Convert the format of the interlanguage links
25        $this->newLinks = [];
26        foreach (
27            $parserOutput->getLinkList( ParserOutputLinkTypes::LANGUAGE )
28            as [ 'link' => $link ]
29        ) {
30            $key = $link->getInterwiki();
31            $title = $link->getText();
32            if ( $link->hasFragment() ) {
33                $title .= '#' . $link->getFragment();
34            }
35            // Ensure that the "first" link has precedence (T26502) although
36            // ParserOutput::addLanguageLink() should ensure we don't see dups
37            $this->newLinks[$key] ??= $title;
38        }
39    }
40
41    /** @inheritDoc */
42    protected function getTableName() {
43        return 'langlinks';
44    }
45
46    /** @inheritDoc */
47    protected function getFromField() {
48        return 'll_from';
49    }
50
51    /** @inheritDoc */
52    protected function getExistingFields() {
53        return [ 'll_lang', 'll_title' ];
54    }
55
56    /** @inheritDoc */
57    protected function getNewLinkIDs() {
58        foreach ( $this->newLinks as $key => $title ) {
59            yield [ (string)$key, $title ];
60        }
61    }
62
63    /**
64     * Get the existing links as an array where the key is the language code
65     * and the value is the title of the target in that language.
66     *
67     * @return array
68     */
69    private function getExistingLinks() {
70        if ( $this->existingLinks === null ) {
71            $this->existingLinks = [];
72            foreach ( $this->fetchExistingRows() as $row ) {
73                $this->existingLinks[$row->ll_lang] = $row->ll_title;
74            }
75        }
76        return $this->existingLinks;
77    }
78
79    /** @inheritDoc */
80    protected function getExistingLinkIDs() {
81        foreach ( $this->getExistingLinks() as $lang => $title ) {
82            yield [ (string)$lang, $title ];
83        }
84    }
85
86    /** @inheritDoc */
87    protected function isExisting( $linkId ) {
88        $links = $this->getExistingLinks();
89        [ $lang, $title ] = $linkId;
90        return \array_key_exists( $lang, $links )
91            && $links[$lang] === $title;
92    }
93
94    /** @inheritDoc */
95    protected function isInNewSet( $linkId ) {
96        [ $lang, $title ] = $linkId;
97        return \array_key_exists( $lang, $this->newLinks )
98            && $this->newLinks[$lang] === $title;
99    }
100
101    /** @inheritDoc */
102    protected function insertLink( $linkId ) {
103        [ $lang, $title ] = $linkId;
104        $this->insertRow( [
105            'll_lang' => $lang,
106            'll_title' => $title
107        ] );
108    }
109
110    /** @inheritDoc */
111    protected function deleteLink( $linkId ) {
112        $this->deleteRow( [
113            'll_lang' => $linkId[0]
114        ] );
115    }
116}