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
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( $row->ar_text
) && !$row->ar_text_id
) {
182 // Pre-1.5 ar_text row
183 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
184 if ( $attribs['text'] === false ) {
185 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
188 return new self( $attribs );
197 public static function newFromRow( $row ) {
198 return new self( $row );
202 * Load a page revision from a given revision ID number.
203 * Returns null if no such revision can be found.
205 * @param $db DatabaseBase
207 * @return Revision or null
209 public static function loadFromId( $db, $id ) {
210 return self
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
214 * Load either the current, or a specified, revision
215 * that's attached to a given page. If not attached
216 * to that page, will return null.
218 * @param $db DatabaseBase
219 * @param $pageid Integer
221 * @return Revision or null
223 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
224 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
226 $conds['rev_id'] = intval( $id );
228 $conds[] = 'rev_id=page_latest';
230 return self
::loadFromConds( $db, $conds );
234 * Load either the current, or a specified, revision
235 * that's attached to a given page. If not attached
236 * to that page, will return null.
238 * @param $db DatabaseBase
239 * @param $title Title
241 * @return Revision or null
243 public static function loadFromTitle( $db, $title, $id = 0 ) {
245 $matchId = intval( $id );
247 $matchId = 'page_latest';
249 return self
::loadFromConds( $db,
250 array( "rev_id=$matchId",
251 'page_namespace' => $title->getNamespace(),
252 'page_title' => $title->getDBkey() )
257 * Load the revision for the given title with the given timestamp.
258 * WARNING: Timestamps may in some circumstances not be unique,
259 * so this isn't the best key to use.
261 * @param $db DatabaseBase
262 * @param $title Title
263 * @param $timestamp String
264 * @return Revision or null
266 public static function loadFromTimestamp( $db, $title, $timestamp ) {
267 return self
::loadFromConds( $db,
268 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
269 'page_namespace' => $title->getNamespace(),
270 'page_title' => $title->getDBkey() )
275 * Given a set of conditions, fetch a revision.
277 * @param $conditions Array
278 * @param $flags integer (optional)
279 * @return Revision or null
281 private static function newFromConds( $conditions, $flags = 0 ) {
282 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_SLAVE
);
283 $rev = self
::loadFromConds( $db, $conditions, $flags );
284 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
285 if ( !( $flags & self
::READ_LATEST
) ) {
286 $dbw = wfGetDB( DB_MASTER
);
287 $rev = self
::loadFromConds( $dbw, $conditions, $flags );
294 * Given a set of conditions, fetch a revision from
295 * the given database connection.
297 * @param $db DatabaseBase
298 * @param $conditions Array
299 * @param $flags integer (optional)
300 * @return Revision or null
302 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
303 $res = self
::fetchFromConds( $db, $conditions, $flags );
305 $row = $res->fetchObject();
307 $ret = new Revision( $row );
316 * Return a wrapper for a series of database rows to
317 * fetch all of a given page's revisions in turn.
318 * Each row can be fed to the constructor to get objects.
320 * @param $title Title
321 * @return ResultWrapper
323 public static function fetchRevision( $title ) {
324 return self
::fetchFromConds(
326 array( 'rev_id=page_latest',
327 'page_namespace' => $title->getNamespace(),
328 'page_title' => $title->getDBkey() )
333 * Given a set of conditions, return a ResultWrapper
334 * which will return matching database rows with the
335 * fields necessary to build Revision objects.
337 * @param $db DatabaseBase
338 * @param $conditions Array
339 * @param $flags integer (optional)
340 * @return ResultWrapper
342 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
343 $fields = array_merge(
344 self
::selectFields(),
345 self
::selectPageFields(),
346 self
::selectUserFields()
348 $options = array( 'LIMIT' => 1 );
349 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
350 $options[] = 'FOR UPDATE';
353 array( 'revision', 'page', 'user' ),
358 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
363 * Return the value of a select() JOIN conds array for the user table.
364 * This will get user table rows for logged-in users.
368 public static function userJoinCond() {
369 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
373 * Return the value of a select() page conds array for the paeg table.
374 * This will assure that the revision(s) are not orphaned from live pages.
378 public static function pageJoinCond() {
379 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
383 * Return the list of revision fields that should be selected to create
387 public static function selectFields() {
388 global $wgContentHandlerUseDB;
405 if ( $wgContentHandlerUseDB ) {
406 $fields[] = 'rev_content_format';
407 $fields[] = 'rev_content_model';
414 * Return the list of text fields that should be selected to read the
418 public static function selectTextFields() {
426 * Return the list of page fields that should be selected from page table
429 public static function selectPageFields() {
441 * Return the list of user fields that should be selected from user table
444 public static function selectUserFields() {
445 return array( 'user_name' );
449 * Do a batched query to get the parent revision lengths
450 * @param $db DatabaseBase
451 * @param $revIds Array
454 public static function getParentLengths( $db, array $revIds ) {
457 return $revLens; // empty
459 wfProfileIn( __METHOD__
);
460 $res = $db->select( 'revision',
461 array( 'rev_id', 'rev_len' ),
462 array( 'rev_id' => $revIds ),
464 foreach ( $res as $row ) {
465 $revLens[$row->rev_id
] = $row->rev_len
;
467 wfProfileOut( __METHOD__
);
474 * @param $row Mixed: either a database row or an array
475 * @throws MWException
478 function __construct( $row ) {
479 if( is_object( $row ) ) {
480 $this->mId
= intval( $row->rev_id
);
481 $this->mPage
= intval( $row->rev_page
);
482 $this->mTextId
= intval( $row->rev_text_id
);
483 $this->mComment
= $row->rev_comment
;
484 $this->mUser
= intval( $row->rev_user
);
485 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
486 $this->mTimestamp
= $row->rev_timestamp
;
487 $this->mDeleted
= intval( $row->rev_deleted
);
489 if( !isset( $row->rev_parent_id
) ) {
490 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
492 $this->mParentId
= intval( $row->rev_parent_id
);
495 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
498 $this->mSize
= intval( $row->rev_len
);
501 if ( !isset( $row->rev_sha1
) ) {
504 $this->mSha1
= $row->rev_sha1
;
507 if( isset( $row->page_latest
) ) {
508 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
509 $this->mTitle
= Title
::newFromRow( $row );
511 $this->mCurrent
= false;
512 $this->mTitle
= null;
515 if( !isset( $row->rev_content_model
) ||
is_null( $row->rev_content_model
) ) {
516 $this->mContentModel
= null; # determine on demand if needed
518 $this->mContentModel
= strval( $row->rev_content_model
);
521 if( !isset( $row->rev_content_format
) ||
is_null( $row->rev_content_format
) ) {
522 $this->mContentFormat
= null; # determine on demand if needed
524 $this->mContentFormat
= strval( $row->rev_content_format
);
527 // Lazy extraction...
529 if( isset( $row->old_text
) ) {
530 $this->mTextRow
= $row;
532 // 'text' table row entry will be lazy-loaded
533 $this->mTextRow
= null;
536 // Use user_name for users and rev_user_text for IPs...
537 $this->mUserText
= null; // lazy load if left null
538 if ( $this->mUser
== 0 ) {
539 $this->mUserText
= $row->rev_user_text
; // IP user
540 } elseif ( isset( $row->user_name
) ) {
541 $this->mUserText
= $row->user_name
; // logged-in user
543 $this->mOrigUserText
= $row->rev_user_text
;
544 } elseif( is_array( $row ) ) {
545 // Build a new revision to be saved...
546 global $wgUser; // ugh
549 # if we have a content object, use it to set the model and type
550 if ( !empty( $row['content'] ) ) {
551 //@todo: when is that set? test with external store setup! check out insertOn() [dk]
552 if ( !empty( $row['text_id'] ) ) {
553 throw new MWException( "Text already stored in external store (id {$row['text_id']}), "
554 . "can't serialize content object" );
557 $row['content_model'] = $row['content']->getModel();
558 # note: mContentFormat is initializes later accordingly
559 # note: content is serialized later in this method!
560 # also set text to null?
563 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
564 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
565 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
566 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
567 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
568 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
569 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
570 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
571 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
572 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
573 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
575 $this->mContentModel
= isset( $row['content_model'] ) ?
strval( $row['content_model'] ) : null;
576 $this->mContentFormat
= isset( $row['content_format'] ) ?
strval( $row['content_format'] ) : null;
578 // Enforce spacing trimming on supplied text
579 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
580 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
581 $this->mTextRow
= null;
583 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
585 // if we have a Content object, override mText and mContentModel
586 if ( !empty( $row['content'] ) ) {
587 if ( !( $row['content'] instanceof Content
) ) {
588 throw new MWException( '`content` field must contain a Content object.' );
591 $handler = $this->getContentHandler();
592 $this->mContent
= $row['content'];
594 $this->mContentModel
= $this->mContent
->getModel();
595 $this->mContentHandler
= null;
597 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
598 } elseif ( !is_null( $this->mText
) ) {
599 $handler = $this->getContentHandler();
600 $this->mContent
= $handler->unserializeContent( $this->mText
);
603 // If we have a Title object, make sure it is consistent with mPage.
604 if ( $this->mTitle
&& $this->mTitle
->exists() ) {
605 if ( $this->mPage
=== null ) {
606 // if the page ID wasn't known, set it now
607 $this->mPage
= $this->mTitle
->getArticleID();
608 } elseif ( $this->mTitle
->getArticleID() !== $this->mPage
) {
609 // Got different page IDs. This may be legit (e.g. during undeletion),
610 // but it seems worth mentioning it in the log.
611 wfDebug( "Page ID " . $this->mPage
. " mismatches the ID "
612 . $this->mTitle
->getArticleID() . " provided by the Title object." );
616 $this->mCurrent
= false;
618 // If we still have no length, see it we have the text to figure it out
619 if ( !$this->mSize
) {
620 if ( !is_null( $this->mContent
) ) {
621 $this->mSize
= $this->mContent
->getSize();
623 #NOTE: this should never happen if we have either text or content object!
629 if ( $this->mSha1
=== null ) {
630 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
634 $this->getContentModel();
635 $this->getContentFormat();
637 throw new MWException( 'Revision constructor passed invalid row format.' );
639 $this->mUnpatrolled
= null;
645 * @return Integer|null
647 public function getId() {
652 * Set the revision ID
657 public function setId( $id ) {
664 * @return Integer|null
666 public function getTextId() {
667 return $this->mTextId
;
671 * Get parent revision ID (the original previous page revision)
673 * @return Integer|null
675 public function getParentId() {
676 return $this->mParentId
;
680 * Returns the length of the text in this revision, or null if unknown.
682 * @return Integer|null
684 public function getSize() {
689 * Returns the base36 sha1 of the text in this revision, or null if unknown.
691 * @return String|null
693 public function getSha1() {
698 * Returns the title of the page associated with this entry or null.
700 * Will do a query, when title is not set and id is given.
704 public function getTitle() {
705 if( isset( $this->mTitle
) ) {
706 return $this->mTitle
;
708 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
709 $dbr = wfGetDB( DB_SLAVE
);
710 $row = $dbr->selectRow(
711 array( 'page', 'revision' ),
712 self
::selectPageFields(),
713 array( 'page_id=rev_page',
714 'rev_id' => $this->mId
),
717 $this->mTitle
= Title
::newFromRow( $row );
721 if ( !$this->mTitle
&& !is_null( $this->mPage
) && $this->mPage
> 0 ) {
722 $this->mTitle
= Title
::newFromID( $this->mPage
);
725 return $this->mTitle
;
729 * Set the title of the revision
731 * @param $title Title
733 public function setTitle( $title ) {
734 $this->mTitle
= $title;
740 * @return Integer|null
742 public function getPage() {
747 * Fetch revision's user id if it's available to the specified audience.
748 * If the specified audience does not have access to it, zero will be
751 * @param $audience Integer: one of:
752 * Revision::FOR_PUBLIC to be displayed to all users
753 * Revision::FOR_THIS_USER to be displayed to the given user
754 * Revision::RAW get the ID regardless of permissions
755 * @param $user User object to check for, only if FOR_THIS_USER is passed
756 * to the $audience parameter
759 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
760 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
762 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
770 * Fetch revision's user id without regard for the current user's permissions
774 public function getRawUser() {
779 * Fetch revision's username if it's available to the specified audience.
780 * If the specified audience does not have access to the username, an
781 * empty string will be returned.
783 * @param $audience Integer: one of:
784 * Revision::FOR_PUBLIC to be displayed to all users
785 * Revision::FOR_THIS_USER to be displayed to the given user
786 * Revision::RAW get the text regardless of permissions
787 * @param $user User object to check for, only if FOR_THIS_USER is passed
788 * to the $audience parameter
791 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
792 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
794 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
797 return $this->getRawUserText();
802 * Fetch revision's username without regard for view restrictions
806 public function getRawUserText() {
807 if ( $this->mUserText
=== null ) {
808 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
809 if ( $this->mUserText
=== false ) {
810 # This shouldn't happen, but it can if the wiki was recovered
811 # via importing revs and there is no user table entry yet.
812 $this->mUserText
= $this->mOrigUserText
;
815 return $this->mUserText
;
819 * Fetch revision comment if it's available to the specified audience.
820 * If the specified audience does not have access to the comment, an
821 * empty string will be returned.
823 * @param $audience Integer: one of:
824 * Revision::FOR_PUBLIC to be displayed to all users
825 * Revision::FOR_THIS_USER to be displayed to the given user
826 * Revision::RAW get the text regardless of permissions
827 * @param $user User object to check for, only if FOR_THIS_USER is passed
828 * to the $audience parameter
831 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
832 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
834 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
837 return $this->mComment
;
842 * Fetch revision comment without regard for the current user's permissions
846 public function getRawComment() {
847 return $this->mComment
;
853 public function isMinor() {
854 return (bool)$this->mMinorEdit
;
858 * @return Integer rcid of the unpatrolled row, zero if there isn't one
860 public function isUnpatrolled() {
861 if( $this->mUnpatrolled
!== null ) {
862 return $this->mUnpatrolled
;
864 $dbr = wfGetDB( DB_SLAVE
);
865 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
867 array( // Add redundant user,timestamp condition so we can use the existing index
868 'rc_user_text' => $this->getRawUserText(),
869 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
870 'rc_this_oldid' => $this->getId(),
875 return (int)$this->mUnpatrolled
;
879 * @param $field int one of DELETED_* bitfield constants
883 public function isDeleted( $field ) {
884 return ( $this->mDeleted
& $field ) == $field;
888 * Get the deletion bitfield of the revision
892 public function getVisibility() {
893 return (int)$this->mDeleted
;
897 * Fetch revision text if it's available to the specified audience.
898 * If the specified audience does not have the ability to view this
899 * revision, an empty string will be returned.
901 * @param $audience Integer: one of:
902 * Revision::FOR_PUBLIC to be displayed to all users
903 * Revision::FOR_THIS_USER to be displayed to the given user
904 * Revision::RAW get the text regardless of permissions
905 * @param $user User object to check for, only if FOR_THIS_USER is passed
906 * to the $audience parameter
908 * @deprecated in 1.21, use getContent() instead
909 * @todo: replace usage in core
912 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
913 ContentHandler
::deprecated( __METHOD__
, '1.21' );
915 $content = $this->getContent( $audience, $user );
916 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
920 * Fetch revision content if it's available to the specified audience.
921 * If the specified audience does not have the ability to view this
922 * revision, null will be returned.
924 * @param $audience Integer: one of:
925 * Revision::FOR_PUBLIC to be displayed to all users
926 * Revision::FOR_THIS_USER to be displayed to $wgUser
927 * Revision::RAW get the text regardless of permissions
928 * @param $user User object to check for, only if FOR_THIS_USER is passed
929 * to the $audience parameter
931 * @return Content|null
933 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
934 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
936 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
939 return $this->getContentInternal();
944 * Alias for getText(Revision::FOR_THIS_USER)
946 * @deprecated since 1.17
949 public function revText() {
950 wfDeprecated( __METHOD__
, '1.17' );
951 return $this->getText( self
::FOR_THIS_USER
);
955 * Fetch revision text without regard for view restrictions
959 * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW )
960 * or Revision::getSerializedData() as appropriate.
962 public function getRawText() {
963 ContentHandler
::deprecated( __METHOD__
, "1.21" );
964 return $this->getText( self
::RAW
);
968 * Fetch original serialized data without regard for view restrictions
973 public function getSerializedData() {
978 * Gets the content object for the revision
983 protected function getContentInternal() {
984 if( is_null( $this->mContent
) ) {
985 // Revision is immutable. Load on demand:
987 $handler = $this->getContentHandler();
988 $format = $this->getContentFormat();
990 if( is_null( $this->mText
) ) {
991 // Load text on demand:
992 $this->mText
= $this->loadText();
995 $this->mContent
= is_null( $this->mText
) ?
null : $handler->unserializeContent( $this->mText
, $format );
998 return $this->mContent
->copy(); // NOTE: copy() will return $this for immutable content objects
1002 * Returns the content model for this revision.
1004 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
1005 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1006 * is used as a last resort.
1008 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
1010 public function getContentModel() {
1011 if ( !$this->mContentModel
) {
1012 $title = $this->getTitle();
1013 $this->mContentModel
= ( $title ?
$title->getContentModel() : CONTENT_MODEL_WIKITEXT
);
1015 assert( !empty( $this->mContentModel
) );
1018 return $this->mContentModel
;
1022 * Returns the content format for this revision.
1024 * If no content format was stored in the database, the default format for this
1025 * revision's content model is returned.
1027 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1029 public function getContentFormat() {
1030 if ( !$this->mContentFormat
) {
1031 $handler = $this->getContentHandler();
1032 $this->mContentFormat
= $handler->getDefaultFormat();
1034 assert( !empty( $this->mContentFormat
) );
1037 return $this->mContentFormat
;
1041 * Returns the content handler appropriate for this revision's content model.
1043 * @throws MWException
1044 * @return ContentHandler
1046 public function getContentHandler() {
1047 if ( !$this->mContentHandler
) {
1048 $model = $this->getContentModel();
1049 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1051 $format = $this->getContentFormat();
1053 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1054 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1058 return $this->mContentHandler
;
1064 public function getTimestamp() {
1065 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1071 public function isCurrent() {
1072 return $this->mCurrent
;
1076 * Get previous revision for this title
1078 * @return Revision or null
1080 public function getPrevious() {
1081 if( $this->getTitle() ) {
1082 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1084 return self
::newFromTitle( $this->getTitle(), $prev );
1091 * Get next revision for this title
1093 * @return Revision or null
1095 public function getNext() {
1096 if( $this->getTitle() ) {
1097 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1099 return self
::newFromTitle( $this->getTitle(), $next );
1106 * Get previous revision Id for this page_id
1107 * This is used to populate rev_parent_id on save
1109 * @param $db DatabaseBase
1112 private function getPreviousRevisionId( $db ) {
1113 if( is_null( $this->mPage
) ) {
1116 # Use page_latest if ID is not given
1118 $prevId = $db->selectField( 'page', 'page_latest',
1119 array( 'page_id' => $this->mPage
),
1122 $prevId = $db->selectField( 'revision', 'rev_id',
1123 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
1125 array( 'ORDER BY' => 'rev_id DESC' ) );
1127 return intval( $prevId );
1131 * Get revision text associated with an old or archive row
1132 * $row is usually an object from wfFetchRow(), both the flags and the text
1133 * field must be included
1135 * @param $row Object: the text data
1136 * @param $prefix String: table prefix (default 'old_')
1137 * @return String: text the text requested or false on failure
1139 public static function getRevisionText( $row, $prefix = 'old_' ) {
1140 wfProfileIn( __METHOD__
);
1143 $textField = $prefix . 'text';
1144 $flagsField = $prefix . 'flags';
1146 if( isset( $row->$flagsField ) ) {
1147 $flags = explode( ',', $row->$flagsField );
1152 if( isset( $row->$textField ) ) {
1153 $text = $row->$textField;
1155 wfProfileOut( __METHOD__
);
1159 # Use external methods for external objects, text in table is URL-only then
1160 if ( in_array( 'external', $flags ) ) {
1162 $parts = explode( '://', $url, 2 );
1163 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
1164 wfProfileOut( __METHOD__
);
1167 $text = ExternalStore
::fetchFromURL( $url );
1170 // If the text was fetched without an error, convert it
1171 if ( $text !== false ) {
1172 if( in_array( 'gzip', $flags ) ) {
1173 # Deal with optional compression of archived pages.
1174 # This can be done periodically via maintenance/compressOld.php, and
1175 # as pages are saved if $wgCompressRevisions is set.
1176 $text = gzinflate( $text );
1179 if( in_array( 'object', $flags ) ) {
1180 # Generic compressed storage
1181 $obj = unserialize( $text );
1182 if ( !is_object( $obj ) ) {
1184 wfProfileOut( __METHOD__
);
1187 $text = $obj->getText();
1190 global $wgLegacyEncoding;
1191 if( $text !== false && $wgLegacyEncoding
1192 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1194 # Old revisions kept around in a legacy encoding?
1195 # Upconvert on demand.
1196 # ("utf8" checked for compatibility with some broken
1197 # conversion scripts 2008-12-30)
1199 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1202 wfProfileOut( __METHOD__
);
1207 * If $wgCompressRevisions is enabled, we will compress data.
1208 * The input string is modified in place.
1209 * Return value is the flags field: contains 'gzip' if the
1210 * data is compressed, and 'utf-8' if we're saving in UTF-8
1213 * @param $text Mixed: reference to a text
1216 public static function compressRevisionText( &$text ) {
1217 global $wgCompressRevisions;
1220 # Revisions not marked this way will be converted
1221 # on load if $wgLegacyCharset is set in the future.
1224 if( $wgCompressRevisions ) {
1225 if( function_exists( 'gzdeflate' ) ) {
1226 $text = gzdeflate( $text );
1229 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1232 return implode( ',', $flags );
1236 * Insert a new revision into the database, returning the new revision ID
1237 * number on success and dies horribly on failure.
1239 * @param $dbw DatabaseBase: (master connection)
1240 * @throws MWException
1243 public function insertOn( $dbw ) {
1244 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1246 wfProfileIn( __METHOD__
);
1248 $this->checkContentModel();
1250 $data = $this->mText
;
1251 $flags = self
::compressRevisionText( $data );
1253 # Write to external storage if required
1254 if( $wgDefaultExternalStore ) {
1255 // Store and get the URL
1256 $data = ExternalStore
::insertToDefault( $data );
1258 throw new MWException( "Unable to store text to external storage" );
1263 $flags .= 'external';
1266 # Record the text (or external storage URL) to the text table
1267 if( !isset( $this->mTextId
) ) {
1268 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1269 $dbw->insert( 'text',
1271 'old_id' => $old_id,
1272 'old_text' => $data,
1273 'old_flags' => $flags,
1276 $this->mTextId
= $dbw->insertId();
1279 if ( $this->mComment
=== null ) $this->mComment
= "";
1281 # Record the edit in revisions
1282 $rev_id = isset( $this->mId
)
1284 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1286 'rev_id' => $rev_id,
1287 'rev_page' => $this->mPage
,
1288 'rev_text_id' => $this->mTextId
,
1289 'rev_comment' => $this->mComment
,
1290 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1291 'rev_user' => $this->mUser
,
1292 'rev_user_text' => $this->mUserText
,
1293 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1294 'rev_deleted' => $this->mDeleted
,
1295 'rev_len' => $this->mSize
,
1296 'rev_parent_id' => is_null( $this->mParentId
)
1297 ?
$this->getPreviousRevisionId( $dbw )
1299 'rev_sha1' => is_null( $this->mSha1
)
1300 ? Revision
::base36Sha1( $this->mText
)
1304 if ( $wgContentHandlerUseDB ) {
1305 //NOTE: Store null for the default model and format, to save space.
1306 //XXX: Makes the DB sensitive to changed defaults. Make this behaviour optional? Only in miser mode?
1308 $model = $this->getContentModel();
1309 $format = $this->getContentFormat();
1311 $title = $this->getTitle();
1313 if ( $title === null ) {
1314 throw new MWException( "Insufficient information to determine the title of the revision's page!" );
1317 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1318 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1320 $row[ 'rev_content_model' ] = ( $model === $defaultModel ) ?
null : $model;
1321 $row[ 'rev_content_format' ] = ( $format === $defaultFormat ) ?
null : $format;
1324 $dbw->insert( 'revision', $row, __METHOD__
);
1326 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1328 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1330 wfProfileOut( __METHOD__
);
1334 protected function checkContentModel() {
1335 global $wgContentHandlerUseDB;
1337 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1339 $model = $this->getContentModel();
1340 $format = $this->getContentFormat();
1341 $handler = $this->getContentHandler();
1343 if ( !$handler->isSupportedFormat( $format ) ) {
1344 $t = $title->getPrefixedDBkey();
1346 throw new MWException( "Can't use format $format with content model $model on $t" );
1349 if ( !$wgContentHandlerUseDB && $title ) {
1350 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1352 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1353 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1354 $defaultFormat = $defaultHandler->getDefaultFormat();
1356 if ( $this->getContentModel() != $defaultModel ) {
1357 $t = $title->getPrefixedDBkey();
1359 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: "
1360 . "model is $model , default for $t is $defaultModel" );
1363 if ( $this->getContentFormat() != $defaultFormat ) {
1364 $t = $title->getPrefixedDBkey();
1366 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: "
1367 . "format is $format, default for $t is $defaultFormat" );
1371 $content = $this->getContent( Revision
::RAW
);
1373 if ( !$content->isValid() ) {
1374 $t = $title->getPrefixedDBkey();
1376 throw new MWException( "Content of $t is not valid! Content model is $model" );
1381 * Get the base 36 SHA-1 value for a string of text
1382 * @param $text String
1385 public static function base36Sha1( $text ) {
1386 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1390 * Lazy-load the revision's text.
1391 * Currently hardcoded to the 'text' table storage engine.
1395 protected function loadText() {
1396 wfProfileIn( __METHOD__
);
1398 // Caching may be beneficial for massive use of external storage
1399 global $wgRevisionCacheExpiry, $wgMemc;
1400 $textId = $this->getTextId();
1401 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1402 if( $wgRevisionCacheExpiry ) {
1403 $text = $wgMemc->get( $key );
1404 if( is_string( $text ) ) {
1405 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1406 wfProfileOut( __METHOD__
);
1411 // If we kept data for lazy extraction, use it now...
1412 if ( isset( $this->mTextRow
) ) {
1413 $row = $this->mTextRow
;
1414 $this->mTextRow
= null;
1420 // Text data is immutable; check slaves first.
1421 $dbr = wfGetDB( DB_SLAVE
);
1422 $row = $dbr->selectRow( 'text',
1423 array( 'old_text', 'old_flags' ),
1424 array( 'old_id' => $this->getTextId() ),
1428 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1429 // Possible slave lag!
1430 $dbw = wfGetDB( DB_MASTER
);
1431 $row = $dbw->selectRow( 'text',
1432 array( 'old_text', 'old_flags' ),
1433 array( 'old_id' => $this->getTextId() ),
1437 $text = self
::getRevisionText( $row );
1439 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1440 if( $wgRevisionCacheExpiry && $text !== false ) {
1441 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1444 wfProfileOut( __METHOD__
);
1450 * Create a new null-revision for insertion into a page's
1451 * history. This will not re-save the text, but simply refer
1452 * to the text from the previous version.
1454 * Such revisions can for instance identify page rename
1455 * operations and other such meta-modifications.
1457 * @param $dbw DatabaseBase
1458 * @param $pageId Integer: ID number of the page to read from
1459 * @param $summary String: revision's summary
1460 * @param $minor Boolean: whether the revision should be considered as minor
1461 * @return Revision|null on error
1463 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1464 global $wgContentHandlerUseDB;
1466 wfProfileIn( __METHOD__
);
1468 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1469 'rev_text_id', 'rev_len', 'rev_sha1' );
1471 if ( $wgContentHandlerUseDB ) {
1472 $fields[] = 'rev_content_model';
1473 $fields[] = 'rev_content_format';
1476 $current = $dbw->selectRow(
1477 array( 'page', 'revision' ),
1480 'page_id' => $pageId,
1481 'page_latest=rev_id',
1488 'comment' => $summary,
1489 'minor_edit' => $minor,
1490 'text_id' => $current->rev_text_id
,
1491 'parent_id' => $current->page_latest
,
1492 'len' => $current->rev_len
,
1493 'sha1' => $current->rev_sha1
1496 if ( $wgContentHandlerUseDB ) {
1497 $row[ 'content_model' ] = $current->rev_content_model
;
1498 $row[ 'content_format' ] = $current->rev_content_format
;
1501 $revision = new Revision( $row );
1502 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1507 wfProfileOut( __METHOD__
);
1512 * Determine if the current user is allowed to view a particular
1513 * field of this revision, if it's marked as deleted.
1515 * @param $field Integer:one of self::DELETED_TEXT,
1516 * self::DELETED_COMMENT,
1517 * self::DELETED_USER
1518 * @param $user User object to check, or null to use $wgUser
1521 public function userCan( $field, User
$user = null ) {
1522 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1526 * Determine if the current user is allowed to view a particular
1527 * field of this revision, if it's marked as deleted. This is used
1528 * by various classes to avoid duplication.
1530 * @param $bitfield Integer: current field
1531 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1532 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1533 * self::DELETED_USER = File::DELETED_USER
1534 * @param $user User object to check, or null to use $wgUser
1537 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1538 if( $bitfield & $field ) { // aspect is deleted
1539 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1540 $permission = 'suppressrevision';
1541 } elseif ( $field & self
::DELETED_TEXT
) {
1542 $permission = 'deletedtext';
1544 $permission = 'deletedhistory';
1546 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1547 if ( $user === null ) {
1551 return $user->isAllowed( $permission );
1558 * Get rev_timestamp from rev_id, without loading the rest of the row
1560 * @param $title Title
1561 * @param $id Integer
1564 static function getTimestampFromId( $title, $id ) {
1565 $dbr = wfGetDB( DB_SLAVE
);
1566 // Casting fix for DB2
1570 $conds = array( 'rev_id' => $id );
1571 $conds['rev_page'] = $title->getArticleID();
1572 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1573 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1574 # Not in slave, try master
1575 $dbw = wfGetDB( DB_MASTER
);
1576 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1578 return wfTimestamp( TS_MW
, $timestamp );
1582 * Get count of revisions per page...not very efficient
1584 * @param $db DatabaseBase
1585 * @param $id Integer: page id
1588 static function countByPageId( $db, $id ) {
1589 $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
1590 array( 'rev_page' => $id ), __METHOD__
);
1592 return $row->revCount
;
1598 * Get count of revisions per page...not very efficient
1600 * @param $db DatabaseBase
1601 * @param $title Title
1604 static function countByTitle( $db, $title ) {
1605 $id = $title->getArticleID();
1607 return self
::countByPageId( $db, $id );
1613 * Check if no edits were made by other users since
1614 * the time a user started editing the page. Limit to
1615 * 50 revisions for the sake of performance.
1619 * @param DatabaseBase|int $db the Database to perform the check on. May be given as a Database object or
1620 * a database identifier usable with wfGetDB.
1621 * @param int $pageId the ID of the page in question
1622 * @param int $userId the ID of the user in question
1623 * @param string $since look at edits since this time
1625 * @return bool True if the given user was the only one to edit since the given timestamp
1627 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1628 if ( !$userId ) return false;
1630 if ( is_int( $db ) ) {
1631 $db = wfGetDB( $db );
1634 $res = $db->select( 'revision',
1637 'rev_page' => $pageId,
1638 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1641 array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
1642 foreach ( $res as $row ) {
1643 if ( $row->rev_user
!= $userId ) {