diff --git a/includes/ApiQueryReferences.php b/includes/ApiQueryReferences.php index eefb2ac25..3b4c85d2c 100644 --- a/includes/ApiQueryReferences.php +++ b/includes/ApiQueryReferences.php @@ -74,7 +74,7 @@ class ApiQueryReferences extends ApiQueryBase { } else { $startId = false; } - $storedRefs = $this->getStoredReferences( $title ); + $storedRefs = $this->getStoredReferences( $pageId ); $allReferences = []; // some pages may not have references stored if ( $storedRefs !== false ) { @@ -111,24 +111,24 @@ class ApiQueryReferences extends ApiQueryBase { * Fetch references stored for the given title in page_props * For performance, results are cached * - * @param Title $title + * @param int $pageId * @return array|false */ - private function getStoredReferences( Title $title ) { + private function getStoredReferences( $pageId ) { global $wgCiteStoreReferencesData; if ( !$wgCiteStoreReferencesData ) { return false; } $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); - $key = $cache->makeKey( Cite::EXT_DATA_KEY, $title->getArticleID() ); + $key = $cache->makeKey( Cite::EXT_DATA_KEY, $pageId ); return $cache->getWithSetCallback( $key, self::CACHE_DURATION_ONFETCH, - function ( $oldValue, &$ttl, array &$setOpts ) use ( $title ) { + function ( $oldValue, &$ttl, array &$setOpts ) use ( $pageId ) { $dbr = wfGetDB( DB_REPLICA ); $setOpts += Database::getCacheSetOptions( $dbr ); - return $this->recursiveFetchRefsFromDB( $title, $dbr ); + return $this->recursiveFetchRefsFromDB( $pageId, $dbr ); }, [ 'checkKeys' => [ $key ], @@ -142,24 +142,23 @@ class ApiQueryReferences extends ApiQueryBase { * It attempts the next step when a decoding error occurs. * Returns json_decoded uncompressed string, with validation of json * - * @param Title $title + * @param int $pageId * @param IDatabase $dbr * @param string $string * @param int $i * @return array|false */ private function recursiveFetchRefsFromDB( - Title $title, + $pageId, IDatabase $dbr, $string = '', $i = 1 ) { - $id = $title->getArticleID(); $result = $dbr->selectField( 'page_props', 'pp_value', [ - 'pp_page' => $id, + 'pp_page' => $pageId, 'pp_propname' => 'references-' . $i ], __METHOD__ @@ -168,7 +167,7 @@ class ApiQueryReferences extends ApiQueryBase { // no refs stored in page_props at this index if ( $i > 1 ) { // shouldn't happen - wfDebug( "Failed to retrieve stored references for title id $id" ); + wfDebug( "Failed to retrieve stored references for title id $pageId" ); } return false; } @@ -182,10 +181,10 @@ class ApiQueryReferences extends ApiQueryBase { } // corrupted json ? // shouldn't happen since when string is truncated, gzdecode should fail - wfDebug( "Corrupted json detected when retrieving stored references for title id $id" ); + wfDebug( "Corrupted json detected when retrieving stored references for title id $pageId" ); } // if gzdecode fails, try to fetch next references- property value - return $this->recursiveFetchRefsFromDB( $title, $dbr, $string, ++$i ); + return $this->recursiveFetchRefsFromDB( $pageId, $dbr, $string, ++$i ); } /** diff --git a/tests/phpunit/unit/ApiQueryReferencesTest.php b/tests/phpunit/unit/ApiQueryReferencesTest.php index 3485f6937..56731b374 100644 --- a/tests/phpunit/unit/ApiQueryReferencesTest.php +++ b/tests/phpunit/unit/ApiQueryReferencesTest.php @@ -6,7 +6,6 @@ use ApiMain; use ApiQuery; use ApiQueryReferences; use IContextSource; -use Title; use Wikimedia\AtEase\AtEase; use Wikimedia\Rdbms\IDatabase; use Wikimedia\TestingAccessWrapper; @@ -36,8 +35,7 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { */ public function testGetStoredReferences_storeDisabled() { $api = $this->newApiQueryReferences(); - $title = $this->createMock( Title::class ); - $this->assertFalse( $api->getStoredReferences( $title ) ); + $this->assertFalse( $api->getStoredReferences( 0 ) ); } /** @@ -45,13 +43,12 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { */ public function testRecursiveFetchRefsFromDB_fails() { $api = $this->newApiQueryReferences(); - $title = $this->createMock( Title::class ); $dbr = $this->createMock( IDatabase::class ); $dbr->method( 'selectField' ) ->willReturn( false ); - $this->assertFalse( $api->recursiveFetchRefsFromDB( $title, $dbr ) ); + $this->assertFalse( $api->recursiveFetchRefsFromDB( 0, $dbr ) ); } /** @@ -59,13 +56,12 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { */ public function testRecursiveFetchRefsFromDB_firstTry() { $api = $this->newApiQueryReferences(); - $title = $this->createMock( Title::class ); $dbr = $this->createMock( IDatabase::class ); $dbr->method( 'selectField' ) ->willReturn( gzencode( '{"refs":{}}' ) ); - $this->assertSame( [ 'refs' => [] ], $api->recursiveFetchRefsFromDB( $title, $dbr ) ); + $this->assertSame( [ 'refs' => [] ], $api->recursiveFetchRefsFromDB( 0, $dbr ) ); } /** @@ -73,7 +69,6 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { */ public function testRecursiveFetchRefsFromDB_secondTry() { $api = $this->newApiQueryReferences(); - $title = $this->createMock( Title::class ); $dbr = $this->createMock( IDatabase::class ); $dbr->expects( $this->exactly( 2 ) ) @@ -82,7 +77,7 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase { // Code relies on gzdecode() returning false, but that reports an error now AtEase::suppressWarnings(); - $refs = $api->recursiveFetchRefsFromDB( $title, $dbr ); + $refs = $api->recursiveFetchRefsFromDB( 0, $dbr ); AtEase::restoreWarnings(); $this->assertSame( [ 'refs' => [] ], $refs );