| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\Renderer\ContentParseParams; |
| 4 |
use MediaWiki\Interwiki\ClassicInterwikiLookup; |
| 5 |
use MediaWiki\Linker\LinkTarget; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Parser\ParserOptions; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWiki\Title\TitleValue; |
| 10 |
use Wikimedia\Parsoid\Parsoid; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegrationTest { |
| 19 |
protected function setUp(): void { |
| 20 |
parent::setUp(); |
| 21 |
|
| 22 |
|
| 23 |
$defaults = [ |
| 24 |
'iw_local' => 0, |
| 25 |
'iw_api' => '/w/api.php', |
| 26 |
'iw_url' => '' |
| 27 |
]; |
| 28 |
$this->overrideConfigValue( |
| 29 |
MainConfigNames::InterwikiCache, |
| 30 |
ClassicInterwikiLookup::buildCdbHash( [ |
| 31 |
[ |
| 32 |
'iw_prefix' => 'en', |
| 33 |
'iw_url' => 'https://en.wikipedia.org/wiki/$1', |
| 34 |
'iw_wikiid' => 'enwiki', |
| 35 |
] + $defaults, |
| 36 |
[ |
| 37 |
'iw_prefix' => 'google', |
| 38 |
'iw_url' => 'https://google.com/?q=$1', |
| 39 |
'iw_wikiid' => 'google', |
| 40 |
] + $defaults, |
| 41 |
] ) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public static function provideGetParserOutput() { |
| 46 |
$commonOptions = [ |
| 47 |
'collapsibleSections', |
| 48 |
'disableContentConversion', |
| 49 |
'interfaceMessage', |
| 50 |
'isPreview', |
| 51 |
'maxIncludeSize', |
| 52 |
'suppressSectionEditLinks', |
| 53 |
'useParsoid', |
| 54 |
'wrapclass', |
| 55 |
]; |
| 56 |
$commonParsoidOptions = array_merge( $commonOptions, [ |
| 57 |
|
| 58 |
] ); |
| 59 |
$commonLegacyOptions = array_merge( $commonOptions, [ |
| 60 |
'disableTitleConversion', |
| 61 |
'expensiveParserFunctionLimit', |
| 62 |
'maxPPExpandDepth', |
| 63 |
'maxPPNodeCount', |
| 64 |
'suppressTOC', |
| 65 |
'targetLanguage', |
| 66 |
] ); |
| 67 |
$parsoidVersion = |
| 68 |
'data-mw-parsoid-version="' . Parsoid::version() . '" ' . |
| 69 |
'data-mw-html-version="' . Parsoid::defaultHTMLVersion() . '"'; |
| 70 |
|
| 71 |
yield 'Basic render' => [ |
| 72 |
'title' => 'WikitextContentTest_testGetParserOutput', |
| 73 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 74 |
'text' => "hello ''world''\n", |
| 75 |
'expectedHtml' => '<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">' . "<p>hello <i>world</i>\n</p></div>", |
| 76 |
'expectedFields' => [ |
| 77 |
'Links' => [ |
| 78 |
], |
| 79 |
'Sections' => [ |
| 80 |
], |
| 81 |
'UsedOptions' => $commonLegacyOptions, |
| 82 |
], |
| 83 |
]; |
| 84 |
yield 'Basic Parsoid render' => [ |
| 85 |
'title' => 'WikitextContentTest_testGetParserOutput', |
| 86 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 87 |
'text' => "hello ''world''\n", |
| 88 |
'expectedHtml' => "<div class=\"mw-content-ltr mw-parser-output\" lang=\"en\" dir=\"ltr\" $parsoidVersion><section data-mw-section-id=\"0\" id=\"mwAQ\"><p id=\"mwAg\">hello <i id=\"mwAw\">world</i></p>\n</section></div>", |
| 89 |
'expectedFields' => [ |
| 90 |
'Links' => [ |
| 91 |
], |
| 92 |
'Sections' => [ |
| 93 |
], |
| 94 |
'UsedOptions' => $commonParsoidOptions, |
| 95 |
], |
| 96 |
'options' => [ 'useParsoid' => true, 'suppressSectionEditLinks' => true ], |
| 97 |
]; |
| 98 |
yield 'Parsoid render (redirect page)' => [ |
| 99 |
'title' => 'WikitextContentTest_testGetParserOutput', |
| 100 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 101 |
'text' => "#REDIRECT [[Main Page]]", |
| 102 |
'expectedHtml' => "<div class=\"mw-content-ltr mw-parser-output\" lang=\"en\" dir=\"ltr\" $parsoidVersion><div class=\"redirectMsg\"><p>Redirect to:</p><ul class=\"redirectText\"><li><a href=\"/w/index.php?title=Main_Page&action=edit&redlink=1\" class=\"new\" title=\"Main Page (page does not exist)\">Main Page</a></li></ul></div><section data-mw-section-id=\"0\" id=\"mwAQ\"><link rel=\"mw:PageProp/redirect\" href=\"./Main_Page\" id=\"mwAg\"></section></div>", |
| 103 |
'expectedFields' => [ |
| 104 |
'Links' => [ |
| 105 |
[ 'Main_Page' => 0 ], |
| 106 |
], |
| 107 |
'Sections' => [ |
| 108 |
], |
| 109 |
'UsedOptions' => $commonParsoidOptions, |
| 110 |
], |
| 111 |
'options' => [ 'useParsoid' => true, 'suppressSectionEditLinks' => true ], |
| 112 |
]; |
| 113 |
yield 'Parsoid render (section edit links)' => [ |
| 114 |
'title' => 'WikitextContentTest_testGetParserOutput', |
| 115 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 116 |
'text' => "== Hello ==", |
| 117 |
'expectedHtml' => "<div class=\"mw-content-ltr mw-parser-output\" lang=\"en\" dir=\"ltr\" $parsoidVersion id=\"mwAw\">" . '<section data-mw-section-id="0" id="mwAQ"></section><section data-mw-section-id="1" id="mwAg"><div class="mw-heading mw-heading2" id="mwBA"><h2 id="Hello">Hello</h2><span class="mw-editsection" id="mwBQ"><span class="mw-editsection-bracket" id="mwBg">[</span><a href="/w/index.php?title=WikitextContentTest_testGetParserOutput&action=edit&section=1" title="Edit section: Hello" id="mwBw"><span id="mwCA">edit</span></a><span class="mw-editsection-bracket" id="mwCQ">]</span></span></div></section></div>', |
| 118 |
'expectedFields' => [ |
| 119 |
'Links' => [ |
| 120 |
], |
| 121 |
'Sections' => [ |
| 122 |
[ |
| 123 |
'toclevel' => 1, |
| 124 |
'level' => '2', |
| 125 |
'line' => 'Hello', |
| 126 |
'number' => '1', |
| 127 |
'index' => '1', |
| 128 |
'fromtitle' => 'WikitextContentTest_testGetParserOutput', |
| 129 |
'byteoffset' => 0, |
| 130 |
'anchor' => 'Hello', |
| 131 |
'linkAnchor' => 'Hello', |
| 132 |
], |
| 133 |
], |
| 134 |
'UsedOptions' => $commonParsoidOptions, |
| 135 |
], |
| 136 |
'options' => [ 'useParsoid' => true ], |
| 137 |
]; |
| 138 |
yield 'Links' => [ |
| 139 |
'title' => 'WikitextContentTest_testGetParserOutput', |
| 140 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 141 |
'text' => "[[title that does not really exist]]", |
| 142 |
'expectedHtml' => null, |
| 143 |
'expectedFields' => [ |
| 144 |
'Links' => [ |
| 145 |
[ 'Title_that_does_not_really_exist' => 0, ], |
| 146 |
], |
| 147 |
'Sections' => [ |
| 148 |
], |
| 149 |
], |
| 150 |
]; |
| 151 |
yield 'TOC' => [ |
| 152 |
'title' => 'WikitextContentTest_testGetParserOutput', |
| 153 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 154 |
'text' => "==One==\n==Two==\n==Three==\n==Four==\n<h2>Five</h2>\n===Six+Seven %2525===", |
| 155 |
'expectedHtml' => null, |
| 156 |
'expectedFields' => [ |
| 157 |
'Links' => [ |
| 158 |
], |
| 159 |
'Sections' => [ |
| 160 |
[ |
| 161 |
'toclevel' => 1, |
| 162 |
'level' => '2', |
| 163 |
'line' => 'One', |
| 164 |
'number' => '1', |
| 165 |
'index' => '1', |
| 166 |
'fromtitle' => 'WikitextContentTest_testGetParserOutput', |
| 167 |
'byteoffset' => 0, |
| 168 |
'anchor' => 'One', |
| 169 |
'linkAnchor' => 'One', |
| 170 |
], |
| 171 |
[ |
| 172 |
'toclevel' => 1, |
| 173 |
'level' => '2', |
| 174 |
'line' => 'Two', |
| 175 |
'number' => '2', |
| 176 |
'index' => '2', |
| 177 |
'fromtitle' => 'WikitextContentTest_testGetParserOutput', |
| 178 |
'byteoffset' => 8, |
| 179 |
'anchor' => 'Two', |
| 180 |
'linkAnchor' => 'Two', |
| 181 |
], |
| 182 |
[ |
| 183 |
'toclevel' => 1, |
| 184 |
'level' => '2', |
| 185 |
'line' => 'Three', |
| 186 |
'number' => '3', |
| 187 |
'index' => '3', |
| 188 |
'fromtitle' => 'WikitextContentTest_testGetParserOutput', |
| 189 |
'byteoffset' => 16, |
| 190 |
'anchor' => 'Three', |
| 191 |
'linkAnchor' => 'Three', |
| 192 |
], |
| 193 |
[ |
| 194 |
'toclevel' => 1, |
| 195 |
'level' => '2', |
| 196 |
'line' => 'Four', |
| 197 |
'number' => '4', |
| 198 |
'index' => '4', |
| 199 |
'fromtitle' => 'WikitextContentTest_testGetParserOutput', |
| 200 |
'byteoffset' => 26, |
| 201 |
'anchor' => 'Four', |
| 202 |
'linkAnchor' => 'Four', |
| 203 |
], |
| 204 |
[ |
| 205 |
'toclevel' => 1, |
| 206 |
'level' => '2', |
| 207 |
'line' => 'Five', |
| 208 |
'number' => '5', |
| 209 |
'index' => '', |
| 210 |
'fromtitle' => false, |
| 211 |
'byteoffset' => null, |
| 212 |
'anchor' => 'Five', |
| 213 |
'linkAnchor' => 'Five', |
| 214 |
], |
| 215 |
[ |
| 216 |
'toclevel' => 2, |
| 217 |
'level' => '3', |
| 218 |
'line' => 'Six+Seven %2525', |
| 219 |
'number' => '5.1', |
| 220 |
'index' => '5', |
| 221 |
'fromtitle' => 'WikitextContentTest_testGetParserOutput', |
| 222 |
'byteoffset' => 49, |
| 223 |
'anchor' => 'Six+Seven_%2525', |
| 224 |
'linkAnchor' => 'Six+Seven_%252525', |
| 225 |
], |
| 226 |
], |
| 227 |
], |
| 228 |
]; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function testGetParserOutput( $title, $model, $text, $expectedHtml, |
| 235 |
$expectedFields = null, $options = null |
| 236 |
) { |
| 237 |
$this->overrideConfigValues( [ |
| 238 |
MainConfigNames::ScriptPath => '/w', |
| 239 |
MainConfigNames::Script => '/w/index.php', |
| 240 |
MainConfigNames::FragmentMode => [ 'html5' ], |
| 241 |
] ); |
| 242 |
|
| 243 |
$parserOptions = null; |
| 244 |
if ( $options ) { |
| 245 |
$parserOptions = ParserOptions::newFromAnon(); |
| 246 |
foreach ( $options as $key => $val ) { |
| 247 |
$parserOptions->setOption( $key, $val ); |
| 248 |
} |
| 249 |
} |
| 250 |
parent::testGetParserOutput( |
| 251 |
$title, $model, $text, $expectedHtml, $expectedFields, $parserOptions |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function testMakeRedirectContent( LinkTarget $target, string $expectedWT, string $expectedTarget ) { |
| 262 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 263 |
->getContentHandler( CONTENT_MODEL_WIKITEXT ); |
| 264 |
$content = $handler->makeRedirectContent( Title::newFromLinkTarget( $target ) ); |
| 265 |
$this->assertEquals( $expectedWT, $content->serialize() ); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
$parserOutput = $handler->getParserOutput( |
| 270 |
$content, |
| 271 |
new ContentParseParams( Title::newMainPage() ) |
| 272 |
); |
| 273 |
$redirectHeader = $parserOutput->getRedirectHeader(); |
| 274 |
$this->assertStringContainsString( '<div class="redirectMsg">', $redirectHeader ); |
| 275 |
$this->assertMatchesRegularExpression( '!<a[^<>]+>' . $expectedTarget . '</a>!', $redirectHeader ); |
| 276 |
} |
| 277 |
|
| 278 |
public static function provideMakeRedirectContent() { |
| 279 |
return [ |
| 280 |
[ new TitleValue( NS_MAIN, 'Hello' ), '#REDIRECT [[Hello]]', 'Hello' ], |
| 281 |
[ new TitleValue( NS_TEMPLATE, 'Hello' ), '#REDIRECT [[Template:Hello]]', 'Template:Hello' ], |
| 282 |
[ new TitleValue( NS_MAIN, 'Hello', 'section' ), '#REDIRECT [[Hello#section]]', 'Hello#section' ], |
| 283 |
[ new TitleValue( NS_USER, 'John doe', 'section' ), '#REDIRECT [[User:John doe#section]]', 'User:John doe#section' ], |
| 284 |
[ new TitleValue( NS_MEDIAWIKI, 'FOOBAR' ), '#REDIRECT [[MediaWiki:FOOBAR]]', 'MediaWiki:FOOBAR' ], |
| 285 |
[ new TitleValue( NS_CATEGORY, 'Foo' ), '#REDIRECT [[:Category:Foo]]', 'Category:Foo' ], |
| 286 |
[ new TitleValue( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]', 'en:Foo' ], |
| 287 |
[ new TitleValue( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]', 'en:Foo' ], |
| 288 |
[ |
| 289 |
new TitleValue( NS_MAIN, 'Bar', 'fragment', 'google' ), |
| 290 |
'#REDIRECT [[google:Bar#fragment]]', |
| 291 |
'google:Bar#fragment' |
| 292 |
], |
| 293 |
]; |
| 294 |
} |
| 295 |
} |