From: C. Scott Ananian Date: Thu, 27 Sep 2018 15:04:45 +0000 (-0400) Subject: Add OutputPage::wrapWikiTextAsInterface() to safely wrap wikitext X-Git-Tag: 1.34.0-rc.0~3748^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=d404b76510057c3fc4151f51bf6630ae27f6a57b Add OutputPage::wrapWikiTextAsInterface() to safely wrap wikitext This patch introduces a new method, OutputPage::wrapWikiTextAsInterface(), which wraps the result of OutputPage::addWikiTextAsInterface() in a tidy and robust way that won't break if the wrapped message contains double newlines, extra , or other nasties. This replaces a common unsafe pattern: $output->addWikiText( '
' ); Bug: T205624 Change-Id: I1040c7cf0ec1f5c4bef7c06d4486f50d85f2dc0f --- diff --git a/includes/OutputPage.php b/includes/OutputPage.php index cde92e8c17..a5f5fab08a 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -1793,6 +1793,29 @@ class OutputPage extends ContextSource { $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/true, /*interface*/true ); } + /** + * Convert wikitext *in the user interface language* to HTML and + * add it to the buffer with a `
` + * wrapper. The result will not be language-converted, as user + * interface messages as already localized into a specific + * variant. The $text will be parsed in start-of-line context. + * Output will be tidy. + * + * @param string $wrapperClass The class attribute value for the
+ * wrapper in the output HTML + * @param string $text Wikitext in the user interface language + * @since 1.32 + */ + public function wrapWikiTextAsInterface( + $wrapperClass, $text + ) { + $this->addWikiTextTitleInternal( + $text, $this->getTitle(), + /*linestart*/true, /*tidy*/true, /*interface*/true, + $wrapperClass + ); + } + /** * Convert wikitext *in the page content language* to HTML and add * it to the buffer. The result with be language-converted to the @@ -1904,10 +1927,12 @@ class OutputPage extends ContextSource { * since 1.32; all wikitext should be tidied. * @param bool $interface Whether it is an interface message * (for example disables conversion) + * @param string $wrapperClass if not empty, wraps the output in + * a `
` * @private */ private function addWikiTextTitleInternal( - $text, Title $title, $linestart, $tidy, $interface + $text, Title $title, $linestart, $tidy, $interface, $wrapperClass = null ) { global $wgParser; @@ -1924,7 +1949,7 @@ class OutputPage extends ContextSource { $this->addParserOutput( $parserOutput, [ 'enableSectionEditLinks' => false, - 'wrapperDivClass' => '', + 'wrapperDivClass' => $wrapperClass ?? '', ] ); } diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index 53e6f464c9..211169eaef 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -1430,6 +1430,7 @@ class OutputPageTest extends MediaWikiTestCase { * @dataProvider provideAddWikiText * @covers OutputPage::addWikiText * @covers OutputPage::addWikiTextAsInterface + * @covers OutputPage::wrapWikiTextAsInterface * @covers OutputPage::addWikiTextAsContent * @covers OutputPage::addWikiTextWithTitle * @covers OutputPage::addWikiTextTitle @@ -1545,6 +1546,21 @@ class OutputPageTest extends MediaWikiTestCase { '
' . "Some page\n
" ], ], + 'wrapWikiTextAsInterface' => [ + 'Simple' => [ + [ 'wrapperClass', 'text' ], + "

text\n

" + ], 'Spurious
' => [ + [ 'wrapperClass', 'text
more' ], + "

text

more\n
" + ], 'Extra newlines would break

wrappers' => [ + [ 'two classes', "1\n\n2\n\n3" ], + "

1\n

2\n

3\n

" + ], 'Other unclosed tags' => [ + [ 'error', 'acd' ], + "

acd\n

" + ], + ], ]; // Test all the others on addWikiTextTitle as well