ParsoidParser: Record page title in ParserCache entries

* This lets post-cache transforms have access to the title.
* Specifically, DiscussionTools uses this to post-process the HTML.

Bug: T341010
Change-Id: I328f533e6cdb11c0c3a873d23bab1a113dfa39be
This commit is contained in:
Subramanya Sastry 2023-10-26 22:05:54 -05:00
parent 1bc45f74c9
commit 6e5413b1d8
2 changed files with 47 additions and 0 deletions

View file

@ -28,6 +28,13 @@ use WikitextContent;
* @unstable since 1.41; see T236809 for plan.
*/
class ParsoidParser /* eventually this will extend \Parser */ {
/**
* @unstable
* This should not be used widely right now since this may go away.
* This is being added to support DiscussionTools with Parsoid HTML
* and after initial exploration, this may be implemented differently.
*/
public const PARSOID_TITLE_KEY = "parsoid:title-dbkey";
private Parsoid $parsoid;
private PageConfigFactory $pageConfigFactory;
private LanguageConverterFactory $languageConverterFactory;
@ -147,6 +154,13 @@ class ParsoidParser /* eventually this will extend \Parser */ {
$parserOutput = PageBundleParserOutputConverter::parserOutputFromPageBundle( $pageBundle, $parserOutput );
// Record the page title in dbkey form so that post-cache transforms
// have access to the title.
$parserOutput->setExtensionData(
self::PARSOID_TITLE_KEY,
strtr( $pageConfig->getTitle(), ' ', '_', ) // pageConfig returns text, not dbkey
);
// Register a watcher again because the $parserOuptut arg
// and $parserOutput return value above are different objects!
$options->registerWatcher( [ $parserOutput, 'recordOption' ] );

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Tests\Parser\Parsoid;
use MediaWiki\Parser\Parsoid\ParsoidParser;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
use ParserOptions;
@ -27,6 +28,10 @@ class ParsoidParserTest extends MediaWikiIntegrationTestCase {
$output = $parsoidParser->parse( ...$args );
$html = $output->getText( $getTextOpts );
$this->assertStringContainsString( $expected, $html );
$this->assertSame(
$args[1]->getPrefixedDBkey(),
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$this->assertSame( [
'disableContentConversion', 'interfaceMessage', 'wrapclass', 'maxIncludeSize'
], $output->getUsedOptions() );
@ -40,4 +45,32 @@ class ParsoidParserTest extends MediaWikiIntegrationTestCase {
// can extend these tests.
];
}
public function testParsoidParseRevisions() {
$helloWorld = 'Hello, World';
$page = $this->getNonexistingTestPage( 'Test' );
$this->editPage( $page, $helloWorld );
$pageTitle = $page->getTitle();
$parsoidParser = $this->getServiceContainer()
->getParsoidParserFactory()->create();
$output = $parsoidParser->parse(
$helloWorld,
$pageTitle,
ParserOptions::newFromAnon(),
true,
true,
$page->getRevisionRecord()->getId()
);
$html = $output->getText();
$this->assertStringContainsString( $helloWorld, $html );
$this->assertSame(
$pageTitle->getPrefixedDBkey(),
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$this->assertSame( [
'disableContentConversion', 'interfaceMessage', 'wrapclass', 'maxIncludeSize'
], $output->getUsedOptions() );
}
}