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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
<?php
use MediaWiki\Cache\LinkCache;
use MediaWiki\Page\PageReference;
use MediaWiki\Page\PageReferenceValue;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleValue;
use Wikimedia\ObjectCache\EmptyBagOStuff;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* @group Database
* @group Cache
* @covers \MediaWiki\Cache\LinkCache
*/
class LinkCacheTest extends MediaWikiIntegrationTestCase {
use LinkCacheTestTrait;
private function newLinkCache( ?WANObjectCache $wanCache = null ) {
if ( !$wanCache ) {
$wanCache = new WANObjectCache( [ 'cache' => new EmptyBagOStuff() ] );
}
return new LinkCache(
$this->getServiceContainer()->getTitleFormatter(),
$wanCache,
$this->getServiceContainer()->getNamespaceInfo(),
$this->getServiceContainer()->getDBLoadBalancer()
);
}
public static function providePageAndLink() {
return [
[ new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ) ],
[ new TitleValue( NS_USER, __METHOD__ ) ]
];
}
public static function providePageAndLinkAndArray() {
return [
[ new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL ) ],
[ new TitleValue( NS_USER, __METHOD__ ) ],
[ [ 'page_namespace' => NS_USER, 'page_title' => __METHOD__ ] ],
];
}
private function getPageRow( $offset = 0 ) {
return (object)[
'page_id' => 8 + $offset,
'page_namespace' => 0,
'page_title' => 'Test ' . $offset,
'page_len' => 18,
'page_is_redirect' => 0,
'page_latest' => 118 + $offset,
'page_content_model' => CONTENT_MODEL_TEXT,
'page_lang' => 'xyz',
'page_is_new' => 0,
'page_touched' => '20200202020202',
];
}
/**
* @dataProvider providePageAndLinkAndArray
* @covers \MediaWiki\Cache\LinkCache::addGoodLinkObjFromRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkID()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkFieldObj()
* @covers \MediaWiki\Cache\LinkCache::clearLink()
*/
public function testAddGoodLinkObjFromRow( $page ) {
$linkCache = $this->newLinkCache();
$row = $this->getPageRow();
$dbkey = is_array( $page ) ? $page['page_title'] : $page->getDBkey();
$ns = is_array( $page ) ? $page['page_namespace'] : $page->getNamespace();
$linkCache->addBadLinkObj( $page );
$linkCache->addGoodLinkObjFromRow( $page, $row );
$this->assertEquals(
$row,
$linkCache->getGoodLinkRow( $ns, $dbkey )
);
$this->assertSame( $row->page_id, $linkCache->getGoodLinkID( $page ) );
$this->assertFalse( $linkCache->isBadLink( $page ) );
$this->assertSame(
$row->page_id,
$linkCache->getGoodLinkFieldObj( $page, 'id' )
);
$this->assertSame(
$row->page_len,
$linkCache->getGoodLinkFieldObj( $page, 'length' )
);
$this->assertSame(
$row->page_is_redirect,
$linkCache->getGoodLinkFieldObj( $page, 'redirect' )
);
$this->assertSame(
$row->page_latest,
$linkCache->getGoodLinkFieldObj( $page, 'revision' )
);
$this->assertSame(
$row->page_content_model,
$linkCache->getGoodLinkFieldObj( $page, 'model' )
);
$this->assertSame(
$row->page_lang,
$linkCache->getGoodLinkFieldObj( $page, 'lang' )
);
$this->assertEquals(
$row,
$linkCache->getGoodLinkRow( $ns, $dbkey )
);
$linkCache->clearBadLink( $page );
$this->assertNotNull( $linkCache->getGoodLinkID( $page ) );
$this->assertNotNull( $linkCache->getGoodLinkFieldObj( $page, 'length' ) );
$linkCache->clearLink( $page );
$this->assertSame( 0, $linkCache->getGoodLinkID( $page ) );
$this->assertNull( $linkCache->getGoodLinkFieldObj( $page, 'length' ) );
$this->assertNull( $linkCache->getGoodLinkRow( $ns, $dbkey ) );
}
/**
* @covers \MediaWiki\Cache\LinkCache::addGoodLinkObjFromRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkID()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkFieldObj()
*/
public function testAddGoodLinkObjWithAllParameters() {
$linkCache = $this->getServiceContainer()->getLinkCache();
$page = new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL );
$this->addGoodLinkObject( 8, $page, 18, 0, 118, CONTENT_MODEL_TEXT, 'xyz' );
$row = $linkCache->getGoodLinkRow( $page->getNamespace(), $page->getDBkey() );
$this->assertEquals( 8, (int)$row->page_id );
$this->assertSame( 8, $linkCache->getGoodLinkID( $page ) );
$this->assertSame( 8, $linkCache->getGoodLinkFieldObj( $page, 'id' ) );
$this->assertSame(
18,
$linkCache->getGoodLinkFieldObj( $page, 'length' )
);
$this->assertSame(
0,
$linkCache->getGoodLinkFieldObj( $page, 'redirect' )
);
$this->assertSame(
118,
$linkCache->getGoodLinkFieldObj( $page, 'revision' )
);
$this->assertSame(
CONTENT_MODEL_TEXT,
$linkCache->getGoodLinkFieldObj( $page, 'model' )
);
$this->assertSame(
'xyz',
$linkCache->getGoodLinkFieldObj( $page, 'lang' )
);
}
/**
* @covers \MediaWiki\Cache\LinkCache::addGoodLinkObjFromRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkID()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkFieldObj()
*/
public function testAddGoodLinkObjFromRowWithMinimalParameters() {
$linkCache = $this->getServiceContainer()->getLinkCache();
$page = new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL );
$this->addGoodLinkObject( 8, $page );
$expectedRow = [
'page_id' => 8,
'page_len' => -1,
'page_is_redirect' => 0,
'page_latest' => 0,
'page_content_model' => null,
'page_lang' => null,
];
$actualRow = (array)$linkCache->getGoodLinkRow( $page->getNamespace(), $page->getDBkey() );
$this->assertEquals(
$expectedRow,
array_intersect_key( $actualRow, $expectedRow )
);
$this->assertSame( 8, $linkCache->getGoodLinkID( $page ) );
$this->assertSame( 8, $linkCache->getGoodLinkFieldObj( $page, 'id' ) );
$this->assertSame(
-1,
$linkCache->getGoodLinkFieldObj( $page, 'length' )
);
$this->assertSame(
0,
$linkCache->getGoodLinkFieldObj( $page, 'redirect' )
);
$this->assertSame(
0,
$linkCache->getGoodLinkFieldObj( $page, 'revision' )
);
$this->assertSame(
null,
$linkCache->getGoodLinkFieldObj( $page, 'model' )
);
$this->assertSame(
null,
$linkCache->getGoodLinkFieldObj( $page, 'lang' )
);
}
/**
* @covers \MediaWiki\Cache\LinkCache::addGoodLinkObjFromRow()
*/
public function testAddGoodLinkObjFromRowWithInterwikiLink() {
$linkCache = $this->getServiceContainer()->getLinkCache();
$page = new TitleValue( NS_USER, __METHOD__, '', 'acme' );
$this->addGoodLinkObject( 8, $page );
$this->assertSame( 0, $linkCache->getGoodLinkID( $page ) );
}
/**
* @dataProvider providePageAndLink
* @covers \MediaWiki\Cache\LinkCache::addBadLinkObj()
* @covers \MediaWiki\Cache\LinkCache::isBadLink()
* @covers \MediaWiki\Cache\LinkCache::clearLink()
*/
public function testAddBadLinkObj( $key ) {
$linkCache = $this->getServiceContainer()->getLinkCache();
$this->assertFalse( $linkCache->isBadLink( $key ) );
$this->addGoodLinkObject( 17, $key );
$linkCache->addBadLinkObj( $key );
$this->assertTrue( $linkCache->isBadLink( $key ) );
$this->assertSame( 0, $linkCache->getGoodLinkID( $key ) );
$linkCache->clearLink( $key );
$this->assertFalse( $linkCache->isBadLink( $key ) );
}
/**
* @covers \MediaWiki\Cache\LinkCache::addBadLinkObj()
*/
public function testAddBadLinkObjWithInterwikiLink() {
$linkCache = $this->newLinkCache();
$page = new TitleValue( NS_USER, __METHOD__, '', 'acme' );
$linkCache->addBadLinkObj( $page );
$this->assertFalse( $linkCache->isBadLink( $page ) );
}
/**
* @covers \MediaWiki\Cache\LinkCache::addLinkObj()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkFieldObj
*/
public function testAddLinkObj() {
$existing = $this->getExistingTestPage();
$missing = $this->getNonexistingTestPage();
$linkCache = $this->newLinkCache();
$linkCache->addLinkObj( $existing );
$linkCache->addLinkObj( $missing );
$this->assertTrue( $linkCache->isBadLink( $missing ) );
$this->assertFalse( $linkCache->isBadLink( $existing ) );
$this->assertSame( $existing->getId(), $linkCache->getGoodLinkID( $existing ) );
$this->assertTrue( $linkCache->isBadLink( $missing ) );
// Make sure nothing explodes when getting a field from a non-existing entry
$this->assertNull( $linkCache->getGoodLinkFieldObj( $missing, 'length' ) );
}
/**
* @covers \MediaWiki\Cache\LinkCache::addLinkObj()
*/
public function testAddLinkObjUsesCachedInfo() {
$existing = $this->getExistingTestPage();
$missing = $this->getNonexistingTestPage();
$fakeRow = $this->getPageRow( $existing->getId() + 100 );
$linkCache = $this->newLinkCache();
// pretend the existing page is missing, and the missing page exists
$linkCache->addGoodLinkObjFromRow( $missing, $fakeRow );
$linkCache->addBadLinkObj( $existing );
// the LinkCache should use the cached info and not look into the database
$this->assertSame( (int)$fakeRow->page_id, $linkCache->addLinkObj( $missing ) );
$this->assertSame( 0, $linkCache->addLinkObj( $existing ) );
// now set the "read latest" flag and try again
$flags = IDBAccessObject::READ_LATEST;
$this->assertSame( 0, $linkCache->addLinkObj( $missing, $flags ) );
$this->assertSame( $existing->getId(), $linkCache->addLinkObj( $existing, $flags ) );
}
/**
* @covers \MediaWiki\Cache\LinkCache::addLinkObj()
*/
public function testAddLinkObjUsesWANCache() {
// For some namespaces we cache data (Template, File, etc)
$existing = $this->getExistingTestPage( Title::makeTitle( NS_TEMPLATE, __METHOD__ ) );
$wanCache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
$linkCache = $this->newLinkCache( $wanCache );
// load the page row into the cache
$linkCache->addLinkObj( $existing );
// replace real row data with fake, and assert that it gets used
$key = $wanCache->makeKey( 'page', $existing->getNamespace(), sha1( $existing->getDBkey() ) );
$fakeRow = $this->getPageRow( $existing->getId() + 100 );
$wanCache->set( $key, $fakeRow );
// clear in-class cache
$linkCache->clearLink( $existing );
$this->assertSame( (int)$fakeRow->page_id, $linkCache->addLinkObj( $existing ) );
// set the "read latest" flag and try again
$flags = IDBAccessObject::READ_LATEST;
$this->assertSame( $existing->getId(), $linkCache->addLinkObj( $existing, $flags ) );
}
public function testFalsyPageName() {
$linkCache = $this->newLinkCache();
// The stringified value is "0", which is falsy in PHP!
$link = new TitleValue( NS_MAIN, '0' );
$linkCache->addBadLinkObj( $link );
$this->assertTrue( $linkCache->isBadLink( $link ) );
$row = $this->getPageRow();
$linkCache->addGoodLinkObjFromRow( $link, $row );
$this->assertGreaterThan( 0, $linkCache->getGoodLinkID( $link ) );
$this->assertSame( $row, $linkCache->getGoodLinkRow( NS_MAIN, '0' ) );
}
public function testClearBadLinkWithString() {
$linkCache = $this->newLinkCache();
$linkCache->clearBadLink( 'Xyzzy' );
$this->addToAssertionCount( 1 );
}
public function testIsBadLinkWithString() {
$linkCache = $this->newLinkCache();
$this->assertFalse( $linkCache->isBadLink( 'Xyzzy' ) );
}
public function testGetGoodLinkIdWithString() {
$linkCache = $this->newLinkCache();
$this->assertSame( 0, $linkCache->getGoodLinkID( 'Xyzzy' ) );
}
public static function provideInvalidPageParams() {
return [
'empty' => [ NS_MAIN, '' ],
'bad chars' => [ NS_MAIN, '_|_' ],
'empty in namspace' => [ NS_USER, '' ],
'special' => [ NS_SPECIAL, 'RecentChanges' ],
];
}
/**
* @dataProvider provideInvalidPageParams
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkRow()
*/
public function testGetGoodLinkRowWithBadParams( $ns, $dbkey ) {
$linkCache = $this->newLinkCache();
$this->assertNull( $linkCache->getGoodLinkRow( $ns, $dbkey ) );
}
public function getRowIfExisting( $db, $ns, $dbkey, $queryOptions ) {
if ( $dbkey === 'Existing' ) {
return $this->getPageRow();
}
return null;
}
/**
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkRow()
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkFieldObj
*/
public function testGetGoodLinkRow() {
$existing = new TitleValue( NS_MAIN, 'Existing' );
$missing = new TitleValue( NS_MAIN, 'Missing' );
$linkCache = $this->newLinkCache();
$callback = [ $this, 'getRowIfExisting' ];
$linkCache->getGoodLinkRow( $existing->getNamespace(), $existing->getDBkey(), $callback );
$linkCache->getGoodLinkRow( $missing->getNamespace(), $missing->getDBkey(), $callback );
$this->assertTrue( $linkCache->isBadLink( $missing ) );
$this->assertFalse( $linkCache->isBadLink( $existing ) );
$this->assertGreaterThan( 0, $linkCache->getGoodLinkID( $existing ) );
$this->assertTrue( $linkCache->isBadLink( $missing ) );
// Make sure nothing explodes when getting a field from a non-existing entry
$this->assertNull( $linkCache->getGoodLinkFieldObj( $missing, 'length' ) );
}
/**
* @covers \MediaWiki\Cache\LinkCache::getGoodLinkRow()
*/
public function testGetGoodLinkRowUsesCachedInfo() {
$existing = new TitleValue( NS_MAIN, 'Existing' );
$missing = new TitleValue( NS_MAIN, 'Missing' );
$callback = [ $this, 'getRowIfExisting' ];
$existingRow = $this->getPageRow( 0 );
$fakeRow = $this->getPageRow( 3 );
$linkCache = $this->newLinkCache();
// pretend the existing page is missing, and the missing page exists
$linkCache->addGoodLinkObjFromRow( $missing, $fakeRow );
$linkCache->addBadLinkObj( $existing );
// the LinkCache should use the cached info and not look into the database
$this->assertSame(
$fakeRow,
$linkCache->getGoodLinkRow( $missing->getNamespace(), $missing->getDBkey(), $callback )
);
$this->assertNull(
$linkCache->getGoodLinkRow( $existing->getNamespace(), $existing->getDBkey(), $callback )
);
// now set the "read latest" flag and try again
$flags = IDBAccessObject::READ_LATEST;
$this->assertNull(
$linkCache->getGoodLinkRow(
$missing->getNamespace(),
$missing->getDBkey(),
$callback,
$flags
)
);
$this->assertEquals(
$existingRow,
$linkCache->getGoodLinkRow(
$existing->getNamespace(),
$existing->getDBkey(),
$callback,
$flags
)
);
// pretend again that the missing page exists, but pretend even harder
$linkCache->addGoodLinkObjFromRow( $missing, $fakeRow, IDBAccessObject::READ_LATEST );
// the LinkCache should use the cached info and not look into the database
$this->assertSame(
$fakeRow,
$linkCache->getGoodLinkRow( $missing->getNamespace(), $missing->getDBkey(), $callback )
);
// now set the "read latest" flag and try again
$flags = IDBAccessObject::READ_LATEST;
$this->assertEquals(
$fakeRow,
$linkCache->getGoodLinkRow(
$missing->getNamespace(),
$missing->getDBkey(),
$callback,
$flags
)
);
}
}
|