Add BacklinkCacheFactory Service

Bug: T279433
Change-Id: I2943935e2d8148fce4457f76eca0234be72a5a5a
This commit is contained in:
Derick Alangi 2021-09-08 18:19:11 +01:00
parent 69039db455
commit 96bc83e8eb
7 changed files with 137 additions and 9 deletions

View file

@ -631,6 +631,8 @@ because of Phabricator reports.
It wasn't used and job given the purpose of JobSpecification class it's It wasn't used and job given the purpose of JobSpecification class it's
not needed. not needed.
* The protected method File::getImageSize() is deprecated. * The protected method File::getImageSize() is deprecated.
* BacklinkCache::get() was deprecated, use
BacklinkCacheFactory::getBacklinkCache() instead.
* MediaHandler::getImageSize(), ::getMetadata() and ::isMetadataValid were * MediaHandler::getImageSize(), ::getMetadata() and ::isMetadataValid were
deprecated and should no longer be overridden. Instead, subclasses should deprecated and should no longer be overridden. Instead, subclasses should
override getSizeAndMetadata(). override getSizeAndMetadata().

View file

@ -38,6 +38,7 @@ use MediaWiki\Block\BlockUserFactory;
use MediaWiki\Block\BlockUtils; use MediaWiki\Block\BlockUtils;
use MediaWiki\Block\DatabaseBlockStore; use MediaWiki\Block\DatabaseBlockStore;
use MediaWiki\Block\UnblockUserFactory; use MediaWiki\Block\UnblockUserFactory;
use MediaWiki\Cache\BacklinkCacheFactory;
use MediaWiki\Cache\LinkBatchFactory; use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\Collation\CollationFactory; use MediaWiki\Collation\CollationFactory;
use MediaWiki\Config\ConfigRepository; use MediaWiki\Config\ConfigRepository;
@ -607,6 +608,14 @@ class MediaWikiServices extends ServiceContainer {
return $this->getService( 'AuthManager' ); return $this->getService( 'AuthManager' );
} }
/**
* @since 1.37
* @return BacklinkCacheFactory
*/
public function getBacklinkCacheFactory(): BacklinkCacheFactory {
return $this->getService( 'BacklinkCacheFactory' );
}
/** /**
* @since 1.34 * @since 1.34
* @return BadFileLookup * @return BadFileLookup

View file

@ -58,6 +58,7 @@ use MediaWiki\Block\BlockUtils;
use MediaWiki\Block\DatabaseBlockStore; use MediaWiki\Block\DatabaseBlockStore;
use MediaWiki\Block\UnblockUserFactory; use MediaWiki\Block\UnblockUserFactory;
use MediaWiki\Block\UserBlockCommandFactory; use MediaWiki\Block\UserBlockCommandFactory;
use MediaWiki\Cache\BacklinkCacheFactory;
use MediaWiki\Cache\LinkBatchFactory; use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\Collation\CollationFactory; use MediaWiki\Collation\CollationFactory;
use MediaWiki\Config\ConfigRepository; use MediaWiki\Config\ConfigRepository;
@ -211,6 +212,10 @@ return [
return $authManager; return $authManager;
}, },
'BacklinkCacheFactory' => static function ( MediaWikiServices $services ): BacklinkCacheFactory {
return new BacklinkCacheFactory( $services->getMainWANObjectCache() );
},
'BadFileLookup' => static function ( MediaWikiServices $services ): BadFileLookup { 'BadFileLookup' => static function ( MediaWikiServices $services ): BadFileLookup {
return new BadFileLookup( return new BadFileLookup(
static function () { static function () {

View file

@ -77,9 +77,7 @@ class BacklinkCache {
*/ */
protected $fullResultCache = []; protected $fullResultCache = [];
/** /** @var WANObjectCache */
* @var WANObjectCache
*/
protected $wanCache; protected $wanCache;
/** /**
@ -102,11 +100,12 @@ class BacklinkCache {
/** /**
* Create a new BacklinkCache * Create a new BacklinkCache
* *
* @param WANObjectCache $wanCache
* @param PageReference $page Page to create a backlink cache for * @param PageReference $page Page to create a backlink cache for
*/ */
public function __construct( PageReference $page ) { public function __construct( WANObjectCache $wanCache, PageReference $page ) {
$this->page = $page; $this->page = $page;
$this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); $this->wanCache = $wanCache;
} }
/** /**
@ -114,14 +113,23 @@ class BacklinkCache {
* Currently, only one cache instance can exist; callers that * Currently, only one cache instance can exist; callers that
* need multiple backlink cache objects should keep them in scope. * need multiple backlink cache objects should keep them in scope.
* *
* @deprecated since 1.37 Use BacklinkCacheFactory::getBacklinkCache() instead
*
* @param PageReference $page Page to get a backlink cache for * @param PageReference $page Page to get a backlink cache for
* @return BacklinkCache * @return BacklinkCache
*/ */
public static function get( PageReference $page ): self { public static function get( PageReference $page ): self {
if ( !self::$instance || !self::$instance->page->isSamePageAs( $page ) ) { $backlinkCacheFactory = MediaWikiServices::getInstance()->getBacklinkCacheFactory();
self::$instance = new self( $page );
} return $backlinkCacheFactory->getBacklinkCache( $page );
return self::$instance; }
/**
* @since 1.37
* @return PageReference
*/
public function getPage(): PageReference {
return $this->page;
} }
/** /**

63
includes/cache/BacklinkCacheFactory.php vendored Normal file
View file

@ -0,0 +1,63 @@
<?php
/**
* Remember the page that was previously loaded.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Cache
*/
namespace MediaWiki\Cache;
use BacklinkCache;
use MediaWiki\Page\PageReference;
use WANObjectCache;
/**
* @since 1.37
*/
class BacklinkCacheFactory {
/** @var BacklinkCache */
private $latestBacklinkCache;
/** @var WANObjectCache */
private $wanCache;
/**
* @param WANObjectCache $wanCache
*/
public function __construct( WANObjectCache $wanCache ) {
$this->wanCache = $wanCache;
}
/**
* Returns a BacklinkCache for $page. May re-use previously
* created instances.
*
* Currently, only one cache instance can exist; callers that
* need multiple backlink cache objects should keep them in scope.
*
* @param PageReference $page Page to get a backlink cache for
* @return BacklinkCache
*/
public function getBacklinkCache( PageReference $page ): BacklinkCache {
if ( !$this->latestBacklinkCache || !$this->latestBacklinkCache->getPage()->isSamePageAs( $page ) ) {
$this->latestBacklinkCache = new BacklinkCache( $this->wanCache, $page );
}
return $this->latestBacklinkCache;
}
}

