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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
<?php
namespace MediaWiki\Deferred\LinksUpdate;
use MediaWiki\ExternalLinks\LinkFilter;
use MediaWiki\Parser\ParserOutput;
/**
* externallinks
*
* Link ID format: string URL
*
* @since 1.38
*/
class ExternalLinksTable extends LinksTable {
/** @var array<string,array<string,true>> */
private $newLinks = [];
/** @var array<string,array<string,true>>|null */
private $existingLinks;
public function setParserOutput( ParserOutput $parserOutput ) {
foreach ( $parserOutput->getExternalLinks() as $url => $unused ) {
foreach ( LinkFilter::makeIndexes( $url ) as [ $domainIndex, $path ] ) {
$this->newLinks[$domainIndex][$path] = true;
}
}
}
protected function getTableName() {
return 'externallinks';
}
protected function getFromField() {
return 'el_from';
}
protected function getExistingFields() {
return [ 'el_to_domain_index', 'el_to_path' ];
}
/**
* Get the existing links as an array
*
* @return array
*/
private function getExistingLinks() {
if ( $this->existingLinks === null ) {
$this->existingLinks = [];
foreach ( $this->fetchExistingRows() as $row ) {
$this->existingLinks[$row->el_to_domain_index][$row->el_to_path] = true;
}
}
return $this->existingLinks;
}
protected function getNewLinkIDs() {
foreach ( $this->newLinks as $domainIndex => $paths ) {
foreach ( $paths as $path => $unused ) {
yield [ (string)$domainIndex, (string)$path ];
}
}
}
protected function getExistingLinkIDs() {
foreach ( $this->getExistingLinks() as $domainIndex => $paths ) {
foreach ( $paths as $path => $unused ) {
yield [ (string)$domainIndex, (string)$path ];
}
}
}
protected function isExisting( $linkId ) {
[ $domainIndex, $path ] = $linkId;
return isset( $this->getExistingLinks()[$domainIndex][$path] );
}
protected function isInNewSet( $linkId ) {
[ $domainIndex, $path ] = $linkId;
return isset( $this->newLinks[$domainIndex][$path] );
}
protected function insertLink( $linkId ) {
[ $domainIndex, $path ] = $linkId;
$params = [
'el_to_domain_index' => substr( $domainIndex, 0, 255 ),
'el_to_path' => $path,
];
$this->insertRow( $params );
}
protected function deleteLink( $linkId ) {
[ $domainIndex, $path ] = $linkId;
$this->deleteRow( [
'el_to_domain_index' => substr( $domainIndex, 0, 255 ),
'el_to_path' => $path
] );
if ( $path === '' ) {
// el_to_path is nullable, but null is not valid in php arrays,
// so both values are handled as one key, delete both rows when exists
$this->deleteRow( [
'el_to_domain_index' => substr( $domainIndex, 0, 255 ),
'el_to_path' => null
] );
}
}
/**
* 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 );
$stringArray = [];
foreach ( $ids as $linkId ) {
[ $domainIndex, $path ] = $linkId;
$stringArray[] = LinkFilter::reverseIndexes( $domainIndex ) . $path;
}
return $stringArray;
}
}
|