* (bug 23524) Api Modules as followup to bug 14473 (Add iwlinks table to track inline...
[lhc/web/wiklou.git] / includes / MessageCache.php
index 458ec8f..79d9e14 100644 (file)
@@ -256,7 +256,7 @@ class MessageCache {
 
                        $this->lock($cacheKey);
 
-                       # Limit the concurrency of loadFromDB to a single process 
+                       # Limit the concurrency of loadFromDB to a single process
                        # This prevents the site from going down when the cache expires
                        $statusKey = wfMemcKey( 'messages', $code, 'status' );
                        $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
@@ -318,12 +318,11 @@ class MessageCache {
                        # database or in code.
                        if ( $code !== $wgContLanguageCode ) {
                                # Messages for particular language
-                               $escapedCode = $dbr->escapeLike( $code );
-                               $conds[] = "page_title like '%%/$escapedCode'";
+                               $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
                        } else {
                                # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
                                # other than language code.
-                               $conds[] = "page_title not like '%%/%%'";
+                               $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
                        }
                }
 
@@ -346,7 +345,7 @@ class MessageCache {
 
                $res = $dbr->select( array( 'page', 'revision', 'text' ),
                        array( 'page_title', 'old_text', 'old_flags' ),
-                       $smallConds, __METHOD__ );
+                       $smallConds, __METHOD__. "($code)" );
 
                for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
                        $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
@@ -400,8 +399,17 @@ class MessageCache {
 
                // Also delete cached sidebar... just in case it is affected
                global $parserMemc;
-               $sidebarKey = wfMemcKey( 'sidebar', $code );
-               $parserMemc->delete( $sidebarKey );
+               $codes = array( $code );
+               if ( $code === 'en'  ) {
+                       // Delete all sidebars, like for example on action=purge on the
+                       // sidebar messages
+                       $codes = array_keys( Language::getLanguageNames() );
+               }
+
+               foreach ( $codes as $code ) {
+                       $sidebarKey = wfMemcKey( 'sidebar', $code );
+                       $parserMemc->delete( $sidebarKey );
+               }
 
                wfRunHooks( "MessageCacheReplace", array( $title, $text ) );
 
@@ -412,7 +420,6 @@ class MessageCache {
         * Shortcut to update caches.
         *
         * @param $cache Array: cached messages with a version.
-        * @param $cacheKey String: Identifier for the cache.
         * @param $memc Bool: Wether to update or not memcache.
         * @param $code String: Language code.
         * @return False on somekind of error.
@@ -423,16 +430,10 @@ class MessageCache {
 
                $cacheKey = wfMemcKey( 'messages', $code );
 
-               $i = 0;
                if ( $memc ) {
-                       # Save in memcached
-                       # Keep trying if it fails, this is kind of important
-
-                       for ($i=0; $i<20 &&
-                               !$this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
-                               $i++ ) {
-                               usleep(mt_rand(500000,1500000));
-                       }
+                       $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
+               } else {
+                       $success = true;
                }
 
                # Save to local cache
@@ -447,18 +448,14 @@ class MessageCache {
                        }
                }
 
-               if ( $i == 20 ) {
-                       $success = false;
-               } else {
-                       $success = true;
-               }
                wfProfileOut( __METHOD__ );
                return $success;
        }
 
        /**
-        * Returns success
         * Represents a write lock on the messages key
+        *
+        * @return Boolean: success
         */
        function lock($key) {
                if ( !$this->mUseCache ) {
@@ -485,26 +482,26 @@ class MessageCache {
        /**
         * Get a message from either the content language or the user language.
         *
-        * @param string $key The message cache key
-        * @param bool $useDB Get the message from the DB, false to use only the localisation
-        * @param string $langcode Code of the language to get the message for, if
-        *                         it is a valid code create a language for that
-        *                         language, if it is a string but not a valid code
-        *                         then make a basic language object, if it is a
-        *                         false boolean then use the current users
-        *                         language (as a fallback for the old parameter
-        *                         functionality), or if it is a true boolean then
-        *                         use the wikis content language (also as a
-        *                         fallback).
-        * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
+        * @param $key String: the message cache key
+        * @param $useDB Boolean: get the message from the DB, false to use only
+        *               the localisation
+        * @param $langcode String: code of the language to get the message for, if
+        *                  it is a valid code create a language for that language,
+        *                  if it is a string but not a valid code then make a basic
+        *                  language object, if it is a false boolean then use the
+        *                  current users language (as a fallback for the old
+        *                  parameter functionality), or if it is a true boolean
+        *                  then use the wikis content language (also as a
+        *                  fallback).
+        * @param $isFullKey Boolean: specifies whether $key is a two part key
+        *                   "msg/lang".
         */
        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' );
+               if ( strval( $key ) === '' ) {
+                       # Shortcut: the empty key is always missing
+                       return false;
                }
 
                $lang = wfGetLangObj( $langcode );
@@ -512,10 +509,15 @@ class MessageCache {
 
                $message = false;
 
-               # Normalise title-case input
+               # Normalise title-case input (with some inlining)
                $lckey = str_replace( ' ', '_', $key );
-               $lckey[0] = strtolower( $lckey[0] );
-               $uckey = ucfirst( $lckey );
+               if ( ord( $key ) < 128 ) {
+                       $lckey[0] = strtolower( $lckey[0] );
+                       $uckey = ucfirst( $lckey );
+               } else {
+                       $lckey = $wgContLang->lcfirst( $lckey );
+                       $uckey = $wgContLang->ucfirst( $lckey );
+               }
 
                # Try the MediaWiki namespace
                if( !$this->mDisable && $useDB ) {
@@ -557,11 +559,11 @@ class MessageCache {
 
                # Final fallback
                if( $message === false ) {
-                       return '&lt;' . htmlspecialchars($key) . '&gt;';
+                       return false;
                }
 
                # Fix whitespace
-               $message = strtr( $message, 
+               $message = strtr( $message,
                        array(
                                # Fix for trailing whitespace, removed by textarea
                                '&#32;' => ' ',
@@ -634,7 +636,7 @@ class MessageCache {
                        $message = $revision->getText();
                        if ($this->mUseCache) {
                                $this->mCache[$code][$title] = ' ' . $message;
-                               $this->mMemc->set( $titleKey, $message, $this->mExpiry );
+                               $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
                        }
                } else {
                        # Negative caching
@@ -676,7 +678,7 @@ class MessageCache {
 
        function disable() { $this->mDisable = true; }
        function enable() { $this->mDisable = false; }
+
        /** @deprecated */
        function disableTransform(){
                wfDeprecated( __METHOD__ );
@@ -711,9 +713,9 @@ 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
+        * @param $key Mixed
+        * @param $value Mixed
+        * @param $lang String: the messages language, English by default
         */
        function addMessage( $key, $value, $lang = 'en' ) {
                wfDeprecated( __METHOD__ );
@@ -725,8 +727,8 @@ class MessageCache {
         * 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
+        * @param $messages Array: an associative array of key => values to be added
+        * @param $lang String: the messages language, English by default
         */
        function addMessages( $messages, $lang = 'en' ) {
                wfDeprecated( __METHOD__ );
@@ -738,7 +740,7 @@ class MessageCache {
         * Add a 2-D array of messages by lang. Useful for extensions.
         * @deprecated Use $wgExtensionMessagesFiles
         *
-        * @param array $messages The array to be added
+        * @param $messages Array: the array to be added
         */
        function addMessagesByLang( $messages ) {
                wfDeprecated( __METHOD__ );