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