| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Deferred\LinksUpdate; |
| 4 |
|
| 5 |
use Collation; |
| 6 |
use MediaWiki\DAO\WikiAwareEntity; |
| 7 |
use MediaWiki\Language\ILanguageConverter; |
| 8 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 9 |
use MediaWiki\Page\PageReferenceValue; |
| 10 |
use MediaWiki\Page\WikiPageFactory; |
| 11 |
use MediaWiki\Parser\ParserOutput; |
| 12 |
use MediaWiki\Parser\Sanitizer; |
| 13 |
use MediaWiki\Title\NamespaceInfo; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use PurgeJobUtils; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class CategoryLinksTable extends TitleLinksTable { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $newLinks = []; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private $existingLinks; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private $savedTimestamps = null; |
| 45 |
|
| 46 |
|
| 47 |
private $languageConverter; |
| 48 |
|
| 49 |
|
| 50 |
private $collation; |
| 51 |
|
| 52 |
|
| 53 |
private $collationName; |
| 54 |
|
| 55 |
|
| 56 |
private $tableName = 'categorylinks'; |
| 57 |
|
| 58 |
|
| 59 |
private $isTempTable; |
| 60 |
|
| 61 |
|
| 62 |
private $categoryType; |
| 63 |
|
| 64 |
|
| 65 |
private $namespaceInfo; |
| 66 |
|
| 67 |
|
| 68 |
private $wikiPageFactory; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function __construct( |
| 80 |
LanguageConverterFactory $converterFactory, |
| 81 |
NamespaceInfo $namespaceInfo, |
| 82 |
WikiPageFactory $wikiPageFactory, |
| 83 |
Collation $collation, |
| 84 |
$collationName, |
| 85 |
$tableName, |
| 86 |
$isTempTable |
| 87 |
) { |
| 88 |
$this->languageConverter = $converterFactory->getLanguageConverter(); |
| 89 |
$this->namespaceInfo = $namespaceInfo; |
| 90 |
$this->wikiPageFactory = $wikiPageFactory; |
| 91 |
$this->collation = $collation; |
| 92 |
$this->collationName = $collationName; |
| 93 |
$this->tableName = $tableName; |
| 94 |
$this->isTempTable = $isTempTable; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function startUpdate() { |
| 101 |
$this->categoryType = $this->namespaceInfo |
| 102 |
->getCategoryLinkType( $this->getSourcePage()->getNamespace() ); |
| 103 |
} |
| 104 |
|
| 105 |
public function setParserOutput( ParserOutput $parserOutput ) { |
| 106 |
$this->newLinks = []; |
| 107 |
$sourceTitle = Title::castFromPageIdentity( $this->getSourcePage() ); |
| 108 |
$sortKeyInputs = []; |
| 109 |
foreach ( $parserOutput->getCategoryNames() as $name ) { |
| 110 |
$sortKey = $parserOutput->getCategorySortKey( $name ); |
| 111 |
'@phan-var string $sortKey'; |
| 112 |
|
| 113 |
if ( $sortKey == '' ) { |
| 114 |
$sortKey = $parserOutput->getPageProperty( "defaultsort" ) ?? ''; |
| 115 |
} |
| 116 |
$sortKey = $this->languageConverter->convertCategoryKey( $sortKey ); |
| 117 |
|
| 118 |
|
| 119 |
$sortKey = Sanitizer::decodeCharReferences( $sortKey ); |
| 120 |
$sortKey = str_replace( "\n", '', $sortKey ); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
$sortKeyPrefix = mb_strcut( $sortKey, 0, 255 ); |
| 126 |
|
| 127 |
$targetTitle = Title::makeTitle( NS_CATEGORY, $name ); |
| 128 |
$this->languageConverter->findVariantLink( $name, $targetTitle, true ); |
| 129 |
|
| 130 |
$name = $targetTitle->getDBKey(); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
$sortKeyInputs[$name] = $sourceTitle->getCategorySortkey( $sortKeyPrefix ); |
| 137 |
$this->newLinks[$name] = [ $sortKeyPrefix ]; |
| 138 |
} |
| 139 |
$sortKeys = $this->collation->getSortKeys( $sortKeyInputs ); |
| 140 |
foreach ( $sortKeys as $name => $sortKey ) { |
| 141 |
$this->newLinks[$name][1] = $sortKey; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
protected function getTableName() { |
| 146 |
return $this->tableName; |
| 147 |
} |
| 148 |
|
| 149 |
protected function getFromField() { |
| 150 |
return 'cl_from'; |
| 151 |
} |
| 152 |
|
| 153 |
protected function getExistingFields() { |
| 154 |
$fields = [ 'cl_to', 'cl_sortkey_prefix' ]; |
| 155 |
if ( $this->needForcedLinkRefresh() ) { |
| 156 |
$fields[] = 'cl_timestamp'; |
| 157 |
} |
| 158 |
return $fields; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
protected function getNewLinkIDs() { |
| 168 |
foreach ( $this->newLinks as $name => [ $prefix, ] ) { |
| 169 |
yield [ (string)$name, $prefix ]; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private function fetchExistingLinks() { |
| 177 |
$this->existingLinks = []; |
| 178 |
$this->savedTimestamps = []; |
| 179 |
$force = $this->needForcedLinkRefresh(); |
| 180 |
foreach ( $this->fetchExistingRows() as $row ) { |
| 181 |
$this->existingLinks[$row->cl_to] = $row->cl_sortkey_prefix; |
| 182 |
if ( $force ) { |
| 183 |
$this->savedTimestamps[$row->cl_to] = $row->cl_timestamp; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private function getExistingLinks() { |
| 195 |
if ( $this->existingLinks === null ) { |
| 196 |
$this->fetchExistingLinks(); |
| 197 |
} |
| 198 |
return $this->existingLinks; |
| 199 |
} |
| 200 |
|
| 201 |
private function getSavedTimestamps() { |
| 202 |
if ( $this->savedTimestamps === null ) { |
| 203 |
$this->fetchExistingLinks(); |
| 204 |
} |
| 205 |
return $this->savedTimestamps; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
protected function getExistingLinkIDs() { |
| 212 |
foreach ( $this->getExistingLinks() as $name => $sortkey ) { |
| 213 |
yield [ (string)$name, $sortkey ]; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
protected function isExisting( $linkId ) { |
| 218 |
$links = $this->getExistingLinks(); |
| 219 |
[ $name, $prefix ] = $linkId; |
| 220 |
return \array_key_exists( $name, $links ) && $links[$name] === $prefix; |
| 221 |
} |
| 222 |
|
| 223 |
protected function isInNewSet( $linkId ) { |
| 224 |
[ $name, $prefix ] = $linkId; |
| 225 |
return \array_key_exists( $name, $this->newLinks ) |
| 226 |
&& $this->newLinks[$name][0] === $prefix; |
| 227 |
} |
| 228 |
|
| 229 |
protected function insertLink( $linkId ) { |
| 230 |
[ $name, $prefix ] = $linkId; |
| 231 |
$sortKey = $this->newLinks[$name][1]; |
| 232 |
$savedTimestamps = $this->getSavedTimestamps(); |
| 233 |
|
| 234 |
|
| 235 |
$timestamp = $this->getDB()->timestamp( $savedTimestamps[$name] ?? 0 ); |
| 236 |
|
| 237 |
$this->insertRow( [ |
| 238 |
'cl_to' => $name, |
| 239 |
'cl_sortkey' => $sortKey, |
| 240 |
'cl_timestamp' => $timestamp, |
| 241 |
'cl_sortkey_prefix' => $prefix, |
| 242 |
'cl_collation' => $this->collationName, |
| 243 |
'cl_type' => $this->categoryType, |
| 244 |
] ); |
| 245 |
} |
| 246 |
|
| 247 |
protected function deleteLink( $linkId ) { |
| 248 |
$this->deleteRow( [ 'cl_to' => $linkId[0] ] ); |
| 249 |
} |
| 250 |
|
| 251 |
protected function needForcedLinkRefresh() { |
| 252 |
|
| 253 |
return $this->isMove(); |
| 254 |
} |
| 255 |
|
| 256 |
protected function makePageReferenceValue( $linkId ): PageReferenceValue { |
| 257 |
return new PageReferenceValue( NS_CATEGORY, $linkId[0], WikiAwareEntity::LOCAL ); |
| 258 |
} |
| 259 |
|
| 260 |
protected function makeTitle( $linkId ): Title { |
| 261 |
return Title::makeTitle( NS_CATEGORY, $linkId[0] ); |
| 262 |
} |
| 263 |
|
| 264 |
protected function deduplicateLinkIds( $linkIds ) { |
| 265 |
$seen = []; |
| 266 |
foreach ( $linkIds as $linkId ) { |
| 267 |
if ( !\array_key_exists( $linkId[0], $seen ) ) { |
| 268 |
$seen[$linkId[0]] = true; |
| 269 |
yield $linkId; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
protected function finishUpdate() { |
| 275 |
if ( $this->isTempTable ) { |
| 276 |
|
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
$allInsertedLinks = array_column( $this->insertedLinks, 0 ); |
| 283 |
$allDeletedLinks = array_column( $this->deletedLinks, 0 ); |
| 284 |
$insertedLinks = array_diff( $allInsertedLinks, $allDeletedLinks ); |
| 285 |
$deletedLinks = array_diff( $allDeletedLinks, $allInsertedLinks ); |
| 286 |
|
| 287 |
$this->invalidateCategories( $insertedLinks, $deletedLinks ); |
| 288 |
$this->updateCategoryCounts( $insertedLinks, $deletedLinks ); |
| 289 |
} |
| 290 |
|
| 291 |
private function invalidateCategories( array $insertedLinks, array $deletedLinks ) { |
| 292 |
$changedCategoryNames = array_merge( |
| 293 |
$insertedLinks, |
| 294 |
$deletedLinks |
| 295 |
); |
| 296 |
PurgeJobUtils::invalidatePages( |
| 297 |
$this->getDB(), NS_CATEGORY, $changedCategoryNames ); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private function updateCategoryCounts( array $insertedLinks, array $deletedLinks ) { |
| 306 |
if ( !$insertedLinks && !$deletedLinks ) { |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
$domainId = $this->getDB()->getDomainID(); |
| 311 |
$wp = $this->wikiPageFactory->newFromTitle( $this->getSourcePage() ); |
| 312 |
$lbf = $this->getLBFactory(); |
| 313 |
$size = $this->getBatchSize(); |
| 314 |
|
| 315 |
$lbf->commitAndWaitForReplication( __METHOD__, $this->getTransactionTicket() ); |
| 316 |
|
| 317 |
if ( count( $insertedLinks ) + count( $deletedLinks ) < $size ) { |
| 318 |
$wp->updateCategoryCounts( |
| 319 |
$insertedLinks, |
| 320 |
$deletedLinks, |
| 321 |
$this->getSourcePageId() |
| 322 |
); |
| 323 |
$lbf->commitAndWaitForReplication( __METHOD__, $this->getTransactionTicket() ); |
| 324 |
} else { |
| 325 |
$addedChunks = array_chunk( $insertedLinks, $size ); |
| 326 |
foreach ( $addedChunks as $chunk ) { |
| 327 |
$wp->updateCategoryCounts( $chunk, [], $this->getSourcePageId() ); |
| 328 |
if ( count( $addedChunks ) > 1 ) { |
| 329 |
$lbf->commitAndWaitForReplication( __METHOD__, $this->getTransactionTicket() ); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
$deletedChunks = array_chunk( $deletedLinks, $size ); |
| 334 |
foreach ( $deletedChunks as $chunk ) { |
| 335 |
$wp->updateCategoryCounts( [], $chunk, $this->getSourcePageId() ); |
| 336 |
if ( count( $deletedChunks ) > 1 ) { |
| 337 |
$lbf->commitAndWaitForReplication( __METHOD__, $this->getTransactionTicket() ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
} |
| 343 |
} |