MediaWiki  REL1_31
ApiParseTest.php
Go to the documentation of this file.
1 <?php
2 
10 class ApiParseTest extends ApiTestCase {
11 
12  protected static $pageId;
13  protected static $revIds = [];
14 
15  public function addDBDataOnce() {
16  $title = Title::newFromText( __CLASS__ );
17 
18  $status = $this->editPage( __CLASS__, 'Test for revdel' );
19  self::$pageId = $status->value['revision']->getPage();
20  self::$revIds['revdel'] = $status->value['revision']->getId();
21 
22  $status = $this->editPage( __CLASS__, 'Test for suppressed' );
23  self::$revIds['suppressed'] = $status->value['revision']->getId();
24 
25  $status = $this->editPage( __CLASS__, 'Test for oldid' );
26  self::$revIds['oldid'] = $status->value['revision']->getId();
27 
28  $status = $this->editPage( __CLASS__, 'Test for latest' );
29  self::$revIds['latest'] = $status->value['revision']->getId();
30 
31  $this->revisionDelete( self::$revIds['revdel'] );
32  $this->revisionDelete(
33  self::$revIds['suppressed'],
35  );
36 
37  Title::clearCaches(); // Otherwise it has the wrong latest revision for some reason
38  }
39 
51  protected function assertParsedTo( $expected, array $res, $warnings = null ) {
52  $this->doAssertParsedTo( $expected, $res, $warnings, [ $this, 'assertSame' ] );
53  }
54 
64  protected function assertParsedToRegExp( $expected, array $res, $warnings = null ) {
65  $this->doAssertParsedTo( $expected, $res, $warnings, [ $this, 'assertRegExp' ] );
66  }
67 
68  private function doAssertParsedTo( $expected, array $res, $warnings, callable $callback ) {
69  $html = $res[0]['parse']['text'];
70 
71  $expectedStart = '<div class="mw-parser-output">';
72  $this->assertSame( $expectedStart, substr( $html, 0, strlen( $expectedStart ) ) );
73 
74  $html = substr( $html, strlen( $expectedStart ) );
75 
76  if ( $res[1]->getBool( 'disablelimitreport' ) ) {
77  $expectedEnd = "</div>";
78  $this->assertSame( $expectedEnd, substr( $html, -strlen( $expectedEnd ) ) );
79 
80  $html = substr( $html, 0, strlen( $html ) - strlen( $expectedEnd ) );
81  } else {
82  $expectedEnd = '#\n<!-- \nNewPP limit report\n(?>.+?\n-->)\n' .
83  '<!--\nTransclusion expansion time report \‍(%,ms,calls,template\‍)\n(?>.*?\n-->)\n' .
84  '</div>(\n<!-- Saved in parser cache (?>.*?\n -->)\n)?$#s';
85  $this->assertRegExp( $expectedEnd, $html );
86 
87  $html = preg_replace( $expectedEnd, '', $html );
88  }
89 
90  call_user_func( $callback, $expected, $html );
91 
92  if ( $warnings === null ) {
93  $this->assertCount( 1, $res[0] );
94  } else {
95  $this->assertCount( 2, $res[0] );
96  // This deliberately fails if there are extra warnings
97  $this->assertSame( [ 'parse' => [ 'warnings' => $warnings ] ], $res[0]['warnings'] );
98  }
99  }
100 
104  protected function setupInterwiki() {
105  $dbw = wfGetDB( DB_MASTER );
106  $dbw->insert(
107  'interwiki',
108  [
109  'iw_prefix' => 'madeuplanguage',
110  'iw_url' => "https://example.com/wiki/$1",
111  'iw_api' => '',
112  'iw_wikiid' => '',
113  'iw_local' => false,
114  ],
115  __METHOD__,
116  'IGNORE'
117  );
118 
119  $this->setMwGlobals( 'wgExtraInterlanguageLinkPrefixes', [ 'madeuplanguage' ] );
120  $this->tablesUsed[] = 'interwiki';
121  }
122 
128  protected function setupSkin() {
129  $factory = new SkinFactory();
130  $factory->register( 'testing', 'Testing', function () {
131  $skin = $this->getMockBuilder( SkinFallback::class )
132  ->setMethods( [ 'getDefaultModules', 'setupSkinUserCss' ] )
133  ->getMock();
134  $skin->expects( $this->once() )->method( 'getDefaultModules' )
135  ->willReturn( [
136  'core' => [ 'foo', 'bar' ],
137  'content' => [ 'baz' ]
138  ] );
139  $skin->expects( $this->once() )->method( 'setupSkinUserCss' )
140  ->will( $this->returnCallback( function ( OutputPage $out ) {
141  $out->addModuleStyles( 'foo.styles' );
142  } ) );
143  return $skin;
144  } );
145  $this->setService( 'SkinFactory', $factory );
146  }
147 
148  public function testParseByName() {
149  $res = $this->doApiRequest( [
150  'action' => 'parse',
151  'page' => __CLASS__,
152  ] );
153  $this->assertParsedTo( "<p>Test for latest\n</p>", $res );
154 
155  $res = $this->doApiRequest( [
156  'action' => 'parse',
157  'page' => __CLASS__,
158  'disablelimitreport' => 1,
159  ] );
160  $this->assertParsedTo( "<p>Test for latest\n</p>", $res );
161  }
162 
163  public function testParseById() {
164  $res = $this->doApiRequest( [
165  'action' => 'parse',
166  'pageid' => self::$pageId,
167  ] );
168  $this->assertParsedTo( "<p>Test for latest\n</p>", $res );
169  }
170 
171  public function testParseByOldId() {
172  $res = $this->doApiRequest( [
173  'action' => 'parse',
174  'oldid' => self::$revIds['oldid'],
175  ] );
176  $this->assertParsedTo( "<p>Test for oldid\n</p>", $res );
177  $this->assertArrayNotHasKey( 'textdeleted', $res[0]['parse'] );
178  $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
179  }
180 
181  public function testRevDel() {
182  $res = $this->doApiRequest( [
183  'action' => 'parse',
184  'oldid' => self::$revIds['revdel'],
185  ] );
186 
187  $this->assertParsedTo( "<p>Test for revdel\n</p>", $res );
188  $this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
189  $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
190  }
191 
192  public function testRevDelNoPermission() {
193  $this->setExpectedException( ApiUsageException::class,
194  "You don't have permission to view deleted revision text." );
195 
196  $this->doApiRequest( [
197  'action' => 'parse',
198  'oldid' => self::$revIds['revdel'],
199  ], null, null, static::getTestUser()->getUser() );
200  }
201 
202  public function testSuppressed() {
203  $this->setGroupPermissions( 'sysop', 'viewsuppressed', true );
204 
205  $res = $this->doApiRequest( [
206  'action' => 'parse',
207  'oldid' => self::$revIds['suppressed']
208  ] );
209 
210  $this->assertParsedTo( "<p>Test for suppressed\n</p>", $res );
211  $this->assertArrayHasKey( 'textsuppressed', $res[0]['parse'] );
212  $this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
213  }
214 
215  public function testNonexistentPage() {
216  try {
217  $this->doApiRequest( [
218  'action' => 'parse',
219  'page' => 'DoesNotExist',
220  ] );
221 
222  $this->fail( "API did not return an error when parsing a nonexistent page" );
223  } catch ( ApiUsageException $ex ) {
224  $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'missingtitle' ),
225  "Parse request for nonexistent page must give 'missingtitle' error: "
226  . var_export( self::getErrorFormatter()->arrayFromStatus( $ex->getStatusValue() ), true )
227  );
228  }
229  }
230 
231  public function testTitleProvided() {
232  $res = $this->doApiRequest( [
233  'action' => 'parse',
234  'title' => 'Some interesting page',
235  'text' => '{{PAGENAME}} has attracted my attention',
236  ] );
237 
238  $this->assertParsedTo( "<p>Some interesting page has attracted my attention\n</p>", $res );
239  }
240 
241  public function testSection() {
242  $name = ucfirst( __FUNCTION__ );
243 
244  $this->editPage( $name,
245  "Intro\n\n== Section 1 ==\n\nContent 1\n\n== Section 2 ==\n\nContent 2" );
246 
247  $res = $this->doApiRequest( [
248  'action' => 'parse',
249  'page' => $name,
250  'section' => 1,
251  ] );
252 
253  $this->assertParsedToRegExp( '!<h2>.*Section 1.*</h2>\n<p>Content 1\n</p>!', $res );
254  }
255 
256  public function testInvalidSection() {
257  $this->setExpectedException( ApiUsageException::class,
258  'The "section" parameter must be a valid section ID or "new".' );
259 
260  $this->doApiRequest( [
261  'action' => 'parse',
262  'section' => 'T-new',
263  ] );
264  }
265 
266  public function testSectionNoContent() {
267  $name = ucfirst( __FUNCTION__ );
268 
269  $status = $this->editPage( $name,
270  "Intro\n\n== Section 1 ==\n\nContent 1\n\n== Section 2 ==\n\nContent 2" );
271 
272  $this->setExpectedException( ApiUsageException::class,
273  "Missing content for page ID {$status->value['revision']->getPage()}." );
274 
275  $this->db->delete( 'revision', [ 'rev_id' => $status->value['revision']->getId() ] );
276 
277  // Suppress warning in WikiPage::getContentModel
278  Wikimedia\suppressWarnings();
279  try {
280  $this->doApiRequest( [
281  'action' => 'parse',
282  'page' => $name,
283  'section' => 1,
284  ] );
285  } finally {
286  Wikimedia\restoreWarnings();
287  }
288  }
289 
290  public function testNewSectionWithPage() {
291  $this->setExpectedException( ApiUsageException::class,
292  '"section=new" cannot be combined with the "oldid", "pageid" or "page" ' .
293  'parameters. Please use "title" and "text".' );
294 
295  $this->doApiRequest( [
296  'action' => 'parse',
297  'page' => __CLASS__,
298  'section' => 'new',
299  ] );
300  }
301 
302  public function testNonexistentOldId() {
303  $this->setExpectedException( ApiUsageException::class,
304  'There is no revision with ID 2147483647.' );
305 
306  $this->doApiRequest( [
307  'action' => 'parse',
308  'oldid' => pow( 2, 31 ) - 1,
309  ] );
310  }
311 
312  public function testUnfollowedRedirect() {
313  $name = ucfirst( __FUNCTION__ );
314 
315  $this->editPage( $name, "#REDIRECT [[$name 2]]" );
316  $this->editPage( "$name 2", "Some ''text''" );
317 
318  $res = $this->doApiRequest( [
319  'action' => 'parse',
320  'page' => $name,
321  ] );
322 
323  // Can't use assertParsedTo because the parser output is different for
324  // redirects
325  $this->assertRegExp( "/Redirect to:.*$name 2/", $res[0]['parse']['text'] );
326  $this->assertArrayNotHasKey( 'warnings', $res[0] );
327  }
328 
329  public function testFollowedRedirect() {
330  $name = ucfirst( __FUNCTION__ );
331 
332  $this->editPage( $name, "#REDIRECT [[$name 2]]" );
333  $this->editPage( "$name 2", "Some ''text''" );
334 
335  $res = $this->doApiRequest( [
336  'action' => 'parse',
337  'page' => $name,
338  'redirects' => true,
339  ] );
340 
341  $this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
342  }
343 
344  public function testFollowedRedirectById() {
345  $name = ucfirst( __FUNCTION__ );
346 
347  $id = $this->editPage( $name, "#REDIRECT [[$name 2]]" )->value['revision']->getPage();
348  $this->editPage( "$name 2", "Some ''text''" );
349 
350  $res = $this->doApiRequest( [
351  'action' => 'parse',
352  'pageid' => $id,
353  'redirects' => true,
354  ] );
355 
356  $this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
357  }
358 
359  public function testInvalidTitle() {
360  $this->setExpectedException( ApiUsageException::class, 'Bad title "|".' );
361 
362  $this->doApiRequest( [
363  'action' => 'parse',
364  'title' => '|',
365  ] );
366  }
367 
368  public function testTitleWithNonexistentRevId() {
369  $this->setExpectedException( ApiUsageException::class,
370  'There is no revision with ID 2147483647.' );
371 
372  $this->doApiRequest( [
373  'action' => 'parse',
374  'title' => __CLASS__,
375  'revid' => pow( 2, 31 ) - 1,
376  ] );
377  }
378 
379  public function testTitleWithNonMatchingRevId() {
380  $name = ucfirst( __FUNCTION__ );
381 
382  $res = $this->doApiRequest( [
383  'action' => 'parse',
384  'title' => $name,
385  'revid' => self::$revIds['latest'],
386  'text' => 'Some text',
387  ] );
388 
389  $this->assertParsedTo( "<p>Some text\n</p>", $res,
390  'r' . self::$revIds['latest'] . " is not a revision of $name." );
391  }
392 
393  public function testRevId() {
394  $res = $this->doApiRequest( [
395  'action' => 'parse',
396  'revid' => self::$revIds['latest'],
397  'text' => 'My revid is {{REVISIONID}}!',
398  ] );
399 
400  $this->assertParsedTo( "<p>My revid is " . self::$revIds['latest'] . "!\n</p>", $res );
401  }
402 
403  public function testTitleNoText() {
404  $res = $this->doApiRequest( [
405  'action' => 'parse',
406  'title' => 'Special:AllPages',
407  ] );
408 
409  $this->assertParsedTo( '', $res,
410  '"title" used without "text", and parsed page properties were requested. ' .
411  'Did you mean to use "page" instead of "title"?' );
412  }
413 
414  public function testRevidNoText() {
415  $res = $this->doApiRequest( [
416  'action' => 'parse',
417  'revid' => self::$revIds['latest'],
418  ] );
419 
420  $this->assertParsedTo( '', $res,
421  '"revid" used without "text", and parsed page properties were requested. ' .
422  'Did you mean to use "oldid" instead of "revid"?' );
423  }
424 
425  public function testTextNoContentModel() {
426  $res = $this->doApiRequest( [
427  'action' => 'parse',
428  'text' => "Some ''text''",
429  ] );
430 
431  $this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res,
432  'No "title" or "contentmodel" was given, assuming wikitext.' );
433  }
434 
435  public function testSerializationError() {
436  $this->setExpectedException( APIUsageException::class,
437  'Content serialization failed: Could not unserialize content' );
438 
439  $this->mergeMwGlobalArrayValue( 'wgContentHandlers',
440  [ 'testing-serialize-error' => 'DummySerializeErrorContentHandler' ] );
441 
442  $this->doApiRequest( [
443  'action' => 'parse',
444  'text' => "Some ''text''",
445  'contentmodel' => 'testing-serialize-error',
446  ] );
447  }
448 
449  public function testNewSection() {
450  $res = $this->doApiRequest( [
451  'action' => 'parse',
452  'title' => __CLASS__,
453  'section' => 'new',
454  'sectiontitle' => 'Title',
455  'text' => 'Content',
456  ] );
457 
458  $this->assertParsedToRegExp( '!<h2>.*Title.*</h2>\n<p>Content\n</p>!', $res );
459  }
460 
461  public function testExistingSection() {
462  $res = $this->doApiRequest( [
463  'action' => 'parse',
464  'title' => __CLASS__,
465  'section' => 1,
466  'text' => "Intro\n\n== Section 1 ==\n\nContent\n\n== Section 2 ==\n\nMore content",
467  ] );
468 
469  $this->assertParsedToRegExp( '!<h2>.*Section 1.*</h2>\n<p>Content\n</p>!', $res );
470  }
471 
472  public function testNoPst() {
473  $name = ucfirst( __FUNCTION__ );
474 
475  $this->editPage( "Template:$name", "Template ''text''" );
476 
477  $res = $this->doApiRequest( [
478  'action' => 'parse',
479  'text' => "{{subst:$name}}",
480  'contentmodel' => 'wikitext',
481  ] );
482 
483  $this->assertParsedTo( "<p>{{subst:$name}}\n</p>", $res );
484  }
485 
486  public function testPst() {
487  $name = ucfirst( __FUNCTION__ );
488 
489  $this->editPage( "Template:$name", "Template ''text''" );
490 
491  $res = $this->doApiRequest( [
492  'action' => 'parse',
493  'pst' => '',
494  'text' => "{{subst:$name}}",
495  'contentmodel' => 'wikitext',
496  'prop' => 'text|wikitext',
497  ] );
498 
499  $this->assertParsedTo( "<p>Template <i>text</i>\n</p>", $res );
500  $this->assertSame( "{{subst:$name}}", $res[0]['parse']['wikitext'] );
501  }
502 
503  public function testOnlyPst() {
504  $name = ucfirst( __FUNCTION__ );
505 
506  $this->editPage( "Template:$name", "Template ''text''" );
507 
508  $res = $this->doApiRequest( [
509  'action' => 'parse',
510  'onlypst' => '',
511  'text' => "{{subst:$name}}",
512  'contentmodel' => 'wikitext',
513  'prop' => 'text|wikitext',
514  'summary' => 'Summary',
515  ] );
516 
517  $this->assertSame(
518  [ 'parse' => [
519  'text' => "Template ''text''",
520  'wikitext' => "{{subst:$name}}",
521  'parsedsummary' => 'Summary',
522  ] ],
523  $res[0]
524  );
525  }
526 
527  public function testHeadHtml() {
528  $res = $this->doApiRequest( [
529  'action' => 'parse',
530  'page' => __CLASS__,
531  'prop' => 'headhtml',
532  ] );
533 
534  // Just do a rough sanity check
535  $this->assertRegExp( '#<!DOCTYPE.*<html.*<head.*</head>.*<body#s',
536  $res[0]['parse']['headhtml'] );
537  $this->assertArrayNotHasKey( 'warnings', $res[0] );
538  }
539 
540  public function testCategoriesHtml() {
541  $name = ucfirst( __FUNCTION__ );
542 
543  $this->editPage( $name, "[[Category:$name]]" );
544 
545  $res = $this->doApiRequest( [
546  'action' => 'parse',
547  'page' => $name,
548  'prop' => 'categorieshtml',
549  ] );
550 
551  $this->assertRegExp( "#Category.*Category:$name.*$name#",
552  $res[0]['parse']['categorieshtml'] );
553  $this->assertArrayNotHasKey( 'warnings', $res[0] );
554  }
555 
556  public function testEffectiveLangLinks() {
557  $hookRan = false;
558  $this->setTemporaryHook( 'LanguageLinks',
559  function () use ( &$hookRan ) {
560  $hookRan = true;
561  }
562  );
563 
564  $res = $this->doApiRequest( [
565  'action' => 'parse',
566  'title' => __CLASS__,
567  'text' => '[[zh:' . __CLASS__ . ']]',
568  'effectivelanglinks' => '',
569  ] );
570 
571  $this->assertTrue( $hookRan );
572  $this->assertSame( 'The parameter "effectivelanglinks" has been deprecated.',
573  $res[0]['warnings']['parse']['warnings'] );
574  }
575 
579  private function doTestLangLinks( array $arr = [] ) {
580  $this->setupInterwiki();
581 
582  $res = $this->doApiRequest( array_merge( [
583  'action' => 'parse',
584  'title' => 'Omelette',
585  'text' => '[[madeuplanguage:Omelette]]',
586  'prop' => 'langlinks',
587  ], $arr ) );
588 
589  $langLinks = $res[0]['parse']['langlinks'];
590 
591  $this->assertCount( 1, $langLinks );
592  $this->assertSame( 'madeuplanguage', $langLinks[0]['lang'] );
593  $this->assertSame( 'Omelette', $langLinks[0]['title'] );
594  $this->assertSame( 'https://example.com/wiki/Omelette', $langLinks[0]['url'] );
595  $this->assertArrayNotHasKey( 'warnings', $res[0] );
596  }
597 
598  public function testLangLinks() {
599  $this->doTestLangLinks();
600  }
601 
602  public function testLangLinksWithSkin() {
603  $this->setupSkin();
604  $this->doTestLangLinks( [ 'useskin' => 'testing' ] );
605  }
606 
607  public function testHeadItems() {
608  $res = $this->doApiRequest( [
609  'action' => 'parse',
610  'title' => __CLASS__,
611  'text' => '',
612  'prop' => 'headitems',
613  ] );
614 
615  $this->assertSame( [], $res[0]['parse']['headitems'] );
616  $this->assertSame(
617  '"prop=headitems" is deprecated since MediaWiki 1.28. ' .
618  'Use "prop=headhtml" when creating new HTML documents, ' .
619  'or "prop=modules|jsconfigvars" when updating a document client-side.',
620  $res[0]['warnings']['parse']['warnings']
621  );
622  }
623 
624  public function testHeadItemsWithSkin() {
625  $this->setupSkin();
626 
627  $res = $this->doApiRequest( [
628  'action' => 'parse',
629  'title' => __CLASS__,
630  'text' => '',
631  'prop' => 'headitems',
632  'useskin' => 'testing',
633  ] );
634 
635  $this->assertSame( [], $res[0]['parse']['headitems'] );
636  $this->assertSame(
637  '"prop=headitems" is deprecated since MediaWiki 1.28. ' .
638  'Use "prop=headhtml" when creating new HTML documents, ' .
639  'or "prop=modules|jsconfigvars" when updating a document client-side.',
640  $res[0]['warnings']['parse']['warnings']
641  );
642  }
643 
644  public function testModules() {
645  $this->setTemporaryHook( 'ParserAfterParse',
646  function ( $parser ) {
647  $output = $parser->getOutput();
648  $output->addModules( [ 'foo', 'bar' ] );
649  $output->addModuleScripts( [ 'baz', 'quuz' ] );
650  $output->addModuleStyles( [ 'aaa', 'zzz' ] );
651  $output->addJsConfigVars( [ 'x' => 'y', 'z' => -3 ] );
652  }
653  );
654  $res = $this->doApiRequest( [
655  'action' => 'parse',
656  'title' => __CLASS__,
657  'text' => 'Content',
658  'prop' => 'modules|jsconfigvars|encodedjsconfigvars',
659  ] );
660 
661  $this->assertSame( [ 'foo', 'bar' ], $res[0]['parse']['modules'] );
662  $this->assertSame( [ 'baz', 'quuz' ], $res[0]['parse']['modulescripts'] );
663  $this->assertSame( [ 'aaa', 'zzz' ], $res[0]['parse']['modulestyles'] );
664  $this->assertSame( [ 'x' => 'y', 'z' => -3 ], $res[0]['parse']['jsconfigvars'] );
665  $this->assertSame( '{"x":"y","z":-3}', $res[0]['parse']['encodedjsconfigvars'] );
666  $this->assertArrayNotHasKey( 'warnings', $res[0] );
667  }
668 
669  public function testModulesWithSkin() {
670  $this->setupSkin();
671 
672  $res = $this->doApiRequest( [
673  'action' => 'parse',
674  'pageid' => self::$pageId,
675  'useskin' => 'testing',
676  'prop' => 'modules',
677  ] );
678  $this->assertSame(
679  [ 'foo', 'bar', 'baz' ],
680  $res[0]['parse']['modules'],
681  'resp.parse.modules'
682  );
683  $this->assertSame(
684  [],
685  $res[0]['parse']['modulescripts'],
686  'resp.parse.modulescripts'
687  );
688  $this->assertSame(
689  [ 'foo.styles' ],
690  $res[0]['parse']['modulestyles'],
691  'resp.parse.modulestyles'
692  );
693  $this->assertSame(
694  [ 'parse' =>
695  [ 'warnings' =>
696  'Property "modules" was set but not "jsconfigvars" or ' .
697  '"encodedjsconfigvars". Configuration variables are necessary for ' .
698  'proper module usage.'
699  ]
700  ],
701  $res[0]['warnings']
702  );
703  }
704 
705  public function testIndicators() {
706  $res = $this->doApiRequest( [
707  'action' => 'parse',
708  'title' => __CLASS__,
709  'text' =>
710  '<indicator name="b">BBB!</indicator>Some text<indicator name="a">aaa</indicator>',
711  'prop' => 'indicators',
712  ] );
713 
714  $this->assertSame(
715  // It seems we return in markup order and not display order
716  [ 'b' => 'BBB!', 'a' => 'aaa' ],
717  $res[0]['parse']['indicators']
718  );
719  $this->assertArrayNotHasKey( 'warnings', $res[0] );
720  }
721 
722  public function testIndicatorsWithSkin() {
723  $this->setupSkin();
724 
725  $res = $this->doApiRequest( [
726  'action' => 'parse',
727  'title' => __CLASS__,
728  'text' =>
729  '<indicator name="b">BBB!</indicator>Some text<indicator name="a">aaa</indicator>',
730  'prop' => 'indicators',
731  'useskin' => 'testing',
732  ] );
733 
734  $this->assertSame(
735  // Now we return in display order rather than markup order
736  [ 'a' => 'aaa', 'b' => 'BBB!' ],
737  $res[0]['parse']['indicators']
738  );
739  $this->assertArrayNotHasKey( 'warnings', $res[0] );
740  }
741 
742  public function testIwlinks() {
743  $this->setupInterwiki();
744 
745  $res = $this->doApiRequest( [
746  'action' => 'parse',
747  'title' => 'Omelette',
748  'text' => '[[:madeuplanguage:Omelette]][[madeuplanguage:Spaghetti]]',
749  'prop' => 'iwlinks',
750  ] );
751 
752  $iwlinks = $res[0]['parse']['iwlinks'];
753 
754  $this->assertCount( 1, $iwlinks );
755  $this->assertSame( 'madeuplanguage', $iwlinks[0]['prefix'] );
756  $this->assertSame( 'https://example.com/wiki/Omelette', $iwlinks[0]['url'] );
757  $this->assertSame( 'madeuplanguage:Omelette', $iwlinks[0]['title'] );
758  $this->assertArrayNotHasKey( 'warnings', $res[0] );
759  }
760 
761  public function testLimitReports() {
762  $res = $this->doApiRequest( [
763  'action' => 'parse',
764  'pageid' => self::$pageId,
765  'prop' => 'limitreportdata|limitreporthtml',
766  ] );
767 
768  // We don't bother testing the actual values here
769  $this->assertInternalType( 'array', $res[0]['parse']['limitreportdata'] );
770  $this->assertInternalType( 'string', $res[0]['parse']['limitreporthtml'] );
771  $this->assertArrayNotHasKey( 'warnings', $res[0] );
772  }
773 
774  public function testParseTreeNonWikitext() {
775  $this->setExpectedException( ApiUsageException::class,
776  '"prop=parsetree" is only supported for wikitext content.' );
777 
778  $this->doApiRequest( [
779  'action' => 'parse',
780  'text' => '',
781  'contentmodel' => 'json',
782  'prop' => 'parsetree',
783  ] );
784  }
785 
786  public function testParseTree() {
787  $res = $this->doApiRequest( [
788  'action' => 'parse',
789  'text' => "Some ''text'' is {{nice|to have|i=think}}",
790  'contentmodel' => 'wikitext',
791  'prop' => 'parsetree',
792  ] );
793 
794  // Preprocessor_DOM and Preprocessor_Hash give different results here,
795  // so we'll accept either
796  $this->assertRegExp(
797  '#^<root>Some \'\'text\'\' is <template><title>nice</title>' .
798  '<part><name index="1"/><value>to have</value></part>' .
799  '<part><name>i</name>(?:<equals>)?=(?:</equals>)?<value>think</value></part>' .
800  '</template></root>$#',
801  $res[0]['parse']['parsetree']
802  );
803  $this->assertArrayNotHasKey( 'warnings', $res[0] );
804  }
805 
806  public function testDisableTidy() {
807  $this->setMwGlobals( 'wgTidyConfig', [ 'driver' => 'RemexHtml' ] );
808 
809  // Check that disabletidy doesn't have an effect just because tidying
810  // doesn't work for some other reason
811  $res1 = $this->doApiRequest( [
812  'action' => 'parse',
813  'text' => "<b>Mixed <i>up</b></i>",
814  'contentmodel' => 'wikitext',
815  ] );
816  $this->assertParsedTo( "<p><b>Mixed <i>up</i></b>\n</p>", $res1 );
817 
818  $res2 = $this->doApiRequest( [
819  'action' => 'parse',
820  'text' => "<b>Mixed <i>up</b></i>",
821  'contentmodel' => 'wikitext',
822  'disabletidy' => '',
823  ] );
824 
825  $this->assertParsedTo( "<p><b>Mixed <i>up</b></i>\n</p>", $res2 );
826  }
827 
828  public function testFormatCategories() {
829  $name = ucfirst( __FUNCTION__ );
830 
831  $this->editPage( "Category:$name", 'Content' );
832  $this->editPage( 'Category:Hidden', '__HIDDENCAT__' );
833 
834  $res = $this->doApiRequest( [
835  'action' => 'parse',
836  'title' => __CLASS__,
837  'text' => "[[Category:$name]][[Category:Foo|Sort me]][[Category:Hidden]]",
838  'prop' => 'categories',
839  ] );
840 
841  $this->assertSame(
842  [ [ 'sortkey' => '', 'category' => $name ],
843  [ 'sortkey' => 'Sort me', 'category' => 'Foo', 'missing' => true ],
844  [ 'sortkey' => '', 'category' => 'Hidden', 'hidden' => true ] ],
845  $res[0]['parse']['categories']
846  );
847  $this->assertArrayNotHasKey( 'warnings', $res[0] );
848  }
849 }
ApiUsageException\getStatusValue
getStatusValue()
Fetch the error status.
Definition: ApiUsageException.php:181
ApiParseTest\testPst
testPst()
Definition: ApiParseTest.php:486
ApiParseTest\testParseByOldId
testParseByOldId()
Definition: ApiParseTest.php:171
Revision\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: Revision.php:50
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:103
ApiParseTest\$pageId
static $pageId
Definition: ApiParseTest.php:12
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
ApiParseTest\testTitleProvided
testTitleProvided()
Definition: ApiParseTest.php:231
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:813
$html
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:2013
ApiParseTest\testIndicators
testIndicators()
Definition: ApiParseTest.php:705
ApiParseTest\testEffectiveLangLinks
testEffectiveLangLinks()
Definition: ApiParseTest.php:556
ApiParseTest\testRevidNoText
testRevidNoText()
Definition: ApiParseTest.php:414
array
the array() calling protocol came about after MediaWiki 1.4rc1.
ApiParseTest\doTestLangLinks
doTestLangLinks(array $arr=[])
Definition: ApiParseTest.php:579
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2255
ApiParseTest\testFollowedRedirect
testFollowedRedirect()
Definition: ApiParseTest.php:329
Title\clearCaches
static clearCaches()
Text form (spaces not underscores) of the main part.
Definition: Title.php:3579
ApiParseTest\testSection
testSection()
Definition: ApiParseTest.php:241
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:864
ApiParseTest\assertParsedToRegExp
assertParsedToRegExp( $expected, array $res, $warnings=null)
Same as above, but asserts that the HTML matches a regexp instead of a literal string match.
Definition: ApiParseTest.php:64
ApiParseTest\doAssertParsedTo
doAssertParsedTo( $expected, array $res, $warnings, callable $callback)
Definition: ApiParseTest.php:68
$res
$res
Definition: database.txt:21
ApiParseTest\testSectionNoContent
testSectionNoContent()
Definition: ApiParseTest.php:266
ApiParseTest\testParseTreeNonWikitext
testParseTreeNonWikitext()
Definition: ApiParseTest.php:774
ApiParseTest\testFollowedRedirectById
testFollowedRedirectById()
Definition: ApiParseTest.php:344
ApiParseTest\testSuppressed
testSuppressed()
Definition: ApiParseTest.php:202
ApiParseTest\testOnlyPst
testOnlyPst()
Definition: ApiParseTest.php:503
ApiParseTest\testLangLinks
testLangLinks()
Definition: ApiParseTest.php:598
php
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:37
ApiParseTest\testIndicatorsWithSkin
testIndicatorsWithSkin()
Definition: ApiParseTest.php:722
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:100
ApiParseTest\testExistingSection
testExistingSection()
Definition: ApiParseTest.php:461
ApiParseTest\testTitleNoText
testTitleNoText()
Definition: ApiParseTest.php:403
ApiParseTest\testParseByName
testParseByName()
Definition: ApiParseTest.php:148
ApiParseTest\testNoPst
testNoPst()
Definition: ApiParseTest.php:472
ApiParseTest\testNewSectionWithPage
testNewSectionWithPage()
Definition: ApiParseTest.php:290
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2603
ApiParseTest\testLimitReports
testLimitReports()
Definition: ApiParseTest.php:761
ApiParseTest\testRevId
testRevId()
Definition: ApiParseTest.php:393
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
DB_MASTER
const DB_MASTER
Definition: defines.php:29
ApiParseTest\testNewSection
testNewSection()
Definition: ApiParseTest.php:449
ApiParseTest\testParseTree
testParseTree()
Definition: ApiParseTest.php:786
OutputPage
This class should be covered by a general architecture document which does not exist as of January 20...
Definition: OutputPage.php:45
ApiParseTest\testModulesWithSkin
testModulesWithSkin()
Definition: ApiParseTest.php:669
ApiParseTest\assertParsedTo
assertParsedTo( $expected, array $res, $warnings=null)
Assert that the given result of calling $this->doApiRequest() with action=parse resulted in $html,...
Definition: ApiParseTest.php:51
ApiParseTest\testNonexistentOldId
testNonexistentOldId()
Definition: ApiParseTest.php:302
ApiParseTest\testParseById
testParseById()
Definition: ApiParseTest.php:163
ApiParseTest\testHeadItems
testHeadItems()
Definition: ApiParseTest.php:607
ApiTestCase
Definition: ApiTestCase.php:5
ApiTestCase\editPage
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
Definition: ApiTestCase.php:52
ApiParseTest
API Database medium.
Definition: ApiParseTest.php:10
SkinFactory
Factory class to create Skin objects.
Definition: SkinFactory.php:31
ApiParseTest\testDisableTidy
testDisableTidy()
Definition: ApiParseTest.php:806
ApiParseTest\testSerializationError
testSerializationError()
Definition: ApiParseTest.php:435
ApiParseTest\testTextNoContentModel
testTextNoContentModel()
Definition: ApiParseTest.php:425
$status
Status::newGood()` to allow deletion, and then `return false` from the hook function. Ensure you consume the 'ChangeTagAfterDelete' hook to carry out custom deletion actions. $tag:name of the tag $user:user initiating the action & $status:Status object. See above. 'ChangeTagsListActive':Allows you to nominate which of the tags your extension uses are in active use. & $tags:list of all active tags. Append to this array. 'ChangeTagsAfterUpdateTags':Called after tags have been updated with the ChangeTags::updateTags function. Params:$addedTags:tags effectively added in the update $removedTags:tags effectively removed in the update $prevTags:tags that were present prior to the update $rc_id:recentchanges table id $rev_id:revision table id $log_id:logging table id $params:tag params $rc:RecentChange being tagged when the tagging accompanies the action or null $user:User who performed the tagging when the tagging is subsequent to the action or null 'ChangeTagsAllowedAdd':Called when checking if a user can add tags to a change. & $allowedTags:List of all the tags the user is allowed to add. Any tags the user wants to add( $addTags) that are not in this array will cause it to fail. You may add or remove tags to this array as required. $addTags:List of tags user intends to add. $user:User who is adding the tags. 'ChangeUserGroups':Called before user groups are changed. $performer:The User who will perform the change $user:The User whose groups will be changed & $add:The groups that will be added & $remove:The groups that will be removed 'Collation::factory':Called if $wgCategoryCollation is an unknown collation. $collationName:Name of the collation in question & $collationObject:Null. Replace with a subclass of the Collation class that implements the collation given in $collationName. 'ConfirmEmailComplete':Called after a user 's email has been confirmed successfully. $user:user(object) whose email is being confirmed 'ContentAlterParserOutput':Modify parser output for a given content object. Called by Content::getParserOutput after parsing has finished. Can be used for changes that depend on the result of the parsing but have to be done before LinksUpdate is called(such as adding tracking categories based on the rendered HTML). $content:The Content to render $title:Title of the page, as context $parserOutput:ParserOutput to manipulate 'ContentGetParserOutput':Customize parser output for a given content object, called by AbstractContent::getParserOutput. May be used to override the normal model-specific rendering of page content. $content:The Content to render $title:Title of the page, as context $revId:The revision ID, as context $options:ParserOptions for rendering. To avoid confusing the parser cache, the output can only depend on parameters provided to this hook function, not on global state. $generateHtml:boolean, indicating whether full HTML should be generated. If false, generation of HTML may be skipped, but other information should still be present in the ParserOutput object. & $output:ParserOutput, to manipulate or replace 'ContentHandlerDefaultModelFor':Called when the default content model is determined for a given title. May be used to assign a different model for that title. $title:the Title in question & $model:the model name. Use with CONTENT_MODEL_XXX constants. 'ContentHandlerForModelID':Called when a ContentHandler is requested for a given content model name, but no entry for that model exists in $wgContentHandlers. Note:if your extension implements additional models via this hook, please use GetContentModels hook to make them known to core. $modeName:the requested content model name & $handler:set this to a ContentHandler object, if desired. 'ContentModelCanBeUsedOn':Called to determine whether that content model can be used on a given page. This is especially useful to prevent some content models to be used in some special location. $contentModel:ID of the content model in question $title:the Title in question. & $ok:Output parameter, whether it is OK to use $contentModel on $title. Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok. 'ContribsPager::getQueryInfo':Before the contributions query is about to run & $pager:Pager object for contributions & $queryInfo:The query for the contribs Pager 'ContribsPager::reallyDoQuery':Called before really executing the query for My Contributions & $data:an array of results of all contribs queries $pager:The ContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'ContributionsLineEnding':Called before a contributions HTML line is finished $page:SpecialPage object for contributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'ContributionsToolLinks':Change tool links above Special:Contributions $id:User identifier $title:User page title & $tools:Array of tool links $specialPage:SpecialPage instance for context and services. Can be either SpecialContributions or DeletedContributionsPage. Extensions should type hint against a generic SpecialPage though. 'ConvertContent':Called by AbstractContent::convert when a conversion to another content model is requested. Handler functions that modify $result should generally return false to disable further attempts at conversion. $content:The Content object to be converted. $toModel:The ID of the content model to convert to. $lossy:boolean indicating whether lossy conversion is allowed. & $result:Output parameter, in case the handler function wants to provide a converted Content object. Note that $result->getContentModel() must return $toModel. 'CustomEditor':When invoking the page editor Return true to allow the normal editor to be used, or false if implementing a custom editor, e.g. for a special namespace, etc. $article:Article being edited $user:User performing the edit 'DatabaseOraclePostInit':Called after initialising an Oracle database $db:the DatabaseOracle object 'DeletedContribsPager::reallyDoQuery':Called before really executing the query for Special:DeletedContributions Similar to ContribsPager::reallyDoQuery & $data:an array of results of all contribs queries $pager:The DeletedContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'DeletedContributionsLineEnding':Called before a DeletedContributions HTML line is finished. Similar to ContributionsLineEnding $page:SpecialPage object for DeletedContributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'DeleteUnknownPreferences':Called by the cleanupPreferences.php maintenance script to build a WHERE clause with which to delete preferences that are not known about. This hook is used by extensions that have dynamically-named preferences that should not be deleted in the usual cleanup process. For example, the Gadgets extension creates preferences prefixed with 'gadget-', and so anything with that prefix is excluded from the deletion. &where:An array that will be passed as the $cond parameter to IDatabase::select() to determine what will be deleted from the user_properties table. $db:The IDatabase object, useful for accessing $db->buildLike() etc. 'DifferenceEngineAfterLoadNewText':called in DifferenceEngine::loadNewText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before returning true from this function. $differenceEngine:DifferenceEngine object 'DifferenceEngineLoadTextAfterNewContentIsLoaded':called in DifferenceEngine::loadText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before checking if the variable 's value is null. This hook can be used to inject content into said class member variable. $differenceEngine:DifferenceEngine object 'DifferenceEngineMarkPatrolledLink':Allows extensions to change the "mark as patrolled" link which is shown both on the diff header as well as on the bottom of a page, usually wrapped in a span element which has class="patrollink". $differenceEngine:DifferenceEngine object & $markAsPatrolledLink:The "mark as patrolled" link HTML(string) $rcid:Recent change ID(rc_id) for this change(int) 'DifferenceEngineMarkPatrolledRCID':Allows extensions to possibly change the rcid parameter. For example the rcid might be set to zero due to the user being the same as the performer of the change but an extension might still want to show it under certain conditions. & $rcid:rc_id(int) of the change or 0 $differenceEngine:DifferenceEngine object $change:RecentChange object $user:User object representing the current user 'DifferenceEngineNewHeader':Allows extensions to change the $newHeader variable, which contains information about the new revision, such as the revision 's author, whether the revision was marked as a minor edit or not, etc. $differenceEngine:DifferenceEngine object & $newHeader:The string containing the various #mw-diff-otitle[1-5] divs, which include things like revision author info, revision comment, RevisionDelete link and more $formattedRevisionTools:Array containing revision tools, some of which may have been injected with the DiffRevisionTools hook $nextlink:String containing the link to the next revision(if any) $status
Definition: hooks.txt:1255
MediaWikiTestCase\setGroupPermissions
setGroupPermissions( $newPerms, $newKey=null, $newValue=null)
Alters $wgGroupPermissions for the duration of the test.
Definition: MediaWikiTestCase.php:920
ApiParseTest\testInvalidTitle
testInvalidTitle()
Definition: ApiParseTest.php:359
$skin
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 $skin
Definition: hooks.txt:2011
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ApiParseTest\testInvalidSection
testInvalidSection()
Definition: ApiParseTest.php:256
ApiParseTest\testTitleWithNonMatchingRevId
testTitleWithNonMatchingRevId()
Definition: ApiParseTest.php:379
ApiParseTest\testLangLinksWithSkin
testLangLinksWithSkin()
Definition: ApiParseTest.php:602
ApiParseTest\testIwlinks
testIwlinks()
Definition: ApiParseTest.php:742
ApiTestCase\apiExceptionHasCode
static apiExceptionHasCode(ApiUsageException $ex, $code)
Definition: ApiTestCase.php:225
ApiTestCase\revisionDelete
revisionDelete( $rev, array $value=[Revision::DELETED_TEXT=> 1], $comment='')
Revision-deletes a revision.
Definition: ApiTestCase.php:67
ApiParseTest\testNonexistentPage
testNonexistentPage()
Definition: ApiParseTest.php:215
ApiParseTest\testFormatCategories
testFormatCategories()
Definition: ApiParseTest.php:828
ApiParseTest\setupInterwiki
setupInterwiki()
Set up an interwiki entry for testing.
Definition: ApiParseTest.php:104
ApiParseTest\testModules
testModules()
Definition: ApiParseTest.php:644
ApiParseTest\testHeadItemsWithSkin
testHeadItemsWithSkin()
Definition: ApiParseTest.php:624
ApiParseTest\$revIds
static $revIds
Definition: ApiParseTest.php:13
ApiParseTest\testHeadHtml
testHeadHtml()
Definition: ApiParseTest.php:527
class
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:56
MediaWikiTestCase\setTemporaryHook
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Definition: MediaWikiTestCase.php:2014
ApiParseTest\testRevDel
testRevDel()
Definition: ApiParseTest.php:181
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:628
ApiParseTest\testUnfollowedRedirect
testUnfollowedRedirect()
Definition: ApiParseTest.php:312
ApiParseTest\testRevDelNoPermission
testRevDelNoPermission()
Definition: ApiParseTest.php:192
ApiParseTest\addDBDataOnce
addDBDataOnce()
Stub.
Definition: ApiParseTest.php:15
Revision\DELETED_TEXT
const DELETED_TEXT
Definition: Revision.php:47
ApiParseTest\setupSkin
setupSkin()
Set up a skin for testing.
Definition: ApiParseTest.php:128
ApiParseTest\testTitleWithNonexistentRevId
testTitleWithNonexistentRevId()
Definition: ApiParseTest.php:368
ApiParseTest\testCategoriesHtml
testCategoriesHtml()
Definition: ApiParseTest.php:540