| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Cache\LinkCache; |
| 4 |
use MediaWiki\Page\PageReference; |
| 5 |
use MediaWiki\Page\PageReferenceValue; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
use MediaWiki\Title\TitleValue; |
| 8 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 9 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 10 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 11 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class LinkCacheTest extends MediaWikiIntegrationTestCase { |
| 19 |
use LinkCacheTestTrait; |
| 20 |
|
| 21 |
private function newLinkCache( ?WANObjectCache $wanCache = null ) { |
| 22 |
if ( !$wanCache ) { |
| 23 |
$wanCache = new WANObjectCache( [ 'cache' => new EmptyBagOStuff() ] ); |
| 24 |
} |
| 25 |
|
| 26 |
return new LinkCache( |
| 27 |
$this->getServiceContainer()->getTitleFormatter(), |
| 28 |
$wanCache, |
| 29 |
$this->getServiceContainer()->getNamespaceInfo(), |
| 30 |
$this->getServiceContainer()->getDBLoadBalancer() |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
public static function providePageAndLink() { |
| 35 |
return [ |
| 36 |
[ new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ) ], |
| 37 |
[ new TitleValue( NS_USER, __METHOD__ ) ] |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
public static function providePageAndLinkAndArray() { |
| 42 |
return [ |
| 43 |
[ new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ) ], |
| 44 |
[ new TitleValue( NS_USER, __METHOD__ ) ], |
| 45 |
[ [ 'page_namespace' => NS_USER, 'page_title' => __METHOD__ ] ], |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
private function getPageRow( $offset = 0 ) { |
| 50 |
return (object)[ |
| 51 |
'page_id' => 8 + $offset, |
| 52 |
'page_namespace' => 0, |
| 53 |
'page_title' => 'Test ' . $offset, |
| 54 |
'page_len' => 18, |
| 55 |
'page_is_redirect' => 0, |
| 56 |
'page_latest' => 118 + $offset, |
| 57 |
'page_content_model' => CONTENT_MODEL_TEXT, |
| 58 |
'page_lang' => 'xyz', |
| 59 |
'page_is_new' => 0, |
| 60 |
'page_touched' => '20200202020202', |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function testAddGoodLinkObjFromRow( $page ) { |
| 73 |
$linkCache = $this->newLinkCache(); |
| 74 |
|
| 75 |
$row = $this->getPageRow(); |
| 76 |
|
| 77 |
$dbkey = is_array( $page ) ? $page['page_title'] : $page->getDBkey(); |
| 78 |
$ns = is_array( $page ) ? $page['page_namespace'] : $page->getNamespace(); |
| 79 |
|
| 80 |
$linkCache->addBadLinkObj( $page ); |
| 81 |
$linkCache->addGoodLinkObjFromRow( $page, $row ); |
| 82 |
|
| 83 |
$this->assertEquals( |
| 84 |
$row, |
| 85 |
$linkCache->getGoodLinkRow( $ns, $dbkey ) |
| 86 |
); |
| 87 |
|
| 88 |
$this->assertSame( $row->page_id, $linkCache->getGoodLinkID( $page ) ); |
| 89 |
$this->assertFalse( $linkCache->isBadLink( $page ) ); |
| 90 |
|
| 91 |
$this->assertSame( |
| 92 |
$row->page_id, |
| 93 |
$linkCache->getGoodLinkFieldObj( $page, 'id' ) |
| 94 |
); |
| 95 |
$this->assertSame( |
| 96 |
$row->page_len, |
| 97 |
$linkCache->getGoodLinkFieldObj( $page, 'length' ) |
| 98 |
); |
| 99 |
$this->assertSame( |
| 100 |
$row->page_is_redirect, |
| 101 |
$linkCache->getGoodLinkFieldObj( $page, 'redirect' ) |
| 102 |
); |
| 103 |
$this->assertSame( |
| 104 |
$row->page_latest, |
| 105 |
$linkCache->getGoodLinkFieldObj( $page, 'revision' ) |
| 106 |
); |
| 107 |
$this->assertSame( |
| 108 |
$row->page_content_model, |
| 109 |
$linkCache->getGoodLinkFieldObj( $page, 'model' ) |
| 110 |
); |
| 111 |
$this->assertSame( |
| 112 |
$row->page_lang, |
| 113 |
$linkCache->getGoodLinkFieldObj( $page, 'lang' ) |
| 114 |
); |
| 115 |
|
| 116 |
$this->assertEquals( |
| 117 |
$row, |
| 118 |
$linkCache->getGoodLinkRow( $ns, $dbkey ) |
| 119 |
); |
| 120 |
|
| 121 |
$linkCache->clearBadLink( $page ); |
| 122 |
$this->assertNotNull( $linkCache->getGoodLinkID( $page ) ); |
| 123 |
$this->assertNotNull( $linkCache->getGoodLinkFieldObj( $page, 'length' ) ); |
| 124 |
|
| 125 |
$linkCache->clearLink( $page ); |
| 126 |
$this->assertSame( 0, $linkCache->getGoodLinkID( $page ) ); |
| 127 |
$this->assertNull( $linkCache->getGoodLinkFieldObj( $page, 'length' ) ); |
| 128 |
$this->assertNull( $linkCache->getGoodLinkRow( $ns, $dbkey ) ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function testAddGoodLinkObjWithAllParameters() { |
| 138 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 139 |
|
| 140 |
$page = new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ); |
| 141 |
$this->addGoodLinkObject( 8, $page, 18, 0, 118, CONTENT_MODEL_TEXT, 'xyz' ); |
| 142 |
|
| 143 |
$row = $linkCache->getGoodLinkRow( $page->getNamespace(), $page->getDBkey() ); |
| 144 |
$this->assertEquals( 8, (int)$row->page_id ); |
| 145 |
$this->assertSame( 8, $linkCache->getGoodLinkID( $page ) ); |
| 146 |
$this->assertSame( 8, $linkCache->getGoodLinkFieldObj( $page, 'id' ) ); |
| 147 |
|
| 148 |
$this->assertSame( |
| 149 |
18, |
| 150 |
$linkCache->getGoodLinkFieldObj( $page, 'length' ) |
| 151 |
); |
| 152 |
$this->assertSame( |
| 153 |
0, |
| 154 |
$linkCache->getGoodLinkFieldObj( $page, 'redirect' ) |
| 155 |
); |
| 156 |
$this->assertSame( |
| 157 |
118, |
| 158 |
$linkCache->getGoodLinkFieldObj( $page, 'revision' ) |
| 159 |
); |
| 160 |
$this->assertSame( |
| 161 |
CONTENT_MODEL_TEXT, |
| 162 |
$linkCache->getGoodLinkFieldObj( $page, 'model' ) |
| 163 |
); |
| 164 |
$this->assertSame( |
| 165 |
'xyz', |
| 166 |
$linkCache->getGoodLinkFieldObj( $page, 'lang' ) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function testAddGoodLinkObjFromRowWithMinimalParameters() { |
| 177 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 178 |
|
| 179 |
$page = new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ); |
| 180 |
|
| 181 |
$this->addGoodLinkObject( 8, $page ); |
| 182 |
$expectedRow = [ |
| 183 |
'page_id' => 8, |
| 184 |
'page_len' => -1, |
| 185 |
'page_is_redirect' => 0, |
| 186 |
'page_latest' => 0, |
| 187 |
'page_content_model' => null, |
| 188 |
'page_lang' => null, |
| 189 |
]; |
| 190 |
|
| 191 |
$actualRow = (array)$linkCache->getGoodLinkRow( $page->getNamespace(), $page->getDBkey() ); |
| 192 |
$this->assertEquals( |
| 193 |
$expectedRow, |
| 194 |
array_intersect_key( $actualRow, $expectedRow ) |
| 195 |
); |
| 196 |
|
| 197 |
$this->assertSame( 8, $linkCache->getGoodLinkID( $page ) ); |
| 198 |
$this->assertSame( 8, $linkCache->getGoodLinkFieldObj( $page, 'id' ) ); |
| 199 |
|
| 200 |
$this->assertSame( |
| 201 |
-1, |
| 202 |
$linkCache->getGoodLinkFieldObj( $page, 'length' ) |
| 203 |
); |
| 204 |
$this->assertSame( |
| 205 |
0, |
| 206 |
$linkCache->getGoodLinkFieldObj( $page, 'redirect' ) |
| 207 |
); |
| 208 |
$this->assertSame( |
| 209 |
0, |
| 210 |
$linkCache->getGoodLinkFieldObj( $page, 'revision' ) |
| 211 |
); |
| 212 |
$this->assertSame( |
| 213 |
null, |
| 214 |
$linkCache->getGoodLinkFieldObj( $page, 'model' ) |
| 215 |
); |
| 216 |
$this->assertSame( |
| 217 |
null, |
| 218 |
$linkCache->getGoodLinkFieldObj( $page, 'lang' ) |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function testAddGoodLinkObjFromRowWithInterwikiLink() { |
| 226 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 227 |
|
| 228 |
$page = new TitleValue( NS_USER, __METHOD__, '', 'acme' ); |
| 229 |
|
| 230 |
$this->addGoodLinkObject( 8, $page ); |
| 231 |
|
| 232 |
$this->assertSame( 0, $linkCache->getGoodLinkID( $page ) ); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public function testAddBadLinkObj( $key ) { |
| 242 |
$linkCache = $this->getServiceContainer()->getLinkCache(); |
| 243 |
$this->assertFalse( $linkCache->isBadLink( $key ) ); |
| 244 |
|
| 245 |
$this->addGoodLinkObject( 17, $key ); |
| 246 |
|
| 247 |
$linkCache->addBadLinkObj( $key ); |
| 248 |
$this->assertTrue( $linkCache->isBadLink( $key ) ); |
| 249 |
$this->assertSame( 0, $linkCache->getGoodLinkID( $key ) ); |
| 250 |
|
| 251 |
$linkCache->clearLink( $key ); |
| 252 |
$this->assertFalse( $linkCache->isBadLink( $key ) ); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function testAddBadLinkObjWithInterwikiLink() { |
| 259 |
$linkCache = $this->newLinkCache(); |
| 260 |
|
| 261 |
$page = new TitleValue( NS_USER, __METHOD__, '', 'acme' ); |
| 262 |
$linkCache->addBadLinkObj( $page ); |
| 263 |
|
| 264 |
$this->assertFalse( $linkCache->isBadLink( $page ) ); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function testAddLinkObj() { |
| 272 |
$existing = $this->getExistingTestPage(); |
| 273 |
$missing = $this->getNonexistingTestPage(); |
| 274 |
|
| 275 |
$linkCache = $this->newLinkCache(); |
| 276 |
|
| 277 |
$linkCache->addLinkObj( $existing ); |
| 278 |
$linkCache->addLinkObj( $missing ); |
| 279 |
|
| 280 |
$this->assertTrue( $linkCache->isBadLink( $missing ) ); |
| 281 |
$this->assertFalse( $linkCache->isBadLink( $existing ) ); |
| 282 |
|
| 283 |
$this->assertSame( $existing->getId(), $linkCache->getGoodLinkID( $existing ) ); |
| 284 |
$this->assertTrue( $linkCache->isBadLink( $missing ) ); |
| 285 |
|
| 286 |
|
| 287 |
$this->assertNull( $linkCache->getGoodLinkFieldObj( $missing, 'length' ) ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public function testAddLinkObjUsesCachedInfo() { |
| 294 |
$existing = $this->getExistingTestPage(); |
| 295 |
$missing = $this->getNonexistingTestPage(); |
| 296 |
|
| 297 |
$fakeRow = $this->getPageRow( $existing->getId() + 100 ); |
| 298 |
|
| 299 |
$linkCache = $this->newLinkCache(); |
| 300 |
|
| 301 |
|
| 302 |
$linkCache->addGoodLinkObjFromRow( $missing, $fakeRow ); |
| 303 |
$linkCache->addBadLinkObj( $existing ); |
| 304 |
|
| 305 |
|
| 306 |
$this->assertSame( (int)$fakeRow->page_id, $linkCache->addLinkObj( $missing ) ); |
| 307 |
$this->assertSame( 0, $linkCache->addLinkObj( $existing ) ); |
| 308 |
|
| 309 |
|
| 310 |
$flags = IDBAccessObject::READ_LATEST; |
| 311 |
$this->assertSame( 0, $linkCache->addLinkObj( $missing, $flags ) ); |
| 312 |
$this->assertSame( $existing->getId(), $linkCache->addLinkObj( $existing, $flags ) ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public function testAddLinkObjUsesWANCache() { |
| 319 |
|
| 320 |
$existing = $this->getExistingTestPage( Title::makeTitle( NS_TEMPLATE, __METHOD__ ) ); |
| 321 |
$wanCache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); |
| 322 |
$linkCache = $this->newLinkCache( $wanCache ); |
| 323 |
|
| 324 |
|
| 325 |
$linkCache->addLinkObj( $existing ); |
| 326 |
|
| 327 |
|
| 328 |
$key = $wanCache->makeKey( 'page', $existing->getNamespace(), sha1( $existing->getDBkey() ) ); |
| 329 |
$fakeRow = $this->getPageRow( $existing->getId() + 100 ); |
| 330 |
$wanCache->set( $key, $fakeRow ); |
| 331 |
|
| 332 |
$linkCache->clearLink( $existing ); |
| 333 |
$this->assertSame( (int)$fakeRow->page_id, $linkCache->addLinkObj( $existing ) ); |
| 334 |
|
| 335 |
|
| 336 |
$flags = IDBAccessObject::READ_LATEST; |
| 337 |
$this->assertSame( $existing->getId(), $linkCache->addLinkObj( $existing, $flags ) ); |
| 338 |
} |
| 339 |
|
| 340 |
public function testFalsyPageName() { |
| 341 |
$linkCache = $this->newLinkCache(); |
| 342 |
|
| 343 |
|
| 344 |
$link = new TitleValue( NS_MAIN, '0' ); |
| 345 |
|
| 346 |
$linkCache->addBadLinkObj( $link ); |
| 347 |
$this->assertTrue( $linkCache->isBadLink( $link ) ); |
| 348 |
|
| 349 |
$row = $this->getPageRow(); |
| 350 |
$linkCache->addGoodLinkObjFromRow( $link, $row ); |
| 351 |
$this->assertGreaterThan( 0, $linkCache->getGoodLinkID( $link ) ); |
| 352 |
|
| 353 |
$this->assertSame( $row, $linkCache->getGoodLinkRow( NS_MAIN, '0' ) ); |
| 354 |
} |
| 355 |
|
| 356 |
public function testClearBadLinkWithString() { |
| 357 |
$linkCache = $this->newLinkCache(); |
| 358 |
$linkCache->clearBadLink( 'Xyzzy' ); |
| 359 |
$this->addToAssertionCount( 1 ); |
| 360 |
} |
| 361 |
|
| 362 |
public function testIsBadLinkWithString() { |
| 363 |
$linkCache = $this->newLinkCache(); |
| 364 |
$this->assertFalse( $linkCache->isBadLink( 'Xyzzy' ) ); |
| 365 |
} |
| 366 |
|
| 367 |
public function testGetGoodLinkIdWithString() { |
| 368 |
$linkCache = $this->newLinkCache(); |
| 369 |
$this->assertSame( 0, $linkCache->getGoodLinkID( 'Xyzzy' ) ); |
| 370 |
} |
| 371 |
|
| 372 |
public static function provideInvalidPageParams() { |
| 373 |
return [ |
| 374 |
'empty' => [ NS_MAIN, '' ], |
| 375 |
'bad chars' => [ NS_MAIN, '_|_' ], |
| 376 |
'empty in namspace' => [ NS_USER, '' ], |
| 377 |
'special' => [ NS_SPECIAL, 'RecentChanges' ], |
| 378 |
]; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function testGetGoodLinkRowWithBadParams( $ns, $dbkey ) { |
| 386 |
$linkCache = $this->newLinkCache(); |
| 387 |
$this->assertNull( $linkCache->getGoodLinkRow( $ns, $dbkey ) ); |
| 388 |
} |
| 389 |
|
| 390 |
public function getRowIfExisting( $db, $ns, $dbkey, $queryOptions ) { |
| 391 |
if ( $dbkey === 'Existing' ) { |
| 392 |
return $this->getPageRow(); |
| 393 |
} |
| 394 |
|
| 395 |
return null; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public function testGetGoodLinkRow() { |
| 403 |
$existing = new TitleValue( NS_MAIN, 'Existing' ); |
| 404 |
$missing = new TitleValue( NS_MAIN, 'Missing' ); |
| 405 |
|
| 406 |
$linkCache = $this->newLinkCache(); |
| 407 |
$callback = [ $this, 'getRowIfExisting' ]; |
| 408 |
|
| 409 |
$linkCache->getGoodLinkRow( $existing->getNamespace(), $existing->getDBkey(), $callback ); |
| 410 |
$linkCache->getGoodLinkRow( $missing->getNamespace(), $missing->getDBkey(), $callback ); |
| 411 |
|
| 412 |
$this->assertTrue( $linkCache->isBadLink( $missing ) ); |
| 413 |
$this->assertFalse( $linkCache->isBadLink( $existing ) ); |
| 414 |
|
| 415 |
$this->assertGreaterThan( 0, $linkCache->getGoodLinkID( $existing ) ); |
| 416 |
$this->assertTrue( $linkCache->isBadLink( $missing ) ); |
| 417 |
|
| 418 |
|
| 419 |
$this->assertNull( $linkCache->getGoodLinkFieldObj( $missing, 'length' ) ); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function testGetGoodLinkRowUsesCachedInfo() { |
| 426 |
$existing = new TitleValue( NS_MAIN, 'Existing' ); |
| 427 |
$missing = new TitleValue( NS_MAIN, 'Missing' ); |
| 428 |
$callback = [ $this, 'getRowIfExisting' ]; |
| 429 |
|
| 430 |
$existingRow = $this->getPageRow( 0 ); |
| 431 |
$fakeRow = $this->getPageRow( 3 ); |
| 432 |
|
| 433 |
$linkCache = $this->newLinkCache(); |
| 434 |
|
| 435 |
|
| 436 |
$linkCache->addGoodLinkObjFromRow( $missing, $fakeRow ); |
| 437 |
$linkCache->addBadLinkObj( $existing ); |
| 438 |
|
| 439 |
|
| 440 |
$this->assertSame( |
| 441 |
$fakeRow, |
| 442 |
$linkCache->getGoodLinkRow( $missing->getNamespace(), $missing->getDBkey(), $callback ) |
| 443 |
); |
| 444 |
$this->assertNull( |
| 445 |
$linkCache->getGoodLinkRow( $existing->getNamespace(), $existing->getDBkey(), $callback ) |
| 446 |
); |
| 447 |
|
| 448 |
|
| 449 |
$flags = IDBAccessObject::READ_LATEST; |
| 450 |
$this->assertNull( |
| 451 |
$linkCache->getGoodLinkRow( |
| 452 |
$missing->getNamespace(), |
| 453 |
$missing->getDBkey(), |
| 454 |
$callback, |
| 455 |
$flags |
| 456 |
) |
| 457 |
); |
| 458 |
$this->assertEquals( |
| 459 |
$existingRow, |
| 460 |
$linkCache->getGoodLinkRow( |
| 461 |
$existing->getNamespace(), |
| 462 |
$existing->getDBkey(), |
| 463 |
$callback, |
| 464 |
$flags |
| 465 |
) |
| 466 |
); |
| 467 |
|
| 468 |
|
| 469 |
$linkCache->addGoodLinkObjFromRow( $missing, $fakeRow, IDBAccessObject::READ_LATEST ); |
| 470 |
|
| 471 |
|
| 472 |
$this->assertSame( |
| 473 |
$fakeRow, |
| 474 |
$linkCache->getGoodLinkRow( $missing->getNamespace(), $missing->getDBkey(), $callback ) |
| 475 |
); |
| 476 |
|
| 477 |
|
| 478 |
$flags = IDBAccessObject::READ_LATEST; |
| 479 |
$this->assertEquals( |
| 480 |
$fakeRow, |
| 481 |
$linkCache->getGoodLinkRow( |
| 482 |
$missing->getNamespace(), |
| 483 |
$missing->getDBkey(), |
| 484 |
$callback, |
| 485 |
$flags |
| 486 |
) |
| 487 |
); |
| 488 |
} |
| 489 |
} |