mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-28 00:40:12 +00:00
Don't pass a Title object around that's not needed
Change-Id: Iea9c366c4b45ba4cd9171c8b4fffc307c852b6e2
This commit is contained in:
parent
e389e3c1bb
commit
9d2d61ff09
|
@ -74,7 +74,7 @@ class ApiQueryReferences extends ApiQueryBase {
|
||||||
} else {
|
} else {
|
||||||
$startId = false;
|
$startId = false;
|
||||||
}
|
}
|
||||||
$storedRefs = $this->getStoredReferences( $title );
|
$storedRefs = $this->getStoredReferences( $pageId );
|
||||||
$allReferences = [];
|
$allReferences = [];
|
||||||
// some pages may not have references stored
|
// some pages may not have references stored
|
||||||
if ( $storedRefs !== false ) {
|
if ( $storedRefs !== false ) {
|
||||||
|
@ -111,24 +111,24 @@ class ApiQueryReferences extends ApiQueryBase {
|
||||||
* Fetch references stored for the given title in page_props
|
* Fetch references stored for the given title in page_props
|
||||||
* For performance, results are cached
|
* For performance, results are cached
|
||||||
*
|
*
|
||||||
* @param Title $title
|
* @param int $pageId
|
||||||
* @return array|false
|
* @return array|false
|
||||||
*/
|
*/
|
||||||
private function getStoredReferences( Title $title ) {
|
private function getStoredReferences( $pageId ) {
|
||||||
global $wgCiteStoreReferencesData;
|
global $wgCiteStoreReferencesData;
|
||||||
if ( !$wgCiteStoreReferencesData ) {
|
if ( !$wgCiteStoreReferencesData ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
||||||
$key = $cache->makeKey( Cite::EXT_DATA_KEY, $title->getArticleID() );
|
$key = $cache->makeKey( Cite::EXT_DATA_KEY, $pageId );
|
||||||
return $cache->getWithSetCallback(
|
return $cache->getWithSetCallback(
|
||||||
$key,
|
$key,
|
||||||
self::CACHE_DURATION_ONFETCH,
|
self::CACHE_DURATION_ONFETCH,
|
||||||
function ( $oldValue, &$ttl, array &$setOpts ) use ( $title ) {
|
function ( $oldValue, &$ttl, array &$setOpts ) use ( $pageId ) {
|
||||||
$dbr = wfGetDB( DB_REPLICA );
|
$dbr = wfGetDB( DB_REPLICA );
|
||||||
$setOpts += Database::getCacheSetOptions( $dbr );
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
||||||
return $this->recursiveFetchRefsFromDB( $title, $dbr );
|
return $this->recursiveFetchRefsFromDB( $pageId, $dbr );
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
'checkKeys' => [ $key ],
|
'checkKeys' => [ $key ],
|
||||||
|
@ -142,24 +142,23 @@ class ApiQueryReferences extends ApiQueryBase {
|
||||||
* It attempts the next step when a decoding error occurs.
|
* It attempts the next step when a decoding error occurs.
|
||||||
* Returns json_decoded uncompressed string, with validation of json
|
* Returns json_decoded uncompressed string, with validation of json
|
||||||
*
|
*
|
||||||
* @param Title $title
|
* @param int $pageId
|
||||||
* @param IDatabase $dbr
|
* @param IDatabase $dbr
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @param int $i
|
* @param int $i
|
||||||
* @return array|false
|
* @return array|false
|
||||||
*/
|
*/
|
||||||
private function recursiveFetchRefsFromDB(
|
private function recursiveFetchRefsFromDB(
|
||||||
Title $title,
|
$pageId,
|
||||||
IDatabase $dbr,
|
IDatabase $dbr,
|
||||||
$string = '',
|
$string = '',
|
||||||
$i = 1
|
$i = 1
|
||||||
) {
|
) {
|
||||||
$id = $title->getArticleID();
|
|
||||||
$result = $dbr->selectField(
|
$result = $dbr->selectField(
|
||||||
'page_props',
|
'page_props',
|
||||||
'pp_value',
|
'pp_value',
|
||||||
[
|
[
|
||||||
'pp_page' => $id,
|
'pp_page' => $pageId,
|
||||||
'pp_propname' => 'references-' . $i
|
'pp_propname' => 'references-' . $i
|
||||||
],
|
],
|
||||||
__METHOD__
|
__METHOD__
|
||||||
|
@ -168,7 +167,7 @@ class ApiQueryReferences extends ApiQueryBase {
|
||||||
// no refs stored in page_props at this index
|
// no refs stored in page_props at this index
|
||||||
if ( $i > 1 ) {
|
if ( $i > 1 ) {
|
||||||
// shouldn't happen
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -182,10 +181,10 @@ class ApiQueryReferences extends ApiQueryBase {
|
||||||
}
|
}
|
||||||
// corrupted json ?
|
// corrupted json ?
|
||||||
// shouldn't happen since when string is truncated, gzdecode should fail
|
// 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
|
// if gzdecode fails, try to fetch next references- property value
|
||||||
return $this->recursiveFetchRefsFromDB( $title, $dbr, $string, ++$i );
|
return $this->recursiveFetchRefsFromDB( $pageId, $dbr, $string, ++$i );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -6,7 +6,6 @@ use ApiMain;
|
||||||
use ApiQuery;
|
use ApiQuery;
|
||||||
use ApiQueryReferences;
|
use ApiQueryReferences;
|
||||||
use IContextSource;
|
use IContextSource;
|
||||||
use Title;
|
|
||||||
use Wikimedia\AtEase\AtEase;
|
use Wikimedia\AtEase\AtEase;
|
||||||
use Wikimedia\Rdbms\IDatabase;
|
use Wikimedia\Rdbms\IDatabase;
|
||||||
use Wikimedia\TestingAccessWrapper;
|
use Wikimedia\TestingAccessWrapper;
|
||||||
|
@ -36,8 +35,7 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase {
|
||||||
*/
|
*/
|
||||||
public function testGetStoredReferences_storeDisabled() {
|
public function testGetStoredReferences_storeDisabled() {
|
||||||
$api = $this->newApiQueryReferences();
|
$api = $this->newApiQueryReferences();
|
||||||
$title = $this->createMock( Title::class );
|
$this->assertFalse( $api->getStoredReferences( 0 ) );
|
||||||
$this->assertFalse( $api->getStoredReferences( $title ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,13 +43,12 @@ class ApiQueryReferencesTest extends \MediaWikiUnitTestCase {
|
||||||
*/
|
*/
|
||||||
public function testRecursiveFetchRefsFromDB_fails() {
|
public function testRecursiveFetchRefsFromDB_fails() {
|
||||||
$api = $this->newApiQueryReferences();
|
$api = $this->newApiQueryReferences();
|
||||||
$title = $this->createMock( Title::class );
|
|
||||||
|
|
||||||
$dbr = $this->createMock( IDatabase::class );
|
$dbr = $this->createMock( IDatabase::class );
|
||||||
$dbr->method( 'selectField' )
|
$dbr->method( 'selectField' )
|
||||||
->willReturn( false );
|
->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() {
|
public function testRecursiveFetchRefsFromDB_firstTry() {
|
||||||
$api = $this->newApiQueryReferences();
|
$api = $this->newApiQueryReferences();
|
||||||
$title = $this->createMock( Title::class );
|
|
||||||
|
|
||||||
$dbr = $this->createMock( IDatabase::class );
|
$dbr = $this->createMock( IDatabase::class );
|
||||||
$dbr->method( 'selectField' )
|
$dbr->method( 'selectField' )
|
||||||
->willReturn( gzencode( '{"refs":{}}' ) );
|
->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() {
|
public function testRecursiveFetchRefsFromDB_secondTry() {
|
||||||
$api = $this->newApiQueryReferences();
|
$api = $this->newApiQueryReferences();
|
||||||
$title = $this->createMock( Title::class );
|
|
||||||
|
|
||||||
$dbr = $this->createMock( IDatabase::class );
|
$dbr = $this->createMock( IDatabase::class );
|
||||||
$dbr->expects( $this->exactly( 2 ) )
|
$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
|
// Code relies on gzdecode() returning false, but that reports an error now
|
||||||
AtEase::suppressWarnings();
|
AtEase::suppressWarnings();
|
||||||
$refs = $api->recursiveFetchRefsFromDB( $title, $dbr );
|
$refs = $api->recursiveFetchRefsFromDB( 0, $dbr );
|
||||||
AtEase::restoreWarnings();
|
AtEase::restoreWarnings();
|
||||||
|
|
||||||
$this->assertSame( [ 'refs' => [] ], $refs );
|
$this->assertSame( [ 'refs' => [] ], $refs );
|
||||||
|
|
Loading…
Reference in a new issue