MediaWiki  master
ParserMethodsTest.php
Go to the documentation of this file.
1 <?php
2 
8 
15 
16  public static function providePreSaveTransform() {
17  return [
18  [ 'hello this is ~~~',
19  "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
20  ],
21  [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
22  'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
23  ],
24  ];
25  }
26 
30  public function testPreSaveTransform( $text, $expected ) {
31  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
32  $user = new User();
33  $user->setName( "127.0.0.1" );
35  $text = MediaWikiServices::getInstance()->getParser()
36  ->preSaveTransform( $text, $title, $user, $popts );
37 
38  $this->assertEquals( $expected, $text );
39  }
40 
41  public static function provideStripOuterParagraph() {
42  // This mimics the most common use case (stripping paragraphs generated by the parser).
43  $message = new RawMessage( "Message text." );
44 
45  return [
46  [
47  "<p>Text.</p>",
48  "Text.",
49  ],
50  [
51  "<p class='foo'>Text.</p>",
52  "<p class='foo'>Text.</p>",
53  ],
54  [
55  "<p>Text.\n</p>\n",
56  "Text.",
57  ],
58  [
59  "<p>Text.</p><p>More text.</p>",
60  "<p>Text.</p><p>More text.</p>",
61  ],
62  [
63  $message->parse(),
64  "Message text.",
65  ],
66  ];
67  }
68 
72  public function testStripOuterParagraph( $text, $expected ) {
73  $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
74  }
75 
81  public function testRecursiveParse() {
82  $title = Title::newFromText( 'foo' );
83  $parser = MediaWikiServices::getInstance()->getParser();
84  $po = new ParserOptions;
85  $parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
86  $parser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
87  }
88 
89  public function helperParserFunc( $input, $args, $parser ) {
90  $title = Title::newFromText( 'foo' );
91  $po = new ParserOptions;
92  $parser->parse( $input, $title, $po );
93  return 'bar';
94  }
95 
96  public function testCallParserFunction() {
97  // Normal parses test passing PPNodes. Test passing an array.
98  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
99  $parser = MediaWikiServices::getInstance()->getParser();
100  $parser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
101  $frame = $parser->getPreprocessor()->newFrame();
102  $ret = $parser->callParserFunction( $frame, '#tag',
103  [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
104  );
105  $ret['text'] = $parser->mStripState->unstripBoth( $ret['text'] );
106  $this->assertSame( [
107  'found' => true,
108  'text' => '<pre style="margin-left: 1.6em">foo</pre>',
109  ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
110  }
111 
116  public function testGetSections() {
117  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
118  $out = MediaWikiServices::getInstance()->getParser()
119  ->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
120  $this->assertSame( [
121  [
122  'toclevel' => 1,
123  'level' => '2',
124  'line' => 'foo',
125  'number' => '1',
126  'index' => '1',
127  'fromtitle' => $title->getPrefixedDBkey(),
128  'byteoffset' => 0,
129  'anchor' => 'foo',
130  ],
131  [
132  'toclevel' => 1,
133  'level' => '2',
134  'line' => 'bar',
135  'number' => '2',
136  'index' => '',
137  'fromtitle' => false,
138  'byteoffset' => null,
139  'anchor' => 'bar',
140  ],
141  [
142  'toclevel' => 1,
143  'level' => '2',
144  'line' => 'baz',
145  'number' => '3',
146  'index' => '2',
147  'fromtitle' => $title->getPrefixedDBkey(),
148  'byteoffset' => 21,
149  'anchor' => 'baz',
150  ],
151  ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
152  }
153 
157  public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
158  $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
159  }
160 
161  public static function provideNormalizeLinkUrl() {
162  return [
163  [
164  'Escaping of unsafe characters',
165  'http://example.org/foo bar?param[]="value"&param[]=val├╝e',
166  'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
167  ],
168  [
169  'Case normalization of percent-encoded characters',
170  'http://example.org/%ab%cD%Ef%FF',
171  'http://example.org/%AB%CD%EF%FF',
172  ],
173  [
174  'Unescaping of safe characters',
175  'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
176  'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
177  ],
178  [
179  'Context-sensitive replacement of sometimes-safe characters',
180  'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
181  'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
182  ],
183  [
184  'IPv6 links aren\'t escaped',
185  'http://[::1]/foobar',
186  'http://[::1]/foobar',
187  ],
188  [
189  'non-IPv6 links aren\'t unescaped',
190  'http://%5B::1%5D/foobar',
191  'http://%5B::1%5D/foobar',
192  ],
193  ];
194  }
195 
196  public function testWrapOutput() {
197  $title = Title::newFromText( 'foo' );
198  $po = new ParserOptions();
199  $parser = MediaWikiServices::getInstance()->getParser();
200  $parser->parse( 'Hello World', $title, $po );
201  $text = $parser->getOutput()->getText();
202 
203  $this->assertContains( 'Hello World', $text );
204  $this->assertContains( '<div', $text );
205  $this->assertContains( 'class="mw-parser-output"', $text );
206  }
207 
212  private function getMockTitle( $name ) {
213  $title = $this->getMock( Title::class );
214  $title->method( 'getPrefixedDBkey' )->willReturn( $name );
215  $title->method( 'getPrefixedText' )->willReturn( $name );
216  $title->method( 'getDBkey' )->willReturn( $name );
217  $title->method( 'getText' )->willReturn( $name );
218  $title->method( 'getNamespace' )->willReturn( 0 );
219  $title->method( 'getPageLanguage' )->willReturn( Language::factory( 'en' ) );
220 
221  return $title;
222  }
223 
224  public function provideRevisionAccess() {
225  $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
226 
227  $frank = $this->getMockBuilder( User::class )
228  ->disableOriginalConstructor()
229  ->getMock();
230 
231  $frank->method( 'getName' )->willReturn( 'Frank' );
232 
233  $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
234  $po = new ParserOptions( $frank );
235 
236  yield 'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
237  yield 'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
238 
239  $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
240  $po = new ParserOptions( $frank );
241 
242  yield 'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
243 
244  $oldRevision = new MutableRevisionRecord( $title );
245  $oldRevision->setId( 100 );
246  $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor', 0 ) );
247  $oldRevision->setTimestamp( '20141111111111' );
248  $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'FAUX' ) );
249 
250  $po = new ParserOptions( $frank );
251  $po->setCurrentRevisionCallback( function () use ( $oldRevision ) {
252  return new Revision( $oldRevision );
253  } );
254 
255  yield 'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
256 
257  $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
258 
259  $po = new ParserOptions( $frank );
260  $po->setIsPreview( true );
261 
262  yield 'preview without override, using context' => [
263  $text,
264  $po,
265  null,
266  'user:Frank;',
267  'user-subst:Frank;',
268  ];
269 
270  $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
271  . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
272 
273  $newRevision = new MutableRevisionRecord( $title );
274  $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
275  $newRevision->setTimestamp( '20180808000000' );
276  $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'NEW' ) );
277 
278  $po = new ParserOptions( $frank );
279  $po->setIsPreview( true );
280  $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
281  return new Revision( $newRevision );
282  } );
283 
284  yield 'preview' => [
285  $text,
286  $po,
287  null,
288  'user:NewAuthor;time:20180808000000;',
289  'user-subst:NewAuthor;time-subst:20180808000000;',
290  ];
291 
292  $po = new ParserOptions( $frank );
293  $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
294  return new Revision( $newRevision );
295  } );
296 
297  yield 'pre-save' => [
298  $text,
299  $po,
300  null,
301  'user:NewAuthor;time:20180808000000;',
302  'user-subst:NewAuthor;time-subst:20180808000000;',
303  ];
304 
305  $text = "(ONE)<includeonly>(TWO)</includeonly>"
306  . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
307 
308  $newRevision = new MutableRevisionRecord( $title );
309  $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
310  $newRevision->setTimestamp( '20180808000000' );
311  $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
312 
313  $po = new ParserOptions( $frank );
314  $po->setIsPreview( true );
315  $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
316  return new Revision( $newRevision );
317  } );
318 
319  yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
320  }
321 
325  public function testRevisionAccess(
326  $text,
327  ParserOptions $po,
328  $revId,
329  $expectedInHtml,
330  $expectedInPst = null
331  ) {
332  $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
333 
334  $po->enableLimitReport( false );
335 
336  $oldRevision = new MutableRevisionRecord( $title );
337  $oldRevision->setId( 100 );
338  $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
339  $oldRevision->setTimestamp( '20140404000000' );
340  $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'OLD' ) );
341 
342  $currentRevision = new MutableRevisionRecord( $title );
343  $currentRevision->setId( 200 );
344  $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
345  $currentRevision->setTimestamp( '20160606000000' );
346  $currentRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'CURRENT' ) );
347 
348  $revisionStore = $this->getMockBuilder( RevisionStore::class )
349  ->disableOriginalConstructor()
350  ->getMock();
351 
352  $revisionStore
353  ->method( 'getKnownCurrentRevision' )
354  ->willReturnMap( [
355  [ $title, 100, $oldRevision ],
356  [ $title, 200, $currentRevision ],
357  [ $title, 0, $currentRevision ],
358  ] );
359 
360  $revisionStore
361  ->method( 'getRevisionById' )
362  ->willReturnMap( [
363  [ 100, 0, $oldRevision ],
364  [ 200, 0, $currentRevision ],
365  ] );
366 
367  $this->setService( 'RevisionStore', $revisionStore );
368 
369  $parser = MediaWikiServices::getInstance()->getParser();
370  $parser->parse( $text, $title, $po, true, true, $revId );
371  $html = $parser->getOutput()->getText();
372 
373  $this->assertContains( $expectedInHtml, $html, 'In HTML' );
374 
375  if ( $expectedInPst !== null ) {
376  $pst = $parser->preSaveTransform( $text, $title, $po->getUser(), $po );
377  $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
378  }
379  }
380 
381  public static function provideGuessSectionNameFromWikiText() {
382  return [
383  [ '1/2', 'html5', '#1/2' ],
384  [ '1/2', 'legacy', '#1.2F2' ],
385  ];
386  }
387 
389  public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
390  $this->setMwGlobals( [ 'wgFragmentMode' => [ $mode ] ] );
391  $result = MediaWikiServices::getInstance()->getParser()
392  ->guessSectionNameFromWikiText( $input );
393  $this->assertEquals( $result, $expected );
394  }
395 
396  // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
397  // replaceSection(), getPreloadText()
398 }
testRevisionAccess( $text, ParserOptions $po, $revId, $expectedInHtml, $expectedInPst=null)
provideRevisionAccess
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1982
if(is_array( $mode)) switch( $mode) $input
static provideGuessSectionNameFromWikiText()
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1982
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
static providePreSaveTransform()
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1799
helperParserFunc( $input, $args, $parser)
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1980
if( $line===false) $args
Definition: cdb.php:64
Mutable RevisionRecord implementation, for building new revision entries programmatically.
static stripOuterParagraph( $html)
Strip outer.
Definition: Parser.php:6401
testPreSaveTransform( $text, $expected)
providePreSaveTransform
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
testNormalizeLinkUrl( $explanation, $url, $expected)
provideNormalizeLinkUrl
testRecursiveParse()
MWException Parser state cleared while parsing.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
Value object representing a user&#39;s identity.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:216
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown...
getUser()
Current user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Database Parser BlockLevelPass.
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Variant of the Message class.
Definition: RawMessage.php:34
static newFromUser( $user)
Get a ParserOptions object from a given user.
testGetSections()
Parser ParserOutput::getSections.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
testStripOuterParagraph( $text, $expected)
provideStripOuterParagraph
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:2095
testGuessSectionNameFromWikiText( $input, $mode, $expected)
provideGuessSectionNameFromWikiText
static provideStripOuterParagraph()
const OT_HTML
Definition: Parser.php:112
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473
static provideNormalizeLinkUrl()
enableLimitReport( $x=true)
Enable limit report in an HTML comment on output.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319