wiki.techinc.nl/includes/deferred/LinksUpdate/ExternalLinksTable.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.1 KiB
PHP

<?php
namespace MediaWiki\Deferred\LinksUpdate;
use LinkFilter;
use ParserOutput;
/**
* externallinks
*
* Link ID format: string URL
*
* @since 1.38
*/
class ExternalLinksTable extends LinksTable {
private $newLinks = [];
private $existingLinks;
public function setParserOutput( ParserOutput $parserOutput ) {
$this->newLinks = $parserOutput->getExternalLinks();
}
protected function getTableName() {
return 'externallinks';
}
protected function getFromField() {
return 'el_from';
}
protected function getExistingFields() {
return [ 'el_to' ];
}
/**
* Get the existing links as an array, where the key is the URL and the
* value is unused.
*
* @return array
*/
private function getExistingLinks() {
if ( $this->existingLinks === null ) {
$this->existingLinks = [];
foreach ( $this->fetchExistingRows() as $row ) {
$this->existingLinks[$row->el_to] = true;
}
}
return $this->existingLinks;
}
protected function getNewLinkIDs() {
foreach ( $this->newLinks as $link => $unused ) {
yield (string)$link;
}
}
protected function getExistingLinkIDs() {
foreach ( $this->getExistingLinks() as $link => $unused ) {
yield (string)$link;
}
}
protected function isExisting( $linkId ) {
return \array_key_exists( $linkId, $this->getExistingLinks() );
}
protected function isInNewSet( $linkId ) {
return \array_key_exists( $linkId, $this->newLinks );
}
protected function insertLink( $linkId ) {
foreach ( LinkFilter::makeIndexes( $linkId ) as $index ) {
$this->insertRow( [
'el_to' => $linkId,
'el_index' => $index,
'el_index_60' => substr( $index, 0, 60 ),
] );
}
}
protected function deleteLink( $linkId ) {
$this->deleteRow( [ 'el_to' => $linkId ] );
}
/**
* Get an array of URLs of the given type
*
* @param int $setType One of the link set constants as in LinksTable::getLinkIDs()
* @return string[]
*/
public function getStringArray( $setType ) {
$ids = $this->getLinkIDs( $setType );
if ( is_array( $ids ) ) {
return $ids;
} else {
return iterator_to_array( $ids );
}
}
}