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
{
34 protected $mOrigUserText;
36 protected $mMinorEdit;
37 protected $mTimestamp;
51 protected $mContentModel;
52 protected $mContentFormat;
60 * @var null|ContentHandler
62 protected $mContentHandler;
64 // Revision deletion constants
65 const DELETED_TEXT
= 1;
66 const DELETED_COMMENT
= 2;
67 const DELETED_USER
= 4;
68 const DELETED_RESTRICTED
= 8;
69 const SUPPRESSED_USER
= 12; // convenience
71 // Audience options for accessors
73 const FOR_THIS_USER
= 2;
77 * Load a page revision from a given revision ID number.
78 * Returns null if no such revision can be found.
81 * Revision::READ_LATEST : Select the data from the master
82 * Revision::READ_LOCKING : Select & lock the data from the master
85 * @param $flags Integer (optional)
86 * @return Revision or null
88 public static function newFromId( $id, $flags = 0 ) {
89 return self
::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
93 * Load either the current, or a specified, revision
94 * that's attached to a given title. If not attached
95 * to that title, will return null.
98 * Revision::READ_LATEST : Select the data from the master
99 * Revision::READ_LOCKING : Select & lock the data from the master
101 * @param $title Title
102 * @param $id Integer (optional)
103 * @param $flags Integer Bitfield (optional)
104 * @return Revision or null
106 public static function newFromTitle( $title, $id = 0, $flags = 0 ) {
108 'page_namespace' => $title->getNamespace(),
109 'page_title' => $title->getDBkey()
112 // Use the specified ID
113 $conds['rev_id'] = $id;
115 // Use a join to get the latest revision
116 $conds[] = 'rev_id=page_latest';
118 return self
::newFromConds( $conds, (int)$flags );
122 * Load either the current, or a specified, revision
123 * that's attached to a given page ID.
124 * Returns null if no such revision can be found.
127 * Revision::READ_LATEST : Select the data from the master (since 1.20)
128 * Revision::READ_LOCKING : Select & lock the data from the master
130 * @param $revId Integer
131 * @param $pageId Integer (optional)
132 * @param $flags Integer Bitfield (optional)
133 * @return Revision or null
135 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
136 $conds = array( 'page_id' => $pageId );
138 $conds['rev_id'] = $revId;
140 // Use a join to get the latest revision
141 $conds[] = 'rev_id = page_latest';
143 return self
::newFromConds( $conds, (int)$flags );
147 * Make a fake revision object from an archive table row. This is queried
148 * for permissions or even inserted (as in Special:Undelete)
149 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
152 * @param $overrides array
154 * @throws MWException
157 public static function newFromArchiveRow( $row, $overrides = array() ) {
158 global $wgContentHandlerUseDB;
160 $attribs = $overrides +
array(
161 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
162 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
163 'comment' => $row->ar_comment
,
164 'user' => $row->ar_user
,
165 'user_text' => $row->ar_user_text
,
166 'timestamp' => $row->ar_timestamp
,
167 'minor_edit' => $row->ar_minor_edit
,
168 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
169 'deleted' => $row->ar_deleted
,
170 'len' => $row->ar_len
,
171 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
172 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
173 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
176 if ( !$wgContentHandlerUseDB ) {
177 unset( $attribs['content_model'] );
178 unset( $attribs['content_format'] );
181 if ( !isset( $attribs['title'] )
182 && isset( $row->ar_namespace
)
183 && isset( $row->ar_title
) ) {
185 $attribs['title'] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
188 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
189 // Pre-1.5 ar_text row
190 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
191 if ( $attribs['text'] === false ) {
192 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
195 return new self( $attribs );
204 public static function newFromRow( $row ) {
205 return new self( $row );
209 * Load a page revision from a given revision ID number.
210 * Returns null if no such revision can be found.
212 * @param $db DatabaseBase
214 * @return Revision or null
216 public static function loadFromId( $db, $id ) {
217 return self
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
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 $pageid Integer
228 * @return Revision or null
230 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
231 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
233 $conds['rev_id'] = intval( $id );
235 $conds[] = 'rev_id=page_latest';
237 return self
::loadFromConds( $db, $conds );
241 * Load either the current, or a specified, revision
242 * that's attached to a given page. If not attached
243 * to that page, will return null.
245 * @param $db DatabaseBase
246 * @param $title Title
248 * @return Revision or null
250 public static function loadFromTitle( $db, $title, $id = 0 ) {
252 $matchId = intval( $id );
254 $matchId = 'page_latest';
256 return self
::loadFromConds( $db,
257 array( "rev_id=$matchId",
258 'page_namespace' => $title->getNamespace(),
259 'page_title' => $title->getDBkey() )
264 * Load the revision for the given title with the given timestamp.
265 * WARNING: Timestamps may in some circumstances not be unique,
266 * so this isn't the best key to use.
268 * @param $db DatabaseBase
269 * @param $title Title
270 * @param $timestamp String
271 * @return Revision or null
273 public static function loadFromTimestamp( $db, $title, $timestamp ) {
274 return self
::loadFromConds( $db,
275 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
276 'page_namespace' => $title->getNamespace(),
277 'page_title' => $title->getDBkey() )
282 * Given a set of conditions, fetch a revision.
284 * @param $conditions Array
285 * @param $flags integer (optional)
286 * @return Revision or null
288 private static function newFromConds( $conditions, $flags = 0 ) {
289 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_SLAVE
);
290 $rev = self
::loadFromConds( $db, $conditions, $flags );
291 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
292 if ( !( $flags & self
::READ_LATEST
) ) {
293 $dbw = wfGetDB( DB_MASTER
);
294 $rev = self
::loadFromConds( $dbw, $conditions, $flags );
301 * Given a set of conditions, fetch a revision from
302 * the given database connection.
304 * @param $db DatabaseBase
305 * @param $conditions Array
306 * @param $flags integer (optional)
307 * @return Revision or null
309 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
310 $res = self
::fetchFromConds( $db, $conditions, $flags );
312 $row = $res->fetchObject();
314 $ret = new Revision( $row );
323 * Return a wrapper for a series of database rows to
324 * fetch all of a given page's revisions in turn.
325 * Each row can be fed to the constructor to get objects.
327 * @param $title Title
328 * @return ResultWrapper
330 public static function fetchRevision( $title ) {
331 return self
::fetchFromConds(
333 array( 'rev_id=page_latest',
334 'page_namespace' => $title->getNamespace(),
335 'page_title' => $title->getDBkey() )
340 * Given a set of conditions, return a ResultWrapper
341 * which will return matching database rows with the
342 * fields necessary to build Revision objects.
344 * @param $db DatabaseBase
345 * @param $conditions Array
346 * @param $flags integer (optional)
347 * @return ResultWrapper
349 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
350 $fields = array_merge(
351 self
::selectFields(),
352 self
::selectPageFields(),
353 self
::selectUserFields()
355 $options = array( 'LIMIT' => 1 );
356 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
357 $options[] = 'FOR UPDATE';
360 array( 'revision', 'page', 'user' ),
365 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
370 * Return the value of a select() JOIN conds array for the user table.
371 * This will get user table rows for logged-in users.
375 public static function userJoinCond() {
376 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
380 * Return the value of a select() page conds array for the paeg table.
381 * This will assure that the revision(s) are not orphaned from live pages.
385 public static function pageJoinCond() {
386 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
390 * Return the list of revision fields that should be selected to create
394 public static function selectFields() {
395 global $wgContentHandlerUseDB;
412 if ( $wgContentHandlerUseDB ) {
413 $fields[] = 'rev_content_format';
414 $fields[] = 'rev_content_model';
421 * Return the list of text fields that should be selected to read the
425 public static function selectTextFields() {
433 * Return the list of page fields that should be selected from page table
436 public static function selectPageFields() {
448 * Return the list of user fields that should be selected from user table
451 public static function selectUserFields() {
452 return array( 'user_name' );
456 * Do a batched query to get the parent revision lengths
457 * @param $db DatabaseBase
458 * @param $revIds Array
461 public static function getParentLengths( $db, array $revIds ) {
464 return $revLens; // empty
466 wfProfileIn( __METHOD__
);
467 $res = $db->select( 'revision',
468 array( 'rev_id', 'rev_len' ),
469 array( 'rev_id' => $revIds ),
471 foreach ( $res as $row ) {
472 $revLens[$row->rev_id
] = $row->rev_len
;
474 wfProfileOut( __METHOD__
);
481 * @param $row Mixed: either a database row or an array
482 * @throws MWException
485 function __construct( $row ) {
486 if( is_object( $row ) ) {
487 $this->mId
= intval( $row->rev_id
);
488 $this->mPage
= intval( $row->rev_page
);
489 $this->mTextId
= intval( $row->rev_text_id
);
490 $this->mComment
= $row->rev_comment
;
491 $this->mUser
= intval( $row->rev_user
);
492 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
493 $this->mTimestamp
= $row->rev_timestamp
;
494 $this->mDeleted
= intval( $row->rev_deleted
);
496 if( !isset( $row->rev_parent_id
) ) {
497 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
499 $this->mParentId
= intval( $row->rev_parent_id
);
502 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
505 $this->mSize
= intval( $row->rev_len
);
508 if ( !isset( $row->rev_sha1
) ) {
511 $this->mSha1
= $row->rev_sha1
;
514 if( isset( $row->page_latest
) ) {
515 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
516 $this->mTitle
= Title
::newFromRow( $row );
518 $this->mCurrent
= false;
519 $this->mTitle
= null;
522 if( !isset( $row->rev_content_model
) ||
is_null( $row->rev_content_model
) ) {
523 $this->mContentModel
= null; # determine on demand if needed
525 $this->mContentModel
= strval( $row->rev_content_model
);
528 if( !isset( $row->rev_content_format
) ||
is_null( $row->rev_content_format
) ) {
529 $this->mContentFormat
= null; # determine on demand if needed
531 $this->mContentFormat
= strval( $row->rev_content_format
);
534 // Lazy extraction...
536 if( isset( $row->old_text
) ) {
537 $this->mTextRow
= $row;
539 // 'text' table row entry will be lazy-loaded
540 $this->mTextRow
= null;
543 // Use user_name for users and rev_user_text for IPs...
544 $this->mUserText
= null; // lazy load if left null
545 if ( $this->mUser
== 0 ) {
546 $this->mUserText
= $row->rev_user_text
; // IP user
547 } elseif ( isset( $row->user_name
) ) {
548 $this->mUserText
= $row->user_name
; // logged-in user
550 $this->mOrigUserText
= $row->rev_user_text
;
551 } elseif( is_array( $row ) ) {
552 // Build a new revision to be saved...
553 global $wgUser; // ugh
556 # if we have a content object, use it to set the model and type
557 if ( !empty( $row['content'] ) ) {
558 //@todo: when is that set? test with external store setup! check out insertOn() [dk]
559 if ( !empty( $row['text_id'] ) ) {
560 throw new MWException( "Text already stored in external store (id {$row['text_id']}), "
561 . "can't serialize content object" );
564 $row['content_model'] = $row['content']->getModel();
565 # note: mContentFormat is initializes later accordingly
566 # note: content is serialized later in this method!
567 # also set text to null?
570 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
571 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
572 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
573 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
574 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
575 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
576 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
577 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
578 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
579 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
580 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
582 $this->mContentModel
= isset( $row['content_model'] ) ?
strval( $row['content_model'] ) : null;
583 $this->mContentFormat
= isset( $row['content_format'] ) ?
strval( $row['content_format'] ) : null;
585 // Enforce spacing trimming on supplied text
586 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
587 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
588 $this->mTextRow
= null;
590 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
592 // if we have a Content object, override mText and mContentModel
593 if ( !empty( $row['content'] ) ) {
594 if ( !( $row['content'] instanceof Content
) ) {
595 throw new MWException( '`content` field must contain a Content object.' );
598 $handler = $this->getContentHandler();
599 $this->mContent
= $row['content'];
601 $this->mContentModel
= $this->mContent
->getModel();
602 $this->mContentHandler
= null;
604 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
605 } elseif ( !is_null( $this->mText
) ) {
606 $handler = $this->getContentHandler();
607 $this->mContent
= $handler->unserializeContent( $this->mText
);
610 // If we have a Title object, make sure it is consistent with mPage.
611 if ( $this->mTitle
&& $this->mTitle
->exists() ) {
612 if ( $this->mPage
=== null ) {
613 // if the page ID wasn't known, set it now
614 $this->mPage
= $this->mTitle
->getArticleID();
615 } elseif ( $this->mTitle
->getArticleID() !== $this->mPage
) {
616 // Got different page IDs. This may be legit (e.g. during undeletion),
617 // but it seems worth mentioning it in the log.
618 wfDebug( "Page ID " . $this->mPage
. " mismatches the ID "
619 . $this->mTitle
->getArticleID() . " provided by the Title object." );
623 $this->mCurrent
= false;
625 // If we still have no length, see it we have the text to figure it out
626 if ( !$this->mSize
) {
627 if ( !is_null( $this->mContent
) ) {
628 $this->mSize
= $this->mContent
->getSize();
630 #NOTE: this should never happen if we have either text or content object!
636 if ( $this->mSha1
=== null ) {
637 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
641 $this->getContentModel();
642 $this->getContentFormat();
644 throw new MWException( 'Revision constructor passed invalid row format.' );
646 $this->mUnpatrolled
= null;
652 * @return Integer|null
654 public function getId() {
659 * Set the revision ID
664 public function setId( $id ) {
671 * @return Integer|null
673 public function getTextId() {
674 return $this->mTextId
;
678 * Get parent revision ID (the original previous page revision)
680 * @return Integer|null
682 public function getParentId() {
683 return $this->mParentId
;
687 * Returns the length of the text in this revision, or null if unknown.
689 * @return Integer|null
691 public function getSize() {
696 * Returns the base36 sha1 of the text in this revision, or null if unknown.
698 * @return String|null
700 public function getSha1() {
705 * Returns the title of the page associated with this entry or null.
707 * Will do a query, when title is not set and id is given.
711 public function getTitle() {
712 if( isset( $this->mTitle
) ) {
713 return $this->mTitle
;
715 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
716 $dbr = wfGetDB( DB_SLAVE
);
717 $row = $dbr->selectRow(
718 array( 'page', 'revision' ),
719 self
::selectPageFields(),
720 array( 'page_id=rev_page',
721 'rev_id' => $this->mId
),
724 $this->mTitle
= Title
::newFromRow( $row );
728 if ( !$this->mTitle
&& !is_null( $this->mPage
) && $this->mPage
> 0 ) {
729 $this->mTitle
= Title
::newFromID( $this->mPage
);
732 return $this->mTitle
;
736 * Set the title of the revision
738 * @param $title Title
740 public function setTitle( $title ) {
741 $this->mTitle
= $title;
747 * @return Integer|null
749 public function getPage() {
754 * Fetch revision's user id if it's available to the specified audience.
755 * If the specified audience does not have access to it, zero will be
758 * @param $audience Integer: one of:
759 * Revision::FOR_PUBLIC to be displayed to all users
760 * Revision::FOR_THIS_USER to be displayed to the given user
761 * Revision::RAW get the ID regardless of permissions
762 * @param $user User object to check for, only if FOR_THIS_USER is passed
763 * to the $audience parameter
766 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
767 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
769 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
777 * Fetch revision's user id without regard for the current user's permissions
781 public function getRawUser() {
786 * Fetch revision's username if it's available to the specified audience.
787 * If the specified audience does not have access to the username, an
788 * empty string will be returned.
790 * @param $audience Integer: one of:
791 * Revision::FOR_PUBLIC to be displayed to all users
792 * Revision::FOR_THIS_USER to be displayed to the given user
793 * Revision::RAW get the text regardless of permissions
794 * @param $user User object to check for, only if FOR_THIS_USER is passed
795 * to the $audience parameter
798 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
799 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
801 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
804 return $this->getRawUserText();
809 * Fetch revision's username without regard for view restrictions
813 public function getRawUserText() {
814 if ( $this->mUserText
=== null ) {
815 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
816 if ( $this->mUserText
=== false ) {
817 # This shouldn't happen, but it can if the wiki was recovered
818 # via importing revs and there is no user table entry yet.
819 $this->mUserText
= $this->mOrigUserText
;
822 return $this->mUserText
;
826 * Fetch revision comment if it's available to the specified audience.
827 * If the specified audience does not have access to the comment, an
828 * empty string will be returned.
830 * @param $audience Integer: one of:
831 * Revision::FOR_PUBLIC to be displayed to all users
832 * Revision::FOR_THIS_USER to be displayed to the given user
833 * Revision::RAW get the text regardless of permissions
834 * @param $user User object to check for, only if FOR_THIS_USER is passed
835 * to the $audience parameter
838 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
839 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
841 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
844 return $this->mComment
;
849 * Fetch revision comment without regard for the current user's permissions
853 public function getRawComment() {
854 return $this->mComment
;
860 public function isMinor() {
861 return (bool)$this->mMinorEdit
;
865 * @return Integer rcid of the unpatrolled row, zero if there isn't one
867 public function isUnpatrolled() {
868 if( $this->mUnpatrolled
!== null ) {
869 return $this->mUnpatrolled
;
871 $dbr = wfGetDB( DB_SLAVE
);
872 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
874 array( // Add redundant user,timestamp condition so we can use the existing index
875 'rc_user_text' => $this->getRawUserText(),
876 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
877 'rc_this_oldid' => $this->getId(),
882 return (int)$this->mUnpatrolled
;
886 * @param $field int one of DELETED_* bitfield constants
890 public function isDeleted( $field ) {
891 return ( $this->mDeleted
& $field ) == $field;
895 * Get the deletion bitfield of the revision
899 public function getVisibility() {
900 return (int)$this->mDeleted
;
904 * Fetch revision text if it's available to the specified audience.
905 * If the specified audience does not have the ability to view this
906 * revision, an empty string will be returned.
908 * @param $audience Integer: one of:
909 * Revision::FOR_PUBLIC to be displayed to all users
910 * Revision::FOR_THIS_USER to be displayed to the given user
911 * Revision::RAW get the text regardless of permissions
912 * @param $user User object to check for, only if FOR_THIS_USER is passed
913 * to the $audience parameter
915 * @deprecated in 1.21, use getContent() instead
916 * @todo: replace usage in core
919 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
920 ContentHandler
::deprecated( __METHOD__
, '1.21' );
922 $content = $this->getContent( $audience, $user );
923 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
927 * Fetch revision content if it's available to the specified audience.
928 * If the specified audience does not have the ability to view this
929 * revision, null will be returned.
931 * @param $audience Integer: one of:
932 * Revision::FOR_PUBLIC to be displayed to all users
933 * Revision::FOR_THIS_USER to be displayed to $wgUser
934 * Revision::RAW get the text regardless of permissions
935 * @param $user User object to check for, only if FOR_THIS_USER is passed
936 * to the $audience parameter
938 * @return Content|null
940 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
941 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
943 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
946 return $this->getContentInternal();
951 * Alias for getText(Revision::FOR_THIS_USER)
953 * @deprecated since 1.17
956 public function revText() {
957 wfDeprecated( __METHOD__
, '1.17' );
958 return $this->getText( self
::FOR_THIS_USER
);
962 * Fetch revision text without regard for view restrictions
966 * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW )
967 * or Revision::getSerializedData() as appropriate.
969 public function getRawText() {
970 ContentHandler
::deprecated( __METHOD__
, "1.21" );
971 return $this->getText( self
::RAW
);
975 * Fetch original serialized data without regard for view restrictions
980 public function getSerializedData() {
985 * Gets the content object for the revision
990 protected function getContentInternal() {
991 if( is_null( $this->mContent
) ) {
992 // Revision is immutable. Load on demand:
994 $handler = $this->getContentHandler();
995 $format = $this->getContentFormat();
997 if( is_null( $this->mText
) ) {
998 // Load text on demand:
999 $this->mText
= $this->loadText();
1002 $this->mContent
= is_null( $this->mText
) ?
null : $handler->unserializeContent( $this->mText
, $format );
1005 return $this->mContent
->copy(); // NOTE: copy() will return $this for immutable content objects
1009 * Returns the content model for this revision.
1011 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
1012 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1013 * is used as a last resort.
1015 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
1017 public function getContentModel() {
1018 if ( !$this->mContentModel
) {
1019 $title = $this->getTitle();
1020 $this->mContentModel
= ( $title ?
$title->getContentModel() : CONTENT_MODEL_WIKITEXT
);
1022 assert( !empty( $this->mContentModel
) );
1025 return $this->mContentModel
;
1029 * Returns the content format for this revision.
1031 * If no content format was stored in the database, the default format for this
1032 * revision's content model is returned.
1034 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1036 public function getContentFormat() {
1037 if ( !$this->mContentFormat
) {
1038 $handler = $this->getContentHandler();
1039 $this->mContentFormat
= $handler->getDefaultFormat();
1041 assert( !empty( $this->mContentFormat
) );
1044 return $this->mContentFormat
;
1048 * Returns the content handler appropriate for this revision's content model.
1050 * @throws MWException
1051 * @return ContentHandler
1053 public function getContentHandler() {
1054 if ( !$this->mContentHandler
) {
1055 $model = $this->getContentModel();
1056 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1058 $format = $this->getContentFormat();
1060 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1061 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1065 return $this->mContentHandler
;
1071 public function getTimestamp() {
1072 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1078 public function isCurrent() {
1079 return $this->mCurrent
;
1083 * Get previous revision for this title
1085 * @return Revision or null
1087 public function getPrevious() {
1088 if( $this->getTitle() ) {
1089 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1091 return self
::newFromTitle( $this->getTitle(), $prev );
1098 * Get next revision for this title
1100 * @return Revision or null
1102 public function getNext() {
1103 if( $this->getTitle() ) {
1104 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1106 return self
::newFromTitle( $this->getTitle(), $next );
1113 * Get previous revision Id for this page_id
1114 * This is used to populate rev_parent_id on save
1116 * @param $db DatabaseBase
1119 private function getPreviousRevisionId( $db ) {
1120 if( is_null( $this->mPage
) ) {
1123 # Use page_latest if ID is not given
1125 $prevId = $db->selectField( 'page', 'page_latest',
1126 array( 'page_id' => $this->mPage
),
1129 $prevId = $db->selectField( 'revision', 'rev_id',
1130 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
1132 array( 'ORDER BY' => 'rev_id DESC' ) );
1134 return intval( $prevId );
1138 * Get revision text associated with an old or archive row
1139 * $row is usually an object from wfFetchRow(), both the flags and the text
1140 * field must be included
1142 * @param $row Object: the text data
1143 * @param $prefix String: table prefix (default 'old_')
1144 * @return String: text the text requested or false on failure
1146 public static function getRevisionText( $row, $prefix = 'old_' ) {
1147 wfProfileIn( __METHOD__
);
1150 $textField = $prefix . 'text';
1151 $flagsField = $prefix . 'flags';
1153 if( isset( $row->$flagsField ) ) {
1154 $flags = explode( ',', $row->$flagsField );
1159 if( isset( $row->$textField ) ) {
1160 $text = $row->$textField;
1162 wfProfileOut( __METHOD__
);
1166 # Use external methods for external objects, text in table is URL-only then
1167 if ( in_array( 'external', $flags ) ) {
1169 $parts = explode( '://', $url, 2 );
1170 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
1171 wfProfileOut( __METHOD__
);
1174 $text = ExternalStore
::fetchFromURL( $url );
1177 // If the text was fetched without an error, convert it
1178 if ( $text !== false ) {
1179 if( in_array( 'gzip', $flags ) ) {
1180 # Deal with optional compression of archived pages.
1181 # This can be done periodically via maintenance/compressOld.php, and
1182 # as pages are saved if $wgCompressRevisions is set.
1183 $text = gzinflate( $text );
1186 if( in_array( 'object', $flags ) ) {
1187 # Generic compressed storage
1188 $obj = unserialize( $text );
1189 if ( !is_object( $obj ) ) {
1191 wfProfileOut( __METHOD__
);
1194 $text = $obj->getText();
1197 global $wgLegacyEncoding;
1198 if( $text !== false && $wgLegacyEncoding
1199 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1201 # Old revisions kept around in a legacy encoding?
1202 # Upconvert on demand.
1203 # ("utf8" checked for compatibility with some broken
1204 # conversion scripts 2008-12-30)
1206 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1209 wfProfileOut( __METHOD__
);
1214 * If $wgCompressRevisions is enabled, we will compress data.
1215 * The input string is modified in place.
1216 * Return value is the flags field: contains 'gzip' if the
1217 * data is compressed, and 'utf-8' if we're saving in UTF-8
1220 * @param $text Mixed: reference to a text
1223 public static function compressRevisionText( &$text ) {
1224 global $wgCompressRevisions;
1227 # Revisions not marked this way will be converted
1228 # on load if $wgLegacyCharset is set in the future.
1231 if( $wgCompressRevisions ) {
1232 if( function_exists( 'gzdeflate' ) ) {
1233 $text = gzdeflate( $text );
1236 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1239 return implode( ',', $flags );
1243 * Insert a new revision into the database, returning the new revision ID
1244 * number on success and dies horribly on failure.
1246 * @param $dbw DatabaseBase: (master connection)
1247 * @throws MWException
1250 public function insertOn( $dbw ) {
1251 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1253 wfProfileIn( __METHOD__
);
1255 $this->checkContentModel();
1257 $data = $this->mText
;
1258 $flags = self
::compressRevisionText( $data );
1260 # Write to external storage if required
1261 if( $wgDefaultExternalStore ) {
1262 // Store and get the URL
1263 $data = ExternalStore
::insertToDefault( $data );
1265 throw new MWException( "Unable to store text to external storage" );
1270 $flags .= 'external';
1273 # Record the text (or external storage URL) to the text table
1274 if( !isset( $this->mTextId
) ) {
1275 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1276 $dbw->insert( 'text',
1278 'old_id' => $old_id,
1279 'old_text' => $data,
1280 'old_flags' => $flags,
1283 $this->mTextId
= $dbw->insertId();
1286 if ( $this->mComment
=== null ) $this->mComment
= "";
1288 # Record the edit in revisions
1289 $rev_id = isset( $this->mId
)
1291 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1293 'rev_id' => $rev_id,
1294 'rev_page' => $this->mPage
,
1295 'rev_text_id' => $this->mTextId
,
1296 'rev_comment' => $this->mComment
,
1297 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1298 'rev_user' => $this->mUser
,
1299 'rev_user_text' => $this->mUserText
,
1300 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1301 'rev_deleted' => $this->mDeleted
,
1302 'rev_len' => $this->mSize
,
1303 'rev_parent_id' => is_null( $this->mParentId
)
1304 ?
$this->getPreviousRevisionId( $dbw )
1306 'rev_sha1' => is_null( $this->mSha1
)
1307 ? Revision
::base36Sha1( $this->mText
)
1311 if ( $wgContentHandlerUseDB ) {
1312 //NOTE: Store null for the default model and format, to save space.
1313 //XXX: Makes the DB sensitive to changed defaults. Make this behaviour optional? Only in miser mode?
1315 $model = $this->getContentModel();
1316 $format = $this->getContentFormat();
1318 $title = $this->getTitle();
1320 if ( $title === null ) {
1321 throw new MWException( "Insufficient information to determine the title of the revision's page!" );
1324 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1325 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1327 $row[ 'rev_content_model' ] = ( $model === $defaultModel ) ?
null : $model;
1328 $row[ 'rev_content_format' ] = ( $format === $defaultFormat ) ?
null : $format;
1331 $dbw->insert( 'revision', $row, __METHOD__
);
1333 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1335 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1337 wfProfileOut( __METHOD__
);
1341 protected function checkContentModel() {
1342 global $wgContentHandlerUseDB;
1344 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1346 $model = $this->getContentModel();
1347 $format = $this->getContentFormat();
1348 $handler = $this->getContentHandler();
1350 if ( !$handler->isSupportedFormat( $format ) ) {
1351 $t = $title->getPrefixedDBkey();
1353 throw new MWException( "Can't use format $format with content model $model on $t" );
1356 if ( !$wgContentHandlerUseDB && $title ) {
1357 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1359 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1360 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1361 $defaultFormat = $defaultHandler->getDefaultFormat();
1363 if ( $this->getContentModel() != $defaultModel ) {
1364 $t = $title->getPrefixedDBkey();
1366 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: "
1367 . "model is $model , default for $t is $defaultModel" );
1370 if ( $this->getContentFormat() != $defaultFormat ) {
1371 $t = $title->getPrefixedDBkey();
1373 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: "
1374 . "format is $format, default for $t is $defaultFormat" );
1378 $content = $this->getContent( Revision
::RAW
);
1380 if ( !$content->isValid() ) {
1381 $t = $title->getPrefixedDBkey();
1383 throw new MWException( "Content of $t is not valid! Content model is $model" );
1388 * Get the base 36 SHA-1 value for a string of text
1389 * @param $text String
1392 public static function base36Sha1( $text ) {
1393 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1397 * Lazy-load the revision's text.
1398 * Currently hardcoded to the 'text' table storage engine.
1402 protected function loadText() {
1403 wfProfileIn( __METHOD__
);
1405 // Caching may be beneficial for massive use of external storage
1406 global $wgRevisionCacheExpiry, $wgMemc;
1407 $textId = $this->getTextId();
1408 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1409 if( $wgRevisionCacheExpiry ) {
1410 $text = $wgMemc->get( $key );
1411 if( is_string( $text ) ) {
1412 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1413 wfProfileOut( __METHOD__
);
1418 // If we kept data for lazy extraction, use it now...
1419 if ( isset( $this->mTextRow
) ) {
1420 $row = $this->mTextRow
;
1421 $this->mTextRow
= null;
1427 // Text data is immutable; check slaves first.
1428 $dbr = wfGetDB( DB_SLAVE
);
1429 $row = $dbr->selectRow( 'text',
1430 array( 'old_text', 'old_flags' ),
1431 array( 'old_id' => $this->getTextId() ),
1435 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1436 // Possible slave lag!
1437 $dbw = wfGetDB( DB_MASTER
);
1438 $row = $dbw->selectRow( 'text',
1439 array( 'old_text', 'old_flags' ),
1440 array( 'old_id' => $this->getTextId() ),
1444 $text = self
::getRevisionText( $row );
1446 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1447 if( $wgRevisionCacheExpiry && $text !== false ) {
1448 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1451 wfProfileOut( __METHOD__
);
1457 * Create a new null-revision for insertion into a page's
1458 * history. This will not re-save the text, but simply refer
1459 * to the text from the previous version.
1461 * Such revisions can for instance identify page rename
1462 * operations and other such meta-modifications.
1464 * @param $dbw DatabaseBase
1465 * @param $pageId Integer: ID number of the page to read from
1466 * @param $summary String: revision's summary
1467 * @param $minor Boolean: whether the revision should be considered as minor
1468 * @return Revision|null on error
1470 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1471 global $wgContentHandlerUseDB;
1473 wfProfileIn( __METHOD__
);
1475 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1476 'rev_text_id', 'rev_len', 'rev_sha1' );
1478 if ( $wgContentHandlerUseDB ) {
1479 $fields[] = 'rev_content_model';
1480 $fields[] = 'rev_content_format';
1483 $current = $dbw->selectRow(
1484 array( 'page', 'revision' ),
1487 'page_id' => $pageId,
1488 'page_latest=rev_id',
1495 'comment' => $summary,
1496 'minor_edit' => $minor,
1497 'text_id' => $current->rev_text_id
,
1498 'parent_id' => $current->page_latest
,
1499 'len' => $current->rev_len
,
1500 'sha1' => $current->rev_sha1
1503 if ( $wgContentHandlerUseDB ) {
1504 $row[ 'content_model' ] = $current->rev_content_model
;
1505 $row[ 'content_format' ] = $current->rev_content_format
;
1508 $revision = new Revision( $row );
1509 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1514 wfProfileOut( __METHOD__
);
1519 * Determine if the current user is allowed to view a particular
1520 * field of this revision, if it's marked as deleted.
1522 * @param $field Integer:one of self::DELETED_TEXT,
1523 * self::DELETED_COMMENT,
1524 * self::DELETED_USER
1525 * @param $user User object to check, or null to use $wgUser
1528 public function userCan( $field, User
$user = null ) {
1529 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1533 * Determine if the current user is allowed to view a particular
1534 * field of this revision, if it's marked as deleted. This is used
1535 * by various classes to avoid duplication.
1537 * @param $bitfield Integer: current field
1538 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1539 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1540 * self::DELETED_USER = File::DELETED_USER
1541 * @param $user User object to check, or null to use $wgUser
1544 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1545 if( $bitfield & $field ) { // aspect is deleted
1546 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1547 $permission = 'suppressrevision';
1548 } elseif ( $field & self
::DELETED_TEXT
) {
1549 $permission = 'deletedtext';
1551 $permission = 'deletedhistory';
1553 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1554 if ( $user === null ) {
1558 return $user->isAllowed( $permission );
1565 * Get rev_timestamp from rev_id, without loading the rest of the row
1567 * @param $title Title
1568 * @param $id Integer
1571 static function getTimestampFromId( $title, $id ) {
1572 $dbr = wfGetDB( DB_SLAVE
);
1573 // Casting fix for DB2
1577 $conds = array( 'rev_id' => $id );
1578 $conds['rev_page'] = $title->getArticleID();
1579 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1580 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1581 # Not in slave, try master
1582 $dbw = wfGetDB( DB_MASTER
);
1583 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1585 return wfTimestamp( TS_MW
, $timestamp );
1589 * Get count of revisions per page...not very efficient
1591 * @param $db DatabaseBase
1592 * @param $id Integer: page id
1595 static function countByPageId( $db, $id ) {
1596 $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
1597 array( 'rev_page' => $id ), __METHOD__
);
1599 return $row->revCount
;
1605 * Get count of revisions per page...not very efficient
1607 * @param $db DatabaseBase
1608 * @param $title Title
1611 static function countByTitle( $db, $title ) {
1612 $id = $title->getArticleID();
1614 return self
::countByPageId( $db, $id );
1620 * Check if no edits were made by other users since
1621 * the time a user started editing the page. Limit to
1622 * 50 revisions for the sake of performance.
1626 * @param DatabaseBase|int $db the Database to perform the check on. May be given as a Database object or
1627 * a database identifier usable with wfGetDB.
1628 * @param int $pageId the ID of the page in question
1629 * @param int $userId the ID of the user in question
1630 * @param string $since look at edits since this time
1632 * @return bool True if the given user was the only one to edit since the given timestamp
1634 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1635 if ( !$userId ) return false;
1637 if ( is_int( $db ) ) {
1638 $db = wfGetDB( $db );
1641 $res = $db->select( 'revision',
1644 'rev_page' => $pageId,
1645 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1648 array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
1649 foreach ( $res as $row ) {
1650 if ( $row->rev_user
!= $userId ) {