MediaWiki r36631 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r36630‎ | r36631 (on ViewVC)‎ | r36632 >
Date:10:59, 25 June 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* $wgExtensionAliasesFiles option to simplify adding aliases for special pages provided by extensions
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 36630)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 36631)
@@ -2189,6 +2189,18 @@
 $wgExtensionMessagesFiles = array();
 
 /**
+ * Aliases for special pages provided by extensions.
+ * Associative array mapping special page to array of aliases. First alternative
+ * for each special page will be used as the normalised name for it. English
+ * aliases will be added to the end of the list so that they always work. The
+ * file must define a variable $aliases.
+ *
+ * Example:
+ *    $wgExtensionAliasesFiles['Translate'] = dirname(__FILE__).'/Translate.alias.php';
+ */
+$wgExtensionAliasesFiles = array();
+
+/**
  * Parser output hooks.
  * This is an associative array where the key is an extension-defined tag
  * (typically the extension name), and the value is a PHP callback.
Index: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php	(revision 36630)
+++ trunk/phase3/languages/Language.php	(revision 36631)
@@ -1719,15 +1719,70 @@
 	 */
 	function getSpecialPageAliases() {
 		$this->load();
+
+		// Cache aliases because it may be slow to load them
 		if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
+
+			// Initialise array
 			$this->mExtendedSpecialPageAliases = $this->specialPageAliases;
-			wfRunHooks( 'LanguageGetSpecialPageAliases', 
-				array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
+
+			global $wgExtensionAliasesFiles;
+			foreach ( $wgExtensionAliasesFiles as $file ) {
+
+				// Fail fast
+				if ( !file_exists($file) )
+					throw new MWException( 'Aliases file does not exist' );
+
+				$aliases = array();
+				require($file);
+
+				// Check the availability of aliases
+				if ( !isset($aliases['en']) )
+					throw new MWException( 'Malformed aliases file' );
+
+				$code = $this->getCode();
+
+				if ( isset($aliases[$code]) ) {
+					$aliases[$code] = $this->fixSpecialPageAliases( $aliases[$code] );
+					/* Merge the aliases, THIS will break if there is special page name
+					* which looks like a numerical key, thanks to PHP...
+					* See the comments for wfArrayMerge in GlobalSettings.php. */
+					$this->mExtendedSpecialPageAliases = array_merge_recursive(
+						$this->mExtendedSpecialPageAliases, $aliases[$code] );
+				}
+
+				/* Add the English aliases to the end of list as aliases... unless we
+				 * already added them! */
+				if ( $code !== 'en' ) {
+					$aliases['en'] = $this->fixSpecialPageAliases( $aliases['en'] );
+					$this->mExtendedSpecialPageAliases = array_merge_recursive(
+						$this->mExtendedSpecialPageAliases, $aliases['en'] );
+				}
+					
+			}
+
+			wfRunHooks( 'LanguageGetSpecialPageAliases',
+				array( &$this->mExtendedSpecialPageAliases, $code ) );
 		}
+
 		return $this->mExtendedSpecialPageAliases;
 	}
 
 	/**
+	 * Function to fix special page aliases. Will convert the first letter to
+	 * upper case and spaces to underscores. Can be given a full aliases array,
+	 * in which case it will recursively fix all aliases.
+	 */
+	public function fixSpecialPageAliases( $mixed ) {
+		// Work recursively until in string level
+		if ( is_array($mixed) ) {
+			$callback = array( $this, 'fixSpecialPageAliases' );
+			return array_map( $callback, $mixed );
+		}
+		return str_replace( ' ', '_', $this->ucfirst( $mixed ) );
+	}
+
+	/**
 	 * Italic is unsuitable for some languages
 	 *
 	 * @param $text String: the text to be emphasized.
@@ -2250,12 +2305,9 @@
 		# Replace spaces with underscores in namespace names
 		$cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
 
-		# And do the same for specialpage aliases. $page is an array.
-		foreach ( $cache['specialPageAliases'] as &$page ) {
-			$page = str_replace( ' ', '_', $page );
-		}
-		# Decouple the reference to prevent accidental damage
-		unset($page);
+		# And do the same for specialpage aliases.
+		$cache['specialPageAliases'] =
+			$this->fixSpecialPageAliases( $cache['specialPageAliases'] );
 		
 		# Save to both caches
 		self::$mLocalisationCache[$code] = $cache;
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 36630)
+++ trunk/phase3/RELEASE-NOTES	(revision 36631)
@@ -62,6 +62,8 @@
   is no longer possible. 
 * $wgMessageCacheType defines now the type of cache used by the MessageCache class,
   previously it was choosen based on $wgParserCacheType
+* $wgExtensionAliasesFiles option to simplify adding aliases to special pages
+* provided by extensions, in a similar way to $wgExtensionMessagesFiles
 
 === New features in 1.13 ===
 

Status & tagging log

  • 15:28, 12 September 2011 Meno25 (Talk | contribs) changed the status of r36631 [removed: ok added: old]
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox