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