*/
private static $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
+ /** @var string[]|null Canonical namespaces cache */
+ private static $canonicalNamespaces = null;
+
+ /** @var array|false Canonical namespaces index cache */
+ private static $namespaceIndexes = false;
+
+ /** @var int[]|null Valid namespaces cache */
+ private static $validNamespaces = null;
+
/**
* Throw an exception when trying to get the subject or talk page
* for a given namespace where it does not make sense.
return true;
}
+ /**
+ * Clear internal caches
+ *
+ * For use in unit testing when namespace configuration is changed.
+ *
+ * @since 1.31
+ */
+ public static function clearCaches() {
+ self::$canonicalNamespaces = null;
+ self::$namespaceIndexes = false;
+ self::$validNamespaces = null;
+ }
+
/**
* Can pages in the given namespace be moved?
*
* (English) names.
*
* @param bool $rebuild Rebuild namespace list (default = false). Used for testing.
+ * Deprecated since 1.31, use self::clearCaches() instead.
*
* @return array
* @since 1.17
*/
public static function getCanonicalNamespaces( $rebuild = false ) {
- static $namespaces = null;
- if ( $namespaces === null || $rebuild ) {
+ if ( $rebuild ) {
+ self::clearCaches();
+ }
+
+ if ( self::$canonicalNamespaces === null ) {
global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
- $namespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames;
+ self::$canonicalNamespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames;
// Add extension namespaces
- $namespaces += ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
+ self::$canonicalNamespaces +=
+ ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
if ( is_array( $wgExtraNamespaces ) ) {
- $namespaces += $wgExtraNamespaces;
+ self::$canonicalNamespaces += $wgExtraNamespaces;
}
- Hooks::run( 'CanonicalNamespaces', [ &$namespaces ] );
+ Hooks::run( 'CanonicalNamespaces', [ &self::$canonicalNamespaces ] );
}
- return $namespaces;
+ return self::$canonicalNamespaces;
}
/**
* @return int
*/
public static function getCanonicalIndex( $name ) {
- static $xNamespaces = false;
- if ( $xNamespaces === false ) {
- $xNamespaces = [];
+ if ( self::$namespaceIndexes === false ) {
+ self::$namespaceIndexes = [];
foreach ( self::getCanonicalNamespaces() as $i => $text ) {
- $xNamespaces[strtolower( $text )] = $i;
+ self::$namespaceIndexes[strtolower( $text )] = $i;
}
}
- if ( array_key_exists( $name, $xNamespaces ) ) {
- return $xNamespaces[$name];
+ if ( array_key_exists( $name, self::$namespaceIndexes ) ) {
+ return self::$namespaceIndexes[$name];
} else {
return null;
}
* @return array
*/
public static function getValidNamespaces() {
- static $mValidNamespaces = null;
-
- if ( is_null( $mValidNamespaces ) ) {
+ if ( is_null( self::$validNamespaces ) ) {
foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
if ( $ns >= 0 ) {
- $mValidNamespaces[] = $ns;
+ self::$validNamespaces[] = $ns;
}
}
// T109137: sort numerically
- sort( $mValidNamespaces, SORT_NUMERIC );
+ sort( self::$validNamespaces, SORT_NUMERIC );
}
- return $mValidNamespaces;
+ return self::$validNamespaces;
}
/**
// Changing wgExtraNamespaces invalidates caches in MWNamespace and
// any live Language object, both on setup and teardown
$reset = function () {
- MWNamespace::getCanonicalNamespaces( true );
+ MWNamespace::clearCaches();
$GLOBALS['wgContLang']->resetNamespaces();
};
$setup[] = $reset;
$wgNamespaceContentModels[12312] = "testing";
$wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
}
+ protected function tearDown() {
+ global $wgContLang;
+
+ MWNamespace::clearCaches();
+ $wgContLang->resetNamespaces(); # reset namespace cache
+ parent::tearDown();
+ }
+
/**
* @dataProvider provideExtractSectionTitle
* @covers EditPage::extractSectionTitle
$wgNamespaceContentModels[12312] = 'DUMMY';
$wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
if ( !$this->the_properties ) {
unset( $wgNamespaceContentModels[12312] );
unset( $wgContentHandlers['DUMMY'] );
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
}
TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = [];
// Clear caches so that our new namespace appears
- MWNamespace::getCanonicalNamespaces( true );
+ MWNamespace::clearCaches();
Language::factory( 'en' )->resetNamespaces();
SpecialPageFactory::resetList();
}
public function tearDown() {
+ MWNamespace::clearCaches();
+ Language::factory( 'en' )->resetNamespaces();
+
parent::tearDown();
TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = $this->originalHandlers;
$wgNamespaceContentModels[12312] = 'DUMMY';
$wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
if ( !$this->the_page ) {
$this->the_page = $this->createPage(
unset( $wgNamespaceContentModels[12312] );
unset( $wgContentHandlers['DUMMY'] );
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
}
]
);
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
}
function tearDown() {
global $wgContLang;
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
parent::tearDown();
]
);
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
}
parent::tearDown();
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
}
] );
}
+ protected function tearDown() {
+ Language::factory( 'en' )->resetNamespaces();
+ parent::tearDown();
+ }
+
/**
* @covers Xml::expandAttributes
*/
$wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
$wgContentHandlers["testing-nontext"] = 'DummyNonTextContentHandler';
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces(); # reset namespace cache
$this->doLogin();
}
protected function tearDown() {
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+ global $wgContLang;
+
+ MWNamespace::clearCaches();
+ $wgContLang->resetNamespaces(); # reset namespace cache
+
parent::tearDown();
}
] );
// Reset namespace cache
- MWNamespace::getCanonicalNamespaces( true );
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces();
// And LinkCache
MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
global $wgContLang;
// Reset namespace cache
- MWNamespace::getCanonicalNamespaces( true );
+ MWNamespace::clearCaches();
$wgContLang->resetNamespaces();
// And LinkCache
MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );