3 * Representation of a page version.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class Revision
implements IDBAccessObject
{
30 protected $mOrigUserText;
32 protected $mMinorEdit;
33 protected $mTimestamp;
43 protected $mContentModel;
44 protected $mContentFormat;
46 protected $mContentHandler;
48 // Revision deletion constants
49 const DELETED_TEXT
= 1;
50 const DELETED_COMMENT
= 2;
51 const DELETED_USER
= 4;
52 const DELETED_RESTRICTED
= 8;
53 const SUPPRESSED_USER
= 12; // convenience
55 // Audience options for accessors
57 const FOR_THIS_USER
= 2;
61 * Load a page revision from a given revision ID number.
62 * Returns null if no such revision can be found.
65 * Revision::READ_LATEST : Select the data from the master
66 * Revision::READ_LOCKING : Select & lock the data from the master
69 * @param $flags Integer (optional)
70 * @return Revision or null
72 public static function newFromId( $id, $flags = 0 ) {
73 return self
::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
77 * Load either the current, or a specified, revision
78 * that's attached to a given title. If not attached
79 * to that title, will return null.
82 * Revision::READ_LATEST : Select the data from the master
83 * Revision::READ_LOCKING : Select & lock the data from the master
86 * @param $id Integer (optional)
87 * @param $flags Integer Bitfield (optional)
88 * @return Revision or null
90 public static function newFromTitle( $title, $id = 0, $flags = null ) {
92 'page_namespace' => $title->getNamespace(),
93 'page_title' => $title->getDBkey()
96 // Use the specified ID
97 $conds['rev_id'] = $id;
99 // Use a join to get the latest revision
100 $conds[] = 'rev_id=page_latest';
101 // Callers assume this will be up-to-date
102 $flags = is_int( $flags ) ?
$flags : self
::READ_LATEST
; // b/c
104 return self
::newFromConds( $conds, (int)$flags );
108 * Load either the current, or a specified, revision
109 * that's attached to a given page ID.
110 * Returns null if no such revision can be found.
113 * Revision::READ_LATEST : Select the data from the master
114 * Revision::READ_LOCKING : Select & lock the data from the master
116 * @param $revId Integer
117 * @param $pageId Integer (optional)
118 * @param $flags Integer Bitfield (optional)
119 * @return Revision or null
121 public static function newFromPageId( $pageId, $revId = 0, $flags = null ) {
122 $conds = array( 'page_id' => $pageId );
124 $conds['rev_id'] = $revId;
126 // Use a join to get the latest revision
127 $conds[] = 'rev_id = page_latest';
128 // Callers assume this will be up-to-date
129 $flags = is_int( $flags ) ?
$flags : self
::READ_LATEST
; // b/c
131 return self
::newFromConds( $conds, (int)$flags );
135 * Make a fake revision object from an archive table row. This is queried
136 * for permissions or even inserted (as in Special:Undelete)
137 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
140 * @param $overrides array
144 public static function newFromArchiveRow( $row, $overrides = array() ) {
145 global $wgContentHandlerUseDB;
147 $attribs = $overrides +
array(
148 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
149 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
150 'comment' => $row->ar_comment
,
151 'user' => $row->ar_user
,
152 'user_text' => $row->ar_user_text
,
153 'timestamp' => $row->ar_timestamp
,
154 'minor_edit' => $row->ar_minor_edit
,
155 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
156 'deleted' => $row->ar_deleted
,
157 'len' => $row->ar_len
,
158 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
159 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
160 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
163 if ( !$wgContentHandlerUseDB ) {
164 unset( $attribs['content_model'] );
165 unset( $attribs['content_format'] );
168 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
169 // Pre-1.5 ar_text row
170 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
171 if ( $attribs['text'] === false ) {
172 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
175 return new self( $attribs );
184 public static function newFromRow( $row ) {
185 return new self( $row );
189 * Load a page revision from a given revision ID number.
190 * Returns null if no such revision can be found.
192 * @param $db DatabaseBase
194 * @return Revision or null
196 public static function loadFromId( $db, $id ) {
197 return self
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
201 * Load either the current, or a specified, revision
202 * that's attached to a given page. If not attached
203 * to that page, will return null.
205 * @param $db DatabaseBase
206 * @param $pageid Integer
208 * @return Revision or null
210 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
211 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
213 $conds['rev_id'] = intval( $id );
215 $conds[] = 'rev_id=page_latest';
217 return self
::loadFromConds( $db, $conds );
221 * Load either the current, or a specified, revision
222 * that's attached to a given page. If not attached
223 * to that page, will return null.
225 * @param $db DatabaseBase
226 * @param $title Title
228 * @return Revision or null
230 public static function loadFromTitle( $db, $title, $id = 0 ) {
232 $matchId = intval( $id );
234 $matchId = 'page_latest';
236 return self
::loadFromConds( $db,
237 array( "rev_id=$matchId",
238 'page_namespace' => $title->getNamespace(),
239 'page_title' => $title->getDBkey() )
244 * Load the revision for the given title with the given timestamp.
245 * WARNING: Timestamps may in some circumstances not be unique,
246 * so this isn't the best key to use.
248 * @param $db DatabaseBase
249 * @param $title Title
250 * @param $timestamp String
251 * @return Revision or null
253 public static function loadFromTimestamp( $db, $title, $timestamp ) {
254 return self
::loadFromConds( $db,
255 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
256 'page_namespace' => $title->getNamespace(),
257 'page_title' => $title->getDBkey() )
262 * Given a set of conditions, fetch a revision.
264 * @param $conditions Array
265 * @param $flags integer (optional)
266 * @return Revision or null
268 private static function newFromConds( $conditions, $flags = 0 ) {
269 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_SLAVE
);
270 $rev = self
::loadFromConds( $db, $conditions, $flags );
271 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
272 if ( !( $flags & self
::READ_LATEST
) ) {
273 $dbw = wfGetDB( DB_MASTER
);
274 $rev = self
::loadFromConds( $dbw, $conditions, $flags );
281 * Given a set of conditions, fetch a revision from
282 * the given database connection.
284 * @param $db DatabaseBase
285 * @param $conditions Array
286 * @param $flags integer (optional)
287 * @return Revision or null
289 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
290 $res = self
::fetchFromConds( $db, $conditions, $flags );
292 $row = $res->fetchObject();
294 $ret = new Revision( $row );
303 * Return a wrapper for a series of database rows to
304 * fetch all of a given page's revisions in turn.
305 * Each row can be fed to the constructor to get objects.
307 * @param $title Title
308 * @return ResultWrapper
310 public static function fetchRevision( $title ) {
311 return self
::fetchFromConds(
313 array( 'rev_id=page_latest',
314 'page_namespace' => $title->getNamespace(),
315 'page_title' => $title->getDBkey() )
320 * Given a set of conditions, return a ResultWrapper
321 * which will return matching database rows with the
322 * fields necessary to build Revision objects.
324 * @param $db DatabaseBase
325 * @param $conditions Array
326 * @param $flags integer (optional)
327 * @return ResultWrapper
329 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
330 $fields = array_merge(
331 self
::selectFields(),
332 self
::selectPageFields(),
333 self
::selectUserFields()
335 $options = array( 'LIMIT' => 1 );
336 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
337 $options[] = 'FOR UPDATE';
340 array( 'revision', 'page', 'user' ),
345 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
350 * Return the value of a select() JOIN conds array for the user table.
351 * This will get user table rows for logged-in users.
355 public static function userJoinCond() {
356 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
360 * Return the value of a select() page conds array for the paeg table.
361 * This will assure that the revision(s) are not orphaned from live pages.
365 public static function pageJoinCond() {
366 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
370 * Return the list of revision fields that should be selected to create
374 public static function selectFields() {
375 global $wgContentHandlerUseDB;
392 if ( $wgContentHandlerUseDB ) {
393 $fields[] = 'rev_content_format';
394 $fields[] = 'rev_content_model';
401 * Return the list of text fields that should be selected to read the
405 public static function selectTextFields() {
413 * Return the list of page fields that should be selected from page table
416 public static function selectPageFields() {
428 * Return the list of user fields that should be selected from user table
431 public static function selectUserFields() {
432 return array( 'user_name' );
436 * Do a batched query to get the parent revision lengths
437 * @param $db DatabaseBase
438 * @param $revIds Array
441 public static function getParentLengths( $db, array $revIds ) {
444 return $revLens; // empty
446 wfProfileIn( __METHOD__
);
447 $res = $db->select( 'revision',
448 array( 'rev_id', 'rev_len' ),
449 array( 'rev_id' => $revIds ),
451 foreach ( $res as $row ) {
452 $revLens[$row->rev_id
] = $row->rev_len
;
454 wfProfileOut( __METHOD__
);
461 * @param $row Mixed: either a database row or an array
464 function __construct( $row ) {
465 if( is_object( $row ) ) {
466 $this->mId
= intval( $row->rev_id
);
467 $this->mPage
= intval( $row->rev_page
);
468 $this->mTextId
= intval( $row->rev_text_id
);
469 $this->mComment
= $row->rev_comment
;
470 $this->mUser
= intval( $row->rev_user
);
471 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
472 $this->mTimestamp
= $row->rev_timestamp
;
473 $this->mDeleted
= intval( $row->rev_deleted
);
475 if( !isset( $row->rev_parent_id
) ) {
476 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
478 $this->mParentId
= intval( $row->rev_parent_id
);
481 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
484 $this->mSize
= intval( $row->rev_len
);
487 if ( !isset( $row->rev_sha1
) ) {
490 $this->mSha1
= $row->rev_sha1
;
493 if( isset( $row->page_latest
) ) {
494 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
495 $this->mTitle
= Title
::newFromRow( $row );
497 $this->mCurrent
= false;
498 $this->mTitle
= null;
501 if( !isset( $row->rev_content_model
) ||
is_null( $row->rev_content_model
) ) {
502 $this->mContentModel
= null; # determine on demand if needed
504 $this->mContentModel
= strval( $row->rev_content_model
);
507 if( !isset( $row->rev_content_format
) ||
is_null( $row->rev_content_format
) ) {
508 $this->mContentFormat
= null; # determine on demand if needed
510 $this->mContentFormat
= strval( $row->rev_content_format
);
513 // Lazy extraction...
515 if( isset( $row->old_text
) ) {
516 $this->mTextRow
= $row;
518 // 'text' table row entry will be lazy-loaded
519 $this->mTextRow
= null;
522 // Use user_name for users and rev_user_text for IPs...
523 $this->mUserText
= null; // lazy load if left null
524 if ( $this->mUser
== 0 ) {
525 $this->mUserText
= $row->rev_user_text
; // IP user
526 } elseif ( isset( $row->user_name
) ) {
527 $this->mUserText
= $row->user_name
; // logged-in user
529 $this->mOrigUserText
= $row->rev_user_text
;
530 } elseif( is_array( $row ) ) {
531 // Build a new revision to be saved...
532 global $wgUser; // ugh
535 # if we have a content object, use it to set the model and type
536 if ( !empty( $row['content'] ) ) {
537 //@todo: when is that set? test with external store setup! check out insertOn() [dk]
538 if ( !empty( $row['text_id'] ) ) {
539 throw new MWException( "Text already stored in external store (id {$row['text_id']}), "
540 . "can't serialize content object" );
543 $row['content_model'] = $row['content']->getModel();
544 # note: mContentFormat is initializes later accordingly
545 # note: content is serialized later in this method!
546 # also set text to null?
549 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
550 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
551 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
552 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
553 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
554 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
555 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
556 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
557 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
558 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
559 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
561 $this->mContentModel
= isset( $row['content_model'] ) ?
strval( $row['content_model'] ) : null;
562 $this->mContentFormat
= isset( $row['content_format'] ) ?
strval( $row['content_format'] ) : null;
564 // Enforce spacing trimming on supplied text
565 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
566 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
567 $this->mTextRow
= null;
569 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
571 // if we have a Content object, override mText and mContentModel
572 if ( !empty( $row['content'] ) ) {
573 $handler = $this->getContentHandler();
574 $this->mContent
= $row['content'];
576 $this->mContentModel
= $this->mContent
->getModel();
577 $this->mContentHandler
= null;
579 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
580 } elseif ( !is_null( $this->mText
) ) {
581 $handler = $this->getContentHandler();
582 $this->mContent
= $handler->unserializeContent( $this->mText
);
585 // if we have a Title object, override mPage. Useful for testing and convenience.
586 if ( isset( $row['title'] ) ) {
587 $this->mTitle
= $row['title'];
588 $this->mPage
= $this->mTitle
->getArticleID();
590 $this->mTitle
= null; // Load on demand if needed
593 // @todo: XXX: really? we are about to create a revision. it will usually then be the current one.
594 $this->mCurrent
= false;
596 // If we still have no length, see it we have the text to figure it out
597 if ( !$this->mSize
) {
598 if ( !is_null( $this->mContent
) ) {
599 $this->mSize
= $this->mContent
->getSize();
601 #NOTE: this should never happen if we have either text or content object!
607 if ( $this->mSha1
=== null ) {
608 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
612 $this->getContentModel();
613 $this->getContentFormat();
615 throw new MWException( 'Revision constructor passed invalid row format.' );
617 $this->mUnpatrolled
= null;
623 * @return Integer|null
625 public function getId() {
630 * Set the revision ID
635 public function setId( $id ) {
642 * @return Integer|null
644 public function getTextId() {
645 return $this->mTextId
;
649 * Get parent revision ID (the original previous page revision)
651 * @return Integer|null
653 public function getParentId() {
654 return $this->mParentId
;
658 * Returns the length of the text in this revision, or null if unknown.
660 * @return Integer|null
662 public function getSize() {
667 * Returns the base36 sha1 of the text in this revision, or null if unknown.
669 * @return String|null
671 public function getSha1() {
676 * Returns the title of the page associated with this entry or null.
678 * Will do a query, when title is not set and id is given.
682 public function getTitle() {
683 if( isset( $this->mTitle
) ) {
684 return $this->mTitle
;
686 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
687 $dbr = wfGetDB( DB_SLAVE
);
688 $row = $dbr->selectRow(
689 array( 'page', 'revision' ),
690 self
::selectPageFields(),
691 array( 'page_id=rev_page',
692 'rev_id' => $this->mId
),
695 $this->mTitle
= Title
::newFromRow( $row );
699 if ( !$this->mTitle
&& !is_null( $this->mPage
) && $this->mPage
> 0 ) {
700 $this->mTitle
= Title
::newFromID( $this->mPage
);
703 return $this->mTitle
;
707 * Set the title of the revision
709 * @param $title Title
711 public function setTitle( $title ) {
712 $this->mTitle
= $title;
718 * @return Integer|null
720 public function getPage() {
725 * Fetch revision's user id if it's available to the specified audience.
726 * If the specified audience does not have access to it, zero will be
729 * @param $audience Integer: one of:
730 * Revision::FOR_PUBLIC to be displayed to all users
731 * Revision::FOR_THIS_USER to be displayed to the given user
732 * Revision::RAW get the ID regardless of permissions
733 * @param $user User object to check for, only if FOR_THIS_USER is passed
734 * to the $audience parameter
737 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
738 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
740 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
748 * Fetch revision's user id without regard for the current user's permissions
752 public function getRawUser() {
757 * Fetch revision's username if it's available to the specified audience.
758 * If the specified audience does not have access to the username, an
759 * empty string will be returned.
761 * @param $audience Integer: one of:
762 * Revision::FOR_PUBLIC to be displayed to all users
763 * Revision::FOR_THIS_USER to be displayed to the given user
764 * Revision::RAW get the text regardless of permissions
765 * @param $user User object to check for, only if FOR_THIS_USER is passed
766 * to the $audience parameter
769 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
770 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
772 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
775 return $this->getRawUserText();
780 * Fetch revision's username without regard for view restrictions
784 public function getRawUserText() {
785 if ( $this->mUserText
=== null ) {
786 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
787 if ( $this->mUserText
=== false ) {
788 # This shouldn't happen, but it can if the wiki was recovered
789 # via importing revs and there is no user table entry yet.
790 $this->mUserText
= $this->mOrigUserText
;
793 return $this->mUserText
;
797 * Fetch revision comment if it's available to the specified audience.
798 * If the specified audience does not have access to the comment, an
799 * empty string will be returned.
801 * @param $audience Integer: one of:
802 * Revision::FOR_PUBLIC to be displayed to all users
803 * Revision::FOR_THIS_USER to be displayed to the given user
804 * Revision::RAW get the text regardless of permissions
805 * @param $user User object to check for, only if FOR_THIS_USER is passed
806 * to the $audience parameter
809 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
810 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
812 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
815 return $this->mComment
;
820 * Fetch revision comment without regard for the current user's permissions
824 public function getRawComment() {
825 return $this->mComment
;
831 public function isMinor() {
832 return (bool)$this->mMinorEdit
;
836 * @return Integer rcid of the unpatrolled row, zero if there isn't one
838 public function isUnpatrolled() {
839 if( $this->mUnpatrolled
!== null ) {
840 return $this->mUnpatrolled
;
842 $dbr = wfGetDB( DB_SLAVE
);
843 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
845 array( // Add redundant user,timestamp condition so we can use the existing index
846 'rc_user_text' => $this->getRawUserText(),
847 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
848 'rc_this_oldid' => $this->getId(),
853 return (int)$this->mUnpatrolled
;
857 * @param $field int one of DELETED_* bitfield constants
861 public function isDeleted( $field ) {
862 return ( $this->mDeleted
& $field ) == $field;
866 * Get the deletion bitfield of the revision
870 public function getVisibility() {
871 return (int)$this->mDeleted
;
875 * Fetch revision text if it's available to the specified audience.
876 * If the specified audience does not have the ability to view this
877 * revision, an empty string will be returned.
879 * @param $audience Integer: one of:
880 * Revision::FOR_PUBLIC to be displayed to all users
881 * Revision::FOR_THIS_USER to be displayed to the given user
882 * Revision::RAW get the text regardless of permissions
883 * @param $user User object to check for, only if FOR_THIS_USER is passed
884 * to the $audience parameter
886 * @deprecated in 1.21, use getContent() instead
887 * @todo: replace usage in core
890 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
891 wfDeprecated( __METHOD__
, '1.21' );
893 $content = $this->getContent( $audience, $user );
894 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
898 * Fetch revision content if it's available to the specified audience.
899 * If the specified audience does not have the ability to view this
900 * revision, null will be returned.
902 * @param $audience Integer: one of:
903 * Revision::FOR_PUBLIC to be displayed to all users
904 * Revision::FOR_THIS_USER to be displayed to $wgUser
905 * Revision::RAW get the text regardless of permissions
906 * @param $user User object to check for, only if FOR_THIS_USER is passed
907 * to the $audience parameter
911 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
912 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
914 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
917 return $this->getContentInternal();
922 * Alias for getText(Revision::FOR_THIS_USER)
924 * @deprecated since 1.17
927 public function revText() {
928 wfDeprecated( __METHOD__
, '1.17' );
929 return $this->getText( self
::FOR_THIS_USER
);
933 * Fetch revision text without regard for view restrictions
937 * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW )
938 * or Revision::getSerializedData() as appropriate.
940 public function getRawText() {
941 wfDeprecated( __METHOD__
, "1.21" );
943 return $this->getText( self
::RAW
);
947 * Fetch original serialized data without regard for view restrictions
952 public function getSerializedData() {
957 * Gets the content object for the revision
962 protected function getContentInternal() {
963 if( is_null( $this->mContent
) ) {
964 // Revision is immutable. Load on demand:
966 $handler = $this->getContentHandler();
967 $format = $this->getContentFormat();
968 $title = $this->getTitle();
970 if( is_null( $this->mText
) ) {
971 // Load text on demand:
972 $this->mText
= $this->loadText();
975 $this->mContent
= is_null( $this->mText
) ?
null : $handler->unserializeContent( $this->mText
, $format );
978 return $this->mContent
->copy(); // NOTE: copy() will return $this for immutable content objects
982 * Returns the content model for this revision.
984 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
985 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
986 * is used as a last resort.
988 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
990 public function getContentModel() {
991 if ( !$this->mContentModel
) {
992 $title = $this->getTitle();
993 $this->mContentModel
= ( $title ?
$title->getContentModel() : CONTENT_MODEL_WIKITEXT
);
995 assert( !empty( $this->mContentModel
) );
998 return $this->mContentModel
;
1002 * Returns the content format for this revision.
1004 * If no content format was stored in the database, the default format for this
1005 * revision's content model is returned.
1007 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1009 public function getContentFormat() {
1010 if ( !$this->mContentFormat
) {
1011 $handler = $this->getContentHandler();
1012 $this->mContentFormat
= $handler->getDefaultFormat();
1014 assert( !empty( $this->mContentFormat
) );
1017 return $this->mContentFormat
;
1021 * Returns the content handler appropriate for this revision's content model.
1023 * @return ContentHandler
1025 public function getContentHandler() {
1026 if ( !$this->mContentHandler
) {
1027 $model = $this->getContentModel();
1028 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1030 $format = $this->getContentFormat();
1032 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1033 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1037 return $this->mContentHandler
;
1043 public function getTimestamp() {
1044 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1050 public function isCurrent() {
1051 return $this->mCurrent
;
1055 * Get previous revision for this title
1057 * @return Revision or null
1059 public function getPrevious() {
1060 if( $this->getTitle() ) {
1061 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1063 return self
::newFromTitle( $this->getTitle(), $prev );
1070 * Get next revision for this title
1072 * @return Revision or null
1074 public function getNext() {
1075 if( $this->getTitle() ) {
1076 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1078 return self
::newFromTitle( $this->getTitle(), $next );
1085 * Get previous revision Id for this page_id
1086 * This is used to populate rev_parent_id on save
1088 * @param $db DatabaseBase
1091 private function getPreviousRevisionId( $db ) {
1092 if( is_null( $this->mPage
) ) {
1095 # Use page_latest if ID is not given
1097 $prevId = $db->selectField( 'page', 'page_latest',
1098 array( 'page_id' => $this->mPage
),
1101 $prevId = $db->selectField( 'revision', 'rev_id',
1102 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
1104 array( 'ORDER BY' => 'rev_id DESC' ) );
1106 return intval( $prevId );
1110 * Get revision text associated with an old or archive row
1111 * $row is usually an object from wfFetchRow(), both the flags and the text
1112 * field must be included
1114 * @param $row Object: the text data
1115 * @param $prefix String: table prefix (default 'old_')
1116 * @return String: text the text requested or false on failure
1118 public static function getRevisionText( $row, $prefix = 'old_' ) {
1119 wfProfileIn( __METHOD__
);
1122 $textField = $prefix . 'text';
1123 $flagsField = $prefix . 'flags';
1125 if( isset( $row->$flagsField ) ) {
1126 $flags = explode( ',', $row->$flagsField );
1131 if( isset( $row->$textField ) ) {
1132 $text = $row->$textField;
1134 wfProfileOut( __METHOD__
);
1138 # Use external methods for external objects, text in table is URL-only then
1139 if ( in_array( 'external', $flags ) ) {
1141 $parts = explode( '://', $url, 2 );
1142 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
1143 wfProfileOut( __METHOD__
);
1146 $text = ExternalStore
::fetchFromURL( $url );
1149 // If the text was fetched without an error, convert it
1150 if ( $text !== false ) {
1151 if( in_array( 'gzip', $flags ) ) {
1152 # Deal with optional compression of archived pages.
1153 # This can be done periodically via maintenance/compressOld.php, and
1154 # as pages are saved if $wgCompressRevisions is set.
1155 $text = gzinflate( $text );
1158 if( in_array( 'object', $flags ) ) {
1159 # Generic compressed storage
1160 $obj = unserialize( $text );
1161 if ( !is_object( $obj ) ) {
1163 wfProfileOut( __METHOD__
);
1166 $text = $obj->getText();
1169 global $wgLegacyEncoding;
1170 if( $text !== false && $wgLegacyEncoding
1171 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1173 # Old revisions kept around in a legacy encoding?
1174 # Upconvert on demand.
1175 # ("utf8" checked for compatibility with some broken
1176 # conversion scripts 2008-12-30)
1178 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1181 wfProfileOut( __METHOD__
);
1186 * If $wgCompressRevisions is enabled, we will compress data.
1187 * The input string is modified in place.
1188 * Return value is the flags field: contains 'gzip' if the
1189 * data is compressed, and 'utf-8' if we're saving in UTF-8
1192 * @param $text Mixed: reference to a text
1195 public static function compressRevisionText( &$text ) {
1196 global $wgCompressRevisions;
1199 # Revisions not marked this way will be converted
1200 # on load if $wgLegacyCharset is set in the future.
1203 if( $wgCompressRevisions ) {
1204 if( function_exists( 'gzdeflate' ) ) {
1205 $text = gzdeflate( $text );
1208 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1211 return implode( ',', $flags );
1215 * Insert a new revision into the database, returning the new revision ID
1216 * number on success and dies horribly on failure.
1218 * @param $dbw DatabaseBase: (master connection)
1221 public function insertOn( $dbw ) {
1222 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1224 wfProfileIn( __METHOD__
);
1226 $this->checkContentModel();
1228 $data = $this->mText
;
1229 $flags = self
::compressRevisionText( $data );
1231 # Write to external storage if required
1232 if( $wgDefaultExternalStore ) {
1233 // Store and get the URL
1234 $data = ExternalStore
::insertToDefault( $data );
1236 throw new MWException( "Unable to store text to external storage" );
1241 $flags .= 'external';
1244 # Record the text (or external storage URL) to the text table
1245 if( !isset( $this->mTextId
) ) {
1246 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1247 $dbw->insert( 'text',
1249 'old_id' => $old_id,
1250 'old_text' => $data,
1251 'old_flags' => $flags,
1254 $this->mTextId
= $dbw->insertId();
1257 if ( $this->mComment
=== null ) $this->mComment
= "";
1259 # Record the edit in revisions
1260 $rev_id = isset( $this->mId
)
1262 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1264 'rev_id' => $rev_id,
1265 'rev_page' => $this->mPage
,
1266 'rev_text_id' => $this->mTextId
,
1267 'rev_comment' => $this->mComment
,
1268 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1269 'rev_user' => $this->mUser
,
1270 'rev_user_text' => $this->mUserText
,
1271 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1272 'rev_deleted' => $this->mDeleted
,
1273 'rev_len' => $this->mSize
,
1274 'rev_parent_id' => is_null( $this->mParentId
)
1275 ?
$this->getPreviousRevisionId( $dbw )
1277 'rev_sha1' => is_null( $this->mSha1
)
1278 ? Revision
::base36Sha1( $this->mText
)
1282 if ( $wgContentHandlerUseDB ) {
1283 //NOTE: Store null for the default model and format, to save space.
1284 //XXX: Makes the DB sensitive to changed defaults. Make this behaviour optional? Only in miser mode?
1286 $model = $this->getContentModel();
1287 $format = $this->getContentFormat();
1289 $title = $this->getTitle();
1291 if ( $title === null ) {
1292 throw new MWException( "Insufficient information to determine the title of the revision's page!" );
1295 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1296 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1298 $row[ 'rev_content_model' ] = ( $model === $defaultModel ) ?
null : $model;
1299 $row[ 'rev_content_format' ] = ( $format === $defaultFormat ) ?
null : $format;
1302 $dbw->insert( 'revision', $row, __METHOD__
);
1304 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1306 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1308 wfProfileOut( __METHOD__
);
1312 protected function checkContentModel() {
1313 global $wgContentHandlerUseDB;
1315 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1317 $model = $this->getContentModel();
1318 $format = $this->getContentFormat();
1319 $handler = $this->getContentHandler();
1321 if ( !$handler->isSupportedFormat( $format ) ) {
1322 $t = $title->getPrefixedDBkey();
1324 throw new MWException( "Can't use format $format with content model $model on $t" );
1327 if ( !$wgContentHandlerUseDB && $title ) {
1328 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1330 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1331 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1332 $defaultFormat = $defaultHandler->getDefaultFormat();
1334 if ( $this->getContentModel() != $defaultModel ) {
1335 $t = $title->getPrefixedDBkey();
1337 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: "
1338 . "model is $model , default for $t is $defaultModel" );
1341 if ( $this->getContentFormat() != $defaultFormat ) {
1342 $t = $title->getPrefixedDBkey();
1344 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: "
1345 . "format is $format, default for $t is $defaultFormat" );
1349 $content = $this->getContent( Revision
::RAW
);
1351 if ( !$content->isValid() ) {
1352 $t = $title->getPrefixedDBkey();
1354 throw new MWException( "Content of $t is not valid! Content model is $model" );
1359 * Get the base 36 SHA-1 value for a string of text
1360 * @param $text String
1363 public static function base36Sha1( $text ) {
1364 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1368 * Lazy-load the revision's text.
1369 * Currently hardcoded to the 'text' table storage engine.
1373 protected function loadText() {
1374 wfProfileIn( __METHOD__
);
1376 // Caching may be beneficial for massive use of external storage
1377 global $wgRevisionCacheExpiry, $wgMemc;
1378 $textId = $this->getTextId();
1379 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1380 if( $wgRevisionCacheExpiry ) {
1381 $text = $wgMemc->get( $key );
1382 if( is_string( $text ) ) {
1383 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1384 wfProfileOut( __METHOD__
);
1389 // If we kept data for lazy extraction, use it now...
1390 if ( isset( $this->mTextRow
) ) {
1391 $row = $this->mTextRow
;
1392 $this->mTextRow
= null;
1398 // Text data is immutable; check slaves first.
1399 $dbr = wfGetDB( DB_SLAVE
);
1400 $row = $dbr->selectRow( 'text',
1401 array( 'old_text', 'old_flags' ),
1402 array( 'old_id' => $this->getTextId() ),
1406 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1407 // Possible slave lag!
1408 $dbw = wfGetDB( DB_MASTER
);
1409 $row = $dbw->selectRow( 'text',
1410 array( 'old_text', 'old_flags' ),
1411 array( 'old_id' => $this->getTextId() ),
1415 $text = self
::getRevisionText( $row );
1417 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1418 if( $wgRevisionCacheExpiry && $text !== false ) {
1419 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1422 wfProfileOut( __METHOD__
);
1428 * Create a new null-revision for insertion into a page's
1429 * history. This will not re-save the text, but simply refer
1430 * to the text from the previous version.
1432 * Such revisions can for instance identify page rename
1433 * operations and other such meta-modifications.
1435 * @param $dbw DatabaseBase
1436 * @param $pageId Integer: ID number of the page to read from
1437 * @param $summary String: revision's summary
1438 * @param $minor Boolean: whether the revision should be considered as minor
1439 * @return Revision|null on error
1441 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1442 global $wgContentHandlerUseDB;
1444 wfProfileIn( __METHOD__
);
1446 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1447 'rev_text_id', 'rev_len', 'rev_sha1' );
1449 if ( $wgContentHandlerUseDB ) {
1450 $fields[] = 'rev_content_model';
1451 $fields[] = 'rev_content_format';
1454 $current = $dbw->selectRow(
1455 array( 'page', 'revision' ),
1458 'page_id' => $pageId,
1459 'page_latest=rev_id',
1466 'comment' => $summary,
1467 'minor_edit' => $minor,
1468 'text_id' => $current->rev_text_id
,
1469 'parent_id' => $current->page_latest
,
1470 'len' => $current->rev_len
,
1471 'sha1' => $current->rev_sha1
1474 if ( $wgContentHandlerUseDB ) {
1475 $row[ 'content_model' ] = $current->rev_content_model
;
1476 $row[ 'content_format' ] = $current->rev_content_format
;
1479 $revision = new Revision( $row );
1480 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1485 wfProfileOut( __METHOD__
);
1490 * Determine if the current user is allowed to view a particular
1491 * field of this revision, if it's marked as deleted.
1493 * @param $field Integer:one of self::DELETED_TEXT,
1494 * self::DELETED_COMMENT,
1495 * self::DELETED_USER
1496 * @param $user User object to check, or null to use $wgUser
1499 public function userCan( $field, User
$user = null ) {
1500 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1504 * Determine if the current user is allowed to view a particular
1505 * field of this revision, if it's marked as deleted. This is used
1506 * by various classes to avoid duplication.
1508 * @param $bitfield Integer: current field
1509 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1510 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1511 * self::DELETED_USER = File::DELETED_USER
1512 * @param $user User object to check, or null to use $wgUser
1515 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1516 if( $bitfield & $field ) { // aspect is deleted
1517 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1518 $permission = 'suppressrevision';
1519 } elseif ( $field & self
::DELETED_TEXT
) {
1520 $permission = 'deletedtext';
1522 $permission = 'deletedhistory';
1524 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1525 if ( $user === null ) {
1529 return $user->isAllowed( $permission );
1536 * Get rev_timestamp from rev_id, without loading the rest of the row
1538 * @param $title Title
1539 * @param $id Integer
1542 static function getTimestampFromId( $title, $id ) {
1543 $dbr = wfGetDB( DB_SLAVE
);
1544 // Casting fix for DB2
1548 $conds = array( 'rev_id' => $id );
1549 $conds['rev_page'] = $title->getArticleID();
1550 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1551 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1552 # Not in slave, try master
1553 $dbw = wfGetDB( DB_MASTER
);
1554 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1556 return wfTimestamp( TS_MW
, $timestamp );
1560 * Get count of revisions per page...not very efficient
1562 * @param $db DatabaseBase
1563 * @param $id Integer: page id
1566 static function countByPageId( $db, $id ) {
1567 $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
1568 array( 'rev_page' => $id ), __METHOD__
);
1570 return $row->revCount
;
1576 * Get count of revisions per page...not very efficient
1578 * @param $db DatabaseBase
1579 * @param $title Title
1582 static function countByTitle( $db, $title ) {
1583 $id = $title->getArticleID();
1585 return self
::countByPageId( $db, $id );
1591 * Check if no edits were made by other users since
1592 * the time a user started editing the page. Limit to
1593 * 50 revisions for the sake of performance.
1597 * @param DatabaseBase|int $db the Database to perform the check on. May be given as a Database object or
1598 * a database identifier usable with wfGetDB.
1599 * @param int $pageId the ID of the page in question
1600 * @param int $userId the ID of the user in question
1601 * @param string $since look at edits since this time
1603 * @return bool True if the given user was the only one to edit since the given timestamp
1605 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1606 if ( !$userId ) return false;
1608 if ( is_int( $db ) ) {
1609 $db = wfGetDB( $db );
1612 $res = $db->select( 'revision',
1615 'rev_page' => $pageId,
1616 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1619 array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
1620 foreach ( $res as $row ) {
1621 if ( $row->rev_user
!= $userId ) {