wiki.techinc.nl/includes/Rest/Handler/Helper/PageRestHelperFactory.php
C. Scott Ananian 16de2c0851 [ParsoidParser] Remove unneeded code to set render ID
Since I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78 it is part of the
contract of ContentRenderer::getParserOutput() that the render ID (and
other cache parameters) will be set when it returns.
(ContentHandler::getParserOutput() can set them even earlier if it has
custom content-based overrides.)  We had a lot of temporary
backward-compatibility code "later" in the parse process to try to close
the barn door if some code path "forgot" to set them, but these are
unnecessary now.

This patch removes that backward-compatibility code in ParsoidParser;
there is similar remaining code in ParserCache etc. which can be
addressed in follow ups.

(For compatibility we do have to temporarily copy the render ID code
inside ParsoidOutputAccess::parseUncachable, but that class is
deprecated and will be removed.)

The HtmlOutputRendererHelper path which used to call
ParsoidParser::parseFakeRevision() is now replaced with a codepath that
goes through RevisionRenderer.  In order to maintain the same behavior
of the ParsoidHandler, we have also added 'useParsoid' handling to the
JsonContentHandler.  This support can perhaps be deprecated eventually.

Bug: T350538
Change-Id: I0853624cf785f72fd956c6c2336f979f4402a68f
2024-07-19 16:09:32 -04:00

230 lines
7.1 KiB
PHP

