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