MediaWiki REL1_30
RevisionStorageTest.php
Go to the documentation of this file.
1<?php
2
17 private $the_page;
18
19 function __construct( $name = null, array $data = [], $dataName = '' ) {
20 parent::__construct( $name, $data, $dataName );
21
22 $this->tablesUsed = array_merge( $this->tablesUsed,
23 [ 'page',
24 'revision',
25 'ip_changes',
26 'text',
27
28 'recentchanges',
29 'logging',
30
31 'page_props',
32 'pagelinks',
33 'categorylinks',
34 'langlinks',
35 'externallinks',
36 'imagelinks',
37 'templatelinks',
38 'iwlinks' ] );
39 }
40
41 protected function setUp() {
43
44 parent::setUp();
45
46 $wgExtraNamespaces[12312] = 'Dummy';
47 $wgExtraNamespaces[12313] = 'Dummy_talk';
48
49 $wgNamespaceContentModels[12312] = 'DUMMY';
50 $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
51
52 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
53 $wgContLang->resetNamespaces(); # reset namespace cache
54 if ( !$this->the_page ) {
55 $this->the_page = $this->createPage(
56 'RevisionStorageTest_the_page',
57 "just a dummy page",
59 );
60 }
61
62 $this->tablesUsed[] = 'archive';
63 }
64
65 protected function tearDown() {
67
68 parent::tearDown();
69
70 unset( $wgExtraNamespaces[12312] );
71 unset( $wgExtraNamespaces[12313] );
72
73 unset( $wgNamespaceContentModels[12312] );
74 unset( $wgContentHandlers['DUMMY'] );
75
76 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
77 $wgContLang->resetNamespaces(); # reset namespace cache
78 }
79
80 protected function makeRevision( $props = null ) {
81 if ( $props === null ) {
82 $props = [];
83 }
84
85 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
86 $props['text'] = 'Lorem Ipsum';
87 }
88
89 if ( !isset( $props['comment'] ) ) {
90 $props['comment'] = 'just a test';
91 }
92
93 if ( !isset( $props['page'] ) ) {
94 $props['page'] = $this->the_page->getId();
95 }
96
97 $rev = new Revision( $props );
98
99 $dbw = wfGetDB( DB_MASTER );
100 $rev->insertOn( $dbw );
101
102 return $rev;
103 }
104
105 protected function createPage( $page, $text, $model = null ) {
106 if ( is_string( $page ) ) {
107 if ( !preg_match( '/:/', $page ) &&
108 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
109 ) {
110 $ns = $this->getDefaultWikitextNS();
111 $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
112 }
113
114 $page = Title::newFromText( $page );
115 }
116
117 if ( $page instanceof Title ) {
118 $page = new WikiPage( $page );
119 }
120
121 if ( $page->exists() ) {
122 $page->doDeleteArticle( "done" );
123 }
124
125 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
126 $page->doEditContent( $content, "testing", EDIT_NEW );
127
128 return $page;
129 }
130
131 protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
132 $this->assertNotNull( $rev, 'missing revision' );
133
134 $this->assertEquals( $orig->getId(), $rev->getId() );
135 $this->assertEquals( $orig->getPage(), $rev->getPage() );
136 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
137 $this->assertEquals( $orig->getUser(), $rev->getUser() );
138 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
139 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
140 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
141 }
142
146 public function testConstructFromRow() {
147 $orig = $this->makeRevision();
148
150 $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
151 $this->assertTrue( is_object( $res ), 'query failed' );
152
153 $row = $res->fetchObject();
154 $res->free();
155
156 $rev = new Revision( $row );
157
158 $this->assertRevEquals( $orig, $rev );
159 }
160
164 public function testNewFromRow() {
165 $orig = $this->makeRevision();
166
168 $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
169 $this->assertTrue( is_object( $res ), 'query failed' );
170
171 $row = $res->fetchObject();
172 $res->free();
173
174 $rev = Revision::newFromRow( $row );
175
176 $this->assertRevEquals( $orig, $rev );
177 }
178
182 public function testNewFromArchiveRow() {
183 $page = $this->createPage(
184 'RevisionStorageTest_testNewFromArchiveRow',
185 'Lorem Ipsum',
187 );
188 $orig = $page->getRevision();
189 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
190
192 $res = $dbr->select(
193 'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
194 );
195 $this->assertTrue( is_object( $res ), 'query failed' );
196
197 $row = $res->fetchObject();
198 $res->free();
199
201
202 $this->assertRevEquals( $orig, $rev );
203 }
204
208 public function testNewFromId() {
209 $orig = $this->makeRevision();
210
211 $rev = Revision::newFromId( $orig->getId() );
212
213 $this->assertRevEquals( $orig, $rev );
214 }
215
219 public function testFetchRevision() {
220 $page = $this->createPage(
221 'RevisionStorageTest_testFetchRevision',
222 'one',
224 );
225
226 // Hidden process cache assertion below
227 $page->getRevision()->getId();
228
229 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
230 $id = $page->getRevision()->getId();
231
232 $res = Revision::fetchRevision( $page->getTitle() );
233
234 # note: order is unspecified
235 $rows = [];
236 while ( ( $row = $res->fetchObject() ) ) {
237 $rows[$row->rev_id] = $row;
238 }
239
240 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
241 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
242 }
243
247 public function testSelectFields() {
249
250 $fields = Revision::selectFields();
251
252 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
253 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
254 $this->assertTrue(
255 in_array( 'rev_timestamp', $fields ),
256 'missing rev_timestamp in list of fields'
257 );
258 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
259
261 $this->assertTrue( in_array( 'rev_content_model', $fields ),
262 'missing rev_content_model in list of fields' );
263 $this->assertTrue( in_array( 'rev_content_format', $fields ),
264 'missing rev_content_format in list of fields' );
265 }
266 }
267
271 public function testGetPage() {
272 $page = $this->the_page;
273
274 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
275 $rev = Revision::newFromId( $orig->getId() );
276
277 $this->assertEquals( $page->getId(), $rev->getPage() );
278 }
279
283 public function testGetContent_failure() {
284 $rev = new Revision( [
285 'page' => $this->the_page->getId(),
286 'content_model' => $this->the_page->getContentModel(),
287 'text_id' => 123456789, // not in the test DB
288 ] );
289
290 $this->assertNull( $rev->getContent(),
291 "getContent() should return null if the revision's text blob could not be loaded." );
292
293 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
294 $this->assertNull( $rev->getContent(),
295 "getContent() should return null if the revision's text blob could not be loaded." );
296 }
297
301 public function testGetContent() {
302 $orig = $this->makeRevision( [ 'text' => 'hello hello.' ] );
303 $rev = Revision::newFromId( $orig->getId() );
304
305 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
306 }
307
311 public function testGetContentModel() {
313
314 if ( !$wgContentHandlerUseDB ) {
315 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
316 }
317
318 $orig = $this->makeRevision( [ 'text' => 'hello hello.',
319 'content_model' => CONTENT_MODEL_JAVASCRIPT ] );
320 $rev = Revision::newFromId( $orig->getId() );
321
322 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
323 }
324
328 public function testGetContentFormat() {
330
331 if ( !$wgContentHandlerUseDB ) {
332 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
333 }
334
335 $orig = $this->makeRevision( [
336 'text' => 'hello hello.',
337 'content_model' => CONTENT_MODEL_JAVASCRIPT,
338 'content_format' => CONTENT_FORMAT_JAVASCRIPT
339 ] );
340 $rev = Revision::newFromId( $orig->getId() );
341
342 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT, $rev->getContentFormat() );
343 }
344
348 public function testIsCurrent() {
349 $page = $this->createPage(
350 'RevisionStorageTest_testIsCurrent',
351 'Lorem Ipsum',
353 );
354 $rev1 = $page->getRevision();
355
356 # @todo find out if this should be true
357 # $this->assertTrue( $rev1->isCurrent() );
358
359 $rev1x = Revision::newFromId( $rev1->getId() );
360 $this->assertTrue( $rev1x->isCurrent() );
361
362 $page->doEditContent(
363 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
364 'second rev'
365 );
366 $rev2 = $page->getRevision();
367
368 # @todo find out if this should be true
369 # $this->assertTrue( $rev2->isCurrent() );
370
371 $rev1x = Revision::newFromId( $rev1->getId() );
372 $this->assertFalse( $rev1x->isCurrent() );
373
374 $rev2x = Revision::newFromId( $rev2->getId() );
375 $this->assertTrue( $rev2x->isCurrent() );
376 }
377
381 public function testGetPrevious() {
382 $page = $this->createPage(
383 'RevisionStorageTest_testGetPrevious',
384 'Lorem Ipsum testGetPrevious',
386 );
387 $rev1 = $page->getRevision();
388
389 $this->assertNull( $rev1->getPrevious() );
390
391 $page->doEditContent(
392 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
393 'second rev testGetPrevious' );
394 $rev2 = $page->getRevision();
395
396 $this->assertNotNull( $rev2->getPrevious() );
397 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
398 }
399
403 public function testGetNext() {
404 $page = $this->createPage(
405 'RevisionStorageTest_testGetNext',
406 'Lorem Ipsum testGetNext',
408 );
409 $rev1 = $page->getRevision();
410
411 $this->assertNull( $rev1->getNext() );
412
413 $page->doEditContent(
414 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
415 'second rev testGetNext'
416 );
417 $rev2 = $page->getRevision();
418
419 $this->assertNotNull( $rev1->getNext() );
420 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
421 }
422
426 public function testNewNullRevision() {
427 $page = $this->createPage(
428 'RevisionStorageTest_testNewNullRevision',
429 'some testing text',
431 );
432 $orig = $page->getRevision();
433
434 $dbw = wfGetDB( DB_MASTER );
435 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
436
437 $this->assertNotEquals( $orig->getId(), $rev->getId(),
438 'new null revision shold have a different id from the original revision' );
439 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
440 'new null revision shold have the same text id as the original revision' );
441 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
442 }
443
447 public function testInsertOn() {
448 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
449
450 $orig = $this->makeRevision( [
451 'user_text' => $ip
452 ] );
453
454 // Make sure the revision was copied to ip_changes
456 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
457 $row = $res->fetchObject();
458
459 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
460 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
461 }
462
463 public static function provideUserWasLastToEdit() {
464 return [
465 [ # 0
466 3, true, # actually the last edit
467 ],
468 [ # 1
469 2, true, # not the current edit, but still by this user
470 ],
471 [ # 2
472 1, false, # edit by another user
473 ],
474 [ # 3
475 0, false, # first edit, by this user, but another user edited in the mean time
476 ],
477 ];
478 }
479
483 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
484 $userA = User::newFromName( "RevisionStorageTest_userA" );
485 $userB = User::newFromName( "RevisionStorageTest_userB" );
486
487 if ( $userA->getId() === 0 ) {
488 $userA = User::createNew( $userA->getName() );
489 }
490
491 if ( $userB->getId() === 0 ) {
492 $userB = User::createNew( $userB->getName() );
493 }
494
495 $ns = $this->getDefaultWikitextNS();
496
497 $dbw = wfGetDB( DB_MASTER );
498 $revisions = [];
499
500 // create revisions -----------------------------
501 $page = WikiPage::factory( Title::newFromText(
502 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
503 $page->insertOn( $dbw );
504
505 # zero
506 $revisions[0] = new Revision( [
507 'page' => $page->getId(),
508 // we need the title to determine the page's default content model
509 'title' => $page->getTitle(),
510 'timestamp' => '20120101000000',
511 'user' => $userA->getId(),
512 'text' => 'zero',
513 'content_model' => CONTENT_MODEL_WIKITEXT,
514 'summary' => 'edit zero'
515 ] );
516 $revisions[0]->insertOn( $dbw );
517
518 # one
519 $revisions[1] = new Revision( [
520 'page' => $page->getId(),
521 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
522 'title' => $page->getTitle(),
523 'timestamp' => '20120101000100',
524 'user' => $userA->getId(),
525 'text' => 'one',
526 'content_model' => CONTENT_MODEL_WIKITEXT,
527 'summary' => 'edit one'
528 ] );
529 $revisions[1]->insertOn( $dbw );
530
531 # two
532 $revisions[2] = new Revision( [
533 'page' => $page->getId(),
534 'title' => $page->getTitle(),
535 'timestamp' => '20120101000200',
536 'user' => $userB->getId(),
537 'text' => 'two',
538 'content_model' => CONTENT_MODEL_WIKITEXT,
539 'summary' => 'edit two'
540 ] );
541 $revisions[2]->insertOn( $dbw );
542
543 # three
544 $revisions[3] = new Revision( [
545 'page' => $page->getId(),
546 'title' => $page->getTitle(),
547 'timestamp' => '20120101000300',
548 'user' => $userA->getId(),
549 'text' => 'three',
550 'content_model' => CONTENT_MODEL_WIKITEXT,
551 'summary' => 'edit three'
552 ] );
553 $revisions[3]->insertOn( $dbw );
554
555 # four
556 $revisions[4] = new Revision( [
557 'page' => $page->getId(),
558 'title' => $page->getTitle(),
559 'timestamp' => '20120101000200',
560 'user' => $userA->getId(),
561 'text' => 'zero',
562 'content_model' => CONTENT_MODEL_WIKITEXT,
563 'summary' => 'edit four'
564 ] );
565 $revisions[4]->insertOn( $dbw );
566
567 // test it ---------------------------------
568 $since = $revisions[$sinceIdx]->getTimestamp();
569
570 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
571
572 $this->assertEquals( $expectedLast, $wasLast );
573 }
574}
$wgExtraNamespaces
Additional namespaces.
$wgNamespaceContentModels
Associative array mapping namespace IDs to the name of the content model pages in that namespace shou...
$wgContentHandlerUseDB
Set to false to disable use of the database fields introduced by the ContentHandler facility.
$wgContentHandlers
Plugins for page content model handling.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Test class for Revision storage.
testGetPage()
Revision::getPage.
assertRevEquals(Revision $orig, Revision $rev=null)
testInsertOn()
Revision::insertOn.
testGetPrevious()
Revision::getPrevious.
testGetContent()
Revision::getContent.
testSelectFields()
Revision::selectFields.
testConstructFromRow()
Revision::__construct.
testIsCurrent()
Revision::isCurrent.
testNewFromId()
Revision::newFromId.
testFetchRevision()
Revision::fetchRevision.
testUserWasLastToEdit( $sinceIdx, $expectedLast)
provideUserWasLastToEdit
testGetContentFormat()
Revision::getContentFormat.
__construct( $name=null, array $data=[], $dataName='')
testGetContentModel()
Revision::getContentModel.
testNewFromRow()
Revision::newFromRow.
testGetContent_failure()
Revision::getContent.
testNewFromArchiveRow()
Revision::newFromArchiveRow.
createPage( $page, $text, $model=null)
testNewNullRevision()
Revision::newNullRevision.
testGetNext()
Revision::getNext.
static selectArchiveFields()
Return the list of revision fields that should be selected to create a new revision from an archive r...
Definition Revision.php:486
getId()
Get revision ID.
Definition Revision.php:743
getContentFormat()
Returns the content format for this revision.
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition Revision.php:452
getPage()
Get the page ID.
Definition Revision.php:860
static newFromRow( $row)
Definition Revision.php:238
static newNullRevision( $dbw, $pageId, $summary, $minor, $user=null)
Create a new null-revision for insertion into a page's history.
getContentModel()
Returns the content model for this revision.
getUser( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision's user id if it's available to the specified audience.
Definition Revision.php:877
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition Revision.php:189
getSha1()
Returns the base36 sha1 of the text in this revision, or null if unknown.
Definition Revision.php:806
static userWasLastToEdit( $db, $pageId, $userId, $since)
Check if no edits were made by other users since the time a user started editing the page.
static fetchRevision(LinkTarget $title)
Return a wrapper for a series of database rows to fetch all of a given page's revisions in turn.
Definition Revision.php:382
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition Revision.php:116
Represents a title within MediaWiki.
Definition Title.php:39
Class representing a MediaWiki article and history.
Definition WikiPage.php:37
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:121
Content object for wiki text pages.
if(! $regexes) $dbr
Definition cleanup.php:94
The ContentHandler facility adds support for arbitrary content types on wiki instead of relying on wikitext for everything It was introduced in MediaWiki Each kind of edited
$res
Definition database.txt:21
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
when a variable name is used in a function
Definition design.txt:94
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 and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2746
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:1760
const CONTENT_FORMAT_JAVASCRIPT
Definition Defines.php:253
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:236
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:237
const EDIT_NEW
Definition Defines.php:153
you have access to all of the normal MediaWiki so you can get a DB use the cache
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26