Select fields returned by Revision::selectPageFields() and call Title::newFromRow...
[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_id',
361 'page_latest'
362 );
363 }
364
365 /**
366 * Return the list of user fields that should be selected from user table
367 */
368 public static function selectUserFields() {
369 return array( 'user_name' );
370 }
371
372 /**
373 * Constructor
374 *
375 * @param $row Mixed: either a database row or an array
376 * @access private
377 */
378 function __construct( $row ) {
379 if( is_object( $row ) ) {
380 $this->mId = intval( $row->rev_id );
381 $this->mPage = intval( $row->rev_page );
382 $this->mTextId = intval( $row->rev_text_id );
383 $this->mComment = $row->rev_comment;
384 $this->mUser = intval( $row->rev_user );
385 $this->mMinorEdit = intval( $row->rev_minor_edit );
386 $this->mTimestamp = $row->rev_timestamp;
387 $this->mDeleted = intval( $row->rev_deleted );
388
389 if( !isset( $row->rev_parent_id ) ) {
390 $this->mParentId = is_null( $row->rev_parent_id ) ? null : 0;
391 } else {
392 $this->mParentId = intval( $row->rev_parent_id );
393 }
394
395 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) ) {
396 $this->mSize = null;
397 } else {
398 $this->mSize = intval( $row->rev_len );
399 }
400
401 if ( !isset( $row->rev_sha1 ) ) {
402 $this->mSha1 = null;
403 } else {
404 $this->mSha1 = $row->rev_sha1;
405 }
406
407 if( isset( $row->page_latest ) ) {
408 $this->mCurrent = ( $row->rev_id == $row->page_latest );
409 $this->mTitle = Title::newFromRow( $row );
410 } else {
411 $this->mCurrent = false;
412 $this->mTitle = null;
413 }
414
415 // Lazy extraction...
416 $this->mText = null;
417 if( isset( $row->old_text ) ) {
418 $this->mTextRow = $row;
419 } else {
420 // 'text' table row entry will be lazy-loaded
421 $this->mTextRow = null;
422 }
423
424 // Use user_name for users and rev_user_text for IPs...
425 $this->mUserText = null; // lazy load if left null
426 if ( $this->mUser == 0 ) {
427 $this->mUserText = $row->rev_user_text; // IP user
428 } elseif ( isset( $row->user_name ) ) {
429 $this->mUserText = $row->user_name; // logged-in user
430 }
431 $this->mOrigUserText = $row->rev_user_text;
432 } elseif( is_array( $row ) ) {
433 // Build a new revision to be saved...
434 global $wgUser; // ugh
435
436 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
437 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
438 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
439 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
440 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
441 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
442 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestampNow();
443 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
444 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
445 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
446 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
447
448 // Enforce spacing trimming on supplied text
449 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
450 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
451 $this->mTextRow = null;
452
453 $this->mTitle = null; # Load on demand if needed
454 $this->mCurrent = false;
455 # If we still have no length, see it we have the text to figure it out
456 if ( !$this->mSize ) {
457 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
458 }
459 # Same for sha1
460 if ( $this->mSha1 === null ) {
461 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
462 }
463 } else {
464 throw new MWException( 'Revision constructor passed invalid row format.' );
465 }
466 $this->mUnpatrolled = null;
467 }
468
469 /**
470 * Get revision ID
471 *
472 * @return Integer
473 */
474 public function getId() {
475 return $this->mId;
476 }
477
478 /**
479 * Set the revision ID
480 *
481 * @since 1.19
482 * @param $id Integer
483 */
484 public function setId( $id ) {
485 $this->mId = $id;
486 }
487
488 /**
489 * Get text row ID
490 *
491 * @return Integer
492 */
493 public function getTextId() {
494 return $this->mTextId;
495 }
496
497 /**
498 * Get parent revision ID (the original previous page revision)
499 *
500 * @return Integer
501 */
502 public function getParentId() {
503 return $this->mParentId;
504 }
505
506 /**
507 * Returns the length of the text in this revision, or null if unknown.
508 *
509 * @return Integer
510 */
511 public function getSize() {
512 return $this->mSize;
513 }
514
515 /**
516 * Returns the base36 sha1 of the text in this revision, or null if unknown.
517 *
518 * @return String
519 */
520 public function getSha1() {
521 return $this->mSha1;
522 }
523
524 /**
525 * Returns the title of the page associated with this entry.
526 *
527 * @return Title
528 */
529 public function getTitle() {
530 if( isset( $this->mTitle ) ) {
531 return $this->mTitle;
532 }
533 $dbr = wfGetDB( DB_SLAVE );
534 $row = $dbr->selectRow(
535 array( 'page', 'revision' ),
536 self::selectPageFields(),
537 array( 'page_id=rev_page',
538 'rev_id' => $this->mId ),
539 __METHOD__ );
540 if ( $row ) {
541 $this->mTitle = Title::newFromRow( $row );
542 }
543 return $this->mTitle;
544 }
545
546 /**
547 * Set the title of the revision
548 *
549 * @param $title Title
550 */
551 public function setTitle( $title ) {
552 $this->mTitle = $title;
553 }
554
555 /**
556 * Get the page ID
557 *
558 * @return Integer
559 */
560 public function getPage() {
561 return $this->mPage;
562 }
563
564 /**
565 * Fetch revision's user id if it's available to the specified audience.
566 * If the specified audience does not have access to it, zero will be
567 * returned.
568 *
569 * @param $audience Integer: one of:
570 * Revision::FOR_PUBLIC to be displayed to all users
571 * Revision::FOR_THIS_USER to be displayed to $wgUser
572 * Revision::RAW get the ID regardless of permissions
573 * @param $user User object to check for, only if FOR_THIS_USER is passed
574 * to the $audience parameter
575 * @return Integer
576 */
577 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
578 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
579 return 0;
580 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
581 return 0;
582 } else {
583 return $this->mUser;
584 }
585 }
586
587 /**
588 * Fetch revision's user id without regard for the current user's permissions
589 *
590 * @return String
591 */
592 public function getRawUser() {
593 return $this->mUser;
594 }
595
596 /**
597 * Fetch revision's username if it's available to the specified audience.
598 * If the specified audience does not have access to the username, an
599 * empty string will be returned.
600 *
601 * @param $audience Integer: one of:
602 * Revision::FOR_PUBLIC to be displayed to all users
603 * Revision::FOR_THIS_USER to be displayed to $wgUser
604 * Revision::RAW get the text regardless of permissions
605 * @param $user User object to check for, only if FOR_THIS_USER is passed
606 * to the $audience parameter
607 * @return string
608 */
609 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
610 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
611 return '';
612 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
613 return '';
614 } else {
615 return $this->getRawUserText();
616 }
617 }
618
619 /**
620 * Fetch revision's username without regard for view restrictions
621 *
622 * @return String
623 */
624 public function getRawUserText() {
625 if ( $this->mUserText === null ) {
626 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
627 if ( $this->mUserText === false ) {
628 # This shouldn't happen, but it can if the wiki was recovered
629 # via importing revs and there is no user table entry yet.
630 $this->mUserText = $this->mOrigUserText;
631 }
632 }
633 return $this->mUserText;
634 }
635
636 /**
637 * Fetch revision comment if it's available to the specified audience.
638 * If the specified audience does not have access to the comment, an
639 * empty string will be returned.
640 *
641 * @param $audience Integer: one of:
642 * Revision::FOR_PUBLIC to be displayed to all users
643 * Revision::FOR_THIS_USER to be displayed to $wgUser
644 * Revision::RAW get the text regardless of permissions
645 * @param $user User object to check for, only if FOR_THIS_USER is passed
646 * to the $audience parameter
647 * @return String
648 */
649 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
650 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
651 return '';
652 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
653 return '';
654 } else {
655 return $this->mComment;
656 }
657 }
658
659 /**
660 * Fetch revision comment without regard for the current user's permissions
661 *
662 * @return String
663 */
664 public function getRawComment() {
665 return $this->mComment;
666 }
667
668 /**
669 * @return Boolean
670 */
671 public function isMinor() {
672 return (bool)$this->mMinorEdit;
673 }
674
675 /**
676 * @return Integer rcid of the unpatrolled row, zero if there isn't one
677 */
678 public function isUnpatrolled() {
679 if( $this->mUnpatrolled !== null ) {
680 return $this->mUnpatrolled;
681 }
682 $dbr = wfGetDB( DB_SLAVE );
683 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
684 'rc_id',
685 array( // Add redundant user,timestamp condition so we can use the existing index
686 'rc_user_text' => $this->getRawUserText(),
687 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
688 'rc_this_oldid' => $this->getId(),
689 'rc_patrolled' => 0
690 ),
691 __METHOD__
692 );
693 return (int)$this->mUnpatrolled;
694 }
695
696 /**
697 * @param $field int one of DELETED_* bitfield constants
698 *
699 * @return Boolean
700 */
701 public function isDeleted( $field ) {
702 return ( $this->mDeleted & $field ) == $field;
703 }
704
705 /**
706 * Get the deletion bitfield of the revision
707 *
708 * @return int
709 */
710 public function getVisibility() {
711 return (int)$this->mDeleted;
712 }
713
714 /**
715 * Fetch revision text if it's available to the specified audience.
716 * If the specified audience does not have the ability to view this
717 * revision, an empty string will be returned.
718 *
719 * @param $audience Integer: one of:
720 * Revision::FOR_PUBLIC to be displayed to all users
721 * Revision::FOR_THIS_USER to be displayed to $wgUser
722 * Revision::RAW get the text regardless of permissions
723 * @param $user User object to check for, only if FOR_THIS_USER is passed
724 * to the $audience parameter
725 * @return String
726 */
727 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
728 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
729 return '';
730 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
731 return '';
732 } else {
733 return $this->getRawText();
734 }
735 }
736
737 /**
738 * Alias for getText(Revision::FOR_THIS_USER)
739 *
740 * @deprecated since 1.17
741 * @return String
742 */
743 public function revText() {
744 wfDeprecated( __METHOD__, '1.17' );
745 return $this->getText( self::FOR_THIS_USER );
746 }
747
748 /**
749 * Fetch revision text without regard for view restrictions
750 *
751 * @return String
752 */
753 public function getRawText() {
754 if( is_null( $this->mText ) ) {
755 // Revision text is immutable. Load on demand:
756 $this->mText = $this->loadText();
757 }
758 return $this->mText;
759 }
760
761 /**
762 * @return String
763 */
764 public function getTimestamp() {
765 return wfTimestamp( TS_MW, $this->mTimestamp );
766 }
767
768 /**
769 * @return Boolean
770 */
771 public function isCurrent() {
772 return $this->mCurrent;
773 }
774
775 /**
776 * Get previous revision for this title
777 *
778 * @return Revision or null
779 */
780 public function getPrevious() {
781 if( $this->getTitle() ) {
782 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
783 if( $prev ) {
784 return Revision::newFromTitle( $this->getTitle(), $prev );
785 }
786 }
787 return null;
788 }
789
790 /**
791 * Get next revision for this title
792 *
793 * @return Revision or null
794 */
795 public function getNext() {
796 if( $this->getTitle() ) {
797 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
798 if ( $next ) {
799 return Revision::newFromTitle( $this->getTitle(), $next );
800 }
801 }
802 return null;
803 }
804
805 /**
806 * Get previous revision Id for this page_id
807 * This is used to populate rev_parent_id on save
808 *
809 * @param $db DatabaseBase
810 * @return Integer
811 */
812 private function getPreviousRevisionId( $db ) {
813 if( is_null( $this->mPage ) ) {
814 return 0;
815 }
816 # Use page_latest if ID is not given
817 if( !$this->mId ) {
818 $prevId = $db->selectField( 'page', 'page_latest',
819 array( 'page_id' => $this->mPage ),
820 __METHOD__ );
821 } else {
822 $prevId = $db->selectField( 'revision', 'rev_id',
823 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
824 __METHOD__,
825 array( 'ORDER BY' => 'rev_id DESC' ) );
826 }
827 return intval( $prevId );
828 }
829
830 /**
831 * Get revision text associated with an old or archive row
832 * $row is usually an object from wfFetchRow(), both the flags and the text
833 * field must be included
834 *
835 * @param $row Object: the text data
836 * @param $prefix String: table prefix (default 'old_')
837 * @return String: text the text requested or false on failure
838 */
839 public static function getRevisionText( $row, $prefix = 'old_' ) {
840 wfProfileIn( __METHOD__ );
841
842 # Get data
843 $textField = $prefix . 'text';
844 $flagsField = $prefix . 'flags';
845
846 if( isset( $row->$flagsField ) ) {
847 $flags = explode( ',', $row->$flagsField );
848 } else {
849 $flags = array();
850 }
851
852 if( isset( $row->$textField ) ) {
853 $text = $row->$textField;
854 } else {
855 wfProfileOut( __METHOD__ );
856 return false;
857 }
858
859 # Use external methods for external objects, text in table is URL-only then
860 if ( in_array( 'external', $flags ) ) {
861 $url = $text;
862 $parts = explode( '://', $url, 2 );
863 if( count( $parts ) == 1 || $parts[1] == '' ) {
864 wfProfileOut( __METHOD__ );
865 return false;
866 }
867 $text = ExternalStore::fetchFromURL( $url );
868 }
869
870 // If the text was fetched without an error, convert it
871 if ( $text !== false ) {
872 if( in_array( 'gzip', $flags ) ) {
873 # Deal with optional compression of archived pages.
874 # This can be done periodically via maintenance/compressOld.php, and
875 # as pages are saved if $wgCompressRevisions is set.
876 $text = gzinflate( $text );
877 }
878
879 if( in_array( 'object', $flags ) ) {
880 # Generic compressed storage
881 $obj = unserialize( $text );
882 if ( !is_object( $obj ) ) {
883 // Invalid object
884 wfProfileOut( __METHOD__ );
885 return false;
886 }
887 $text = $obj->getText();
888 }
889
890 global $wgLegacyEncoding;
891 if( $text !== false && $wgLegacyEncoding
892 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
893 {
894 # Old revisions kept around in a legacy encoding?
895 # Upconvert on demand.
896 # ("utf8" checked for compatibility with some broken
897 # conversion scripts 2008-12-30)
898 global $wgContLang;
899 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
900 }
901 }
902 wfProfileOut( __METHOD__ );
903 return $text;
904 }
905
906 /**
907 * If $wgCompressRevisions is enabled, we will compress data.
908 * The input string is modified in place.
909 * Return value is the flags field: contains 'gzip' if the
910 * data is compressed, and 'utf-8' if we're saving in UTF-8
911 * mode.
912 *
913 * @param $text Mixed: reference to a text
914 * @return String
915 */
916 public static function compressRevisionText( &$text ) {
917 global $wgCompressRevisions;
918 $flags = array();
919
920 # Revisions not marked this way will be converted
921 # on load if $wgLegacyCharset is set in the future.
922 $flags[] = 'utf-8';
923
924 if( $wgCompressRevisions ) {
925 if( function_exists( 'gzdeflate' ) ) {
926 $text = gzdeflate( $text );
927 $flags[] = 'gzip';
928 } else {
929 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
930 }
931 }
932 return implode( ',', $flags );
933 }
934
935 /**
936 * Insert a new revision into the database, returning the new revision ID
937 * number on success and dies horribly on failure.
938 *
939 * @param $dbw DatabaseBase: (master connection)
940 * @return Integer
941 */
942 public function insertOn( $dbw ) {
943 global $wgDefaultExternalStore;
944
945 wfProfileIn( __METHOD__ );
946
947 $data = $this->mText;
948 $flags = Revision::compressRevisionText( $data );
949
950 # Write to external storage if required
951 if( $wgDefaultExternalStore ) {
952 // Store and get the URL
953 $data = ExternalStore::insertToDefault( $data );
954 if( !$data ) {
955 throw new MWException( "Unable to store text to external storage" );
956 }
957 if( $flags ) {
958 $flags .= ',';
959 }
960 $flags .= 'external';
961 }
962
963 # Record the text (or external storage URL) to the text table
964 if( !isset( $this->mTextId ) ) {
965 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
966 $dbw->insert( 'text',
967 array(
968 'old_id' => $old_id,
969 'old_text' => $data,
970 'old_flags' => $flags,
971 ), __METHOD__
972 );
973 $this->mTextId = $dbw->insertId();
974 }
975
976 if ( $this->mComment === null ) $this->mComment = "";
977
978 # Record the edit in revisions
979 $rev_id = isset( $this->mId )
980 ? $this->mId
981 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
982 $dbw->insert( 'revision',
983 array(
984 'rev_id' => $rev_id,
985 'rev_page' => $this->mPage,
986 'rev_text_id' => $this->mTextId,
987 'rev_comment' => $this->mComment,
988 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
989 'rev_user' => $this->mUser,
990 'rev_user_text' => $this->mUserText,
991 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
992 'rev_deleted' => $this->mDeleted,
993 'rev_len' => $this->mSize,
994 'rev_parent_id' => is_null( $this->mParentId )
995 ? $this->getPreviousRevisionId( $dbw )
996 : $this->mParentId,
997 'rev_sha1' => is_null( $this->mSha1 )
998 ? Revision::base36Sha1( $this->mText )
999 : $this->mSha1
1000 ), __METHOD__
1001 );
1002
1003 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1004
1005 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1006
1007 wfProfileOut( __METHOD__ );
1008 return $this->mId;
1009 }
1010
1011 /**
1012 * Get the base 36 SHA-1 value for a string of text
1013 * @param $text String
1014 * @return String
1015 */
1016 public static function base36Sha1( $text ) {
1017 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1018 }
1019
1020 /**
1021 * Lazy-load the revision's text.
1022 * Currently hardcoded to the 'text' table storage engine.
1023 *
1024 * @return String
1025 */
1026 protected function loadText() {
1027 wfProfileIn( __METHOD__ );
1028
1029 // Caching may be beneficial for massive use of external storage
1030 global $wgRevisionCacheExpiry, $wgMemc;
1031 $textId = $this->getTextId();
1032 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1033 if( $wgRevisionCacheExpiry ) {
1034 $text = $wgMemc->get( $key );
1035 if( is_string( $text ) ) {
1036 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1037 wfProfileOut( __METHOD__ );
1038 return $text;
1039 }
1040 }
1041
1042 // If we kept data for lazy extraction, use it now...
1043 if ( isset( $this->mTextRow ) ) {
1044 $row = $this->mTextRow;
1045 $this->mTextRow = null;
1046 } else {
1047 $row = null;
1048 }
1049
1050 if( !$row ) {
1051 // Text data is immutable; check slaves first.
1052 $dbr = wfGetDB( DB_SLAVE );
1053 $row = $dbr->selectRow( 'text',
1054 array( 'old_text', 'old_flags' ),
1055 array( 'old_id' => $this->getTextId() ),
1056 __METHOD__ );
1057 }
1058
1059 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1060 // Possible slave lag!
1061 $dbw = wfGetDB( DB_MASTER );
1062 $row = $dbw->selectRow( 'text',
1063 array( 'old_text', 'old_flags' ),
1064 array( 'old_id' => $this->getTextId() ),
1065 __METHOD__ );
1066 }
1067
1068 $text = self::getRevisionText( $row );
1069
1070 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1071 if( $wgRevisionCacheExpiry && $text !== false ) {
1072 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1073 }
1074
1075 wfProfileOut( __METHOD__ );
1076
1077 return $text;
1078 }
1079
1080 /**
1081 * Create a new null-revision for insertion into a page's
1082 * history. This will not re-save the text, but simply refer
1083 * to the text from the previous version.
1084 *
1085 * Such revisions can for instance identify page rename
1086 * operations and other such meta-modifications.
1087 *
1088 * @param $dbw DatabaseBase
1089 * @param $pageId Integer: ID number of the page to read from
1090 * @param $summary String: revision's summary
1091 * @param $minor Boolean: whether the revision should be considered as minor
1092 * @return Revision|null on error
1093 */
1094 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1095 wfProfileIn( __METHOD__ );
1096
1097 $current = $dbw->selectRow(
1098 array( 'page', 'revision' ),
1099 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' ),
1100 array(
1101 'page_id' => $pageId,
1102 'page_latest=rev_id',
1103 ),
1104 __METHOD__ );
1105
1106 if( $current ) {
1107 $revision = new Revision( array(
1108 'page' => $pageId,
1109 'comment' => $summary,
1110 'minor_edit' => $minor,
1111 'text_id' => $current->rev_text_id,
1112 'parent_id' => $current->page_latest,
1113 'len' => $current->rev_len,
1114 'sha1' => $current->rev_sha1
1115 ) );
1116 } else {
1117 $revision = null;
1118 }
1119
1120 wfProfileOut( __METHOD__ );
1121 return $revision;
1122 }
1123
1124 /**
1125 * Determine if the current user is allowed to view a particular
1126 * field of this revision, if it's marked as deleted.
1127 *
1128 * @param $field Integer:one of self::DELETED_TEXT,
1129 * self::DELETED_COMMENT,
1130 * self::DELETED_USER
1131 * @param $user User object to check, or null to use $wgUser
1132 * @return Boolean
1133 */
1134 public function userCan( $field, User $user = null ) {
1135 return self::userCanBitfield( $this->mDeleted, $field, $user );
1136 }
1137
1138 /**
1139 * Determine if the current user is allowed to view a particular
1140 * field of this revision, if it's marked as deleted. This is used
1141 * by various classes to avoid duplication.
1142 *
1143 * @param $bitfield Integer: current field
1144 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1145 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1146 * self::DELETED_USER = File::DELETED_USER
1147 * @param $user User object to check, or null to use $wgUser
1148 * @return Boolean
1149 */
1150 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1151 if( $bitfield & $field ) { // aspect is deleted
1152 if ( $bitfield & self::DELETED_RESTRICTED ) {
1153 $permission = 'suppressrevision';
1154 } elseif ( $field & self::DELETED_TEXT ) {
1155 $permission = 'deletedtext';
1156 } else {
1157 $permission = 'deletedhistory';
1158 }
1159 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1160 if ( $user === null ) {
1161 global $wgUser;
1162 $user = $wgUser;
1163 }
1164 return $user->isAllowed( $permission );
1165 } else {
1166 return true;
1167 }
1168 }
1169
1170 /**
1171 * Get rev_timestamp from rev_id, without loading the rest of the row
1172 *
1173 * @param $title Title
1174 * @param $id Integer
1175 * @return String
1176 */
1177 static function getTimestampFromId( $title, $id ) {
1178 $dbr = wfGetDB( DB_SLAVE );
1179 // Casting fix for DB2
1180 if ( $id == '' ) {
1181 $id = 0;
1182 }
1183 $conds = array( 'rev_id' => $id );
1184 $conds['rev_page'] = $title->getArticleId();
1185 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1186 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1187 # Not in slave, try master
1188 $dbw = wfGetDB( DB_MASTER );
1189 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1190 }
1191 return wfTimestamp( TS_MW, $timestamp );
1192 }
1193
1194 /**
1195 * Get count of revisions per page...not very efficient
1196 *
1197 * @param $db DatabaseBase
1198 * @param $id Integer: page id
1199 * @return Integer
1200 */
1201 static function countByPageId( $db, $id ) {
1202 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1203 array( 'rev_page' => $id ), __METHOD__ );
1204 if( $row ) {
1205 return $row->revCount;
1206 }
1207 return 0;
1208 }
1209
1210 /**
1211 * Get count of revisions per page...not very efficient
1212 *
1213 * @param $db DatabaseBase
1214 * @param $title Title
1215 * @return Integer
1216 */
1217 static function countByTitle( $db, $title ) {
1218 $id = $title->getArticleId();
1219 if( $id ) {
1220 return Revision::countByPageId( $db, $id );
1221 }
1222 return 0;
1223 }
1224 }