Debian Codegraph @

includes/deferred/LinksUpdate/LangLinksTable.php

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