private function addWikiTextTitleInternal(
$text, Title $title, $linestart, $tidy, $interface, $wrapperClass = null
) {
- global $wgParser;
-
if ( !$tidy ) {
wfDeprecated( 'disabling tidy', '1.32' );
}
- $popts = $this->parserOptions();
- $oldTidy = $popts->setTidy( $tidy );
- $popts->setInterfaceMessage( (bool)$interface );
-
- $parserOutput = $wgParser->getFreshParser()->parse(
- $text, $title, $popts,
- $linestart, true, $this->mRevisionId
+ $parserOutput = $this->parseInternal(
+ $text, $title, $linestart, $tidy, $interface, /*language*/null
);
- $popts->setTidy( $oldTidy );
-
$this->addParserOutput( $parserOutput, [
'enableSectionEditLinks' => false,
'wrapperDivClass' => $wrapperClass ?? '',
* @param Language|null $language Target language object, will override $interface
* @throws MWException
* @return string HTML
+ * @deprecated since 1.33, due to untidy output and inconsistent wrapper;
+ * use parseAsContent() if $interface is default value or false, or else
+ * parseAsInterface() if $interface is true.
*/
public function parse( $text, $linestart = true, $interface = false, $language = null ) {
return $this->parseInternal(
- $text, $linestart, $interface, $language
+ $text, $this->getTitle(), $linestart, /*tidy*/false, $interface, $language
+ )->getText( [
+ 'enableSectionEditLinks' => false,
+ ] );
+ }
+
+ /**
+ * Parse wikitext *in the page content language* and return the HTML.
+ * The result will be language-converted to the user's preferred variant.
+ * Output will be tidy.
+ *
+ * @param string $text Wikitext in the page content language
+ * @param bool $linestart Is this the start of a line? (Defaults to true)
+ * @throws MWException
+ * @return string HTML
+ * @since 1.33
+ */
+ public function parseAsContent( $text, $linestart = true ) {
+ return $this->parseInternal(
+ $text, $this->getTitle(), $linestart, /*tidy*/true, /*interface*/false, /*language*/null
)->getText( [
'enableSectionEditLinks' => false,
+ 'wrapperDivClass' => ''
] );
}
+ /**
+ * Parse wikitext *in the user interface language* and return the HTML.
+ * The result will not be language-converted, as user interface messages
+ * are already localized into a specific variant.
+ * Output will be tidy.
+ *
+ * @param string $text Wikitext in the user interface language
+ * @param bool $linestart Is this the start of a line? (Defaults to true)
+ * @throws MWException
+ * @return string HTML
+ * @since 1.33
+ */
+ public function parseAsInterface( $text, $linestart = true ) {
+ return $this->parseInternal(
+ $text, $this->getTitle(), $linestart, /*tidy*/true, /*interface*/true, /*language*/null
+ )->getText( [
+ 'enableSectionEditLinks' => false,
+ 'wrapperDivClass' => ''
+ ] );
+ }
+
+ /**
+ * Parse wikitext *in the user interface language*, strip
+ * paragraph wrapper, and return the HTML.
+ * The result will not be language-converted, as user interface messages
+ * are already localized into a specific variant.
+ * Output will be tidy. Outer paragraph wrapper will only be stripped
+ * if the result is a single paragraph.
+ *
+ * @param string $text Wikitext in the user interface language
+ * @param bool $linestart Is this the start of a line? (Defaults to true)
+ * @throws MWException
+ * @return string HTML
+ * @since 1.33
+ */
+ public function parseInlineAsInterface( $text, $linestart = true ) {
+ return Parser::stripOuterParagraph(
+ $this->parseAsInterface( $text, $linestart )
+ );
+ }
+
/**
* Parse wikitext, strip paragraph wrapper, and return the HTML.
*
* @param bool $interface Use interface language (instead of content language) while parsing
* language sensitive magic words like GRAMMAR and PLURAL
* @return string HTML
+ * @deprecated since 1.33, due to untidy output and confusing default
+ * for $interface. Use parseInlineAsInterface() if $interface is
+ * the default value or false, or else use
+ * Parser::stripOuterParagraph($outputPage->parseAsContent(...)).
*/
public function parseInline( $text, $linestart = true, $interface = false ) {
$parsed = $this->parseInternal(
- $text, $linestart, $interface, /*language*/null
+ $text, $this->getTitle(), $linestart, /*tidy*/false, $interface, /*language*/null
)->getText( [
'enableSectionEditLinks' => false,
'wrapperDivClass' => '', /* no wrapper div */
* Parse wikitext and return the HTML (internal implementation helper)
*
* @param string $text
+ * @param Title The title to use
* @param bool $linestart Is this the start of a line?
+ * @param bool $tidy Whether the output should be tidied
* @param bool $interface Use interface language (instead of content language) while parsing
* language sensitive magic words like GRAMMAR and PLURAL. This also disables
* LanguageConverter.
* @throws MWException
* @return ParserOutput
*/
- private function parseInternal( $text, $linestart, $interface, $language ) {
+ private function parseInternal( $text, $title, $linestart, $tidy, $interface, $language ) {
global $wgParser;
- if ( is_null( $this->getTitle() ) ) {
+ if ( is_null( $title ) ) {
throw new MWException( 'Empty $mTitle in ' . __METHOD__ );
}
$popts = $this->parserOptions();
- if ( $interface ) {
- $popts->setInterfaceMessage( true );
- }
+ $oldTidy = $popts->setTidy( $tidy );
+ $oldInterface = $popts->setInterfaceMessage( (bool)$interface );
+
if ( $language !== null ) {
$oldLang = $popts->setTargetLanguage( $language );
}
$parserOutput = $wgParser->getFreshParser()->parse(
- $text, $this->getTitle(), $popts,
+ $text, $title, $popts,
$linestart, true, $this->mRevisionId
);
- if ( $interface ) {
- $popts->setInterfaceMessage( false );
- }
+ $popts->setTidy( $oldTidy );
+ $popts->setInterfaceMessage( $oldInterface );
+
if ( $language !== null ) {
$popts->setTargetLanguage( $oldLang );
}
];
}
+ /**
+ * @dataProvider provideParseAs
+ * @covers OutputPage::parseAsContent
+ * @param array $args To pass to parse()
+ * @param string $expectedHTML Expected return value for parseAsContent()
+ * @param string $expectedHTML Expected return value for parseInlineAsInterface(), if different
+ */
+ public function testParseAsContent(
+ array $args, $expectedHTML, $expectedHTMLInline = null
+ ) {
+ $op = $this->newInstance();
+ $this->assertSame( $expectedHTML, $op->parseAsContent( ...$args ) );
+ }
+
+ /**
+ * @dataProvider provideParseAs
+ * @covers OutputPage::parseAsInterface
+ * @param array $args To pass to parse()
+ * @param string $expectedHTML Expected return value for parseAsInterface()
+ * @param string $expectedHTML Expected return value for parseInlineAsInterface(), if different
+ */
+ public function testParseAsInterface(
+ array $args, $expectedHTML, $expectedHTMLInline = null
+ ) {
+ $op = $this->newInstance();
+ $this->assertSame( $expectedHTML, $op->parseAsInterface( ...$args ) );
+ }
+
+ /**
+ * @dataProvider provideParseAs
+ * @covers OutputPage::parseInlineAsInterface
+ */
+ public function testParseInlineAsInterface(
+ array $args, $expectedHTML, $expectedHTMLInline = null
+ ) {
+ $op = $this->newInstance();
+ $this->assertSame(
+ $expectedHTMLInline ?? $expectedHTML,
+ $op->parseInlineAsInterface( ...$args )
+ );
+ }
+
+ public function provideParseAs() {
+ return [
+ 'List at start of line' => [
+ [ '* List', true ],
+ "<ul><li>List</li></ul>\n",
+ ],
+ 'List not at start' => [
+ [ "* ''Not'' list", false ],
+ '<p>* <i>Not</i> list</p>',
+ '* <i>Not</i> list',
+ ],
+ 'Italics' => [
+ [ "''Italic''", true ],
+ "<p><i>Italic</i>\n</p>",
+ '<i>Italic</i>',
+ ],
+ 'formatnum' => [
+ [ '{{formatnum:123456.789}}', true ],
+ "<p>123,456.789\n</p>",
+ "123,456.789",
+ ],
+ 'No section edit links' => [
+ [ '== Header ==' ],
+ '<h2><span class="mw-headline" id="Header">Header</span></h2>' .
+ "\n",
+ ]
+ ];
+ }
+
/**
* @covers OutputPage::parse
*/
public function testParseNullTitle() {
- $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parse' );
+ $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
$op = $this->newInstance( [], null, 'notitle' );
$op->parse( '' );
}
/**
- * @covers OutputPage::parse
+ * @covers OutputPage::parseInline
*/
public function testParseInlineNullTitle() {
- $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parse' );
+ $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
$op = $this->newInstance( [], null, 'notitle' );
$op->parseInline( '' );
}
+ /**
+ * @covers OutputPage::parseAsContent
+ */
+ public function testParseAsContentNullTitle() {
+ $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
+ $op = $this->newInstance( [], null, 'notitle' );
+ $op->parseAsContent( '' );
+ }
+
+ /**
+ * @covers OutputPage::parseAsInterface
+ */
+ public function testParseAsInterfaceNullTitle() {
+ $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
+ $op = $this->newInstance( [], null, 'notitle' );
+ $op->parseAsInterface( '' );
+ }
+
+ /**
+ * @covers OutputPage::parseInlineAsInterface
+ */
+ public function testParseInlineAsInterfaceNullTitle() {
+ $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
+ $op = $this->newInstance( [], null, 'notitle' );
+ $op->parseInlineAsInterface( '' );
+ }
+
/**
* @covers OutputPage::setCdnMaxage
* @covers OutputPage::lowerCdnMaxage