* Re-added $wgMessageCache->addMessages(), there are still some extensions (in and...
authorTim Starling <tstarling@users.mediawiki.org>
Fri, 3 Jul 2009 06:19:39 +0000 (06:19 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Fri, 3 Jul 2009 06:19:39 +0000 (06:19 +0000)
* Made wfDeprecated() issue a notice only once for each function name instead of every time the function is called.
* Added exceptions for malformed keys input to $wgMessageCache->get(), per CR comments on r52503
* Fixed notice from MWException::useMessageCache()

RELEASE-NOTES
includes/Exception.php
includes/GlobalFunctions.php
includes/LocalisationCache.php
includes/MessageCache.php

index db1ad79..19b6c4e 100644 (file)
@@ -50,6 +50,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   to true
 * Removed $wgEnableSerializedMessages and $wgCheckSerialized. Similar 
   functionality is now available via $wgLocalisationCacheConf.
+* $wgMessageCache->addMessages() is deprecated. Messages added via this 
+  interface will not appear in Special:AllMessages.
 
 === New features in 1.16 ===
 
index b2d668c..f6bc6f8 100644 (file)
@@ -26,7 +26,7 @@ class MWException extends Exception {
        function useMessageCache() {
                global $wgLang;
                foreach ( $this->getTrace() as $frame ) {
-                       if ( $frame['class'] == 'LocalisationCache' ) {
+                       if ( isset( $frame['class'] ) && $frame['class'] === 'LocalisationCache' ) {
                                return false;
                        }
                }
index 1f6002c..e75786b 100644 (file)
@@ -2975,7 +2975,11 @@ function wfMaxlagError( $host, $lag, $maxLag ) {
  * @return null
  */
 function wfDeprecated( $function ) {
-       wfWarn( "Use of $function is deprecated.", 2 );
+       static $functionsWarned = array();
+       if ( !isset( $functionsWarned[$function] ) ) {
+               $functionsWarned[$function] = true;
+               wfWarn( "Use of $function is deprecated.", 2 );
+       }
 }
 
 function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
index 142dc2c..d5ad424 100644 (file)
@@ -77,6 +77,12 @@ class LocalisationCache {
         */
        var $recachedLangs = array();
 
+       /**
+        * Data added by extensions using the deprecated $wgMessageCache->addMessages() 
+        * interface.
+        */
+       var $legacyData = array();
+
        /**
         * All item keys
         */
@@ -137,10 +143,6 @@ class LocalisationCache {
                global $wgCacheDirectory;
 
                $this->conf = $conf;
-               $this->data = array();
-               $this->loadedItems = array();
-               $this->loadedSubitems = array();
-               $this->initialisedLangs = array();
                if ( !empty( $conf['storeClass'] ) ) {
                        $storeClass = $conf['storeClass'];
                } else {
@@ -208,6 +210,9 @@ class LocalisationCache {
         * Get a subitem, for instance a single message for a given language.
         */
        public function getSubitem( $code, $key, $subkey ) {
+               if ( isset( $this->legacyData[$code][$key][$subkey] ) ) {
+                       return $this->legacyData[$code][$key][$subkey];
+               }
                if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) ) {
                        if ( isset( $this->loadedItems[$code][$key] ) ) {
                                if ( isset( $this->data[$code][$key][$subkey] ) ) {
@@ -619,12 +624,30 @@ class LocalisationCache {
                unset( $this->loadedItems[$code] );
                unset( $this->loadedSubitems[$code] );
                unset( $this->initialisedLangs[$code] );
+               // We don't unload legacyData because there's no way to get it back 
+               // again, it's not really a cache
                foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
                        if ( $fbCode === $code ) {
                                $this->unload( $shallowCode );
                        }
                }
        }
+
+       /**
+        * Add messages to the cache, from an extension that has not yet been 
+        * migrated to $wgExtensionMessages or the LocalisationCacheRecache hook. 
+        * Called by deprecated function $wgMessageCache->addMessages(). 
+        */
+       public function addLegacyMessages( $messages ) {
+               foreach ( $messages as $lang => $langMessages ) {
+                       if ( isset( $this->legacyData[$lang]['messages'] ) ) {
+                               $this->legacyData[$lang]['messages'] = 
+                                       $langMessages + $this->legacyData[$lang]['messages'];
+                       } else {
+                               $this->legacyData[$lang]['messages'] = $langMessages;
+                       }
+               }
+       }
 }
 
 /**
index b354b3b..458ec8f 100644 (file)
@@ -501,6 +501,12 @@ class MessageCache {
        function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
                global $wgContLanguageCode, $wgContLang;
 
+               if ( !is_string( $key ) ) {
+                       throw new MWException( __METHOD__.': Invalid message key of type ' . gettype( $key ) );
+               } elseif ( $key === '' ) {
+                       throw new MWException( __METHOD__.': Invaild message key: empty string' );
+               }
+
                $lang = wfGetLangObj( $langcode );
                $langcode = $lang->getCode();
 
@@ -701,6 +707,52 @@ class MessageCache {
                }
        }
 
+       /**
+        * Add a message to the cache
+        * @deprecated Use $wgExtensionMessagesFiles
+        *
+        * @param mixed $key
+        * @param mixed $value
+        * @param string $lang The messages language, English by default
+        */
+       function addMessage( $key, $value, $lang = 'en' ) {
+               wfDeprecated( __METHOD__ );
+               $lc = Language::getLocalisationCache();
+               $lc->addLegacyMessages( array( $lang => array( $key => $value ) ) );
+       }
+
+       /**
+        * Add an associative array of message to the cache
+        * @deprecated Use $wgExtensionMessagesFiles
+        *
+        * @param array $messages An associative array of key => values to be added
+        * @param string $lang The messages language, English by default
+        */
+       function addMessages( $messages, $lang = 'en' ) {
+               wfDeprecated( __METHOD__ );
+               $lc = Language::getLocalisationCache();
+               $lc->addLegacyMessages( array( $lang => $messages ) );
+       }
+
+       /**
+        * Add a 2-D array of messages by lang. Useful for extensions.
+        * @deprecated Use $wgExtensionMessagesFiles
+        *
+        * @param array $messages The array to be added
+        */
+       function addMessagesByLang( $messages ) {
+               wfDeprecated( __METHOD__ );
+               $lc = Language::getLocalisationCache();
+               $lc->addLegacyMessages( $messages );
+       }
+
+       /**
+        * Set a hook for addMessagesByLang()
+        */
+       function setExtensionMessagesHook( $callback ) {
+               $this->mAddMessagesHook = $callback;
+       }
+
        /**
         * @deprecated
         */