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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Parser\Parsoid\Config;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
|
use MediaWiki\Revision\SlotRecord;
|
|
|
|
|
use MediaWiki\Revision\SlotRoleHandler;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-03-25 20:34:04 +00:00
|
|
|
use ParserOptions;
|
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\Parsoid\Config\PageConfig as IPageConfig;
|
|
|
|
|
use Wikimedia\Parsoid\Config\PageContent as IPageContent;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Page-level configuration interface for Parsoid
|
|
|
|
|
*
|
2023-03-07 22:30:54 +00:00
|
|
|
* This is effectively "Parsoid's view of ParserOptions".
|
|
|
|
|
*
|
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 PageConfig extends IPageConfig {
|
2023-10-23 21:22:54 +00:00
|
|
|
private ParserOptions $parserOptions;
|
|
|
|
|
private SlotRoleHandler $slotRoleHandler;
|
|
|
|
|
private Title $title;
|
|
|
|
|
private ?RevisionRecord $revision = null;
|
|
|
|
|
private Bcp47Code $pageLanguage;
|
|
|
|
|
private string $pageLanguageDir;
|
2022-03-25 20:34:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ParserOptions $parserOptions
|
|
|
|
|
* @param SlotRoleHandler $slotRoleHandler
|
|
|
|
|
* @param Title $title Title being parsed
|
|
|
|
|
* @param ?RevisionRecord $revision
|
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 $pageLanguage
|
|
|
|
|
* @param string $pageLanguageDir
|
2022-03-25 20:34:04 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
ParserOptions $parserOptions,
|
2022-03-25 20:53:02 +00:00
|
|
|
SlotRoleHandler $slotRoleHandler,
|
|
|
|
|
Title $title,
|
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
|
|
|
?RevisionRecord $revision,
|
|
|
|
|
Bcp47Code $pageLanguage,
|
|
|
|
|
string $pageLanguageDir
|
2022-03-25 20:34:04 +00:00
|
|
|
) {
|
|
|
|
|
$this->parserOptions = $parserOptions;
|
|
|
|
|
$this->slotRoleHandler = $slotRoleHandler;
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
$this->revision = $revision;
|
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
|
|
|
$this->pageLanguage = $pageLanguage;
|
|
|
|
|
$this->pageLanguageDir = $pageLanguageDir;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get content model
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getContentModel(): string {
|
|
|
|
|
// @todo Check just the main slot, or all slots, or what?
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
if ( $rev ) {
|
|
|
|
|
$content = $rev->getContent( SlotRecord::MAIN );
|
|
|
|
|
if ( $content ) {
|
|
|
|
|
return $content->getModel();
|
|
|
|
|
} else {
|
|
|
|
|
// The page does have a content model but we can't see it. Returning the
|
|
|
|
|
// default model is not really correct. But we can't see the content either
|
|
|
|
|
// so it won't matter much what we do here.
|
|
|
|
|
return $this->slotRoleHandler->getDefaultModel( $this->title );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $this->slotRoleHandler->getDefaultModel( $this->title );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-03 18:22:34 +00:00
|
|
|
/** @inheritDoc */
|
2023-11-30 17:32:15 +00:00
|
|
|
public function getLinkTarget(): Title {
|
2023-11-03 18:22:34 +00:00
|
|
|
return $this->title;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 20:34:04 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getPageId(): int {
|
|
|
|
|
return $this->title->getArticleID();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
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 getPageLanguageBcp47(): Bcp47Code {
|
|
|
|
|
return $this->pageLanguage;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getPageLanguageDir(): string {
|
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
|
|
|
return $this->pageLanguageDir;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-07 22:30:54 +00:00
|
|
|
* @internal Used by DataAccess; not part of Parsoid's interface.
|
2022-03-25 20:34:04 +00:00
|
|
|
* @return ParserOptions
|
|
|
|
|
*/
|
|
|
|
|
public function getParserOptions(): ParserOptions {
|
|
|
|
|
return $this->parserOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use ParserOptions::getTemplateCallback() to fetch the correct
|
|
|
|
|
* (usually latest) RevisionRecord for the given title.
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @return ?RevisionRecord
|
|
|
|
|
*/
|
|
|
|
|
public function fetchRevisionRecordOfTemplate( Title $title ): ?RevisionRecord {
|
|
|
|
|
// See Parser::fetchTemplateAndTitle(), but stateless
|
|
|
|
|
// (Parsoid will track dependencies, etc, itself.)
|
|
|
|
|
// The callback defaults to Parser::statelessFetchTemplate()
|
|
|
|
|
$templateCb = $this->parserOptions->getTemplateCallback();
|
2022-04-04 09:57:04 +00:00
|
|
|
$stuff = $templateCb( $title, $this );
|
|
|
|
|
return $stuff['revision-record'] ?? null;
|
2022-03-25 20:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ?RevisionRecord
|
|
|
|
|
*/
|
|
|
|
|
private function getRevision(): ?RevisionRecord {
|
|
|
|
|
return $this->revision;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionId(): ?int {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
return $rev ? $rev->getId() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getParentRevisionId(): ?int {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
return $rev ? $rev->getParentId() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionTimestamp(): ?string {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
return $rev ? $rev->getTimestamp() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionUser(): ?string {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
$user = $rev ? $rev->getUser() : null;
|
|
|
|
|
return $user ? $user->getName() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionUserId(): ?int {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
$user = $rev ? $rev->getUser() : null;
|
|
|
|
|
return $user ? $user->getId() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionSha1(): ?string {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
if ( $rev ) {
|
|
|
|
|
// This matches what the Parsoid/JS gets from the API
|
|
|
|
|
// FIXME: Maybe we don't need to do this in the future?
|
|
|
|
|
return \Wikimedia\base_convert( $rev->getSha1(), 36, 16, 40 );
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionSize(): ?int {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
return $rev ? $rev->getSize() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getRevisionContent(): ?IPageContent {
|
|
|
|
|
$rev = $this->getRevision();
|
|
|
|
|
return $rev ? new PageContent( $rev ) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|