2022-06-17 14:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
2024-05-17 10:11:24 +00:00
|
|
|
use MediaWiki\Content\JavaScriptContent;
|
2024-08-06 13:40:20 +00:00
|
|
|
use MediaWiki\Content\WikitextContent;
|
2023-11-06 15:22:44 +00:00
|
|
|
use MediaWiki\Edit\ParsoidRenderID;
|
2023-08-29 20:13:43 +00:00
|
|
|
use MediaWiki\Page\ParserOutputAccess;
|
2024-06-13 21:21:02 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2023-03-09 15:52:24 +00:00
|
|
|
use MediaWiki\Parser\ParserOutputFlags;
|
2022-09-26 06:18:43 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
|
2023-08-29 20:13:43 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidParser;
|
|
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
|
2022-08-30 10:26:39 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Revision\RevisionAccessException;
|
2023-06-13 18:34:25 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2023-12-11 14:59:55 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-06-17 14:00:27 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2024-07-09 13:37:44 +00:00
|
|
|
use Wikimedia\ObjectCache\BagOStuff;
|
|
|
|
|
use Wikimedia\ObjectCache\HashBagOStuff;
|
2022-06-17 14:00:27 +00:00
|
|
|
use Wikimedia\Parsoid\Config\PageConfig;
|
2023-03-09 15:52:24 +00:00
|
|
|
use Wikimedia\Parsoid\Core\ContentMetadataCollector;
|
2022-06-17 14:00:27 +00:00
|
|
|
use Wikimedia\Parsoid\Core\PageBundle;
|
|
|
|
|
use Wikimedia\Parsoid\Parsoid;
|
2023-06-01 19:18:52 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2022-06-17 14:00:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class ParsoidOutputAccessTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
private const WIKITEXT = 'Hello \'\'\'Parsoid\'\'\'!';
|
|
|
|
|
private const MOCKED_HTML = 'mocked HTML';
|
2022-10-05 18:47:44 +00:00
|
|
|
private const ENV_OPTS = [ 'pageBundle' => true ];
|
2022-06-17 14:00:27 +00:00
|
|
|
|
2024-05-17 16:42:00 +00:00
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
// This class is deprecated, as all are its methods.
|
|
|
|
|
$this->filterDeprecated( '/' . preg_quote( ParsoidOutputAccess::class, '/' ) . '/' );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $expectedCalls
|
2023-06-01 19:18:52 +00:00
|
|
|
* @param string|null $version
|
2022-06-17 14:00:27 +00:00
|
|
|
*
|
|
|
|
|
* @return MockObject|Parsoid
|
|
|
|
|
*/
|
2023-06-01 19:18:52 +00:00
|
|
|
private function newMockParsoid( int $expectedCalls = 1, ?string $version = null ) {
|
2022-06-17 14:00:27 +00:00
|
|
|
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
|
|
|
|
|
$parsoid->expects( $this->exactly( $expectedCalls ) )->method( 'wikitext2html' )->willReturnCallback(
|
2023-06-01 19:18:52 +00:00
|
|
|
static function (
|
|
|
|
|
PageConfig $pageConfig, $options, &$headers, ?ContentMetadataCollector $metadata = null
|
|
|
|
|
) use ( $version ) {
|
2022-06-17 14:00:27 +00:00
|
|
|
$wikitext = $pageConfig->getRevisionContent()->getContent( SlotRecord::MAIN );
|
2023-03-09 15:52:24 +00:00
|
|
|
if ( $metadata !== null ) {
|
|
|
|
|
$metadata->setExtensionData( 'my-key', 'my-data' );
|
|
|
|
|
$metadata->setPageProperty( 'forcetoc', '' );
|
|
|
|
|
$metadata->setOutputFlag( ParserOutputFlags::NO_GALLERY );
|
|
|
|
|
}
|
2022-09-09 09:32:17 +00:00
|
|
|
|
|
|
|
|
return new PageBundle(
|
|
|
|
|
self::MOCKED_HTML . ' of ' . $wikitext,
|
|
|
|
|
[ 'parsoid-data' ],
|
|
|
|
|
[ 'mw-data' ],
|
2023-06-01 19:18:52 +00:00
|
|
|
$version ?? Parsoid::defaultHTMLVersion(),
|
2022-09-09 09:32:17 +00:00
|
|
|
[ 'content-language' => 'en' ],
|
|
|
|
|
$pageConfig->getContentModel()
|
|
|
|
|
);
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $parsoid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $expectedParses
|
|
|
|
|
* @param array $parsoidCacheConfig
|
2022-12-08 07:34:25 +00:00
|
|
|
* @param BagOStuff|null $parserCacheBag
|
2023-06-01 19:18:52 +00:00
|
|
|
* @param string|null $version
|
2022-06-17 14:00:27 +00:00
|
|
|
*
|
|
|
|
|
* @return ParsoidOutputAccess
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-08-29 20:13:43 +00:00
|
|
|
private function resetServicesWithMockedParsoid(
|
2022-12-08 07:34:25 +00:00
|
|
|
$expectedParses,
|
|
|
|
|
$parsoidCacheConfig = [],
|
2023-06-01 19:18:52 +00:00
|
|
|
?BagOStuff $parserCacheBag = null,
|
|
|
|
|
?string $version = null
|
2023-08-29 20:13:43 +00:00
|
|
|
): void {
|
2022-06-17 14:00:27 +00:00
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
|
2023-06-01 19:18:52 +00:00
|
|
|
$mockParsoid = $this->newMockParsoid( $expectedParses, $version );
|
2023-08-29 20:13:43 +00:00
|
|
|
$parsoidParser = new ParsoidParser(
|
|
|
|
|
$mockParsoid,
|
|
|
|
|
$services->getParsoidPageConfigFactory(),
|
|
|
|
|
$services->getLanguageConverterFactory(),
|
|
|
|
|
$services->getParserFactory(),
|
|
|
|
|
$services->getGlobalIdGenerator()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Create a mock Parsoid factory that returns the ParsoidParser object
|
|
|
|
|
// with the mocked Parsoid object.
|
|
|
|
|
$mockParsoidParserFactory = $this->createNoOpMock( ParsoidParserFactory::class, [ 'create' ] );
|
|
|
|
|
$mockParsoidParserFactory->expects( $this->exactly( $expectedParses ) )
|
|
|
|
|
->method( 'create' )
|
|
|
|
|
->willReturn( $parsoidParser );
|
|
|
|
|
|
|
|
|
|
$this->setService( 'ParsoidParserFactory', $mockParsoidParserFactory );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-01 19:18:52 +00:00
|
|
|
* @param ?ParserOutputAccess $parserOutputAccess
|
2023-08-29 20:13:43 +00:00
|
|
|
* @return ParsoidOutputAccess
|
|
|
|
|
*/
|
2023-06-01 19:18:52 +00:00
|
|
|
private function getParsoidOutputAccessWithCache(
|
|
|
|
|
?ParserOutputAccess $parserOutputAccess = null
|
|
|
|
|
): ParsoidOutputAccess {
|
2023-08-29 20:13:43 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2022-06-17 14:00:27 +00:00
|
|
|
return new ParsoidOutputAccess(
|
2023-08-29 20:13:43 +00:00
|
|
|
$services->getParsoidParserFactory(),
|
2023-06-01 19:18:52 +00:00
|
|
|
$parserOutputAccess ?? $services->getParserOutputAccess(),
|
2022-09-13 15:52:44 +00:00
|
|
|
$services->getPageStore(),
|
2022-06-17 14:00:27 +00:00
|
|
|
$services->getRevisionLookup(),
|
2022-06-29 17:01:21 +00:00
|
|
|
$services->getParsoidSiteConfig(),
|
2022-12-08 22:01:49 +00:00
|
|
|
$services->getContentHandlerFactory()
|
2022-06-17 14:00:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ParserOptions
|
|
|
|
|
*/
|
|
|
|
|
private function getParserOptions() {
|
|
|
|
|
return ParserOptions::newFromAnon();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
private function getHtml( $value ) {
|
|
|
|
|
if ( $value instanceof StatusValue ) {
|
|
|
|
|
$value = $value->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $value instanceof ParserOutput ) {
|
|
|
|
|
$value = $value->getRawText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$html = preg_replace( '/<!--.*?-->/s', '', $value );
|
2023-03-24 03:21:20 +00:00
|
|
|
$html = trim( preg_replace( '/[\r\n]{2,}/', "\n", $html ) );
|
|
|
|
|
$html = trim( preg_replace( '/\s{2,}/', ' ', $html ) );
|
2022-06-28 10:30:44 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function assertContainsHtml( $needle, $actual, $msg = '' ) {
|
|
|
|
|
$this->assertNotNull( $actual );
|
|
|
|
|
|
|
|
|
|
if ( $actual instanceof StatusValue ) {
|
|
|
|
|
$this->assertStatusOK( $actual, 'isOK' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString( $needle, $this->getHtml( $actual ), $msg );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutputThrowsIfRevisionNotFound() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 0 );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-06-17 14:00:27 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$this->expectException( RevisionAccessException::class );
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will return output.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutput() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 1 );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-06-17 14:00:27 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
|
|
|
|
|
|
|
|
|
$output = $status->getValue();
|
2022-06-17 14:00:27 +00:00
|
|
|
|
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
|
|
|
// check that ParsoidRenderID::newFromParserOutput() doesn't throw
|
|
|
|
|
$this->assertNotNull( ParsoidRenderID::newFromParserOutput( $output ) );
|
2022-06-28 09:49:36 +00:00
|
|
|
|
2022-09-26 06:18:43 +00:00
|
|
|
// Ensure that we can still create a valid instance of PageBundle from the ParserOutput
|
|
|
|
|
$pageBundle = PageBundleParserOutputConverter::pageBundleFromParserOutput( $output );
|
2022-06-28 09:49:36 +00:00
|
|
|
$this->assertSame( $output->getRawText(), $pageBundle->html );
|
|
|
|
|
|
2022-09-26 06:18:43 +00:00
|
|
|
// Ensure that the expected mw and parsoid fields are set in the PageBundle
|
2022-06-28 09:49:36 +00:00
|
|
|
$this->assertNotEmpty( $pageBundle->mw );
|
|
|
|
|
$this->assertNotEmpty( $pageBundle->parsoid );
|
2022-09-09 09:32:17 +00:00
|
|
|
$this->assertNotEmpty( $pageBundle->headers );
|
|
|
|
|
$this->assertNotEmpty( $pageBundle->version );
|
2023-03-09 15:52:24 +00:00
|
|
|
|
|
|
|
|
// Check that the metadata set by our mock parsoid is preserved
|
|
|
|
|
$this->checkMetadata( $output );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will place the generated output for the latest revision
|
|
|
|
|
* in the parsoid parser cache.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionIsCached() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 1 );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-06-17 14:00:27 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
2022-09-13 15:52:44 +00:00
|
|
|
// Get the ParserOutput from cache
|
|
|
|
|
$status = $access->getCachedParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput from cache, without supplying a PageRecord
|
|
|
|
|
$status = $access->getCachedParserOutput( $page->getTitle(), $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
// Get the ParserOutput again, this should not trigger a new parse.
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will force a parse since we know that
|
|
|
|
|
* the revision is not in the cache.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionWithForceParse() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 2 );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-06-17 14:00:27 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput again, this should trigger a new parse
|
|
|
|
|
// since we're forcing it to.
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput(
|
2022-06-17 14:00:27 +00:00
|
|
|
$page,
|
|
|
|
|
$parserOptions,
|
|
|
|
|
null,
|
2023-08-29 20:13:43 +00:00
|
|
|
ParserOutputAccess::OPT_FORCE_PARSE
|
2022-06-17 14:00:27 +00:00
|
|
|
);
|
2022-06-28 10:30:44 +00:00
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:58:56 +00:00
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will force a parse since we know that
|
|
|
|
|
* the revision is not in the cache.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionWithNoUpdateCache() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$cacheBag = $this->getMockBuilder( HashBagOStuff::class )
|
|
|
|
|
->onlyMethods( [ 'set', 'setMulti' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$cacheBag->expects( $this->never() )->method( 'set' );
|
|
|
|
|
$cacheBag->expects( $this->never() )->method( 'setMulti' );
|
|
|
|
|
|
|
|
|
|
// ParserCache should not get anything stored in it.
|
|
|
|
|
$this->resetServicesWithMockedParsoid( 1, [], $cacheBag );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-11-24 19:58:56 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
|
|
|
|
$status = $access->getParserOutput(
|
|
|
|
|
$page,
|
|
|
|
|
$parserOptions,
|
|
|
|
|
null,
|
2023-08-29 20:13:43 +00:00
|
|
|
ParserOutputAccess::OPT_NO_UPDATE_CACHE
|
2022-11-24 19:58:56 +00:00
|
|
|
);
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-11-24 19:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-08 07:01:30 +00:00
|
|
|
/**
|
2022-12-08 07:34:25 +00:00
|
|
|
* Tests that getParserOutput() will not call Parsoid and will not write to ParserCache
|
|
|
|
|
* for unsupported content models.
|
2022-12-08 07:01:30 +00:00
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
2024-03-06 09:04:24 +00:00
|
|
|
public function testNonParsoidOutput() {
|
2022-12-08 07:34:25 +00:00
|
|
|
// Expect no cache writes!
|
|
|
|
|
$cacheBag = $this->getMockBuilder( HashBagOStuff::class )
|
|
|
|
|
->onlyMethods( [ 'set', 'setMulti' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$cacheBag->expects( $this->never() )->method( 'set' );
|
|
|
|
|
$cacheBag->expects( $this->never() )->method( 'setMulti' );
|
|
|
|
|
|
|
|
|
|
// Expect no calls to parsoid!
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 0, [], $cacheBag );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-12-08 07:01:30 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, new JavaScriptContent( '"not wikitext"' ) );
|
|
|
|
|
|
|
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
2024-03-06 09:04:24 +00:00
|
|
|
$this->assertContainsHtml( 'not wikitext', $status );
|
2022-12-08 07:01:30 +00:00
|
|
|
|
2023-01-09 10:45:25 +00:00
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2024-03-06 09:04:24 +00:00
|
|
|
$this->assertNotNull(
|
|
|
|
|
ParsoidRenderID::newFromParserOutput( $parserOutput )->getKey()
|
|
|
|
|
);
|
2022-12-08 07:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
public function testOldRevisionIsCached() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 1 );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-06-17 14:00:27 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$status1 = $this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
$rev = $status1->getValue()['revision-record'];
|
|
|
|
|
|
|
|
|
|
// Make an edit so that the revision we're getting output
|
|
|
|
|
// for below is not the current revision.
|
|
|
|
|
$this->editPage( $page, 'Second revision' );
|
|
|
|
|
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions, $rev );
|
|
|
|
|
|
2022-09-13 15:52:44 +00:00
|
|
|
// Get the ParserOutput from cache, using revision object
|
|
|
|
|
$status = $access->getCachedParserOutput( $page, $parserOptions, $rev );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput from cache, using revision ID
|
|
|
|
|
$status = $access->getCachedParserOutput( $page->getTitle(), $parserOptions, $rev->getId() );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
// Get the ParserOutput again, this should not trigger a new parse.
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions, $rev );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetParserOutputWithOldRevision() {
|
2023-08-29 20:13:43 +00:00
|
|
|
$this->resetServicesWithMockedParsoid( 2 );
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache();
|
2022-06-17 14:00:27 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$status1 = $this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
$rev1 = $status1->getValue()['revision-record'];
|
|
|
|
|
|
|
|
|
|
$this->editPage( $page, 'Second revision' );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status2 = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of Second revision', $status2 );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status2 );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status1 = $access->getParserOutput( $page, $parserOptions, $rev1 );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status1 );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status1 );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
// Again, using just the revision ID
|
|
|
|
|
$status1 = $access->getParserOutput( $page, $parserOptions, $rev1->getId() );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status1 );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status1 );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
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
|
|
|
// check that ParsoidRenderID::newFromParserOutput() doesn't throw
|
2022-06-28 10:30:44 +00:00
|
|
|
$output1 = $status1->getValue();
|
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
|
|
|
$this->assertNotNull( ParsoidRenderID::newFromParserOutput( $output1 ) );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
2022-06-29 17:01:21 +00:00
|
|
|
|
2022-08-30 10:26:39 +00:00
|
|
|
/**
|
2023-08-29 20:13:43 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
2022-08-30 10:26:39 +00:00
|
|
|
*/
|
|
|
|
|
public function testParseWithPageRecordAndNoRevision() {
|
|
|
|
|
$pageRecord = $this->getExistingTestPage( __METHOD__ )->toPageRecord();
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2023-08-29 20:13:43 +00:00
|
|
|
$status = $parsoidOutputAccess->getParserOutput( $pageRecord, $pOpts, null );
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
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
|
|
|
$this->assertNotEmpty( $parserOutput->getRenderId() );
|
2022-08-30 10:26:39 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 15:52:24 +00:00
|
|
|
private function checkMetadata( $output ) {
|
|
|
|
|
$parserOutput = $output instanceof StatusValue ? $output->getValue() : $output;
|
|
|
|
|
|
|
|
|
|
// Check the metadata added by ::newMockParsoid() is preserved
|
|
|
|
|
$this->assertSame( 'my-data', $parserOutput->getExtensionData( 'my-key' ) );
|
|
|
|
|
$this->assertSame( '', $parserOutput->getPageProperty( 'forcetoc' ) );
|
|
|
|
|
$this->assertSame( true, $parserOutput->getOutputFlag( ParserOutputFlags::NO_GALLERY ) );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 10:26:39 +00:00
|
|
|
/**
|
2023-08-29 20:13:43 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
2022-08-30 10:26:39 +00:00
|
|
|
*/
|
|
|
|
|
public function testParseWithPageRecordAndRevision() {
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
|
|
|
|
$pageRecord = $page->toPageRecord();
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
$revRecord = $page->getRevisionRecord();
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2023-08-29 20:13:43 +00:00
|
|
|
$status = $parsoidOutputAccess->getParserOutput( $pageRecord, $pOpts, $revRecord );
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
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
|
|
|
$this->assertNotEmpty( $parserOutput->getRenderId() );
|
2022-08-30 10:26:39 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 14:27:01 +00:00
|
|
|
/**
|
2023-08-29 20:13:43 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
2022-10-07 14:27:01 +00:00
|
|
|
*/
|
|
|
|
|
public function testParseWithPageIdentityAndRevisionId() {
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
$revId = $page->getLatest();
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2023-08-29 20:13:43 +00:00
|
|
|
$status = $parsoidOutputAccess->getParserOutput( $page->getTitle(), $pOpts, $revId );
|
2022-10-07 14:27:01 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
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
|
|
|
$this->assertNotEmpty( $parserOutput->getRenderId() );
|
2022-10-07 14:27:01 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 10:26:39 +00:00
|
|
|
/**
|
2023-08-29 20:13:43 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseUncacheable
|
2022-08-30 10:26:39 +00:00
|
|
|
*/
|
|
|
|
|
public function testParseWithNonExistingPageAndFakeRevision() {
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
|
|
|
|
|
// Create a fake revision record
|
|
|
|
|
$revRecord = new MutableRevisionRecord( $page->getTitle() );
|
|
|
|
|
$revRecord->setId( 0 );
|
|
|
|
|
$revRecord->setPageId( $page->getId() );
|
|
|
|
|
$revRecord->setContent(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
new WikitextContent( 'test' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2023-08-29 20:13:43 +00:00
|
|
|
$status = $parsoidOutputAccess->parseUncacheable( $page->getTitle(), $pOpts, $revRecord );
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
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
|
|
|
$this->assertNotEmpty( $parserOutput->getRenderId() );
|
2022-08-30 10:26:39 +00:00
|
|
|
// The revision ID is set to 0, so that's what is in the cache.
|
|
|
|
|
$this->assertSame( 0, $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 18:34:25 +00:00
|
|
|
/**
|
2023-08-29 20:13:43 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseUncacheable
|
2023-06-13 18:34:25 +00:00
|
|
|
*/
|
|
|
|
|
public function testParseDeletedRevision() {
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
|
|
|
|
|
// Create a fake revision record
|
|
|
|
|
$revRecord = new MutableRevisionRecord( $page->getTitle() );
|
|
|
|
|
$revRecord->setId( 0 );
|
|
|
|
|
$revRecord->setPageId( $page->getId() );
|
|
|
|
|
$revRecord->setContent(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
new WikitextContent( 'test' )
|
|
|
|
|
);
|
|
|
|
|
// Induce a RevisionAccessException
|
|
|
|
|
$revRecord->setVisibility( RevisionRecord::DELETED_TEXT );
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2023-08-29 20:13:43 +00:00
|
|
|
$status = $parsoidOutputAccess->parseUncacheable( $page->getTitle(), $pOpts, $revRecord );
|
2023-06-13 18:34:25 +00:00
|
|
|
|
2024-06-13 01:04:26 +00:00
|
|
|
$this->assertStatusNotOK( $status );
|
|
|
|
|
$this->assertStatusMessagesExactly(
|
|
|
|
|
StatusValue::newFatal( 'parsoid-revision-access', 'Not an available content version.' ),
|
|
|
|
|
$status
|
2023-06-13 18:34:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 14:27:01 +00:00
|
|
|
/**
|
|
|
|
|
* Mock the language class based on a language code.
|
|
|
|
|
*
|
|
|
|
|
* @param string $langCode
|
|
|
|
|
*
|
|
|
|
|
* @return Language|Language&MockObject|MockObject
|
|
|
|
|
*/
|
|
|
|
|
private function getLanguageMock( string $langCode ) {
|
|
|
|
|
$language = $this->createMock( Language::class );
|
|
|
|
|
$language->method( 'getCode' )->willReturn( $langCode );
|
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->method( 'getDir' )->willReturn( 'ltr' );
|
|
|
|
|
$bcp47 = LanguageCode::bcp47( $langCode );
|
|
|
|
|
$language
|
|
|
|
|
->method( 'getHtmlCode' )
|
|
|
|
|
->willReturn( $bcp47 );
|
|
|
|
|
$language
|
|
|
|
|
->method( 'toBcp47Code' )
|
|
|
|
|
->willReturn( $bcp47 );
|
2022-10-07 14:27:01 +00:00
|
|
|
return $language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return Generator */
|
|
|
|
|
public function provideParserOptionsWithLanguageOverride() {
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
2023-08-29 20:13:43 +00:00
|
|
|
$parserOptions->method( 'optionsHash' )->willReturn( '' );
|
|
|
|
|
$parserOptions->method( 'getUseParsoid' )->willReturn( true );
|
2022-10-07 14:27:01 +00:00
|
|
|
$parserOptions->method( 'getTargetLanguage' )
|
|
|
|
|
->willReturn( null );
|
|
|
|
|
yield 'ParserOptions with no language' => [ $parserOptions, 'en' ];
|
|
|
|
|
|
|
|
|
|
$langCode = 'de';
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
2023-08-29 20:13:43 +00:00
|
|
|
$parserOptions->method( 'optionsHash' )->willReturn( '' );
|
|
|
|
|
$parserOptions->method( 'getUseParsoid' )->willReturn( true );
|
2022-10-07 14:27:01 +00:00
|
|
|
$parserOptions->method( 'getTargetLanguage' )
|
|
|
|
|
->willReturn( $this->getLanguageMock( $langCode ) );
|
|
|
|
|
yield 'ParserOptions for "de" language' => [ $parserOptions, $langCode ];
|
|
|
|
|
|
|
|
|
|
$langCode = 'ar';
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
2023-08-29 20:13:43 +00:00
|
|
|
$parserOptions->method( 'optionsHash' )->willReturn( '' );
|
|
|
|
|
$parserOptions->method( 'getUseParsoid' )->willReturn( true );
|
2022-10-07 14:27:01 +00:00
|
|
|
$parserOptions->method( 'getTargetLanguage' )
|
|
|
|
|
->willReturn( $this->getLanguageMock( $langCode ) );
|
|
|
|
|
yield 'ParserOptions for "ar" language' => [ $parserOptions, $langCode ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-13 21:30:21 +00:00
|
|
|
* @covers \MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper::getParserOutput
|
2022-10-07 14:27:01 +00:00
|
|
|
* @dataProvider provideParserOptionsWithLanguageOverride
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutputWithLanguageOverride( $parserOptions, $expectedLangCode ) {
|
|
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
$parserOutputAccess = $services->getParsoidOutputAccess();
|
|
|
|
|
|
2023-08-09 15:43:50 +00:00
|
|
|
$content = 'Test content for ' . __METHOD__;
|
|
|
|
|
$page = Title::makeTitle( NS_MAIN, 'TestGetParserOutputWithLanguageOverride' );
|
|
|
|
|
$this->editPage( $page, $content );
|
2022-10-07 14:27:01 +00:00
|
|
|
|
|
|
|
|
$status = $parserOutputAccess->getParserOutput( $page, $parserOptions );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
|
|
|
|
|
// assert dummy content in parsoid output HTML
|
2022-05-27 16:38:32 +00:00
|
|
|
$html = $status->getValue()->getRawText();
|
2023-08-09 15:43:50 +00:00
|
|
|
$this->assertStringContainsString( $content, $html );
|
2022-10-07 14:27:01 +00:00
|
|
|
|
|
|
|
|
if ( $parserOptions->getTargetLanguage() !== null ) {
|
|
|
|
|
$targetLanguage = $parserOptions->getTargetLanguage()->getCode();
|
|
|
|
|
$this->assertSame( $expectedLangCode, $targetLanguage );
|
|
|
|
|
$this->assertInstanceOf( Language::class, $parserOptions->getTargetLanguage() );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertNull( $parserOptions->getTargetLanguage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assert the page language in parsoid output HTML
|
|
|
|
|
$this->assertStringContainsString( 'lang="' . $expectedLangCode . '"', $html );
|
|
|
|
|
$this->assertStringContainsString( 'content="' . $expectedLangCode . '"', $html );
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 19:18:52 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testRerenderForNonDefaultVersion() {
|
|
|
|
|
// Rendering is asked for twice because version is not the Parsoid default
|
|
|
|
|
// so even though the output is found in the primary cache, it's obsolete.
|
|
|
|
|
$this->resetServicesWithMockedParsoid( 2, [], null, '1.1.1' );
|
|
|
|
|
|
|
|
|
|
$parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( $parserOutputAccess );
|
|
|
|
|
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
$page = $this->getExistingTestPage();
|
|
|
|
|
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
|
|
|
|
|
// Clear the localCache since that has priority and updating the Parsoid
|
|
|
|
|
// default version would require a process restart anyways.
|
|
|
|
|
$testingAccess = TestingAccessWrapper::newFromObject( $parserOutputAccess );
|
|
|
|
|
$testingAccess->localCache->clear();
|
|
|
|
|
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|