2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2012-05-08 12:51:21 +00:00
|
|
|
* Localisation messages cache.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-09-29 03:59:11 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-10-12 05:36:03 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2016-11-29 03:38:57 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2013-05-20 09:16:27 +00:00
|
|
|
* MediaWiki message cache structure version.
|
|
|
|
|
* Bump this whenever the message cache format has changed.
|
|
|
|
|
*/
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
define( 'MSG_CACHE_VERSION', 2 );
|
2013-05-20 09:16:27 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Message cache
|
2007-01-05 18:08:29 +00:00
|
|
|
* Performs various MediaWiki namespace-related functions
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-12-04 18:27:59 +00:00
|
|
|
class MessageCache {
|
2015-05-20 02:34:20 +00:00
|
|
|
const FOR_UPDATE = 1; // force message reload
|
|
|
|
|
|
2015-08-19 19:27:20 +00:00
|
|
|
/** How long to wait for memcached locks */
|
2015-08-27 08:46:05 +00:00
|
|
|
const WAIT_SEC = 15;
|
|
|
|
|
/** How long memcached locks last */
|
2015-08-19 19:27:20 +00:00
|
|
|
const LOCK_TTL = 30;
|
|
|
|
|
|
2010-08-05 18:38:42 +00:00
|
|
|
/**
|
|
|
|
|
* Process local cache of loaded messages that are defined in
|
|
|
|
|
* MediaWiki namespace. First array level is a language code,
|
|
|
|
|
* second level is message key and the values are either message
|
|
|
|
|
* content prefixed with space, or !NONEXISTENT for negative
|
|
|
|
|
* caching.
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
* @var array $mCache
|
2010-08-05 18:38:42 +00:00
|
|
|
*/
|
|
|
|
|
protected $mCache;
|
|
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool[] Map of (language code => boolean)
|
|
|
|
|
*/
|
|
|
|
|
protected $mCacheVolatile = [];
|
|
|
|
|
|
2013-05-20 09:16:27 +00:00
|
|
|
/**
|
2016-04-20 19:37:00 +00:00
|
|
|
* Should mean that database cannot be used, but check
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var bool $mDisable
|
|
|
|
|
*/
|
2010-08-05 18:38:42 +00:00
|
|
|
protected $mDisable;
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2013-05-20 09:16:27 +00:00
|
|
|
/**
|
|
|
|
|
* Lifetime for cache, used by object caching.
|
|
|
|
|
* Set on construction, see __construct().
|
|
|
|
|
*/
|
2010-08-05 18:38:42 +00:00
|
|
|
protected $mExpiry;
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-29 03:38:57 +00:00
|
|
|
* Message cache has its own parser which it uses to transform messages
|
|
|
|
|
* @var ParserOptions
|
2010-08-05 18:38:42 +00:00
|
|
|
*/
|
2016-11-29 03:38:57 +00:00
|
|
|
protected $mParserOptions;
|
|
|
|
|
/** @var Parser */
|
|
|
|
|
protected $mParser;
|
2004-09-25 02:23:04 +00:00
|
|
|
|
2013-05-20 09:16:27 +00:00
|
|
|
/**
|
|
|
|
|
* Variable for tracking which variables are already loaded
|
|
|
|
|
* @var array $mLoadedLanguages
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $mLoadedLanguages = [];
|
2004-09-25 02:23:04 +00:00
|
|
|
|
2015-05-20 16:51:29 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool $mInParser
|
|
|
|
|
*/
|
|
|
|
|
protected $mInParser = false;
|
|
|
|
|
|
|
|
|
|
/** @var BagOStuff */
|
|
|
|
|
protected $mMemc;
|
|
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
protected $wanCache;
|
|
|
|
|
|
2011-01-26 15:42:04 +00:00
|
|
|
/**
|
|
|
|
|
* Singleton instance
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
2013-05-20 09:16:27 +00:00
|
|
|
* @var MessageCache $instance
|
2011-01-26 15:42:04 +00:00
|
|
|
*/
|
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the signleton instance of this class
|
|
|
|
|
*
|
|
|
|
|
* @since 1.18
|
2013-04-08 12:09:48 +00:00
|
|
|
* @return MessageCache
|
2011-01-26 15:42:04 +00:00
|
|
|
*/
|
|
|
|
|
public static function singleton() {
|
2015-11-13 00:04:12 +00:00
|
|
|
if ( self::$instance === null ) {
|
2011-01-26 15:42:04 +00:00
|
|
|
global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
|
2013-04-08 12:09:48 +00:00
|
|
|
self::$instance = new self(
|
|
|
|
|
wfGetMessageCacheStorage(),
|
|
|
|
|
$wgUseDatabaseMessages,
|
|
|
|
|
$wgMsgCacheExpiry
|
|
|
|
|
);
|
2011-01-26 15:42:04 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-01-26 15:42:04 +00:00
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-01-26 17:41:18 +00:00
|
|
|
* Destroy the singleton instance
|
2011-01-26 15:42:04 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
public static function destroyInstance() {
|
|
|
|
|
self::$instance = null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 16:35:11 +00:00
|
|
|
/**
|
|
|
|
|
* Normalize message key input
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Input message key to be normalized
|
|
|
|
|
* @return string Normalized message key
|
|
|
|
|
*/
|
|
|
|
|
public static function normalizeKey( $key ) {
|
2015-07-19 08:54:35 +00:00
|
|
|
global $wgContLang;
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2015-07-13 16:35:11 +00:00
|
|
|
$lckey = strtr( $key, ' ', '_' );
|
|
|
|
|
if ( ord( $lckey ) < 128 ) {
|
|
|
|
|
$lckey[0] = strtolower( $lckey[0] );
|
|
|
|
|
} else {
|
|
|
|
|
$lckey = $wgContLang->lcfirst( $lckey );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $lckey;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-20 09:16:27 +00:00
|
|
|
/**
|
2013-12-30 17:32:53 +00:00
|
|
|
* @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
|
2013-05-20 09:16:27 +00:00
|
|
|
* @param bool $useDB
|
|
|
|
|
* @param int $expiry Lifetime for cache. @see $mExpiry.
|
|
|
|
|
*/
|
2016-11-22 21:02:10 +00:00
|
|
|
function __construct( BagOStuff $memCached, $useDB, $expiry ) {
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
global $wgUseLocalMessageCache;
|
|
|
|
|
|
2010-08-05 18:19:34 +00:00
|
|
|
$this->mMemc = $memCached;
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->mDisable = !$useDB;
|
|
|
|
|
$this->mExpiry = $expiry;
|
2015-05-20 16:51:29 +00:00
|
|
|
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
if ( $wgUseLocalMessageCache ) {
|
2016-09-29 03:59:11 +00:00
|
|
|
$this->localCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
} else {
|
2016-09-29 03:59:11 +00:00
|
|
|
$this->localCache = new EmptyBagOStuff();
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-20 16:51:29 +00:00
|
|
|
$this->wanCache = ObjectCache::getMainWANInstance();
|
2008-06-30 03:02:06 +00:00
|
|
|
}
|
2008-06-03 20:41:57 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
/**
|
|
|
|
|
* ParserOptions is lazy initialised.
|
2011-04-18 12:43:53 +00:00
|
|
|
*
|
|
|
|
|
* @return ParserOptions
|
2008-07-05 15:39:10 +00:00
|
|
|
*/
|
2006-07-26 07:15:39 +00:00
|
|
|
function getParserOptions() {
|
2016-02-03 20:41:00 +00:00
|
|
|
global $wgUser;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2006-07-26 07:15:39 +00:00
|
|
|
if ( !$this->mParserOptions ) {
|
2016-02-03 20:41:00 +00:00
|
|
|
if ( !$wgUser->isSafeToLoad() ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
// $wgUser isn't unstubbable yet, so don't try to get a
|
|
|
|
|
// ParserOptions for it. And don't cache this ParserOptions
|
|
|
|
|
// either.
|
2016-02-03 20:41:00 +00:00
|
|
|
$po = ParserOptions::newFromAnon();
|
2016-02-01 20:44:03 +00:00
|
|
|
$po->setEditSection( false );
|
|
|
|
|
return $po;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-26 07:15:39 +00:00
|
|
|
$this->mParserOptions = new ParserOptions;
|
2012-05-19 16:23:24 +00:00
|
|
|
$this->mParserOptions->setEditSection( false );
|
2006-07-26 07:15:39 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2006-07-26 07:15:39 +00:00
|
|
|
return $this->mParserOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-07 13:09:30 +00:00
|
|
|
/**
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
* Try to load the cache from APC.
|
2008-07-05 15:39:10 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $code Optional language code, see documenation of load().
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
* @return array|bool The cache array, or false if not in cache.
|
2005-11-08 11:54:04 +00:00
|
|
|
*/
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
protected function getLocalCache( $code ) {
|
|
|
|
|
$cacheKey = wfMemcKey( __CLASS__, $code );
|
2015-08-28 01:31:35 +00:00
|
|
|
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
return $this->localCache->get( $cacheKey );
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
* Save the cache to APC.
|
|
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $code
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
* @param array $cache The cache array
|
2005-11-08 11:54:04 +00:00
|
|
|
*/
|
MessageCache: use APC for local caching, rather than files
In addition to eliminating disk IO in a hot path, using APC spares us from
having to serialize and unserialize cache arrays. Since we're not serializing,
though, we don't have a string representation to hash, so use a random string
instead. (The code already treats the association of hash string to cache as
purely symbolic, so this is not problematic.)
Whereas the hash was previously stored as the first 32 bytes of each cache
file, we now store it as an array key instead (like VERSION and EXPIRY were
already). Because this changes the structure of cached data, we have to bump
MSG_CACHE_VERSION.
While we're here, make MessageCache::getLocalCache() and
MessageCache::saveToLocal() protected, make their signatures more
consistent with other methods in this class. While they were (implicitly)
public before, there are absolutely no external callers in Core or
extensions[0][1], so we can skip the standard deprecation process.
[0]: https://github.com/search?q=%40wikimedia+getlocalcache&type=Code&utf8=%E2%9C%93
[1]: https://github.com/search?utf8=%E2%9C%93&q=%40wikimedia+savetolocal&type=Code
Change-Id: I020617d2df2a8f0f243b85f3383dc7b16f15aaad
2015-08-09 04:59:47 +00:00
|
|
|
protected function saveToLocalCache( $code, $cache ) {
|
|
|
|
|
$cacheKey = wfMemcKey( __CLASS__, $code );
|
|
|
|
|
$this->localCache->set( $cacheKey, $cache );
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-07-05 15:39:10 +00:00
|
|
|
* Loads messages from caches or from database in this order:
|
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput.
* The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true;
* Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache.
* Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words.
* $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c.
* Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess.
* Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly.
* Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load()
* Fixed FileDependency::__sleep()
* In Cdb.php, fixed newlines in debug messages
In MessageCache::get():
* Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII.
* Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us
* Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us.
* Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
|
|
|
* (1) local message cache (if $wgUseLocalMessageCache is enabled)
|
2008-07-05 15:39:10 +00:00
|
|
|
* (2) memcached
|
|
|
|
|
* (3) from the database.
|
|
|
|
|
*
|
|
|
|
|
* When succesfully loading from (2) or (3), all higher level caches are
|
|
|
|
|
* updated for the newest version.
|
|
|
|
|
*
|
2010-08-07 23:41:03 +00:00
|
|
|
* Nothing is loaded if member variable mDisable is true, either manually
|
2008-07-05 15:39:10 +00:00
|
|
|
* set by calling code or if message loading fails (is this possible?).
|
|
|
|
|
*
|
|
|
|
|
* Returns true if cache is already populated or it was succesfully populated,
|
|
|
|
|
* or false if populating empty cache fails. Also returns true if MessageCache
|
|
|
|
|
* is disabled.
|
|
|
|
|
*
|
2016-10-15 02:11:51 +00:00
|
|
|
* @param string $code Language to which load messages
|
|
|
|
|
* @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
|
2012-12-09 03:09:48 +00:00
|
|
|
* @throws MWException
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-10-15 02:11:51 +00:00
|
|
|
protected function load( $code, $mode = null ) {
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( !is_string( $code ) ) {
|
2016-10-15 02:11:51 +00:00
|
|
|
throw new InvalidArgumentException( "Missing language code" );
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
2008-06-03 20:41:57 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Don't do double loading...
|
2015-05-20 02:34:20 +00:00
|
|
|
if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
|
2011-01-26 17:41:18 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
# 8 lines of code just to say (once) that message cache is disabled
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( $this->mDisable ) {
|
2005-05-28 11:07:55 +00:00
|
|
|
static $shownDisabled = false;
|
|
|
|
|
if ( !$shownDisabled ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
wfDebug( __METHOD__ . ": disabled\n" );
|
2005-05-28 11:07:55 +00:00
|
|
|
$shownDisabled = true;
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Loading code starts
|
|
|
|
|
$success = false; # Keep track of success
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
$staleCache = false; # a cache array with expired data, or false if none has been loaded
|
2016-02-17 09:09:32 +00:00
|
|
|
$where = []; # Debug info, delayed to avoid spamming debug log too much
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2015-08-12 02:53:03 +00:00
|
|
|
# Hash of the contents is stored in memcache, to detect if data-center cache
|
|
|
|
|
# or local cache goes out of date (e.g. due to replace() on some other server)
|
|
|
|
|
list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
|
2016-11-29 03:38:57 +00:00
|
|
|
$this->mCacheVolatile[$code] = $hashVolatile;
|
2015-08-12 02:53:03 +00:00
|
|
|
|
2015-08-28 01:31:35 +00:00
|
|
|
# Try the local cache and check against the cluster hash key...
|
|
|
|
|
$cache = $this->getLocalCache( $code );
|
|
|
|
|
if ( !$cache ) {
|
|
|
|
|
$where[] = 'local cache is empty';
|
|
|
|
|
} elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
|
|
|
|
|
$where[] = 'local cache has the wrong hash';
|
|
|
|
|
$staleCache = $cache;
|
|
|
|
|
} elseif ( $this->isCacheExpired( $cache ) ) {
|
|
|
|
|
$where[] = 'local cache is expired';
|
|
|
|
|
$staleCache = $cache;
|
|
|
|
|
} elseif ( $hashVolatile ) {
|
|
|
|
|
$where[] = 'local cache validation key is expired/volatile';
|
|
|
|
|
$staleCache = $cache;
|
|
|
|
|
} else {
|
|
|
|
|
$where[] = 'got from local cache';
|
|
|
|
|
$success = true;
|
|
|
|
|
$this->mCache[$code] = $cache;
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$success ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
$cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
# Try the global cache. If it is empty, try to acquire a lock. If
|
|
|
|
|
# the lock can't be acquired, wait for the other thread to finish
|
|
|
|
|
# and then try the global cache a second time.
|
2015-08-27 08:46:05 +00:00
|
|
|
for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
|
2015-08-12 02:53:03 +00:00
|
|
|
if ( $hashVolatile && $staleCache ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
# Do not bother fetching the whole cache blob to avoid I/O.
|
|
|
|
|
# Instead, just try to get the non-blocking $statusKey lock
|
|
|
|
|
# below, and use the local stale value if it was not acquired.
|
|
|
|
|
$where[] = 'global cache is presumed expired';
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
} else {
|
2015-05-20 16:51:29 +00:00
|
|
|
$cache = $this->mMemc->get( $cacheKey );
|
|
|
|
|
if ( !$cache ) {
|
|
|
|
|
$where[] = 'global cache is empty';
|
|
|
|
|
} elseif ( $this->isCacheExpired( $cache ) ) {
|
|
|
|
|
$where[] = 'global cache is expired';
|
|
|
|
|
$staleCache = $cache;
|
2015-08-12 02:53:03 +00:00
|
|
|
} elseif ( $hashVolatile ) {
|
2016-09-05 20:21:26 +00:00
|
|
|
# DB results are replica DB lag prone until the holdoff TTL passes.
|
2015-08-12 02:53:03 +00:00
|
|
|
# By then, updates should be reflected in loadFromDBWithLock().
|
|
|
|
|
# One thread renerates the cache while others use old values.
|
|
|
|
|
$where[] = 'global cache is expired/volatile';
|
|
|
|
|
$staleCache = $cache;
|
2015-05-20 16:51:29 +00:00
|
|
|
} else {
|
|
|
|
|
$where[] = 'got from global cache';
|
|
|
|
|
$this->mCache[$code] = $cache;
|
|
|
|
|
$this->saveToCaches( $cache, 'local-only', $code );
|
|
|
|
|
$success = true;
|
|
|
|
|
}
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
}
|
2008-06-01 03:27:48 +00:00
|
|
|
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
if ( $success ) {
|
|
|
|
|
# Done, no need to retry
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 16:51:29 +00:00
|
|
|
# We need to call loadFromDB. Limit the concurrency to one process.
|
|
|
|
|
# This prevents the site from going down when the cache expires.
|
2015-08-27 08:46:05 +00:00
|
|
|
# Note that the DB slam protection lock here is non-blocking.
|
|
|
|
|
$loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
|
|
|
|
|
if ( $loadStatus === true ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
$success = true;
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
break;
|
|
|
|
|
} elseif ( $staleCache ) {
|
|
|
|
|
# Use the stale cache while some other thread constructs the new one
|
|
|
|
|
$where[] = 'using stale cache';
|
|
|
|
|
$this->mCache[$code] = $staleCache;
|
|
|
|
|
$success = true;
|
|
|
|
|
break;
|
|
|
|
|
} elseif ( $failedAttempts > 0 ) {
|
2015-08-27 08:46:05 +00:00
|
|
|
# Already blocked once, so avoid another lock/unlock cycle.
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
# This case will typically be hit if memcached is down, or if
|
2015-08-27 08:46:05 +00:00
|
|
|
# loadFromDB() takes longer than LOCK_WAIT.
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
$where[] = "could not acquire status key.";
|
|
|
|
|
break;
|
2015-08-27 08:46:05 +00:00
|
|
|
} elseif ( $loadStatus === 'cantacquire' ) {
|
|
|
|
|
# Wait for the other thread to finish, then retry. Normally,
|
|
|
|
|
# the memcached get() will then yeild the other thread's result.
|
|
|
|
|
$where[] = 'waited for other thread to complete';
|
|
|
|
|
$this->getReentrantScopedLock( $cacheKey );
|
2008-10-10 13:02:57 +00:00
|
|
|
} else {
|
2015-08-27 08:46:05 +00:00
|
|
|
# Disable cache; $loadStatus is 'disabled'
|
|
|
|
|
break;
|
2008-10-10 13:02:57 +00:00
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( !$success ) {
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
$where[] = 'loading FAILED - cache is disabled';
|
2007-01-05 18:08:29 +00:00
|
|
|
$this->mDisable = true;
|
|
|
|
|
$this->mCache = false;
|
2016-04-20 19:37:00 +00:00
|
|
|
wfDebugLog( 'MessageCacheError', __METHOD__ . ": Failed to load $code\n" );
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
# This used to throw an exception, but that led to nasty side effects like
|
|
|
|
|
# the whole wiki being instantly down if the memcached server died
|
2008-07-05 15:39:10 +00:00
|
|
|
} else {
|
|
|
|
|
# All good, just record the success
|
|
|
|
|
$this->mLoadedLanguages[$code] = true;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2015-05-20 16:51:29 +00:00
|
|
|
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
$info = implode( ', ', $where );
|
2014-10-03 03:52:05 +00:00
|
|
|
wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 16:51:29 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @param array $where List of wfDebug() comments
|
2015-08-27 08:46:05 +00:00
|
|
|
* @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
|
|
|
|
|
* @return bool|string True on success or one of ("cantacquire", "disabled")
|
2015-05-20 16:51:29 +00:00
|
|
|
*/
|
2015-08-27 08:46:05 +00:00
|
|
|
protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
global $wgUseLocalMessageCache;
|
|
|
|
|
|
2015-08-27 08:46:05 +00:00
|
|
|
# If cache updates on all levels fail, give up on message overrides.
|
|
|
|
|
# This is to avoid easy site outages; see $saveSuccess comments below.
|
2015-05-20 16:51:29 +00:00
|
|
|
$statusKey = wfMemcKey( 'messages', $code, 'status' );
|
2015-08-27 08:46:05 +00:00
|
|
|
$status = $this->mMemc->get( $statusKey );
|
|
|
|
|
if ( $status === 'error' ) {
|
|
|
|
|
$where[] = "could not load; method is still globally disabled";
|
|
|
|
|
return 'disabled';
|
2015-05-20 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Now let's regenerate
|
|
|
|
|
$where[] = 'loading from database';
|
|
|
|
|
|
2015-08-27 08:46:05 +00:00
|
|
|
# Lock the cache to prevent conflicting writes.
|
|
|
|
|
# This lock is non-blocking so stale cache can quickly be used.
|
|
|
|
|
# Note that load() will call a blocking getReentrantScopedLock()
|
|
|
|
|
# after this if it really need to wait for any current thread.
|
2015-05-20 16:51:29 +00:00
|
|
|
$cacheKey = wfMemcKey( 'messages', $code );
|
2015-08-27 08:46:05 +00:00
|
|
|
$scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
|
|
|
|
|
if ( !$scopedLock ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
$where[] = 'could not acquire main lock';
|
2015-08-27 08:46:05 +00:00
|
|
|
return 'cantacquire';
|
2015-05-20 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-27 08:46:05 +00:00
|
|
|
$cache = $this->loadFromDB( $code, $mode );
|
2015-05-20 16:51:29 +00:00
|
|
|
$this->mCache[$code] = $cache;
|
|
|
|
|
$saveSuccess = $this->saveToCaches( $cache, 'all', $code );
|
|
|
|
|
|
|
|
|
|
if ( !$saveSuccess ) {
|
2015-09-28 11:18:54 +00:00
|
|
|
/**
|
|
|
|
|
* Cache save has failed.
|
|
|
|
|
*
|
|
|
|
|
* There are two main scenarios where this could be a problem:
|
|
|
|
|
* - The cache is more than the maximum size (typically 1MB compressed).
|
|
|
|
|
* - Memcached has no space remaining in the relevant slab class. This is
|
|
|
|
|
* unlikely with recent versions of memcached.
|
|
|
|
|
*
|
|
|
|
|
* Either way, if there is a local cache, nothing bad will happen. If there
|
|
|
|
|
* is no local cache, disabling the message cache for all requests avoids
|
|
|
|
|
* incurring a loadFromDB() overhead on every request, and thus saves the
|
|
|
|
|
* wiki from complete downtime under moderate traffic conditions.
|
|
|
|
|
*/
|
2015-05-20 16:51:29 +00:00
|
|
|
if ( !$wgUseLocalMessageCache ) {
|
2015-08-27 08:46:05 +00:00
|
|
|
$this->mMemc->set( $statusKey, 'error', 60 * 5 );
|
2015-05-20 16:51:29 +00:00
|
|
|
$where[] = 'could not save cache, disabled globally for 5 minutes';
|
|
|
|
|
} else {
|
|
|
|
|
$where[] = "could not save global cache";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-07-05 15:39:10 +00:00
|
|
|
* Loads cacheable messages from the database. Messages bigger than
|
|
|
|
|
* $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
|
|
|
|
|
* on-demand from the database later.
|
|
|
|
|
*
|
2015-08-27 08:46:05 +00:00
|
|
|
* @param string $code Language code
|
|
|
|
|
* @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
|
|
|
|
|
* @return array Loaded messages for storing in caches
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2015-08-27 08:46:05 +00:00
|
|
|
function loadFromDB( $code, $mode = null ) {
|
2010-09-07 22:37:55 +00:00
|
|
|
global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
|
2015-05-20 02:34:20 +00:00
|
|
|
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA );
|
2015-08-27 08:46:05 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$cache = [];
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
# Common conditions
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [
|
2008-07-05 15:39:10 +00:00
|
|
|
'page_is_redirect' => 0,
|
|
|
|
|
'page_namespace' => NS_MEDIAWIKI,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$mostused = [];
|
2012-10-24 14:59:37 +00:00
|
|
|
if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
|
|
|
|
|
if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
|
|
|
|
|
$this->load( $wgLanguageCode );
|
|
|
|
|
}
|
|
|
|
|
$mostused = array_keys( $this->mCache[$wgLanguageCode] );
|
|
|
|
|
foreach ( $mostused as $key => $value ) {
|
|
|
|
|
$mostused[$key] = "$value/$code";
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-21 16:41:53 +00:00
|
|
|
if ( count( $mostused ) ) {
|
|
|
|
|
$conds['page_title'] = $mostused;
|
2010-09-07 22:37:55 +00:00
|
|
|
} elseif ( $code !== $wgLanguageCode ) {
|
2012-10-24 14:59:37 +00:00
|
|
|
$conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
|
2010-08-21 16:41:53 +00:00
|
|
|
} else {
|
|
|
|
|
# Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
|
|
|
|
|
# other than language code.
|
|
|
|
|
$conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Conditions to fetch oversized pages to ignore them
|
|
|
|
|
$bigConds = $conds;
|
|
|
|
|
$bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
# Load titles for all oversized pages in the MediaWiki namespace
|
2016-11-29 03:38:57 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'page',
|
|
|
|
|
[ 'page_title', 'page_latest' ],
|
|
|
|
|
$bigConds,
|
|
|
|
|
__METHOD__ . "($code)-big"
|
|
|
|
|
);
|
2010-08-07 23:43:28 +00:00
|
|
|
foreach ( $res as $row ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$cache[$row->page_title] = '!TOO BIG';
|
2016-11-29 03:38:57 +00:00
|
|
|
// At least include revision ID so page changes are reflected in the hash
|
|
|
|
|
$cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
|
2005-03-19 10:40:41 +00:00
|
|
|
}
|
2006-06-25 08:38:17 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Conditions to load the remaining pages with their contents
|
|
|
|
|
$smallConds = $conds;
|
|
|
|
|
$smallConds[] = 'page_latest=rev_id';
|
|
|
|
|
$smallConds[] = 'rev_text_id=old_id';
|
|
|
|
|
$smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
|
|
|
|
|
|
2011-01-26 17:41:18 +00:00
|
|
|
$res = $dbr->select(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page', 'revision', 'text' ],
|
|
|
|
|
[ 'page_title', 'old_text', 'old_flags' ],
|
2011-01-26 17:41:18 +00:00
|
|
|
$smallConds,
|
|
|
|
|
__METHOD__ . "($code)-small"
|
|
|
|
|
);
|
2006-06-25 08:38:17 +00:00
|
|
|
|
2010-08-07 23:43:28 +00:00
|
|
|
foreach ( $res as $row ) {
|
2011-09-27 00:41:24 +00:00
|
|
|
$text = Revision::getRevisionText( $row );
|
2013-04-08 12:09:48 +00:00
|
|
|
if ( $text === false ) {
|
2011-09-27 00:41:24 +00:00
|
|
|
// Failed to fetch data; possible ES errors?
|
|
|
|
|
// Store a marker to fetch on-demand as a workaround...
|
2016-10-17 15:00:47 +00:00
|
|
|
// TODO Use a differnt marker
|
2011-09-27 00:41:24 +00:00
|
|
|
$entry = '!TOO BIG';
|
2013-04-08 12:09:48 +00:00
|
|
|
wfDebugLog(
|
|
|
|
|
'MessageCache',
|
|
|
|
|
__METHOD__
|
|
|
|
|
. ": failed to load message page text for {$row->page_title} ($code)"
|
|
|
|
|
);
|
2011-09-27 00:41:24 +00:00
|
|
|
} else {
|
|
|
|
|
$entry = ' ' . $text;
|
|
|
|
|
}
|
|
|
|
|
$cache[$row->page_title] = $entry;
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
$cache['VERSION'] = MSG_CACHE_VERSION;
|
2015-08-13 04:40:49 +00:00
|
|
|
ksort( $cache );
|
2016-10-17 15:00:47 +00:00
|
|
|
|
|
|
|
|
# Hash for validating local cache (APC). No need to take into account
|
|
|
|
|
# messages larger than $wgMaxMsgCacheEntrySize, since those are only
|
|
|
|
|
# stored and fetched from memcache.
|
2015-08-13 04:40:49 +00:00
|
|
|
$cache['HASH'] = md5( serialize( $cache ) );
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
$cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
return $cache;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
/**
|
|
|
|
|
* Updates cache as necessary when message page is changed
|
|
|
|
|
*
|
2015-08-27 08:46:05 +00:00
|
|
|
* @param string|bool $title Name of the page changed (false if deleted)
|
2016-11-29 03:38:57 +00:00
|
|
|
* @param string|bool $text New contents of the page (false if deleted)
|
2008-07-05 15:39:10 +00:00
|
|
|
*/
|
|
|
|
|
public function replace( $title, $text ) {
|
2015-05-20 22:39:52 +00:00
|
|
|
global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( $this->mDisable ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
list( $msg, $code ) = $this->figureMessage( $title );
|
2015-05-20 22:39:52 +00:00
|
|
|
if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
|
2015-08-30 06:52:14 +00:00
|
|
|
// Content language overrides do not use the /<code> suffix
|
2015-05-20 22:39:52 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2015-08-30 06:52:14 +00:00
|
|
|
// Note that if the cache is volatile, load() may trigger a DB fetch.
|
|
|
|
|
// In that case we reenter/reuse the existing cache key lock to avoid
|
|
|
|
|
// a self-deadlock. This is safe as no reads happen *directly* in this
|
|
|
|
|
// method between getReentrantScopedLock() and load() below. There is
|
|
|
|
|
// no risk of data "changing under our feet" for replace().
|
2016-11-29 03:38:57 +00:00
|
|
|
$scopedLock = $this->getReentrantScopedLock( wfMemcKey( 'messages', $code ) );
|
|
|
|
|
// Load the messages from the master DB to avoid race conditions
|
2015-05-20 02:34:20 +00:00
|
|
|
$this->load( $code, self::FOR_UPDATE );
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
// Load the new value into the process cache...
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( $text === false ) {
|
|
|
|
|
$this->mCache[$code][$title] = '!NONEXISTENT';
|
|
|
|
|
} elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
|
|
|
|
|
$this->mCache[$code][$title] = '!TOO BIG';
|
2016-11-29 03:38:57 +00:00
|
|
|
// Pre-fill the individual key cache with the known latest message text
|
|
|
|
|
$key = $this->wanCache->makeKey( 'messages-big', $this->mCache[$code]['HASH'], $title );
|
|
|
|
|
$this->wanCache->set( $key, " $text", $this->mExpiry );
|
2010-08-08 00:28:17 +00:00
|
|
|
} else {
|
|
|
|
|
$this->mCache[$code][$title] = ' ' . $text;
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
2016-11-29 03:38:57 +00:00
|
|
|
// Mark this cache as definitely being "latest" (non-volatile) so
|
|
|
|
|
// load() calls do not try to refresh the cache with replica DB data
|
2015-08-30 06:52:14 +00:00
|
|
|
$this->mCache[$code]['LATEST'] = time();
|
|
|
|
|
|
|
|
|
|
// Update caches if the lock was acquired
|
2015-08-27 08:46:05 +00:00
|
|
|
if ( $scopedLock ) {
|
|
|
|
|
$this->saveToCaches( $this->mCache[$code], 'all', $code );
|
2016-11-29 03:38:57 +00:00
|
|
|
} else {
|
|
|
|
|
LoggerFactory::getInstance( 'MessageCache' )->error(
|
|
|
|
|
__METHOD__ . ': could not acquire lock to update {title} ({code})',
|
|
|
|
|
[ 'title' => $title, 'code' => $code ] );
|
2015-08-27 08:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-19 19:27:20 +00:00
|
|
|
ScopedCallback::consume( $scopedLock );
|
2016-10-17 15:00:47 +00:00
|
|
|
// Relay the purge. Touching this check key expires cache contents
|
|
|
|
|
// and local cache (APC) validation hash across all datacenters.
|
2015-05-20 16:51:29 +00:00
|
|
|
$this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
// Also delete cached sidebar... just in case it is affected
|
2016-02-17 09:09:32 +00:00
|
|
|
$codes = [ $code ];
|
2013-02-09 22:03:53 +00:00
|
|
|
if ( $code === 'en' ) {
|
2010-02-01 04:57:42 +00:00
|
|
|
// Delete all sidebars, like for example on action=purge on the
|
|
|
|
|
// sidebar messages
|
2012-03-08 20:56:26 +00:00
|
|
|
$codes = array_keys( Language::fetchLanguageNames() );
|
2010-02-01 04:57:42 +00:00
|
|
|
}
|
2009-07-31 07:12:25 +00:00
|
|
|
|
2010-02-01 04:57:42 +00:00
|
|
|
foreach ( $codes as $code ) {
|
|
|
|
|
$sidebarKey = wfMemcKey( 'sidebar', $code );
|
2015-10-28 05:25:36 +00:00
|
|
|
$this->wanCache->delete( $sidebarKey );
|
2009-07-31 07:12:25 +00:00
|
|
|
}
|
2011-01-26 17:41:18 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Update the message in the message blob store
|
2015-11-06 23:20:16 +00:00
|
|
|
$resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
|
|
|
|
|
$blobStore = $resourceloader->getMessageBlobStore();
|
2015-03-29 04:24:31 +00:00
|
|
|
$blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'MessageCacheReplace', [ $title, $text ] );
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
/**
|
|
|
|
|
* Is the given cache array expired due to time passing or a version change?
|
2013-04-08 12:09:48 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param array $cache
|
2013-04-08 12:09:48 +00:00
|
|
|
* @return bool
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
*/
|
|
|
|
|
protected function isCacheExpired( $cache ) {
|
|
|
|
|
if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
/**
|
|
|
|
|
* Shortcut to update caches.
|
|
|
|
|
*
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param array $cache Cached messages with a version.
|
|
|
|
|
* @param string $dest Either "local-only" to save to local caches only
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
* or "all" to save to all caches.
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string|bool $code Language code (default: false)
|
|
|
|
|
* @return bool
|
2008-07-05 15:39:10 +00:00
|
|
|
*/
|
2015-08-30 06:52:14 +00:00
|
|
|
protected function saveToCaches( array $cache, $dest, $code = false ) {
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
if ( $dest === 'all' ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
$cacheKey = wfMemcKey( 'messages', $code );
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
$success = $this->mMemc->set( $cacheKey, $cache );
|
2016-10-13 17:55:01 +00:00
|
|
|
$this->setValidationHash( $code, $cache );
|
* Converted BagOStuff.php from the style of memcached-client.php to the standard MediaWiki style, including camel case, using protected visibility instead of initial underscore, abstract functions instead of stubs, stylize.php.
* In SqlBagOStuff, ignore errors due to a read-only database, per my comments on CR r42796. Same for LocalisationCache.
* Merged SqlBagOStuff and MediaWikiBagOStuff, that proved to be an awkward and unnecessary generalisation. Use the standard quoting wrapper functions instead of $db->query().
* Implemented atomic incr() and decr() functions for SqlBagOStuff.
* Made incr() and decr() generally work roughly the same as it does in memcached, respecting negative steps instead of ignoring such operations. This allows decr() to be implemented in terms of incr().
* Per bug 11533, in MessageCache.php, don't retry 20 times on a cache failure, that's really memcached-specific and won't be useful for other cache types. It's not really very useful for memcached either.
* Moved MySQL-specific implementations of wasDeadlock() and wasErrorReissuable() to DatabaseMysql.
* Briefly tested page views with $wgReadOnly=read_only=1, fixed an error from Article::viewUpdates(). A CentralAuth fix will be in a subsequent commit.
2009-08-15 03:45:19 +00:00
|
|
|
} else {
|
|
|
|
|
$success = true;
|
2008-06-03 20:41:57 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2015-08-28 01:31:35 +00:00
|
|
|
$this->saveToLocalCache( $code, $cache );
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
return $success;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-20 16:51:29 +00:00
|
|
|
/**
|
2015-08-30 06:52:14 +00:00
|
|
|
* Get the md5 used to validate the local APC cache
|
2015-05-20 16:51:29 +00:00
|
|
|
*
|
|
|
|
|
* @param string $code
|
2015-08-12 02:53:03 +00:00
|
|
|
* @return array (hash or false, bool expiry/volatility status)
|
2015-05-20 16:51:29 +00:00
|
|
|
*/
|
|
|
|
|
protected function getValidationHash( $code ) {
|
|
|
|
|
$curTTL = null;
|
|
|
|
|
$value = $this->wanCache->get(
|
2016-11-29 03:38:57 +00:00
|
|
|
$this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
|
2015-05-20 16:51:29 +00:00
|
|
|
$curTTL,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ wfMemcKey( 'messages', $code ) ]
|
2015-05-20 16:51:29 +00:00
|
|
|
);
|
|
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
if ( $value ) {
|
2015-09-03 05:10:45 +00:00
|
|
|
$hash = $value['hash'];
|
2016-11-29 03:38:57 +00:00
|
|
|
if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) {
|
|
|
|
|
// Cache was recently updated via replace() and should be up-to-date.
|
|
|
|
|
// That method is only called in the primary datacenter and uses FOR_UPDATE.
|
|
|
|
|
// Also, it is unlikely that the current datacenter is *now* secondary one.
|
2015-09-03 05:10:45 +00:00
|
|
|
$expired = false;
|
|
|
|
|
} else {
|
|
|
|
|
// See if the "check" key was bumped after the hash was generated
|
|
|
|
|
$expired = ( $curTTL < 0 );
|
|
|
|
|
}
|
2016-11-29 03:38:57 +00:00
|
|
|
} else {
|
|
|
|
|
// No hash found at all; cache must regenerate to be safe
|
|
|
|
|
$hash = false;
|
|
|
|
|
$expired = true;
|
2015-08-30 06:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $hash, $expired ];
|
2015-05-20 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the md5 used to validate the local disk cache
|
|
|
|
|
*
|
2015-08-30 06:52:14 +00:00
|
|
|
* If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
|
2016-11-29 03:38:57 +00:00
|
|
|
* be treated as "volatile" by getValidationHash() for the next few seconds.
|
|
|
|
|
* This is triggered when $cache is generated using FOR_UPDATE mode.
|
2015-08-30 06:52:14 +00:00
|
|
|
*
|
2015-05-20 16:51:29 +00:00
|
|
|
* @param string $code
|
2015-08-30 06:52:14 +00:00
|
|
|
* @param array $cache Cached messages with a version
|
2015-05-20 16:51:29 +00:00
|
|
|
*/
|
2015-08-30 06:52:14 +00:00
|
|
|
protected function setValidationHash( $code, array $cache ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
$this->wanCache->set(
|
2016-11-29 03:38:57 +00:00
|
|
|
$this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-08-30 06:52:14 +00:00
|
|
|
'hash' => $cache['HASH'],
|
|
|
|
|
'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-10-13 15:15:36 +00:00
|
|
|
WANObjectCache::TTL_INDEFINITE
|
2015-05-20 16:51:29 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 21:46:44 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $key A language message cache key that stores blobs
|
2015-08-27 08:46:05 +00:00
|
|
|
* @param integer $timeout Wait timeout in seconds
|
2015-08-21 21:46:44 +00:00
|
|
|
* @return null|ScopedCallback
|
|
|
|
|
*/
|
2015-08-27 08:46:05 +00:00
|
|
|
protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
|
|
|
|
|
return $this->mMemc->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
|
2015-08-21 21:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
/**
|
2013-03-28 10:24:19 +00:00
|
|
|
* Get a message from either the content language or the user language.
|
2007-01-05 18:08:29 +00:00
|
|
|
*
|
2013-01-16 07:28:54 +00:00
|
|
|
* First, assemble a list of languages to attempt getting the message from. This
|
|
|
|
|
* chain begins with the requested language and its fallbacks and then continues with
|
|
|
|
|
* the content language and its fallbacks. For each language in the chain, the following
|
|
|
|
|
* process will occur (in this order):
|
|
|
|
|
* 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
|
|
|
|
|
* Note: for the content language, there is no /lang subpage.
|
|
|
|
|
* 2. Fetch from the static CDB cache.
|
|
|
|
|
* 3. If available, check the database for fallback language overrides.
|
2011-09-14 15:07:20 +00:00
|
|
|
*
|
2013-01-16 07:28:54 +00:00
|
|
|
* This process provides a number of guarantees. When changing this code, make sure all
|
|
|
|
|
* of these guarantees are preserved.
|
|
|
|
|
* * If the requested language is *not* the content language, then the CDB cache for that
|
|
|
|
|
* specific language will take precedence over the root database page ([[MW:msg]]).
|
|
|
|
|
* * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
|
|
|
|
|
* the message is available *anywhere* in the language for which it is a fallback.
|
|
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $key The message key
|
2013-01-16 07:28:54 +00:00
|
|
|
* @param bool $useDB If true, look for the message in the DB, false
|
2014-04-18 23:19:46 +00:00
|
|
|
* to use only the compiled l10n cache.
|
2013-01-16 07:28:54 +00:00
|
|
|
* @param bool|string|object $langcode Code of the language to get the message for.
|
2014-04-18 23:19:46 +00:00
|
|
|
* - If string and a valid code, will create a standard language object
|
|
|
|
|
* - If string but not a valid code, will create a basic language object
|
|
|
|
|
* - If boolean and false, create object from the current users language
|
|
|
|
|
* - If boolean and true, create object from the wikis content language
|
|
|
|
|
* - If language object, use it as given
|
2014-07-24 17:42:45 +00:00
|
|
|
* @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
|
2013-01-16 07:28:54 +00:00
|
|
|
*
|
2014-07-24 17:42:45 +00:00
|
|
|
* @throws MWException When given an invalid key
|
2013-11-17 20:49:59 +00:00
|
|
|
* @return string|bool False if the message doesn't exist, otherwise the
|
|
|
|
|
* message (which can be empty)
|
2007-01-05 18:08:29 +00:00
|
|
|
*/
|
2008-04-27 14:50:55 +00:00
|
|
|
function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( is_int( $key ) ) {
|
|
|
|
|
// Fix numerical strings that somehow become ints
|
|
|
|
|
// on their way here
|
|
|
|
|
$key = (string)$key;
|
|
|
|
|
} elseif ( !is_string( $key ) ) {
|
2011-01-26 17:41:18 +00:00
|
|
|
throw new MWException( 'Non-string key given' );
|
2013-01-16 07:28:54 +00:00
|
|
|
} elseif ( $key === '' ) {
|
|
|
|
|
// Shortcut: the empty key is always missing
|
2010-03-25 20:21:31 +00:00
|
|
|
return false;
|
2009-07-03 06:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
// For full keys, get the language code from the key
|
|
|
|
|
$pos = strrpos( $key, '/' );
|
|
|
|
|
if ( $isFullKey && $pos !== false ) {
|
|
|
|
|
$langcode = substr( $key, $pos + 1 );
|
|
|
|
|
$key = substr( $key, 0, $pos );
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
2013-03-28 10:24:19 +00:00
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
// Normalise title-case input (with some inlining)
|
2015-07-13 16:35:11 +00:00
|
|
|
$lckey = MessageCache::normalizeKey( $key );
|
2013-11-28 09:43:00 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'MessageCache::get', [ &$lckey ] );
|
2013-11-28 09:43:00 +00:00
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
// Loop through each language in the fallback list until we find something useful
|
|
|
|
|
$lang = wfGetLangObj( $langcode );
|
2013-11-17 20:49:59 +00:00
|
|
|
$message = $this->getMessageFromFallbackChain(
|
|
|
|
|
$lang,
|
|
|
|
|
$lckey,
|
|
|
|
|
!$this->mDisable && $useDB
|
|
|
|
|
);
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
// If we still have no message, maybe the key was in fact a full key so try that
|
2013-04-08 12:09:48 +00:00
|
|
|
if ( $message === false ) {
|
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput.
* The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true;
* Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache.
* Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words.
* $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c.
* Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess.
* Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly.
* Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load()
* Fixed FileDependency::__sleep()
* In Cdb.php, fixed newlines in debug messages
In MessageCache::get():
* Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII.
* Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us
* Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us.
* Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
|
|
|
$parts = explode( '/', $lckey );
|
2013-01-16 07:28:54 +00:00
|
|
|
// We may get calls for things that are http-urls from sidebar
|
|
|
|
|
// Let's not load nonexistent languages for those
|
|
|
|
|
// They usually have more than one slash.
|
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput.
* The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true;
* Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache.
* Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words.
* $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c.
* Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess.
* Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly.
* Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load()
* Fixed FileDependency::__sleep()
* In Cdb.php, fixed newlines in debug messages
In MessageCache::get():
* Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII.
* Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us
* Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us.
* Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
|
|
|
if ( count( $parts ) == 2 && $parts[1] !== '' ) {
|
|
|
|
|
$message = Language::getMessageFor( $parts[0], $parts[1] );
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $message === null ) {
|
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput.
* The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true;
* Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache.
* Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words.
* $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c.
* Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess.
* Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly.
* Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load()
* Fixed FileDependency::__sleep()
* In Cdb.php, fixed newlines in debug messages
In MessageCache::get():
* Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII.
* Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us
* Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us.
* Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
|
|
|
$message = false;
|
2006-08-10 09:10:06 +00:00
|
|
|
}
|
2006-08-07 12:21:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
// Post-processing if the message exists
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( $message !== false ) {
|
2013-01-16 07:28:54 +00:00
|
|
|
// Fix whitespace
|
|
|
|
|
$message = str_replace(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2013-01-16 07:28:54 +00:00
|
|
|
# Fix for trailing whitespace, removed by textarea
|
|
|
|
|
' ',
|
|
|
|
|
# Fix for NBSP, converted to space by firefox
|
|
|
|
|
' ',
|
|
|
|
|
' ',
|
2016-02-16 14:02:03 +00:00
|
|
|
'­'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-16 07:28:54 +00:00
|
|
|
' ',
|
|
|
|
|
"\xc2\xa0",
|
2016-02-16 14:02:03 +00:00
|
|
|
"\xc2\xa0",
|
|
|
|
|
"\xc2\xad"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2013-01-16 07:28:54 +00:00
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-17 17:51:37 +00:00
|
|
|
* Given a language, try and fetch messages from that language.
|
2013-01-16 07:28:54 +00:00
|
|
|
*
|
2015-11-17 17:51:37 +00:00
|
|
|
* Will also consider fallbacks of that language, the site language, and fallbacks for
|
|
|
|
|
* the site language.
|
2013-01-16 07:28:54 +00:00
|
|
|
*
|
|
|
|
|
* @see MessageCache::get
|
2015-11-17 17:51:37 +00:00
|
|
|
* @param Language|StubObject $lang Preferred language
|
|
|
|
|
* @param string $lckey Lowercase key for the message (as for localisation cache)
|
|
|
|
|
* @param bool $useDB Whether to include messages from the wiki database
|
2013-01-16 07:28:54 +00:00
|
|
|
* @return string|bool The message, or false if not found
|
|
|
|
|
*/
|
2015-11-17 17:51:37 +00:00
|
|
|
protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
global $wgContLang;
|
2013-01-16 07:28:54 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
$alreadyTried = [];
|
2013-01-16 07:28:54 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
// First try the requested language.
|
|
|
|
|
$message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $message !== false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
// Now try checking the site language.
|
|
|
|
|
$message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2013-01-16 07:28:54 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
/**
|
|
|
|
|
* Given a language, try and fetch messages from that language and its fallbacks.
|
|
|
|
|
*
|
|
|
|
|
* @see MessageCache::get
|
|
|
|
|
* @param Language|StubObject $lang Preferred language
|
|
|
|
|
* @param string $lckey Lowercase key for the message (as for localisation cache)
|
|
|
|
|
* @param bool $useDB Whether to include messages from the wiki database
|
|
|
|
|
* @param bool[] $alreadyTried Contains true for each language that has been tried already
|
|
|
|
|
* @return string|bool The message, or false if not found
|
|
|
|
|
*/
|
|
|
|
|
private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
|
|
|
|
|
global $wgContLang;
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
$langcode = $lang->getCode();
|
2013-01-16 07:28:54 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
// Try checking the database for the requested language
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $useDB ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
$uckey = $wgContLang->ucfirst( $lckey );
|
|
|
|
|
|
|
|
|
|
if ( !isset( $alreadyTried[ $langcode ] ) ) {
|
|
|
|
|
$message = $this->getMsgFromNamespace(
|
|
|
|
|
$this->getMessagePageName( $langcode, $uckey ),
|
|
|
|
|
$langcode
|
|
|
|
|
);
|
2013-01-16 07:28:54 +00:00
|
|
|
|
|
|
|
|
if ( $message !== false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2016-04-07 09:56:06 +00:00
|
|
|
$alreadyTried[ $langcode ] = true;
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
2016-10-15 02:11:51 +00:00
|
|
|
} else {
|
|
|
|
|
$uckey = null;
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
// Check the CDB cache
|
|
|
|
|
$message = $lang->getMessage( $lckey );
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $message !== null ) {
|
|
|
|
|
return $message;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2009-06-04 07:26:46 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
// Try checking the database for all of the fallback languages
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $useDB ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
$fallbackChain = Language::getFallbacksFor( $langcode );
|
|
|
|
|
|
|
|
|
|
foreach ( $fallbackChain as $code ) {
|
|
|
|
|
if ( isset( $alreadyTried[ $code ] ) ) {
|
|
|
|
|
continue;
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
2009-06-04 07:26:46 +00:00
|
|
|
|
2016-10-15 02:11:51 +00:00
|
|
|
$message = $this->getMsgFromNamespace(
|
|
|
|
|
$this->getMessagePageName( $code, $uckey ), $code );
|
2016-04-07 09:56:06 +00:00
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $message !== false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2016-04-07 09:56:06 +00:00
|
|
|
$alreadyTried[ $code ] = true;
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
/**
|
|
|
|
|
* Get the message page name for a given language
|
|
|
|
|
*
|
|
|
|
|
* @param string $langcode
|
|
|
|
|
* @param string $uckey Uppercase key for the message
|
|
|
|
|
* @return string The page name
|
|
|
|
|
*/
|
|
|
|
|
private function getMessagePageName( $langcode, $uckey ) {
|
|
|
|
|
global $wgLanguageCode;
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2016-04-07 09:56:06 +00:00
|
|
|
if ( $langcode === $wgLanguageCode ) {
|
|
|
|
|
// Messages created in the content language will not have the /lang extension
|
|
|
|
|
return $uckey;
|
|
|
|
|
} else {
|
|
|
|
|
return "$uckey/$langcode";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Get a message from the MediaWiki namespace, with caching. The key must
|
2007-01-05 18:08:29 +00:00
|
|
|
* first be converted to two-part lang/msg form if necessary.
|
|
|
|
|
*
|
2013-03-26 15:42:48 +00:00
|
|
|
* Unlike self::get(), this function doesn't resolve fallback chains, and
|
|
|
|
|
* some callers require this behavior. LanguageConverter::parseCachedTable()
|
|
|
|
|
* and self::get() are some examples in core.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $title Message cache key with initial uppercase letter.
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string $code Code denoting the language to try.
|
2013-06-11 23:01:52 +00:00
|
|
|
* @return string|bool The message, or false if it does not exist or on error
|
2007-01-05 18:08:29 +00:00
|
|
|
*/
|
2015-11-17 17:51:37 +00:00
|
|
|
public function getMsgFromNamespace( $title, $code ) {
|
2010-08-05 18:19:34 +00:00
|
|
|
$this->load( $code );
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( isset( $this->mCache[$code][$title] ) ) {
|
2010-08-05 18:19:34 +00:00
|
|
|
$entry = $this->mCache[$code][$title];
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( substr( $entry, 0, 1 ) === ' ' ) {
|
2016-11-29 03:38:57 +00:00
|
|
|
// The message exists, so make sure a string is returned.
|
2013-05-17 12:10:31 +00:00
|
|
|
return (string)substr( $entry, 1 );
|
2010-08-08 00:28:17 +00:00
|
|
|
} elseif ( $entry === '!NONEXISTENT' ) {
|
|
|
|
|
return false;
|
2013-04-08 12:09:48 +00:00
|
|
|
} elseif ( $entry === '!TOO BIG' ) {
|
2010-08-21 16:41:53 +00:00
|
|
|
// Fall through and try invididual message cache below
|
2010-12-10 13:18:11 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// XXX: This is not cached in process cache, should it?
|
|
|
|
|
$message = false;
|
2017-01-08 00:34:22 +00:00
|
|
|
Hooks::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
|
2010-12-10 13:18:11 +00:00
|
|
|
if ( $message !== false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2007-01-05 18:08:29 +00:00
|
|
|
|
2012-10-24 14:59:37 +00:00
|
|
|
return false;
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-17 16:37:36 +00:00
|
|
|
// Try the individual message cache
|
2016-11-29 03:38:57 +00:00
|
|
|
$titleKey = $this->wanCache->makeKey( 'messages-big', $this->mCache[$code]['HASH'], $title );
|
|
|
|
|
|
|
|
|
|
if ( $this->mCacheVolatile[$code] ) {
|
|
|
|
|
$entry = false;
|
|
|
|
|
// Make sure that individual keys respect the WAN cache holdoff period too
|
|
|
|
|
LoggerFactory::getInstance( 'MessageCache' )->debug(
|
|
|
|
|
__METHOD__ . ': loading volatile key \'{titleKey}\'',
|
|
|
|
|
[ 'titleKey' => $titleKey, 'code' => $code ] );
|
|
|
|
|
} else {
|
|
|
|
|
$entry = $this->wanCache->get( $titleKey );
|
|
|
|
|
}
|
2016-11-29 01:06:00 +00:00
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
if ( $entry !== false ) {
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( substr( $entry, 0, 1 ) === ' ' ) {
|
2010-08-05 18:19:34 +00:00
|
|
|
$this->mCache[$code][$title] = $entry;
|
2016-10-17 16:37:36 +00:00
|
|
|
// The message exists, so make sure a string is returned
|
2013-05-17 12:10:31 +00:00
|
|
|
return (string)substr( $entry, 1 );
|
2010-08-05 18:19:34 +00:00
|
|
|
} elseif ( $entry === '!NONEXISTENT' ) {
|
2010-08-08 00:28:17 +00:00
|
|
|
$this->mCache[$code][$title] = '!NONEXISTENT';
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2010-08-05 18:19:34 +00:00
|
|
|
return false;
|
|
|
|
|
} else {
|
2016-10-17 16:37:36 +00:00
|
|
|
// Corrupt/obsolete entry, delete it
|
2015-08-27 09:10:03 +00:00
|
|
|
$this->wanCache->delete( $titleKey );
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
// Try loading the message from the database
|
2016-10-17 16:37:36 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
|
|
|
$cacheOpts = Database::getCacheSetOptions( $dbr );
|
|
|
|
|
// Use newKnownCurrent() to avoid querying revision/user tables
|
|
|
|
|
$titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
|
|
|
|
|
if ( $titleObj->getLatestRevID() ) {
|
|
|
|
|
$revision = Revision::newKnownCurrent(
|
|
|
|
|
$dbr,
|
|
|
|
|
$titleObj->getArticleID(),
|
|
|
|
|
$titleObj->getLatestRevID()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$revision = false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( $revision ) {
|
2012-06-08 07:41:04 +00:00
|
|
|
$content = $revision->getContent();
|
2016-11-29 03:38:57 +00:00
|
|
|
if ( $content ) {
|
|
|
|
|
$message = $this->getMessageTextFromContent( $content );
|
|
|
|
|
if ( is_string( $message ) ) {
|
2012-06-08 07:41:04 +00:00
|
|
|
$this->mCache[$code][$title] = ' ' . $message;
|
2016-10-17 16:37:36 +00:00
|
|
|
$this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry, $cacheOpts );
|
2012-06-08 07:41:04 +00:00
|
|
|
}
|
2016-11-29 03:38:57 +00:00
|
|
|
} else {
|
|
|
|
|
// A possibly temporary loading failure
|
|
|
|
|
LoggerFactory::getInstance( 'MessageCache' )->warning(
|
|
|
|
|
__METHOD__ . ': failed to load message page text for \'{titleKey}\'',
|
|
|
|
|
[ 'titleKey' => $titleKey, 'code' => $code ] );
|
|
|
|
|
$message = null; // no negative caching
|
2011-09-27 00:41:24 +00:00
|
|
|
}
|
2005-09-11 14:50:47 +00:00
|
|
|
} else {
|
2012-06-08 07:41:04 +00:00
|
|
|
$message = false; // negative caching
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $message === false ) { // negative caching
|
2010-08-08 00:28:17 +00:00
|
|
|
$this->mCache[$code][$title] = '!NONEXISTENT';
|
2016-10-17 16:37:36 +00:00
|
|
|
$this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry, $cacheOpts );
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
2010-08-08 00:28:17 +00:00
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
2004-03-01 05:51:55 +00:00
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
/**
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string $message
|
|
|
|
|
* @param bool $interface
|
|
|
|
|
* @param string $language Language code
|
|
|
|
|
* @param Title $title
|
2011-04-18 12:43:53 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-02-09 15:19:45 +00:00
|
|
|
function transform( $message, $interface = false, $language = null, $title = null ) {
|
* Introduced a new system for localisation caching. The system is based around fast fetches of individual messages, minimising memory overhead and startup time in the typical case. It handles both core messages (formerly in Language.php) and extension messages (formerly in MessageCache.php). Profiling indicates a significant win for average throughput.
* The serialized message cache, which would have been redundant, has been removed. Similar performance characteristics can be achieved with $wgLocalisationCacheConf['manualRecache'] = true;
* Added a maintenance script rebuildLocalisationCache.php for offline rebuilding of the localisation cache.
* Extension i18n files can now contain any of the variables which can be set in Messages*.php. It is possible, and recommended, to use this feature instead of the hooks for special page aliases and magic words.
* $wgExtensionAliasesFiles, LanguageGetMagic and LanguageGetSpecialPageAliases are retained for backwards compatibility. $wgMessageCache->addMessages() and related functions have been removed. wfLoadExtensionMessages() is a no-op and can continue to be called for b/c.
* Introduced $wgCacheDirectory as a default location for the various local caches that have accumulated. Suggested $IP/cache as a good place for it in the default LocalSettings.php and created this directory with a deny-all .htaccess.
* Patched Exception.php to avoid using the message cache when an exception is thrown from within LocalisationCache, since this tends to fail horribly.
* Removed Language::getLocalisationArray(), Language::loadLocalisation(), Language::load()
* Fixed FileDependency::__sleep()
* In Cdb.php, fixed newlines in debug messages
In MessageCache::get():
* Replaced calls to $wgContLang capitalisation functions with plain PHP functions, reducing the typical case from 99us to 93us. Message cache keys are already documented as being restricted to ASCII.
* Implemented a more efficient way to filter out bogus language codes, reducing the "foo/en" case from 430us to 101us
* Optimised wfRunHooks() in the typical do-nothing case, from ~30us to ~3us. This reduced MessageCache::get() typical case time from 93us to 38us.
* Removed hook MessageNotInMwNs to save an extra 3us per cache hit. Reimplemented the only user (LocalisationUpdate) using the new hook LocalisationCacheRecache.
2009-06-28 07:11:43 +00:00
|
|
|
// Avoid creating parser if nothing to transform
|
2013-04-08 12:09:48 +00:00
|
|
|
if ( strpos( $message, '{{' ) === false ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
if ( $this->mInParser ) {
|
2011-04-18 12:59:50 +00:00
|
|
|
return $message;
|
2011-04-18 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parser = $this->getParser();
|
|
|
|
|
if ( $parser ) {
|
|
|
|
|
$popts = $this->getParserOptions();
|
|
|
|
|
$popts->setInterfaceMessage( $interface );
|
|
|
|
|
$popts->setTargetLanguage( $language );
|
|
|
|
|
|
2011-05-15 07:16:25 +00:00
|
|
|
$userlang = $popts->setUserLang( $language );
|
2011-04-18 12:43:53 +00:00
|
|
|
$this->mInParser = true;
|
|
|
|
|
$message = $parser->transformMsg( $message, $popts, $title );
|
|
|
|
|
$this->mInParser = false;
|
2011-05-15 07:16:25 +00:00
|
|
|
$popts->setUserLang( $userlang );
|
2011-04-18 12:43:53 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Parser
|
|
|
|
|
*/
|
|
|
|
|
function getParser() {
|
2008-08-26 14:37:15 +00:00
|
|
|
global $wgParser, $wgParserConf;
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2006-07-03 11:17:27 +00:00
|
|
|
if ( !$this->mParser && isset( $wgParser ) ) {
|
2006-07-04 09:13:10 +00:00
|
|
|
# Do some initialisation so that we don't have to do it twice
|
|
|
|
|
$wgParser->firstCallInit();
|
|
|
|
|
# Clone it and store it
|
2008-08-26 14:37:15 +00:00
|
|
|
$class = $wgParserConf['class'];
|
2014-06-22 20:13:46 +00:00
|
|
|
if ( $class == 'ParserDiffTest' ) {
|
2008-08-26 14:37:15 +00:00
|
|
|
# Uncloneable
|
|
|
|
|
$this->mParser = new $class( $wgParserConf );
|
|
|
|
|
} else {
|
|
|
|
|
$this->mParser = clone $wgParser;
|
|
|
|
|
}
|
2006-07-03 11:17:27 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
return $this->mParser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param bool $linestart Whether or not this is at the start of a line
|
|
|
|
|
* @param bool $interface Whether this is an interface message
|
2016-03-09 17:39:50 +00:00
|
|
|
* @param Language|string $language Language code
|
2012-11-01 17:13:30 +00:00
|
|
|
* @return ParserOutput|string
|
2011-04-18 12:43:53 +00:00
|
|
|
*/
|
2013-04-08 12:09:48 +00:00
|
|
|
public function parse( $text, $title = null, $linestart = true,
|
|
|
|
|
$interface = false, $language = null
|
|
|
|
|
) {
|
2016-11-29 03:38:57 +00:00
|
|
|
global $wgTitle;
|
|
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
if ( $this->mInParser ) {
|
2011-04-18 12:59:50 +00:00
|
|
|
return htmlspecialchars( $text );
|
2011-04-18 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parser = $this->getParser();
|
|
|
|
|
$popts = $this->getParserOptions();
|
2012-03-02 04:00:58 +00:00
|
|
|
$popts->setInterfaceMessage( $interface );
|
2016-03-09 17:39:50 +00:00
|
|
|
|
|
|
|
|
if ( is_string( $language ) ) {
|
|
|
|
|
$language = Language::factory( $language );
|
|
|
|
|
}
|
2012-03-02 04:00:58 +00:00
|
|
|
$popts->setTargetLanguage( $language );
|
2011-04-18 12:43:53 +00:00
|
|
|
|
2011-04-22 20:17:21 +00:00
|
|
|
if ( !$title || !$title instanceof Title ) {
|
2015-06-26 05:32:28 +00:00
|
|
|
wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
|
2016-09-15 22:41:11 +00:00
|
|
|
wfGetAllCallers( 6 ) . ' with no title set.' );
|
2011-04-22 20:17:21 +00:00
|
|
|
$title = $wgTitle;
|
2011-04-18 14:02:13 +00:00
|
|
|
}
|
2011-09-14 12:32:22 +00:00
|
|
|
// Sometimes $wgTitle isn't set either...
|
|
|
|
|
if ( !$title ) {
|
|
|
|
|
# It's not uncommon having a null $wgTitle in scripts. See r80898
|
|
|
|
|
# Create a ghost title in such case
|
2015-04-03 08:33:38 +00:00
|
|
|
$title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
|
2011-09-14 12:32:22 +00:00
|
|
|
}
|
2011-04-22 20:17:21 +00:00
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
$this->mInParser = true;
|
|
|
|
|
$res = $parser->parse( $text, $title, $popts, $linestart );
|
|
|
|
|
$this->mInParser = false;
|
|
|
|
|
|
|
|
|
|
return $res;
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2011-01-26 17:41:18 +00:00
|
|
|
function disable() {
|
|
|
|
|
$this->mDisable = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enable() {
|
|
|
|
|
$this->mDisable = false;
|
|
|
|
|
}
|
2010-02-05 04:25:30 +00:00
|
|
|
|
2016-04-20 19:37:00 +00:00
|
|
|
/**
|
|
|
|
|
* Whether DB/cache usage is disabled for determining messages
|
|
|
|
|
*
|
|
|
|
|
* If so, this typically indicates either:
|
|
|
|
|
* - a) load() failed to find a cached copy nor query the DB
|
|
|
|
|
* - b) we are in a special context or error mode that cannot use the DB
|
|
|
|
|
* If the DB is ignored, any derived HTML output or cached objects may be wrong.
|
|
|
|
|
* To avoid long-term cache pollution, TTLs can be adjusted accordingly.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public function isDisabled() {
|
|
|
|
|
return $this->mDisable;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Clear all stored messages. Mainly used after a mass rebuild.
|
|
|
|
|
*/
|
2004-08-11 02:31:47 +00:00
|
|
|
function clear() {
|
2012-03-08 20:56:26 +00:00
|
|
|
$langs = Language::fetchLanguageNames( null, 'mw' );
|
2012-10-24 14:59:37 +00:00
|
|
|
foreach ( array_keys( $langs ) as $code ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
# Global and local caches
|
|
|
|
|
$this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
|
2004-08-11 02:31:47 +00:00
|
|
|
}
|
2015-05-20 16:51:29 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mLoadedLanguages = [];
|
2004-08-11 02:31:47 +00:00
|
|
|
}
|
2006-08-24 16:58:44 +00:00
|
|
|
|
2011-09-14 15:07:20 +00:00
|
|
|
/**
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $key
|
2011-09-14 15:07:20 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
public function figureMessage( $key ) {
|
2010-09-07 22:37:55 +00:00
|
|
|
global $wgLanguageCode;
|
2015-05-20 22:39:52 +00:00
|
|
|
|
2008-10-07 18:10:08 +00:00
|
|
|
$pieces = explode( '/', $key );
|
2013-04-08 12:09:48 +00:00
|
|
|
if ( count( $pieces ) < 2 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $key, $wgLanguageCode ];
|
2011-01-26 17:41:18 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2008-10-07 18:10:08 +00:00
|
|
|
$lang = array_pop( $pieces );
|
2013-04-08 12:09:48 +00:00
|
|
|
if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $key, $wgLanguageCode ];
|
2011-01-26 17:41:18 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2008-10-07 18:10:08 +00:00
|
|
|
$message = implode( '/', $pieces );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $message, $lang ];
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-14 19:59:50 +00:00
|
|
|
/**
|
|
|
|
|
* Get all message keys stored in the message cache for a given language.
|
|
|
|
|
* If $code is the content language code, this will return all message keys
|
|
|
|
|
* for which MediaWiki:msgkey exists. If $code is another language code, this
|
|
|
|
|
* will ONLY return message keys for which MediaWiki:msgkey/$code exists.
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string $code Language code
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return array Array of message keys (strings)
|
2011-09-14 19:59:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getAllMessageKeys( $code ) {
|
|
|
|
|
global $wgContLang;
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2011-09-14 19:59:50 +00:00
|
|
|
$this->load( $code );
|
|
|
|
|
if ( !isset( $this->mCache[$code] ) ) {
|
|
|
|
|
// Apparently load() failed
|
|
|
|
|
return null;
|
|
|
|
|
}
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
// Remove administrative keys
|
|
|
|
|
$cache = $this->mCache[$code];
|
|
|
|
|
unset( $cache['VERSION'] );
|
|
|
|
|
unset( $cache['EXPIRY'] );
|
2016-11-29 03:38:57 +00:00
|
|
|
unset( $cache['EXCESSIVE'] );
|
Fix message cache expiry semantics
* Use the stale message cache while the new one is being generated
* Revert I811755d4 (make message cache load failure fatal). This
escalated several very plausible temporary site issues from barely
noticeable to complete downtime -- for example, memcached being down
on a site with only one memcached server.
* Remove $wgLocalMessageCacheSerialized, it's always been pointless
* Clarify a couple of comments.
* Increased lock wait timeout to 30s
* Make lock() fail immediately on memcached connection refused
Tests done:
* With local cache enabled: normal cold refill; refill local from
global cache; use stale local cache during remote refill; use stale
global cache during remote refill; cold cache wait for remote refill;
saveToCaches() failure; memcached connection refused.
* With local cache disabled: saveToCaches() failure; cache disabled due
to "error" status key; memcached connection refused.
Setting a 1-day expiry in memcached, with a ~10s CPU cost to replace, is
not the best idea since it inevitably leads to a cache stampede. Dealing
with the stampede by waiting for a lock is not ideal, even if it were
implemented properly, since it's not necessary to deliver perfectly
fresh message cache data to all clients.
This is especially obvious when you note that barring bugs, expiry and
regeneration always gives you back the exact same data, because we have
incremental updates (MessageCache::replace()). Keeping all clients
waiting for 10s just to give them the data they have already is pretty
pointless.
So, continue to serve the site from the stale message cache while the
new one is being generated.
One caveat: if local caching enabled, when the message cache becomes
stale, a sudden spike in network bandwidth may result due to the full
array (also typically stale) being fetched from the shared cache.
Bug: 43516
Change-Id: Ia145fd90da33956d8aac127634606aaecfaa176b
2013-04-03 10:54:34 +00:00
|
|
|
// Remove any !NONEXISTENT keys
|
2016-02-17 09:09:32 +00:00
|
|
|
$cache = array_diff( $cache, [ '!NONEXISTENT' ] );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-09-14 19:59:50 +00:00
|
|
|
// Keys may appear with a capital first letter. lcfirst them.
|
2016-02-17 09:09:32 +00:00
|
|
|
return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
|
2011-09-14 19:59:50 +00:00
|
|
|
}
|
2016-11-29 03:38:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Purge message caches when a MediaWiki: page is created, updated, or deleted
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title Message page title
|
|
|
|
|
* @param Content|null $content New content for edit/create, null on deletion
|
|
|
|
|
* @since 1.29
|
|
|
|
|
*/
|
|
|
|
|
public function updateMessageOverride( Title $title, Content $content = null ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
|
|
|
|
|
$msgText = $this->getMessageTextFromContent( $content );
|
|
|
|
|
if ( $msgText === null ) {
|
|
|
|
|
$msgText = false; // treat as not existing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->replace( $title->getDBkey(), $msgText );
|
|
|
|
|
|
|
|
|
|
if ( $wgContLang->hasVariants() ) {
|
|
|
|
|
$wgContLang->updateConversionTable( $title );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Content|null $content Content or null if the message page does not exist
|
|
|
|
|
* @return string|bool|null Returns false if $content is null and null on error
|
|
|
|
|
*/
|
|
|
|
|
private function getMessageTextFromContent( Content $content = null ) {
|
|
|
|
|
// @TODO: could skip pseudo-messages like js/css here, based on content model
|
|
|
|
|
if ( $content ) {
|
|
|
|
|
// Message page exists...
|
|
|
|
|
// XXX: Is this the right way to turn a Content object into a message?
|
|
|
|
|
// NOTE: $content is typically either WikitextContent, JavaScriptContent or
|
|
|
|
|
// CssContent. MessageContent is *not* used for storing messages, it's
|
|
|
|
|
// only used for wrapping them when needed.
|
|
|
|
|
$msgText = $content->getWikitextForTransclusion();
|
|
|
|
|
if ( $msgText === false || $msgText === null ) {
|
|
|
|
|
// This might be due to some kind of misconfiguration...
|
|
|
|
|
$msgText = null;
|
|
|
|
|
LoggerFactory::getInstance( 'MessageCache' )->warning(
|
|
|
|
|
__METHOD__ . ": message content doesn't provide wikitext "
|
|
|
|
|
. "(content model: " . $content->getModel() . ")" );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Message page does not exist...
|
|
|
|
|
$msgText = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $msgText;
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|