2005-04-15 14:12:39 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2010-08-20 20:39:04 +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
|
|
|
|
|
*
|
2008-07-09 08:16:39 +00:00
|
|
|
* @file
|
2020-02-12 02:09:33 +00:00
|
|
|
* @author Zhengzhu Feng <zhengzhu@gmail.com>
|
|
|
|
|
* @author fdcn <fdcn64@gmail.com>
|
|
|
|
|
* @author shinjiman <shinjiman@gmail.com>
|
|
|
|
|
* @author PhiLiP <philip.npc@gmail.com>
|
2008-07-09 08:16:39 +00:00
|
|
|
*/
|
2022-01-06 18:44:56 +00:00
|
|
|
|
2024-08-08 09:39:26 +00:00
|
|
|
namespace MediaWiki\Language;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2024-05-04 18:15:02 +00:00
|
|
|
use MediaWiki\Debug\DeprecationHelper;
|
2023-05-06 20:01:10 +00:00
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2023-02-16 19:27:21 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2019-07-22 09:49:57 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2016-01-24 10:29:10 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-25 15:19:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-06-16 18:56:48 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
2023-12-13 22:50:44 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2023-09-19 16:59:47 +00:00
|
|
|
use MediaWiki\Parser\Sanitizer;
|
2019-09-12 19:40:57 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2020-04-15 21:50:29 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2022-10-25 16:58:49 +00:00
|
|
|
use MediaWiki\StubObject\StubUserLang;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-08-08 09:39:26 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
use StringUtils;
|
|
|
|
|
use UnexpectedValueException;
|
2024-07-09 13:37:44 +00:00
|
|
|
use Wikimedia\ObjectCache\BagOStuff;
|
2016-01-24 10:29:10 +00:00
|
|
|
|
2008-07-09 08:16:39 +00:00
|
|
|
/**
|
2020-01-23 18:39:23 +00:00
|
|
|
* Base class for multi-variant language conversion.
|
2008-07-09 08:16:39 +00:00
|
|
|
*
|
2020-02-12 02:09:33 +00:00
|
|
|
* @ingroup Language
|
2008-07-09 08:16:39 +00:00
|
|
|
*/
|
2020-01-23 18:39:23 +00:00
|
|
|
abstract class LanguageConverter implements ILanguageConverter {
|
2020-01-29 15:13:08 +00:00
|
|
|
use DeprecationHelper;
|
|
|
|
|
|
2012-07-19 18:49:55 +00:00
|
|
|
/**
|
|
|
|
|
* languages supporting variants
|
|
|
|
|
* @since 1.20
|
2023-02-14 08:40:00 +00:00
|
|
|
* @var string[]
|
2012-07-19 18:49:55 +00:00
|
|
|
*/
|
2019-02-07 09:21:19 +00:00
|
|
|
public static $languagesWithVariants = [
|
2020-10-22 18:38:06 +00:00
|
|
|
'ban',
|
2013-07-27 23:01:54 +00:00
|
|
|
'en',
|
2017-07-31 22:35:39 +00:00
|
|
|
'crh',
|
2012-07-19 18:49:55 +00:00
|
|
|
'gan',
|
|
|
|
|
'iu',
|
|
|
|
|
'ku',
|
2024-05-04 11:46:02 +00:00
|
|
|
'mni',
|
2021-10-05 21:55:15 +00:00
|
|
|
'sh',
|
2012-07-19 18:49:55 +00:00
|
|
|
'shi',
|
|
|
|
|
'sr',
|
|
|
|
|
'tg',
|
2022-12-01 17:35:44 +00:00
|
|
|
'tly',
|
2012-08-20 14:55:28 +00:00
|
|
|
'uz',
|
2014-07-26 14:55:14 +00:00
|
|
|
'wuu',
|
2023-08-30 09:21:23 +00:00
|
|
|
'zgh',
|
2012-07-19 18:49:55 +00:00
|
|
|
'zh',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-07-19 18:49:55 +00:00
|
|
|
|
2022-11-08 06:31:39 +00:00
|
|
|
/**
|
|
|
|
|
* static default variant of languages supporting variants
|
|
|
|
|
* for use with DefaultOptionsLookup.php
|
|
|
|
|
* @since 1.40
|
2023-02-14 08:40:00 +00:00
|
|
|
* @var array<string,string>
|
2022-11-08 06:31:39 +00:00
|
|
|
*/
|
|
|
|
|
public static $languagesWithStaticDefaultVariant = [
|
|
|
|
|
'ban' => 'ban',
|
|
|
|
|
'en' => 'en',
|
|
|
|
|
'crh' => 'crh',
|
|
|
|
|
'gan' => 'gan',
|
|
|
|
|
'iu' => 'iu',
|
|
|
|
|
'ku' => 'ku',
|
2024-05-04 11:46:02 +00:00
|
|
|
'mni' => 'mni',
|
2021-10-05 21:55:15 +00:00
|
|
|
'sh' => 'sh-latn',
|
2022-11-08 06:31:39 +00:00
|
|
|
'shi' => 'shi',
|
|
|
|
|
'sr' => 'sr',
|
|
|
|
|
'tg' => 'tg',
|
2022-12-01 17:35:44 +00:00
|
|
|
'tly' => 'tly',
|
2022-11-08 06:31:39 +00:00
|
|
|
'uz' => 'uz',
|
2014-07-26 14:55:14 +00:00
|
|
|
'wuu' => 'wuu',
|
2023-08-30 09:21:23 +00:00
|
|
|
'zgh' => 'zgh',
|
2022-11-08 06:31:39 +00:00
|
|
|
'zh' => 'zh',
|
|
|
|
|
];
|
|
|
|
|
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var bool */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mTablesLoaded = false;
|
2022-02-20 05:45:06 +00:00
|
|
|
/** @var ReplacementArray[] */
|
2022-02-20 04:09:31 +00:00
|
|
|
protected $mTables = [];
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var Language|StubUserLang */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mLangObj;
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var string|false */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mConvRuleTitle = false;
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var string|null */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mURLVariant;
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var string|null */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mUserVariant;
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var string|null */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mHeaderVariant;
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var int */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mMaxDepth = 10;
|
2023-02-14 08:40:00 +00:00
|
|
|
/** @var string|null */
|
2020-01-29 15:13:08 +00:00
|
|
|
private $mVarSeparatorPattern;
|
2007-12-03 04:54:28 +00:00
|
|
|
|
2020-05-16 00:18:15 +00:00
|
|
|
private const CACHE_VERSION_KEY = 'VERSION 7';
|
2007-12-03 04:54:28 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2022-02-25 06:54:27 +00:00
|
|
|
* @param Language|StubUserLang $langobj
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2021-06-09 18:59:40 +00:00
|
|
|
public function __construct( $langobj ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$this->mLangObj = $langobj;
|
2021-01-29 11:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-08 06:31:39 +00:00
|
|
|
* Get the language code with converter (the "main" language code).
|
|
|
|
|
* Page language code would be the same of the language code with converter.
|
2023-10-02 12:56:25 +00:00
|
|
|
* Note that this code might not be included as one of the variant languages.
|
2021-01-29 11:23:29 +00:00
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getMainCode(): string;
|
|
|
|
|
|
2022-11-08 06:31:39 +00:00
|
|
|
/**
|
|
|
|
|
* Get static default variant.
|
2023-10-02 12:56:25 +00:00
|
|
|
* For use of specify the default variant form when it different from the
|
2022-11-08 06:31:39 +00:00
|
|
|
* default "unconverted/mixed-variant form".
|
|
|
|
|
* @since 1.40
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getStaticDefaultVariant(): string {
|
|
|
|
|
$code = $this->getMainCode();
|
|
|
|
|
return self::$languagesWithStaticDefaultVariant[$code] ?? $code;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 11:23:29 +00:00
|
|
|
/**
|
|
|
|
|
* Get supported variants of the language.
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
abstract protected function getLanguageVariants(): array;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get language variants fallbacks.
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getVariantsFallbacks(): array;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Get the strings that map to the flags.
|
2021-01-29 11:23:29 +00:00
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
final public function getFlags(): array {
|
2016-02-17 09:09:32 +00:00
|
|
|
$defaultflags = [
|
2023-10-02 12:56:25 +00:00
|
|
|
// 'S' show the converted text
|
2008-06-26 03:00:34 +00:00
|
|
|
// '+' add rules for alltext
|
2023-10-02 12:56:25 +00:00
|
|
|
// 'E' the flags have an error
|
2008-06-26 03:00:34 +00:00
|
|
|
// these flags above are reserved for program
|
2023-10-24 22:49:24 +00:00
|
|
|
'A' => 'A', // add rule for convert code (all text converted)
|
|
|
|
|
'T' => 'T', // title convert
|
|
|
|
|
'R' => 'R', // raw content
|
|
|
|
|
'D' => 'D', // convert description (subclass implement)
|
|
|
|
|
'-' => '-', // remove convert (not implement)
|
|
|
|
|
'H' => 'H', // add rule for convert code (but no display in placed code)
|
|
|
|
|
'N' => 'N', // current variant name
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2021-01-29 11:23:29 +00:00
|
|
|
$flags = array_merge( $defaultflags, $this->getAdditionalFlags() );
|
|
|
|
|
foreach ( $this->getVariants() as $v ) {
|
|
|
|
|
$flags[$v] = $v;
|
|
|
|
|
}
|
|
|
|
|
return $flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Provides additional flags for converter. By default, it returns empty array and
|
2021-08-23 08:56:24 +00:00
|
|
|
* typically should be overridden by implementation of converter.
|
2021-01-29 11:23:29 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getAdditionalFlags(): array {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get manual level limit for supported variants.
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
final public function getManualLevel() {
|
|
|
|
|
$manualLevel = $this->getAdditionalManualLevel();
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ( $this->getVariants() as $v ) {
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( array_key_exists( $v, $manualLevel ) ) {
|
2021-01-29 11:23:29 +00:00
|
|
|
$result[$v] = $manualLevel[$v];
|
2010-01-05 09:07:12 +00:00
|
|
|
} else {
|
2021-01-29 11:23:29 +00:00
|
|
|
$result[$v] = 'bidirectional';
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2009-01-17 05:01:43 +00:00
|
|
|
}
|
2021-01-29 11:23:29 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Provides additional flags for converter. By default, this function returns an empty array and
|
|
|
|
|
* typically should be overridden by the implementation of converter.
|
2021-01-29 11:23:29 +00:00
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getAdditionalManualLevel(): array {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get desc code separator. By default returns ":", can be overridden by
|
|
|
|
|
* implementation of converter.
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDescCodeSeparator(): string {
|
|
|
|
|
return ':';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get desc var separator. By default returns ";", can be overridden by
|
|
|
|
|
* implementation of converter.
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDescVarSeparator(): string {
|
|
|
|
|
return ';';
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-01-29 11:23:29 +00:00
|
|
|
* Get variant names.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getVariantNames(): array {
|
|
|
|
|
return MediaWikiServices::getInstance()
|
|
|
|
|
->getLanguageNameUtils()
|
|
|
|
|
->getLanguageNames();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function getVariants() {
|
2022-04-25 15:19:41 +00:00
|
|
|
$disabledVariants = MediaWikiServices::getInstance()->getMainConfig()->get(
|
|
|
|
|
MainConfigNames::DisabledVariants );
|
2022-01-06 18:44:56 +00:00
|
|
|
return array_diff( $this->getLanguageVariants(), $disabledVariants );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-07 08:35:25 +00:00
|
|
|
public function getVariantFallbacks( $variant ) {
|
2022-11-08 06:31:39 +00:00
|
|
|
return $this->getVariantsFallbacks()[$variant] ?? $this->getStaticDefaultVariant();
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-07 08:35:25 +00:00
|
|
|
public function getConvRuleTitle() {
|
2010-01-15 19:14:23 +00:00
|
|
|
return $this->mConvRuleTitle;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-28 16:58:39 +00:00
|
|
|
public function getPreferredVariant() {
|
2010-01-12 05:31:46 +00:00
|
|
|
$req = $this->getURLVariant();
|
2010-01-06 03:50:59 +00:00
|
|
|
|
2023-05-06 20:01:10 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
( new HookRunner( $services->getHookContainer() ) )->onGetLangPreferredVariant( $req );
|
2017-07-23 13:35:09 +00:00
|
|
|
|
2023-02-14 08:46:25 +00:00
|
|
|
if ( !$req ) {
|
|
|
|
|
$user = RequestContext::getMain()->getUser();
|
|
|
|
|
// NOTE: For some calls there may not be a context user or session that is safe
|
|
|
|
|
// to use, see (T235360)
|
2023-10-02 12:56:25 +00:00
|
|
|
// Use case: During user autocreation, UserNameUtils::isUsable is called which uses interface
|
2023-02-14 08:46:25 +00:00
|
|
|
// messages for reserved usernames.
|
|
|
|
|
if ( $user->isSafeToLoad() && $user->isRegistered() ) {
|
|
|
|
|
$req = $this->getUserVariant( $user );
|
|
|
|
|
} else {
|
|
|
|
|
$req = $this->getHeaderVariant();
|
|
|
|
|
}
|
2009-01-29 08:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-06 20:01:10 +00:00
|
|
|
$defaultLanguageVariant = $services->getMainConfig()
|
2023-02-14 08:46:25 +00:00
|
|
|
->get( MainConfigNames::DefaultLanguageVariant );
|
|
|
|
|
if ( !$req && $defaultLanguageVariant ) {
|
2022-01-06 18:44:56 +00:00
|
|
|
$req = $this->validateVariant( $defaultLanguageVariant );
|
2010-01-06 03:50:59 +00:00
|
|
|
}
|
2010-01-05 09:07:12 +00:00
|
|
|
|
2018-07-03 20:12:38 +00:00
|
|
|
$req = $this->validateVariant( $req );
|
|
|
|
|
|
2010-01-12 05:31:46 +00:00
|
|
|
// This function, unlike the other get*Variant functions, is
|
2023-10-02 12:56:25 +00:00
|
|
|
// not memoized (i.e., there return value is not cached) since
|
2010-01-12 05:31:46 +00:00
|
|
|
// new information might appear during processing after this
|
|
|
|
|
// is first called.
|
2022-12-05 20:37:13 +00:00
|
|
|
return $req ?? $this->getStaticDefaultVariant();
|
2010-01-06 03:50:59 +00:00
|
|
|
}
|
2010-01-05 09:34:42 +00:00
|
|
|
|
2010-10-28 16:58:39 +00:00
|
|
|
public function getDefaultVariant() {
|
2022-04-25 15:19:41 +00:00
|
|
|
$defaultLanguageVariant = MediaWikiServices::getInstance()->getMainConfig()->get(
|
|
|
|
|
MainConfigNames::DefaultLanguageVariant );
|
2010-10-28 16:58:39 +00:00
|
|
|
|
2022-12-05 20:37:13 +00:00
|
|
|
$req = $this->getURLVariant() ?? $this->getHeaderVariant();
|
2012-05-12 15:52:52 +00:00
|
|
|
|
2022-12-05 20:37:13 +00:00
|
|
|
if ( !$req && $defaultLanguageVariant ) {
|
2022-01-06 18:44:56 +00:00
|
|
|
$req = $this->validateVariant( $defaultLanguageVariant );
|
2010-10-28 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 20:37:13 +00:00
|
|
|
return $req ?? $this->getStaticDefaultVariant();
|
2010-10-28 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-12 19:19:23 +00:00
|
|
|
public function validateVariant( $variant = null ) {
|
2018-07-03 20:12:38 +00:00
|
|
|
if ( $variant === null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// Our internal variants are always lower-case; the variant we
|
2023-10-02 12:56:25 +00:00
|
|
|
// are validating may have mixed cases.
|
2018-07-03 20:12:38 +00:00
|
|
|
$variant = LanguageCode::replaceDeprecatedCodes( strtolower( $variant ) );
|
2021-01-29 11:23:29 +00:00
|
|
|
if ( in_array( $variant, $this->getVariants() ) ) {
|
2010-07-07 08:35:25 +00:00
|
|
|
return $variant;
|
2010-01-12 05:31:46 +00:00
|
|
|
}
|
2018-07-03 20:12:38 +00:00
|
|
|
// Browsers are supposed to use BCP 47 standard in the
|
|
|
|
|
// Accept-Language header, but not all of our internal
|
|
|
|
|
// mediawiki variant codes are BCP 47. Map BCP 47 code
|
|
|
|
|
// to our internal code.
|
2021-01-29 11:23:29 +00:00
|
|
|
foreach ( $this->getVariants() as $v ) {
|
2023-10-02 12:56:25 +00:00
|
|
|
// Case-insensitive match (BCP 47 is mixed-case)
|
2018-07-03 20:12:38 +00:00
|
|
|
if ( strtolower( LanguageCode::bcp47( $v ) ) === $variant ) {
|
|
|
|
|
return $v;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-12 05:31:46 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-28 16:58:39 +00:00
|
|
|
public function getURLVariant() {
|
2010-01-12 05:31:46 +00:00
|
|
|
if ( $this->mURLVariant ) {
|
|
|
|
|
return $this->mURLVariant;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 09:50:44 +00:00
|
|
|
$request = RequestContext::getMain()->getRequest();
|
2010-01-12 05:31:46 +00:00
|
|
|
// see if the preference is set in the request
|
2024-06-01 09:50:44 +00:00
|
|
|
$ret = $request->getText( 'variant' );
|
2010-01-12 05:31:46 +00:00
|
|
|
|
2010-01-12 20:54:26 +00:00
|
|
|
if ( !$ret ) {
|
2024-06-01 09:50:44 +00:00
|
|
|
$ret = $request->getVal( 'uselang' );
|
2010-01-12 05:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
2014-01-01 18:56:08 +00:00
|
|
|
$this->mURLVariant = $this->validateVariant( $ret );
|
|
|
|
|
return $this->mURLVariant;
|
2010-01-12 05:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user has a variant set.
|
2010-01-11 05:53:39 +00:00
|
|
|
*
|
2020-02-10 07:44:01 +00:00
|
|
|
* @param User $user
|
2023-02-14 08:40:00 +00:00
|
|
|
* @return string|null Variant if one found, null otherwise
|
2010-01-11 05:53:39 +00:00
|
|
|
*/
|
2020-02-10 07:44:01 +00:00
|
|
|
protected function getUserVariant( User $user ) {
|
2020-01-07 13:56:48 +00:00
|
|
|
// This should only be called within the class after the user is known to be
|
2020-09-27 08:09:09 +00:00
|
|
|
// safe to load and logged in, but check just in case.
|
2020-02-10 07:44:01 +00:00
|
|
|
if ( !$user->isSafeToLoad() ) {
|
2023-02-14 08:40:00 +00:00
|
|
|
return null;
|
2016-03-09 19:30:31 +00:00
|
|
|
}
|
2020-02-10 07:44:01 +00:00
|
|
|
|
2021-11-02 21:59:46 +00:00
|
|
|
if ( !$this->mUserVariant ) {
|
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
if ( $user->isRegistered() ) {
|
|
|
|
|
// Get language variant preference from logged in users
|
|
|
|
|
if (
|
2022-11-08 06:31:39 +00:00
|
|
|
$this->getMainCode() ===
|
2021-11-02 21:59:46 +00:00
|
|
|
$services->getContentLanguage()->getCode()
|
|
|
|
|
) {
|
|
|
|
|
$optionName = 'variant';
|
|
|
|
|
} else {
|
|
|
|
|
$optionName = 'variant-' . $this->getMainCode();
|
|
|
|
|
}
|
2013-06-12 12:13:04 +00:00
|
|
|
} else {
|
2021-11-02 21:59:46 +00:00
|
|
|
// figure out user lang without constructing wgLang to avoid
|
|
|
|
|
// infinite recursion
|
|
|
|
|
$optionName = 'language';
|
2013-06-12 12:13:04 +00:00
|
|
|
}
|
2021-11-02 21:59:46 +00:00
|
|
|
$ret = $services->getUserOptionsLookup()->getOption( $user, $optionName );
|
|
|
|
|
|
|
|
|
|
$this->mUserVariant = $this->validateVariant( $ret );
|
2010-01-11 05:53:39 +00:00
|
|
|
}
|
2010-01-12 05:31:46 +00:00
|
|
|
|
2014-01-01 18:56:08 +00:00
|
|
|
return $this->mUserVariant;
|
2010-01-11 05:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-06 03:50:59 +00:00
|
|
|
/**
|
|
|
|
|
* Determine the language variant from the Accept-Language header.
|
|
|
|
|
*
|
2023-02-14 08:40:00 +00:00
|
|
|
* @return string|null Variant if one found, null otherwise
|
2010-01-06 03:50:59 +00:00
|
|
|
*/
|
2010-07-07 08:35:25 +00:00
|
|
|
protected function getHeaderVariant() {
|
2010-01-06 03:50:59 +00:00
|
|
|
if ( $this->mHeaderVariant ) {
|
|
|
|
|
return $this->mHeaderVariant;
|
|
|
|
|
}
|
2009-05-22 17:40:00 +00:00
|
|
|
|
2024-06-01 09:50:44 +00:00
|
|
|
$request = RequestContext::getMain()->getRequest();
|
2018-07-03 20:12:38 +00:00
|
|
|
// See if some supported language variant is set in the
|
2011-03-22 20:23:52 +00:00
|
|
|
// HTTP header.
|
2024-06-01 09:50:44 +00:00
|
|
|
$languages = array_keys( $request->getAcceptLang() );
|
2023-09-08 21:18:11 +00:00
|
|
|
if ( !$languages ) {
|
2010-01-12 05:31:46 +00:00
|
|
|
return null;
|
2010-01-06 03:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$fallbackLanguages = [];
|
2010-01-06 03:50:59 +00:00
|
|
|
foreach ( $languages as $language ) {
|
2010-01-12 05:31:46 +00:00
|
|
|
$this->mHeaderVariant = $this->validateVariant( $language );
|
2010-01-12 18:35:45 +00:00
|
|
|
if ( $this->mHeaderVariant ) {
|
2010-01-12 05:31:46 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To see if there are fallbacks of current language.
|
|
|
|
|
// We record these fallback variants, and process
|
|
|
|
|
// them later.
|
|
|
|
|
$fallbacks = $this->getVariantFallbacks( $language );
|
2022-11-08 06:31:39 +00:00
|
|
|
if (
|
|
|
|
|
is_string( $fallbacks ) &&
|
|
|
|
|
$fallbacks !== $this->getStaticDefaultVariant()
|
|
|
|
|
) {
|
2011-03-22 20:23:52 +00:00
|
|
|
$fallbackLanguages[] = $fallbacks;
|
2010-01-12 05:31:46 +00:00
|
|
|
} elseif ( is_array( $fallbacks ) ) {
|
2011-03-22 20:23:52 +00:00
|
|
|
$fallbackLanguages =
|
|
|
|
|
array_merge( $fallbackLanguages, $fallbacks );
|
2009-01-29 08:59:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-06 03:50:59 +00:00
|
|
|
|
2010-01-12 18:35:45 +00:00
|
|
|
if ( !$this->mHeaderVariant ) {
|
2010-01-12 05:31:46 +00:00
|
|
|
// process fallback languages now
|
2011-03-22 20:23:52 +00:00
|
|
|
$fallback_languages = array_unique( $fallbackLanguages );
|
2011-03-30 12:04:25 +00:00
|
|
|
foreach ( $fallback_languages as $language ) {
|
2010-01-12 05:31:46 +00:00
|
|
|
$this->mHeaderVariant = $this->validateVariant( $language );
|
2010-01-12 18:35:45 +00:00
|
|
|
if ( $this->mHeaderVariant ) {
|
2010-01-12 05:31:46 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2010-01-06 03:50:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-12 05:31:46 +00:00
|
|
|
|
|
|
|
|
return $this->mHeaderVariant;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2010-01-05 09:34:42 +00:00
|
|
|
|
2010-07-07 08:35:25 +00:00
|
|
|
public function autoConvert( $text, $toVariant = false ) {
|
2010-07-07 08:59:03 +00:00
|
|
|
$this->loadTables();
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( !$toVariant ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$toVariant = $this->getPreferredVariant();
|
2010-01-12 05:31:46 +00:00
|
|
|
if ( !$toVariant ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2013-04-17 19:10:02 +00:00
|
|
|
if ( $this->guessVariant( $text, $toVariant ) ) {
|
2011-11-16 15:12:00 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2023-10-02 12:56:25 +00:00
|
|
|
/**
|
|
|
|
|
* We convert everything except:
|
2022-07-12 18:03:53 +00:00
|
|
|
* 1. HTML markups (anything between < and >)
|
|
|
|
|
* 2. HTML entities
|
|
|
|
|
* 3. placeholders created by the parser
|
|
|
|
|
* IMPORTANT: Beware of failure from pcre.backtrack_limit (T124404).
|
2023-10-02 12:56:25 +00:00
|
|
|
* Minimize the use of backtracking where possible.
|
2022-07-12 18:03:53 +00:00
|
|
|
*/
|
2019-07-02 18:04:39 +00:00
|
|
|
static $reg;
|
|
|
|
|
if ( $reg === null ) {
|
|
|
|
|
$marker = '|' . Parser::MARKER_PREFIX . '[^\x7f]++\x7f';
|
|
|
|
|
|
|
|
|
|
// this one is needed when the text is inside an HTML markup
|
|
|
|
|
$htmlfix = '|<[^>\004]++(?=\004$)|^[^<>]*+>';
|
|
|
|
|
|
|
|
|
|
// Optimize for the common case where these tags have
|
2022-01-09 17:24:04 +00:00
|
|
|
// few or no children. Thus try and possessively get as much as
|
2019-07-02 18:04:39 +00:00
|
|
|
// possible, and only engage in backtracking when we hit a '<'.
|
|
|
|
|
|
|
|
|
|
// disable convert to variants between <code> tags
|
|
|
|
|
$codefix = '<code>[^<]*+(?:(?:(?!<\/code>).)[^<]*+)*+<\/code>|';
|
|
|
|
|
// disable conversion of <script> tags
|
|
|
|
|
$scriptfix = '<script[^>]*+>[^<]*+(?:(?:(?!<\/script>).)[^<]*+)*+<\/script>|';
|
|
|
|
|
// disable conversion of <pre> tags
|
|
|
|
|
$prefix = '<pre[^>]*+>[^<]*+(?:(?:(?!<\/pre>).)[^<]*+)*+<\/pre>|';
|
|
|
|
|
// The "|.*+)" at the end, is in case we missed some part of html syntax,
|
|
|
|
|
// we will fail securely (hopefully) by matching the rest of the string.
|
|
|
|
|
$htmlFullTag = '<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)|';
|
|
|
|
|
|
|
|
|
|
$reg = '/' . $codefix . $scriptfix . $prefix . $htmlFullTag .
|
2021-09-03 22:52:31 +00:00
|
|
|
'&[a-zA-Z#][a-z0-9]++;' . $marker . $htmlfix . '|\004$/s';
|
2019-07-02 18:04:39 +00:00
|
|
|
}
|
2011-02-21 14:17:50 +00:00
|
|
|
$startPos = 0;
|
|
|
|
|
$sourceBlob = '';
|
|
|
|
|
$literalBlob = '';
|
|
|
|
|
|
|
|
|
|
// Guard against delimiter nulls in the input
|
2017-02-27 21:27:15 +00:00
|
|
|
// (should never happen: see T159174)
|
2011-02-21 14:17:50 +00:00
|
|
|
$text = str_replace( "\000", '', $text );
|
2017-11-15 05:34:10 +00:00
|
|
|
$text = str_replace( "\004", '', $text );
|
2008-06-26 03:00:34 +00:00
|
|
|
|
2011-02-23 15:54:19 +00:00
|
|
|
$markupMatches = null;
|
|
|
|
|
$elementMatches = null;
|
2016-01-24 10:29:10 +00:00
|
|
|
|
|
|
|
|
// We add a marker (\004) at the end of text, to ensure we always match the
|
|
|
|
|
// entire text (Otherwise, pcre.backtrack_limit might cause silent failure)
|
2019-07-02 18:02:33 +00:00
|
|
|
$textWithMarker = $text . "\004";
|
2011-02-21 14:17:50 +00:00
|
|
|
while ( $startPos < strlen( $text ) ) {
|
2019-07-02 18:02:33 +00:00
|
|
|
if ( preg_match( $reg, $textWithMarker, $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
|
2011-02-21 14:17:50 +00:00
|
|
|
$elementPos = $markupMatches[0][1];
|
|
|
|
|
$element = $markupMatches[0][0];
|
2016-01-24 10:29:10 +00:00
|
|
|
if ( $element === "\004" ) {
|
|
|
|
|
// We hit the end.
|
|
|
|
|
$elementPos = strlen( $text );
|
|
|
|
|
$element = '';
|
2017-11-15 06:56:38 +00:00
|
|
|
} elseif ( substr( $element, -1 ) === "\004" ) {
|
2017-11-15 05:34:10 +00:00
|
|
|
// This can sometimes happen if we have
|
2023-10-02 12:56:25 +00:00
|
|
|
// unclosed html tags. For example,
|
2017-11-15 05:34:10 +00:00
|
|
|
// when converting a title attribute
|
|
|
|
|
// during a recursive call that contains
|
|
|
|
|
// a < e.g. <div title="<">.
|
|
|
|
|
$element = substr( $element, 0, -1 );
|
2016-01-24 10:29:10 +00:00
|
|
|
}
|
2011-02-21 14:17:50 +00:00
|
|
|
} else {
|
2016-01-24 10:29:10 +00:00
|
|
|
// If we hit here, then Language Converter could be tricked
|
|
|
|
|
// into doing an XSS, so we refuse to translate.
|
2023-10-02 12:56:25 +00:00
|
|
|
// If expected input manages to reach this code path,
|
2016-01-24 10:29:10 +00:00
|
|
|
// we should consider it a bug.
|
|
|
|
|
$log = LoggerFactory::getInstance( 'languageconverter' );
|
|
|
|
|
$log->error( "Hit pcre.backtrack_limit in " . __METHOD__
|
|
|
|
|
. ". Disabling language conversion for this page.",
|
2017-11-15 03:37:27 +00:00
|
|
|
[
|
2016-01-24 10:29:10 +00:00
|
|
|
"method" => __METHOD__,
|
|
|
|
|
"variant" => $toVariant,
|
|
|
|
|
"startOfText" => substr( $text, 0, 500 )
|
2017-11-15 03:37:27 +00:00
|
|
|
]
|
2016-01-24 10:29:10 +00:00
|
|
|
);
|
|
|
|
|
return $text;
|
2011-02-21 14:17:50 +00:00
|
|
|
}
|
|
|
|
|
// Queue the part before the markup for translation in a batch
|
|
|
|
|
$sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) . "\000";
|
|
|
|
|
|
|
|
|
|
// Advance to the next position
|
2011-10-18 17:27:52 +00:00
|
|
|
$startPos = $elementPos + strlen( $element );
|
2011-02-21 14:17:50 +00:00
|
|
|
|
|
|
|
|
// Translate any alt or title attributes inside the matched element
|
2013-12-01 20:39:00 +00:00
|
|
|
if ( $element !== ''
|
2016-01-24 10:29:10 +00:00
|
|
|
&& preg_match( '/^(<[^>\s]*+)\s([^>]*+)(.*+)$/', $element, $elementMatches )
|
2013-12-01 20:39:00 +00:00
|
|
|
) {
|
2017-11-15 05:34:10 +00:00
|
|
|
// FIXME, this decodes entities, so if you have something
|
|
|
|
|
// like <div title="foo<bar"> the bar won't get
|
|
|
|
|
// translated since after entity decoding it looks like
|
|
|
|
|
// unclosed html and we call this method recursively
|
|
|
|
|
// on attributes.
|
2011-02-21 14:17:50 +00:00
|
|
|
$attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
|
2017-11-15 05:34:10 +00:00
|
|
|
// Ensure self-closing tags stay self-closing.
|
|
|
|
|
$close = substr( $elementMatches[2], -1 ) === '/' ? ' /' : '';
|
2011-02-21 14:17:50 +00:00
|
|
|
$changed = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( [ 'title', 'alt' ] as $attrName ) {
|
2011-02-21 14:17:50 +00:00
|
|
|
if ( !isset( $attrs[$attrName] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$attr = $attrs[$attrName];
|
|
|
|
|
// Don't convert URLs
|
2024-01-25 11:13:06 +00:00
|
|
|
if ( !str_contains( $attr, '://' ) ) {
|
2012-12-21 09:55:57 +00:00
|
|
|
$attr = $this->recursiveConvertTopLevel( $attr, $toVariant );
|
2011-02-21 14:17:50 +00:00
|
|
|
}
|
2011-10-18 17:27:52 +00:00
|
|
|
|
2011-02-21 14:17:50 +00:00
|
|
|
if ( $attr !== $attrs[$attrName] ) {
|
|
|
|
|
$attrs[$attrName] = $attr;
|
|
|
|
|
$changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $changed ) {
|
2020-12-30 18:38:48 +00:00
|
|
|
// @phan-suppress-next-line SecurityCheck-DoubleEscaped Explained above with decodeTagAttributes
|
2011-10-18 17:27:52 +00:00
|
|
|
$element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
|
2017-11-15 05:34:10 +00:00
|
|
|
$close . $elementMatches[3];
|
2011-02-21 14:17:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$literalBlob .= $element . "\000";
|
2005-08-15 19:27:58 +00:00
|
|
|
}
|
2011-02-21 14:17:50 +00:00
|
|
|
|
|
|
|
|
// Do the main translation batch
|
|
|
|
|
$translatedBlob = $this->translate( $sourceBlob, $toVariant );
|
|
|
|
|
|
|
|
|
|
// Put the output back together
|
|
|
|
|
$translatedIter = StringUtils::explode( "\000", $translatedBlob );
|
|
|
|
|
$literalIter = StringUtils::explode( "\000", $literalBlob );
|
|
|
|
|
$output = '';
|
|
|
|
|
while ( $translatedIter->valid() && $literalIter->valid() ) {
|
|
|
|
|
$output .= $translatedIter->current();
|
|
|
|
|
$output .= $literalIter->current();
|
|
|
|
|
$translatedIter->next();
|
|
|
|
|
$literalIter->next();
|
2009-11-27 17:25:59 +00:00
|
|
|
}
|
2011-02-21 14:17:50 +00:00
|
|
|
|
|
|
|
|
return $output;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2005-08-15 19:27:58 +00:00
|
|
|
|
2011-03-01 13:36:25 +00:00
|
|
|
public function translate( $text, $variant ) {
|
2009-11-27 17:25:59 +00:00
|
|
|
// If $text is empty or only includes spaces, do nothing
|
|
|
|
|
// Otherwise translate it
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( trim( $text ) ) {
|
2010-07-07 08:59:03 +00:00
|
|
|
$this->loadTables();
|
2010-01-05 09:07:12 +00:00
|
|
|
$text = $this->mTables[$variant]->replace( $text );
|
2009-11-27 17:25:59 +00:00
|
|
|
}
|
2006-11-21 09:53:45 +00:00
|
|
|
return $text;
|
2006-07-26 14:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 13:06:06 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $text Text to convert
|
|
|
|
|
* @param string $variant Variant language code
|
|
|
|
|
* @return string Translated text
|
|
|
|
|
*/
|
|
|
|
|
protected function translateWithoutRomanNumbers( $text, $variant ) {
|
|
|
|
|
$breaks = '[^\w\x80-\xff]';
|
|
|
|
|
|
|
|
|
|
// regexp for roman numbers
|
|
|
|
|
// Lookahead assertion ensures $roman doesn't match the empty string
|
|
|
|
|
$roman = '(?=[MDCLXVI])M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
|
|
|
|
|
|
|
|
|
|
$reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
|
|
|
|
|
. $roman . '$|' . $breaks . $roman . $breaks . '/';
|
|
|
|
|
|
|
|
|
|
$matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
|
|
|
|
|
|
|
|
|
|
$m = array_shift( $matches );
|
|
|
|
|
$this->loadTables();
|
|
|
|
|
if ( !isset( $this->mTables[$variant] ) ) {
|
2024-01-28 21:41:46 +00:00
|
|
|
throw new RuntimeException( "Broken variant table: "
|
2023-10-02 13:06:06 +00:00
|
|
|
. implode( ',', array_keys( $this->mTables ) ) );
|
|
|
|
|
}
|
|
|
|
|
$ret = $this->mTables[$variant]->replace( $m[0] );
|
|
|
|
|
$mstart = (int)$m[1] + strlen( $m[0] );
|
|
|
|
|
foreach ( $matches as $m ) {
|
|
|
|
|
$ret .= substr( $text, $mstart, (int)$m[1] - $mstart );
|
|
|
|
|
$ret .= $this->translate( $m[0], $variant );
|
|
|
|
|
$mstart = (int)$m[1] + strlen( $m[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-22 21:34:19 +00:00
|
|
|
public function autoConvertToAllVariants( $text ) {
|
2010-07-07 08:59:03 +00:00
|
|
|
$this->loadTables();
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2021-01-29 11:23:29 +00:00
|
|
|
foreach ( $this->getVariants() as $variant ) {
|
2010-01-05 09:07:12 +00:00
|
|
|
$ret[$variant] = $this->translate( $text, $variant );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-09 08:16:39 +00:00
|
|
|
/**
|
2010-07-07 14:46:27 +00:00
|
|
|
* Apply manual conversion rules.
|
2010-07-29 09:43:18 +00:00
|
|
|
*
|
2014-04-17 13:31:28 +00:00
|
|
|
* @param ConverterRule $convRule
|
2008-07-09 08:16:39 +00:00
|
|
|
*/
|
2020-01-23 18:39:23 +00:00
|
|
|
protected function applyManualConv( ConverterRule $convRule ) {
|
2010-07-06 03:06:31 +00:00
|
|
|
// Use syntax -{T|zh-cn:TitleCN; zh-tw:TitleTw}- to custom
|
|
|
|
|
// title conversion.
|
2017-02-20 22:46:45 +00:00
|
|
|
// T26072: $mConvRuleTitle was overwritten by other manual
|
2010-07-07 14:46:27 +00:00
|
|
|
// rule(s) not for title, this breaks the title conversion.
|
2010-07-06 03:06:31 +00:00
|
|
|
$newConvRuleTitle = $convRule->getTitle();
|
2021-10-04 18:40:24 +00:00
|
|
|
if ( $newConvRuleTitle !== false ) {
|
2010-07-07 14:46:27 +00:00
|
|
|
// So I add an empty check for getTitle()
|
2010-07-06 03:06:31 +00:00
|
|
|
$this->mConvRuleTitle = $newConvRuleTitle;
|
|
|
|
|
}
|
2008-07-09 08:16:39 +00:00
|
|
|
|
2010-07-07 14:46:27 +00:00
|
|
|
// merge/remove manual conversion rules to/from global table
|
2008-07-09 08:16:39 +00:00
|
|
|
$convTable = $convRule->getConvTable();
|
2008-07-10 13:56:23 +00:00
|
|
|
$action = $convRule->getRulesAction();
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $convTable as $variant => $pair ) {
|
2018-07-03 20:12:38 +00:00
|
|
|
$v = $this->validateVariant( $variant );
|
|
|
|
|
if ( !$v ) {
|
2010-01-05 09:07:12 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $action == 'add' ) {
|
2014-05-06 16:16:12 +00:00
|
|
|
// More efficient than array_merge(), about 2.5 times.
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $pair as $from => $to ) {
|
2018-07-03 20:12:38 +00:00
|
|
|
$this->mTables[$v]->setPair( $from, $to );
|
2009-01-29 10:09:19 +00:00
|
|
|
}
|
2010-01-05 09:07:12 +00:00
|
|
|
} elseif ( $action == 'remove' ) {
|
2018-07-03 20:12:38 +00:00
|
|
|
$this->mTables[$v]->removeArray( $pair );
|
2009-01-17 05:01:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 02:52:53 +00:00
|
|
|
public function convertSplitTitle( $title ) {
|
2010-04-10 13:38:50 +00:00
|
|
|
$variant = $this->getPreferredVariant();
|
2022-08-09 02:52:53 +00:00
|
|
|
|
2021-04-25 17:29:33 +00:00
|
|
|
$index = $title->getNamespace();
|
2022-08-09 02:52:53 +00:00
|
|
|
$nsText = $this->convertNamespace( $index, $variant );
|
|
|
|
|
|
2021-04-25 17:29:33 +00:00
|
|
|
$name = str_replace( '_', ' ', $title->getDBKey() );
|
2022-08-09 02:52:53 +00:00
|
|
|
$mainText = $this->translate( $name, $variant );
|
2019-07-22 09:49:57 +00:00
|
|
|
|
2022-08-09 02:52:53 +00:00
|
|
|
return [ $nsText, ':', $mainText ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function convertTitle( $title ) {
|
|
|
|
|
[ $nsText, $nsSeparator, $mainText ] = $this->convertSplitTitle( $title );
|
|
|
|
|
return $nsText !== '' ?
|
|
|
|
|
$nsText . $nsSeparator . $mainText :
|
|
|
|
|
$mainText;
|
2012-05-29 09:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 11:29:49 +00:00
|
|
|
public function convertNamespace( $index, $variant = null ) {
|
2015-09-26 05:57:58 +00:00
|
|
|
if ( $index === NS_MAIN ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 20:37:13 +00:00
|
|
|
$variant ??= $this->getPreferredVariant();
|
2015-09-26 05:57:58 +00:00
|
|
|
|
2016-09-29 02:58:57 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
|
|
|
|
|
$key = $cache->makeKey( 'languageconverter', 'namespace-text', $index, $variant );
|
2021-10-21 14:11:56 +00:00
|
|
|
return $cache->getWithSetCallback(
|
|
|
|
|
$key,
|
|
|
|
|
BagOStuff::TTL_MINUTE,
|
|
|
|
|
function () use ( $index, $variant ) {
|
|
|
|
|
return $this->computeNsVariantText( $index, $variant );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $index
|
|
|
|
|
* @param string|null $variant
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function computeNsVariantText( int $index, ?string $variant ): string {
|
|
|
|
|
$nsVariantText = false;
|
2015-09-26 05:57:58 +00:00
|
|
|
|
|
|
|
|
// First check if a message gives a converted name in the target variant.
|
|
|
|
|
$nsConvMsg = wfMessage( 'conversion-ns' . $index )->inLanguage( $variant );
|
|
|
|
|
if ( $nsConvMsg->exists() ) {
|
|
|
|
|
$nsVariantText = $nsConvMsg->plain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then check if a message gives a converted name in content language
|
|
|
|
|
// which needs extra translation to the target variant.
|
|
|
|
|
if ( $nsVariantText === false ) {
|
2011-05-25 18:56:05 +00:00
|
|
|
$nsConvMsg = wfMessage( 'conversion-ns' . $index )->inContentLanguage();
|
|
|
|
|
if ( $nsConvMsg->exists() ) {
|
2015-09-26 05:57:58 +00:00
|
|
|
$nsVariantText = $this->translate( $nsConvMsg->plain(), $variant );
|
2010-04-10 13:38:50 +00:00
|
|
|
}
|
2015-09-26 05:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $nsVariantText === false ) {
|
2012-05-29 09:56:21 +00:00
|
|
|
// No message exists, retrieve it from the target variant's namespace names.
|
2019-07-22 09:49:57 +00:00
|
|
|
$mLangObj = MediaWikiServices::getInstance()
|
|
|
|
|
->getLanguageFactory()
|
|
|
|
|
->getLanguage( $variant );
|
2020-01-23 18:39:23 +00:00
|
|
|
$nsVariantText = $mLangObj->getFormattedNsText( $index );
|
2010-04-10 13:38:50 +00:00
|
|
|
}
|
2015-09-26 05:57:58 +00:00
|
|
|
return $nsVariantText;
|
2010-04-10 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-07 14:46:27 +00:00
|
|
|
public function convert( $text ) {
|
|
|
|
|
$variant = $this->getPreferredVariant();
|
|
|
|
|
return $this->convertTo( $text, $variant );
|
|
|
|
|
}
|
2010-07-29 09:43:18 +00:00
|
|
|
|
2023-03-06 19:24:42 +00:00
|
|
|
public function convertTo( $text, $variant, bool $clearState = true ) {
|
2020-11-25 04:35:49 +00:00
|
|
|
$languageConverterFactory = MediaWikiServices::getInstance()->getLanguageConverterFactory();
|
|
|
|
|
if ( $languageConverterFactory->isConversionDisabled() ) {
|
2011-03-22 20:23:52 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2012-09-25 17:45:54 +00:00
|
|
|
// Reset converter state for a new converter run.
|
2023-03-06 19:24:42 +00:00
|
|
|
if ( $clearState ) {
|
|
|
|
|
$this->mConvRuleTitle = false;
|
|
|
|
|
}
|
2010-07-07 14:46:27 +00:00
|
|
|
return $this->recursiveConvertTopLevel( $text, $variant );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recursively convert text on the outside. Allow to use nested
|
|
|
|
|
* markups to custom rules.
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $text Text to be converted
|
|
|
|
|
* @param string $variant The target variant code
|
|
|
|
|
* @param int $depth Depth of recursion
|
|
|
|
|
* @return string Converted text
|
2010-07-07 14:46:27 +00:00
|
|
|
*/
|
2010-01-19 02:36:33 +00:00
|
|
|
protected function recursiveConvertTopLevel( $text, $variant, $depth = 0 ) {
|
|
|
|
|
$startPos = 0;
|
|
|
|
|
$out = '';
|
|
|
|
|
$length = strlen( $text );
|
2012-04-10 16:36:51 +00:00
|
|
|
$shouldConvert = !$this->guessVariant( $text, $variant );
|
2020-06-15 10:46:22 +00:00
|
|
|
$continue = true;
|
2016-02-11 22:08:03 +00:00
|
|
|
|
|
|
|
|
$noScript = '<script.*?>.*?<\/script>(*SKIP)(*FAIL)';
|
|
|
|
|
$noStyle = '<style.*?>.*?<\/style>(*SKIP)(*FAIL)';
|
2018-01-01 13:10:16 +00:00
|
|
|
// phpcs:ignore Generic.Files.LineLength
|
2016-02-11 22:08:03 +00:00
|
|
|
$noHtml = '<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)(*SKIP)(*FAIL)';
|
|
|
|
|
while ( $startPos < $length && $continue ) {
|
|
|
|
|
$continue = preg_match(
|
2023-10-02 12:56:25 +00:00
|
|
|
// Only match "-{" outside the html.
|
2016-02-11 22:08:03 +00:00
|
|
|
"/$noScript|$noStyle|$noHtml|-\{/",
|
|
|
|
|
$text,
|
|
|
|
|
$m,
|
|
|
|
|
PREG_OFFSET_CAPTURE,
|
|
|
|
|
$startPos
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( !$continue ) {
|
2010-01-19 02:36:33 +00:00
|
|
|
// No more markup, append final segment
|
2012-04-10 16:36:51 +00:00
|
|
|
$fragment = substr( $text, $startPos );
|
2013-04-17 19:10:02 +00:00
|
|
|
$out .= $shouldConvert ? $this->autoConvert( $fragment, $variant ) : $fragment;
|
2010-01-19 02:36:33 +00:00
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 22:08:03 +00:00
|
|
|
// Offset of the match of the regex pattern.
|
|
|
|
|
$pos = $m[0][1];
|
|
|
|
|
|
2010-01-19 02:36:33 +00:00
|
|
|
// Append initial segment
|
2012-04-10 16:36:51 +00:00
|
|
|
$fragment = substr( $text, $startPos, $pos - $startPos );
|
2013-04-17 19:10:02 +00:00
|
|
|
$out .= $shouldConvert ? $this->autoConvert( $fragment, $variant ) : $fragment;
|
2016-02-11 22:08:03 +00:00
|
|
|
// -{ marker found, not in attribute
|
|
|
|
|
// Advance position up to -{ marker.
|
2010-01-19 02:36:33 +00:00
|
|
|
$startPos = $pos;
|
|
|
|
|
// Do recursive conversion
|
2016-02-11 22:08:03 +00:00
|
|
|
// Note: This passes $startPos by reference, and advances it.
|
2010-01-19 02:36:33 +00:00
|
|
|
$out .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 14:46:27 +00:00
|
|
|
/**
|
|
|
|
|
* Recursively convert text on the inside.
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $text Text to be converted
|
|
|
|
|
* @param string $variant The target variant code
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param int &$startPos
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param int $depth Depth of recursion
|
|
|
|
|
* @return string Converted text
|
2010-07-07 14:46:27 +00:00
|
|
|
*/
|
2010-01-19 02:36:33 +00:00
|
|
|
protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
|
2021-11-19 23:19:42 +00:00
|
|
|
// Quick check (no function calls)
|
2010-01-19 02:36:33 +00:00
|
|
|
if ( $text[$startPos] !== '-' || $text[$startPos + 1] !== '{' ) {
|
2023-06-10 13:05:22 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ': invalid input string' );
|
2010-01-19 02:36:33 +00:00
|
|
|
}
|
2010-01-11 05:53:39 +00:00
|
|
|
|
2010-01-19 02:36:33 +00:00
|
|
|
$startPos += 2;
|
|
|
|
|
$inner = '';
|
|
|
|
|
$warningDone = false;
|
|
|
|
|
$length = strlen( $text );
|
|
|
|
|
|
|
|
|
|
while ( $startPos < $length ) {
|
|
|
|
|
$m = false;
|
2013-04-17 19:10:02 +00:00
|
|
|
preg_match( '/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
|
2010-01-19 02:36:33 +00:00
|
|
|
if ( !$m ) {
|
|
|
|
|
// Unclosed rule
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$token = $m[0][0];
|
|
|
|
|
$pos = $m[0][1];
|
|
|
|
|
|
|
|
|
|
// Markup found
|
|
|
|
|
// Append initial segment
|
|
|
|
|
$inner .= substr( $text, $startPos, $pos - $startPos );
|
|
|
|
|
|
|
|
|
|
// Advance position
|
|
|
|
|
$startPos = $pos;
|
|
|
|
|
|
|
|
|
|
switch ( $token ) {
|
|
|
|
|
case '-{':
|
|
|
|
|
// Check max depth
|
|
|
|
|
if ( $depth >= $this->mMaxDepth ) {
|
|
|
|
|
$inner .= '-{';
|
|
|
|
|
if ( !$warningDone ) {
|
|
|
|
|
$inner .= '<span class="error">' .
|
2012-08-20 14:55:28 +00:00
|
|
|
wfMessage( 'language-converter-depth-warning' )
|
|
|
|
|
->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
|
2010-01-19 02:36:33 +00:00
|
|
|
'</span>';
|
|
|
|
|
$warningDone = true;
|
|
|
|
|
}
|
|
|
|
|
$startPos += 2;
|
2018-08-23 16:18:07 +00:00
|
|
|
break;
|
2010-01-19 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
// Recursively parse another rule
|
|
|
|
|
$inner .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
|
|
|
|
|
break;
|
|
|
|
|
case '}-':
|
|
|
|
|
// Apply the rule
|
|
|
|
|
$startPos += 2;
|
|
|
|
|
$rule = new ConverterRule( $inner, $this );
|
|
|
|
|
$rule->parse( $variant );
|
|
|
|
|
$this->applyManualConv( $rule );
|
|
|
|
|
return $rule->getDisplay();
|
|
|
|
|
default:
|
2023-06-10 13:05:22 +00:00
|
|
|
throw new UnexpectedValueException( __METHOD__ . ': invalid regex match' );
|
2010-01-19 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-01-17 05:01:43 +00:00
|
|
|
|
2010-01-19 02:36:33 +00:00
|
|
|
// Unclosed rule
|
|
|
|
|
if ( $startPos < $length ) {
|
|
|
|
|
$inner .= substr( $text, $startPos );
|
|
|
|
|
}
|
|
|
|
|
$startPos = $length;
|
|
|
|
|
return '-{' . $this->autoConvert( $inner, $variant );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-22 21:34:19 +00:00
|
|
|
public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
|
2009-08-10 08:01:08 +00:00
|
|
|
# If the article has already existed, there is no need to
|
2023-10-02 12:56:25 +00:00
|
|
|
# check it again. Otherwise it may cause a fault.
|
2023-06-16 18:56:48 +00:00
|
|
|
if ( $nt instanceof LinkTarget ) {
|
|
|
|
|
$nt = Title::castFromLinkTarget( $nt );
|
|
|
|
|
if ( $nt->exists() ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $nt instanceof PageIdentity && $nt->exists() ) {
|
2009-08-10 08:01:08 +00:00
|
|
|
return;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2009-08-10 08:01:08 +00:00
|
|
|
|
2024-06-01 09:50:44 +00:00
|
|
|
$request = RequestContext::getMain()->getRequest();
|
2023-09-07 11:46:15 +00:00
|
|
|
|
2024-06-01 09:50:44 +00:00
|
|
|
$isredir = $request->getText( 'redirect', 'yes' );
|
|
|
|
|
$action = $request->getText( 'action' );
|
|
|
|
|
if ( $action == 'edit' && $request->getBool( 'redlink' ) ) {
|
2013-12-05 08:57:41 +00:00
|
|
|
$action = 'view';
|
|
|
|
|
}
|
2024-06-01 09:50:44 +00:00
|
|
|
$linkconvert = $request->getText( 'linkconvert', 'yes' );
|
2020-11-25 04:35:49 +00:00
|
|
|
$disableLinkConversion =
|
|
|
|
|
MediaWikiServices::getInstance()->getLanguageConverterFactory()
|
|
|
|
|
->isLinkConversionDisabled();
|
2020-08-07 22:22:37 +00:00
|
|
|
$linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
|
|
|
|
|
$linkBatch = $linkBatchFactory->newLinkBatch();
|
2006-12-11 23:33:27 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
$ns = NS_MAIN;
|
2006-12-11 23:33:27 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( $disableLinkConversion ||
|
2013-12-01 19:58:51 +00:00
|
|
|
( !$ignoreOtherCond &&
|
|
|
|
|
( $isredir == 'no'
|
|
|
|
|
|| $action == 'edit'
|
|
|
|
|
|| $action == 'submit'
|
2023-10-02 12:56:25 +00:00
|
|
|
|| $linkconvert == 'no' )
|
|
|
|
|
)
|
|
|
|
|
) {
|
2009-02-02 07:54:43 +00:00
|
|
|
return;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2008-08-07 14:26:34 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( is_object( $nt ) ) {
|
2005-04-28 03:49:23 +00:00
|
|
|
$ns = $nt->getNamespace();
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
$variants = $this->autoConvertToAllVariants( $link );
|
2010-06-09 11:44:05 +00:00
|
|
|
if ( !$variants ) { // give up
|
2005-04-15 14:12:39 +00:00
|
|
|
return;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2006-12-11 23:33:27 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [];
|
2006-12-11 23:33:27 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $variants as $v ) {
|
|
|
|
|
if ( $v != $link ) {
|
2006-12-11 23:33:27 +00:00
|
|
|
$varnt = Title::newFromText( $v, $ns );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $varnt !== null ) {
|
2010-01-05 09:07:12 +00:00
|
|
|
$linkBatch->addObj( $varnt );
|
|
|
|
|
$titles[] = $varnt;
|
2006-12-11 23:33:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fetch all variants in single query
|
|
|
|
|
$linkBatch->execute();
|
|
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $titles as $varnt ) {
|
|
|
|
|
if ( $varnt->getArticleID() > 0 ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$nt = $varnt;
|
2009-02-02 06:53:49 +00:00
|
|
|
$link = $varnt->getText();
|
2005-04-15 14:12:39 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-22 21:34:19 +00:00
|
|
|
public function getExtraHashOptions() {
|
2005-04-15 14:12:39 +00:00
|
|
|
$variant = $this->getPreferredVariant();
|
2014-04-21 18:44:54 +00:00
|
|
|
|
2011-03-22 20:23:52 +00:00
|
|
|
return '!' . $variant;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-27 11:23:52 +00:00
|
|
|
public function guessVariant( $text, $variant ) {
|
2011-11-16 15:12:00 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2010-01-05 09:07:12 +00:00
|
|
|
* Load default conversion tables.
|
2008-06-26 03:00:34 +00:00
|
|
|
*
|
2022-02-20 05:45:06 +00:00
|
|
|
* @return array
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2022-02-20 05:45:06 +00:00
|
|
|
abstract protected function loadDefaultTables(): array;
|
2005-04-15 14:12:39 +00:00
|
|
|
|
|
|
|
|
/**
|
2010-01-05 09:07:12 +00:00
|
|
|
* Load conversion tables either from the cache or the disk.
|
2008-06-26 03:00:34 +00:00
|
|
|
* @private
|
2022-02-20 05:45:06 +00:00
|
|
|
* @param bool $fromCache Whether to load from cache. Defaults to true.
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2019-11-16 23:22:43 +00:00
|
|
|
protected function loadTables( $fromCache = true ) {
|
2024-05-05 08:33:19 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$languageConverterCacheType = $services
|
2022-04-25 15:19:41 +00:00
|
|
|
->getMainConfig()->get( MainConfigNames::LanguageConverterCacheType );
|
2012-04-06 19:45:49 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( $this->mTablesLoaded ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
return;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2012-04-06 19:45:49 +00:00
|
|
|
|
2024-05-05 08:33:19 +00:00
|
|
|
$cache = $services->getObjectCacheFactory()->getInstance( $languageConverterCacheType );
|
2023-05-26 09:29:26 +00:00
|
|
|
$cacheKey = $cache->makeKey(
|
|
|
|
|
'conversiontables', $this->getMainCode(),
|
|
|
|
|
md5( implode( ',', $this->getVariants() ) ), self::CACHE_VERSION_KEY
|
|
|
|
|
);
|
2022-02-20 05:45:06 +00:00
|
|
|
if ( !$fromCache ) {
|
|
|
|
|
$cache->delete( $cacheKey );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2023-05-26 09:29:26 +00:00
|
|
|
$this->mTables = $cache->getWithSetCallback( $cacheKey, $cache::TTL_HOUR * 12, function () {
|
2011-03-22 20:23:52 +00:00
|
|
|
// We will first load the default tables
|
|
|
|
|
// then update them using things in MediaWiki:Conversiontable/*
|
2022-02-20 05:45:06 +00:00
|
|
|
$tables = $this->loadDefaultTables();
|
2021-01-29 11:23:29 +00:00
|
|
|
foreach ( $this->getVariants() as $var ) {
|
2010-01-05 09:07:12 +00:00
|
|
|
$cached = $this->parseCachedTable( $var );
|
2022-02-20 05:45:06 +00:00
|
|
|
$tables[$var]->mergeArray( $cached );
|
2006-10-16 02:36:28 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2022-02-20 05:45:06 +00:00
|
|
|
$this->postLoadTables( $tables );
|
|
|
|
|
return $tables;
|
|
|
|
|
} );
|
2025-03-18 00:27:46 +00:00
|
|
|
$this->mTablesLoaded = true;
|
2006-07-26 14:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-07 14:46:27 +00:00
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Hook for post-processing after conversion tables are loaded.
|
2022-02-20 05:45:06 +00:00
|
|
|
*
|
|
|
|
|
* @param ReplacementArray[] &$tables
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2022-02-20 05:45:06 +00:00
|
|
|
protected function postLoadTables( &$tables ) {
|
2014-04-21 18:44:54 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2010-07-07 14:46:27 +00:00
|
|
|
/**
|
2010-01-05 09:07:12 +00:00
|
|
|
* Reload the conversion tables.
|
2008-06-26 03:00:34 +00:00
|
|
|
*
|
2017-06-27 21:09:12 +00:00
|
|
|
* Also used by test suites which need to reset the converter state.
|
2017-06-26 17:56:30 +00:00
|
|
|
*
|
2021-08-17 19:15:15 +00:00
|
|
|
* Called by ParserTestRunner with the help of TestingAccessWrapper
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2017-06-27 21:09:12 +00:00
|
|
|
private function reloadTables() {
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( $this->mTables ) {
|
2022-02-20 04:09:31 +00:00
|
|
|
$this->mTables = [];
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2014-04-21 18:44:54 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
$this->mTablesLoaded = false;
|
2010-01-05 09:07:12 +00:00
|
|
|
$this->loadTables( false );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-05 09:07:12 +00:00
|
|
|
* Parse the conversion table stored in the cache.
|
2008-06-26 03:00:34 +00:00
|
|
|
*
|
2010-01-05 09:07:12 +00:00
|
|
|
* The tables should be in blocks of the following form:
|
2018-05-19 20:46:54 +00:00
|
|
|
* -{
|
|
|
|
|
* word => word ;
|
|
|
|
|
* word => word ;
|
|
|
|
|
* ...
|
|
|
|
|
* }-
|
2008-06-26 03:00:34 +00:00
|
|
|
*
|
2011-03-22 20:23:52 +00:00
|
|
|
* To make the tables more manageable, subpages are allowed
|
|
|
|
|
* and will be parsed recursively if $recursive == true.
|
2008-06-26 03:00:34 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $code Language code
|
|
|
|
|
* @param string $subpage Subpage name
|
|
|
|
|
* @param bool $recursive Parse subpages recursively? Defaults to true.
|
2011-05-29 15:59:47 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2005-04-15 14:12:39 +00:00
|
|
|
*/
|
2019-11-16 23:22:43 +00:00
|
|
|
private function parseCachedTable( $code, $subpage = '', $recursive = true ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
static $parsed = [];
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
$key = 'Conversiontable/' . $code;
|
|
|
|
|
if ( $subpage ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$key .= '/' . $subpage;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
|
|
|
|
if ( array_key_exists( $key, $parsed ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2009-06-03 14:51:08 +00:00
|
|
|
|
2011-11-26 15:12:23 +00:00
|
|
|
$parsed[$key] = true;
|
|
|
|
|
|
|
|
|
|
if ( $subpage === '' ) {
|
2020-02-27 19:36:57 +00:00
|
|
|
$messageCache = MediaWikiServices::getInstance()->getMessageCache();
|
|
|
|
|
$txt = $messageCache->getMsgFromNamespace( $key, $code );
|
2008-07-05 15:39:10 +00:00
|
|
|
} else {
|
2011-11-26 15:12:23 +00:00
|
|
|
$txt = false;
|
|
|
|
|
$title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $title && $title->exists() ) {
|
2020-04-15 21:50:29 +00:00
|
|
|
$revision = MediaWikiServices::getInstance()
|
|
|
|
|
->getRevisionLookup()
|
|
|
|
|
->getRevisionByTitle( $title );
|
2011-11-26 15:12:23 +00:00
|
|
|
if ( $revision ) {
|
2020-04-15 21:50:29 +00:00
|
|
|
$model = $revision->getSlot(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
RevisionRecord::RAW
|
|
|
|
|
)->getModel();
|
|
|
|
|
if ( $model == CONTENT_MODEL_WIKITEXT ) {
|
2019-08-31 16:14:38 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod
|
2020-04-15 21:50:29 +00:00
|
|
|
$txt = $revision->getContent(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
RevisionRecord::RAW
|
|
|
|
|
)->getText();
|
2012-06-08 06:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 01:12:35 +00:00
|
|
|
// @todo in the future, use a specialized content model, perhaps based on json!
|
2011-11-26 15:12:23 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-06-03 14:51:08 +00:00
|
|
|
|
2011-11-26 15:12:23 +00:00
|
|
|
# Nothing to parse if there's no text
|
|
|
|
|
if ( $txt === false || $txt === null || $txt === '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2011-11-26 15:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
// get all subpage links of the form
|
2011-03-22 20:23:52 +00:00
|
|
|
// [[MediaWiki:Conversiontable/zh-xx/...|...]]
|
2010-01-05 09:07:12 +00:00
|
|
|
$linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
|
|
|
|
|
':Conversiontable';
|
2010-01-19 02:36:33 +00:00
|
|
|
$subs = StringUtils::explode( '[[', $txt );
|
2016-02-17 09:09:32 +00:00
|
|
|
$sublinks = [];
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $subs as $sub ) {
|
|
|
|
|
$link = explode( ']]', $sub, 2 );
|
|
|
|
|
if ( count( $link ) != 2 ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
continue;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2010-01-19 02:36:33 +00:00
|
|
|
$b = explode( '|', $link[0], 2 );
|
2010-01-05 09:07:12 +00:00
|
|
|
$b = explode( '/', trim( $b[0] ), 3 );
|
|
|
|
|
if ( count( $b ) == 3 ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$sublink = $b[2];
|
2010-01-05 09:07:12 +00:00
|
|
|
} else {
|
2005-04-15 14:12:39 +00:00
|
|
|
$sublink = '';
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( $b[0] == $linkhead && $b[1] == $code ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$sublinks[] = $sublink;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse the mappings in this page
|
2010-01-19 02:36:33 +00:00
|
|
|
$blocks = StringUtils::explode( '-{', $txt );
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2010-01-19 02:36:33 +00:00
|
|
|
$first = true;
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $blocks as $block ) {
|
2010-01-19 02:36:33 +00:00
|
|
|
if ( $first ) {
|
|
|
|
|
// Skip the part before the first -{
|
|
|
|
|
$first = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-02-17 19:54:59 +00:00
|
|
|
$mappings = explode( '}-', $block, 2 )[0];
|
|
|
|
|
$stripped = str_replace( [ "'", '"', '*', '#' ], '', $mappings );
|
2010-01-19 02:36:33 +00:00
|
|
|
$table = StringUtils::explode( ';', $stripped );
|
2010-01-05 09:07:12 +00:00
|
|
|
foreach ( $table as $t ) {
|
2010-01-19 02:36:33 +00:00
|
|
|
$m = explode( '=>', $t, 3 );
|
2011-03-22 20:23:52 +00:00
|
|
|
if ( count( $m ) != 2 ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
continue;
|
2011-03-22 20:23:52 +00:00
|
|
|
}
|
2022-01-09 17:24:04 +00:00
|
|
|
// trim any trailing comments starting with '//'
|
2010-01-05 09:07:12 +00:00
|
|
|
$tt = explode( '//', $m[1], 2 );
|
|
|
|
|
$ret[trim( $m[0] )] = trim( $tt[0] );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// recursively parse the subpages
|
2010-01-05 09:07:12 +00:00
|
|
|
if ( $recursive ) {
|
|
|
|
|
foreach ( $sublinks as $link ) {
|
|
|
|
|
$s = $this->parseCachedTable( $code, $link, $recursive );
|
2014-05-06 16:16:12 +00:00
|
|
|
$ret = $s + $ret;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-22 21:34:19 +00:00
|
|
|
public function markNoConversion( $text, $noParse = false ) {
|
2005-04-20 02:33:04 +00:00
|
|
|
# don't mark if already marked
|
2024-01-25 15:29:36 +00:00
|
|
|
if ( str_contains( $text, '-{' ) || str_contains( $text, '}-' ) ) {
|
2005-04-20 02:33:04 +00:00
|
|
|
return $text;
|
2010-01-05 09:07:12 +00:00
|
|
|
}
|
2005-04-20 02:33:04 +00:00
|
|
|
|
2023-10-02 12:56:25 +00:00
|
|
|
return "-{R|$text}-";
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-16 23:22:43 +00:00
|
|
|
public function convertCategoryKey( $key ) {
|
2005-04-28 03:49:23 +00:00
|
|
|
return $key;
|
|
|
|
|
}
|
2010-01-05 09:07:12 +00:00
|
|
|
|
2019-07-22 09:49:57 +00:00
|
|
|
public function updateConversionTable( LinkTarget $linkTarget ) {
|
2020-07-22 17:29:48 +00:00
|
|
|
if ( $linkTarget->getNamespace() === NS_MEDIAWIKI ) {
|
2019-07-22 09:49:57 +00:00
|
|
|
$t = explode( '/', $linkTarget->getDBkey(), 3 );
|
2010-01-05 09:07:12 +00:00
|
|
|
$c = count( $t );
|
2023-10-02 12:56:25 +00:00
|
|
|
if ( $c > 1 && $t[0] == 'Conversiontable' && $this->validateVariant( $t[1] ) ) {
|
|
|
|
|
$this->reloadTables();
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2010-01-19 02:36:33 +00:00
|
|
|
/**
|
|
|
|
|
* Get the cached separator pattern for ConverterRule::parseRules()
|
2014-08-25 16:50:35 +00:00
|
|
|
* @return string
|
2010-01-19 02:36:33 +00:00
|
|
|
*/
|
2019-11-16 23:22:43 +00:00
|
|
|
public function getVarSeparatorPattern() {
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $this->mVarSeparatorPattern === null ) {
|
2010-01-19 02:36:33 +00:00
|
|
|
// varsep_pattern for preg_split:
|
2023-10-02 12:56:25 +00:00
|
|
|
// The text should be split by ";" only if a valid variant
|
|
|
|
|
// name exists after the markup.
|
|
|
|
|
// For example
|
2010-01-19 02:36:33 +00:00
|
|
|
// -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:\
|
2017-02-25 21:53:36 +00:00
|
|
|
// <span style="font-size:120%;">yyy</span>;}-
|
2010-01-19 02:36:33 +00:00
|
|
|
// we should split it as:
|
2016-09-27 03:15:08 +00:00
|
|
|
// [
|
2017-02-25 21:53:36 +00:00
|
|
|
// [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>'
|
|
|
|
|
// [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
|
|
|
|
|
// [2] => ''
|
2016-09-27 03:15:08 +00:00
|
|
|
// ]
|
2018-07-13 19:40:20 +00:00
|
|
|
$expandedVariants = [];
|
2021-01-29 11:23:29 +00:00
|
|
|
foreach ( $this->getVariants() as $variant ) {
|
2018-07-13 19:40:20 +00:00
|
|
|
$expandedVariants[ $variant ] = 1;
|
|
|
|
|
// Accept standard BCP 47 names for variants as well.
|
|
|
|
|
$expandedVariants[ LanguageCode::bcp47( $variant ) ] = 1;
|
|
|
|
|
}
|
|
|
|
|
// Accept old deprecated names for variants
|
|
|
|
|
foreach ( LanguageCode::getDeprecatedCodeMapping() as $old => $new ) {
|
|
|
|
|
if ( isset( $expandedVariants[ $new ] ) ) {
|
|
|
|
|
$expandedVariants[ $old ] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-26 09:01:48 +00:00
|
|
|
$expandedVariants = implode( '|', array_keys( $expandedVariants ) );
|
2018-07-13 19:40:20 +00:00
|
|
|
|
|
|
|
|
$pat = '/;\s*(?=';
|
2022-03-26 09:01:48 +00:00
|
|
|
// zh-hans:xxx;zh-hant:yyy
|
|
|
|
|
$pat .= '(?:' . $expandedVariants . ')\s*:';
|
|
|
|
|
// xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
|
|
|
|
|
$pat .= '|[^;]*?=>\s*(?:' . $expandedVariants . ')\s*:';
|
|
|
|
|
$pat .= '|\s*$)/';
|
2010-01-19 02:36:33 +00:00
|
|
|
$this->mVarSeparatorPattern = $pat;
|
|
|
|
|
}
|
|
|
|
|
return $this->mVarSeparatorPattern;
|
|
|
|
|
}
|
2020-01-23 18:39:23 +00:00
|
|
|
|
|
|
|
|
public function hasVariants() {
|
|
|
|
|
return count( $this->getVariants() ) > 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasVariant( $variant ) {
|
|
|
|
|
return $variant && ( $variant === $this->validateVariant( $variant ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function convertHtml( $text ) {
|
2020-12-30 18:38:48 +00:00
|
|
|
// @phan-suppress-next-line SecurityCheck-DoubleEscaped convert() is documented to return html
|
2020-01-23 18:39:23 +00:00
|
|
|
return htmlspecialchars( $this->convert( $text ) );
|
|
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2024-08-08 09:39:26 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( LanguageConverter::class, 'LanguageConverter' );
|