| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Deferred\LinksUpdate; |
| 4 |
|
| 5 |
use MediaWiki\Parser\ParserOutput; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class LangLinksTable extends LinksTable { |
| 17 |
|
| 18 |
private $newLinks = []; |
| 19 |
|
| 20 |
private $existingLinks; |
| 21 |
|
| 22 |
public function setParserOutput( ParserOutput $parserOutput ) { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
$ill = $parserOutput->getLanguageLinks(); |
| 28 |
$this->newLinks = []; |
| 29 |
foreach ( $ill as $link ) { |
| 30 |
[ $key, $title ] = explode( ':', $link, 2 ); |
| 31 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
} |