private static $aliases;
/**
- * Get the special page list
+ * Reset the internal list of special pages. Useful when changing $wgSpecialPages after
+ * the internal list has already been initialized, e.g. during testing.
+ */
+ public static function resetList() {
+ self::$list = null;
+ self::$aliases = null;
+ }
+
+ /**
+ * Returns a list of canonical special page names.
+ * May be used to iterate over all registered special pages.
+ *
+ * @return string[]
+ */
+ public static function getNames() {
+ return array_keys( get_object_vars( self::getListObject() ) );
+ }
+
+ /**
+ * Get the special page list as an object, with each special page represented by a member
+ * field in the object.
*
- * @return array
+ * @deprecated since 1.24, use getNames() instead.
+ * @return object
*/
- static function getList() {
+ public static function getList() {
+ wfDeprecated( __FUNCTION__, '1.24' );
+ return self::getListObject();
+ }
+
+ /**
+ * Get the special page list as an object, with each special page represented by a member
+ * field in the object.
+ *
+ * @return object
+ */
+ private static function getListObject() {
global $wgSpecialPages;
global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
global $wgEnableEmail, $wgEnableJavaScriptTest;
// This hook can be used to remove undesired built-in special pages
wfRunHooks( 'SpecialPage_initList', array( &self::$list ) );
+ self::$list = (object)self::$list;
+
wfProfileOut( __METHOD__ );
}
* contain at least one entry (English fallbacks will be added if necessary).
* @return object
*/
- static function getAliasList() {
+ private static function getAliasListObject() {
if ( !is_object( self::$aliases ) ) {
global $wgContLang;
$aliases = $wgContLang->getSpecialPageAliases();
- $missingPages = self::getList();
+ // Objects are passed by reference by default, need to create a copy
+ $missingPages = clone self::getListObject();
self::$aliases = array();
// Check for $aliases being an array since Language::getSpecialPageAliases can return null
$caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
$caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
- if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
- $name = self::getAliasList()->$caseFoldedAlias;
+ if ( isset( self::getAliasListObject()->$caseFoldedAlias ) ) {
+ $name = self::getAliasListObject()->$caseFoldedAlias;
} else {
return array( null, null );
}
public static function exists( $name ) {
list( $title, /*...*/ ) = self::resolveAlias( $name );
- $specialPageList = self::getList();
- return isset( $specialPageList[$title] );
+ return property_exists( self::getListObject(), $title );
}
/**
*/
public static function getPage( $name ) {
list( $realName, /*...*/ ) = self::resolveAlias( $name );
- $specialPageList = self::getList();
- if ( isset( $specialPageList[$realName] ) ) {
- $rec = $specialPageList[$realName];
+ if ( property_exists( self::getListObject(), $realName ) ) {
+ $rec = self::getListObject()->$realName;
+
if ( is_string( $rec ) ) {
$className = $rec;
-
- return new $className;
+ $page = new $className;
+ } elseif ( is_callable( $rec ) ) {
+ // Use callback to instantiate the special page
+ $page = call_user_func( $rec );
} elseif ( is_array( $rec ) ) {
$className = array_shift( $rec );
// @deprecated, officially since 1.18, unofficially since forever
wfDeprecated( "Array syntax for \$wgSpecialPages is deprecated ($className), " .
"define a subclass of SpecialPage instead.", '1.18' );
- $specialPageList[$realName] = MWFunction::newObj( $className, $rec );
+ $page = MWFunction::newObj( $className, $rec );
+ } elseif ( $rec instanceof SpecialPage ) {
+ $page = $rec; //XXX: we should deep clone here
+ } else {
+ $page = null;
+ }
+
+ if ( $page instanceof SpecialPage ) {
+ return $page;
+ } else {
+ // It's not a classname, nor a callback, nor a legacy constructor array,
+ // nor a special page object. Give up.
+ wfLogWarning( "Cannot instantiate special page $realName: bad spec!" );
+ return null;
}
- return $specialPageList[$realName];
} else {
return null;
}
global $wgUser;
$user = $wgUser;
}
- foreach ( self::getList() as $name => $rec ) {
+ foreach ( self::getListObject() as $name => $rec ) {
$page = self::getPage( $name );
if ( $page ) { // not null
$page->setContext( RequestContext::getMain() );
*/
public static function getRegularPages() {
$pages = array();
- foreach ( self::getList() as $name => $rec ) {
+ foreach ( self::getListObject() as $name => $rec ) {
$page = self::getPage( $name );
if ( $page->isListed() && !$page->isRestricted() ) {
$pages[$name] = $page;
global $wgUser;
$user = $wgUser;
}
- foreach ( self::getList() as $name => $rec ) {
+ foreach ( self::getListObject() as $name => $rec ) {
$page = self::getPage( $name );
if (
$page->isListed()
* @param IContextSource $context
* @return string HTML fragment
*/
- static function capturePath( Title $title, IContextSource $context ) {
+ public static function capturePath( Title $title, IContextSource $context ) {
global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
// Save current globals
* @param string|bool $subpage
* @return string
*/
- static function getLocalNameFor( $name, $subpage = false ) {
+ public static function getLocalNameFor( $name, $subpage = false ) {
global $wgContLang;
$aliases = $wgContLang->getSpecialPageAliases();
* @param string $alias
* @return Title|null Title or null if there is no such alias
*/
- static function getTitleForAlias( $alias ) {
+ public static function getTitleForAlias( $alias ) {
list( $name, $subpage ) = self::resolveAlias( $alias );
if ( $name != null ) {
return SpecialPage::getTitleFor( $name, $subpage );
--- /dev/null
+<?php
+/**
+ * Factory for handling the special page list and generating SpecialPage objects.
+ *
+ * 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
+ *
+ * @covers SpecialPageFactory
+ * @group SpecialPage
+ */
+class SpecialPageFactoryTest extends MediaWikiTestCase {
+
+ public function newSpecialAllPages() {
+ return new SpecialAllPages();
+ }
+
+ public function specialPageProvider() {
+ return array(
+ 'class name' => array( 'SpecialAllPages', false ),
+ 'closure' => array( function() {
+ return new SpecialAllPages();
+ }, false ),
+ 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
+ );
+ }
+
+ /**
+ * @dataProvider specialPageProvider
+ */
+ public function testGetPage( $spec, $shouldReuseInstance ) {
+ $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => $spec ) );
+
+ SpecialPageFactory::resetList();
+
+ $page = SpecialPageFactory::getPage( 'testdummy' );
+ $this->assertInstanceOf( 'SpecialPage', $page );
+
+ $page2 = SpecialPageFactory::getPage( 'testdummy' );
+ $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
+
+ SpecialPageFactory::resetList();
+ }
+
+ public function testGetNames() {
+ $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => 'SpecialAllPages' ) );
+
+ SpecialPageFactory::resetList();
+ $names = SpecialPageFactory::getNames();
+ $this->assertInternalType( 'array', $names );
+ $this->assertContains( 'testdummy', $names );
+ SpecialPageFactory::resetList();
+ }
+
+ public function testResolveAlias() {
+ $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
+
+ SpecialPageFactory::resetList();
+
+ list( $name, $param ) = SpecialPageFactory::resolveAlias( 'Spezialseiten/Foo' );
+ $this->assertEquals( 'Specialpages', $name );
+ $this->assertEquals( 'Foo', $param );
+
+ SpecialPageFactory::resetList();
+ }
+
+ public function testGetLocalNameFor() {
+ $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
+
+ SpecialPageFactory::resetList();
+
+ $name = SpecialPageFactory::getLocalNameFor( 'Specialpages', 'Foo' );
+ $this->assertEquals( 'Spezialseiten/Foo', $name );
+
+ SpecialPageFactory::resetList();
+ }
+
+ public function testGetTitleForAlias() {
+ $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
+
+ SpecialPageFactory::resetList();
+
+ $title = SpecialPageFactory::getTitleForAlias( 'Specialpages/Foo' );
+ $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
+ $this->assertEquals( NS_SPECIAL, $title->getNamespace() );
+
+ SpecialPageFactory::resetList();
+ }
+
+}