added unit tests for WikitextContent
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2
3 /**
4 * @todo document
5 */
6 class Revision {
7 protected $mId;
8 protected $mPage;
9 protected $mUserText;
10 protected $mOrigUserText;
11 protected $mUser;
12 protected $mMinorEdit;
13 protected $mTimestamp;
14 protected $mDeleted;
15 protected $mSize;
16 protected $mSha1;
17 protected $mParentId;
18 protected $mComment;
19 protected $mText;
20 protected $mTextRow;
21 protected $mTitle;
22 protected $mCurrent;
23 protected $mContentModelName;
24 protected $mContentFormat;
25 protected $mContent;
26 protected $mContentHandler;
27
28 const DELETED_TEXT = 1;
29 const DELETED_COMMENT = 2;
30 const DELETED_USER = 4;
31 const DELETED_RESTRICTED = 8;
32 // Convenience field
33 const SUPPRESSED_USER = 12;
34 // Audience options for Revision::getText()
35 const FOR_PUBLIC = 1;
36 const FOR_THIS_USER = 2;
37 const RAW = 3;
38
39 /**
40 * Load a page revision from a given revision ID number.
41 * Returns null if no such revision can be found.
42 *
43 * @param $id Integer
44 * @return Revision or null
45 */
46 public static function newFromId( $id ) {
47 return Revision::newFromConds( array( 'rev_id' => intval( $id ) ) );
48 }
49
50 /**
51 * Load either the current, or a specified, revision
52 * that's attached to a given title. If not attached
53 * to that title, will return null.
54 *
55 * @param $title Title
56 * @param $id Integer (optional)
57 * @return Revision or null
58 */
59 public static function newFromTitle( $title, $id = 0 ) {
60 $conds = array(
61 'page_namespace' => $title->getNamespace(),
62 'page_title' => $title->getDBkey()
63 );
64 if ( $id ) {
65 // Use the specified ID
66 $conds['rev_id'] = $id;
67 } elseif ( wfGetLB()->getServerCount() > 1 ) {
68 // Get the latest revision ID from the master
69 $dbw = wfGetDB( DB_MASTER );
70 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
71 if ( $latest === false ) {
72 return null; // page does not exist
73 }
74 $conds['rev_id'] = $latest;
75 } else {
76 // Use a join to get the latest revision
77 $conds[] = 'rev_id=page_latest';
78 }
79 return Revision::newFromConds( $conds );
80 }
81
82 /**
83 * Load either the current, or a specified, revision
84 * that's attached to a given page ID.
85 * Returns null if no such revision can be found.
86 *
87 * @param $revId Integer
88 * @param $pageId Integer (optional)
89 * @return Revision or null
90 */
91 public static function newFromPageId( $pageId, $revId = 0 ) {
92 $conds = array( 'page_id' => $pageId );
93 if ( $revId ) {
94 $conds['rev_id'] = $revId;
95 } elseif ( wfGetLB()->getServerCount() > 1 ) {
96 // Get the latest revision ID from the master
97 $dbw = wfGetDB( DB_MASTER );
98 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
99 if ( $latest === false ) {
100 return null; // page does not exist
101 }
102 $conds['rev_id'] = $latest;
103 } else {
104 $conds[] = 'rev_id = page_latest';
105 }
106 return Revision::newFromConds( $conds );
107 }
108
109 /**
110 * Make a fake revision object from an archive table row. This is queried
111 * for permissions or even inserted (as in Special:Undelete)
112 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
113 *
114 * @param $row
115 * @param $overrides array
116 *
117 * @return Revision
118 */
119 public static function newFromArchiveRow( $row, $overrides = array() ) {
120 $attribs = $overrides + array(
121 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
122 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
123 'comment' => $row->ar_comment,
124 'user' => $row->ar_user,
125 'user_text' => $row->ar_user_text,
126 'timestamp' => $row->ar_timestamp,
127 'minor_edit' => $row->ar_minor_edit,
128 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
129 'deleted' => $row->ar_deleted,
130 'len' => $row->ar_len,
131 'sha1' => isset( $row->ar_sha1 ) ? $row->ar_sha1 : null,
132 'content_model' => isset( $row->ar_content_model ) ? $row->ar_content_model : null,
133 'content_format' => isset( $row->ar_content_format ) ? $row->ar_content_format : null,
134 );
135 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
136 // Pre-1.5 ar_text row
137 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
138 if ( $attribs['text'] === false ) {
139 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
140 }
141 }
142 return new self( $attribs );
143 }
144
145 /**
146 * @since 1.19
147 *
148 * @param $row
149 * @return Revision
150 */
151 public static function newFromRow( $row ) {
152 return new self( $row );
153 }
154
155 /**
156 * Load a page revision from a given revision ID number.
157 * Returns null if no such revision can be found.
158 *
159 * @param $db DatabaseBase
160 * @param $id Integer
161 * @return Revision or null
162 */
163 public static function loadFromId( $db, $id ) {
164 return Revision::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
165 }
166
167 /**
168 * Load either the current, or a specified, revision
169 * that's attached to a given page. If not attached
170 * to that page, will return null.
171 *
172 * @param $db DatabaseBase
173 * @param $pageid Integer
174 * @param $id Integer
175 * @return Revision or null
176 */
177 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
178 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
179 if( $id ) {
180 $conds['rev_id'] = intval( $id );
181 } else {
182 $conds[] = 'rev_id=page_latest';
183 }
184 return Revision::loadFromConds( $db, $conds );
185 }
186
187 /**
188 * Load either the current, or a specified, revision
189 * that's attached to a given page. If not attached
190 * to that page, will return null.
191 *
192 * @param $db DatabaseBase
193 * @param $title Title
194 * @param $id Integer
195 * @return Revision or null
196 */
197 public static function loadFromTitle( $db, $title, $id = 0 ) {
198 if( $id ) {
199 $matchId = intval( $id );
200 } else {
201 $matchId = 'page_latest';
202 }
203 return Revision::loadFromConds( $db,
204 array( "rev_id=$matchId",
205 'page_namespace' => $title->getNamespace(),
206 'page_title' => $title->getDBkey() )
207 );
208 }
209
210 /**
211 * Load the revision for the given title with the given timestamp.
212 * WARNING: Timestamps may in some circumstances not be unique,
213 * so this isn't the best key to use.
214 *
215 * @param $db DatabaseBase
216 * @param $title Title
217 * @param $timestamp String
218 * @return Revision or null
219 */
220 public static function loadFromTimestamp( $db, $title, $timestamp ) {
221 return Revision::loadFromConds( $db,
222 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
223 'page_namespace' => $title->getNamespace(),
224 'page_title' => $title->getDBkey() )
225 );
226 }
227
228 /**
229 * Given a set of conditions, fetch a revision.
230 *
231 * @param $conditions Array
232 * @return Revision or null
233 */
234 public static function newFromConds( $conditions ) {
235 $db = wfGetDB( DB_SLAVE );
236 $rev = Revision::loadFromConds( $db, $conditions );
237 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
238 $dbw = wfGetDB( DB_MASTER );
239 $rev = Revision::loadFromConds( $dbw, $conditions );
240 }
241 return $rev;
242 }
243
244 /**
245 * Given a set of conditions, fetch a revision from
246 * the given database connection.
247 *
248 * @param $db DatabaseBase
249 * @param $conditions Array
250 * @return Revision or null
251 */
252 private static function loadFromConds( $db, $conditions ) {
253 $res = Revision::fetchFromConds( $db, $conditions );
254 if( $res ) {
255 $row = $res->fetchObject();
256 if( $row ) {
257 $ret = new Revision( $row );
258 return $ret;
259 }
260 }
261 $ret = null;
262 return $ret;
263 }
264
265 /**
266 * Return a wrapper for a series of database rows to
267 * fetch all of a given page's revisions in turn.
268 * Each row can be fed to the constructor to get objects.
269 *
270 * @param $title Title
271 * @return ResultWrapper
272 */
273 public static function fetchRevision( $title ) {
274 return Revision::fetchFromConds(
275 wfGetDB( DB_SLAVE ),
276 array( 'rev_id=page_latest',
277 'page_namespace' => $title->getNamespace(),
278 'page_title' => $title->getDBkey() )
279 );
280 }
281
282 /**
283 * Given a set of conditions, return a ResultWrapper
284 * which will return matching database rows with the
285 * fields necessary to build Revision objects.
286 *
287 * @param $db DatabaseBase
288 * @param $conditions Array
289 * @return ResultWrapper
290 */
291 private static function fetchFromConds( $db, $conditions ) {
292 $fields = array_merge(
293 self::selectFields(),
294 self::selectPageFields(),
295 self::selectUserFields()
296 );
297 return $db->select(
298 array( 'revision', 'page', 'user' ),
299 $fields,
300 $conditions,
301 __METHOD__,
302 array( 'LIMIT' => 1 ),
303 array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
304 );
305 }
306
307 /**
308 * Return the value of a select() JOIN conds array for the user table.
309 * This will get user table rows for logged-in users.
310 * @since 1.19
311 * @return Array
312 */
313 public static function userJoinCond() {
314 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
315 }
316
317 /**
318 * Return the value of a select() page conds array for the paeg table.
319 * This will assure that the revision(s) are not orphaned from live pages.
320 * @since 1.19
321 * @return Array
322 */
323 public static function pageJoinCond() {
324 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
325 }
326
327 /**
328 * Return the list of revision fields that should be selected to create
329 * a new revision.
330 * @return array
331 */
332 public static function selectFields() {
333 return array(
334 'rev_id',
335 'rev_page',
336 'rev_text_id',
337 'rev_timestamp',
338 'rev_comment',
339 'rev_user_text',
340 'rev_user',
341 'rev_minor_edit',
342 'rev_deleted',
343 'rev_len',
344 'rev_parent_id',
345 'rev_sha1',
346 'rev_content_format',
347 'rev_content_model'
348 );
349 }
350
351 /**
352 * Return the list of text fields that should be selected to read the
353 * revision text
354 * @return array
355 */
356 public static function selectTextFields() {
357 return array(
358 'old_text',
359 'old_flags'
360 );
361 }
362
363 /**
364 * Return the list of page fields that should be selected from page table
365 * @return array
366 */
367 public static function selectPageFields() {
368 return array(
369 'page_namespace',
370 'page_title',
371 'page_id',
372 'page_latest'
373 );
374 }
375
376 /**
377 * Return the list of user fields that should be selected from user table
378 * @return array
379 */
380 public static function selectUserFields() {
381 return array( 'user_name' );
382 }
383
384 /**
385 * Constructor
386 *
387 * @param $row Mixed: either a database row or an array
388 * @access private
389 */
390 function __construct( $row ) {
391 if( is_object( $row ) ) {
392 $this->mId = intval( $row->rev_id );
393 $this->mPage = intval( $row->rev_page );
394 $this->mTextId = intval( $row->rev_text_id );
395 $this->mComment = $row->rev_comment;
396 $this->mUser = intval( $row->rev_user );
397 $this->mMinorEdit = intval( $row->rev_minor_edit );
398 $this->mTimestamp = $row->rev_timestamp;
399 $this->mDeleted = intval( $row->rev_deleted );
400
401 if( !isset( $row->rev_parent_id ) ) {
402 $this->mParentId = is_null( $row->rev_parent_id ) ? null : 0;
403 } else {
404 $this->mParentId = intval( $row->rev_parent_id );
405 }
406
407 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) ) {
408 $this->mSize = null;
409 } else {
410 $this->mSize = intval( $row->rev_len );
411 }
412
413 if ( !isset( $row->rev_sha1 ) ) {
414 $this->mSha1 = null;
415 } else {
416 $this->mSha1 = $row->rev_sha1;
417 }
418
419 if( isset( $row->page_latest ) ) {
420 $this->mCurrent = ( $row->rev_id == $row->page_latest );
421 $this->mTitle = Title::newFromRow( $row );
422 } else {
423 $this->mCurrent = false;
424 $this->mTitle = null;
425 }
426
427 if( !isset( $row->rev_content_model ) || is_null( $row->rev_content_model ) ) {
428 $this->mContentModelName = null; # determine on demand if needed
429 } else {
430 $this->mContentModelName = strval( $row->rev_content_model );
431 }
432
433 if( !isset( $row->rev_content_format ) || is_null( $row->rev_content_format ) ) {
434 $this->mContentFormat = null; # determine on demand if needed
435 } else {
436 $this->mContentFormat = strval( $row->rev_content_format );
437 }
438
439 // Lazy extraction...
440 $this->mText = null;
441 if( isset( $row->old_text ) ) {
442 $this->mTextRow = $row;
443 } else {
444 // 'text' table row entry will be lazy-loaded
445 $this->mTextRow = null;
446 }
447
448 // Use user_name for users and rev_user_text for IPs...
449 $this->mUserText = null; // lazy load if left null
450 if ( $this->mUser == 0 ) {
451 $this->mUserText = $row->rev_user_text; // IP user
452 } elseif ( isset( $row->user_name ) ) {
453 $this->mUserText = $row->user_name; // logged-in user
454 }
455 $this->mOrigUserText = $row->rev_user_text;
456 } elseif( is_array( $row ) ) {
457 // Build a new revision to be saved...
458 global $wgUser; // ugh
459
460
461 # if we have a content object, use it to set the model and type
462 if ( !empty( $row['content'] ) ) {
463 if ( !empty( $row['text_id'] ) ) { #FIXME: when is that set? test with external store setup! check out insertOn()
464 throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
465 }
466
467 $row['content_model'] = $row['content']->getModelName();
468 # note: mContentFormat is initializes later accordingly
469 # note: content is serialized later in this method!
470 # also set text to null?
471 }
472
473 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
474 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
475 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
476 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
477 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
478 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
479 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestampNow();
480 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
481 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
482 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
483 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
484
485 $this->mContentModelName = isset( $row['content_model'] ) ? strval( $row['content_model'] ) : null;
486 $this->mContentFormat = isset( $row['content_format'] ) ? strval( $row['content_format'] ) : null;
487
488 // Enforce spacing trimming on supplied text
489 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
490 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
491 $this->mTextRow = null;
492
493 # if we have a content object, override mText and mContentModelName
494 if ( !empty( $row['content'] ) ) {
495 $handler = $this->getContentHandler();
496 $this->mContent = $row['content'];
497
498 $this->mContentModelName = $this->mContent->getModelName();
499 $this->mContentHandler = null;
500
501 $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
502 }
503
504 $this->mTitle = null; # Load on demand if needed
505 $this->mCurrent = false;
506 # If we still have no length, see it we have the text to figure it out
507 if ( !$this->mSize ) {
508 #XXX: my be inconsistent with the notion of "size" use for the present content model
509 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
510 }
511 # Same for sha1
512 if ( $this->mSha1 === null ) {
513 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
514 }
515
516 $this->getContentModelName(); # force lazy init
517 $this->getContentFormat(); # force lazy init
518 } else {
519 throw new MWException( 'Revision constructor passed invalid row format.' );
520 }
521 $this->mUnpatrolled = null;
522
523 // @TODO: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
524 // @TODO: get rid of $mText
525 }
526
527 /**
528 * Get revision ID
529 *
530 * @return Integer
531 */
532 public function getId() {
533 return $this->mId;
534 }
535
536 /**
537 * Set the revision ID
538 *
539 * @since 1.19
540 * @param $id Integer
541 */
542 public function setId( $id ) {
543 $this->mId = $id;
544 }
545
546 /**
547 * Get text row ID
548 *
549 * @return Integer
550 */
551 public function getTextId() {
552 return $this->mTextId;
553 }
554
555 /**
556 * Get parent revision ID (the original previous page revision)
557 *
558 * @return Integer|null
559 */
560 public function getParentId() {
561 return $this->mParentId;
562 }
563
564 /**
565 * Returns the length of the text in this revision, or null if unknown.
566 *
567 * @return Integer
568 */
569 public function getSize() {
570 return $this->mSize;
571 }
572
573 /**
574 * Returns the base36 sha1 of the text in this revision, or null if unknown.
575 *
576 * @return String
577 */
578 public function getSha1() {
579 return $this->mSha1;
580 }
581
582 /**
583 * Returns the title of the page associated with this entry.
584 *
585 * @return Title
586 */
587 public function getTitle() {
588 if( isset( $this->mTitle ) ) {
589 return $this->mTitle;
590 }
591 $dbr = wfGetDB( DB_SLAVE );
592 $row = $dbr->selectRow(
593 array( 'page', 'revision' ),
594 self::selectPageFields(),
595 array( 'page_id=rev_page',
596 'rev_id' => $this->mId ),
597 __METHOD__ );
598 if ( $row ) {
599 $this->mTitle = Title::newFromRow( $row );
600 }
601 return $this->mTitle;
602 }
603
604 /**
605 * Set the title of the revision
606 *
607 * @param $title Title
608 */
609 public function setTitle( $title ) {
610 $this->mTitle = $title;
611 }
612
613 /**
614 * Get the page ID
615 *
616 * @return Integer
617 */
618 public function getPage() {
619 return $this->mPage;
620 }
621
622 /**
623 * Fetch revision's user id if it's available to the specified audience.
624 * If the specified audience does not have access to it, zero will be
625 * returned.
626 *
627 * @param $audience Integer: one of:
628 * Revision::FOR_PUBLIC to be displayed to all users
629 * Revision::FOR_THIS_USER to be displayed to $wgUser
630 * Revision::RAW get the ID regardless of permissions
631 * @param $user User object to check for, only if FOR_THIS_USER is passed
632 * to the $audience parameter
633 * @return Integer
634 */
635 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
636 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
637 return 0;
638 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
639 return 0;
640 } else {
641 return $this->mUser;
642 }
643 }
644
645 /**
646 * Fetch revision's user id without regard for the current user's permissions
647 *
648 * @return String
649 */
650 public function getRawUser() {
651 return $this->mUser;
652 }
653
654 /**
655 * Fetch revision's username if it's available to the specified audience.
656 * If the specified audience does not have access to the username, an
657 * empty string will be returned.
658 *
659 * @param $audience Integer: one of:
660 * Revision::FOR_PUBLIC to be displayed to all users
661 * Revision::FOR_THIS_USER to be displayed to $wgUser
662 * Revision::RAW get the text regardless of permissions
663 * @param $user User object to check for, only if FOR_THIS_USER is passed
664 * to the $audience parameter
665 * @return string
666 */
667 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
668 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
669 return '';
670 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
671 return '';
672 } else {
673 return $this->getRawUserText();
674 }
675 }
676
677 /**
678 * Fetch revision's username without regard for view restrictions
679 *
680 * @return String
681 */
682 public function getRawUserText() {
683 if ( $this->mUserText === null ) {
684 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
685 if ( $this->mUserText === false ) {
686 # This shouldn't happen, but it can if the wiki was recovered
687 # via importing revs and there is no user table entry yet.
688 $this->mUserText = $this->mOrigUserText;
689 }
690 }
691 return $this->mUserText;
692 }
693
694 /**
695 * Fetch revision comment if it's available to the specified audience.
696 * If the specified audience does not have access to the comment, an
697 * empty string will be returned.
698 *
699 * @param $audience Integer: one of:
700 * Revision::FOR_PUBLIC to be displayed to all users
701 * Revision::FOR_THIS_USER to be displayed to $wgUser
702 * Revision::RAW get the text regardless of permissions
703 * @param $user User object to check for, only if FOR_THIS_USER is passed
704 * to the $audience parameter
705 * @return String
706 */
707 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
708 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
709 return '';
710 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
711 return '';
712 } else {
713 return $this->mComment;
714 }
715 }
716
717 /**
718 * Fetch revision comment without regard for the current user's permissions
719 *
720 * @return String
721 */
722 public function getRawComment() {
723 return $this->mComment;
724 }
725
726 /**
727 * @return Boolean
728 */
729 public function isMinor() {
730 return (bool)$this->mMinorEdit;
731 }
732
733 /**
734 * @return Integer rcid of the unpatrolled row, zero if there isn't one
735 */
736 public function isUnpatrolled() {
737 if( $this->mUnpatrolled !== null ) {
738 return $this->mUnpatrolled;
739 }
740 $dbr = wfGetDB( DB_SLAVE );
741 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
742 'rc_id',
743 array( // Add redundant user,timestamp condition so we can use the existing index
744 'rc_user_text' => $this->getRawUserText(),
745 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
746 'rc_this_oldid' => $this->getId(),
747 'rc_patrolled' => 0
748 ),
749 __METHOD__
750 );
751 return (int)$this->mUnpatrolled;
752 }
753
754 /**
755 * @param $field int one of DELETED_* bitfield constants
756 *
757 * @return Boolean
758 */
759 public function isDeleted( $field ) {
760 return ( $this->mDeleted & $field ) == $field;
761 }
762
763 /**
764 * Get the deletion bitfield of the revision
765 *
766 * @return int
767 */
768 public function getVisibility() {
769 return (int)$this->mDeleted;
770 }
771
772 /**
773 * Fetch revision text if it's available to the specified audience.
774 * If the specified audience does not have the ability to view this
775 * revision, an empty string will be returned.
776 *
777 * @param $audience Integer: one of:
778 * Revision::FOR_PUBLIC to be displayed to all users
779 * Revision::FOR_THIS_USER to be displayed to $wgUser
780 * Revision::RAW get the text regardless of permissions
781 * @param $user User object to check for, only if FOR_THIS_USER is passed
782 * to the $audience parameter
783 * @return String
784 * @deprectaed in 1.20, use getContent() instead
785 */
786 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) { #FIXME: deprecated, replace usage! #FIXME: used a LOT!
787 wfDeprecated( __METHOD__, '1.20' );
788
789 $content = $this->getContent();
790 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
791 }
792
793 /**
794 * Fetch revision content if it's available to the specified audience.
795 * If the specified audience does not have the ability to view this
796 * revision, null will be returned.
797 *
798 * @param $audience Integer: one of:
799 * Revision::FOR_PUBLIC to be displayed to all users
800 * Revision::FOR_THIS_USER to be displayed to $wgUser
801 * Revision::RAW get the text regardless of permissions
802 * @param $user User object to check for, only if FOR_THIS_USER is passed
803 * to the $audience parameter
804 * @return Content
805 */
806 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
807 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
808 return null;
809 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
810 return null;
811 } else {
812 return $this->getContentInternal();
813 }
814 }
815
816 /**
817 * Alias for getText(Revision::FOR_THIS_USER)
818 *
819 * @deprecated since 1.17
820 * @return String
821 */
822 public function revText() {
823 wfDeprecated( __METHOD__, '1.17' );
824 return $this->getText( self::FOR_THIS_USER );
825 }
826
827 /**
828 * Fetch revision text without regard for view restrictions
829 *
830 * @return String
831 */
832 public function getRawText() { #FIXME: deprecated, replace usage!
833 return $this->getText( self::RAW );
834 }
835
836 protected function getContentInternal() {
837 if( is_null( $this->mContent ) ) {
838 // Revision is immutable. Load on demand:
839
840 $handler = $this->getContentHandler();
841 $format = $this->getContentFormat();
842 $title = $this->getTitle();
843
844 if( is_null( $this->mText ) ) {
845 // Load text on demand:
846 $this->mText = $this->loadText();
847 }
848
849 $this->mContent = is_null( $this->mText ) ? null : $handler->unserializeContent( $this->mText, $format );
850 }
851
852 return $this->mContent;
853 }
854
855 public function getContentModelName() {
856 if ( !$this->mContentModelName ) {
857 $title = $this->getTitle();
858 $this->mContentModelName = ( $title ? $title->getContentModelName() : CONTENT_MODEL_WIKITEXT );
859 }
860
861 return $this->mContentModelName;
862 }
863
864 public function getContentFormat() {
865 if ( !$this->mContentFormat ) {
866 $handler = $this->getContentHandler();
867 $this->mContentFormat = $handler->getDefaultFormat();
868 }
869
870 return $this->mContentFormat;
871 }
872
873 /**
874 * @return ContentHandlert
875 */
876 public function getContentHandler() {
877 if ( !$this->mContentHandler ) {
878 $model = $this->getContentModelName();
879 $this->mContentHandler = ContentHandler::getForModelName( $model );
880
881 assert( $this->mContentHandler->isSupportedFormat( $this->getContentFormat() ) );
882 }
883
884 return $this->mContentHandler;
885 }
886
887 /**
888 * @return String
889 */
890 public function getTimestamp() {
891 return wfTimestamp( TS_MW, $this->mTimestamp );
892 }
893
894 /**
895 * @return Boolean
896 */
897 public function isCurrent() {
898 return $this->mCurrent;
899 }
900
901 /**
902 * Get previous revision for this title
903 *
904 * @return Revision or null
905 */
906 public function getPrevious() {
907 if( $this->getTitle() ) {
908 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
909 if( $prev ) {
910 return Revision::newFromTitle( $this->getTitle(), $prev );
911 }
912 }
913 return null;
914 }
915
916 /**
917 * Get next revision for this title
918 *
919 * @return Revision or null
920 */
921 public function getNext() {
922 if( $this->getTitle() ) {
923 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
924 if ( $next ) {
925 return Revision::newFromTitle( $this->getTitle(), $next );
926 }
927 }
928 return null;
929 }
930
931 /**
932 * Get previous revision Id for this page_id
933 * This is used to populate rev_parent_id on save
934 *
935 * @param $db DatabaseBase
936 * @return Integer
937 */
938 private function getPreviousRevisionId( $db ) {
939 if( is_null( $this->mPage ) ) {
940 return 0;
941 }
942 # Use page_latest if ID is not given
943 if( !$this->mId ) {
944 $prevId = $db->selectField( 'page', 'page_latest',
945 array( 'page_id' => $this->mPage ),
946 __METHOD__ );
947 } else {
948 $prevId = $db->selectField( 'revision', 'rev_id',
949 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
950 __METHOD__,
951 array( 'ORDER BY' => 'rev_id DESC' ) );
952 }
953 return intval( $prevId );
954 }
955
956 /**
957 * Get revision text associated with an old or archive row
958 * $row is usually an object from wfFetchRow(), both the flags and the text
959 * field must be included
960 *
961 * @param $row Object: the text data
962 * @param $prefix String: table prefix (default 'old_')
963 * @return String: text the text requested or false on failure
964 */
965 public static function getRevisionText( $row, $prefix = 'old_' ) {
966 wfProfileIn( __METHOD__ );
967
968 # Get data
969 $textField = $prefix . 'text';
970 $flagsField = $prefix . 'flags';
971
972 if( isset( $row->$flagsField ) ) {
973 $flags = explode( ',', $row->$flagsField );
974 } else {
975 $flags = array();
976 }
977
978 if( isset( $row->$textField ) ) {
979 $text = $row->$textField;
980 } else {
981 wfProfileOut( __METHOD__ );
982 return false;
983 }
984
985 # Use external methods for external objects, text in table is URL-only then
986 if ( in_array( 'external', $flags ) ) {
987 $url = $text;
988 $parts = explode( '://', $url, 2 );
989 if( count( $parts ) == 1 || $parts[1] == '' ) {
990 wfProfileOut( __METHOD__ );
991 return false;
992 }
993 $text = ExternalStore::fetchFromURL( $url );
994 }
995
996 // If the text was fetched without an error, convert it
997 if ( $text !== false ) {
998 if( in_array( 'gzip', $flags ) ) {
999 # Deal with optional compression of archived pages.
1000 # This can be done periodically via maintenance/compressOld.php, and
1001 # as pages are saved if $wgCompressRevisions is set.
1002 $text = gzinflate( $text );
1003 }
1004
1005 if( in_array( 'object', $flags ) ) {
1006 # Generic compressed storage
1007 $obj = unserialize( $text );
1008 if ( !is_object( $obj ) ) {
1009 // Invalid object
1010 wfProfileOut( __METHOD__ );
1011 return false;
1012 }
1013 $text = $obj->getText();
1014 }
1015
1016 global $wgLegacyEncoding;
1017 if( $text !== false && $wgLegacyEncoding
1018 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1019 {
1020 # Old revisions kept around in a legacy encoding?
1021 # Upconvert on demand.
1022 # ("utf8" checked for compatibility with some broken
1023 # conversion scripts 2008-12-30)
1024 global $wgContLang;
1025 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1026 }
1027 }
1028 wfProfileOut( __METHOD__ );
1029 return $text;
1030 }
1031
1032 /**
1033 * If $wgCompressRevisions is enabled, we will compress data.
1034 * The input string is modified in place.
1035 * Return value is the flags field: contains 'gzip' if the
1036 * data is compressed, and 'utf-8' if we're saving in UTF-8
1037 * mode.
1038 *
1039 * @param $text Mixed: reference to a text
1040 * @return String
1041 */
1042 public static function compressRevisionText( &$text ) {
1043 global $wgCompressRevisions;
1044 $flags = array();
1045
1046 # Revisions not marked this way will be converted
1047 # on load if $wgLegacyCharset is set in the future.
1048 $flags[] = 'utf-8';
1049
1050 if( $wgCompressRevisions ) {
1051 if( function_exists( 'gzdeflate' ) ) {
1052 $text = gzdeflate( $text );
1053 $flags[] = 'gzip';
1054 } else {
1055 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
1056 }
1057 }
1058 return implode( ',', $flags );
1059 }
1060
1061 /**
1062 * Insert a new revision into the database, returning the new revision ID
1063 * number on success and dies horribly on failure.
1064 *
1065 * @param $dbw DatabaseBase: (master connection)
1066 * @return Integer
1067 */
1068 public function insertOn( $dbw ) {
1069 global $wgDefaultExternalStore;
1070
1071 wfProfileIn( __METHOD__ );
1072
1073 $data = $this->mText;
1074 $flags = Revision::compressRevisionText( $data );
1075
1076 # Write to external storage if required
1077 if( $wgDefaultExternalStore ) {
1078 // Store and get the URL
1079 $data = ExternalStore::insertToDefault( $data );
1080 if( !$data ) {
1081 throw new MWException( "Unable to store text to external storage" );
1082 }
1083 if( $flags ) {
1084 $flags .= ',';
1085 }
1086 $flags .= 'external';
1087 }
1088
1089 # Record the text (or external storage URL) to the text table
1090 if( !isset( $this->mTextId ) ) {
1091 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1092 $dbw->insert( 'text',
1093 array(
1094 'old_id' => $old_id,
1095 'old_text' => $data,
1096 'old_flags' => $flags,
1097 ), __METHOD__
1098 );
1099 $this->mTextId = $dbw->insertId();
1100 }
1101
1102 if ( $this->mComment === null ) $this->mComment = "";
1103
1104 # Record the edit in revisions
1105 $rev_id = isset( $this->mId )
1106 ? $this->mId
1107 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1108
1109 $row = array(
1110 'rev_id' => $rev_id,
1111 'rev_page' => $this->mPage,
1112 'rev_text_id' => $this->mTextId,
1113 'rev_comment' => $this->mComment,
1114 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1115 'rev_user' => $this->mUser,
1116 'rev_user_text' => $this->mUserText,
1117 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1118 'rev_deleted' => $this->mDeleted,
1119 'rev_len' => $this->mSize,
1120 'rev_parent_id' => is_null( $this->mParentId )
1121 ? $this->getPreviousRevisionId( $dbw )
1122 : $this->mParentId,
1123 'rev_sha1' => is_null( $this->mSha1 )
1124 ? Revision::base36Sha1( $this->mText )
1125 : $this->mSha1,
1126 'rev_content_model' => $this->getContentModelName(),
1127 'rev_content_format' => $this->getContentFormat(),
1128 );
1129
1130 $dbw->insert( 'revision', $row, __METHOD__ );
1131
1132 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1133
1134 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1135
1136 wfProfileOut( __METHOD__ );
1137 return $this->mId;
1138 }
1139
1140 /**
1141 * Get the base 36 SHA-1 value for a string of text
1142 * @param $text String
1143 * @return String
1144 */
1145 public static function base36Sha1( $text ) {
1146 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1147 }
1148
1149 /**
1150 * Lazy-load the revision's text.
1151 * Currently hardcoded to the 'text' table storage engine.
1152 *
1153 * @return String
1154 */
1155 protected function loadText() {
1156 wfProfileIn( __METHOD__ );
1157
1158 // Caching may be beneficial for massive use of external storage
1159 global $wgRevisionCacheExpiry, $wgMemc;
1160 $textId = $this->getTextId();
1161 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1162 if( $wgRevisionCacheExpiry ) {
1163 $text = $wgMemc->get( $key );
1164 if( is_string( $text ) ) {
1165 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1166 wfProfileOut( __METHOD__ );
1167 return $text;
1168 }
1169 }
1170
1171 // If we kept data for lazy extraction, use it now...
1172 if ( isset( $this->mTextRow ) ) {
1173 $row = $this->mTextRow;
1174 $this->mTextRow = null;
1175 } else {
1176 $row = null;
1177 }
1178
1179 if( !$row ) {
1180 // Text data is immutable; check slaves first.
1181 $dbr = wfGetDB( DB_SLAVE );
1182 $row = $dbr->selectRow( 'text',
1183 array( 'old_text', 'old_flags' ),
1184 array( 'old_id' => $this->getTextId() ),
1185 __METHOD__ );
1186 }
1187
1188 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1189 // Possible slave lag!
1190 $dbw = wfGetDB( DB_MASTER );
1191 $row = $dbw->selectRow( 'text',
1192 array( 'old_text', 'old_flags' ),
1193 array( 'old_id' => $this->getTextId() ),
1194 __METHOD__ );
1195 }
1196
1197 $text = self::getRevisionText( $row );
1198
1199 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1200 if( $wgRevisionCacheExpiry && $text !== false ) {
1201 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1202 }
1203
1204 wfProfileOut( __METHOD__ );
1205
1206 return $text;
1207 }
1208
1209 /**
1210 * Create a new null-revision for insertion into a page's
1211 * history. This will not re-save the text, but simply refer
1212 * to the text from the previous version.
1213 *
1214 * Such revisions can for instance identify page rename
1215 * operations and other such meta-modifications.
1216 *
1217 * @param $dbw DatabaseBase
1218 * @param $pageId Integer: ID number of the page to read from
1219 * @param $summary String: revision's summary
1220 * @param $minor Boolean: whether the revision should be considered as minor
1221 * @return Revision|null on error
1222 */
1223 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1224 wfProfileIn( __METHOD__ );
1225
1226 $current = $dbw->selectRow(
1227 array( 'page', 'revision' ),
1228 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1',
1229 'rev_content_model', 'rev_content_format' ),
1230 array(
1231 'page_id' => $pageId,
1232 'page_latest=rev_id',
1233 ),
1234 __METHOD__ );
1235
1236 if( $current ) {
1237 $revision = new Revision( array(
1238 'page' => $pageId,
1239 'comment' => $summary,
1240 'minor_edit' => $minor,
1241 'text_id' => $current->rev_text_id,
1242 'parent_id' => $current->page_latest,
1243 'len' => $current->rev_len,
1244 'sha1' => $current->rev_sha1,
1245 'content_model' => $current->rev_content_model,
1246 'content_format' => $current->rev_content_format
1247 ) );
1248 } else {
1249 $revision = null;
1250 }
1251
1252 wfProfileOut( __METHOD__ );
1253 return $revision;
1254 }
1255
1256 /**
1257 * Determine if the current user is allowed to view a particular
1258 * field of this revision, if it's marked as deleted.
1259 *
1260 * @param $field Integer:one of self::DELETED_TEXT,
1261 * self::DELETED_COMMENT,
1262 * self::DELETED_USER
1263 * @param $user User object to check, or null to use $wgUser
1264 * @return Boolean
1265 */
1266 public function userCan( $field, User $user = null ) {
1267 return self::userCanBitfield( $this->mDeleted, $field, $user );
1268 }
1269
1270 /**
1271 * Determine if the current user is allowed to view a particular
1272 * field of this revision, if it's marked as deleted. This is used
1273 * by various classes to avoid duplication.
1274 *
1275 * @param $bitfield Integer: current field
1276 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1277 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1278 * self::DELETED_USER = File::DELETED_USER
1279 * @param $user User object to check, or null to use $wgUser
1280 * @return Boolean
1281 */
1282 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1283 if( $bitfield & $field ) { // aspect is deleted
1284 if ( $bitfield & self::DELETED_RESTRICTED ) {
1285 $permission = 'suppressrevision';
1286 } elseif ( $field & self::DELETED_TEXT ) {
1287 $permission = 'deletedtext';
1288 } else {
1289 $permission = 'deletedhistory';
1290 }
1291 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1292 if ( $user === null ) {
1293 global $wgUser;
1294 $user = $wgUser;
1295 }
1296 return $user->isAllowed( $permission );
1297 } else {
1298 return true;
1299 }
1300 }
1301
1302 /**
1303 * Get rev_timestamp from rev_id, without loading the rest of the row
1304 *
1305 * @param $title Title
1306 * @param $id Integer
1307 * @return String
1308 */
1309 static function getTimestampFromId( $title, $id ) {
1310 $dbr = wfGetDB( DB_SLAVE );
1311 // Casting fix for DB2
1312 if ( $id == '' ) {
1313 $id = 0;
1314 }
1315 $conds = array( 'rev_id' => $id );
1316 $conds['rev_page'] = $title->getArticleID();
1317 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1318 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1319 # Not in slave, try master
1320 $dbw = wfGetDB( DB_MASTER );
1321 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1322 }
1323 return wfTimestamp( TS_MW, $timestamp );
1324 }
1325
1326 /**
1327 * Get count of revisions per page...not very efficient
1328 *
1329 * @param $db DatabaseBase
1330 * @param $id Integer: page id
1331 * @return Integer
1332 */
1333 static function countByPageId( $db, $id ) {
1334 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1335 array( 'rev_page' => $id ), __METHOD__ );
1336 if( $row ) {
1337 return $row->revCount;
1338 }
1339 return 0;
1340 }
1341
1342 /**
1343 * Get count of revisions per page...not very efficient
1344 *
1345 * @param $db DatabaseBase
1346 * @param $title Title
1347 * @return Integer
1348 */
1349 static function countByTitle( $db, $title ) {
1350 $id = $title->getArticleID();
1351 if( $id ) {
1352 return Revision::countByPageId( $db, $id );
1353 }
1354 return 0;
1355 }
1356 }