MediaWiki master
LangLinksTable.php
Go to the documentation of this file.
1<?php
2
4
7
19 private $newLinks = [];
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 protected function getTableName() {
42 return 'langlinks';
43 }
44
45 protected function getFromField() {
46 return 'll_from';
47 }
48
49 protected function getExistingFields() {
50 return [ 'll_lang', 'll_title' ];
51 }
52
53 protected function getNewLinkIDs() {
54 foreach ( $this->newLinks as $key => $title ) {
55 yield [ (string)$key, $title ];
56 }
57 }
58
65 private function getExistingLinks() {
66 if ( $this->existingLinks === null ) {
67 $this->existingLinks = [];
68 foreach ( $this->fetchExistingRows() as $row ) {
69 $this->existingLinks[$row->ll_lang] = $row->ll_title;
70 }
71 }
72 return $this->existingLinks;
73 }
74
75 protected function getExistingLinkIDs() {
76 foreach ( $this->getExistingLinks() as $lang => $title ) {
77 yield [ (string)$lang, $title ];
78 }
79 }
80
81 protected function isExisting( $linkId ) {
82 $links = $this->getExistingLinks();
83 [ $lang, $title ] = $linkId;
84 return \array_key_exists( $lang, $links )
85 && $links[$lang] === $title;
86 }
87
88 protected function isInNewSet( $linkId ) {
89 [ $lang, $title ] = $linkId;
90 return \array_key_exists( $lang, $this->newLinks )
91 && $this->newLinks[$lang] === $title;
92 }
93
94 protected function insertLink( $linkId ) {
95 [ $lang, $title ] = $linkId;
96 $this->insertRow( [
97 'll_lang' => $lang,
98 'll_title' => $title
99 ] );
100 }
101
102 protected function deleteLink( $linkId ) {
103 $this->deleteRow( [
104 'll_lang' => $linkId[0]
105 ] );
106 }
107}
getNewLinkIDs()
Get an array (or iterator) of link IDs for the new state.
getExistingLinkIDs()
Get an array (or iterator) of link IDs for the existing state.
insertLink( $linkId)
Insert a link identified by ID.
deleteLink( $linkId)
Delete a link identified by ID.
isExisting( $linkId)
Determine whether a link (from the new set) is in the existing set.
setParserOutput(ParserOutput $parserOutput)
Subclasses should implement this to extract the data they need from the ParserOutput.
getExistingFields()
Get the fields to be used in fetchExistingRows().
isInNewSet( $linkId)
Determine whether a link (from the existing set) is in the new set.
getFromField()
Get the name of the field which links to page_id.
The base class for classes which update a single link table.
insertRow( $row)
Queue a row for insertion.
fetchExistingRows()
Do a select query to fetch the existing rows.
deleteRow( $conds)
Queue a deletion operation.
ParserOutput is a rendering of a Content object or a message.
getLinkList(string $linkType)
Get a list of links of the given type.