| 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 InterwikiLinksTable extends LinksTable { |
| 17 |
|
| 18 |
private $newLinks = []; |
| 19 |
|
| 20 |
|
| 21 |
private $existingLinks; |
| 22 |
|
| 23 |
public function setParserOutput( ParserOutput $parserOutput ) { |
| 24 |
$this->newLinks = $parserOutput->getInterwikiLinks(); |
| 25 |
} |
| 26 |
|
| 27 |
protected function getTableName() { |
| 28 |
return 'iwlinks'; |
| 29 |
} |
| 30 |
|
| 31 |
protected function getFromField() { |
| 32 |
return 'iwl_from'; |
| 33 |
} |
| 34 |
|
| 35 |
protected function getExistingFields() { |
| 36 |
return [ 'iwl_prefix', 'iwl_title' ]; |
| 37 |
} |
| 38 |
|
| 39 |
protected function getNewLinkIDs() { |
| 40 |
foreach ( $this->newLinks as $prefix => $links ) { |
| 41 |
foreach ( $links as $title => $unused ) { |
| 42 |
yield [ (string)$prefix, (string)$title ]; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private function getExistingLinks() { |
| 54 |
if ( $this->existingLinks === null ) { |
| 55 |
$this->existingLinks = []; |
| 56 |
foreach ( $this->fetchExistingRows() as $row ) { |
| 57 |
$this->existingLinks[$row->iwl_prefix][$row->iwl_title] = true; |
| 58 |
} |
| 59 |
} |
| 60 |
return $this->existingLinks; |
| 61 |
} |
| 62 |
|
| 63 |
protected function getExistingLinkIDs() { |
| 64 |
foreach ( $this->getExistingLinks() as $prefix => $links ) { |
| 65 |
foreach ( $links as $title => $unused ) { |
| 66 |
yield [ (string)$prefix, (string)$title ]; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
protected function isExisting( $linkId ) { |
| 72 |
$links = $this->getExistingLinks(); |
| 73 |
[ $prefix, $title ] = $linkId; |
| 74 |
return isset( $links[$prefix][$title] ); |
| 75 |
} |
| 76 |
|
| 77 |
protected function isInNewSet( $linkId ) { |
| 78 |
[ $prefix, $title ] = $linkId; |
| 79 |
return isset( $this->newLinks[$prefix][$title] ); |
| 80 |
} |
| 81 |
|
| 82 |
protected function insertLink( $linkId ) { |
| 83 |
[ $prefix, $title ] = $linkId; |
| 84 |
$this->insertRow( [ |
| 85 |
'iwl_prefix' => $prefix, |
| 86 |
'iwl_title' => $title |
| 87 |
] ); |
| 88 |
} |
| 89 |
|
| 90 |
protected function deleteLink( $linkId ) { |
| 91 |
[ $prefix, $title ] = $linkId; |
| 92 |
$this->deleteRow( [ |
| 93 |
'iwl_prefix' => $prefix, |
| 94 |
'iwl_title' => $title |
| 95 |
] ); |
| 96 |
} |
| 97 |
} |