Merge "Convert PageImages::fetchPageImage to a nonstatic method"

This commit is contained in:
jenkins-bot 2023-10-16 03:05:22 +00:00 committed by Gerrit Code Review
commit df9b7a9c92
2 changed files with 34 additions and 11 deletions

View file

@ -64,6 +64,15 @@ class PageImages implements
/** @var MapCacheLRU */
private static $cache = null;
/**
* @return PageImages
*/
private static function factory() {
return new self(
MediaWikiServices::getInstance()->getUserOptionsLookup()
);
}
/**
* @param UserOptionsLookup $userOptionsLookup
*/
@ -108,11 +117,21 @@ class PageImages implements
* @return File|bool
*/
public static function getPageImage( Title $title ) {
return self::factory()->getPageImageInternal( $title );
}
/**
* Return page image for a given title
*
* @param Title $title Title to get page image for
* @return File|bool
*/
public function getPageImageInternal( Title $title ) {
self::$cache ??= new MapCacheLRU( 100 );
$file = self::$cache->getWithSetCallback(
CacheKeyHelper::getKeyForPage( $title ),
fn() => self::fetchPageImage( $title )
fn() => $this->fetchPageImage( $title )
);
// Cast any cacheable null to false
@ -123,7 +142,7 @@ class PageImages implements
* @param Title $title Title to get page image for
* @return File|null|bool
*/
private static function fetchPageImage( Title $title ) {
private function fetchPageImage( Title $title ) {
if ( !$title->canExist() ) {
// Optimization: Do not query for special pages or other titles never in the database
return false;
@ -169,7 +188,7 @@ class PageImages implements
public function onInfoAction( $context, &$pageInfo ) {
global $wgThumbLimits;
$imageFile = self::getPageImage( $context->getTitle() );
$imageFile = $this->getPageImageInternal( $context->getTitle() );
if ( !$imageFile ) {
// The page has no image
return;
@ -260,7 +279,7 @@ class PageImages implements
if ( !$out->getConfig()->get( 'PageImagesOpenGraph' ) ) {
return;
}
$imageFile = self::getPageImage( $out->getContext()->getTitle() );
$imageFile = $this->getPageImageInternal( $out->getContext()->getTitle() );
if ( !$imageFile ) {
$fallback = $out->getConfig()->get( 'PageImagesOpenGraphFallbackImage' );
if ( $fallback ) {

View file

@ -21,18 +21,25 @@ use SkinTemplate;
*/
class PageImagesTest extends MediaWikiIntegrationTestCase {
private function newPageImages() {
return new PageImages(
$this->getServiceContainer()->getUserOptionsLookup()
);
}
public function testPagePropertyNames() {
$this->assertSame( 'page_image', PageImages::PROP_NAME );
$this->assertSame( 'page_image_free', PageImages::PROP_NAME_FREE );
}
public function testConstructor() {
$pageImages = new PageImages( $this->getServiceContainer()->getUserOptionsLookup() );
$pageImages = $this->newPageImages();
$this->assertInstanceOf( PageImages::class, $pageImages );
}
public function testGivenNonExistingPageGetPageImageReturnsFalse() {
$title = $this->newTitle();
$this->assertFalse( $this->newPageImages()->getPageImageInternal( $title ) );
$this->assertFalse( PageImages::getPageImage( $title ) );
}
@ -60,8 +67,7 @@ class PageImagesTest extends MediaWikiIntegrationTestCase {
->method( 'addMeta' );
$skinTemplate = new SkinTemplate();
( new PageImages( $this->getServiceContainer()->getUserOptionsLookup() ) )
->onBeforePageDisplay( $outputPage, $skinTemplate );
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate );
}
public function testGivenNonExistingPageOnBeforePageDisplayDoesNotAddMeta() {
@ -73,8 +79,7 @@ class PageImagesTest extends MediaWikiIntegrationTestCase {
->method( 'addMeta' );
$skinTemplate = new SkinTemplate();
( new PageImages( $this->getServiceContainer()->getUserOptionsLookup() ) )
->onBeforePageDisplay( $outputPage, $skinTemplate );
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate );
}
public static function provideFallbacks() {
@ -99,8 +104,7 @@ class PageImagesTest extends MediaWikiIntegrationTestCase {
->with( $this->equalTo( 'og:image' ), $this->equalTo( $expected ) );
$skinTemplate = new SkinTemplate();
( new PageImages( $this->getServiceContainer()->getUserOptionsLookup() ) )
->onBeforePageDisplay( $outputPage, $skinTemplate );
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate );
}
/**