2022-08-30 19:19:53 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2023-01-13 21:30:21 +00:00
|
|
|
namespace MediaWiki\Rest\Handler\Helper;
|
2022-08-30 19:19:53 +00:00
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2022-10-01 19:38:30 +00:00
|
|
|
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
|
2024-08-08 09:14:35 +00:00
|
|
|
use MediaWiki\Content\Content;
|
2022-09-09 09:32:17 +00:00
|
|
|
use MediaWiki\Edit\ParsoidOutputStash;
|
2023-11-06 15:22:44 +00:00
|
|
|
use MediaWiki\Edit\ParsoidRenderID;
|
2022-10-29 18:52:23 +00:00
|
|
|
use MediaWiki\Edit\SelserContext;
|
2024-08-08 09:39:26 +00:00
|
|
|
use MediaWiki\Language\LanguageCode;
|
2022-08-30 19:19:53 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
use MediaWiki\Page\PageIdentity;
|
2024-05-15 17:21:13 +00:00
|
|
|
use MediaWiki\Page\PageLookup;
|
|
|
|
|
use MediaWiki\Page\PageRecord;
|
|
|
|
|
use MediaWiki\Page\ParserOutputAccess;
|
2024-10-03 18:39:06 +00:00
|
|
|
use MediaWiki\Parser\ParserOptions;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2022-10-16 21:44:53 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\HtmlToContentTransform;
|
|
|
|
|
use MediaWiki\Parser\Parsoid\HtmlTransformFactory;
|
2022-09-13 15:52:44 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
|
2022-08-30 19:19:53 +00:00
|
|
|
use MediaWiki\Rest\Handler;
|
|
|
|
|
use MediaWiki\Rest\HttpException;
|
|
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\ResponseInterface;
|
2022-09-13 15:52:44 +00:00
|
|
|
use MediaWiki\Revision\RevisionAccessException;
|
2024-05-15 17:21:13 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2022-08-30 19:19:53 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2022-08-30 19:19:53 +00:00
|
|
|
use MWUnknownContentModelException;
|
2023-07-07 08:00:34 +00:00
|
|
|
use Wikimedia\Bcp47Code\Bcp47Code;
|
2022-08-30 19:19:53 +00:00
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
use Wikimedia\Parsoid\Core\ClientError;
|
2022-09-13 15:52:44 +00:00
|
|
|
use Wikimedia\Parsoid\Core\PageBundle;
|
2022-08-30 19:19:53 +00:00
|
|
|
use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
|
|
|
|
|
use Wikimedia\Parsoid\Parsoid;
|
2024-09-17 15:11:18 +00:00
|
|
|
use Wikimedia\Stats\StatsFactory;
|
2022-08-30 19:19:53 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* REST helper for converting HTML to page content source (e.g. wikitext).
|
|
|
|
|
*
|
|
|
|
|
* @since 1.40
|
|
|
|
|
*
|
|
|
|
|
* @unstable Pending consolidation of the Parsoid extension with core code.
|
|
|
|
|
*/
|
|
|
|
|
class HtmlInputTransformHelper {
|
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
|
MainConfigNames::ParsoidCacheConfig
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/** @var PageIdentity|null */
|
|
|
|
|
private $page = null;
|
|
|
|
|
|
|
|
|
|
/**
|
2022-10-16 21:44:53 +00:00
|
|
|
* @var HtmlToContentTransform
|
2022-08-30 19:19:53 +00:00
|
|
|
*/
|
|
|
|
|
private $transform;
|
|
|
|
|
|
2022-09-09 09:32:17 +00:00
|
|
|
/**
|
2024-06-10 20:49:30 +00:00
|
|
|
* @var array
|
2022-09-09 09:32:17 +00:00
|
|
|
*/
|
2024-06-10 20:49:30 +00:00
|
|
|
private $envOptions;
|
2022-09-09 09:32:17 +00:00
|
|
|
|
2024-09-17 15:11:18 +00:00
|
|
|
private StatsFactory $statsFactory;
|
2024-06-10 20:49:30 +00:00
|
|
|
private HtmlTransformFactory $htmlTransformFactory;
|
|
|
|
|
private ParsoidOutputStash $parsoidOutputStash;
|
2024-05-15 17:21:13 +00:00
|
|
|
private ParserOutputAccess $parserOutputAccess;
|
|
|
|
|
private PageLookup $pageLookup;
|
|
|
|
|
private RevisionLookup $revisionLookup;
|
2022-09-13 15:52:44 +00:00
|
|
|
|
2022-08-30 19:19:53 +00:00
|
|
|
/**
|
2024-09-17 15:11:18 +00:00
|
|
|
* @param StatsFactory $statsFactory
|
2022-10-16 21:44:53 +00:00
|
|
|
* @param HtmlTransformFactory $htmlTransformFactory
|
2022-09-09 09:32:17 +00:00
|
|
|
* @param ParsoidOutputStash $parsoidOutputStash
|
2024-05-15 17:21:13 +00:00
|
|
|
* @param ParserOutputAccess $parserOutputAccess
|
|
|
|
|
* @param PageLookup $pageLookup
|
|
|
|
|
* @param RevisionLookup $revisionLookup
|
2022-08-30 19:19:53 +00:00
|
|
|
* @param array $envOptions
|
2024-06-05 15:49:49 +00:00
|
|
|
* @param ?PageIdentity $page
|
|
|
|
|
* @param array|string $body Body structure, or an HTML string
|
|
|
|
|
* @param array $parameters
|
|
|
|
|
* @param RevisionRecord|null $originalRevision
|
|
|
|
|
* @param Bcp47Code|null $pageLanguage
|
2022-08-30 19:19:53 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2024-09-17 15:11:18 +00:00
|
|
|
StatsFactory $statsFactory,
|
2022-10-16 21:44:53 +00:00
|
|
|
HtmlTransformFactory $htmlTransformFactory,
|
2022-09-09 09:32:17 +00:00
|
|
|
ParsoidOutputStash $parsoidOutputStash,
|
2024-05-15 17:21:13 +00:00
|
|
|
ParserOutputAccess $parserOutputAccess,
|
|
|
|
|
PageLookup $pageLookup,
|
|
|
|
|
RevisionLookup $revisionLookup,
|
2024-06-05 15:49:49 +00:00
|
|
|
array $envOptions = [],
|
|
|
|
|
?PageIdentity $page = null,
|
|
|
|
|
$body = '',
|
|
|
|
|
array $parameters = [],
|
|
|
|
|
?RevisionRecord $originalRevision = null,
|
|
|
|
|
?Bcp47Code $pageLanguage = null
|
2022-08-30 19:19:53 +00:00
|
|
|
) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory = $statsFactory;
|
2022-08-30 19:19:53 +00:00
|
|
|
$this->htmlTransformFactory = $htmlTransformFactory;
|
2022-09-09 09:32:17 +00:00
|
|
|
$this->parsoidOutputStash = $parsoidOutputStash;
|
2022-08-30 19:19:53 +00:00
|
|
|
$this->envOptions = $envOptions + [
|
|
|
|
|
'outputContentVersion' => Parsoid::defaultHTMLVersion(),
|
|
|
|
|
'offsetType' => 'byte',
|
|
|
|
|
];
|
2024-05-15 17:21:13 +00:00
|
|
|
$this->parserOutputAccess = $parserOutputAccess;
|
|
|
|
|
$this->pageLookup = $pageLookup;
|
|
|
|
|
$this->revisionLookup = $revisionLookup;
|
2024-06-05 16:14:02 +00:00
|
|
|
if ( $page === null ) {
|
|
|
|
|
wfDeprecated( __METHOD__ . ' without $page', '1.43' );
|
|
|
|
|
} else {
|
|
|
|
|
$this->initInternal( $page, $body, $parameters, $originalRevision, $pageLanguage );
|
2024-06-05 15:49:49 +00:00
|
|
|
}
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getParamSettings(): array {
|
|
|
|
|
// JSON body schema:
|
|
|
|
|
/*
|
|
|
|
|
doc:
|
|
|
|
|
properties:
|
|
|
|
|
headers:
|
|
|
|
|
type: array
|
|
|
|
|
items:
|
|
|
|
|
type: string
|
|
|
|
|
body:
|
|
|
|
|
type: [ string, object ]
|
|
|
|
|
required: [ body ]
|
|
|
|
|
|
|
|
|
|
body:
|
|
|
|
|
properties:
|
|
|
|
|
offsetType:
|
|
|
|
|
type: string
|
2022-09-09 09:32:17 +00:00
|
|
|
revid:
|
2022-08-30 19:19:53 +00:00
|
|
|
type: integer
|
2022-09-09 09:32:17 +00:00
|
|
|
renderid:
|
|
|
|
|
type: string
|
|
|
|
|
etag:
|
|
|
|
|
type: string
|
2022-08-30 19:19:53 +00:00
|
|
|
html:
|
|
|
|
|
type: [ doc, string ]
|
|
|
|
|
data-mw:
|
|
|
|
|
type: doc
|
|
|
|
|
original:
|
|
|
|
|
properties:
|
|
|
|
|
html:
|
|
|
|
|
type: doc
|
|
|
|
|
source:
|
|
|
|
|
type: doc
|
|
|
|
|
data-mw:
|
|
|
|
|
type: doc
|
|
|
|
|
data-parsoid:
|
|
|
|
|
type: doc
|
|
|
|
|
required: [ html ]
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// FUTURE: more params
|
|
|
|
|
// - slot (for loading the base content)
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
// XXX: should we really declare this here? Or should end endpoint do this?
|
|
|
|
|
// We are not reading this property...
|
|
|
|
|
'title' => [
|
|
|
|
|
Handler::PARAM_SOURCE => 'path',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => '',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
|
],
|
|
|
|
|
// XXX: Needed for compatibility with the parsoid transform endpoint.
|
2022-09-09 09:32:17 +00:00
|
|
|
// But revid should just be part of the info about the original data
|
2022-08-30 19:19:53 +00:00
|
|
|
// in the body.
|
2022-09-09 09:32:17 +00:00
|
|
|
'oldid' => [
|
2022-08-30 19:19:53 +00:00
|
|
|
Handler::PARAM_SOURCE => 'path',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'int',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => 0,
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
|
],
|
|
|
|
|
// XXX: Supported for compatibility with the parsoid transform endpoint.
|
|
|
|
|
// If given, it should be 'html' or 'pagebundle'.
|
|
|
|
|
'from' => [
|
|
|
|
|
Handler::PARAM_SOURCE => 'path',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => '',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
|
],
|
|
|
|
|
// XXX: Supported for compatibility with the parsoid transform endpoint.
|
|
|
|
|
// Ignored.
|
|
|
|
|
'format' => [
|
|
|
|
|
Handler::PARAM_SOURCE => 'path',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => '',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
|
],
|
|
|
|
|
'contentmodel' => [ // XXX: get this from the Accept header?
|
|
|
|
|
Handler::PARAM_SOURCE => 'query',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => '',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
|
],
|
|
|
|
|
'language' => [ // TODO: get this from Accept-Language header?!
|
|
|
|
|
Handler::PARAM_SOURCE => 'query',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => '',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
2022-09-09 09:32:17 +00:00
|
|
|
]
|
2022-08-30 19:19:53 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Modify body and parameters to provide compatibility with legacy endpoints.
|
|
|
|
|
*
|
|
|
|
|
* @see ParsoidHandler::getRequestAttributes
|
|
|
|
|
*
|
|
|
|
|
* @param array<string,mixed> &$body
|
|
|
|
|
* @param array<string,mixed> &$parameters
|
|
|
|
|
*
|
|
|
|
|
* @throws HttpException
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function normalizeParameters( array &$body, array &$parameters ) {
|
|
|
|
|
// If the revision ID is given in the path, pretend it was given in the body.
|
2022-09-09 09:32:17 +00:00
|
|
|
if ( isset( $parameters['oldid'] ) && (int)$parameters['oldid'] > 0 ) {
|
|
|
|
|
$body['original']['revid'] = (int)$parameters['oldid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If an etag is given in the body, use it as the render ID.
|
2022-10-14 15:44:57 +00:00
|
|
|
// Note that we support ETag format in the renderid field.
|
2022-09-09 09:32:17 +00:00
|
|
|
if ( !empty( $body['original']['etag'] ) ) {
|
|
|
|
|
// @phan-suppress-next-line PhanTypeInvalidDimOffset false positive
|
|
|
|
|
$body['original']['renderid'] = $body['original']['etag'];
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accept 'wikitext' as an alias for 'source'.
|
|
|
|
|
if ( isset( $body['original']['wikitext'] ) ) {
|
|
|
|
|
// @phan-suppress-next-line PhanTypeInvalidDimOffset false positive
|
|
|
|
|
$body['original']['source'] = $body['original']['wikitext'];
|
|
|
|
|
unset( $body['original']['wikitext'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If 'from' is not set, we accept page bundle style input as well as full HTML.
|
|
|
|
|
// If 'from' is set, we only accept page bundle style input if it is set to FORMAT_PAGEBUNDLE.
|
|
|
|
|
if (
|
|
|
|
|
isset( $parameters['from'] ) && $parameters['from'] !== '' &&
|
|
|
|
|
$parameters['from'] !== ParsoidFormatHelper::FORMAT_PAGEBUNDLE
|
|
|
|
|
) {
|
|
|
|
|
unset( $body['original']['data-parsoid']['body'] );
|
|
|
|
|
unset( $body['original']['data-mw']['body'] );
|
|
|
|
|
unset( $body['data-mw']['body'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If 'from' is given, it must be html or pagebundle.
|
|
|
|
|
if (
|
|
|
|
|
isset( $parameters['from'] ) && $parameters['from'] !== '' &&
|
|
|
|
|
$parameters['from'] !== ParsoidFormatHelper::FORMAT_HTML &&
|
|
|
|
|
$parameters['from'] !== ParsoidFormatHelper::FORMAT_PAGEBUNDLE
|
|
|
|
|
) {
|
2024-03-28 23:26:37 +00:00
|
|
|
throw new LocalizedHttpException(
|
|
|
|
|
new MessageValue( "rest-unsupported-transform-input", [ $parameters['from'] ] ), 400
|
|
|
|
|
);
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $body['contentmodel'] ) && $body['contentmodel'] !== '' ) {
|
|
|
|
|
$parameters['contentmodel'] = $body['contentmodel'];
|
|
|
|
|
} elseif ( isset( $parameters['format'] ) && $parameters['format'] !== '' ) {
|
|
|
|
|
$parameters['contentmodel'] = $parameters['format'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param PageIdentity $page
|
2022-10-14 15:44:57 +00:00
|
|
|
* @param array|string $body Body structure, or an HTML string
|
2022-08-30 19:19:53 +00:00
|
|
|
* @param array $parameters
|
2022-10-14 15:44:57 +00:00
|
|
|
* @param RevisionRecord|null $originalRevision
|
2023-07-07 08:00:34 +00:00
|
|
|
* @param Bcp47Code|null $pageLanguage
|
2022-08-30 19:19:53 +00:00
|
|
|
*
|
|
|
|
|
* @throws HttpException
|
2024-06-05 16:14:02 +00:00
|
|
|
* @deprecated since 1.43; pass arguments to constructor instead
|
2022-08-30 19:19:53 +00:00
|
|
|
*/
|
|
|
|
|
public function init(
|
|
|
|
|
PageIdentity $page,
|
2022-10-14 15:44:57 +00:00
|
|
|
$body,
|
2022-08-30 19:19:53 +00:00
|
|
|
array $parameters,
|
2022-10-14 15:44:57 +00:00
|
|
|
?RevisionRecord $originalRevision = null,
|
2023-07-07 08:00:34 +00:00
|
|
|
?Bcp47Code $pageLanguage = null
|
2024-06-05 16:14:02 +00:00
|
|
|
) {
|
|
|
|
|
wfDeprecated( __METHOD__, '1.43' );
|
|
|
|
|
$this->initInternal( $page, $body, $parameters, $originalRevision, $pageLanguage );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param PageIdentity $page
|
|
|
|
|
* @param array|string $body Body structure, or an HTML string
|
|
|
|
|
* @param array $parameters
|
|
|
|
|
* @param RevisionRecord|null $originalRevision
|
|
|
|
|
* @param Bcp47Code|null $pageLanguage
|
|
|
|
|
*
|
|
|
|
|
* @throws HttpException
|
|
|
|
|
*/
|
|
|
|
|
private function initInternal(
|
|
|
|
|
PageIdentity $page,
|
|
|
|
|
$body,
|
|
|
|
|
array $parameters,
|
|
|
|
|
?RevisionRecord $originalRevision = null,
|
|
|
|
|
?Bcp47Code $pageLanguage = null
|
2022-08-30 19:19:53 +00:00
|
|
|
) {
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( is_string( $body ) ) {
|
|
|
|
|
$body = [ 'html' => $body ];
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 19:19:53 +00:00
|
|
|
self::normalizeParameters( $body, $parameters );
|
|
|
|
|
|
|
|
|
|
$this->page = $page;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $body['html'] ) ) {
|
2024-03-05 21:00:43 +00:00
|
|
|
throw new LocalizedHttpException( new MessageValue( "rest-missing-body-field", [ 'html' ] ) );
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$html = is_array( $body['html'] ) ? $body['html']['body'] : $body['html'];
|
|
|
|
|
|
|
|
|
|
// TODO: validate $body against a proper schema.
|
2022-10-16 21:44:53 +00:00
|
|
|
$this->transform = $this->htmlTransformFactory->getHtmlToContentTransform(
|
2022-08-30 19:19:53 +00:00
|
|
|
$html,
|
|
|
|
|
$this->page
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->transform->setMetrics( $this->statsFactory );
|
2022-08-30 19:19:53 +00:00
|
|
|
|
|
|
|
|
// NOTE: Env::getContentModel will fall back to the page's recorded content model
|
|
|
|
|
// if none is set here.
|
|
|
|
|
$this->transform->setOptions( [
|
2023-07-11 20:51:27 +00:00
|
|
|
'contentmodel' => $parameters['contentmodel'] ?? null,
|
2022-08-30 19:19:53 +00:00
|
|
|
'offsetType' => $body['offsetType'] ?? $this->envOptions['offsetType'],
|
|
|
|
|
] );
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
$original = $body['original'] ?? [];
|
|
|
|
|
$originalRendering = null;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $original['html'] ) && !empty( $original['renderid'] ) ) {
|
|
|
|
|
$key = $original['renderid'];
|
|
|
|
|
if ( preg_match( '!^(W/)?".*"$!', $key ) ) {
|
|
|
|
|
$originalRendering = ParsoidRenderID::newFromETag( $key );
|
2023-03-10 19:44:40 +00:00
|
|
|
|
|
|
|
|
if ( !$originalRendering ) {
|
2024-03-28 23:26:37 +00:00
|
|
|
throw new LocalizedHttpException( new MessageValue( "rest-bad-etag", [ $key ] ), 400 );
|
2023-03-10 19:44:40 +00:00
|
|
|
}
|
2022-10-14 15:44:57 +00:00
|
|
|
} else {
|
2025-02-04 12:31:32 +00:00
|
|
|
try {
|
|
|
|
|
$originalRendering = ParsoidRenderID::newFromKey( $key );
|
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
|
throw new LocalizedHttpException(
|
|
|
|
|
new MessageValue( 'rest-parsoid-bad-render-id', [ $key ] ),
|
|
|
|
|
400
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-10-14 15:44:57 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( !empty( $original['html'] ) || !empty( $original['data-parsoid'] ) ) {
|
|
|
|
|
// NOTE: We might have an incomplete PageBundle here, with no HTML but with data-parsoid!
|
|
|
|
|
// XXX: Do we need to support that, or can that just be a 400?
|
|
|
|
|
$originalRendering = new PageBundle(
|
|
|
|
|
$original['html']['body'] ?? '',
|
|
|
|
|
$original['data-parsoid']['body'] ?? null,
|
|
|
|
|
$original['data-mw']['body'] ?? null,
|
|
|
|
|
null, // will be derived from $original['html']['headers']['content-type']
|
|
|
|
|
$original['html']['headers'] ?? []
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$originalRevision && !empty( $original['revid'] ) ) {
|
|
|
|
|
$originalRevision = (int)$original['revid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $originalRevision || $originalRendering ) {
|
|
|
|
|
$this->setOriginal( $originalRevision, $originalRendering );
|
2022-10-19 15:26:02 +00:00
|
|
|
} else {
|
|
|
|
|
if ( $this->page->exists() ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory
|
|
|
|
|
->getCounter( 'html_input_transform_total' )
|
|
|
|
|
->setLabel( 'original_html_given', 'false' )
|
|
|
|
|
->setLabel( 'page_exists', 'true' )
|
|
|
|
|
->setLabel( 'status', 'unknown' )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.not_given.page_exists' )
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
} else {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory
|
|
|
|
|
->getCounter( 'html_input_transform_total' )
|
|
|
|
|
->setLabel( 'original_html_given', 'false' )
|
|
|
|
|
->setLabel( 'page_exists', 'false' )
|
|
|
|
|
->setLabel( 'status', 'unknown' )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.not_given.page_not_exist' )
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
}
|
2022-10-14 15:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $body['data-mw']['body'] ) ) {
|
|
|
|
|
$this->transform->setModifiedDataMW( $body['data-mw']['body'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $pageLanguage ) {
|
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->transform->setContentLanguage( $pageLanguage );
|
2022-10-14 15:44:57 +00:00
|
|
|
} elseif ( isset( $parameters['language'] ) && $parameters['language'] !== '' ) {
|
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
|
|
|
$pageLanguage = LanguageCode::normalizeNonstandardCodeAndWarn(
|
|
|
|
|
$parameters['language']
|
|
|
|
|
);
|
|
|
|
|
$this->transform->setContentLanguage( $pageLanguage );
|
2022-10-14 15:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $original['source']['body'] ) ) {
|
|
|
|
|
// XXX: do we really have to support wikitext overrides?
|
|
|
|
|
$this->transform->setOriginalText( $original['source']['body'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return HTMLTransform object, so additional context can be provided by calling setters on it.
|
|
|
|
|
* @return HtmlToContentTransform
|
|
|
|
|
*/
|
|
|
|
|
public function getTransform(): HtmlToContentTransform {
|
|
|
|
|
return $this->transform;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 16:55:18 +00:00
|
|
|
/**
|
|
|
|
|
* Set metrics sink.
|
|
|
|
|
*
|
2024-10-18 22:18:34 +00:00
|
|
|
* @note Passing a StatsdDataFactoryInterface here has been deprecated
|
|
|
|
|
* since 1.43.
|
|
|
|
|
*
|
2024-09-17 15:11:18 +00:00
|
|
|
* @param StatsFactory|StatsdDataFactoryInterface $statsFactory
|
2022-11-18 16:55:18 +00:00
|
|
|
*/
|
2024-09-17 15:11:18 +00:00
|
|
|
public function setMetrics( $statsFactory ) {
|
|
|
|
|
if ( $statsFactory instanceof StatsdDataFactoryInterface ) {
|
|
|
|
|
// Uncomment this once all WMF code has been transitioned, but
|
|
|
|
|
// leave it in for the 1.43 release.
|
2024-10-18 22:18:34 +00:00
|
|
|
wfDeprecated( __METHOD__ . ' with StatsdDataFactoryInterface', '1.43' );
|
2024-09-17 15:11:18 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->statsFactory = $statsFactory;
|
2022-11-18 16:55:18 +00:00
|
|
|
|
|
|
|
|
if ( $this->transform ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->transform->setMetrics( $statsFactory );
|
2022-11-18 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
/**
|
|
|
|
|
* Supply information about the revision and rendering that was the original basis of
|
|
|
|
|
* the input HTML. This is used to apply selective serialization (selser), if possible.
|
|
|
|
|
*
|
|
|
|
|
* @param RevisionRecord|int|null $rev
|
|
|
|
|
* @param ParsoidRenderID|PageBundle|ParserOutput|null $originalRendering
|
|
|
|
|
*/
|
|
|
|
|
public function setOriginal( $rev, $originalRendering ) {
|
|
|
|
|
if ( $originalRendering instanceof ParsoidRenderID ) {
|
|
|
|
|
$renderId = $originalRendering;
|
|
|
|
|
|
2022-09-09 09:32:17 +00:00
|
|
|
// If the client asked for a render ID, load original data from stash
|
|
|
|
|
try {
|
2022-10-29 18:52:23 +00:00
|
|
|
$selserContext = $this->fetchSelserContextFromStash( $renderId );
|
2022-09-09 09:32:17 +00:00
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory
|
|
|
|
|
->getCounter( 'html_input_transform_total' )
|
|
|
|
|
->setLabel( 'original_html_given', 'as_renderid' )
|
|
|
|
|
->setLabel( 'page_exists', 'unknown' )
|
|
|
|
|
->setLabel( 'status', 'bad_renderid' )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.given.as_renderid.bad' )
|
|
|
|
|
->increment();
|
2024-03-05 21:00:43 +00:00
|
|
|
throw new LocalizedHttpException( new MessageValue( "rest-bad-stash-key" ),
|
2022-09-09 09:32:17 +00:00
|
|
|
400,
|
|
|
|
|
[
|
|
|
|
|
'reason' => $ex->getMessage(),
|
2022-10-14 15:44:57 +00:00
|
|
|
'key' => "$renderId"
|
2022-09-09 09:32:17 +00:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 18:52:23 +00:00
|
|
|
if ( !$selserContext ) {
|
2022-09-09 09:32:17 +00:00
|
|
|
// NOTE: When the client asked for a specific stash key (resp. etag),
|
|
|
|
|
// we should fail with a 412 if we don't have the specific rendering.
|
|
|
|
|
// On the other hand, of the client only provided a base revision ID,
|
|
|
|
|
// we can re-parse and hope for the best.
|
2022-08-30 19:19:53 +00:00
|
|
|
|
2024-03-28 23:26:37 +00:00
|
|
|
throw new LocalizedHttpException(
|
|
|
|
|
new MessageValue( "rest-no-stashed-content", [ $renderId->getKey() ] ), 412
|
2022-09-09 09:32:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: This class should provide getETag and getLastModified methods for use by
|
|
|
|
|
// the REST endpoint, to provide proper support for conditionals.
|
|
|
|
|
// However, that requires some refactoring of how HTTP conditional checks
|
|
|
|
|
// work in the Handler base class.
|
|
|
|
|
}
|
2022-10-14 15:44:57 +00:00
|
|
|
|
|
|
|
|
if ( !$rev ) {
|
|
|
|
|
$rev = $renderId->getRevisionID();
|
|
|
|
|
}
|
2022-10-29 18:52:23 +00:00
|
|
|
|
|
|
|
|
$originalRendering = $selserContext->getPageBundle();
|
|
|
|
|
$content = $selserContext->getContent();
|
|
|
|
|
|
|
|
|
|
if ( $content ) {
|
|
|
|
|
$this->transform->setOriginalContent( $content );
|
|
|
|
|
}
|
2022-10-14 15:44:57 +00:00
|
|
|
} elseif ( !$originalRendering && $rev ) {
|
2022-09-27 20:53:02 +00:00
|
|
|
// The client provided a revision ID, but not stash key.
|
|
|
|
|
// Try to get a rendering for the given revision, and use it as the basis for selser.
|
|
|
|
|
// Chances are good that the resulting diff will be reasonably clean.
|
|
|
|
|
// NOTE: If we don't have a revision ID, we should not attempt selser!
|
2024-05-15 17:21:13 +00:00
|
|
|
$originalRendering = $this->fetchParserOutputFromParsoid( $this->page, $rev, true );
|
2022-10-19 15:26:02 +00:00
|
|
|
|
|
|
|
|
if ( $originalRendering ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory->getCounter( 'html_input_transform_total' )
|
|
|
|
|
->setLabel( 'original_html_given', 'as_revid' )
|
|
|
|
|
->setLabel( 'page_exists', 'unknown' )
|
|
|
|
|
->setLabel( 'status', 'found' )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.given.as_revid.found' )
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
} else {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory->getCounter( 'html_input_transform_total' )
|
|
|
|
|
->setLabel( 'original_html_given', 'as_revid' )
|
|
|
|
|
->setLabel( 'page_exists', 'unknown' )
|
|
|
|
|
->setLabel( 'status', 'not_found' )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.given.as_revid.not_found' )
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( $originalRendering ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$this->statsFactory->getCounter( 'html_input_transform_total' )
|
|
|
|
|
->setLabel( 'original_html_given', 'true' )
|
|
|
|
|
->setLabel( 'page_exists', 'unknown' )
|
|
|
|
|
->setLabel( 'status', 'verbatim' )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.given.verbatim' )
|
|
|
|
|
->increment();
|
2022-09-09 09:32:17 +00:00
|
|
|
}
|
2022-08-30 19:19:53 +00:00
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $originalRendering instanceof ParserOutput ) {
|
|
|
|
|
$originalRendering = PageBundleParserOutputConverter::pageBundleFromParserOutput( $originalRendering );
|
2022-08-30 19:19:53 +00:00
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
// NOTE: Use the default if we got a ParserOutput object.
|
|
|
|
|
// Don't apply the default if we got passed a PageBundle,
|
|
|
|
|
// in that case, we want to require the version to be explicit.
|
|
|
|
|
if ( $originalRendering->version === null && !isset( $originalRendering->headers['content-type'] ) ) {
|
|
|
|
|
$originalRendering->version = Parsoid::defaultHTMLVersion();
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( !$originalRendering instanceof PageBundle ) {
|
|
|
|
|
return;
|
2022-09-09 09:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $originalRendering->version !== null ) {
|
|
|
|
|
$this->transform->setOriginalSchemaVersion( $originalRendering->version );
|
|
|
|
|
} elseif ( !empty( $originalRendering->headers['content-type'] ) ) {
|
|
|
|
|
$vOriginal = ParsoidFormatHelper::parseContentTypeHeader(
|
|
|
|
|
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable Silly Phan, we just checked.
|
|
|
|
|
$originalRendering->headers['content-type']
|
|
|
|
|
);
|
2022-08-30 19:19:53 +00:00
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $vOriginal ) {
|
|
|
|
|
$this->transform->setOriginalSchemaVersion( $vOriginal );
|
|
|
|
|
}
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $rev instanceof RevisionRecord ) {
|
|
|
|
|
$this->transform->setOriginalRevision( $rev );
|
|
|
|
|
} elseif ( $rev && is_int( $rev ) ) {
|
|
|
|
|
$this->transform->setOriginalRevisionId( $rev );
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
// NOTE: We might have an incomplete PageBundle here, with no HTML.
|
|
|
|
|
// PageBundle::$html is declared to not be nullable, so it would be set to the empty
|
|
|
|
|
// string if not given. Note however that it might also be null, since it's a public field.
|
|
|
|
|
if ( $originalRendering->html !== null && $originalRendering->html !== '' ) {
|
|
|
|
|
$this->transform->setOriginalHtml( $originalRendering->html );
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $originalRendering->parsoid !== null ) {
|
|
|
|
|
$this->transform->setOriginalDataParsoid( $originalRendering->parsoid );
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $originalRendering->mw !== null ) {
|
|
|
|
|
$this->transform->setOriginalDataMW( $originalRendering->mw );
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Content the content derived from the input HTML.
|
|
|
|
|
* @throws HttpException
|
|
|
|
|
*/
|
|
|
|
|
public function getContent(): Content {
|
|
|
|
|
try {
|
|
|
|
|
return $this->transform->htmlToContent();
|
|
|
|
|
} catch ( ClientError $e ) {
|
|
|
|
|
throw new LocalizedHttpException(
|
2024-02-27 17:50:27 +00:00
|
|
|
new MessageValue( 'rest-html-backend-error', [ $e->getMessage() ] ),
|
2022-08-30 19:19:53 +00:00
|
|
|
400,
|
|
|
|
|
[ 'reason' => $e->getMessage() ]
|
|
|
|
|
);
|
|
|
|
|
} catch ( ResourceLimitExceededException $e ) {
|
|
|
|
|
throw new LocalizedHttpException(
|
|
|
|
|
new MessageValue( 'rest-resource-limit-exceeded' ),
|
|
|
|
|
413,
|
|
|
|
|
[ 'reason' => $e->getMessage() ]
|
|
|
|
|
);
|
|
|
|
|
} catch ( MWUnknownContentModelException $e ) {
|
2024-03-28 23:26:37 +00:00
|
|
|
throw new LocalizedHttpException(
|
|
|
|
|
new MessageValue( "rest-unknown-content-model", [ $e->getModelId() ] ),
|
|
|
|
|
400
|
|
|
|
|
);
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a response containing the content derived from the input HTML.
|
|
|
|
|
* This will set the appropriate Content-Type header.
|
|
|
|
|
*
|
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
|
*/
|
|
|
|
|
public function putContent( ResponseInterface $response ) {
|
|
|
|
|
$content = $this->getContent();
|
|
|
|
|
$data = $content->serialize();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$contentType = ParsoidFormatHelper::getContentType(
|
|
|
|
|
$content->getModel(),
|
|
|
|
|
$this->envOptions['outputContentVersion']
|
|
|
|
|
);
|
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
|
// If Parsoid doesn't know the content type,
|
|
|
|
|
// ask the ContentHandler!
|
|
|
|
|
$contentType = $content->getDefaultFormat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response->setHeader( 'Content-Type', $contentType );
|
|
|
|
|
$response->getBody()->write( $data );
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
/**
|
2024-05-15 17:21:13 +00:00
|
|
|
* @param PageIdentity $page
|
|
|
|
|
* @param RevisionRecord|int $revision
|
2022-10-14 15:44:57 +00:00
|
|
|
* @param bool $mayParse
|
|
|
|
|
*
|
|
|
|
|
* @return ParserOutput|null
|
|
|
|
|
* @throws HttpException
|
|
|
|
|
*/
|
2024-05-15 17:21:13 +00:00
|
|
|
private function fetchParserOutputFromParsoid( PageIdentity $page, $revision, bool $mayParse ): ?ParserOutput {
|
2022-09-13 15:52:44 +00:00
|
|
|
$parserOptions = ParserOptions::newFromAnon();
|
2024-05-15 17:21:13 +00:00
|
|
|
$parserOptions->setUseParsoid();
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
try {
|
2024-05-15 17:21:13 +00:00
|
|
|
if ( !$page instanceof PageRecord ) {
|
|
|
|
|
$name = "$page";
|
|
|
|
|
$page = $this->pageLookup->getPageByReference( $page );
|
|
|
|
|
if ( !$page ) {
|
|
|
|
|
throw new RevisionAccessException( 'Page {name} not found',
|
|
|
|
|
[ 'name' => $name ] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_int( $revision ) ) {
|
|
|
|
|
$revId = $revision;
|
|
|
|
|
$revision = $this->revisionLookup->getRevisionById( $revId, 0, $page );
|
|
|
|
|
|
|
|
|
|
if ( !$revision ) {
|
|
|
|
|
throw new RevisionAccessException( 'Revision {revId} not found',
|
|
|
|
|
[ 'revId' => $revId ] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $page->getId() !== $revision->getPageId() ) {
|
|
|
|
|
throw new RevisionAccessException( 'Revision {revId} does not belong to page {name}',
|
|
|
|
|
[ 'name' => $page->getDBkey(),
|
|
|
|
|
'revId' => $revision->getId() ] );
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 20:53:02 +00:00
|
|
|
if ( $mayParse ) {
|
2024-05-15 17:21:13 +00:00
|
|
|
try {
|
|
|
|
|
$status = $this->parserOutputAccess->getParserOutput(
|
|
|
|
|
$page, $parserOptions, $revision
|
|
|
|
|
);
|
|
|
|
|
} catch ( ClientError $e ) {
|
|
|
|
|
$status = Status::newFatal( 'parsoid-client-error', $e->getMessage() );
|
|
|
|
|
} catch ( ResourceLimitExceededException $e ) {
|
|
|
|
|
$status = Status::newFatal( 'parsoid-resource-limit-exceeded', $e->getMessage() );
|
|
|
|
|
}
|
2022-09-27 20:53:02 +00:00
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->throwHttpExceptionForStatus( $status );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parserOutput = $status->getValue();
|
|
|
|
|
} else {
|
2024-05-15 17:21:13 +00:00
|
|
|
$parserOutput = $this->parserOutputAccess->getCachedParserOutput(
|
|
|
|
|
$page, $parserOptions, $revision
|
2022-09-27 20:53:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
2022-09-13 15:52:44 +00:00
|
|
|
} catch ( RevisionAccessException $e ) {
|
|
|
|
|
// The client supplied bad revision ID, or the revision was deleted or suppressed.
|
2024-03-05 21:00:43 +00:00
|
|
|
throw new LocalizedHttpException( new MessageValue( "rest-specified-revision-unavailable" ),
|
2022-09-13 15:52:44 +00:00
|
|
|
404,
|
|
|
|
|
[ 'reason' => $e->getMessage() ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
return $parserOutput;
|
2022-09-13 15:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 15:44:57 +00:00
|
|
|
/**
|
|
|
|
|
* @param ParsoidRenderID $renderID
|
|
|
|
|
*
|
2022-10-29 18:52:23 +00:00
|
|
|
* @return SelserContext|null
|
2022-10-14 15:44:57 +00:00
|
|
|
*/
|
2022-10-29 18:52:23 +00:00
|
|
|
private function fetchSelserContextFromStash( $renderID ): ?SelserContext {
|
|
|
|
|
$selserContext = $this->parsoidOutputStash->get( $renderID );
|
2024-09-17 15:11:18 +00:00
|
|
|
$labels = [
|
|
|
|
|
'original_html_given' => 'as_renderid',
|
|
|
|
|
'page_exists' => 'unknown',
|
|
|
|
|
'status' => 'hit-stashed'
|
|
|
|
|
];
|
|
|
|
|
$counter = $this->statsFactory->getCounter( 'html_input_transform_total' );
|
2022-10-29 18:52:23 +00:00
|
|
|
if ( $selserContext ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$counter->setLabels( $labels )
|
|
|
|
|
->copyToStatsdAt( 'html_input_transform.original_html.given.as_renderid.stash_hit.found.hit' )
|
|
|
|
|
->increment();
|
2022-10-29 18:52:23 +00:00
|
|
|
return $selserContext;
|
2022-10-19 15:26:02 +00:00
|
|
|
} else {
|
2022-09-13 15:52:44 +00:00
|
|
|
// Looks like the rendering is gone from stash (or the client send us a bogus key).
|
|
|
|
|
// Try to load it from the parser cache instead.
|
|
|
|
|
// On a wiki with low edit frequency, there is a good chance that it's still there.
|
|
|
|
|
try {
|
2024-05-15 17:21:13 +00:00
|
|
|
$parserOutput = $this->fetchParserOutputFromParsoid( $this->page, $renderID->getRevisionID(), false );
|
2022-10-14 15:44:57 +00:00
|
|
|
|
|
|
|
|
if ( !$parserOutput ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$labels[ 'status' ] = 'miss-fallback_not_found';
|
|
|
|
|
$counter->setLabels( $labels )->copyToStatsdAt(
|
|
|
|
|
'html_input_transform.original_html.given.as_renderid.stash_miss_pc_fallback.not_found.miss'
|
|
|
|
|
)->increment();
|
2022-10-14 15:44:57 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
$cachedRenderID = ParsoidRenderID::newFromParserOutput( $parserOutput );
|
2022-10-14 15:44:57 +00:00
|
|
|
if ( $cachedRenderID->getKey() !== $renderID->getKey() ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$labels[ 'status' ] = 'mismatch-fallback_not_found';
|
|
|
|
|
$counter->setLabels( $labels )
|
|
|
|
|
->copyToStatsdAt(
|
|
|
|
|
'html_input_transform.original_html.given.as_renderid.' .
|
|
|
|
|
'stash_miss_pc_fallback.not_found.mismatch'
|
|
|
|
|
)
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
|
|
|
|
|
// It's not the correct rendering.
|
2022-10-14 15:44:57 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-09-17 15:11:18 +00:00
|
|
|
$labels[ 'status' ] = 'hit-fallback_found';
|
|
|
|
|
$counter->setLabels( $labels )
|
|
|
|
|
->copyToStatsdAt(
|
|
|
|
|
'html_input_transform.original_html.given.as_renderid.' .
|
|
|
|
|
'stash_miss_pc_fallback.found.hit'
|
|
|
|
|
)
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
|
|
|
|
|
$pb = PageBundleParserOutputConverter::pageBundleFromParserOutput( $parserOutput );
|
2022-10-29 18:52:23 +00:00
|
|
|
return new SelserContext( $pb, $renderID->getRevisionID() );
|
2022-09-13 15:52:44 +00:00
|
|
|
} catch ( HttpException $e ) {
|
2024-09-17 15:11:18 +00:00
|
|
|
$labels[ 'status' ] = 'failed-fallback_not_found';
|
|
|
|
|
$counter->setLabels( $labels )
|
|
|
|
|
->copyToStatsdAt(
|
|
|
|
|
'html_input_transform.original_html.given.as_renderid.' .
|
|
|
|
|
'stash_miss_pc_fallback.not_found.failed'
|
|
|
|
|
)
|
|
|
|
|
->increment();
|
2022-10-19 15:26:02 +00:00
|
|
|
|
2022-09-13 15:52:44 +00:00
|
|
|
// If the revision isn't found, don't trigger a 404. Return null to trigger a 412.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-09-09 09:32:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 20:53:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param Status $status
|
|
|
|
|
*
|
|
|
|
|
* @return never
|
|
|
|
|
* @throws HttpException
|
|
|
|
|
*/
|
|
|
|
|
private function throwHttpExceptionForStatus( Status $status ) {
|
|
|
|
|
// TODO: make this nicer.
|
|
|
|
|
if ( $status->hasMessage( 'parsoid-resource-limit-exceeded' ) ) {
|
2024-03-05 21:00:43 +00:00
|
|
|
throw new LocalizedHttpException( new MessageValue( "rest-parsoid-resource-exceeded" ),
|
2022-09-27 20:53:02 +00:00
|
|
|
413,
|
|
|
|
|
[ 'reason' => $status->getHTML() ]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2024-03-05 21:00:43 +00:00
|
|
|
throw new LocalizedHttpException( new MessageValue( "rest-parsoid-error" ),
|
2022-09-27 20:53:02 +00:00
|
|
|
400,
|
|
|
|
|
[ 'reason' => $status->getHTML() ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 19:19:53 +00:00
|
|
|
}
|