Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
LangLinksTable
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
11 / 11
18
100.00% covered (success)
100.00%
1 / 1
 setParserOutput
100.00% covered (success)
100.00%
5 / 5
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%
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;
6
7/**
8 * langlinks
9 *
10 * Link ID format: string[]
11 *   - 0: Language code
12 *   - 1: Foreign title
13 *
14 * @since 1.38
15 */
16class LangLinksTable extends LinksTable {
17    private $newLinks = [];
18    private $existingLinks;
19
20    public function setParserOutput( ParserOutput $parserOutput ) {
21        // Convert the format of the interlanguage links
22        // I didn't want to change it in the ParserOutput, because that array is passed all
23        // the way back to the skin, so either a skin API break would be required, or an
24        // inefficient back-conversion.
25        $ill = $parserOutput->getLanguageLinks();
26        $this->newLinks = [];
27        foreach ( $ill as $link ) {
28            [ $key, $title ] = explode( ':', $link, 2 );
29            $this->newLinks[$key] = $title;
30        }
31    }
32
33    protected function getTableName() {
34        return 'langlinks';
35    }
36
37    protected function getFromField() {
38        return 'll_from';
39    }
40
41    protected function getExistingFields() {
42        return [ 'll_lang', 'll_title' ];
43    }
44
45    protected function getNewLinkIDs() {
46        foreach ( $this->newLinks as $key => $title ) {
47            yield [ (string)$key, $title ];
48        }
49    }
50
51    /**
52     * Get the existing links as an array where the key is the language code
53     * and the value is the title of the target in that language.
54     *
55     * @return array
56     */
57    private function getExistingLinks() {
58        if ( $this->existingLinks === null ) {
59            $this->existingLinks = [];
60            foreach ( $this->fetchExistingRows() as $row ) {
61                $this->existingLinks[$row->ll_lang] = $row->ll_title;
62            }
63        }
64        return $this->existingLinks;
65    }
66
67    protected function getExistingLinkIDs() {
68        foreach ( $this->getExistingLinks() as $lang => $title ) {
69            yield [ (string)$lang, $title ];
70        }
71    }
72
73    protected function isExisting( $linkId ) {
74        $links = $this->getExistingLinks();
75        [ $lang, $title ] = $linkId;
76        return \array_key_exists( $lang, $links )
77            && $links[$lang] === $title;
78    }
79
80    protected function isInNewSet( $linkId ) {
81        [ $lang, $title ] = $linkId;
82        return \array_key_exists( $lang, $this->newLinks )
83            && $this->newLinks[$lang] === $title;
84    }
85
86    protected function insertLink( $linkId ) {
87        [ $lang, $title ] = $linkId;
88        $this->insertRow( [
89            'll_lang' => $lang,
90            'll_title' => $title
91        ] );
92    }
93
94    protected function deleteLink( $linkId ) {
95        $this->deleteRow( [
96            'll_lang' => $linkId[0]
97        ] );
98    }
99}