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
42 protected function getTableName() {
43 return 'langlinks';
44 }
45
47 protected function getFromField() {
48 return 'll_from';
49 }
50
52 protected function getExistingFields() {
53 return [ 'll_lang', 'll_title' ];
54 }
55
57 protected function getNewLinkIDs() {
58 foreach ( $this->newLinks as $key => $title ) {
59 yield [ (string)$key, $title ];
60 }
61 }
62
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
80 protected function getExistingLinkIDs() {
81 foreach ( $this->getExistingLinks() as $lang => $title ) {
82 yield [ (string)$lang, $title ];
83 }
84 }
85
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
95 protected function isInNewSet( $linkId ) {
96 [ $lang, $title ] = $linkId;
97 return \array_key_exists( $lang, $this->newLinks )
98 && $this->newLinks[$lang] === $title;
99 }
100
102 protected function insertLink( $linkId ) {
103 [ $lang, $title ] = $linkId;
104 $this->insertRow( [
105 'll_lang' => $lang,
106 'll_title' => $title
107 ] );
108 }
109
111 protected function deleteLink( $linkId ) {
112 $this->deleteRow( [
113 'll_lang' => $linkId[0]
114 ] );
115 }
116}
getNewLinkIDs()
Get an array (or iterator) of link IDs for the new state.See the LinksTable doc comment for an explan...
getExistingLinkIDs()
Get an array (or iterator) of link IDs for the existing state.The subclass should load the data from ...
insertLink( $linkId)
Insert a link identified by ID.The subclass is expected to queue the insertion by calling insertRow()...
deleteLink( $linkId)
Delete a link identified by ID.The subclass is expected to queue the deletion by calling deleteRow().
isExisting( $linkId)
Determine whether a link (from the new set) is in the existing set.bool
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().Note that fetchExistingRows() is just a helper for s...
isInNewSet( $linkId)
Determine whether a link (from the existing set) is in the new set.bool
getFromField()
Get the name of the field which links to page_id.string
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.