View file

@ -1,5 +1,7 @@
<?php <?php
use MediaWiki\Page\PageReferenceValue;
/** /**
* @group Database * @group Database
* @group Cache * @group Cache
@ -172,4 +174,13 @@ class BacklinkCacheTest extends MediaWikiIntegrationTestCase {
$this->assertTrue( self::$backlinkCacheTest['title']->isSamePageAs( $array[0] ) ); $this->assertTrue( self::$backlinkCacheTest['title']->isSamePageAs( $array[0] ) );
} }
/**
* @covers BacklinkCache::get
*/
public function testGet() {
$page = PageReferenceValue::localReference( NS_CATEGORY, "kittens" );
$cache = BacklinkCache::get( $page );
$this->assertTrue( $cache->getPage()->isSamePageAs( $page ) );
}
} }

View file

@ -0,0 +1,30 @@
<?php
use MediaWiki\Cache\BacklinkCacheFactory;
use MediaWiki\Page\PageReferenceValue;
/**
* @group Cache
*/
class BacklinkCacheFactoryTest extends MediaWikiUnitTestCase {
/**
* @covers MediaWiki\Cache\BacklinkCacheFactory::getBacklinkCache
*/
public function testGetBacklinkCache() {
$wanCache = new WANObjectCache( [ 'cache' => new EmptyBagOStuff() ] );
$page = PageReferenceValue::localReference( NS_CATEGORY, "kittens" );
$factory = new BacklinkCacheFactory( $wanCache );
$cache = $factory->getBacklinkCache( $page );
$this->assertTrue( $cache->getPage()->isSamePageAs( $page ) );
$cache2 = $factory->getBacklinkCache( $page );
$this->assertSame( $cache, $cache2 );
$page2 = PageReferenceValue::localReference( NS_CATEGORY, "doggos" );
$cache2 = $factory->getBacklinkCache( $page2 );
$this->assertNotSame( $cache, $cache2 );
$this->assertTrue( $cache2->getPage()->isSamePageAs( $page2 ) );
}
}