2022-03-25 20:34:04 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2022-03-25 20:36:10 +00:00
|
|
|
* Copyright (C) 2011-2022 Wikimedia Foundation and others.
|
2022-03-25 20:34: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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// NO_PRELOAD -- anonymous class in parent
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Parser\Parsoid\Config;
|
|
|
|
|
|
|
|
|
|
use Language;
|
2023-02-02 19:42:58 +00:00
|
|
|
use LanguageCode;
|
2022-03-25 20:34:04 +00:00
|
|
|
use LanguageConverter;
|
|
|
|
|
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\Config;
|
|
|
|
|
use MediaWiki\Config\MutableConfig;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-05-22 14:08:45 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWiki\Interwiki\InterwikiLookup;
|
|
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
|
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
|
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-10 15:34:45 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-12-09 12:28:41 +00:00
|
|
|
use MediaWiki\Parser\MagicWordArray;
|
|
|
|
|
use MediaWiki\Parser\MagicWordFactory;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2022-03-25 20:34:04 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2023-09-18 14:17:28 +00:00
|
|
|
use MediaWiki\Title\NamespaceInfo;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-11-29 10:21:43 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2022-08-26 19:57:04 +00:00
|
|
|
use MediaWiki\Utils\UrlUtils;
|
2023-02-23 20:44:38 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2024-05-22 14:08:45 +00:00
|
|
|
use MWUnknownContentModelException;
|
2023-07-29 10:48:43 +00:00
|
|
|
use ParserFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
use PrefixingStatsdDataFactoryProxy;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use UnexpectedValueException;
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
use Wikimedia\Bcp47Code\Bcp47Code;
|
2022-03-25 20:34:04 +00:00
|
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
|
|
|
|
use Wikimedia\Parsoid\Config\SiteConfig as ISiteConfig;
|
|
|
|
|
use Wikimedia\Parsoid\Core\ContentMetadataCollector;
|
|
|
|
|
use Wikimedia\Parsoid\DOM\Document;
|
|
|
|
|
use Wikimedia\Parsoid\Utils\Utils;
|
2024-07-08 13:43:18 +00:00
|
|
|
use Wikimedia\Stats\StatsFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Site-level configuration for Parsoid
|
|
|
|
|
*
|
|
|
|
|
* This includes both global configuration and wiki-level configuration.
|
|
|
|
|
*
|
2022-03-25 20:41:24 +00:00
|
|
|
* @since 1.39
|
2023-03-07 22:30:54 +00:00
|
|
|
* @internal
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
|
|
|
|
class SiteConfig extends ISiteConfig {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regular expression fragment for matching wikitext comments.
|
|
|
|
|
* Meant for inclusion in other regular expressions.
|
|
|
|
|
*/
|
|
|
|
|
protected const COMMENT_REGEXP_FRAGMENT = '<!--(?>[\s\S]*?-->)';
|
|
|
|
|
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
2022-04-26 15:48:03 +00:00
|
|
|
MainConfigNames::GalleryOptions,
|
|
|
|
|
MainConfigNames::AllowExternalImages,
|
|
|
|
|
MainConfigNames::AllowExternalImagesFrom,
|
|
|
|
|
MainConfigNames::Server,
|
|
|
|
|
MainConfigNames::ArticlePath,
|
|
|
|
|
MainConfigNames::InterwikiMagic,
|
|
|
|
|
MainConfigNames::ExtraInterlanguageLinkPrefixes,
|
2023-02-02 19:42:58 +00:00
|
|
|
MainConfigNames::InterlanguageLinkCodeMap,
|
2022-04-26 15:48:03 +00:00
|
|
|
MainConfigNames::LocalInterwikis,
|
|
|
|
|
MainConfigNames::LanguageCode,
|
|
|
|
|
MainConfigNames::NamespaceAliases,
|
|
|
|
|
MainConfigNames::UrlProtocols,
|
|
|
|
|
MainConfigNames::Script,
|
|
|
|
|
MainConfigNames::ScriptPath,
|
|
|
|
|
MainConfigNames::LoadScript,
|
|
|
|
|
MainConfigNames::LocalTZoffset,
|
|
|
|
|
MainConfigNames::ThumbLimits,
|
|
|
|
|
MainConfigNames::MaxTemplateDepth,
|
2022-06-08 14:41:35 +00:00
|
|
|
MainConfigNames::NoFollowLinks,
|
|
|
|
|
MainConfigNames::NoFollowNsExceptions,
|
|
|
|
|
MainConfigNames::NoFollowDomainExceptions,
|
|
|
|
|
MainConfigNames::ExternalLinkTarget,
|
2024-02-16 17:03:44 +00:00
|
|
|
MainConfigNames::EnableMagicLinks,
|
2022-03-25 20:34:04 +00:00
|
|
|
];
|
|
|
|
|
|
2023-10-23 21:22:54 +00:00
|
|
|
private ServiceOptions $config;
|
|
|
|
|
private Config $mwConfig;
|
|
|
|
|
/** Parsoid-specific options array from $config */
|
|
|
|
|
private array $parsoidSettings;
|
|
|
|
|
private Language $contLang;
|
|
|
|
|
private StatsdDataFactoryInterface $stats;
|
2024-07-08 13:43:18 +00:00
|
|
|
private StatsFactory $statsFactory;
|
2023-10-23 21:22:54 +00:00
|
|
|
private MagicWordFactory $magicWordFactory;
|
|
|
|
|
private NamespaceInfo $namespaceInfo;
|
|
|
|
|
private SpecialPageFactory $specialPageFactory;
|
|
|
|
|
private InterwikiLookup $interwikiLookup;
|
|
|
|
|
private ParserFactory $parserFactory;
|
|
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
|
|
|
|
private ObjectFactory $objectFactory;
|
|
|
|
|
private LanguageFactory $languageFactory;
|
|
|
|
|
private LanguageConverterFactory $languageConverterFactory;
|
|
|
|
|
private LanguageNameUtils $languageNameUtils;
|
|
|
|
|
private UrlUtils $urlUtils;
|
2024-05-22 14:08:45 +00:00
|
|
|
private IContentHandlerFactory $contentHandlerFactory;
|
2023-10-23 21:22:54 +00:00
|
|
|
private ?string $baseUri = null;
|
|
|
|
|
private ?string $relativeLinkPrefix = null;
|
|
|
|
|
private ?array $interwikiMap = null;
|
|
|
|
|
private ?array $variants = null;
|
|
|
|
|
private ?array $extensionTags = null;
|
2023-07-15 14:51:26 +00:00
|
|
|
private bool $isTimedMediaHandlerLoaded;
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
/**
|
|
|
|
|
* @param ServiceOptions $config MediaWiki main configuration object
|
|
|
|
|
* @param array $parsoidSettings Parsoid-specific options array from main configuration.
|
|
|
|
|
* @param ObjectFactory $objectFactory
|
|
|
|
|
* @param Language $contentLanguage Content language.
|
|
|
|
|
* @param StatsdDataFactoryInterface $stats
|
2024-07-08 13:43:18 +00:00
|
|
|
* @param StatsFactory $statsFactory
|
2022-03-25 20:34:04 +00:00
|
|
|
* @param MagicWordFactory $magicWordFactory
|
|
|
|
|
* @param NamespaceInfo $namespaceInfo
|
|
|
|
|
* @param SpecialPageFactory $specialPageFactory
|
|
|
|
|
* @param InterwikiLookup $interwikiLookup
|
|
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
|
|
|
|
* @param LanguageFactory $languageFactory
|
|
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
|
|
|
|
* @param LanguageNameUtils $languageNameUtils
|
2022-08-26 19:57:04 +00:00
|
|
|
* @param UrlUtils $urlUtils
|
2024-05-22 14:08:45 +00:00
|
|
|
* @param IContentHandlerFactory $contentHandlerFactory
|
2023-07-15 14:51:26 +00:00
|
|
|
* @param array $extensionParsoidModules
|
2023-07-29 10:48:43 +00:00
|
|
|
* @param ParserFactory $parserFactory
|
SiteConfig: Transition away from hardcoded Cite info
* This is step 1 of the process.
* In step 2, we'll update Parsoid's SiteConfig abstract class
and its subclasses with this same method.
* In step 3, we'll transition Cite in Parsoid repo to use the
new method.
* In step 4, we'll remove the responsiveReferences method from
SiteConfig
* In step 5, we'll remove the responsiveReferences method here.
* Multiple steps could be deployed on the same train.
- Aggressive: All steps on the same train.
- Less aggressive: 1,2,3 on one train, 4,5 on another train.
- Conservative: 2 on first train, 3,4 on next, 4,5 on following.
Bug: T268777
Change-Id: Ia489cd880de3df73d7cd47d1eb01971f2655effe
2022-10-03 22:56:13 +00:00
|
|
|
* @param Config $mwConfig
|
2023-07-15 14:51:26 +00:00
|
|
|
* @param bool $isTimedMediaHandlerLoaded
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
ServiceOptions $config,
|
|
|
|
|
array $parsoidSettings,
|
|
|
|
|
ObjectFactory $objectFactory,
|
|
|
|
|
Language $contentLanguage,
|
|
|
|
|
StatsdDataFactoryInterface $stats,
|
2024-07-08 13:43:18 +00:00
|
|
|
StatsFactory $statsFactory,
|
2022-03-25 20:34:04 +00:00
|
|
|
MagicWordFactory $magicWordFactory,
|
|
|
|
|
NamespaceInfo $namespaceInfo,
|
|
|
|
|
SpecialPageFactory $specialPageFactory,
|
|
|
|
|
InterwikiLookup $interwikiLookup,
|
|
|
|
|
UserOptionsLookup $userOptionsLookup,
|
|
|
|
|
LanguageFactory $languageFactory,
|
|
|
|
|
LanguageConverterFactory $languageConverterFactory,
|
|
|
|
|
LanguageNameUtils $languageNameUtils,
|
2022-08-26 19:57:04 +00:00
|
|
|
UrlUtils $urlUtils,
|
2024-05-22 14:08:45 +00:00
|
|
|
IContentHandlerFactory $contentHandlerFactory,
|
2023-07-15 14:51:26 +00:00
|
|
|
array $extensionParsoidModules,
|
2023-07-29 10:48:43 +00:00
|
|
|
// $parserFactory is temporary and may be removed once a better solution is found.
|
|
|
|
|
ParserFactory $parserFactory, // T268776
|
2023-07-15 14:51:26 +00:00
|
|
|
Config $mwConfig,
|
|
|
|
|
bool $isTimedMediaHandlerLoaded
|
2022-03-25 20:34:04 +00:00
|
|
|
) {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
|
|
$config->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->config = $config;
|
SiteConfig: Transition away from hardcoded Cite info
* This is step 1 of the process.
* In step 2, we'll update Parsoid's SiteConfig abstract class
and its subclasses with this same method.
* In step 3, we'll transition Cite in Parsoid repo to use the
new method.
* In step 4, we'll remove the responsiveReferences method from
SiteConfig
* In step 5, we'll remove the responsiveReferences method here.
* Multiple steps could be deployed on the same train.
- Aggressive: All steps on the same train.
- Less aggressive: 1,2,3 on one train, 4,5 on another train.
- Conservative: 2 on first train, 3,4 on next, 4,5 on following.
Bug: T268777
Change-Id: Ia489cd880de3df73d7cd47d1eb01971f2655effe
2022-10-03 22:56:13 +00:00
|
|
|
$this->mwConfig = $mwConfig;
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->parsoidSettings = $parsoidSettings;
|
|
|
|
|
|
|
|
|
|
$this->objectFactory = $objectFactory;
|
|
|
|
|
$this->contLang = $contentLanguage;
|
|
|
|
|
$this->stats = $stats;
|
2024-07-08 13:43:18 +00:00
|
|
|
$this->statsFactory = $statsFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->magicWordFactory = $magicWordFactory;
|
|
|
|
|
$this->namespaceInfo = $namespaceInfo;
|
|
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
|
|
|
|
$this->interwikiLookup = $interwikiLookup;
|
2023-07-29 10:48:43 +00:00
|
|
|
$this->parserFactory = $parserFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
|
|
|
|
$this->languageFactory = $languageFactory;
|
|
|
|
|
$this->languageConverterFactory = $languageConverterFactory;
|
|
|
|
|
$this->languageNameUtils = $languageNameUtils;
|
2022-08-26 19:57:04 +00:00
|
|
|
$this->urlUtils = $urlUtils;
|
2024-05-22 14:08:45 +00:00
|
|
|
$this->contentHandlerFactory = $contentHandlerFactory;
|
2022-03-25 20:34:04 +00:00
|
|
|
|
|
|
|
|
// Override parent default
|
|
|
|
|
if ( isset( $this->parsoidSettings['linting'] ) ) {
|
2022-02-21 16:28:35 +00:00
|
|
|
// @todo: Add this setting to MW's MainConfigSchema
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->linterEnabled = $this->parsoidSettings['linting'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $this->parsoidSettings['wt2htmlLimits'] ) ) {
|
|
|
|
|
$this->wt2htmlLimits = $this->parsoidSettings['wt2htmlLimits'] + $this->wt2htmlLimits;
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $this->parsoidSettings['html2wtLimits'] ) ) {
|
|
|
|
|
$this->html2wtLimits = $this->parsoidSettings['html2wtLimits'] + $this->html2wtLimits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register extension modules
|
2023-07-15 14:51:26 +00:00
|
|
|
foreach ( $extensionParsoidModules as $configOrSpec ) {
|
2022-03-25 20:34:04 +00:00
|
|
|
$this->registerExtensionModule( $configOrSpec );
|
|
|
|
|
}
|
2023-07-15 14:51:26 +00:00
|
|
|
|
|
|
|
|
$this->isTimedMediaHandlerLoaded = $isTimedMediaHandlerLoaded;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getObjectFactory(): ObjectFactory {
|
|
|
|
|
return $this->objectFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getLogger(): LoggerInterface {
|
|
|
|
|
// TODO: inject
|
|
|
|
|
if ( $this->logger === null ) {
|
|
|
|
|
$this->logger = LoggerFactory::getInstance( 'Parsoid' );
|
|
|
|
|
}
|
|
|
|
|
return $this->logger;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 13:43:18 +00:00
|
|
|
/**
|
|
|
|
|
* Get stats prefix
|
|
|
|
|
* @param bool $trimmed Trim trailing dot on prefix name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getStatsPrefix( bool $trimmed = false ): string {
|
|
|
|
|
$component = $this->parsoidSettings['metricsPrefix'] ?? 'Parsoid.';
|
|
|
|
|
if ( $trimmed ) {
|
|
|
|
|
$component = rtrim( $component, '.' );
|
|
|
|
|
}
|
|
|
|
|
return $component;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
public function metrics(): ?StatsdDataFactoryInterface {
|
|
|
|
|
// TODO: inject
|
|
|
|
|
static $prefixedMetrics = null;
|
|
|
|
|
if ( $prefixedMetrics === null ) {
|
|
|
|
|
$prefixedMetrics = new PrefixingStatsdDataFactoryProxy(
|
|
|
|
|
// Our stats will also get prefixed with 'MediaWiki.'
|
|
|
|
|
$this->stats,
|
2024-07-08 13:43:18 +00:00
|
|
|
$this->getStatsPrefix()
|
2022-03-25 20:34:04 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $prefixedMetrics;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 13:43:18 +00:00
|
|
|
/**
|
|
|
|
|
* Record a timing metric
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param float $value
|
|
|
|
|
* @param array $labels
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function observeTiming( string $name, float $value, array $labels ) {
|
|
|
|
|
$component = $this->getStatsPrefix( true );
|
|
|
|
|
$metric = $this->statsFactory->withComponent( $component )->getTiming( $name );
|
|
|
|
|
foreach ( $labels as $labelKey => $labelValue ) {
|
|
|
|
|
$metric->setLabel( $labelKey, $labelValue );
|
|
|
|
|
}
|
|
|
|
|
$metric->observe( $value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Increment a counter metric
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $labels
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function incrementCounter( string $name, array $labels ) {
|
|
|
|
|
$component = $this->getStatsPrefix( true );
|
|
|
|
|
$metric = $this->statsFactory->withComponent( $component )->getCounter( $name );
|
|
|
|
|
foreach ( $labels as $labelKey => $labelValue ) {
|
|
|
|
|
$metric->setLabel( $labelKey, $labelValue );
|
|
|
|
|
}
|
|
|
|
|
$metric->increment();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
public function galleryOptions(): array {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::GalleryOptions );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function allowedExternalImagePrefixes(): array {
|
2022-04-10 15:34:45 +00:00
|
|
|
if ( $this->config->get( MainConfigNames::AllowExternalImages ) ) {
|
2022-03-25 20:34:04 +00:00
|
|
|
return [ '' ];
|
|
|
|
|
} else {
|
2022-04-10 15:34:45 +00:00
|
|
|
$allowFrom = $this->config->get( MainConfigNames::AllowExternalImagesFrom );
|
2022-03-25 20:34:04 +00:00
|
|
|
return $allowFrom ? (array)$allowFrom : [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine the article base URI and relative prefix
|
|
|
|
|
*
|
|
|
|
|
* Populates `$this->baseUri` and `$this->relativeLinkPrefix` based on
|
|
|
|
|
* `$wgServer` and `$wgArticlePath`, by splitting it at the last '/' in the
|
|
|
|
|
* path portion.
|
|
|
|
|
*/
|
|
|
|
|
private function determineArticlePath(): void {
|
2022-04-10 15:34:45 +00:00
|
|
|
$url = $this->config->get( MainConfigNames::Server ) .
|
|
|
|
|
$this->config->get( MainConfigNames::ArticlePath );
|
2022-03-25 20:34:04 +00:00
|
|
|
|
|
|
|
|
if ( substr( $url, -2 ) !== '$1' ) {
|
|
|
|
|
throw new UnexpectedValueException( "Article path '$url' does not have '$1' at the end" );
|
|
|
|
|
}
|
|
|
|
|
$url = substr( $url, 0, -2 );
|
|
|
|
|
|
2023-08-05 15:41:35 +00:00
|
|
|
$bits = $this->urlUtils->parse( $url );
|
2022-03-25 20:34:04 +00:00
|
|
|
if ( !$bits ) {
|
|
|
|
|
throw new UnexpectedValueException( "Failed to parse article path '$url'" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( empty( $bits['path'] ) ) {
|
|
|
|
|
$path = '/';
|
|
|
|
|
} else {
|
|
|
|
|
$path = wfRemoveDotSegments( $bits['path'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$relParts = [ 'query' => true, 'fragment' => true ];
|
|
|
|
|
$base = array_diff_key( $bits, $relParts );
|
|
|
|
|
$rel = array_intersect_key( $bits, $relParts );
|
|
|
|
|
|
|
|
|
|
$i = strrpos( $path, '/' );
|
|
|
|
|
$base['path'] = substr( $path, 0, $i + 1 );
|
|
|
|
|
$rel['path'] = '.' . substr( $path, $i );
|
|
|
|
|
|
|
|
|
|
$this->baseUri = wfAssembleUrl( $base );
|
|
|
|
|
$this->relativeLinkPrefix = wfAssembleUrl( $rel );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function baseURI(): string {
|
|
|
|
|
if ( $this->baseUri === null ) {
|
|
|
|
|
$this->determineArticlePath();
|
|
|
|
|
}
|
|
|
|
|
return $this->baseUri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function relativeLinkPrefix(): string {
|
|
|
|
|
if ( $this->relativeLinkPrefix === null ) {
|
|
|
|
|
$this->determineArticlePath();
|
|
|
|
|
}
|
|
|
|
|
return $this->relativeLinkPrefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is very similar to MagicWordArray::getBaseRegex() except we
|
|
|
|
|
* don't emit the named grouping constructs, which can cause havoc
|
|
|
|
|
* when embedded in other regexps with grouping constructs.
|
|
|
|
|
*
|
|
|
|
|
* @param MagicWordArray $magicWordArray
|
|
|
|
|
* @param string $delimiter
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function mwaToRegex(
|
|
|
|
|
MagicWordArray $magicWordArray,
|
|
|
|
|
string $delimiter = '/'
|
|
|
|
|
): string {
|
|
|
|
|
return implode( '|', $magicWordArray->getBaseRegex( false, $delimiter ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function redirectRegexp(): string {
|
|
|
|
|
$redirect = self::mwaToRegex( $this->magicWordFactory->newArray( [ 'redirect' ] ), '@' );
|
|
|
|
|
return "@$redirect@Su";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function categoryRegexp(): string {
|
|
|
|
|
$canon = $this->namespaceInfo->getCanonicalName( NS_CATEGORY );
|
|
|
|
|
$result = [ $canon ];
|
|
|
|
|
foreach ( $this->contLang->getNamespaceAliases() as $alias => $ns ) {
|
|
|
|
|
if ( $ns === NS_CATEGORY && $alias !== $canon ) {
|
|
|
|
|
$result[] = $alias;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$category = implode( '|', array_map( function ( $v ) {
|
|
|
|
|
return $this->quoteTitleRe( $v, '@' );
|
|
|
|
|
}, $result ) );
|
|
|
|
|
return "@(?i:$category)@";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function bswRegexp(): string {
|
|
|
|
|
$bsw = self::mwaToRegex( $this->magicWordFactory->getDoubleUnderscoreArray(), '@' );
|
|
|
|
|
// Aliases for double underscore mws include the underscores
|
|
|
|
|
// So, strip them since the base regexp will have included them
|
|
|
|
|
// and they aren't expected at the use sites of bswRegexp
|
|
|
|
|
$bsw = str_replace( '__', '', $bsw );
|
|
|
|
|
return "@$bsw@Su";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function canonicalNamespaceId( string $name ): ?int {
|
|
|
|
|
$ret = $this->namespaceInfo->getCanonicalIndex( $name );
|
|
|
|
|
return $ret === false ? null : $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function namespaceId( string $name ): ?int {
|
|
|
|
|
$ret = $this->contLang->getNsIndex( $name );
|
|
|
|
|
return $ret === false ? null : $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function namespaceName( int $ns ): ?string {
|
|
|
|
|
$ret = $this->contLang->getFormattedNsText( $ns );
|
|
|
|
|
return $ret === '' && $ns !== NS_MAIN ? null : $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function namespaceHasSubpages( int $ns ): bool {
|
|
|
|
|
return $this->namespaceInfo->hasSubpages( $ns );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function namespaceCase( int $ns ): string {
|
|
|
|
|
return $this->namespaceInfo->isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function namespaceIsTalk( int $ns ): bool {
|
|
|
|
|
return $this->namespaceInfo->isTalk( $ns );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function ucfirst( string $str ): string {
|
|
|
|
|
return $this->contLang->ucfirst( $str );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function specialPageLocalName( string $alias ): ?string {
|
|
|
|
|
$aliases = $this->specialPageFactory->resolveAlias( $alias );
|
|
|
|
|
return $aliases[0] !== null ? $this->specialPageFactory->getLocalNameFor( ...$aliases ) : $alias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function interwikiMagic(): bool {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::InterwikiMagic );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-16 17:03:44 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function magicLinkEnabled( string $which ): bool {
|
|
|
|
|
$m = $this->config->get( MainConfigNames::EnableMagicLinks );
|
|
|
|
|
return $m[$which] ?? true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
public function interwikiMap(): array {
|
|
|
|
|
// Unfortunate that this mostly duplicates \ApiQuerySiteinfo::appendInterwikiMap()
|
2022-03-25 20:53:02 +00:00
|
|
|
if ( $this->interwikiMap !== null ) {
|
|
|
|
|
return $this->interwikiMap;
|
|
|
|
|
}
|
|
|
|
|
$this->interwikiMap = [];
|
|
|
|
|
|
|
|
|
|
$getPrefixes = $this->interwikiLookup->getAllPrefixes();
|
|
|
|
|
$langNames = $this->languageNameUtils->getLanguageNames();
|
2022-04-10 15:34:45 +00:00
|
|
|
$extraLangPrefixes = $this->config->get( MainConfigNames::ExtraInterlanguageLinkPrefixes );
|
2023-02-02 19:42:58 +00:00
|
|
|
$extraLangCodeMap = $this->config->get( MainConfigNames::InterlanguageLinkCodeMap );
|
2022-04-10 15:34:45 +00:00
|
|
|
$localInterwikis = $this->config->get( MainConfigNames::LocalInterwikis );
|
2022-03-25 20:53:02 +00:00
|
|
|
|
|
|
|
|
foreach ( $getPrefixes as $row ) {
|
|
|
|
|
$prefix = $row['iw_prefix'];
|
|
|
|
|
$val = [];
|
|
|
|
|
$val['prefix'] = $prefix;
|
|
|
|
|
// ApiQuerySiteInfo::appendInterwikiMap uses PROTO_CURRENT here,
|
|
|
|
|
// but that's the 'current' protocol *of the API request*; use
|
|
|
|
|
// PROTO_CANONICAL instead.
|
2022-08-26 19:57:04 +00:00
|
|
|
$val['url'] = $this->urlUtils->expand( $row['iw_url'], PROTO_CANONICAL ) ?? false;
|
2022-03-25 20:53:02 +00:00
|
|
|
|
|
|
|
|
// Fix up broken interwiki hrefs that are missing a $1 placeholder
|
|
|
|
|
// Just append the placeholder at the end.
|
|
|
|
|
// This makes sure that the interwikiMatcher adds one match
|
|
|
|
|
// group per URI, and that interwiki links work as expected.
|
2023-10-04 10:52:33 +00:00
|
|
|
if ( !str_contains( $val['url'], '$1' ) ) {
|
2022-03-25 20:53:02 +00:00
|
|
|
$val['url'] .= '$1';
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
|
2023-10-04 10:52:33 +00:00
|
|
|
if ( str_starts_with( $row['iw_url'], '//' ) ) {
|
2022-03-25 20:53:02 +00:00
|
|
|
$val['protorel'] = true;
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $row['iw_local'] ) && $row['iw_local'] == '1' ) {
|
|
|
|
|
$val['local'] = true;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
2022-03-25 20:53:02 +00:00
|
|
|
if ( isset( $langNames[$prefix] ) ) {
|
|
|
|
|
$val['language'] = true;
|
2023-02-02 19:42:58 +00:00
|
|
|
$standard = LanguageCode::replaceDeprecatedCodes( $prefix );
|
|
|
|
|
if ( $standard !== $prefix ) {
|
|
|
|
|
# Note that even if this code is deprecated, it should
|
|
|
|
|
# only be remapped if extralanglink (set below) is false.
|
|
|
|
|
$val['deprecated'] = $standard;
|
|
|
|
|
}
|
|
|
|
|
$val['bcp47'] = LanguageCode::bcp47( $standard );
|
2022-03-25 20:53:02 +00:00
|
|
|
}
|
|
|
|
|
if ( in_array( $prefix, $localInterwikis, true ) ) {
|
|
|
|
|
$val['localinterwiki'] = true;
|
|
|
|
|
}
|
|
|
|
|
if ( in_array( $prefix, $extraLangPrefixes, true ) ) {
|
|
|
|
|
$val['extralanglink'] = true;
|
2023-02-02 19:42:58 +00:00
|
|
|
$val['code'] = $extraLangCodeMap[$prefix] ?? $prefix;
|
|
|
|
|
$val['bcp47'] = LanguageCode::bcp47( $val['code'] );
|
2022-03-25 20:53:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->interwikiMap[$prefix] = $val;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
return $this->interwikiMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function iwp(): string {
|
|
|
|
|
return WikiMap::getCurrentWikiId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function legalTitleChars(): string {
|
|
|
|
|
return Title::legalChars();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function linkPrefixRegex(): ?string {
|
|
|
|
|
if ( !$this->contLang->linkPrefixExtension() ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return '/[' . $this->contLang->linkPrefixCharset() . ']+$/Du';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function linkTrail(): string {
|
|
|
|
|
return $this->contLang->linkTrail();
|
|
|
|
|
}
|
|
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
public function langBcp47(): Bcp47Code {
|
|
|
|
|
return $this->contLang;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function mainpage(): string {
|
|
|
|
|
// @todo Perhaps should inject TitleFactory here?
|
|
|
|
|
return Title::newMainPage()->getPrefixedText();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-12 22:05:24 +00:00
|
|
|
public function mainPageLinkTarget(): Title {
|
2023-11-03 18:22:34 +00:00
|
|
|
// @todo Perhaps should inject TitleFactory here?
|
|
|
|
|
return Title::newMainPage();
|
|
|
|
|
}
|
|
|
|
|
|
SiteConfig: Transition away from hardcoded Cite info
* This is step 1 of the process.
* In step 2, we'll update Parsoid's SiteConfig abstract class
and its subclasses with this same method.
* In step 3, we'll transition Cite in Parsoid repo to use the
new method.
* In step 4, we'll remove the responsiveReferences method from
SiteConfig
* In step 5, we'll remove the responsiveReferences method here.
* Multiple steps could be deployed on the same train.
- Aggressive: All steps on the same train.
- Less aggressive: 1,2,3 on one train, 4,5 on another train.
- Conservative: 2 on first train, 3,4 on next, 4,5 on following.
Bug: T268777
Change-Id: Ia489cd880de3df73d7cd47d1eb01971f2655effe
2022-10-03 22:56:13 +00:00
|
|
|
/**
|
|
|
|
|
* Lookup config
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return mixed config value for $key, if present or null, if not.
|
|
|
|
|
*/
|
|
|
|
|
public function getMWConfigValue( string $key ) {
|
|
|
|
|
return $this->mwConfig->has( $key ) ? $this->mwConfig->get( $key ) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
public function rtl(): bool {
|
|
|
|
|
return $this->contLang->isRTL();
|
|
|
|
|
}
|
|
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
/**
|
|
|
|
|
* @param Bcp47Code $lang
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function langConverterEnabledBcp47( Bcp47Code $lang ): bool {
|
2022-03-25 20:34:04 +00:00
|
|
|
if ( $this->languageConverterFactory->isConversionDisabled() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-10 13:05:22 +00:00
|
|
|
|
|
|
|
|
$langObject = $this->languageFactory->getLanguage( $lang );
|
|
|
|
|
if ( !in_array( $langObject->getCode(), LanguageConverter::$languagesWithVariants, true ) ) {
|
2022-03-25 20:34:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-10 13:05:22 +00:00
|
|
|
$converter = $this->languageConverterFactory->getLanguageConverter( $langObject );
|
|
|
|
|
return $converter->hasVariants();
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function script(): string {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::Script );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scriptpath(): string {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::ScriptPath );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function server(): string {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::Server );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @param Document $document
|
|
|
|
|
* @param ContentMetadataCollector $metadata
|
|
|
|
|
* @param string $defaultTitle
|
|
|
|
|
* @param Bcp47Code $lang
|
|
|
|
|
*/
|
|
|
|
|
public function exportMetadataToHeadBcp47(
|
2022-03-25 20:34:04 +00:00
|
|
|
Document $document,
|
|
|
|
|
ContentMetadataCollector $metadata,
|
|
|
|
|
string $defaultTitle,
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
Bcp47Code $lang
|
2022-03-25 20:34:04 +00:00
|
|
|
): void {
|
|
|
|
|
'@phan-var ParserOutput $metadata'; // @var ParserOutput $metadata
|
|
|
|
|
// Look for a displaytitle.
|
|
|
|
|
$displayTitle = $metadata->getPageProperty( 'displaytitle' ) ?:
|
|
|
|
|
// Use the default title, properly escaped
|
|
|
|
|
Utils::escapeHtml( $defaultTitle );
|
|
|
|
|
$this->exportMetadataHelper(
|
|
|
|
|
$document,
|
2022-04-10 15:34:45 +00:00
|
|
|
$this->config->get( MainConfigNames::LoadScript ),
|
2022-03-25 20:34:04 +00:00
|
|
|
$metadata->getModules(),
|
|
|
|
|
$metadata->getModuleStyles(),
|
|
|
|
|
$metadata->getJsConfigVars(),
|
|
|
|
|
$displayTitle,
|
|
|
|
|
$lang
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function timezoneOffset(): int {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::LocalTZoffset );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
/**
|
|
|
|
|
* Language variant information
|
|
|
|
|
* @return array<string,array> Keys are MediaWiki-internal variant codes (e.g. "zh-cn"),
|
|
|
|
|
* values are arrays with two fields:
|
|
|
|
|
* - base: (string) Base language code (e.g. "zh") (MediaWiki-internal)
|
|
|
|
|
* - fallbacks: (string[]) Fallback variants (MediaWiki-internal codes)
|
2024-04-30 17:24:34 +00:00
|
|
|
* @deprecated since 1.43; use ::variantsFor() (T320662)
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
*/
|
2022-03-25 20:34:04 +00:00
|
|
|
public function variants(): array {
|
2024-04-30 17:24:34 +00:00
|
|
|
// Deprecated for all external callers; to make private and remove this warning.
|
|
|
|
|
if ( wfGetCaller() !== __CLASS__ . '->variantsFor' ) {
|
|
|
|
|
wfDeprecated( __METHOD__, '1.43' );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:53:02 +00:00
|
|
|
if ( $this->variants !== null ) {
|
|
|
|
|
return $this->variants;
|
|
|
|
|
}
|
|
|
|
|
$this->variants = [];
|
2022-03-25 20:34:04 +00:00
|
|
|
|
2022-03-25 20:53:02 +00:00
|
|
|
$langNames = LanguageConverter::$languagesWithVariants;
|
|
|
|
|
if ( $this->languageConverterFactory->isConversionDisabled() ) {
|
|
|
|
|
// Ensure result is empty if language conversion is disabled.
|
|
|
|
|
$langNames = [];
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
|
2022-03-25 20:53:02 +00:00
|
|
|
foreach ( $langNames as $langCode ) {
|
|
|
|
|
$lang = $this->languageFactory->getLanguage( $langCode );
|
|
|
|
|
$converter = $this->languageConverterFactory->getLanguageConverter( $lang );
|
|
|
|
|
if ( !$converter->hasVariants() ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-25 20:34:04 +00:00
|
|
|
|
2022-03-25 20:53:02 +00:00
|
|
|
$variants = $converter->getVariants();
|
|
|
|
|
foreach ( $variants as $v ) {
|
|
|
|
|
$fallbacks = $converter->getVariantFallbacks( $v );
|
|
|
|
|
if ( !is_array( $fallbacks ) ) {
|
|
|
|
|
$fallbacks = [ $fallbacks ];
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
2022-03-25 20:53:02 +00:00
|
|
|
$this->variants[$v] = [
|
|
|
|
|
'base' => $langCode,
|
|
|
|
|
'fallbacks' => $fallbacks,
|
|
|
|
|
];
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->variants;
|
|
|
|
|
}
|
|
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
/**
|
|
|
|
|
* Language variant information for the given language (or null if
|
|
|
|
|
* unknown).
|
|
|
|
|
* @param Bcp47Code $code The language for which you want variant information
|
|
|
|
|
* @return ?array{base:Bcp47Code,fallbacks:Bcp47Code[]} an array with
|
|
|
|
|
* two fields:
|
|
|
|
|
* - base: (Bcp47Code) Base BCP-47 language code (e.g. "zh")
|
|
|
|
|
* - fallbacks: (Bcp47Code[]) Fallback variants, as BCP-47 codes
|
|
|
|
|
*/
|
|
|
|
|
public function variantsFor( Bcp47Code $code ): ?array {
|
|
|
|
|
$variants = $this->variants();
|
|
|
|
|
$lang = $this->languageFactory->getLanguage( $code );
|
|
|
|
|
$tuple = $variants[$lang->getCode()] ?? null;
|
|
|
|
|
if ( $tuple === null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'base' => $this->languageFactory->getLanguage( $tuple['base'] ),
|
|
|
|
|
'fallbacks' => array_map(
|
|
|
|
|
[ $this->languageFactory, 'getLanguage' ],
|
|
|
|
|
$tuple['fallbacks']
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
public function widthOption(): int {
|
|
|
|
|
// Even though this looks like Parsoid is supporting per-user thumbsize
|
|
|
|
|
// options, that is not the case, Parsoid doesn't receive user session state
|
|
|
|
|
$thumbsize = $this->userOptionsLookup->getDefaultOption( 'thumbsize' );
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::ThumbLimits )[$thumbsize];
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function getVariableIDs(): array {
|
|
|
|
|
return $this->magicWordFactory->getVariableIDs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function getFunctionSynonyms(): array {
|
2023-07-29 10:48:43 +00:00
|
|
|
return $this->parserFactory->getMainInstance()->getFunctionSynonyms();
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-03 13:50:55 +00:00
|
|
|
/** @return array<string,array> $magicWord => [ int $caseSensitive, string ...$alias ] */
|
2022-03-25 20:34:04 +00:00
|
|
|
protected function getMagicWords(): array {
|
|
|
|
|
return $this->contLang->getMagicWords();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getMagicWordMatcher( string $id ): string {
|
|
|
|
|
return $this->magicWordFactory->get( $id )->getRegexStartToEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getParameterizedAliasMatcher( array $words ): callable {
|
|
|
|
|
// PORT-FIXME: this should be combined with
|
|
|
|
|
// getMediaPrefixParameterizedAliasMatcher; see PORT-FIXME comment
|
|
|
|
|
// in that method.
|
|
|
|
|
// Filter out timedmedia-* unless that extension is loaded, so Parsoid
|
|
|
|
|
// doesn't have a hard dependency on an extension.
|
2023-07-15 14:51:26 +00:00
|
|
|
if ( !$this->isTimedMediaHandlerLoaded ) {
|
2022-03-25 20:34:04 +00:00
|
|
|
$words = preg_grep( '/^timedmedia_/', $words, PREG_GREP_INVERT );
|
|
|
|
|
}
|
|
|
|
|
$words = $this->magicWordFactory->newArray( $words );
|
|
|
|
|
return static function ( $text ) use ( $words ) {
|
|
|
|
|
$ret = $words->matchVariableStartToEnd( $text );
|
|
|
|
|
if ( $ret[0] === false || $ret[1] === false ) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
return [ 'k' => $ret[0], 'v' => $ret[1] ];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function populateExtensionTags(): void {
|
2023-07-29 10:48:43 +00:00
|
|
|
$this->extensionTags = array_fill_keys( $this->parserFactory->getMainInstance()->getTags(), true );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function getNonNativeExtensionTags(): array {
|
|
|
|
|
if ( $this->extensionTags === null ) {
|
|
|
|
|
$this->populateExtensionTags();
|
|
|
|
|
}
|
|
|
|
|
return $this->extensionTags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getMaxTemplateDepth(): int {
|
2022-04-10 15:34:45 +00:00
|
|
|
return (int)$this->config->get( MainConfigNames::MaxTemplateDepth );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Overrides the max template depth in the MediaWiki configuration.
|
|
|
|
|
* @param int $depth
|
|
|
|
|
*/
|
|
|
|
|
public function setMaxTemplateDepth( int $depth ): void {
|
|
|
|
|
// Parsoid's command-line tools let you set the max template depth
|
|
|
|
|
// as a CLI argument. Since we currently invoke the legacy
|
|
|
|
|
// preprocessor in some situations, we can't just override
|
|
|
|
|
// ::getMaxTemplateDepth() above, we need to reset the Config
|
|
|
|
|
// service.
|
|
|
|
|
if ( $this->config instanceof MutableConfig ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$this->config->set( MainConfigNames::MaxTemplateDepth, $depth );
|
2022-03-25 20:34:04 +00:00
|
|
|
} else {
|
|
|
|
|
// Fall back on global variable (hopefully we're using
|
|
|
|
|
// a GlobalVarConfig and this will work)
|
|
|
|
|
$GLOBALS['wgMaxTemplateDepth'] = $depth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function getSpecialNSAliases(): array {
|
|
|
|
|
$nsAliases = [
|
|
|
|
|
'Special',
|
|
|
|
|
$this->quoteTitleRe( $this->contLang->getNsText( NS_SPECIAL ) )
|
|
|
|
|
];
|
|
|
|
|
foreach (
|
2022-04-10 15:34:45 +00:00
|
|
|
$this->contLang->getNamespaceAliases() +
|
|
|
|
|
$this->config->get( MainConfigNames::NamespaceAliases )
|
2022-03-25 20:34:04 +00:00
|
|
|
as $name => $ns
|
|
|
|
|
) {
|
|
|
|
|
if ( $ns === NS_SPECIAL ) {
|
|
|
|
|
$nsAliases[] = $this->quoteTitleRe( $name );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $nsAliases;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function getSpecialPageAliases( string $specialPage ): array {
|
|
|
|
|
return array_merge( [ $specialPage ],
|
|
|
|
|
$this->contLang->getSpecialPageAliases()[$specialPage] ?? []
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
protected function getProtocols(): array {
|
2022-04-10 15:34:45 +00:00
|
|
|
return $this->config->get( MainConfigNames::UrlProtocols );
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
2022-06-08 14:41:35 +00:00
|
|
|
|
|
|
|
|
/** @return array */
|
|
|
|
|
public function getNoFollowConfig(): array {
|
|
|
|
|
return [
|
|
|
|
|
'nofollow' => $this->config->get( MainConfigNames::NoFollowLinks ),
|
|
|
|
|
'nsexceptions' => $this->config->get( MainConfigNames::NoFollowNsExceptions ),
|
|
|
|
|
'domainexceptions' => $this->config->get( MainConfigNames::NoFollowDomainExceptions )
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return string|false */
|
|
|
|
|
public function getExternalLinkTarget() {
|
|
|
|
|
return $this->config->get( MainConfigNames::ExternalLinkTarget );
|
|
|
|
|
}
|
2024-05-22 14:08:45 +00:00
|
|
|
|
|
|
|
|
// MW-specific helper
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true iff Parsoid natively supports the given content model.
|
|
|
|
|
* @param string $model content model identifier
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function supportsContentModel( string $model ): bool {
|
|
|
|
|
if ( $model === CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the content model serializes to wikitext.
|
|
|
|
|
// NOTE: We could use isSupportedFormat( CONTENT_FORMAT_WIKITEXT ) if PageContent::getContent()
|
|
|
|
|
// would specify the format when calling serialize().
|
|
|
|
|
try {
|
|
|
|
|
$handler = $this->contentHandlerFactory->getContentHandler( $model );
|
|
|
|
|
if ( $handler->getDefaultFormat() === CONTENT_FORMAT_WIKITEXT ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch ( MWUnknownContentModelException $ex ) {
|
|
|
|
|
// If the content model is not known, it can't be supported.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->getContentModelHandler( $model ) !== null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|