<?php
namespace MediaWiki\Rest\Handler\Helper;
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Edit\ParsoidOutputStash;
use MediaWiki\Languages\LanguageConverterFactory;
use MediaWiki\Languages\LanguageFactory;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageLookup;
use MediaWiki\Page\ParserOutputAccess;
use MediaWiki\Page\RedirectStore;
use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
use MediaWiki\Parser\Parsoid\HtmlTransformFactory;
use MediaWiki\Permissions\Authority;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\ResponseFactory;
use MediaWiki\Rest\Router;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionRenderer;
use MediaWiki\Title\TitleFormatter;
use Wikimedia\Bcp47Code\Bcp47Code;
/**
* @since 1.40 Factory for helper objects designed for sharing logic between REST handlers that deal with page content.
* @unstable during Parsoid migration
*/
class PageRestHelperFactory {
/**
* @internal
*/
public const CONSTRUCTOR_OPTIONS = PageContentHelper::CONSTRUCTOR_OPTIONS;
private ServiceOptions $options;
private RevisionLookup $revisionLookup;
private RevisionRenderer $revisionRenderer;
private TitleFormatter $titleFormatter;
private PageLookup $pageLookup;
private ParsoidOutputStash $parsoidOutputStash;
private StatsdDataFactoryInterface $stats;
private ParserOutputAccess $parserOutputAccess;
private ParsoidSiteConfig $parsoidSiteConfig;
private HtmlTransformFactory $htmlTransformFactory;
private IContentHandlerFactory $contentHandlerFactory;
private LanguageFactory $languageFactory;
private RedirectStore $redirectStore;
private LanguageConverterFactory $languageConverterFactory;
/**
* @param ServiceOptions $options
* @param RevisionLookup $revisionLookup
* @param RevisionRenderer $revisionRenderer
* @param TitleFormatter $titleFormatter
* @param PageLookup $pageLookup
* @param ParsoidOutputStash $parsoidOutputStash
* @param StatsdDataFactoryInterface $statsDataFactory
* @param ParserOutputAccess $parserOutputAccess
* @param ParsoidSiteConfig $parsoidSiteConfig
* @param HtmlTransformFactory $htmlTransformFactory
* @param IContentHandlerFactory $contentHandlerFactory
* @param LanguageFactory $languageFactory
* @param RedirectStore $redirectStore
* @param LanguageConverterFactory $languageConverterFactory
*/
public function __construct(
ServiceOptions $options,
RevisionLookup $revisionLookup,
RevisionRenderer $revisionRenderer,
TitleFormatter $titleFormatter,
PageLookup $pageLookup,
ParsoidOutputStash $parsoidOutputStash,
StatsdDataFactoryInterface $statsDataFactory,
ParserOutputAccess $parserOutputAccess,
ParsoidSiteConfig $parsoidSiteConfig,
HtmlTransformFactory $htmlTransformFactory,
IContentHandlerFactory $contentHandlerFactory,
LanguageFactory $languageFactory,
RedirectStore $redirectStore,
LanguageConverterFactory $languageConverterFactory
) {
$this->options = $options;
$this->revisionLookup = $revisionLookup;
$this->revisionRenderer = $revisionRenderer;
$this->titleFormatter = $titleFormatter;
$this->pageLookup = $pageLookup;
$this->parsoidOutputStash = $parsoidOutputStash;
$this->stats = $statsDataFactory;
$this->parserOutputAccess = $parserOutputAccess;
$this->parsoidSiteConfig = $parsoidSiteConfig;
$this->htmlTransformFactory = $htmlTransformFactory;
$this->contentHandlerFactory = $contentHandlerFactory;
$this->languageFactory = $languageFactory;
$this->redirectStore = $redirectStore;
$this->languageConverterFactory = $languageConverterFactory;
}
public function newRevisionContentHelper(): RevisionContentHelper {
return new RevisionContentHelper(
$this->options,
$this->revisionLookup,
$this->titleFormatter,
$this->pageLookup
);
}
public function newPageContentHelper(): PageContentHelper {
return new PageContentHelper(
$this->options,
$this->revisionLookup,
$this->titleFormatter,
$this->pageLookup
);
}
/**
* Should we ignore page id mismatches between page and revision objects
* in HTML/pagebundle requests? Mismatches arise because of page moves.
* This is recommended only for handling calls to internal APIs.
* @note Since 1.43, passing 'null' for $page has been deprecated.
* @note Since 1.43, passing 'null' for $authority has been deprecated.
* @note Since 1.43, passing $lenientRevHandling as the first parameter
* has been deprecated.
* @param bool|PageIdentity|null $page
* If `false`, this argument is used as the value for $lenientRevHandling,
* for backward-compatibility.
* @param array $parameters
* @param ?Authority $authority
* @param int|RevisionRecord|null $revision
* @param bool $lenientRevHandling
*/
public function newHtmlOutputRendererHelper(
$page = null,
array $parameters = [],
?Authority $authority = null,
$revision = null,
bool $lenientRevHandling = false
): HtmlOutputRendererHelper {
if ( is_bool( $page ) ) {
// Backward compatibility w/ pre-1.43 (deprecated)
$lenientRevHandling = $page;
$page = null;
wfDeprecated( __METHOD__ . ' with boolean first parameter', '1.43' );
}
if ( $page === null ) {
wfDeprecated( __METHOD__ . ' with null $page', '1.43' );
}
if ( $authority === null ) {
wfDeprecated( __METHOD__ . ' with null $authority', '1.43' );
}
return new HtmlOutputRendererHelper(
$this->parsoidOutputStash,
$this->stats,
$this->parserOutputAccess,
$this->pageLookup,
$this->revisionLookup,
$this->revisionRenderer,
$this->parsoidSiteConfig,
$this->htmlTransformFactory,
$this->contentHandlerFactory,
$this->languageFactory,
$page,
$parameters,
$authority,
$revision,
$lenientRevHandling
);
}
/**
* @note Since 1.43, passing a null $page is deprecated.
*/
public function newHtmlMessageOutputHelper( ?PageIdentity $page = null ): HtmlMessageOutputHelper {
if ( $page === null ) {
wfDeprecated( __METHOD__ . ' with null $page', '1.43' );
}
return new HtmlMessageOutputHelper( $page );
}
public function newHtmlInputTransformHelper(
$envOptions = [],
?PageIdentity $page = null,
$body = null,
array $parameters = [],
?RevisionRecord $originalRevision = null,
?Bcp47Code $pageLanguage = null
): HtmlInputTransformHelper {
if ( $page === null || $body === null ) {
wfDeprecated( __METHOD__ . ' without $page or $body' );
}
return new HtmlInputTransformHelper(
$this->stats,
$this->htmlTransformFactory,
$this->parsoidOutputStash,
$this->parserOutputAccess,
$this->pageLookup,
$this->revisionLookup,
$envOptions,
$page,
$body ?? '',
$parameters,
$originalRevision,
$pageLanguage
);
}
/**
* @since 1.41
*/
public function newPageRedirectHelper(
ResponseFactory $responseFactory,
Router $router,
string $route,
RequestInterface $request
): PageRedirectHelper {
return new PageRedirectHelper(
$this->redirectStore,
$this->titleFormatter,
$responseFactory,
$router,
$route,
$request,
$this->languageConverterFactory
);
}
}