| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Cache; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use MediaWiki\Language\Language; |
| 28 |
use MediaWiki\Linker\LinksMigration; |
| 29 |
use MediaWiki\Linker\LinkTarget; |
| 30 |
use MediaWiki\Page\PageIdentityValue; |
| 31 |
use MediaWiki\Page\PageReference; |
| 32 |
use MediaWiki\Page\ProperPageIdentity; |
| 33 |
use MediaWiki\Title\TitleFormatter; |
| 34 |
use MediaWiki\Title\TitleValue; |
| 35 |
use Psr\Log\LoggerInterface; |
| 36 |
use RuntimeException; |
| 37 |
use Wikimedia\Assert\Assert; |
| 38 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 39 |
use Wikimedia\Rdbms\IResultWrapper; |
| 40 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class LinkBatch { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public $data = []; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private $pageIdentities = null; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected $caller; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private $linkCache; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private $titleFormatter; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private $contentLanguage; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private $genderCache; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private $dbProvider; |
| 88 |
|
| 89 |
|
| 90 |
private $linksMigration; |
| 91 |
|
| 92 |
|
| 93 |
private $logger; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function __construct( |
| 109 |
iterable $arr, |
| 110 |
LinkCache $linkCache, |
| 111 |
TitleFormatter $titleFormatter, |
| 112 |
Language $contentLanguage, |
| 113 |
GenderCache $genderCache, |
| 114 |
IConnectionProvider $dbProvider, |
| 115 |
LinksMigration $linksMigration, |
| 116 |
LoggerInterface $logger |
| 117 |
) { |
| 118 |
$this->linkCache = $linkCache; |
| 119 |
$this->titleFormatter = $titleFormatter; |
| 120 |
$this->contentLanguage = $contentLanguage; |
| 121 |
$this->genderCache = $genderCache; |
| 122 |
$this->dbProvider = $dbProvider; |
| 123 |
$this->linksMigration = $linksMigration; |
| 124 |
$this->logger = $logger; |
| 125 |
|
| 126 |
foreach ( $arr as $item ) { |
| 127 |
$this->addObj( $item ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function setCaller( $caller ) { |
| 140 |
$this->caller = $caller; |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function addObj( $link ) { |
| 149 |
if ( !$link ) { |
| 150 |
|
| 151 |
|
| 152 |
$this->logger->warning( |
| 153 |
'Skipping null link, probably due to a bad title.', |
| 154 |
[ 'exception' => new RuntimeException() ] |
| 155 |
); |
| 156 |
return; |
| 157 |
} |
| 158 |
if ( $link instanceof LinkTarget && $link->isExternal() ) { |
| 159 |
$this->logger->warning( |
| 160 |
'Skipping interwiki link', |
| 161 |
[ 'exception' => new RuntimeException() ] |
| 162 |
); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
Assert::parameterType( [ LinkTarget::class, PageReference::class ], $link, '$link' ); |
| 167 |
$this->add( $link->getNamespace(), $link->getDBkey() ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function add( $ns, $dbkey ) { |
| 175 |
if ( $ns < 0 || $dbkey === '' ) { |
| 176 |
|
| 177 |
return; |
| 178 |
} |
| 179 |
$this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function setArray( $array ) { |
| 189 |
$this->data = $array; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function isEmpty() { |
| 198 |
return $this->getSize() == 0; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function getSize() { |
| 207 |
return count( $this->data ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function execute() { |
| 216 |
return $this->executeInto( $this->linkCache ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function getPageIdentities(): array { |
| 227 |
if ( $this->pageIdentities === null ) { |
| 228 |
$this->execute(); |
| 229 |
} |
| 230 |
|
| 231 |
return $this->pageIdentities; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
protected function executeInto( $cache ) { |
| 242 |
$res = $this->doQuery(); |
| 243 |
$this->doGenderQuery(); |
| 244 |
return $this->addResultToCache( $cache, $res ); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public function addResultToCache( $cache, $res ) { |
| 258 |
if ( !$res ) { |
| 259 |
return []; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
$this->pageIdentities ??= []; |
| 265 |
|
| 266 |
$ids = []; |
| 267 |
$remaining = $this->data; |
| 268 |
foreach ( $res as $row ) { |
| 269 |
try { |
| 270 |
$title = new TitleValue( (int)$row->page_namespace, $row->page_title ); |
| 271 |
|
| 272 |
$cache->addGoodLinkObjFromRow( $title, $row ); |
| 273 |
$pdbk = $this->titleFormatter->getPrefixedDBkey( $title ); |
| 274 |
$ids[$pdbk] = $row->page_id; |
| 275 |
|
| 276 |
$pageIdentity = new PageIdentityValue( |
| 277 |
(int)$row->page_id, |
| 278 |
(int)$row->page_namespace, |
| 279 |
$row->page_title, |
| 280 |
ProperPageIdentity::LOCAL |
| 281 |
); |
| 282 |
|
| 283 |
$key = CacheKeyHelper::getKeyForPage( $pageIdentity ); |
| 284 |
$this->pageIdentities[$key] = $pageIdentity; |
| 285 |
} catch ( InvalidArgumentException $ex ) { |
| 286 |
$this->logger->warning( |
| 287 |
'Encountered invalid title', |
| 288 |
[ 'title_namespace' => $row->page_namespace, 'title_dbkey' => $row->page_title ] |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
unset( $remaining[$row->page_namespace][$row->page_title] ); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
foreach ( $remaining as $ns => $dbkeys ) { |
| 297 |
foreach ( $dbkeys as $dbkey => $unused ) { |
| 298 |
try { |
| 299 |
$title = new TitleValue( (int)$ns, (string)$dbkey ); |
| 300 |
|
| 301 |
$cache->addBadLinkObj( $title ); |
| 302 |
$pdbk = $this->titleFormatter->getPrefixedDBkey( $title ); |
| 303 |
$ids[$pdbk] = 0; |
| 304 |
|
| 305 |
$pageIdentity = new PageIdentityValue( 0, (int)$ns, $dbkey, ProperPageIdentity::LOCAL ); |
| 306 |
$key = CacheKeyHelper::getKeyForPage( $pageIdentity ); |
| 307 |
$this->pageIdentities[$key] = $pageIdentity; |
| 308 |
} catch ( InvalidArgumentException $ex ) { |
| 309 |
$this->logger->warning( |
| 310 |
'Encountered invalid title', |
| 311 |
[ 'title_namespace' => $ns, 'title_dbkey' => $dbkey ] |
| 312 |
); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
return $ids; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public function doQuery() { |
| 325 |
if ( $this->isEmpty() ) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 331 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 332 |
->select( LinkCache::getSelectFields() ) |
| 333 |
->from( 'page' ) |
| 334 |
->where( $this->constructSet( 'page', $dbr ) ); |
| 335 |
|
| 336 |
$caller = __METHOD__; |
| 337 |
if ( strval( $this->caller ) !== '' ) { |
| 338 |
$caller .= " (for {$this->caller})"; |
| 339 |
} |
| 340 |
|
| 341 |
return $queryBuilder->caller( $caller )->fetchResultSet(); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public function doGenderQuery() { |
| 350 |
if ( $this->isEmpty() || !$this->contentLanguage->needsGenderDistinction() ) { |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
$this->genderCache->doLinkBatch( $this->data, $this->caller ); |
| 355 |
|
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public function constructSet( $prefix, $db ) { |
| 371 |
if ( isset( $this->linksMigration::$prefixToTableMapping[$prefix] ) ) { |
| 372 |
[ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields( |
| 373 |
$this->linksMigration::$prefixToTableMapping[$prefix] |
| 374 |
); |
| 375 |
} else { |
| 376 |
$blNamespace = "{$prefix}_namespace"; |
| 377 |
$blTitle = "{$prefix}_title"; |
| 378 |
} |
| 379 |
return $db->makeWhereFrom2d( $this->data, $blNamespace, $blTitle ); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
class_alias( LinkBatch::class, 'LinkBatch' ); |