MediaWiki  1.27.2
WikiPageTest.php
Go to the documentation of this file.
1 <?php
2 
10 
11  protected $pages_to_delete;
12 
13  function __construct( $name = null, array $data = [], $dataName = '' ) {
14  parent::__construct( $name, $data, $dataName );
15 
16  $this->tablesUsed = array_merge(
17  $this->tablesUsed,
18  [ 'page',
19  'revision',
20  'text',
21 
22  'recentchanges',
23  'logging',
24 
25  'page_props',
26  'pagelinks',
27  'categorylinks',
28  'langlinks',
29  'externallinks',
30  'imagelinks',
31  'templatelinks',
32  'iwlinks' ] );
33  }
34 
35  protected function setUp() {
36  parent::setUp();
37  $this->pages_to_delete = [];
38 
39  LinkCache::singleton()->clear(); # avoid cached redirect status, etc
40  }
41 
42  protected function tearDown() {
43  foreach ( $this->pages_to_delete as $p ) {
44  /* @var $p WikiPage */
45 
46  try {
47  if ( $p->exists() ) {
48  $p->doDeleteArticle( "testing done." );
49  }
50  } catch ( MWException $ex ) {
51  // fail silently
52  }
53  }
54  parent::tearDown();
55  }
56 
62  protected function newPage( $title, $model = null ) {
63  if ( is_string( $title ) ) {
64  $ns = $this->getDefaultWikitextNS();
66  }
67 
68  $p = new WikiPage( $title );
69 
70  $this->pages_to_delete[] = $p;
71 
72  return $p;
73  }
74 
82  protected function createPage( $page, $text, $model = null ) {
83  if ( is_string( $page ) || $page instanceof Title ) {
84  $page = $this->newPage( $page, $model );
85  }
86 
87  $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
88  $page->doEditContent( $content, "testing", EDIT_NEW );
89 
90  return $page;
91  }
92 
96  public function testDoEditContent() {
97  $page = $this->newPage( "WikiPageTest_testDoEditContent" );
98  $title = $page->getTitle();
99 
101  "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
102  . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
103  $title,
105  );
106 
107  $page->doEditContent( $content, "[[testing]] 1" );
108 
109  $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
110  $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
111  $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
112  $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
113 
114  $id = $page->getId();
115 
116  # ------------------------
117  $dbr = wfGetDB( DB_SLAVE );
118  $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
119  $n = $res->numRows();
120  $res->free();
121 
122  $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
123 
124  # ------------------------
125  $page = new WikiPage( $title );
126 
127  $retrieved = $page->getContent();
128  $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
129 
130  # ------------------------
132  "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
133  . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
134  $title,
136  );
137 
138  $page->doEditContent( $content, "testing 2" );
139 
140  # ------------------------
141  $page = new WikiPage( $title );
142 
143  $retrieved = $page->getContent();
144  $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
145 
146  # ------------------------
147  $dbr = wfGetDB( DB_SLAVE );
148  $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
149  $n = $res->numRows();
150  $res->free();
151 
152  $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
153  }
154 
158  public function testDoEdit() {
159  $this->hideDeprecated( "WikiPage::doEdit" );
160  $this->hideDeprecated( "WikiPage::getText" );
161  $this->hideDeprecated( "Revision::getText" );
162 
163  // NOTE: assume help namespace will default to wikitext
164  $title = Title::newFromText( "Help:WikiPageTest_testDoEdit" );
165 
166  $page = $this->newPage( $title );
167 
168  $text = "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
169  . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
170 
171  $page->doEdit( $text, "[[testing]] 1" );
172 
173  $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
174  $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
175  $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
176  $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
177 
178  $id = $page->getId();
179 
180  # ------------------------
181  $dbr = wfGetDB( DB_SLAVE );
182  $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
183  $n = $res->numRows();
184  $res->free();
185 
186  $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
187 
188  # ------------------------
189  $page = new WikiPage( $title );
190 
191  $retrieved = $page->getText();
192  $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
193 
194  # ------------------------
195  $text = "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
196  . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.";
197 
198  $page->doEdit( $text, "testing 2" );
199 
200  # ------------------------
201  $page = new WikiPage( $title );
202 
203  $retrieved = $page->getText();
204  $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
205 
206  # ------------------------
207  $dbr = wfGetDB( DB_SLAVE );
208  $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
209  $n = $res->numRows();
210  $res->free();
211 
212  $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
213  }
214 
218  public function testDoQuickEditContent() {
219  global $wgUser;
220 
221  $page = $this->createPage(
222  "WikiPageTest_testDoQuickEditContent",
223  "original text",
225  );
226 
228  "quick text",
229  $page->getTitle(),
231  );
232  $page->doQuickEditContent( $content, $wgUser, "testing q" );
233 
234  # ---------------------
235  $page = new WikiPage( $page->getTitle() );
236  $this->assertTrue( $content->equals( $page->getContent() ) );
237  }
238 
242  public function testDoDeleteArticle() {
243  $page = $this->createPage(
244  "WikiPageTest_testDoDeleteArticle",
245  "[[original text]] foo",
247  );
248  $id = $page->getId();
249 
250  $page->doDeleteArticle( "testing deletion" );
251 
252  $this->assertFalse(
253  $page->getTitle()->getArticleID() > 0,
254  "Title object should now have page id 0"
255  );
256  $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
257  $this->assertFalse(
258  $page->exists(),
259  "WikiPage::exists should return false after page was deleted"
260  );
261  $this->assertNull(
262  $page->getContent(),
263  "WikiPage::getContent should return null after page was deleted"
264  );
265  $this->assertFalse(
266  $page->getText(),
267  "WikiPage::getText should return false after page was deleted"
268  );
269 
270  $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
271  $this->assertFalse(
272  $t->exists(),
273  "Title::exists should return false after page was deleted"
274  );
275 
276  // Run the job queue
278  $jobs = new RunJobs;
279  $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
280  $jobs->execute();
281 
282  # ------------------------
283  $dbr = wfGetDB( DB_SLAVE );
284  $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
285  $n = $res->numRows();
286  $res->free();
287 
288  $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
289  }
290 
294  public function testDoDeleteUpdates() {
295  $page = $this->createPage(
296  "WikiPageTest_testDoDeleteArticle",
297  "[[original text]] foo",
299  );
300  $id = $page->getId();
301 
302  // Similar to MovePage logic
303  wfGetDB( DB_MASTER )->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
304  $page->doDeleteUpdates( $id );
305 
306  // Run the job queue
308  $jobs = new RunJobs;
309  $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
310  $jobs->execute();
311 
312  # ------------------------
313  $dbr = wfGetDB( DB_SLAVE );
314  $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
315  $n = $res->numRows();
316  $res->free();
317 
318  $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
319  }
320 
324  public function testGetRevision() {
325  $page = $this->newPage( "WikiPageTest_testGetRevision" );
326 
327  $rev = $page->getRevision();
328  $this->assertNull( $rev );
329 
330  # -----------------
331  $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
332 
333  $rev = $page->getRevision();
334 
335  $this->assertEquals( $page->getLatest(), $rev->getId() );
336  $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
337  }
338 
342  public function testGetContent() {
343  $page = $this->newPage( "WikiPageTest_testGetContent" );
344 
345  $content = $page->getContent();
346  $this->assertNull( $content );
347 
348  # -----------------
349  $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
350 
351  $content = $page->getContent();
352  $this->assertEquals( "some text", $content->getNativeData() );
353  }
354 
358  public function testGetText() {
359  $this->hideDeprecated( "WikiPage::getText" );
360 
361  $page = $this->newPage( "WikiPageTest_testGetText" );
362 
363  $text = $page->getText();
364  $this->assertFalse( $text );
365 
366  # -----------------
367  $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
368 
369  $text = $page->getText();
370  $this->assertEquals( "some text", $text );
371  }
372 
376  public function testGetContentModel() {
377  global $wgContentHandlerUseDB;
378 
379  if ( !$wgContentHandlerUseDB ) {
380  $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
381  }
382 
383  $page = $this->createPage(
384  "WikiPageTest_testGetContentModel",
385  "some text",
387  );
388 
389  $page = new WikiPage( $page->getTitle() );
390  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $page->getContentModel() );
391  }
392 
396  public function testGetContentHandler() {
397  global $wgContentHandlerUseDB;
398 
399  if ( !$wgContentHandlerUseDB ) {
400  $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
401  }
402 
403  $page = $this->createPage(
404  "WikiPageTest_testGetContentHandler",
405  "some text",
407  );
408 
409  $page = new WikiPage( $page->getTitle() );
410  $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
411  }
412 
416  public function testExists() {
417  $page = $this->newPage( "WikiPageTest_testExists" );
418  $this->assertFalse( $page->exists() );
419 
420  # -----------------
421  $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
422  $this->assertTrue( $page->exists() );
423 
424  $page = new WikiPage( $page->getTitle() );
425  $this->assertTrue( $page->exists() );
426 
427  # -----------------
428  $page->doDeleteArticle( "done testing" );
429  $this->assertFalse( $page->exists() );
430 
431  $page = new WikiPage( $page->getTitle() );
432  $this->assertFalse( $page->exists() );
433  }
434 
435  public static function provideHasViewableContent() {
436  return [
437  [ 'WikiPageTest_testHasViewableContent', false, true ],
438  [ 'Special:WikiPageTest_testHasViewableContent', false ],
439  [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
440  [ 'Special:Userlogin', true ],
441  [ 'MediaWiki:help', true ],
442  ];
443  }
444 
449  public function testHasViewableContent( $title, $viewable, $create = false ) {
450  $page = $this->newPage( $title );
451  $this->assertEquals( $viewable, $page->hasViewableContent() );
452 
453  if ( $create ) {
454  $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
455  $this->assertTrue( $page->hasViewableContent() );
456 
457  $page = new WikiPage( $page->getTitle() );
458  $this->assertTrue( $page->hasViewableContent() );
459  }
460  }
461 
462  public static function provideGetRedirectTarget() {
463  return [
464  [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ],
465  [
466  'WikiPageTest_testGetRedirectTarget_2',
468  "#REDIRECT [[hello world]]",
469  "Hello world"
470  ],
471  ];
472  }
473 
478  public function testGetRedirectTarget( $title, $model, $text, $target ) {
479  $this->setMwGlobals( [
480  'wgCapitalLinks' => true,
481  ] );
482 
483  $page = $this->createPage( $title, $text, $model );
484 
485  # sanity check, because this test seems to fail for no reason for some people.
486  $c = $page->getContent();
487  $this->assertEquals( 'WikitextContent', get_class( $c ) );
488 
489  # now, test the actual redirect
490  $t = $page->getRedirectTarget();
491  $this->assertEquals( $target, is_null( $t ) ? null : $t->getPrefixedText() );
492  }
493 
498  public function testIsRedirect( $title, $model, $text, $target ) {
499  $page = $this->createPage( $title, $text, $model );
500  $this->assertEquals( !is_null( $target ), $page->isRedirect() );
501  }
502 
503  public static function provideIsCountable() {
504  return [
505 
506  // any
507  [ 'WikiPageTest_testIsCountable',
509  '',
510  'any',
511  true
512  ],
513  [ 'WikiPageTest_testIsCountable',
515  'Foo',
516  'any',
517  true
518  ],
519 
520  // comma
521  [ 'WikiPageTest_testIsCountable',
523  'Foo',
524  'comma',
525  false
526  ],
527  [ 'WikiPageTest_testIsCountable',
529  'Foo, bar',
530  'comma',
531  true
532  ],
533 
534  // link
535  [ 'WikiPageTest_testIsCountable',
537  'Foo',
538  'link',
539  false
540  ],
541  [ 'WikiPageTest_testIsCountable',
543  'Foo [[bar]]',
544  'link',
545  true
546  ],
547 
548  // redirects
549  [ 'WikiPageTest_testIsCountable',
551  '#REDIRECT [[bar]]',
552  'any',
553  false
554  ],
555  [ 'WikiPageTest_testIsCountable',
557  '#REDIRECT [[bar]]',
558  'comma',
559  false
560  ],
561  [ 'WikiPageTest_testIsCountable',
563  '#REDIRECT [[bar]]',
564  'link',
565  false
566  ],
567 
568  // not a content namespace
569  [ 'Talk:WikiPageTest_testIsCountable',
571  'Foo',
572  'any',
573  false
574  ],
575  [ 'Talk:WikiPageTest_testIsCountable',
577  'Foo, bar',
578  'comma',
579  false
580  ],
581  [ 'Talk:WikiPageTest_testIsCountable',
583  'Foo [[bar]]',
584  'link',
585  false
586  ],
587 
588  // not a content namespace, different model
589  [ 'MediaWiki:WikiPageTest_testIsCountable.js',
590  null,
591  'Foo',
592  'any',
593  false
594  ],
595  [ 'MediaWiki:WikiPageTest_testIsCountable.js',
596  null,
597  'Foo, bar',
598  'comma',
599  false
600  ],
601  [ 'MediaWiki:WikiPageTest_testIsCountable.js',
602  null,
603  'Foo [[bar]]',
604  'link',
605  false
606  ],
607  ];
608  }
609 
614  public function testIsCountable( $title, $model, $text, $mode, $expected ) {
615  global $wgContentHandlerUseDB;
616 
617  $this->setMwGlobals( 'wgArticleCountMethod', $mode );
618 
620 
621  if ( !$wgContentHandlerUseDB
622  && $model
624  ) {
625  $this->markTestSkipped( "Can not use non-default content model $model for "
626  . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
627  }
628 
629  $page = $this->createPage( $title, $text, $model );
630 
631  $editInfo = $page->prepareContentForEdit( $page->getContent() );
632 
633  $v = $page->isCountable();
634  $w = $page->isCountable( $editInfo );
635 
636  $this->assertEquals(
637  $expected,
638  $v,
639  "isCountable( null ) returned unexpected value " . var_export( $v, true )
640  . " instead of " . var_export( $expected, true )
641  . " in mode `$mode` for text \"$text\""
642  );
643 
644  $this->assertEquals(
645  $expected,
646  $w,
647  "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
648  . " instead of " . var_export( $expected, true )
649  . " in mode `$mode` for text \"$text\""
650  );
651  }
652 
653  public static function provideGetParserOutput() {
654  return [
655  [ CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>" ],
656  // @todo more...?
657  ];
658  }
659 
664  public function testGetParserOutput( $model, $text, $expectedHtml ) {
665  $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
666 
667  $opt = $page->makeParserOptions( 'canonical' );
668  $po = $page->getParserOutput( $opt );
669  $text = $po->getText();
670 
671  $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
672  $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
673 
674  $this->assertEquals( $expectedHtml, $text );
675 
676  return $po;
677  }
678 
682  public function testGetParserOutput_nonexisting() {
683  static $count = 0;
684  $count++;
685 
686  $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
687 
688  $opt = new ParserOptions();
689  $po = $page->getParserOutput( $opt );
690 
691  $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
692  }
693 
697  public function testGetParserOutput_badrev() {
698  $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT );
699 
700  $opt = new ParserOptions();
701  $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
702 
703  // @todo would be neat to also test deleted revision
704 
705  $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
706  }
707 
708  public static $sections =
709 
710  "Intro
711 
712 == stuff ==
713 hello world
714 
715 == test ==
716 just a test
717 
718 == foo ==
719 more stuff
720 ";
721 
722  public function dataReplaceSection() {
723  // NOTE: assume the Help namespace to contain wikitext
724  return [
725  [ 'Help:WikiPageTest_testReplaceSection',
726  CONTENT_MODEL_WIKITEXT,
727  WikiPageTest::$sections,
728  "0",
729  "No more",
730  null,
731  trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest::$sections ) )
732  ],
733  [ 'Help:WikiPageTest_testReplaceSection',
734  CONTENT_MODEL_WIKITEXT,
735  WikiPageTest::$sections,
736  "",
737  "No more",
738  null,
739  "No more"
740  ],
741  [ 'Help:WikiPageTest_testReplaceSection',
742  CONTENT_MODEL_WIKITEXT,
743  WikiPageTest::$sections,
744  "2",
745  "== TEST ==\nmore fun",
746  null,
747  trim( preg_replace( '/^== test ==.*== foo ==/sm',
748  "== TEST ==\nmore fun\n\n== foo ==",
749  WikiPageTest::$sections ) )
750  ],
751  [ 'Help:WikiPageTest_testReplaceSection',
752  CONTENT_MODEL_WIKITEXT,
753  WikiPageTest::$sections,
754  "8",
755  "No more",
756  null,
757  trim( WikiPageTest::$sections )
758  ],
759  [ 'Help:WikiPageTest_testReplaceSection',
760  CONTENT_MODEL_WIKITEXT,
761  WikiPageTest::$sections,
762  "new",
763  "No more",
764  "New",
765  trim( WikiPageTest::$sections ) . "\n\n== New ==\n\nNo more"
766  ],
767  ];
768  }
769 
774  public function testReplaceSectionContent( $title, $model, $text, $section,
775  $with, $sectionTitle, $expected
776  ) {
777  $page = $this->createPage( $title, $text, $model );
778 
779  $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
780  $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
781 
782  $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
783  }
784 
789  public function testReplaceSectionAtRev( $title, $model, $text, $section,
790  $with, $sectionTitle, $expected
791  ) {
792  $page = $this->createPage( $title, $text, $model );
793  $baseRevId = $page->getLatest();
794 
795  $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
796  $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
797 
798  $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
799  }
800 
801  /* @todo FIXME: fix this!
802  public function testGetUndoText() {
803  $this->markTestSkippedIfNoDiff3();
804 
805  $text = "one";
806  $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
807  $rev1 = $page->getRevision();
808 
809  $text .= "\n\ntwo";
810  $page->doEditContent(
811  ContentHandler::makeContent( $text, $page->getTitle() ),
812  "adding section two"
813  );
814  $rev2 = $page->getRevision();
815 
816  $text .= "\n\nthree";
817  $page->doEditContent(
818  ContentHandler::makeContent( $text, $page->getTitle() ),
819  "adding section three"
820  );
821  $rev3 = $page->getRevision();
822 
823  $text .= "\n\nfour";
824  $page->doEditContent(
825  ContentHandler::makeContent( $text, $page->getTitle() ),
826  "adding section four"
827  );
828  $rev4 = $page->getRevision();
829 
830  $text .= "\n\nfive";
831  $page->doEditContent(
832  ContentHandler::makeContent( $text, $page->getTitle() ),
833  "adding section five"
834  );
835  $rev5 = $page->getRevision();
836 
837  $text .= "\n\nsix";
838  $page->doEditContent(
839  ContentHandler::makeContent( $text, $page->getTitle() ),
840  "adding section six"
841  );
842  $rev6 = $page->getRevision();
843 
844  $undo6 = $page->getUndoText( $rev6 );
845  if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
846  $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
847 
848  $undo3 = $page->getUndoText( $rev4, $rev2 );
849  if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
850  $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
851 
852  $undo2 = $page->getUndoText( $rev2 );
853  if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
854  $this->assertEquals( "one\n\nfive", $undo2 );
855  }
856  */
857 
862  public function broken_testDoRollback() {
863  $admin = new User();
864  $admin->setName( "Admin" );
865 
866  $text = "one";
867  $page = $this->newPage( "WikiPageTest_testDoRollback" );
868  $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
869  "section one", EDIT_NEW, false, $admin );
870 
871  $user1 = new User();
872  $user1->setName( "127.0.1.11" );
873  $text .= "\n\ntwo";
874  $page = new WikiPage( $page->getTitle() );
875  $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
876  "adding section two", 0, false, $user1 );
877 
878  $user2 = new User();
879  $user2->setName( "127.0.2.13" );
880  $text .= "\n\nthree";
881  $page = new WikiPage( $page->getTitle() );
882  $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
883  "adding section three", 0, false, $user2 );
884 
885  # we are having issues with doRollback spuriously failing. Apparently
886  # the last revision somehow goes missing or not committed under some
887  # circumstances. So, make sure the last revision has the right user name.
888  $dbr = wfGetDB( DB_SLAVE );
889  $this->assertEquals( 3, Revision::countByPageId( $dbr, $page->getId() ) );
890 
891  $page = new WikiPage( $page->getTitle() );
892  $rev3 = $page->getRevision();
893  $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
894 
895  $rev2 = $rev3->getPrevious();
896  $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
897 
898  $rev1 = $rev2->getPrevious();
899  $this->assertEquals( 'Admin', $rev1->getUserText() );
900 
901  # now, try the actual rollback
902  $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
903  $token = $admin->getEditToken(
904  [ $page->getTitle()->getPrefixedText(), $user2->getName() ],
905  null
906  );
907  $errors = $page->doRollback(
908  $user2->getName(),
909  "testing revert",
910  $token,
911  false,
912  $details,
913  $admin
914  );
915 
916  if ( $errors ) {
917  $this->fail( "Rollback failed:\n" . print_r( $errors, true )
918  . ";\n" . print_r( $details, true ) );
919  }
920 
921  $page = new WikiPage( $page->getTitle() );
922  $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
923  "rollback did not revert to the correct revision" );
924  $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
925  }
926 
931  public function testDoRollback() {
932  $admin = new User();
933  $admin->setName( "Admin" );
934 
935  $text = "one";
936  $page = $this->newPage( "WikiPageTest_testDoRollback" );
937  $page->doEditContent(
938  ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
939  "section one",
940  EDIT_NEW,
941  false,
942  $admin
943  );
944  $rev1 = $page->getRevision();
945 
946  $user1 = new User();
947  $user1->setName( "127.0.1.11" );
948  $text .= "\n\ntwo";
949  $page = new WikiPage( $page->getTitle() );
950  $page->doEditContent(
951  ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
952  "adding section two",
953  0,
954  false,
955  $user1
956  );
957 
958  # now, try the rollback
959  $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
960  $token = $admin->getEditToken(
961  [ $page->getTitle()->getPrefixedText(), $user1->getName() ],
962  null
963  );
964  $errors = $page->doRollback(
965  $user1->getName(),
966  "testing revert",
967  $token,
968  false,
969  $details,
970  $admin
971  );
972 
973  if ( $errors ) {
974  $this->fail( "Rollback failed:\n" . print_r( $errors, true )
975  . ";\n" . print_r( $details, true ) );
976  }
977 
978  $page = new WikiPage( $page->getTitle() );
979  $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
980  "rollback did not revert to the correct revision" );
981  $this->assertEquals( "one", $page->getContent()->getNativeData() );
982  }
983 
987  public function testDoRollbackFailureSameContent() {
988  $admin = new User();
989  $admin->setName( "Admin" );
990  $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
991 
992  $text = "one";
993  $page = $this->newPage( "WikiPageTest_testDoRollback" );
994  $page->doEditContent(
995  ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
996  "section one",
997  EDIT_NEW,
998  false,
999  $admin
1000  );
1001  $rev1 = $page->getRevision();
1002 
1003  $user1 = new User();
1004  $user1->setName( "127.0.1.11" );
1005  $user1->addGroup( "sysop" ); # XXX: make the test user a sysop...
1006  $text .= "\n\ntwo";
1007  $page = new WikiPage( $page->getTitle() );
1008  $page->doEditContent(
1009  ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
1010  "adding section two",
1011  0,
1012  false,
1013  $user1
1014  );
1015 
1016  # now, do a the rollback from the same user was doing the edit before
1017  $resultDetails = [];
1018  $token = $user1->getEditToken(
1019  [ $page->getTitle()->getPrefixedText(), $user1->getName() ],
1020  null
1021  );
1022  $errors = $page->doRollback(
1023  $user1->getName(),
1024  "testing revert same user",
1025  $token,
1026  false,
1027  $resultDetails,
1028  $admin
1029  );
1030 
1031  $this->assertEquals( [], $errors, "Rollback failed same user" );
1032 
1033  # now, try the rollback
1034  $resultDetails = [];
1035  $token = $admin->getEditToken(
1036  [ $page->getTitle()->getPrefixedText(), $user1->getName() ],
1037  null
1038  );
1039  $errors = $page->doRollback(
1040  $user1->getName(),
1041  "testing revert",
1042  $token,
1043  false,
1044  $resultDetails,
1045  $admin
1046  );
1047 
1048  $this->assertEquals( [ [ 'alreadyrolled', 'WikiPageTest testDoRollback',
1049  '127.0.1.11', 'Admin' ] ], $errors, "Rollback not failed" );
1050 
1051  $page = new WikiPage( $page->getTitle() );
1052  $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1053  "rollback did not revert to the correct revision" );
1054  $this->assertEquals( "one", $page->getContent()->getNativeData() );
1055  }
1056 
1057  public static function provideGetAutosummary() {
1058  return [
1059  [
1060  'Hello there, world!',
1061  '#REDIRECT [[Foo]]',
1062  0,
1063  '/^Redirected page .*Foo/'
1064  ],
1065 
1066  [
1067  null,
1068  'Hello world!',
1069  EDIT_NEW,
1070  '/^Created page .*Hello/'
1071  ],
1072 
1073  [
1074  'Hello there, world!',
1075  '',
1076  0,
1077  '/^Blanked/'
1078  ],
1079 
1080  [
1081  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
1082  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
1083  voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
1084  clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
1085  'Hello world!',
1086  0,
1087  '/^Replaced .*Hello/'
1088  ],
1089 
1090  [
1091  'foo',
1092  'bar',
1093  0,
1094  '/^$/'
1095  ],
1096  ];
1097  }
1098 
1103  public function testGetAutosummary( $old, $new, $flags, $expected ) {
1104  $this->hideDeprecated( "WikiPage::getAutosummary" );
1105 
1106  $page = $this->newPage( "WikiPageTest_testGetAutosummary" );
1107 
1108  $summary = $page->getAutosummary( $old, $new, $flags );
1109 
1110  $this->assertTrue( (bool)preg_match( $expected, $summary ),
1111  "Autosummary didn't match expected pattern $expected: $summary" );
1112  }
1113 
1114  public static function provideGetAutoDeleteReason() {
1115  return [
1116  [
1117  [],
1118  false,
1119  false
1120  ],
1121 
1122  [
1123  [
1124  [ "first edit", null ],
1125  ],
1126  "/first edit.*only contributor/",
1127  false
1128  ],
1129 
1130  [
1131  [
1132  [ "first edit", null ],
1133  [ "second edit", null ],
1134  ],
1135  "/second edit.*only contributor/",
1136  true
1137  ],
1138 
1139  [
1140  [
1141  [ "first edit", "127.0.2.22" ],
1142  [ "second edit", "127.0.3.33" ],
1143  ],
1144  "/second edit/",
1145  true
1146  ],
1147 
1148  [
1149  [
1150  [
1151  "first edit: "
1152  . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1153  . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1154  . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1155  . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1156  . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1157  null
1158  ],
1159  ],
1160  '/first edit:.*\.\.\."/',
1161  false
1162  ],
1163 
1164  [
1165  [
1166  [ "first edit", "127.0.2.22" ],
1167  [ "", "127.0.3.33" ],
1168  ],
1169  "/before blanking.*first edit/",
1170  true
1171  ],
1172 
1173  ];
1174  }
1175 
1180  public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1181  global $wgUser;
1182 
1183  // NOTE: assume Help namespace to contain wikitext
1184  $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1185 
1186  $c = 1;
1187 
1188  foreach ( $edits as $edit ) {
1189  $user = new User();
1190 
1191  if ( !empty( $edit[1] ) ) {
1192  $user->setName( $edit[1] );
1193  } else {
1194  $user = $wgUser;
1195  }
1196 
1197  $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1198 
1199  $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
1200 
1201  $c += 1;
1202  }
1203 
1204  $reason = $page->getAutoDeleteReason( $hasHistory );
1205 
1206  if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
1207  $this->assertEquals( $expectedResult, $reason );
1208  } else {
1209  $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1210  "Autosummary didn't match expected pattern $expectedResult: $reason" );
1211  }
1212 
1213  $this->assertEquals( $expectedHistory, $hasHistory,
1214  "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1215 
1216  $page->doDeleteArticle( "done" );
1217  }
1218 
1219  public static function providePreSaveTransform() {
1220  return [
1221  [ 'hello this is ~~~',
1222  "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1223  ],
1224  [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1225  'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1226  ],
1227  ];
1228  }
1229 
1233  public function testWikiPageFactory() {
1234  $title = Title::makeTitle( NS_FILE, 'Someimage.png' );
1235  $page = WikiPage::factory( $title );
1236  $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1237 
1238  $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' );
1239  $page = WikiPage::factory( $title );
1240  $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1241 
1242  $title = Title::makeTitle( NS_MAIN, 'SomePage' );
1243  $page = WikiPage::factory( $title );
1244  $this->assertEquals( 'WikiPage', get_class( $page ) );
1245  }
1246 }
static provideIsCountable()
loadParamsAndArgs($self=null, $opts=null, $args=null)
Process command line arguments $mOptions becomes an array with keys set to the option names $mArgs be...
static provideGetRedirectTarget()
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
the array() calling protocol came about after MediaWiki 1.4rc1.
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:277
testIsCountable($title, $model, $text, $mode, $expected)
provideIsCountable WikiPage::isCountable
static provideHasViewableContent()
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like please read the documentation for except in special pages derived from QueryPage It s a common pitfall for new developers to submit code containing SQL queries which examine huge numbers of rows Remember that COUNT * is(N), counting rows in atable is like counting beans in a bucket.------------------------------------------------------------------------Replication------------------------------------------------------------------------The largest installation of MediaWiki, Wikimedia, uses a large set ofslave MySQL servers replicating writes made to a master MySQL server.Itis important to understand the issues associated with this setup if youwant to write code destined for Wikipedia.It's often the case that the best algorithm to use for a given taskdepends on whether or not replication is in use.Due to our unabashedWikipedia-centrism, we often just use the replication-friendly version, but if you like, you can use wfGetLB() ->getServerCount() > 1 tocheck to see if replication is in use.===Lag===Lag primarily occurs when large write queries are sent to the master.Writes on the master are executed in parallel, but they are executed inserial when they are replicated to the slaves.The master writes thequery to the binlog when the transaction is committed.The slaves pollthe binlog and start executing the query as soon as it appears.They canservice reads while they are performing a write query, but will not readanything more from the binlog and thus will perform no more writes.Thismeans that if the write query runs for a long time, the slaves will lagbehind the master for the time it takes for the write query to complete.Lag can be exacerbated by high read load.MediaWiki's load balancer willstop sending reads to a slave when it is lagged by more than 30 seconds.If the load ratios are set incorrectly, or if there is too much loadgenerally, this may lead to a slave permanently hovering around 30seconds lag.If all slaves are lagged by more than 30 seconds, MediaWiki will stopwriting to the database.All edits and other write operations will berefused, with an error returned to the user.This gives the slaves achance to catch up.Before we had this mechanism, the slaves wouldregularly lag by several minutes, making review of recent editsdifficult.In addition to this, MediaWiki attempts to ensure that the user seesevents occurring on the wiki in chronological order.A few seconds of lagcan be tolerated, as long as the user sees a consistent picture fromsubsequent requests.This is done by saving the master binlog positionin the session, and then at the start of each request, waiting for theslave to catch up to that position before doing any reads from it.Ifthis wait times out, reads are allowed anyway, but the request isconsidered to be in"lagged slave mode".Lagged slave mode can bechecked by calling wfGetLB() ->getLaggedSlaveMode().The onlypractical consequence at present is a warning displayed in the pagefooter.===Lag avoidance===To avoid excessive lag, queries which write large numbers of rows shouldbe split up, generally to write one row at a time.Multi-row INSERT...SELECT queries are the worst offenders should be avoided altogether.Instead do the select first and then the insert.===Working with lag===Despite our best efforts, it's not practical to guarantee a low-lagenvironment.Lag will usually be less than one second, but mayoccasionally be up to 30 seconds.For scalability, it's very importantto keep load on the master low, so simply sending all your queries tothe master is not the answer.So when you have a genuine need forup-to-date data, the following approach is advised:1) Do a quick query to the master for a sequence number or timestamp 2) Run the full query on the slave and check if it matches the data you gotfrom the master 3) If it doesn't, run the full query on the masterTo avoid swamping the master every time the slaves lag, use of thisapproach should be kept to a minimum.In most cases you should just readfrom the slave and let the user deal with the delay.------------------------------------------------------------------------Lock contention------------------------------------------------------------------------Due to the high write rate on Wikipedia(and some other wikis), MediaWiki developers need to be very careful to structure their writesto avoid long-lasting locks.By default, MediaWiki opens a transactionat the first query, and commits it before the output is sent.Locks willbe held from the time when the query is done until the commit.So youcan reduce lock time by doing as much processing as possible before youdo your write queries.Often this approach is not good enough, and it becomes necessary toenclose small groups of queries in their own transaction.Use thefollowing syntax:$dbw=wfGetDB(DB_MASTER
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
testDoQuickEditContent()
WikiPage::doQuickEditContent.
static getDefaultModelFor(Title $title)
Returns the name of the default content model to be used for the page with the given title...
has been added to your &Future changes to this page and its associated Talk page will be listed there
testDoEditContent()
WikiPage::doEditContent.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
Represents a title within MediaWiki.
Definition: Title.php:34
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
testGetText()
WikiPage::getText.
testGetRevision()
WikiPage::getRevision.
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
testGetContentModel()
WikiPage::getContentModel.
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 true
Definition: hooks.txt:1798
static provideGetParserOutput()
static singleton()
Get an instance of this class.
Definition: LinkCache.php:61
__construct($name=null, array $data=[], $dataName= '')
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add etc
Definition: design.txt:12
$res
Definition: database.txt:21
$summary
doDeleteArticle($reason, $suppress=false, $u1=null, $u2=null, &$error= '', User $user=null)
Same as doDeleteArticleReal(), but returns a simple boolean.
Definition: WikiPage.php:2759
Base class that store and restore the Language objects.
Maintenance script that runs pending jobs.
Definition: runJobs.php:33
testGetContent()
WikiPage::getContent.
testIsRedirect($title, $model, $text, $target)
provideGetRedirectTarget WikiPage::isRedirect
const DB_SLAVE
Definition: Defines.php:46
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
hideDeprecated($function)
Don't throw a warning if $function is deprecated and called later.
newPage($title, $model=null)
static makeContent($text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1584
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Special handling for file pages.
CONTENT_MODEL_JAVASCRIPT
Uploads have to be specially set up to be secure.
testGetRedirectTarget($title, $model, $text, $target)
provideGetRedirectTarget WikiPage::getRedirectTarget
testDoEdit()
WikiPage::doEdit.
Class representing a MediaWiki article and history.
Definition: WikiPage.php:29
testGetContentHandler()
WikiPage::getContentHandler.
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
testGetParserOutput($model, $text, $expectedHtml)
provideGetParserOutput WikiPage::getParserOutput
const EDIT_NEW
Definition: Defines.php:179
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1004
ContentHandler Database ^— important, causes temporary tables to be used instead of the real databas...
Definition: WikiPageTest.php:9
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second redirect
const DB_MASTER
Definition: Defines.php:47
testDoDeleteUpdates()
WikiPage::doDeleteUpdates.
setMwGlobals($pairs, $value=null)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk page
Definition: hooks.txt:2338
testDoDeleteArticle()
WikiPage::doDeleteArticle.
testHasViewableContent($title, $viewable, $create=false)
provideHasViewableContent WikiPage::hasViewableContent
Special handling for category pages.
testExists()
WikiPage::exists.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2338
static destroySingletons()
Destroy the singleton instances.
$wgUser
Definition: Setup.php:794
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310
createPage($page, $text, $model=null)