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
|
|
|
* 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
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2023-11-21 21:08:14 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
|
|
|
|
use MediaWiki\Deferred\MessageCacheUpdate;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2021-10-15 23:20:32 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2019-08-26 12:24:37 +00:00
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
2020-01-03 23:03:14 +00:00
|
|
|
use MediaWiki\Languages\LanguageFallback;
|
|
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2019-07-22 09:49:57 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2019-12-20 13:12:59 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2016-09-29 03:59:11 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-05-12 01:20:12 +00:00
|
|
|
use MediaWiki\Page\PageReference;
|
|
|
|
|
use MediaWiki\Page\PageReferenceValue;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2020-02-29 03:49:41 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2022-10-25 16:58:49 +00:00
|
|
|
use MediaWiki\StubObject\StubObject;
|
2023-01-12 18:04:20 +00:00
|
|
|
use MediaWiki\StubObject\StubUserLang;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2019-10-11 23:37:37 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2023-08-06 21:41:29 +00:00
|
|
|
use Wikimedia\LightweightObjectStore\ExpirationAwareness;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2023-11-07 17:30:50 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
2022-07-14 11:46:16 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
2023-11-07 17:30:50 +00:00
|
|
|
use Wikimedia\Rdbms\LikeValue;
|
2022-02-01 01:11:09 +00:00
|
|
|
use Wikimedia\RequestTimeout\TimeoutException;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
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
|
|
|
/**
|
language: Add missing `@ingroup`, subgroup "Languages" and ungroup files
== Ungroup file blocks
Remove `@ingroup` from `@file` blocks and keep only the class block.
This matches similar changes previously applied to API, Skins, Profile,
and ResourceLoader.
This helps make the API documentation easier to navigate.
E.g. Modules -> Language in the sidebar of
<https://doc.wikimedia.org/mediawiki-core/master/php/> as well as
<https://doc.wikimedia.org/mediawiki-core/master/php/group__Language.html>
These are currently cluttered with tons of duplicate entries for files
and classes both. We only need to group files that aren't also
documented as a class (e.g. message files, entry points, other scripts
or files that we mainly consider a data file). This has the helpful
side-effect that we don't encourage duplication of the class
description (or worse, place useful docs only in the file block), and
makes the class files consistently start with a mentally ignorable
block. Basically, unless there's something other than a class, don't
describe or group the file itself.
== Missing group
Various classes in this subtree were missing the `Language` group,
or were using different group from before T225756.
== Subgroup
For ease of navigation, move Converter subclasses to a group called
"Languages", which for documentation purposes is a subgroup of
"Language". The next commit does the same for Messages* files,
and Language subclasses (done separately for ease of review).
Change-Id: I301f471f86ba2dee924fece29a16dc3c20b5bebe
2022-06-22 22:37:31 +00:00
|
|
|
* Cache messages that are defined by MediaWiki-namespace pages or by hooks.
|
2018-06-23 16:59:12 +00:00
|
|
|
*
|
language: Add missing `@ingroup`, subgroup "Languages" and ungroup files
== Ungroup file blocks
Remove `@ingroup` from `@file` blocks and keep only the class block.
This matches similar changes previously applied to API, Skins, Profile,
and ResourceLoader.
This helps make the API documentation easier to navigate.
E.g. Modules -> Language in the sidebar of
<https://doc.wikimedia.org/mediawiki-core/master/php/> as well as
<https://doc.wikimedia.org/mediawiki-core/master/php/group__Language.html>
These are currently cluttered with tons of duplicate entries for files
and classes both. We only need to group files that aren't also
documented as a class (e.g. message files, entry points, other scripts
or files that we mainly consider a data file). This has the helpful
side-effect that we don't encourage duplication of the class
description (or worse, place useful docs only in the file block), and
makes the class files consistently start with a mentally ignorable
block. Basically, unless there's something other than a class, don't
describe or group the file itself.
== Missing group
Various classes in this subtree were missing the `Language` group,
or were using different group from before T225756.
== Subgroup
For ease of navigation, move Converter subclasses to a group called
"Languages", which for documentation purposes is a subgroup of
"Language". The next commit does the same for Messages* files,
and Language subclasses (done separately for ease of review).
Change-Id: I301f471f86ba2dee924fece29a16dc3c20b5bebe
2022-06-22 22:37:31 +00:00
|
|
|
* @ingroup Language
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2019-10-11 23:37:37 +00:00
|
|
|
class MessageCache implements LoggerAwareInterface {
|
2022-07-14 06:24:44 +00:00
|
|
|
/**
|
|
|
|
|
* Options to be included in the ServiceOptions
|
|
|
|
|
*/
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
|
MainConfigNames::UseDatabaseMessages,
|
|
|
|
|
MainConfigNames::MaxMsgCacheEntrySize,
|
|
|
|
|
MainConfigNames::AdaptiveMessageCache,
|
2023-07-12 15:44:30 +00:00
|
|
|
MainConfigNames::UseXssLanguage,
|
|
|
|
|
MainConfigNames::RawHtmlMessages,
|
2022-07-14 06:24:44 +00:00
|
|
|
];
|
|
|
|
|
|
2022-06-15 00:40:56 +00:00
|
|
|
/**
|
|
|
|
|
* The size of the MapCacheLRU which stores message data. The maximum
|
|
|
|
|
* number of languages which can be efficiently loaded in a given request.
|
|
|
|
|
*/
|
|
|
|
|
public const MAX_REQUEST_LANGUAGES = 10;
|
|
|
|
|
|
2020-05-15 22:16:46 +00:00
|
|
|
private const FOR_UPDATE = 1; // force message reload
|
2015-05-20 02:34:20 +00:00
|
|
|
|
2015-08-19 19:27:20 +00:00
|
|
|
/** How long to wait for memcached locks */
|
2020-05-15 22:16:46 +00:00
|
|
|
private const WAIT_SEC = 15;
|
2015-08-27 08:46:05 +00:00
|
|
|
/** How long memcached locks last */
|
2020-05-15 22:16:46 +00:00
|
|
|
private const LOCK_TTL = 30;
|
2015-08-19 19:27:20 +00:00
|
|
|
|
2019-08-27 15:35:49 +00:00
|
|
|
/**
|
|
|
|
|
* Lifetime for cache, for keys stored in $wanCache, in seconds.
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2023-08-06 21:41:29 +00:00
|
|
|
private const WAN_TTL = ExpirationAwareness::TTL_DAY;
|
2019-08-27 15:35:49 +00:00
|
|
|
|
2019-10-11 23:37:37 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
private $logger;
|
|
|
|
|
|
2010-08-05 18:38:42 +00:00
|
|
|
/**
|
2018-06-25 09:45:55 +00:00
|
|
|
* Process cache of loaded messages that are defined in MediaWiki namespace
|
|
|
|
|
*
|
2019-03-15 15:59:41 +00:00
|
|
|
* @var MapCacheLRU Map of (language code => key => " <MESSAGE>" or "!TOO BIG" or "!ERROR")
|
2010-08-05 18:38:42 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private $cache;
|
2010-08-05 18:38:42 +00:00
|
|
|
|
2018-10-15 15:08:24 +00:00
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Map of (lowercase message key => unused) for all software-defined messages
|
2018-10-15 15:08:24 +00:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-05-11 06:05:11 +00:00
|
|
|
private $systemMessageNames;
|
2018-10-15 15:08:24 +00:00
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool[] Map of (language code => boolean)
|
|
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private $cacheVolatile = [];
|
2016-11-29 03:38:57 +00:00
|
|
|
|
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
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var bool
|
2013-05-20 09:16:27 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private $disable;
|
|
|
|
|
|
|
|
|
|
/** @var int Maximum entry size in bytes */
|
|
|
|
|
private $maxEntrySize;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $adaptive;
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2023-07-12 15:44:30 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
private $useXssLanguage;
|
|
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
|
private $rawHtmlMessages;
|
|
|
|
|
|
2010-08-05 18:38:42 +00:00
|
|
|
/**
|
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
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private $parserOptions;
|
2023-10-02 12:56:25 +00:00
|
|
|
|
2023-07-29 19:21:04 +00:00
|
|
|
/** @var ?Parser Lazy-created via self::getParser() */
|
|
|
|
|
private $parser = null;
|
2004-09-25 02:23:04 +00:00
|
|
|
|
2015-05-20 16:51:29 +00:00
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var bool
|
2015-05-20 16:51:29 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private $inParser = false;
|
2015-05-20 16:51:29 +00:00
|
|
|
|
|
|
|
|
/** @var WANObjectCache */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $wanCache;
|
2017-01-21 01:47:15 +00:00
|
|
|
/** @var BagOStuff */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $clusterCache;
|
2017-01-21 01:47:15 +00:00
|
|
|
/** @var BagOStuff */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $srvCache;
|
2018-07-29 12:24:54 +00:00
|
|
|
/** @var Language */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $contLang;
|
2021-05-13 23:32:38 +00:00
|
|
|
/** @var string */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $contLangCode;
|
2020-01-23 18:39:23 +00:00
|
|
|
/** @var ILanguageConverter */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $contLangConverter;
|
2019-08-26 12:24:37 +00:00
|
|
|
/** @var LanguageFactory */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $langFactory;
|
2019-10-28 18:05:35 +00:00
|
|
|
/** @var LocalisationCache */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $localisationCache;
|
2020-01-03 23:03:14 +00:00
|
|
|
/** @var LanguageNameUtils */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $languageNameUtils;
|
2020-01-03 23:03:14 +00:00
|
|
|
/** @var LanguageFallback */
|
2022-07-14 06:24:44 +00:00
|
|
|
private $languageFallback;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
/** @var HookRunner */
|
|
|
|
|
private $hookRunner;
|
2023-07-29 19:21:04 +00:00
|
|
|
/** @var ParserFactory */
|
|
|
|
|
private $parserFactory;
|
2015-05-20 16:51:29 +00:00
|
|
|
|
2023-01-28 11:31:48 +00:00
|
|
|
/** @var (string|callable)[]|null */
|
|
|
|
|
private $messageKeyOverrides;
|
|
|
|
|
|
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 ) {
|
|
|
|
|
$lckey = strtr( $key, ' ', '_' );
|
2022-02-04 03:37:36 +00:00
|
|
|
if ( $lckey === '' ) {
|
|
|
|
|
// T300792
|
|
|
|
|
return $lckey;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 16:35:11 +00:00
|
|
|
if ( ord( $lckey ) < 128 ) {
|
|
|
|
|
$lckey[0] = strtolower( $lckey[0] );
|
|
|
|
|
} else {
|
2018-07-29 12:24:54 +00:00
|
|
|
$lckey = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $lckey );
|
2015-07-13 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $lckey;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-20 09:16:27 +00:00
|
|
|
/**
|
2019-10-11 23:37:37 +00:00
|
|
|
* @internal For use by ServiceWiring
|
2017-12-28 15:06:10 +00:00
|
|
|
* @param WANObjectCache $wanCache
|
|
|
|
|
* @param BagOStuff $clusterCache
|
|
|
|
|
* @param BagOStuff $serverCache
|
2019-10-11 23:37:37 +00:00
|
|
|
* @param Language $contLang Content language of site
|
2021-10-15 23:20:32 +00:00
|
|
|
* @param LanguageConverterFactory $langConverterFactory
|
2019-10-11 23:37:37 +00:00
|
|
|
* @param LoggerInterface $logger
|
2022-07-14 06:24:44 +00:00
|
|
|
* @param ServiceOptions $options
|
2019-08-26 12:24:37 +00:00
|
|
|
* @param LanguageFactory $langFactory
|
2019-10-28 18:05:35 +00:00
|
|
|
* @param LocalisationCache $localisationCache
|
2020-01-03 23:03:14 +00:00
|
|
|
* @param LanguageNameUtils $languageNameUtils
|
|
|
|
|
* @param LanguageFallback $languageFallback
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
* @param HookContainer $hookContainer
|
2023-07-29 19:21:04 +00:00
|
|
|
* @param ParserFactory $parserFactory
|
2013-05-20 09:16:27 +00:00
|
|
|
*/
|
2017-01-21 01:47:15 +00:00
|
|
|
public function __construct(
|
|
|
|
|
WANObjectCache $wanCache,
|
|
|
|
|
BagOStuff $clusterCache,
|
2017-12-28 15:06:10 +00:00
|
|
|
BagOStuff $serverCache,
|
2019-10-11 23:37:37 +00:00
|
|
|
Language $contLang,
|
2021-10-15 23:20:32 +00:00
|
|
|
LanguageConverterFactory $langConverterFactory,
|
2019-10-11 23:37:37 +00:00
|
|
|
LoggerInterface $logger,
|
2022-07-14 06:24:44 +00:00
|
|
|
ServiceOptions $options,
|
2019-10-28 18:05:35 +00:00
|
|
|
LanguageFactory $langFactory,
|
2020-01-03 23:03:14 +00:00
|
|
|
LocalisationCache $localisationCache,
|
|
|
|
|
LanguageNameUtils $languageNameUtils,
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
LanguageFallback $languageFallback,
|
2023-07-29 19:21:04 +00:00
|
|
|
HookContainer $hookContainer,
|
|
|
|
|
ParserFactory $parserFactory
|
2017-01-21 01:47:15 +00:00
|
|
|
) {
|
|
|
|
|
$this->wanCache = $wanCache;
|
|
|
|
|
$this->clusterCache = $clusterCache;
|
2017-12-28 15:06:10 +00:00
|
|
|
$this->srvCache = $serverCache;
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->contLang = $contLang;
|
2021-10-15 23:20:32 +00:00
|
|
|
$this->contLangConverter = $langConverterFactory->getLanguageConverter( $contLang );
|
2021-05-13 23:32:38 +00:00
|
|
|
$this->contLangCode = $contLang->getCode();
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger = $logger;
|
2019-08-26 12:24:37 +00:00
|
|
|
$this->langFactory = $langFactory;
|
2019-10-28 18:05:35 +00:00
|
|
|
$this->localisationCache = $localisationCache;
|
2020-01-03 23:03:14 +00:00
|
|
|
$this->languageNameUtils = $languageNameUtils;
|
|
|
|
|
$this->languageFallback = $languageFallback;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2023-07-29 19:21:04 +00:00
|
|
|
$this->parserFactory = $parserFactory;
|
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
|
|
|
|
2021-11-19 23:19:42 +00:00
|
|
|
// limit size
|
2022-06-15 00:40:56 +00:00
|
|
|
$this->cache = new MapCacheLRU( self::MAX_REQUEST_LANGUAGES );
|
2018-06-25 09:45:55 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->disable = !$options->get( MainConfigNames::UseDatabaseMessages );
|
|
|
|
|
$this->maxEntrySize = $options->get( MainConfigNames::MaxMsgCacheEntrySize );
|
|
|
|
|
$this->adaptive = $options->get( MainConfigNames::AdaptiveMessageCache );
|
2023-07-12 15:44:30 +00:00
|
|
|
$this->useXssLanguage = $options->get( MainConfigNames::UseXssLanguage );
|
|
|
|
|
$this->rawHtmlMessages = $options->get( MainConfigNames::RawHtmlMessages );
|
2019-10-11 23:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
$this->logger = $logger;
|
2008-06-30 03:02:06 +00:00
|
|
|
}
|
2008-06-03 20:41:57 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* ParserOptions is lazily initialised.
|
2011-04-18 12:43:53 +00:00
|
|
|
*
|
|
|
|
|
* @return ParserOptions
|
2008-07-05 15:39:10 +00:00
|
|
|
*/
|
2020-03-13 03:07:52 +00:00
|
|
|
private function getParserOptions() {
|
2022-07-14 06:24:44 +00:00
|
|
|
if ( !$this->parserOptions ) {
|
2021-10-26 22:10:36 +00:00
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
$user = $context->getUser();
|
2021-06-24 08:32:02 +00:00
|
|
|
if ( !$user->isSafeToLoad() ) {
|
|
|
|
|
// It isn't safe to use the context user yet, so don't try to get a
|
2016-02-01 20:44:03 +00:00
|
|
|
// ParserOptions for it. And don't cache this ParserOptions
|
|
|
|
|
// either.
|
2016-02-03 20:41:00 +00:00
|
|
|
$po = ParserOptions::newFromAnon();
|
2017-02-06 03:00:39 +00:00
|
|
|
$po->setAllowUnsafeRawHtml( false );
|
2016-02-01 20:44:03 +00:00
|
|
|
return $po;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->parserOptions = ParserOptions::newFromContext( $context );
|
2017-02-06 03:00:39 +00:00
|
|
|
// Messages may take parameters that could come
|
|
|
|
|
// from malicious sources. As a precaution, disable
|
|
|
|
|
// the <html> parser tag when parsing messages.
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->parserOptions->setAllowUnsafeRawHtml( false );
|
2006-07-26 07:15:39 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
return $this->parserOptions;
|
2006-07-26 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2019-05-28 19:12:45 +00:00
|
|
|
* @param string $code Optional language code, see documentation of load().
|
2020-11-30 16:58:58 +00:00
|
|
|
* @return array|false The cache array, or false if not in cache.
|
2005-11-08 11:54:04 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private function getLocalCache( $code ) {
|
2017-05-24 12:49:53 +00:00
|
|
|
$cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
|
2015-08-28 01:31:35 +00:00
|
|
|
|
2017-01-21 01:47:15 +00:00
|
|
|
return $this->srvCache->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
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private function saveToLocalCache( $code, $cache ) {
|
2017-05-24 12:49:53 +00:00
|
|
|
$cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
|
2017-01-21 01:47:15 +00:00
|
|
|
$this->srvCache->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.
|
|
|
|
|
*
|
2018-08-14 07:56:35 +00:00
|
|
|
* When successfully loading from (2) or (3), all higher level caches are
|
2008-07-05 15:39:10 +00:00
|
|
|
* 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?).
|
|
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* Returns true if cache is already populated, or it was successfully populated,
|
2008-07-05 15:39:10 +00:00
|
|
|
* or false if populating empty cache fails. Also returns true if MessageCache
|
|
|
|
|
* is disabled.
|
|
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $code Which language to load messages for
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2022-07-15 04:32:46 +00:00
|
|
|
private function load( string $code, $mode = null ) {
|
2022-07-14 06:24:44 +00:00
|
|
|
// Don't do double loading...
|
2022-07-15 04:32:46 +00:00
|
|
|
if ( $this->isLanguageLoaded( $code ) && $mode !== self::FOR_UPDATE ) {
|
2011-01-26 17:41:18 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
// Show a log message (once) if loading is disabled
|
|
|
|
|
if ( $this->disable ) {
|
2005-05-28 11:07:55 +00:00
|
|
|
static $shownDisabled = false;
|
|
|
|
|
if ( !$shownDisabled ) {
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ': disabled' );
|
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
|
|
|
|
2022-07-14 05:47:25 +00:00
|
|
|
try {
|
|
|
|
|
return $this->loadUnguarded( $code, $mode );
|
|
|
|
|
} catch ( Throwable $e ) {
|
|
|
|
|
// Don't try to load again during the exception handler
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->disable = true;
|
2022-07-14 05:47:25 +00:00
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load messages from the cache or database, without exception guarding.
|
|
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $code Which language to load messages for
|
2022-07-14 05:47:25 +00:00
|
|
|
* @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function loadUnguarded( $code, $mode ) {
|
2022-07-14 06:24:44 +00:00
|
|
|
$success = false; // Keep track of success
|
|
|
|
|
$staleCache = false; // a cache array with expired data, or false if none has been loaded
|
|
|
|
|
$where = []; // Debug info, delayed to avoid spamming debug log too much
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2023-02-03 21:36:06 +00:00
|
|
|
// A hash of the expected content is stored in a WAN cache key, providing a way
|
|
|
|
|
// to invalid the local cache on every server whenever a message page changes.
|
2022-10-21 04:32:38 +00:00
|
|
|
[ $hash, $hashVolatile ] = $this->getValidationHash( $code );
|
2018-06-25 12:15:27 +00:00
|
|
|
$this->cacheVolatile[$code] = $hashVolatile;
|
2023-02-04 08:26:19 +00:00
|
|
|
$volatilityOnlyStaleness = false;
|
2015-08-12 02:53:03 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
// Try the local cache and check against the cluster hash key...
|
2015-08-28 01:31:35 +00:00
|
|
|
$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 ) {
|
2023-02-03 21:36:06 +00:00
|
|
|
// Some recent message page changes might not show due to DB lag
|
2015-08-28 01:31:35 +00:00
|
|
|
$where[] = 'local cache validation key is expired/volatile';
|
|
|
|
|
$staleCache = $cache;
|
2023-02-04 08:26:19 +00:00
|
|
|
$volatilityOnlyStaleness = true;
|
2015-08-28 01:31:35 +00:00
|
|
|
} else {
|
|
|
|
|
$where[] = 'got from local cache';
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->set( $code, $cache );
|
2015-08-28 01:31:35 +00:00
|
|
|
$success = true;
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$success ) {
|
2023-02-03 21:36:06 +00:00
|
|
|
// Try the cluster cache, using a lock for regeneration...
|
2018-02-19 11:50:29 +00:00
|
|
|
$cacheKey = $this->clusterCache->makeKey( 'messages', $code );
|
2015-08-27 08:46:05 +00:00
|
|
|
for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
|
2023-02-04 08:26:19 +00:00
|
|
|
if ( $volatilityOnlyStaleness && $staleCache ) {
|
2023-02-03 21:36:06 +00:00
|
|
|
// While the cluster cache *might* be more up-to-date, we do not want
|
|
|
|
|
// the I/O strain of every application server fetching the key here during
|
|
|
|
|
// the volatility period. Either this thread wins the lock and regenerates
|
|
|
|
|
// the cache or the stale local cache value gets reused.
|
2015-05-20 16:51:29 +00:00
|
|
|
$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 {
|
2017-01-21 01:47:15 +00:00
|
|
|
$cache = $this->clusterCache->get( $cacheKey );
|
2015-05-20 16:51:29 +00:00
|
|
|
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 ) {
|
2023-02-03 21:36:06 +00:00
|
|
|
// Some recent message page changes might not show due to DB lag
|
2015-08-12 02:53:03 +00:00
|
|
|
$where[] = 'global cache is expired/volatile';
|
|
|
|
|
$staleCache = $cache;
|
2015-05-20 16:51:29 +00:00
|
|
|
} else {
|
|
|
|
|
$where[] = 'got from global cache';
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->set( $code, $cache );
|
2015-05-20 16:51:29 +00:00
|
|
|
$this->saveToCaches( $cache, 'local-only', $code );
|
|
|
|
|
$success = true;
|
2023-02-03 21:36:06 +00:00
|
|
|
break;
|
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
|
|
|
}
|
2008-06-01 03:27:48 +00:00
|
|
|
|
2023-02-03 21:36:06 +00:00
|
|
|
// We need to call loadFromDB(). Limit the concurrency to one thread.
|
2022-07-14 06:24:44 +00:00
|
|
|
// This prevents the site from going down when the cache expires.
|
|
|
|
|
// Note that the DB slam protection lock here is non-blocking.
|
2023-02-03 21:36:06 +00:00
|
|
|
$loadStatus = $this->loadFromDBWithMainLock( $code, $where, $mode );
|
2015-08-27 08:46:05 +00:00
|
|
|
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 ) {
|
2022-07-14 06:24:44 +00:00
|
|
|
// Use the stale cache while some other thread constructs the new one
|
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[] = 'using stale cache';
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->set( $code, $staleCache );
|
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 = true;
|
|
|
|
|
break;
|
|
|
|
|
} elseif ( $failedAttempts > 0 ) {
|
2023-02-03 21:36:06 +00:00
|
|
|
$where[] = 'failed to find cache after waiting';
|
2022-07-14 06:24:44 +00:00
|
|
|
// Already blocked once, so avoid another lock/unlock cycle.
|
|
|
|
|
// This case will typically be hit if memcached is down, or if
|
|
|
|
|
// 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
|
|
|
break;
|
2015-08-27 08:46:05 +00:00
|
|
|
} elseif ( $loadStatus === 'cantacquire' ) {
|
2022-07-14 06:24:44 +00:00
|
|
|
// Wait for the other thread to finish, then retry. Normally,
|
|
|
|
|
// the memcached get() will then yield the other thread's result.
|
2023-02-03 21:36:06 +00:00
|
|
|
$where[] = 'waiting for other thread to complete';
|
|
|
|
|
[ , $ioError ] = $this->getReentrantScopedLock( $code );
|
|
|
|
|
if ( $ioError ) {
|
|
|
|
|
$where[] = 'failed waiting';
|
|
|
|
|
// Call loadFromDB() with concurrency limited to one thread per server.
|
|
|
|
|
// It should be rare for all servers to lack even a stale local cache.
|
|
|
|
|
$success = $this->loadFromDBWithLocalLock( $code, $where, $mode );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-10-10 13:02:57 +00:00
|
|
|
} else {
|
2022-07-14 06:24:44 +00:00
|
|
|
// Disable cache; $loadStatus is 'disabled'
|
2015-08-27 08:46:05 +00:00
|
|
|
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';
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->disable = true;
|
2018-08-14 06:03:27 +00:00
|
|
|
$this->cache->set( $code, [] );
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->error( __METHOD__ . ": Failed to load $code" );
|
2022-07-14 06:24:44 +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
|
2018-06-25 09:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-19 23:19:42 +00:00
|
|
|
if ( !$this->isLanguageLoaded( $code ) ) {
|
2018-06-25 09:45:55 +00:00
|
|
|
throw new LogicException( "Process cache for '$code' should be set by now." );
|
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 );
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Loading $code... $info" );
|
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
|
2019-10-11 23:37:37 +00:00
|
|
|
* @param string[] &$where List of debug comments
|
2021-04-19 01:02:08 +00:00
|
|
|
* @param int|null $mode Use MessageCache::FOR_UPDATE to use DB_PRIMARY
|
2023-02-03 21:36:06 +00:00
|
|
|
* @return true|string One (true, "cantacquire", "disabled")
|
2015-05-20 16:51:29 +00:00
|
|
|
*/
|
2023-02-03 21:36:06 +00:00
|
|
|
private function loadFromDBWithMainLock( $code, array &$where, $mode = null ) {
|
2022-07-14 06:24:44 +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.
|
2017-05-24 12:49:53 +00:00
|
|
|
$statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' );
|
2017-01-21 01:47:15 +00:00
|
|
|
$status = $this->clusterCache->get( $statusKey );
|
2015-08-27 08:46:05 +00:00
|
|
|
if ( $status === 'error' ) {
|
|
|
|
|
$where[] = "could not load; method is still globally disabled";
|
|
|
|
|
return 'disabled';
|
2015-05-20 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
// Now let's regenerate
|
2023-02-03 21:36:06 +00:00
|
|
|
$where[] = 'loading from DB';
|
2015-05-20 16:51:29 +00:00
|
|
|
|
2022-07-14 06:24:44 +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()
|
2023-10-02 12:56:25 +00:00
|
|
|
// after this if it really needs to wait for any current thread.
|
2023-02-03 21:36:06 +00:00
|
|
|
[ $scopedLock ] = $this->getReentrantScopedLock( $code, 0 );
|
2015-08-27 08:46:05 +00:00
|
|
|
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 );
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->set( $code, $cache );
|
2015-05-20 16:51:29 +00:00
|
|
|
$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.
|
|
|
|
|
*/
|
2017-01-21 01:47:15 +00:00
|
|
|
if ( $this->srvCache instanceof EmptyBagOStuff ) {
|
|
|
|
|
$this->clusterCache->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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 21:36:06 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @param string[] &$where List of debug comments
|
|
|
|
|
* @param int|null $mode Use MessageCache::FOR_UPDATE to use DB_PRIMARY
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
private function loadFromDBWithLocalLock( $code, array &$where, $mode = null ) {
|
|
|
|
|
$success = false;
|
|
|
|
|
$where[] = 'loading from DB using local lock';
|
|
|
|
|
|
|
|
|
|
$scopedLock = $this->srvCache->getScopedLock(
|
|
|
|
|
$this->srvCache->makeKey( 'messages', $code ),
|
|
|
|
|
self::WAIT_SEC,
|
|
|
|
|
self::LOCK_TTL,
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
if ( $scopedLock ) {
|
|
|
|
|
$cache = $this->loadFromDB( $code, $mode );
|
|
|
|
|
$this->cache->set( $code, $cache );
|
|
|
|
|
$this->saveToCaches( $cache, 'local-only', $code );
|
|
|
|
|
$success = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache
|
2015-08-27 08:46:05 +00:00
|
|
|
* @return array Loaded messages for storing in caches
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private function loadFromDB( $code, $mode = null ) {
|
2022-08-03 15:14:09 +00:00
|
|
|
$dbr = wfGetDB( ( $mode === self::FOR_UPDATE ) ? DB_PRIMARY : 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
|
|
|
|
2018-10-02 17:59:28 +00:00
|
|
|
$mostused = []; // list of "<cased message key>/<code>"
|
2022-07-14 06:24:44 +00:00
|
|
|
if ( $this->adaptive && $code !== $this->contLangCode ) {
|
2021-05-13 23:32:38 +00:00
|
|
|
if ( !$this->cache->has( $this->contLangCode ) ) {
|
|
|
|
|
$this->load( $this->contLangCode );
|
2012-10-24 14:59:37 +00:00
|
|
|
}
|
2021-05-13 23:32:38 +00:00
|
|
|
$mostused = array_keys( $this->cache->get( $this->contLangCode ) );
|
2012-10-24 14:59:37 +00:00
|
|
|
foreach ( $mostused as $key => $value ) {
|
|
|
|
|
$mostused[$key] = "$value/$code";
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 17:59:28 +00:00
|
|
|
// Common conditions
|
|
|
|
|
$conds = [
|
|
|
|
|
'page_is_redirect' => 0,
|
|
|
|
|
'page_namespace' => NS_MEDIAWIKI,
|
|
|
|
|
];
|
2010-08-21 16:41:53 +00:00
|
|
|
if ( count( $mostused ) ) {
|
|
|
|
|
$conds['page_title'] = $mostused;
|
2021-05-13 23:32:38 +00:00
|
|
|
} elseif ( $code !== $this->contLangCode ) {
|
2023-11-07 17:30:50 +00:00
|
|
|
$conds[] = $dbr->expr(
|
|
|
|
|
'page_title',
|
|
|
|
|
IExpression::LIKE,
|
|
|
|
|
new LikeValue( $dbr->anyString(), '/', $code )
|
|
|
|
|
);
|
2010-08-21 16:41:53 +00:00
|
|
|
} else {
|
2022-07-14 06:24:44 +00:00
|
|
|
// Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
|
|
|
|
|
// other than language code.
|
2023-12-06 19:08:47 +00:00
|
|
|
$conds[] = $dbr->expr(
|
|
|
|
|
'page_title',
|
|
|
|
|
IExpression::NOT_LIKE,
|
|
|
|
|
new LikeValue( $dbr->anyString(), '/', $dbr->anyString() )
|
|
|
|
|
);
|
2010-08-21 16:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-02 17:59:28 +00:00
|
|
|
// Set the stubs for oversized software-defined messages in the main cache map
|
2023-09-19 11:06:53 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'page_title', 'page_latest' ] )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->where( $conds )
|
|
|
|
|
->andWhere( [ 'page_len > ' . intval( $this->maxEntrySize ) ] )
|
|
|
|
|
->caller( __METHOD__ . "($code)-big" )->fetchResultSet();
|
2010-08-07 23:43:28 +00:00
|
|
|
foreach ( $res as $row ) {
|
2018-10-02 17:59:28 +00:00
|
|
|
// Include entries/stubs for all keys in $mostused in adaptive mode
|
2022-07-14 06:24:44 +00:00
|
|
|
if ( $this->adaptive || $this->isMainCacheable( $row->page_title ) ) {
|
2018-10-02 17:59:28 +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
|
|
|
|
2023-10-02 12:56:25 +00:00
|
|
|
// RevisionStore cannot be injected as it would break the installer since
|
2020-02-29 03:49:41 +00:00
|
|
|
// it instantiates MessageCache before the DB.
|
2019-03-15 15:59:41 +00:00
|
|
|
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
|
2020-02-29 03:49:41 +00:00
|
|
|
// Set the text for small software-defined messages in the main cache map
|
2020-08-17 22:37:45 +00:00
|
|
|
$revQuery = $revisionStore->getQueryInfo( [ 'page' ] );
|
2019-08-26 19:12:30 +00:00
|
|
|
|
|
|
|
|
// T231196: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` then
|
|
|
|
|
// `revision` then `page` is somehow better than starting with `page`. Tell it not to reorder the
|
|
|
|
|
// query (and also reorder it ourselves because as generated by RevisionStore it'll have
|
|
|
|
|
// `revision` first rather than `page`).
|
|
|
|
|
$revQuery['joins']['revision'] = $revQuery['joins']['page'];
|
|
|
|
|
unset( $revQuery['joins']['page'] );
|
2021-10-06 00:08:37 +00:00
|
|
|
// It isn't actually necessary to reorder $revQuery['tables'] as Database does the right thing
|
2019-08-26 19:12:30 +00:00
|
|
|
// when join conditions are given for all joins, but Gergő is wary of relying on that so pull
|
|
|
|
|
// `page` to the start.
|
|
|
|
|
$revQuery['tables'] = array_merge(
|
|
|
|
|
[ 'page' ],
|
|
|
|
|
array_diff( $revQuery['tables'], [ 'page' ] )
|
|
|
|
|
);
|
|
|
|
|
|
2011-01-26 17:41:18 +00:00
|
|
|
$res = $dbr->select(
|
2019-03-15 15:59:41 +00:00
|
|
|
$revQuery['tables'],
|
|
|
|
|
$revQuery['fields'],
|
2019-03-22 12:26:21 +00:00
|
|
|
array_merge( $conds, [
|
2022-07-14 06:24:44 +00:00
|
|
|
'page_len <= ' . intval( $this->maxEntrySize ),
|
2019-03-22 12:26:21 +00:00
|
|
|
'page_latest = rev_id' // get the latest revision only
|
|
|
|
|
] ),
|
2017-05-08 17:57:13 +00:00
|
|
|
__METHOD__ . "($code)-small",
|
2019-08-26 19:12:30 +00:00
|
|
|
[ 'STRAIGHT_JOIN' ],
|
2019-03-15 15:59:41 +00:00
|
|
|
$revQuery['joins']
|
2011-01-26 17:41:18 +00:00
|
|
|
);
|
2022-07-14 11:46:16 +00:00
|
|
|
|
|
|
|
|
// Don't load content from uncacheable rows (T313004)
|
|
|
|
|
[ $cacheableRows, $uncacheableRows ] = $this->separateCacheableRows( $res );
|
|
|
|
|
$result = $revisionStore->newRevisionsFromBatch( $cacheableRows, [
|
2020-08-17 22:37:45 +00:00
|
|
|
'slots' => [ SlotRecord::MAIN ],
|
|
|
|
|
'content' => true
|
|
|
|
|
] );
|
|
|
|
|
$revisions = $result->isOK() ? $result->getValue() : [];
|
2019-03-15 15:59:41 +00:00
|
|
|
|
2022-07-14 11:46:16 +00:00
|
|
|
foreach ( $cacheableRows as $row ) {
|
|
|
|
|
try {
|
|
|
|
|
$rev = $revisions[$row->rev_id] ?? null;
|
|
|
|
|
$content = $rev ? $rev->getContent( SlotRecord::MAIN ) : null;
|
|
|
|
|
$text = $this->getMessageTextFromContent( $content );
|
|
|
|
|
} catch ( TimeoutException $e ) {
|
|
|
|
|
throw $e;
|
|
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
|
$text = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_string( $text ) ) {
|
|
|
|
|
$entry = '!ERROR';
|
|
|
|
|
$this->logger->error(
|
|
|
|
|
__METHOD__
|
|
|
|
|
. ": failed to load message page text for {$row->page_title} ($code)"
|
|
|
|
|
);
|
2011-09-27 00:41:24 +00:00
|
|
|
} else {
|
2022-07-14 11:46:16 +00:00
|
|
|
$entry = ' ' . $text;
|
2011-09-27 00:41:24 +00:00
|
|
|
}
|
2022-07-14 11:46:16 +00:00
|
|
|
$cache[$row->page_title] = $entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $uncacheableRows as $row ) {
|
2023-10-02 12:56:25 +00:00
|
|
|
// T193271: The cache object gets too big and slow to generate.
|
|
|
|
|
// At least include revision ID, so that page changes are reflected in the hash.
|
2022-07-14 11:46:16 +00:00
|
|
|
$cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
|
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
|
|
|
|
2022-07-14 06:24:44 +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 ) );
|
2019-08-27 15:35:49 +00:00
|
|
|
$cache['EXPIRY'] = wfTimestamp( TS_MW, time() + self::WAN_TTL );
|
2018-10-03 19:38:46 +00:00
|
|
|
unset( $cache['EXCESSIVE'] ); // only needed for hash
|
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
|
|
|
|
MessageCache: Replace internal loadedLanguages array with special cache key
Before c962b480568ea, the 'loadedLanguages' array was used to track
which languages were loaded and in the cache, with 'cache' being a
simple array. In that commit, the 'cache' array also started being used
for incomplete datasets, which didn't affect 'loadedLanguages'.
Then in 97e86d934b348, the 'loadedLanguages' array was removed in favour
of checking keys on 'cache' directly, and 'cache' was converted to
MapCacheLRU.
This led to problem where partially loaded data was mistaken for being
full datasets (fatal error, T208897). This was fixed in a5c984cc5978f86,
by bringing back the 'loadedLanguages' array, which fixed the issue from
the POV of partially loaded data.
However, this then exposed a new problem. The 'cache' data can be evicted
by MapCacheLRU, whereas 'loadedLanguages' is not aware of that. Thus it
claims languages are loaded that sometimes aren't. (This only affects web
requests where more than 5 language codes are involved, per MapCacheLRU.)
Fix this by re-removing the 'loadedLanguages' array, this time
strengthening the 'cache' key check to not just check that the root key
exists, but that it is in fact holding the full dataset as generated by
MessageCache::load(). The 'VERSION' key appears to be a good proxy for
that.
Bug: T230690
Change-Id: I1162a3857376aa37e5894ae3c8be84a2295782a3
2019-09-26 00:21:39 +00:00
|
|
|
/**
|
|
|
|
|
* Whether the language was loaded and its data is still in the process cache.
|
|
|
|
|
*
|
|
|
|
|
* @param string $lang
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function isLanguageLoaded( $lang ) {
|
|
|
|
|
// It is important that this only returns true if the cache was fully
|
|
|
|
|
// populated by load(), so that callers can assume all cache keys exist.
|
2022-01-09 17:30:20 +00:00
|
|
|
// It is possible for $this->cache to be only partially populated through
|
MessageCache: Replace internal loadedLanguages array with special cache key
Before c962b480568ea, the 'loadedLanguages' array was used to track
which languages were loaded and in the cache, with 'cache' being a
simple array. In that commit, the 'cache' array also started being used
for incomplete datasets, which didn't affect 'loadedLanguages'.
Then in 97e86d934b348, the 'loadedLanguages' array was removed in favour
of checking keys on 'cache' directly, and 'cache' was converted to
MapCacheLRU.
This led to problem where partially loaded data was mistaken for being
full datasets (fatal error, T208897). This was fixed in a5c984cc5978f86,
by bringing back the 'loadedLanguages' array, which fixed the issue from
the POV of partially loaded data.
However, this then exposed a new problem. The 'cache' data can be evicted
by MapCacheLRU, whereas 'loadedLanguages' is not aware of that. Thus it
claims languages are loaded that sometimes aren't. (This only affects web
requests where more than 5 language codes are involved, per MapCacheLRU.)
Fix this by re-removing the 'loadedLanguages' array, this time
strengthening the 'cache' key check to not just check that the root key
exists, but that it is in fact holding the full dataset as generated by
MessageCache::load(). The 'VERSION' key appears to be a good proxy for
that.
Bug: T230690
Change-Id: I1162a3857376aa37e5894ae3c8be84a2295782a3
2019-09-26 00:21:39 +00:00
|
|
|
// methods like MessageCache::replace(), which must not make this method
|
|
|
|
|
// return true (T208897). And this method must cease to return true
|
|
|
|
|
// if the language was evicted by MapCacheLRU (T230690).
|
|
|
|
|
return $this->cache->hasField( $lang, 'VERSION' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-27 23:41:33 +00:00
|
|
|
/**
|
2021-05-11 06:05:11 +00:00
|
|
|
* Can the given DB key be added to the main cache blob? To reduce the
|
2023-10-02 12:56:25 +00:00
|
|
|
* abuse impact of the MediaWiki namespace by {{int:}} and CentralNotice,
|
2021-05-11 06:05:11 +00:00
|
|
|
* this is only true if the page overrides a predefined message.
|
|
|
|
|
*
|
2019-07-19 23:51:01 +00:00
|
|
|
* @param string $name Message name (possibly with /code suffix)
|
2021-05-11 06:05:11 +00:00
|
|
|
* @param string|null $code The language code. If this is null, message
|
|
|
|
|
* presence will be bulk loaded for the content language. Otherwise,
|
|
|
|
|
* presence will be detected by loading the specified message.
|
2018-10-27 23:41:33 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-05-11 06:05:11 +00:00
|
|
|
private function isMainCacheable( $name, $code = null ) {
|
2023-10-02 12:56:25 +00:00
|
|
|
// Convert the first letter to lowercase, and strip /code suffix
|
2019-07-19 23:51:01 +00:00
|
|
|
$name = $this->contLang->lcfirst( $name );
|
2018-10-27 23:41:33 +00:00
|
|
|
// Include common conversion table pages. This also avoids problems with
|
|
|
|
|
// Installer::parse() bailing out due to disallowed DB queries (T207979).
|
2021-05-11 06:05:11 +00:00
|
|
|
if ( strpos( $name, 'conversiontable/' ) === 0 ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$msg = preg_replace( '/\/[a-z0-9-]{2,}$/', '', $name );
|
|
|
|
|
|
|
|
|
|
if ( $code === null ) {
|
|
|
|
|
// Bulk load
|
|
|
|
|
if ( $this->systemMessageNames === null ) {
|
2021-06-11 02:52:06 +00:00
|
|
|
$this->systemMessageNames = array_fill_keys(
|
|
|
|
|
$this->localisationCache->getSubitemList( $this->contLangCode, 'messages' ),
|
|
|
|
|
true );
|
2021-05-11 06:05:11 +00:00
|
|
|
}
|
|
|
|
|
return isset( $this->systemMessageNames[$msg] );
|
|
|
|
|
} else {
|
|
|
|
|
// Use individual subitem
|
|
|
|
|
return $this->localisationCache->getSubitem( $code, 'messages', $msg ) !== null;
|
|
|
|
|
}
|
2018-10-27 23:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 11:46:16 +00:00
|
|
|
/**
|
2022-07-15 04:32:46 +00:00
|
|
|
* Separate cacheable from uncacheable rows in a page/revsion query result.
|
2022-07-14 11:46:16 +00:00
|
|
|
*
|
|
|
|
|
* @param IResultWrapper $res
|
2022-07-15 04:32:46 +00:00
|
|
|
* @return array{0:IResultWrapper|stdClass[],1:stdClass[]} An array with the cacheable
|
|
|
|
|
* rows in the first element and the uncacheable rows in the second.
|
2022-07-14 11:46:16 +00:00
|
|
|
*/
|
|
|
|
|
private function separateCacheableRows( $res ) {
|
|
|
|
|
if ( $this->adaptive ) {
|
|
|
|
|
// Include entries/stubs for all keys in $mostused in adaptive mode
|
|
|
|
|
return [ $res, [] ];
|
|
|
|
|
}
|
|
|
|
|
$cacheableRows = [];
|
|
|
|
|
$uncacheableRows = [];
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( $this->isMainCacheable( $row->page_title ) ) {
|
|
|
|
|
$cacheableRows[] = $row;
|
|
|
|
|
} else {
|
|
|
|
|
$uncacheableRows[] = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [ $cacheableRows, $uncacheableRows ];
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
/**
|
|
|
|
|
* Updates cache as necessary when message page is changed
|
|
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $title Message cache key with the initial uppercase letter
|
2020-11-30 16:58:58 +00:00
|
|
|
* @param string|false $text New contents of the page (false if deleted)
|
2008-07-05 15:39:10 +00:00
|
|
|
*/
|
|
|
|
|
public function replace( $title, $text ) {
|
2022-07-14 06:24:44 +00:00
|
|
|
if ( $this->disable ) {
|
2010-08-08 00:28:17 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2022-10-21 04:32:38 +00:00
|
|
|
[ $msg, $code ] = $this->figureMessage( $title );
|
2021-05-13 23:32:38 +00:00
|
|
|
if ( strpos( $title, '/' ) !== false && $code === $this->contLangCode ) {
|
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
|
|
|
|
2016-10-28 05:53:51 +00:00
|
|
|
// (a) Update the process cache with the new message text
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( $text === false ) {
|
2016-10-28 05:53:51 +00:00
|
|
|
// Page deleted
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->setField( $code, $title, '!NONEXISTENT' );
|
2010-08-08 00:28:17 +00:00
|
|
|
} else {
|
2023-10-02 12:56:25 +00:00
|
|
|
// Ignore $wgMaxMsgCacheEntrySize so the process cache is up-to-date
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->setField( $code, $title, ' ' . $text );
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
2009-07-31 07:12:25 +00:00
|
|
|
|
2016-10-28 05:53:51 +00:00
|
|
|
// (b) Update the shared caches in a deferred update with a fresh DB snapshot
|
2018-10-04 20:14:32 +00:00
|
|
|
DeferredUpdates::addUpdate(
|
|
|
|
|
new MessageCacheUpdate( $code, $title, $msg ),
|
2016-10-28 05:53:51 +00:00
|
|
|
DeferredUpdates::PRESEND
|
|
|
|
|
);
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 20:14:32 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @param array[] $replacements List of (title, message key) pairs
|
|
|
|
|
*/
|
2022-07-15 04:32:46 +00:00
|
|
|
public function refreshAndReplaceInternal( string $code, array $replacements ) {
|
2018-10-04 20:14:32 +00:00
|
|
|
// Allow one caller at a time to avoid race conditions
|
2023-02-03 21:36:06 +00:00
|
|
|
[ $scopedLock ] = $this->getReentrantScopedLock( $code );
|
2018-10-04 20:14:32 +00:00
|
|
|
if ( !$scopedLock ) {
|
2022-10-21 04:32:38 +00:00
|
|
|
foreach ( $replacements as [ $title ] ) {
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->error(
|
2018-10-04 20:14:32 +00:00
|
|
|
__METHOD__ . ': could not acquire lock to update {title} ({code})',
|
|
|
|
|
[ 'title' => $title, 'code' => $code ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 16:56:58 +00:00
|
|
|
// Load the existing cache to update it in the local DC cache.
|
|
|
|
|
// The other DCs will see a hash mismatch.
|
|
|
|
|
if ( $this->load( $code, self::FOR_UPDATE ) ) {
|
|
|
|
|
$cache = $this->cache->get( $code );
|
|
|
|
|
} else {
|
|
|
|
|
// Err? Fall back to loading from the database.
|
|
|
|
|
$cache = $this->loadFromDB( $code, self::FOR_UPDATE );
|
|
|
|
|
}
|
2018-10-04 20:14:32 +00:00
|
|
|
// Check if individual cache keys should exist and update cache accordingly
|
|
|
|
|
$newTextByTitle = []; // map of (title => content)
|
2018-09-26 16:56:58 +00:00
|
|
|
$newBigTitles = []; // map of (title => latest revision ID), like EXCESSIVE in loadFromDB()
|
2022-06-24 17:14:53 +00:00
|
|
|
// Can not inject the WikiPageFactory as it would break the installer since
|
|
|
|
|
// it instantiates MessageCache before the DB.
|
|
|
|
|
$wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
|
2022-10-21 04:32:38 +00:00
|
|
|
foreach ( $replacements as [ $title ] ) {
|
2022-06-24 17:14:53 +00:00
|
|
|
$page = $wikiPageFactory->newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
|
2024-01-23 14:01:06 +00:00
|
|
|
$page->loadPageData( IDBAccessObject::READ_LATEST );
|
2018-10-04 20:14:32 +00:00
|
|
|
$text = $this->getMessageTextFromContent( $page->getContent() );
|
|
|
|
|
// Remember the text for the blob store update later on
|
2022-03-09 19:15:36 +00:00
|
|
|
$newTextByTitle[$title] = $text ?? '';
|
2018-10-04 20:14:32 +00:00
|
|
|
// Note that if $text is false, then $cache should have a !NONEXISTANT entry
|
2018-09-26 16:56:58 +00:00
|
|
|
if ( !is_string( $text ) ) {
|
|
|
|
|
$cache[$title] = '!NONEXISTENT';
|
2022-07-14 06:24:44 +00:00
|
|
|
} elseif ( strlen( $text ) > $this->maxEntrySize ) {
|
2018-09-26 16:56:58 +00:00
|
|
|
$cache[$title] = '!TOO BIG';
|
|
|
|
|
$newBigTitles[$title] = $page->getLatest();
|
|
|
|
|
} else {
|
|
|
|
|
$cache[$title] = ' ' . $text;
|
2018-10-04 20:14:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-26 16:56:58 +00:00
|
|
|
// Update HASH for the new key. Incorporates various administrative keys,
|
|
|
|
|
// including the old HASH (and thereby the EXCESSIVE value from loadFromDB()
|
|
|
|
|
// and previous replace() calls), but that doesn't really matter since we
|
|
|
|
|
// only ever compare it for equality with a copy saved by saveToCaches().
|
|
|
|
|
$cache['HASH'] = md5( serialize( $cache + [ 'EXCESSIVE' => $newBigTitles ] ) );
|
|
|
|
|
// Update the too-big WAN cache entries now that we have the new HASH
|
|
|
|
|
foreach ( $newBigTitles as $title => $id ) {
|
|
|
|
|
// Match logic of loadCachedMessagePageEntry()
|
|
|
|
|
$this->wanCache->set(
|
|
|
|
|
$this->bigMessageCacheKey( $cache['HASH'], $title ),
|
|
|
|
|
' ' . $newTextByTitle[$title],
|
2019-08-27 15:35:49 +00:00
|
|
|
self::WAN_TTL
|
2018-09-26 16:56:58 +00:00
|
|
|
);
|
|
|
|
|
}
|
2018-10-04 20:14:32 +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
|
|
|
|
|
$cache['LATEST'] = time();
|
|
|
|
|
// Update the process cache
|
|
|
|
|
$this->cache->set( $code, $cache );
|
|
|
|
|
// Pre-emptively update the local datacenter cache so things like edit filter and
|
2021-03-19 18:36:44 +00:00
|
|
|
// prevented changes are reflected immediately; these often use MediaWiki: pages.
|
2018-10-04 20:14:32 +00:00
|
|
|
// The datacenter handling replace() calls should be the same one handling edits
|
|
|
|
|
// as they require HTTP POST.
|
|
|
|
|
$this->saveToCaches( $cache, 'all', $code );
|
|
|
|
|
// Release the lock now that the cache is saved
|
|
|
|
|
ScopedCallback::consume( $scopedLock );
|
|
|
|
|
|
|
|
|
|
// Relay the purge. Touching this check key expires cache contents
|
|
|
|
|
// and local cache (APC) validation hash across all datacenters.
|
|
|
|
|
$this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
|
|
|
|
|
|
|
|
|
|
// Purge the messages in the message blob store and fire any hook handlers
|
2019-05-07 01:07:49 +00:00
|
|
|
$blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore();
|
2022-10-21 04:32:38 +00:00
|
|
|
foreach ( $replacements as [ $title, $msg ] ) {
|
2018-10-04 20:14:32 +00:00
|
|
|
$blobStore->updateMessage( $this->contLang->lcfirst( $msg ) );
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner->onMessageCacheReplace( $title, $newTextByTitle[$title] );
|
2018-10-04 20:14:32 +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
|
|
|
/**
|
2023-10-02 12:56:25 +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
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private function isCacheExpired( $cache ) {
|
2023-06-15 13:14:40 +00:00
|
|
|
return !isset( $cache['VERSION'] ) ||
|
|
|
|
|
!isset( $cache['EXPIRY'] ) ||
|
|
|
|
|
$cache['VERSION'] !== MSG_CACHE_VERSION ||
|
|
|
|
|
$cache['EXPIRY'] <= wfTimestampNow();
|
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-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.
|
2020-11-30 16:58:58 +00:00
|
|
|
* @param string|false $code Language code (default: false)
|
2013-04-08 12:09:48 +00:00
|
|
|
* @return bool
|
2008-07-05 15:39:10 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private 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' ) {
|
2017-05-24 12:49:53 +00:00
|
|
|
$cacheKey = $this->clusterCache->makeKey( 'messages', $code );
|
2017-01-21 01:47:15 +00:00
|
|
|
$success = $this->clusterCache->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
|
|
|
/**
|
2023-02-03 21:36:06 +00:00
|
|
|
* Get the md5 used to validate the local server 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
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private function getValidationHash( $code ) {
|
2015-05-20 16:51:29 +00:00
|
|
|
$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,
|
2018-02-19 11:50:29 +00:00
|
|
|
[ $this->getCheckKey( $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.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-02-03 21:36:06 +00:00
|
|
|
* Set the md5 used to validate the local server cache
|
2015-05-20 16:51:29 +00:00
|
|
|
*
|
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
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private 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'],
|
2017-10-06 22:17:58 +00:00
|
|
|
'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
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $code Which language to load messages for
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $timeout Wait timeout in seconds
|
2023-02-03 21:36:06 +00:00
|
|
|
* @return array (ScopedCallback or null, whether locking failed due to an I/O error)
|
|
|
|
|
* @phan-return array{0:ScopedCallback|null,1:bool}
|
2015-08-21 21:46:44 +00:00
|
|
|
*/
|
2023-02-03 21:36:06 +00:00
|
|
|
private function getReentrantScopedLock( $code, $timeout = self::WAIT_SEC ) {
|
|
|
|
|
$key = $this->clusterCache->makeKey( 'messages', $code );
|
|
|
|
|
|
|
|
|
|
$watchPoint = $this->clusterCache->watchErrors();
|
|
|
|
|
$scopedLock = $this->clusterCache->getScopedLock(
|
|
|
|
|
$key,
|
|
|
|
|
$timeout,
|
|
|
|
|
self::LOCK_TTL,
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$error = ( !$scopedLock && $this->clusterCache->getLastError( $watchPoint ) );
|
|
|
|
|
|
|
|
|
|
return [ $scopedLock, $error ];
|
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.
|
2020-11-30 16:58:58 +00:00
|
|
|
* @param bool|string|Language $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
|
2013-01-16 07:28:54 +00:00
|
|
|
*
|
2020-11-30 16:58:58 +00:00
|
|
|
* @return string|false False if the message doesn't exist, otherwise the
|
2013-11-17 20:49:59 +00:00
|
|
|
* message (which can be empty)
|
2007-01-05 18:08:29 +00:00
|
|
|
*/
|
2019-12-13 10:38:26 +00:00
|
|
|
public function get( $key, $useDB = true, $langcode = true ) {
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( is_int( $key ) ) {
|
2022-07-15 04:32:46 +00:00
|
|
|
// Fix numerical strings that somehow become ints on their way here
|
2013-01-16 07:28:54 +00:00
|
|
|
$key = (string)$key;
|
|
|
|
|
} elseif ( !is_string( $key ) ) {
|
2022-07-15 04:32:46 +00:00
|
|
|
throw new TypeError( 'Message key must be a string' );
|
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
|
|
|
}
|
|
|
|
|
|
2022-09-15 19:32:54 +00:00
|
|
|
$language = $this->getLanguageObject( $langcode );
|
2023-01-28 11:31:48 +00:00
|
|
|
|
2013-01-16 07:28:54 +00:00
|
|
|
// Normalise title-case input (with some inlining)
|
2017-07-23 01:24:09 +00:00
|
|
|
$lckey = self::normalizeKey( $key );
|
2013-11-28 09:43:00 +00:00
|
|
|
|
2023-01-28 11:31:48 +00:00
|
|
|
// Initialize the overrides here to prevent calling the hook too early.
|
|
|
|
|
if ( $this->messageKeyOverrides === null ) {
|
|
|
|
|
$this->messageKeyOverrides = [];
|
|
|
|
|
$this->hookRunner->onMessageCacheFetchOverrides( $this->messageKeyOverrides );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $this->messageKeyOverrides[$lckey] ) ) {
|
|
|
|
|
$override = $this->messageKeyOverrides[$lckey];
|
|
|
|
|
|
|
|
|
|
// Strings are deliberately interpreted as message keys,
|
|
|
|
|
// to prevent ambiguity between message keys and functions.
|
|
|
|
|
if ( is_string( $override ) ) {
|
|
|
|
|
$lckey = $override;
|
|
|
|
|
} else {
|
|
|
|
|
$lckey = $override( $lckey, $this, $language, $useDB );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner->onMessageCache__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
|
2013-11-17 20:49:59 +00:00
|
|
|
$message = $this->getMessageFromFallbackChain(
|
2023-01-28 11:31:48 +00:00
|
|
|
$language,
|
2013-11-17 20:49:59 +00:00
|
|
|
$lckey,
|
2022-07-14 06:24:44 +00:00
|
|
|
!$this->disable && $useDB
|
2013-11-17 20:49:59 +00:00
|
|
|
);
|
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.
|
2022-07-15 04:32:46 +00:00
|
|
|
if ( count( $parts ) === 2 && $parts[1] !== '' ) {
|
2022-12-05 20:37:13 +00:00
|
|
|
$message = $this->localisationCache->getSubitem( $parts[1], 'messages', $parts[0] ) ?? false;
|
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
|
|
|
[
|
2022-07-14 06:24:44 +00:00
|
|
|
// Fix for trailing whitespace, removed by textarea
|
2013-01-16 07:28:54 +00:00
|
|
|
' ',
|
2022-07-14 06:24:44 +00:00
|
|
|
// Fix for NBSP, converted to space by firefox
|
2013-01-16 07:28:54 +00:00
|
|
|
' ',
|
|
|
|
|
' ',
|
2016-02-16 14:02:03 +00:00
|
|
|
'­'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-01-16 07:28:54 +00:00
|
|
|
' ',
|
2017-10-07 00:26:23 +00:00
|
|
|
"\u{00A0}",
|
|
|
|
|
"\u{00A0}",
|
|
|
|
|
"\u{00AD}"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2013-01-16 07:28:54 +00:00
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-15 19:32:54 +00:00
|
|
|
* Return a Language object from $langcode
|
|
|
|
|
*
|
|
|
|
|
* @param Language|string|bool $langcode Either:
|
|
|
|
|
* - a Language object
|
|
|
|
|
* - code of the language to get the message for, if it is
|
|
|
|
|
* a valid code create a language for that language, if
|
|
|
|
|
* it is a string but not a valid code then make a basic
|
|
|
|
|
* language object
|
|
|
|
|
* - a boolean: if it's false then use the global object for
|
|
|
|
|
* the current user's language (as a fallback for the old parameter
|
|
|
|
|
* functionality), or if it is true then use global object
|
|
|
|
|
* for the wiki's content language.
|
|
|
|
|
* @return Language|StubUserLang
|
|
|
|
|
*/
|
|
|
|
|
private function getLanguageObject( $langcode ) {
|
|
|
|
|
# Identify which language to get or create a language object for.
|
|
|
|
|
# Using is_object here due to Stub objects.
|
|
|
|
|
if ( is_object( $langcode ) ) {
|
|
|
|
|
# Great, we already have the object (hopefully)!
|
|
|
|
|
return $langcode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $langcode === true || $langcode === $this->contLangCode ) {
|
|
|
|
|
# $langcode is the language code of the wikis content language object.
|
|
|
|
|
# or it is a boolean and value is true
|
|
|
|
|
return $this->contLang;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgLang;
|
|
|
|
|
if ( $langcode === false || $langcode === $wgLang->getCode() ) {
|
|
|
|
|
# $langcode is the language code of user language object.
|
|
|
|
|
# or it was a boolean and value is false
|
|
|
|
|
return $wgLang;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validCodes = array_keys( $this->languageNameUtils->getLanguageNames() );
|
|
|
|
|
if ( in_array( $langcode, $validCodes ) ) {
|
|
|
|
|
# $langcode corresponds to a valid language.
|
|
|
|
|
return $this->langFactory->getLanguage( $langcode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# $langcode is a string, but not a valid language code; use content language.
|
|
|
|
|
$this->logger->debug( 'Invalid language code passed to' . __METHOD__ . ', falling back to content language.' );
|
|
|
|
|
return $this->contLang;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
2017-10-05 17:27:08 +00:00
|
|
|
* @param Language|StubObject $lang Preferred language
|
2015-11-17 17:51:37 +00:00
|
|
|
* @param string $lckey Lowercase key for the message (as for localisation cache)
|
|
|
|
|
* @param bool $useDB Whether to include messages from the wiki database
|
2020-11-30 16:58:58 +00:00
|
|
|
* @return string|false The message, or false if not found
|
2013-01-16 07:28:54 +00:00
|
|
|
*/
|
2022-07-14 06:24:44 +00:00
|
|
|
private function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
$alreadyTried = [];
|
2013-01-16 07:28:54 +00:00
|
|
|
|
2016-10-28 05:53:51 +00:00
|
|
|
// First try the requested language.
|
2016-04-07 09:56:06 +00:00
|
|
|
$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.
|
2018-07-29 12:24:54 +00:00
|
|
|
$message = $this->getMessageForLang( $this->contLang, $lckey, $useDB, $alreadyTried );
|
2016-04-07 09:56:06 +00:00
|
|
|
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
|
2017-10-05 17:27:08 +00:00
|
|
|
* @param Language|StubObject $lang Preferred language
|
2016-04-07 09:56:06 +00:00
|
|
|
* @param string $lckey Lowercase key for the message (as for localisation cache)
|
|
|
|
|
* @param bool $useDB Whether to include messages from the wiki database
|
2019-11-23 22:28:57 +00:00
|
|
|
* @param bool[] &$alreadyTried Contains true for each language that has been tried already
|
2020-11-30 16:58:58 +00:00
|
|
|
* @return string|false The message, or false if not found
|
2016-04-07 09:56:06 +00:00
|
|
|
*/
|
|
|
|
|
private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
|
|
|
|
|
$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 ) {
|
2018-07-29 12:24:54 +00:00
|
|
|
$uckey = $this->contLang->ucfirst( $lckey );
|
2016-04-07 09:56:06 +00:00
|
|
|
|
2018-02-19 11:21:24 +00:00
|
|
|
if ( !isset( $alreadyTried[$langcode] ) ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
$message = $this->getMsgFromNamespace(
|
|
|
|
|
$this->getMessagePageName( $langcode, $uckey ),
|
|
|
|
|
$langcode
|
|
|
|
|
);
|
2013-01-16 07:28:54 +00:00
|
|
|
if ( $message !== false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2018-02-19 11:21:24 +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
|
|
|
}
|
|
|
|
|
|
2023-05-30 17:00:58 +00:00
|
|
|
// Return a special value handled in Message::format() to display the message key
|
|
|
|
|
// (and fallback keys) and the parameters passed to the message.
|
|
|
|
|
// TODO: Move to a better place.
|
|
|
|
|
if ( $langcode === 'qqx' ) {
|
|
|
|
|
return '($*)';
|
2023-07-12 15:44:30 +00:00
|
|
|
} elseif (
|
|
|
|
|
$langcode === 'x-xss' &&
|
|
|
|
|
$this->useXssLanguage &&
|
|
|
|
|
!in_array( $lckey, $this->rawHtmlMessages, true )
|
|
|
|
|
) {
|
|
|
|
|
$xssViaInnerHtml = "<script>alert('$lckey')</script>";
|
|
|
|
|
$xssViaAttribute = '">' . $xssViaInnerHtml . '<x y="';
|
2023-09-22 05:49:04 +00:00
|
|
|
return $xssViaInnerHtml . $xssViaAttribute . '($*)';
|
2023-05-30 17:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check the localisation cache
|
|
|
|
|
[ $defaultMessage, $messageSource ] =
|
|
|
|
|
$this->localisationCache->getSubitemWithSource( $langcode, 'messages', $lckey );
|
|
|
|
|
if ( $messageSource === $langcode ) {
|
|
|
|
|
return $defaultMessage;
|
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 ) {
|
2020-01-03 23:03:14 +00:00
|
|
|
$fallbackChain = $this->languageFallback->getAll( $langcode );
|
2016-04-07 09:56:06 +00:00
|
|
|
|
|
|
|
|
foreach ( $fallbackChain as $code ) {
|
2018-02-19 11:21:24 +00:00
|
|
|
if ( isset( $alreadyTried[$code] ) ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
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(
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable uckey is set when used
|
2016-10-15 02:11:51 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
2018-02-19 11:21:24 +00:00
|
|
|
$alreadyTried[$code] = true;
|
2023-05-30 17:00:58 +00:00
|
|
|
|
|
|
|
|
// Reached the source language of the default message. Don't look for DB overrides
|
|
|
|
|
// further back in the fallback chain. (T229992)
|
|
|
|
|
if ( $code === $messageSource ) {
|
|
|
|
|
return $defaultMessage;
|
|
|
|
|
}
|
2013-01-16 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 17:00:58 +00:00
|
|
|
return $defaultMessage ?? 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 ) {
|
2021-05-13 23:32:38 +00:00
|
|
|
if ( $langcode === $this->contLangCode ) {
|
2016-04-07 09:56:06 +00:00
|
|
|
// 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.
|
|
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $title Message cache key with the initial uppercase letter
|
2017-04-06 06:16:16 +00:00
|
|
|
* @param string $code Code denoting the language to try
|
2020-11-30 16:58:58 +00:00
|
|
|
* @return string|false 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 ) {
|
2018-06-25 12:15:27 +00:00
|
|
|
// Load all MediaWiki page definitions into cache. Note that individual keys
|
2023-10-02 12:56:25 +00:00
|
|
|
// already loaded into the cache during this request remain in the cache, which
|
2018-06-25 12:15:27 +00:00
|
|
|
// includes the value of hook-defined messages.
|
2010-08-05 18:19:34 +00:00
|
|
|
$this->load( $code );
|
2016-10-28 05:53:51 +00:00
|
|
|
|
2018-06-25 12:15:27 +00:00
|
|
|
$entry = $this->cache->getField( $code, $title );
|
2018-10-02 17:59:28 +00:00
|
|
|
|
2018-06-25 12:15:27 +00:00
|
|
|
if ( $entry !== null ) {
|
2018-10-02 17:59:28 +00:00
|
|
|
// Message page exists as an override of a software messages
|
2010-08-08 00:28:17 +00:00
|
|
|
if ( substr( $entry, 0, 1 ) === ' ' ) {
|
2019-03-15 15:59:41 +00:00
|
|
|
// The message exists and is not '!TOO BIG' or '!ERROR'
|
2013-05-17 12:10:31 +00:00
|
|
|
return (string)substr( $entry, 1 );
|
2010-08-08 00:28:17 +00:00
|
|
|
} elseif ( $entry === '!NONEXISTENT' ) {
|
2018-10-02 17:59:28 +00:00
|
|
|
// The text might be '-' or missing due to some data loss
|
2010-08-08 00:28:17 +00:00
|
|
|
return false;
|
2010-12-10 13:18:11 +00:00
|
|
|
}
|
2018-10-02 17:59:28 +00:00
|
|
|
// Load the message page, utilizing the individual message cache.
|
|
|
|
|
// If the page does not exist, there will be no hook handler fallbacks.
|
|
|
|
|
$entry = $this->loadCachedMessagePageEntry(
|
|
|
|
|
$title,
|
|
|
|
|
$code,
|
|
|
|
|
$this->cache->getField( $code, 'HASH' )
|
|
|
|
|
);
|
2016-11-29 03:38:57 +00:00
|
|
|
} else {
|
2018-10-15 15:08:24 +00:00
|
|
|
// Message page either does not exist or does not override a software message
|
2021-05-11 06:05:11 +00:00
|
|
|
if ( !$this->isMainCacheable( $title, $code ) ) {
|
2018-10-15 15:08:24 +00:00
|
|
|
// Message page does not override any software-defined message. A custom
|
|
|
|
|
// message might be defined to have content or settings specific to the wiki.
|
|
|
|
|
// Load the message page, utilizing the individual message cache as needed.
|
|
|
|
|
$entry = $this->loadCachedMessagePageEntry(
|
|
|
|
|
$title,
|
|
|
|
|
$code,
|
|
|
|
|
$this->cache->getField( $code, 'HASH' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( $entry === null || substr( $entry, 0, 1 ) !== ' ' ) {
|
2018-10-02 17:59:28 +00:00
|
|
|
// Message does not have a MediaWiki page definition; try hook handlers
|
|
|
|
|
$message = false;
|
2022-03-16 23:34:23 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner->onMessagesPreLoad( $title, $message, $code );
|
2018-10-02 17:59:28 +00:00
|
|
|
if ( $message !== false ) {
|
|
|
|
|
$this->cache->setField( $code, $title, ' ' . $message );
|
|
|
|
|
} else {
|
|
|
|
|
$this->cache->setField( $code, $title, '!NONEXISTENT' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2016-11-29 03:38:57 +00:00
|
|
|
}
|
2016-11-29 01:06:00 +00:00
|
|
|
|
2018-06-25 12:15:27 +00:00
|
|
|
if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
|
2018-10-02 17:59:28 +00:00
|
|
|
if ( $this->cacheVolatile[$code] ) {
|
|
|
|
|
// Make sure that individual keys respect the WAN cache holdoff period too
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->debug(
|
2018-10-02 17:59:28 +00:00
|
|
|
__METHOD__ . ': loading volatile key \'{titleKey}\'',
|
|
|
|
|
[ 'titleKey' => $title, 'code' => $code ] );
|
|
|
|
|
} else {
|
|
|
|
|
$this->cache->setField( $code, $title, $entry );
|
|
|
|
|
}
|
2018-06-25 12:15:27 +00:00
|
|
|
// The message exists, so make sure a string is returned
|
|
|
|
|
return (string)substr( $entry, 1 );
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-25 12:15:27 +00:00
|
|
|
$this->cache->setField( $code, $title, '!NONEXISTENT' );
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $dbKey
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @param string $hash
|
|
|
|
|
* @return string Either " <MESSAGE>" or "!NONEXISTANT"
|
|
|
|
|
*/
|
|
|
|
|
private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
|
2018-09-30 15:06:53 +00:00
|
|
|
$fname = __METHOD__;
|
2018-06-25 12:16:15 +00:00
|
|
|
return $this->srvCache->getWithSetCallback(
|
|
|
|
|
$this->srvCache->makeKey( 'messages-big', $hash, $dbKey ),
|
2019-08-27 15:51:54 +00:00
|
|
|
BagOStuff::TTL_HOUR,
|
2018-09-30 15:06:53 +00:00
|
|
|
function () use ( $code, $dbKey, $hash, $fname ) {
|
2018-06-25 12:16:15 +00:00
|
|
|
return $this->wanCache->getWithSetCallback(
|
|
|
|
|
$this->bigMessageCacheKey( $hash, $dbKey ),
|
2019-08-27 15:35:49 +00:00
|
|
|
self::WAN_TTL,
|
2018-09-30 15:06:53 +00:00
|
|
|
function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code, $fname ) {
|
2018-06-25 12:16:15 +00:00
|
|
|
// Try loading the message from the database
|
2020-02-29 03:49:41 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
|
2018-06-25 12:16:15 +00:00
|
|
|
// Use newKnownCurrent() to avoid querying revision/user tables
|
|
|
|
|
$title = Title::makeTitle( NS_MEDIAWIKI, $dbKey );
|
2020-02-29 03:49:41 +00:00
|
|
|
// Injecting RevisionStore breaks installer since it
|
|
|
|
|
// instantiates MessageCache before DB.
|
|
|
|
|
$revision = MediaWikiServices::getInstance()
|
|
|
|
|
->getRevisionLookup()
|
|
|
|
|
->getKnownCurrentRevision( $title );
|
2018-06-25 12:16:15 +00:00
|
|
|
if ( !$revision ) {
|
|
|
|
|
// The wiki doesn't have a local override page. Cache absence with normal TTL.
|
|
|
|
|
// When overrides are created, self::replace() takes care of the cache.
|
|
|
|
|
return '!NONEXISTENT';
|
|
|
|
|
}
|
2020-02-29 03:49:41 +00:00
|
|
|
$content = $revision->getContent( SlotRecord::MAIN );
|
2018-06-25 12:16:15 +00:00
|
|
|
if ( $content ) {
|
|
|
|
|
$message = $this->getMessageTextFromContent( $content );
|
|
|
|
|
} else {
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->warning(
|
2018-09-30 15:06:53 +00:00
|
|
|
$fname . ': failed to load page text for \'{titleKey}\'',
|
2018-06-25 12:16:15 +00:00
|
|
|
[ 'titleKey' => $dbKey, 'code' => $code ]
|
|
|
|
|
);
|
|
|
|
|
$message = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_string( $message ) ) {
|
|
|
|
|
// Revision failed to load Content, or Content is incompatible with wikitext.
|
|
|
|
|
// Possibly a temporary loading failure.
|
|
|
|
|
$ttl = 5;
|
|
|
|
|
|
|
|
|
|
return '!NONEXISTENT';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ' ' . $message;
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-06-25 12:15:27 +00:00
|
|
|
}
|
|
|
|
|
);
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
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
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param Language|null $language
|
2021-05-12 01:20:12 +00:00
|
|
|
* @param PageReference|null $page
|
2011-04-18 12:43:53 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-05-12 01:20:12 +00:00
|
|
|
public function transform( $message, $interface = false, $language = null, PageReference $page = 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
|
2023-06-15 13:04:33 +00:00
|
|
|
if ( $this->inParser || !str_contains( $message, '{{' ) ) {
|
2011-04-18 12:59:50 +00:00
|
|
|
return $message;
|
2011-04-18 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parser = $this->getParser();
|
2023-07-29 19:21:04 +00:00
|
|
|
$popts = $this->getParserOptions();
|
|
|
|
|
$popts->setInterfaceMessage( $interface );
|
|
|
|
|
$popts->setTargetLanguage( $language );
|
|
|
|
|
|
|
|
|
|
$userlang = $popts->setUserLang( $language );
|
|
|
|
|
$this->inParser = true;
|
|
|
|
|
$message = $parser->transformMsg( $message, $popts, $page );
|
|
|
|
|
$this->inParser = false;
|
|
|
|
|
$popts->setUserLang( $userlang );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Parser
|
|
|
|
|
*/
|
2018-02-19 11:21:24 +00:00
|
|
|
public function getParser() {
|
2022-07-14 06:24:44 +00:00
|
|
|
if ( !$this->parser ) {
|
2023-07-29 19:21:04 +00:00
|
|
|
$this->parser = $this->parserFactory->create();
|
2006-07-03 11:17:27 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
return $this->parser;
|
2011-04-18 12:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string $text
|
2021-05-12 01:20:12 +00:00
|
|
|
* @param PageReference|null $page
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param bool $linestart Whether this is at the start of a line
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param bool $interface Whether this is an interface message
|
2023-01-12 18:04:20 +00:00
|
|
|
* @param Language|StubUserLang|string|null $language Language code
|
2012-11-01 17:13:30 +00:00
|
|
|
* @return ParserOutput|string
|
2011-04-18 12:43:53 +00:00
|
|
|
*/
|
2021-05-12 01:20:12 +00:00
|
|
|
public function parse( $text, PageReference $page = null, $linestart = true,
|
2013-04-08 12:09:48 +00:00
|
|
|
$interface = false, $language = null
|
|
|
|
|
) {
|
2016-11-29 03:38:57 +00:00
|
|
|
global $wgTitle;
|
|
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
if ( $this->inParser ) {
|
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 ) ) {
|
2019-08-26 12:24:37 +00:00
|
|
|
$language = $this->langFactory->getLanguage( $language );
|
2016-03-09 17:39:50 +00:00
|
|
|
}
|
2012-03-02 04:00:58 +00:00
|
|
|
$popts->setTargetLanguage( $language );
|
2011-04-18 12:43:53 +00:00
|
|
|
|
2021-05-12 01:20:12 +00:00
|
|
|
if ( !$page ) {
|
2019-12-20 13:12:59 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'GlobalTitleFail' );
|
|
|
|
|
$logger->info(
|
|
|
|
|
__METHOD__ . ' called with no title set.',
|
|
|
|
|
[ 'exception' => new Exception ]
|
|
|
|
|
);
|
2021-05-12 01:20:12 +00:00
|
|
|
$page = $wgTitle;
|
2011-04-18 14:02:13 +00:00
|
|
|
}
|
2011-09-14 12:32:22 +00:00
|
|
|
// Sometimes $wgTitle isn't set either...
|
2021-05-12 01:20:12 +00:00
|
|
|
if ( !$page ) {
|
2022-07-14 06:24:44 +00:00
|
|
|
// It's not uncommon having a null $wgTitle in scripts. See r80898
|
|
|
|
|
// Create a ghost title in such case
|
2021-05-12 01:20:12 +00:00
|
|
|
$page = PageReferenceValue::localReference(
|
|
|
|
|
NS_SPECIAL,
|
|
|
|
|
'Badtitle/title not set in ' . __METHOD__
|
|
|
|
|
);
|
2011-09-14 12:32:22 +00:00
|
|
|
}
|
2011-04-22 20:17:21 +00:00
|
|
|
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->inParser = true;
|
2021-05-12 01:20:12 +00:00
|
|
|
$res = $parser->parse( $text, $page, $popts, $linestart );
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->inParser = false;
|
2011-04-18 12:43:53 +00:00
|
|
|
|
|
|
|
|
return $res;
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2018-02-19 11:21:24 +00:00
|
|
|
public function disable() {
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->disable = true;
|
2011-01-26 17:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-19 11:21:24 +00:00
|
|
|
public function enable() {
|
2022-07-14 06:24:44 +00:00
|
|
|
$this->disable = false;
|
2011-01-26 17:41:18 +00:00
|
|
|
}
|
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() {
|
2022-07-14 06:24:44 +00:00
|
|
|
return $this->disable;
|
2016-04-20 19:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2018-02-19 11:50:29 +00:00
|
|
|
* Clear all stored messages in global and local cache
|
|
|
|
|
*
|
|
|
|
|
* Mainly used after a mass rebuild
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2018-06-25 09:45:55 +00:00
|
|
|
public function clear() {
|
2021-12-05 20:03:21 +00:00
|
|
|
$langs = $this->languageNameUtils->getLanguageNames();
|
2023-07-08 19:54:11 +00:00
|
|
|
foreach ( $langs as $code => $_ ) {
|
2018-02-19 11:50:29 +00:00
|
|
|
$this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
|
2004-08-11 02:31:47 +00:00
|
|
|
}
|
2018-06-25 09:45:55 +00:00
|
|
|
$this->cache->clear();
|
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 ) {
|
2008-10-07 18:10:08 +00:00
|
|
|
$pieces = explode( '/', $key );
|
2013-04-08 12:09:48 +00:00
|
|
|
if ( count( $pieces ) < 2 ) {
|
2021-05-13 23:32:38 +00:00
|
|
|
return [ $key, $this->contLangCode ];
|
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 );
|
2021-12-05 20:03:21 +00:00
|
|
|
if ( !$this->languageNameUtils->getLanguageName(
|
|
|
|
|
$lang,
|
|
|
|
|
LanguageNameUtils::AUTONYMS,
|
|
|
|
|
LanguageNameUtils::DEFINED
|
|
|
|
|
) ) {
|
2021-05-13 23:32:38 +00:00
|
|
|
return [ $key, $this->contLangCode ];
|
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.
|
2023-10-02 12:56:25 +00:00
|
|
|
*
|
2013-04-08 12:09:48 +00:00
|
|
|
* @param string $code Language code
|
2022-02-26 07:54:16 +00:00
|
|
|
* @return string[]|null Array of message keys
|
2011-09-14 19:59:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getAllMessageKeys( $code ) {
|
|
|
|
|
$this->load( $code );
|
2018-06-25 09:45:55 +00:00
|
|
|
if ( !$this->cache->has( $code ) ) {
|
2011-09-14 19:59:50 +00:00
|
|
|
// 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
|
2018-06-25 09:45:55 +00:00
|
|
|
$cache = $this->cache->get( $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
|
|
|
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.
|
2018-07-29 12:24:54 +00:00
|
|
|
return array_map( [ $this->contLang, '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
|
|
|
|
|
*
|
2019-07-22 09:49:57 +00:00
|
|
|
* @param LinkTarget $linkTarget Message page title
|
2016-11-29 03:38:57 +00:00
|
|
|
* @param Content|null $content New content for edit/create, null on deletion
|
|
|
|
|
* @since 1.29
|
|
|
|
|
*/
|
2019-07-22 09:49:57 +00:00
|
|
|
public function updateMessageOverride( LinkTarget $linkTarget, Content $content = null ) {
|
2022-12-22 07:17:13 +00:00
|
|
|
// treat null as not existing
|
|
|
|
|
$msgText = $this->getMessageTextFromContent( $content ) ?? false;
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2019-07-22 09:49:57 +00:00
|
|
|
$this->replace( $linkTarget->getDBkey(), $msgText );
|
2016-11-29 03:38:57 +00:00
|
|
|
|
2020-01-23 18:39:23 +00:00
|
|
|
if ( $this->contLangConverter->hasVariants() ) {
|
2019-07-22 09:49:57 +00:00
|
|
|
$this->contLangConverter->updateConversionTable( $linkTarget );
|
2016-11-29 03:38:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 11:50:29 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $code Language code
|
|
|
|
|
* @return string WAN cache key usable as a "check key" against language page edits
|
|
|
|
|
*/
|
|
|
|
|
public function getCheckKey( $code ) {
|
|
|
|
|
return $this->wanCache->makeKey( 'messages', $code );
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 03:38:57 +00:00
|
|
|
/**
|
|
|
|
|
* @param Content|null $content Content or null if the message page does not exist
|
2020-11-30 16:58:58 +00:00
|
|
|
* @return string|false|null Returns false if $content is null and null on error
|
2016-11-29 03:38:57 +00:00
|
|
|
*/
|
|
|
|
|
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;
|
2019-10-11 23:37:37 +00:00
|
|
|
$this->logger->warning(
|
2016-11-29 03:38:57 +00:00
|
|
|
__METHOD__ . ": message content doesn't provide wikitext "
|
|
|
|
|
. "(content model: " . $content->getModel() . ")" );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Message page does not exist...
|
|
|
|
|
$msgText = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $msgText;
|
|
|
|
|
}
|
2017-04-06 06:16:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $hash Hash for this version of the entire key/value overrides map
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $title Message cache key with the initial uppercase letter
|
2017-04-06 06:16:16 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function bigMessageCacheKey( $hash, $title ) {
|
|
|
|
|
return $this->wanCache->makeKey( 'messages-big', $hash, $title );
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|