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
30 protected $mOrigUserText;
32 protected $mMinorEdit;
33 protected $mTimestamp;
43 protected $mContentModel;
44 protected $mContentFormat;
46 protected $mContentHandler;
48 const DELETED_TEXT
= 1;
49 const DELETED_COMMENT
= 2;
50 const DELETED_USER
= 4;
51 const DELETED_RESTRICTED
= 8;
53 const SUPPRESSED_USER
= 12;
54 // Audience options for Revision::getText()
56 const FOR_THIS_USER
= 2;
60 * Load a page revision from a given revision ID number.
61 * Returns null if no such revision can be found.
64 * @return Revision or null
66 public static function newFromId( $id ) {
67 return Revision
::newFromConds( array( 'rev_id' => intval( $id ) ) );
71 * Load either the current, or a specified, revision
72 * that's attached to a given title. If not attached
73 * to that title, will return null.
76 * @param $id Integer (optional)
77 * @return Revision or null
79 public static function newFromTitle( $title, $id = 0 ) {
81 'page_namespace' => $title->getNamespace(),
82 'page_title' => $title->getDBkey()
85 // Use the specified ID
86 $conds['rev_id'] = $id;
87 } elseif ( wfGetLB()->getServerCount() > 1 ) {
88 // Get the latest revision ID from the master
89 $dbw = wfGetDB( DB_MASTER
);
90 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
91 if ( $latest === false ) {
92 return null; // page does not exist
94 $conds['rev_id'] = $latest;
96 // Use a join to get the latest revision
97 $conds[] = 'rev_id=page_latest';
99 return Revision
::newFromConds( $conds );
103 * Load either the current, or a specified, revision
104 * that's attached to a given page ID.
105 * Returns null if no such revision can be found.
107 * @param $revId Integer
108 * @param $pageId Integer (optional)
109 * @return Revision or null
111 public static function newFromPageId( $pageId, $revId = 0 ) {
112 $conds = array( 'page_id' => $pageId );
114 $conds['rev_id'] = $revId;
115 } elseif ( wfGetLB()->getServerCount() > 1 ) {
116 // Get the latest revision ID from the master
117 $dbw = wfGetDB( DB_MASTER
);
118 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
119 if ( $latest === false ) {
120 return null; // page does not exist
122 $conds['rev_id'] = $latest;
124 $conds[] = 'rev_id = page_latest';
126 return Revision
::newFromConds( $conds );
130 * Make a fake revision object from an archive table row. This is queried
131 * for permissions or even inserted (as in Special:Undelete)
132 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
135 * @param $overrides array
139 public static function newFromArchiveRow( $row, $overrides = array() ) {
140 global $wgContentHandlerUseDB;
142 $attribs = $overrides +
array(
143 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
144 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
145 'comment' => $row->ar_comment
,
146 'user' => $row->ar_user
,
147 'user_text' => $row->ar_user_text
,
148 'timestamp' => $row->ar_timestamp
,
149 'minor_edit' => $row->ar_minor_edit
,
150 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
151 'deleted' => $row->ar_deleted
,
152 'len' => $row->ar_len
,
153 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
154 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
155 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
158 if ( !$wgContentHandlerUseDB ) {
159 unset( $attribs['content_model'] );
160 unset( $attribs['content_format'] );
163 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
164 // Pre-1.5 ar_text row
165 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
166 if ( $attribs['text'] === false ) {
167 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
170 return new self( $attribs );
179 public static function newFromRow( $row ) {
180 return new self( $row );
184 * Load a page revision from a given revision ID number.
185 * Returns null if no such revision can be found.
187 * @param $db DatabaseBase
189 * @return Revision or null
191 public static function loadFromId( $db, $id ) {
192 return Revision
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
196 * Load either the current, or a specified, revision
197 * that's attached to a given page. If not attached
198 * to that page, will return null.
200 * @param $db DatabaseBase
201 * @param $pageid Integer
203 * @return Revision or null
205 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
206 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
208 $conds['rev_id'] = intval( $id );
210 $conds[] = 'rev_id=page_latest';
212 return Revision
::loadFromConds( $db, $conds );
216 * Load either the current, or a specified, revision
217 * that's attached to a given page. If not attached
218 * to that page, will return null.
220 * @param $db DatabaseBase
221 * @param $title Title
223 * @return Revision or null
225 public static function loadFromTitle( $db, $title, $id = 0 ) {
227 $matchId = intval( $id );
229 $matchId = 'page_latest';
231 return Revision
::loadFromConds( $db,
232 array( "rev_id=$matchId",
233 'page_namespace' => $title->getNamespace(),
234 'page_title' => $title->getDBkey() )
239 * Load the revision for the given title with the given timestamp.
240 * WARNING: Timestamps may in some circumstances not be unique,
241 * so this isn't the best key to use.
243 * @param $db DatabaseBase
244 * @param $title Title
245 * @param $timestamp String
246 * @return Revision or null
248 public static function loadFromTimestamp( $db, $title, $timestamp ) {
249 return Revision
::loadFromConds( $db,
250 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
251 'page_namespace' => $title->getNamespace(),
252 'page_title' => $title->getDBkey() )
257 * Given a set of conditions, fetch a revision.
259 * @param $conditions Array
260 * @return Revision or null
262 public static function newFromConds( $conditions ) {
263 $db = wfGetDB( DB_SLAVE
);
264 $rev = Revision
::loadFromConds( $db, $conditions );
265 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
266 $dbw = wfGetDB( DB_MASTER
);
267 $rev = Revision
::loadFromConds( $dbw, $conditions );
273 * Given a set of conditions, fetch a revision from
274 * the given database connection.
276 * @param $db DatabaseBase
277 * @param $conditions Array
278 * @return Revision or null
280 private static function loadFromConds( $db, $conditions ) {
281 $res = Revision
::fetchFromConds( $db, $conditions );
283 $row = $res->fetchObject();
285 $ret = new Revision( $row );
294 * Return a wrapper for a series of database rows to
295 * fetch all of a given page's revisions in turn.
296 * Each row can be fed to the constructor to get objects.
298 * @param $title Title
299 * @return ResultWrapper
301 public static function fetchRevision( $title ) {
302 return Revision
::fetchFromConds(
304 array( 'rev_id=page_latest',
305 'page_namespace' => $title->getNamespace(),
306 'page_title' => $title->getDBkey() )
311 * Given a set of conditions, return a ResultWrapper
312 * which will return matching database rows with the
313 * fields necessary to build Revision objects.
315 * @param $db DatabaseBase
316 * @param $conditions Array
317 * @return ResultWrapper
319 private static function fetchFromConds( $db, $conditions ) {
320 $fields = array_merge(
321 self
::selectFields(),
322 self
::selectPageFields(),
323 self
::selectUserFields()
326 array( 'revision', 'page', 'user' ),
330 array( 'LIMIT' => 1 ),
331 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
336 * Return the value of a select() JOIN conds array for the user table.
337 * This will get user table rows for logged-in users.
341 public static function userJoinCond() {
342 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
346 * Return the value of a select() page conds array for the paeg table.
347 * This will assure that the revision(s) are not orphaned from live pages.
351 public static function pageJoinCond() {
352 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
356 * Return the list of revision fields that should be selected to create
360 public static function selectFields() {
361 global $wgContentHandlerUseDB;
378 if ( $wgContentHandlerUseDB ) {
379 $fields[] = 'rev_content_format';
380 $fields[] = 'rev_content_model';
387 * Return the list of text fields that should be selected to read the
391 public static function selectTextFields() {
399 * Return the list of page fields that should be selected from page table
402 public static function selectPageFields() {
414 * Return the list of user fields that should be selected from user table
417 public static function selectUserFields() {
418 return array( 'user_name' );
422 * Do a batched query to get the parent revision lengths
423 * @param $db DatabaseBase
424 * @param $revIds Array
427 public static function getParentLengths( $db, array $revIds ) {
430 return $revLens; // empty
432 wfProfileIn( __METHOD__
);
433 $res = $db->select( 'revision',
434 array( 'rev_id', 'rev_len' ),
435 array( 'rev_id' => $revIds ),
437 foreach ( $res as $row ) {
438 $revLens[$row->rev_id
] = $row->rev_len
;
440 wfProfileOut( __METHOD__
);
447 * @param $row Mixed: either a database row or an array
450 function __construct( $row ) {
451 if( is_object( $row ) ) {
452 $this->mId
= intval( $row->rev_id
);
453 $this->mPage
= intval( $row->rev_page
);
454 $this->mTextId
= intval( $row->rev_text_id
);
455 $this->mComment
= $row->rev_comment
;
456 $this->mUser
= intval( $row->rev_user
);
457 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
458 $this->mTimestamp
= $row->rev_timestamp
;
459 $this->mDeleted
= intval( $row->rev_deleted
);
461 if( !isset( $row->rev_parent_id
) ) {
462 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
464 $this->mParentId
= intval( $row->rev_parent_id
);
467 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
470 $this->mSize
= intval( $row->rev_len
);
473 if ( !isset( $row->rev_sha1
) ) {
476 $this->mSha1
= $row->rev_sha1
;
479 if( isset( $row->page_latest
) ) {
480 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
481 $this->mTitle
= Title
::newFromRow( $row );
483 $this->mCurrent
= false;
484 $this->mTitle
= null;
487 if( !isset( $row->rev_content_model
) ||
is_null( $row->rev_content_model
) ) {
488 $this->mContentModel
= null; # determine on demand if needed
490 $this->mContentModel
= strval( $row->rev_content_model
);
493 if( !isset( $row->rev_content_format
) ||
is_null( $row->rev_content_format
) ) {
494 $this->mContentFormat
= null; # determine on demand if needed
496 $this->mContentFormat
= strval( $row->rev_content_format
);
499 // Lazy extraction...
501 if( isset( $row->old_text
) ) {
502 $this->mTextRow
= $row;
504 // 'text' table row entry will be lazy-loaded
505 $this->mTextRow
= null;
508 // Use user_name for users and rev_user_text for IPs...
509 $this->mUserText
= null; // lazy load if left null
510 if ( $this->mUser
== 0 ) {
511 $this->mUserText
= $row->rev_user_text
; // IP user
512 } elseif ( isset( $row->user_name
) ) {
513 $this->mUserText
= $row->user_name
; // logged-in user
515 $this->mOrigUserText
= $row->rev_user_text
;
516 } elseif( is_array( $row ) ) {
517 // Build a new revision to be saved...
518 global $wgUser; // ugh
521 # if we have a content object, use it to set the model and type
522 if ( !empty( $row['content'] ) ) {
523 if ( !empty( $row['text_id'] ) ) { //@todo: when is that set? test with external store setup! check out insertOn() [dk]
524 throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
527 $row['content_model'] = $row['content']->getModel();
528 # note: mContentFormat is initializes later accordingly
529 # note: content is serialized later in this method!
530 # also set text to null?
533 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
534 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
535 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
536 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
537 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
538 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
539 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
540 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
541 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
542 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
543 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
545 $this->mContentModel
= isset( $row['content_model'] ) ?
strval( $row['content_model'] ) : null;
546 $this->mContentFormat
= isset( $row['content_format'] ) ?
strval( $row['content_format'] ) : null;
548 // Enforce spacing trimming on supplied text
549 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
550 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
551 $this->mTextRow
= null;
553 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
555 // if we have a Content object, override mText and mContentModel
556 if ( !empty( $row['content'] ) ) {
557 $handler = $this->getContentHandler();
558 $this->mContent
= $row['content'];
560 $this->mContentModel
= $this->mContent
->getModel();
561 $this->mContentHandler
= null;
563 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
564 } elseif ( !is_null( $this->mText
) ) {
565 $handler = $this->getContentHandler();
566 $this->mContent
= $handler->unserializeContent( $this->mText
);
569 // if we have a Title object, override mPage. Useful for testing and convenience.
570 if ( isset( $row['title'] ) ) {
571 $this->mTitle
= $row['title'];
572 $this->mPage
= $this->mTitle
->getArticleID();
574 $this->mTitle
= null; // Load on demand if needed
577 $this->mCurrent
= false; // @todo: XXX: really? we are about to create a revision. it will usually then be the current one.
579 // If we still have no length, see it we have the text to figure it out
580 if ( !$this->mSize
) {
581 if ( !is_null( $this->mContent
) ) {
582 $this->mSize
= $this->mContent
->getSize();
584 #NOTE: this should never happen if we have either text or content object!
590 if ( $this->mSha1
=== null ) {
591 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
595 $this->getContentModel();
596 $this->getContentFormat();
598 throw new MWException( 'Revision constructor passed invalid row format.' );
600 $this->mUnpatrolled
= null;
606 * @return Integer|null
608 public function getId() {
613 * Set the revision ID
618 public function setId( $id ) {
625 * @return Integer|null
627 public function getTextId() {
628 return $this->mTextId
;
632 * Get parent revision ID (the original previous page revision)
634 * @return Integer|null
636 public function getParentId() {
637 return $this->mParentId
;
641 * Returns the length of the text in this revision, or null if unknown.
643 * @return Integer|null
645 public function getSize() {
650 * Returns the base36 sha1 of the text in this revision, or null if unknown.
652 * @return String|null
654 public function getSha1() {
659 * Returns the title of the page associated with this entry or null.
661 * Will do a query, when title is not set and id is given.
665 public function getTitle() {
666 if( isset( $this->mTitle
) ) {
667 return $this->mTitle
;
669 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
670 $dbr = wfGetDB( DB_SLAVE
);
671 $row = $dbr->selectRow(
672 array( 'page', 'revision' ),
673 self
::selectPageFields(),
674 array( 'page_id=rev_page',
675 'rev_id' => $this->mId
),
678 $this->mTitle
= Title
::newFromRow( $row );
682 if ( !$this->mTitle
&& !is_null( $this->mPage
) && $this->mPage
> 0 ) {
683 $this->mTitle
= Title
::newFromID( $this->mPage
);
686 return $this->mTitle
;
690 * Set the title of the revision
692 * @param $title Title
694 public function setTitle( $title ) {
695 $this->mTitle
= $title;
701 * @return Integer|null
703 public function getPage() {
708 * Fetch revision's user id if it's available to the specified audience.
709 * If the specified audience does not have access to it, zero will be
712 * @param $audience Integer: one of:
713 * Revision::FOR_PUBLIC to be displayed to all users
714 * Revision::FOR_THIS_USER to be displayed to the given user
715 * Revision::RAW get the ID regardless of permissions
716 * @param $user User object to check for, only if FOR_THIS_USER is passed
717 * to the $audience parameter
720 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
721 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
723 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
731 * Fetch revision's user id without regard for the current user's permissions
735 public function getRawUser() {
740 * Fetch revision's username if it's available to the specified audience.
741 * If the specified audience does not have access to the username, an
742 * empty string will be returned.
744 * @param $audience Integer: one of:
745 * Revision::FOR_PUBLIC to be displayed to all users
746 * Revision::FOR_THIS_USER to be displayed to the given user
747 * Revision::RAW get the text regardless of permissions
748 * @param $user User object to check for, only if FOR_THIS_USER is passed
749 * to the $audience parameter
752 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
753 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
755 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
758 return $this->getRawUserText();
763 * Fetch revision's username without regard for view restrictions
767 public function getRawUserText() {
768 if ( $this->mUserText
=== null ) {
769 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
770 if ( $this->mUserText
=== false ) {
771 # This shouldn't happen, but it can if the wiki was recovered
772 # via importing revs and there is no user table entry yet.
773 $this->mUserText
= $this->mOrigUserText
;
776 return $this->mUserText
;
780 * Fetch revision comment if it's available to the specified audience.
781 * If the specified audience does not have access to the comment, an
782 * empty string will be returned.
784 * @param $audience Integer: one of:
785 * Revision::FOR_PUBLIC to be displayed to all users
786 * Revision::FOR_THIS_USER to be displayed to the given user
787 * Revision::RAW get the text regardless of permissions
788 * @param $user User object to check for, only if FOR_THIS_USER is passed
789 * to the $audience parameter
792 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
793 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
795 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
798 return $this->mComment
;
803 * Fetch revision comment without regard for the current user's permissions
807 public function getRawComment() {
808 return $this->mComment
;
814 public function isMinor() {
815 return (bool)$this->mMinorEdit
;
819 * @return Integer rcid of the unpatrolled row, zero if there isn't one
821 public function isUnpatrolled() {
822 if( $this->mUnpatrolled
!== null ) {
823 return $this->mUnpatrolled
;
825 $dbr = wfGetDB( DB_SLAVE
);
826 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
828 array( // Add redundant user,timestamp condition so we can use the existing index
829 'rc_user_text' => $this->getRawUserText(),
830 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
831 'rc_this_oldid' => $this->getId(),
836 return (int)$this->mUnpatrolled
;
840 * @param $field int one of DELETED_* bitfield constants
844 public function isDeleted( $field ) {
845 return ( $this->mDeleted
& $field ) == $field;
849 * Get the deletion bitfield of the revision
853 public function getVisibility() {
854 return (int)$this->mDeleted
;
858 * Fetch revision text if it's available to the specified audience.
859 * If the specified audience does not have the ability to view this
860 * revision, an empty string will be returned.
862 * @param $audience Integer: one of:
863 * Revision::FOR_PUBLIC to be displayed to all users
864 * Revision::FOR_THIS_USER to be displayed to the given user
865 * Revision::RAW get the text regardless of permissions
866 * @param $user User object to check for, only if FOR_THIS_USER is passed
867 * to the $audience parameter
869 * @deprecated in 1.WD, use getContent() instead
870 * @todo: replace usage in core
872 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
873 wfDeprecated( __METHOD__
, '1.WD' );
875 $content = $this->getContent( $audience, $user );
876 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
880 * Fetch revision content if it's available to the specified audience.
881 * If the specified audience does not have the ability to view this
882 * revision, null will be returned.
884 * @param $audience Integer: one of:
885 * Revision::FOR_PUBLIC to be displayed to all users
886 * Revision::FOR_THIS_USER to be displayed to $wgUser
887 * Revision::RAW get the text regardless of permissions
888 * @param $user User object to check for, only if FOR_THIS_USER is passed
889 * to the $audience parameter
894 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
895 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
897 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
900 return $this->getContentInternal();
905 * Alias for getText(Revision::FOR_THIS_USER)
907 * @deprecated since 1.17
910 public function revText() {
911 wfDeprecated( __METHOD__
, '1.17' );
912 return $this->getText( self
::FOR_THIS_USER
);
916 * Fetch revision text without regard for view restrictions
920 * @deprecated since 1.WD. Instead, use Revision::getContent( Revision::RAW ) or Revision::getSerializedData() as appropriate.
922 public function getRawText() {
923 wfDeprecated( __METHOD__
, "1.WD" );
925 return $this->getText( self
::RAW
);
929 * Fetch original serialized data without regard for view restrictions
935 public function getSerializedData() {
939 protected function getContentInternal() {
940 if( is_null( $this->mContent
) ) {
941 // Revision is immutable. Load on demand:
943 $handler = $this->getContentHandler();
944 $format = $this->getContentFormat();
945 $title = $this->getTitle();
947 if( is_null( $this->mText
) ) {
948 // Load text on demand:
949 $this->mText
= $this->loadText();
952 $this->mContent
= is_null( $this->mText
) ?
null : $handler->unserializeContent( $this->mText
, $format );
955 return $this->mContent
;
959 * Returns the content model for this revision.
961 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
962 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
963 * is used as a last resort.
965 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
967 public function getContentModel() {
968 if ( !$this->mContentModel
) {
969 $title = $this->getTitle();
970 $this->mContentModel
= ( $title ?
$title->getContentModel() : CONTENT_MODEL_WIKITEXT
);
972 assert( !empty( $this->mContentModel
) );
975 return $this->mContentModel
;
979 * Returns the content format for this revision.
981 * If no content format was stored in the database, the default format for this
982 * revision's content model is returned.
984 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
986 public function getContentFormat() {
987 if ( !$this->mContentFormat
) {
988 $handler = $this->getContentHandler();
989 $this->mContentFormat
= $handler->getDefaultFormat();
991 assert( !empty( $this->mContentFormat
) );
994 return $this->mContentFormat
;
998 * Returns the content handler appropriate for this revision's content model.
1000 * @return ContentHandler
1002 public function getContentHandler() {
1003 if ( !$this->mContentHandler
) {
1004 $model = $this->getContentModel();
1005 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1007 $format = $this->getContentFormat();
1009 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1010 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1014 return $this->mContentHandler
;
1020 public function getTimestamp() {
1021 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1027 public function isCurrent() {
1028 return $this->mCurrent
;
1032 * Get previous revision for this title
1034 * @return Revision or null
1036 public function getPrevious() {
1037 if( $this->getTitle() ) {
1038 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1040 return Revision
::newFromTitle( $this->getTitle(), $prev );
1047 * Get next revision for this title
1049 * @return Revision or null
1051 public function getNext() {
1052 if( $this->getTitle() ) {
1053 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1055 return Revision
::newFromTitle( $this->getTitle(), $next );
1062 * Get previous revision Id for this page_id
1063 * This is used to populate rev_parent_id on save
1065 * @param $db DatabaseBase
1068 private function getPreviousRevisionId( $db ) {
1069 if( is_null( $this->mPage
) ) {
1072 # Use page_latest if ID is not given
1074 $prevId = $db->selectField( 'page', 'page_latest',
1075 array( 'page_id' => $this->mPage
),
1078 $prevId = $db->selectField( 'revision', 'rev_id',
1079 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
1081 array( 'ORDER BY' => 'rev_id DESC' ) );
1083 return intval( $prevId );
1087 * Get revision text associated with an old or archive row
1088 * $row is usually an object from wfFetchRow(), both the flags and the text
1089 * field must be included
1091 * @param $row Object: the text data
1092 * @param $prefix String: table prefix (default 'old_')
1093 * @return String: text the text requested or false on failure
1095 public static function getRevisionText( $row, $prefix = 'old_' ) {
1096 wfProfileIn( __METHOD__
);
1099 $textField = $prefix . 'text';
1100 $flagsField = $prefix . 'flags';
1102 if( isset( $row->$flagsField ) ) {
1103 $flags = explode( ',', $row->$flagsField );
1108 if( isset( $row->$textField ) ) {
1109 $text = $row->$textField;
1111 wfProfileOut( __METHOD__
);
1115 # Use external methods for external objects, text in table is URL-only then
1116 if ( in_array( 'external', $flags ) ) {
1118 $parts = explode( '://', $url, 2 );
1119 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
1120 wfProfileOut( __METHOD__
);
1123 $text = ExternalStore
::fetchFromURL( $url );
1126 // If the text was fetched without an error, convert it
1127 if ( $text !== false ) {
1128 if( in_array( 'gzip', $flags ) ) {
1129 # Deal with optional compression of archived pages.
1130 # This can be done periodically via maintenance/compressOld.php, and
1131 # as pages are saved if $wgCompressRevisions is set.
1132 $text = gzinflate( $text );
1135 if( in_array( 'object', $flags ) ) {
1136 # Generic compressed storage
1137 $obj = unserialize( $text );
1138 if ( !is_object( $obj ) ) {
1140 wfProfileOut( __METHOD__
);
1143 $text = $obj->getText();
1146 global $wgLegacyEncoding;
1147 if( $text !== false && $wgLegacyEncoding
1148 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1150 # Old revisions kept around in a legacy encoding?
1151 # Upconvert on demand.
1152 # ("utf8" checked for compatibility with some broken
1153 # conversion scripts 2008-12-30)
1155 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1158 wfProfileOut( __METHOD__
);
1163 * If $wgCompressRevisions is enabled, we will compress data.
1164 * The input string is modified in place.
1165 * Return value is the flags field: contains 'gzip' if the
1166 * data is compressed, and 'utf-8' if we're saving in UTF-8
1169 * @param $text Mixed: reference to a text
1172 public static function compressRevisionText( &$text ) {
1173 global $wgCompressRevisions;
1176 # Revisions not marked this way will be converted
1177 # on load if $wgLegacyCharset is set in the future.
1180 if( $wgCompressRevisions ) {
1181 if( function_exists( 'gzdeflate' ) ) {
1182 $text = gzdeflate( $text );
1185 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
1188 return implode( ',', $flags );
1192 * Insert a new revision into the database, returning the new revision ID
1193 * number on success and dies horribly on failure.
1195 * @param $dbw DatabaseBase: (master connection)
1198 public function insertOn( $dbw ) {
1199 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1201 wfProfileIn( __METHOD__
);
1203 $this->checkContentModel();
1205 $data = $this->mText
;
1206 $flags = Revision
::compressRevisionText( $data );
1208 # Write to external storage if required
1209 if( $wgDefaultExternalStore ) {
1210 // Store and get the URL
1211 $data = ExternalStore
::insertToDefault( $data );
1213 throw new MWException( "Unable to store text to external storage" );
1218 $flags .= 'external';
1221 # Record the text (or external storage URL) to the text table
1222 if( !isset( $this->mTextId
) ) {
1223 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1224 $dbw->insert( 'text',
1226 'old_id' => $old_id,
1227 'old_text' => $data,
1228 'old_flags' => $flags,
1231 $this->mTextId
= $dbw->insertId();
1234 if ( $this->mComment
=== null ) $this->mComment
= "";
1236 # Record the edit in revisions
1237 $rev_id = isset( $this->mId
)
1239 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1242 'rev_id' => $rev_id,
1243 'rev_page' => $this->mPage
,
1244 'rev_text_id' => $this->mTextId
,
1245 'rev_comment' => $this->mComment
,
1246 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1247 'rev_user' => $this->mUser
,
1248 'rev_user_text' => $this->mUserText
,
1249 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1250 'rev_deleted' => $this->mDeleted
,
1251 'rev_len' => $this->mSize
,
1252 'rev_parent_id' => is_null( $this->mParentId
)
1253 ?
$this->getPreviousRevisionId( $dbw )
1255 'rev_sha1' => is_null( $this->mSha1
)
1256 ? Revision
::base36Sha1( $this->mText
)
1260 if ( $wgContentHandlerUseDB ) {
1261 //NOTE: Store null for the default model and format, to save space.
1262 //XXX: Makes the DB sensitive to changed defaults. Make this behaviour optional? Only in miser mode?
1264 $model = $this->getContentModel();
1265 $format = $this->getContentFormat();
1267 $defaultModel = ContentHandler
::getDefaultModelFor( $this->getTitle() );
1268 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1270 $row[ 'rev_content_model' ] = ( $model === $defaultModel ) ?
null : $model;
1271 $row[ 'rev_content_format' ] = ( $format === $defaultFormat ) ?
null : $format;
1274 $dbw->insert( 'revision', $row, __METHOD__
);
1276 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1278 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1280 wfProfileOut( __METHOD__
);
1284 protected function checkContentModel() {
1285 global $wgContentHandlerUseDB;
1287 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1289 $model = $this->getContentModel();
1290 $format = $this->getContentFormat();
1291 $handler = $this->getContentHandler();
1293 if ( !$handler->isSupportedFormat( $format ) ) {
1294 $t = $title->getPrefixedDBkey();
1296 throw new MWException( "Can't use format $format with content model $model on $t" );
1299 if ( !$wgContentHandlerUseDB && $title ) {
1300 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1302 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1303 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1304 $defaultFormat = $defaultHandler->getDefaultFormat();
1306 if ( $this->getContentModel() != $defaultModel ) {
1307 $t = $title->getPrefixedDBkey();
1309 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: model is $model , default for $t is $defaultModel" );
1312 if ( $this->getContentFormat() != $defaultFormat ) {
1313 $t = $title->getPrefixedDBkey();
1315 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: format is $format, default for $t is $defaultFormat" );
1319 $content = $this->getContent( Revision
::RAW
);
1321 if ( !$content->isValid() ) {
1322 $t = $title->getPrefixedDBkey();
1324 throw new MWException( "Content of $t is not valid! Content model is $model" );
1329 * Get the base 36 SHA-1 value for a string of text
1330 * @param $text String
1333 public static function base36Sha1( $text ) {
1334 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1338 * Lazy-load the revision's text.
1339 * Currently hardcoded to the 'text' table storage engine.
1343 protected function loadText() {
1344 wfProfileIn( __METHOD__
);
1346 // Caching may be beneficial for massive use of external storage
1347 global $wgRevisionCacheExpiry, $wgMemc;
1348 $textId = $this->getTextId();
1349 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1350 if( $wgRevisionCacheExpiry ) {
1351 $text = $wgMemc->get( $key );
1352 if( is_string( $text ) ) {
1353 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1354 wfProfileOut( __METHOD__
);
1359 // If we kept data for lazy extraction, use it now...
1360 if ( isset( $this->mTextRow
) ) {
1361 $row = $this->mTextRow
;
1362 $this->mTextRow
= null;
1368 // Text data is immutable; check slaves first.
1369 $dbr = wfGetDB( DB_SLAVE
);
1370 $row = $dbr->selectRow( 'text',
1371 array( 'old_text', 'old_flags' ),
1372 array( 'old_id' => $this->getTextId() ),
1376 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1377 // Possible slave lag!
1378 $dbw = wfGetDB( DB_MASTER
);
1379 $row = $dbw->selectRow( 'text',
1380 array( 'old_text', 'old_flags' ),
1381 array( 'old_id' => $this->getTextId() ),
1385 $text = self
::getRevisionText( $row );
1387 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1388 if( $wgRevisionCacheExpiry && $text !== false ) {
1389 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1392 wfProfileOut( __METHOD__
);
1398 * Create a new null-revision for insertion into a page's
1399 * history. This will not re-save the text, but simply refer
1400 * to the text from the previous version.
1402 * Such revisions can for instance identify page rename
1403 * operations and other such meta-modifications.
1405 * @param $dbw DatabaseBase
1406 * @param $pageId Integer: ID number of the page to read from
1407 * @param $summary String: revision's summary
1408 * @param $minor Boolean: whether the revision should be considered as minor
1409 * @return Revision|null on error
1411 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1412 global $wgContentHandlerUseDB;
1414 wfProfileIn( __METHOD__
);
1416 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1417 'rev_text_id', 'rev_len', 'rev_sha1' );
1419 if ( $wgContentHandlerUseDB ) {
1420 $fields[] = 'rev_content_model';
1421 $fields[] = 'rev_content_format';
1424 $current = $dbw->selectRow(
1425 array( 'page', 'revision' ),
1428 'page_id' => $pageId,
1429 'page_latest=rev_id',
1436 'comment' => $summary,
1437 'minor_edit' => $minor,
1438 'text_id' => $current->rev_text_id
,
1439 'parent_id' => $current->page_latest
,
1440 'len' => $current->rev_len
,
1441 'sha1' => $current->rev_sha1
1444 if ( $wgContentHandlerUseDB ) {
1445 $row[ 'content_model' ] = $current->rev_content_model
;
1446 $row[ 'content_format' ] = $current->rev_content_format
;
1449 $revision = new Revision( $row );
1450 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1455 wfProfileOut( __METHOD__
);
1460 * Determine if the current user is allowed to view a particular
1461 * field of this revision, if it's marked as deleted.
1463 * @param $field Integer:one of self::DELETED_TEXT,
1464 * self::DELETED_COMMENT,
1465 * self::DELETED_USER
1466 * @param $user User object to check, or null to use $wgUser
1469 public function userCan( $field, User
$user = null ) {
1470 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1474 * Determine if the current user is allowed to view a particular
1475 * field of this revision, if it's marked as deleted. This is used
1476 * by various classes to avoid duplication.
1478 * @param $bitfield Integer: current field
1479 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1480 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1481 * self::DELETED_USER = File::DELETED_USER
1482 * @param $user User object to check, or null to use $wgUser
1485 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1486 if( $bitfield & $field ) { // aspect is deleted
1487 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1488 $permission = 'suppressrevision';
1489 } elseif ( $field & self
::DELETED_TEXT
) {
1490 $permission = 'deletedtext';
1492 $permission = 'deletedhistory';
1494 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1495 if ( $user === null ) {
1499 return $user->isAllowed( $permission );
1506 * Get rev_timestamp from rev_id, without loading the rest of the row
1508 * @param $title Title
1509 * @param $id Integer
1512 static function getTimestampFromId( $title, $id ) {
1513 $dbr = wfGetDB( DB_SLAVE
);
1514 // Casting fix for DB2
1518 $conds = array( 'rev_id' => $id );
1519 $conds['rev_page'] = $title->getArticleID();
1520 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1521 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1522 # Not in slave, try master
1523 $dbw = wfGetDB( DB_MASTER
);
1524 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1526 return wfTimestamp( TS_MW
, $timestamp );
1530 * Get count of revisions per page...not very efficient
1532 * @param $db DatabaseBase
1533 * @param $id Integer: page id
1536 static function countByPageId( $db, $id ) {
1537 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1538 array( 'rev_page' => $id ), __METHOD__
);
1540 return $row->revCount
;
1546 * Get count of revisions per page...not very efficient
1548 * @param $db DatabaseBase
1549 * @param $title Title
1552 static function countByTitle( $db, $title ) {
1553 $id = $title->getArticleID();
1555 return Revision
::countByPageId( $db, $id );