diff --git a/includes/title/Title.php b/includes/title/Title.php index 15b60f7e8b3..0aa9ce80b63 100644 --- a/includes/title/Title.php +++ b/includes/title/Title.php @@ -82,6 +82,12 @@ class Title implements Stringable, LinkTarget, PageIdentity { /** @var MapCacheLRU|null */ private static $titleCache = null; + /** + * Cached instance of the main page title, to speed up isMainPage() checks. + * @var Title|null + */ + private static ?Title $cachedMainPage = null; + /** * Title::newFromText maintains a cache to avoid expensive re-normalization of * commonly used titles. On a batch operation this can become a memory leak @@ -1383,10 +1389,8 @@ class Title implements Stringable, LinkTarget, PageIdentity { * @return bool */ public function isMainPage() { - /** @var Title|null */ - static $cachedMainPage; - $cachedMainPage ??= self::newMainPage(); - return $this->equals( $cachedMainPage ); + self::$cachedMainPage ??= self::newMainPage(); + return $this->equals( self::$cachedMainPage ); } /** @@ -2693,6 +2697,9 @@ class Title implements Stringable, LinkTarget, PageIdentity { $linkCache = MediaWikiServices::getInstance()->getLinkCache(); $linkCache->clear(); + // Reset cached main page instance (T395214). + self::$cachedMainPage = null; + $titleCache = self::getTitleCache(); $titleCache->clear(); } diff --git a/tests/phpunit/MediaWikiIntegrationTestCase.php b/tests/phpunit/MediaWikiIntegrationTestCase.php index 05873a58ba2..07f0159bbaa 100644 --- a/tests/phpunit/MediaWikiIntegrationTestCase.php +++ b/tests/phpunit/MediaWikiIntegrationTestCase.php @@ -541,6 +541,9 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { MediaWiki\Session\SessionManager::resetCache(); TestUserRegistry::clear(); + + // Invalidate any Title objects cached by newFromText() or isMainPage() (T395214). + Title::clearCaches(); } /**