wiki.techinc.nl/includes/deferred/LinksUpdate/LangLinksTable.php
Tim Starling a7df0148d8 LinksTable: Cast all array keys to string
When fetching a key from an array, PHP converts numeric strings to
integers. This led to an incorrect category update query.

So:

* In LinksTable subclasses, cast all string-like array keys to string.
* Add a unit test which confirms that no integers leak into link IDs.
* Add a fully integrated regression test for T301433.

The integration test and 7 of the link ID cases are confirmed to fail
if Amir's patch is reverted.

Bug: T301433
Change-Id: I8d19443607121b3efcafb82096bcff18c41035df
2022-02-14 15:56:34 +11:00

99 lines
2.3 KiB
PHP

<?php
namespace MediaWiki\Deferred\LinksUpdate;
use ParserOutput;
/**
* langlinks
*
* Link ID format: string[]
* - 0: Language code
* - 1: Foreign title
*
* @since 1.38
*/
class LangLinksTable extends LinksTable {
private $newLinks = [];
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 );
$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]
] );
}
}