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' );
424 * @param $row Mixed: either a database row or an array
427 function __construct( $row ) {
428 if( is_object( $row ) ) {
429 $this->mId
= intval( $row->rev_id
);
430 $this->mPage
= intval( $row->rev_page
);
431 $this->mTextId
= intval( $row->rev_text_id
);
432 $this->mComment
= $row->rev_comment
;
433 $this->mUser
= intval( $row->rev_user
);
434 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
435 $this->mTimestamp
= $row->rev_timestamp
;
436 $this->mDeleted
= intval( $row->rev_deleted
);
438 if( !isset( $row->rev_parent_id
) ) {
439 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
441 $this->mParentId
= intval( $row->rev_parent_id
);
444 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
447 $this->mSize
= intval( $row->rev_len
);
450 if ( !isset( $row->rev_sha1
) ) {
453 $this->mSha1
= $row->rev_sha1
;
456 if( isset( $row->page_latest
) ) {
457 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
458 $this->mTitle
= Title
::newFromRow( $row );
460 $this->mCurrent
= false;
461 $this->mTitle
= null;
464 if( !isset( $row->rev_content_model
) ||
is_null( $row->rev_content_model
) ) {
465 $this->mContentModel
= null; # determine on demand if needed
467 $this->mContentModel
= intval( $row->rev_content_model
);
470 if( !isset( $row->rev_content_format
) ||
is_null( $row->rev_content_format
) ) {
471 $this->mContentFormat
= null; # determine on demand if needed
473 $this->mContentFormat
= intval( $row->rev_content_format
);
476 // Lazy extraction...
478 if( isset( $row->old_text
) ) {
479 $this->mTextRow
= $row;
481 // 'text' table row entry will be lazy-loaded
482 $this->mTextRow
= null;
485 // Use user_name for users and rev_user_text for IPs...
486 $this->mUserText
= null; // lazy load if left null
487 if ( $this->mUser
== 0 ) {
488 $this->mUserText
= $row->rev_user_text
; // IP user
489 } elseif ( isset( $row->user_name
) ) {
490 $this->mUserText
= $row->user_name
; // logged-in user
492 $this->mOrigUserText
= $row->rev_user_text
;
493 } elseif( is_array( $row ) ) {
494 // Build a new revision to be saved...
495 global $wgUser; // ugh
498 # if we have a content object, use it to set the model and type
499 if ( !empty( $row['content'] ) ) {
500 if ( !empty( $row['text_id'] ) ) { //@todo: when is that set? test with external store setup! check out insertOn() [dk]
501 throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
504 $row['content_model'] = $row['content']->getModel();
505 # note: mContentFormat is initializes later accordingly
506 # note: content is serialized later in this method!
507 # also set text to null?
510 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
511 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
512 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
513 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
514 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
515 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
516 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
517 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
518 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
519 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
520 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
522 $this->mContentModel
= isset( $row['content_model'] ) ?
intval( $row['content_model'] ) : null;
523 $this->mContentFormat
= isset( $row['content_format'] ) ?
intval( $row['content_format'] ) : null;
525 // Enforce spacing trimming on supplied text
526 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
527 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
528 $this->mTextRow
= null;
530 # if we have a content object, override mText and mContentModel
531 if ( !empty( $row['content'] ) ) {
532 $handler = $this->getContentHandler();
533 $this->mContent
= $row['content'];
535 $this->mContentModel
= $this->mContent
->getModel();
536 $this->mContentHandler
= null;
538 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
539 } elseif ( !is_null( $this->mText
) ) {
540 $handler = $this->getContentHandler();
541 $this->mContent
= $handler->unserializeContent( $this->mText
);
544 $this->mTitle
= null; # Load on demand if needed
545 $this->mCurrent
= false; # XXX: really? we are about to create a revision. it will usually then be the current one.
547 # If we still have no length, see it we have the text to figure it out
548 if ( !$this->mSize
) {
549 if ( !is_null( $this->mContent
) ) {
550 $this->mSize
= $this->mContent
->getSize();
552 #XXX: my be inconsistent with the notion of "size" use for the present content model
553 #NOTE: should never happen if we have either text or content object!
554 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
559 if ( $this->mSha1
=== null ) {
560 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
563 $this->getContentModel(); # force lazy init
564 $this->getContentFormat(); # force lazy init
566 throw new MWException( 'Revision constructor passed invalid row format.' );
568 $this->mUnpatrolled
= null;
574 * @return Integer|null
576 public function getId() {
581 * Set the revision ID
586 public function setId( $id ) {
593 * @return Integer|null
595 public function getTextId() {
596 return $this->mTextId
;
600 * Get parent revision ID (the original previous page revision)
602 * @return Integer|null
604 public function getParentId() {
605 return $this->mParentId
;
609 * Returns the length of the text in this revision, or null if unknown.
611 * @return Integer|null
613 public function getSize() {
618 * Returns the base36 sha1 of the text in this revision, or null if unknown.
620 * @return String|null
622 public function getSha1() {
627 * Returns the title of the page associated with this entry or null.
629 * Will do a query, when title is not set and id is given.
633 public function getTitle() {
634 if( isset( $this->mTitle
) ) {
635 return $this->mTitle
;
637 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL
638 $dbr = wfGetDB( DB_SLAVE
);
639 $row = $dbr->selectRow(
640 array( 'page', 'revision' ),
641 self
::selectPageFields(),
642 array( 'page_id=rev_page',
643 'rev_id' => $this->mId
),
646 $this->mTitle
= Title
::newFromRow( $row );
649 return $this->mTitle
;
653 * Set the title of the revision
655 * @param $title Title
657 public function setTitle( $title ) {
658 $this->mTitle
= $title;
664 * @return Integer|null
666 public function getPage() {
671 * Fetch revision's user id if it's available to the specified audience.
672 * If the specified audience does not have access to it, zero will be
675 * @param $audience Integer: one of:
676 * Revision::FOR_PUBLIC to be displayed to all users
677 * Revision::FOR_THIS_USER to be displayed to the given user
678 * Revision::RAW get the ID regardless of permissions
679 * @param $user User object to check for, only if FOR_THIS_USER is passed
680 * to the $audience parameter
683 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
684 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
686 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
694 * Fetch revision's user id without regard for the current user's permissions
698 public function getRawUser() {
703 * Fetch revision's username if it's available to the specified audience.
704 * If the specified audience does not have access to the username, an
705 * empty string will be returned.
707 * @param $audience Integer: one of:
708 * Revision::FOR_PUBLIC to be displayed to all users
709 * Revision::FOR_THIS_USER to be displayed to the given user
710 * Revision::RAW get the text regardless of permissions
711 * @param $user User object to check for, only if FOR_THIS_USER is passed
712 * to the $audience parameter
715 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
716 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
718 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
721 return $this->getRawUserText();
726 * Fetch revision's username without regard for view restrictions
730 public function getRawUserText() {
731 if ( $this->mUserText
=== null ) {
732 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
733 if ( $this->mUserText
=== false ) {
734 # This shouldn't happen, but it can if the wiki was recovered
735 # via importing revs and there is no user table entry yet.
736 $this->mUserText
= $this->mOrigUserText
;
739 return $this->mUserText
;
743 * Fetch revision comment if it's available to the specified audience.
744 * If the specified audience does not have access to the comment, an
745 * empty string will be returned.
747 * @param $audience Integer: one of:
748 * Revision::FOR_PUBLIC to be displayed to all users
749 * Revision::FOR_THIS_USER to be displayed to the given user
750 * Revision::RAW get the text regardless of permissions
751 * @param $user User object to check for, only if FOR_THIS_USER is passed
752 * to the $audience parameter
755 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
756 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
758 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
761 return $this->mComment
;
766 * Fetch revision comment without regard for the current user's permissions
770 public function getRawComment() {
771 return $this->mComment
;
777 public function isMinor() {
778 return (bool)$this->mMinorEdit
;
782 * @return Integer rcid of the unpatrolled row, zero if there isn't one
784 public function isUnpatrolled() {
785 if( $this->mUnpatrolled
!== null ) {
786 return $this->mUnpatrolled
;
788 $dbr = wfGetDB( DB_SLAVE
);
789 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
791 array( // Add redundant user,timestamp condition so we can use the existing index
792 'rc_user_text' => $this->getRawUserText(),
793 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
794 'rc_this_oldid' => $this->getId(),
799 return (int)$this->mUnpatrolled
;
803 * @param $field int one of DELETED_* bitfield constants
807 public function isDeleted( $field ) {
808 return ( $this->mDeleted
& $field ) == $field;
812 * Get the deletion bitfield of the revision
816 public function getVisibility() {
817 return (int)$this->mDeleted
;
821 * Fetch revision text if it's available to the specified audience.
822 * If the specified audience does not have the ability to view this
823 * revision, an empty string will be returned.
825 * @param $audience Integer: one of:
826 * Revision::FOR_PUBLIC to be displayed to all users
827 * Revision::FOR_THIS_USER to be displayed to the given user
828 * Revision::RAW get the text regardless of permissions
829 * @param $user User object to check for, only if FOR_THIS_USER is passed
830 * to the $audience parameter
832 * @deprecated in 1.WD, use getContent() instead
834 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) { #FIXME: deprecated, replace usage! #FIXME: used a LOT!
835 wfDeprecated( __METHOD__
, '1.WD' );
837 $content = $this->getContent( $audience, $user );
838 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
842 * Fetch revision content if it's available to the specified audience.
843 * If the specified audience does not have the ability to view this
844 * revision, null will be returned.
846 * @param $audience Integer: one of:
847 * Revision::FOR_PUBLIC to be displayed to all users
848 * Revision::FOR_THIS_USER to be displayed to $wgUser
849 * Revision::RAW get the text regardless of permissions
850 * @param $user User object to check for, only if FOR_THIS_USER is passed
851 * to the $audience parameter
856 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
857 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
859 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
862 return $this->getContentInternal();
867 * Alias for getText(Revision::FOR_THIS_USER)
869 * @deprecated since 1.17
872 public function revText() {
873 wfDeprecated( __METHOD__
, '1.17' );
874 return $this->getText( self
::FOR_THIS_USER
);
878 * Fetch revision text without regard for view restrictions
882 * @deprecated since 1.WD. Instead, use Revision::getContent( Revision::RAW ) or Revision::getSerializedData() as appropriate.
884 public function getRawText() { #FIXME: deprecated, replace usage!
885 wfDeprecated( __METHOD__
, "1.WD" );
887 return $this->getText( self
::RAW
);
891 * Fetch original serialized data without regard for view restrictions
897 public function getSerializedData() {
901 protected function getContentInternal() {
902 if( is_null( $this->mContent
) ) {
903 // Revision is immutable. Load on demand:
905 $handler = $this->getContentHandler();
906 $format = $this->getContentFormat();
907 $title = $this->getTitle();
909 if( is_null( $this->mText
) ) {
910 // Load text on demand:
911 $this->mText
= $this->loadText();
914 $this->mContent
= is_null( $this->mText
) ?
null : $handler->unserializeContent( $this->mText
, $format );
917 return $this->mContent
;
921 * Returns the content model for this revision.
923 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
924 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
925 * is used as a last resort.
927 * @return int the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
929 public function getContentModel() {
930 if ( !$this->mContentModel
) {
931 $title = $this->getTitle();
932 $this->mContentModel
= ( $title ?
$title->getContentModel() : CONTENT_MODEL_WIKITEXT
);
934 assert( !empty( $this->mContentModel
) );
937 return $this->mContentModel
;
941 * Returns the content format for this revision.
943 * If no content format was stored in the database, the default format for this
944 * revision's content model is returned.
946 * @return int the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
948 public function getContentFormat() {
949 if ( !$this->mContentFormat
) {
950 $handler = $this->getContentHandler();
951 $this->mContentFormat
= $handler->getDefaultFormat();
953 assert( !empty( $this->mContentFormat
) );
956 return $this->mContentFormat
;
960 * Returns the content handler appropriate for this revision's content model.
962 * @return ContentHandler
964 public function getContentHandler() {
965 if ( !$this->mContentHandler
) {
966 $model = $this->getContentModel();
967 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
969 $format = $this->getContentFormat();
971 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
972 $formatName = ContentHandler
::getContentFormatMimeType( $format );
973 $modelName = ContentHandler
::getContentModelName( $model );
975 throw new MWException( "Oops, the content format #$format ($formatName) is not supported for this content model, #$model ($modelName)" );
979 return $this->mContentHandler
;
985 public function getTimestamp() {
986 return wfTimestamp( TS_MW
, $this->mTimestamp
);
992 public function isCurrent() {
993 return $this->mCurrent
;
997 * Get previous revision for this title
999 * @return Revision or null
1001 public function getPrevious() {
1002 if( $this->getTitle() ) {
1003 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1005 return Revision
::newFromTitle( $this->getTitle(), $prev );
1012 * Get next revision for this title
1014 * @return Revision or null
1016 public function getNext() {
1017 if( $this->getTitle() ) {
1018 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1020 return Revision
::newFromTitle( $this->getTitle(), $next );
1027 * Get previous revision Id for this page_id
1028 * This is used to populate rev_parent_id on save
1030 * @param $db DatabaseBase
1033 private function getPreviousRevisionId( $db ) {
1034 if( is_null( $this->mPage
) ) {
1037 # Use page_latest if ID is not given
1039 $prevId = $db->selectField( 'page', 'page_latest',
1040 array( 'page_id' => $this->mPage
),
1043 $prevId = $db->selectField( 'revision', 'rev_id',
1044 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
1046 array( 'ORDER BY' => 'rev_id DESC' ) );
1048 return intval( $prevId );
1052 * Get revision text associated with an old or archive row
1053 * $row is usually an object from wfFetchRow(), both the flags and the text
1054 * field must be included
1056 * @param $row Object: the text data
1057 * @param $prefix String: table prefix (default 'old_')
1058 * @return String: text the text requested or false on failure
1060 public static function getRevisionText( $row, $prefix = 'old_' ) {
1061 wfProfileIn( __METHOD__
);
1064 $textField = $prefix . 'text';
1065 $flagsField = $prefix . 'flags';
1067 if( isset( $row->$flagsField ) ) {
1068 $flags = explode( ',', $row->$flagsField );
1073 if( isset( $row->$textField ) ) {
1074 $text = $row->$textField;
1076 wfProfileOut( __METHOD__
);
1080 # Use external methods for external objects, text in table is URL-only then
1081 if ( in_array( 'external', $flags ) ) {
1083 $parts = explode( '://', $url, 2 );
1084 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
1085 wfProfileOut( __METHOD__
);
1088 $text = ExternalStore
::fetchFromURL( $url );
1091 // If the text was fetched without an error, convert it
1092 if ( $text !== false ) {
1093 if( in_array( 'gzip', $flags ) ) {
1094 # Deal with optional compression of archived pages.
1095 # This can be done periodically via maintenance/compressOld.php, and
1096 # as pages are saved if $wgCompressRevisions is set.
1097 $text = gzinflate( $text );
1100 if( in_array( 'object', $flags ) ) {
1101 # Generic compressed storage
1102 $obj = unserialize( $text );
1103 if ( !is_object( $obj ) ) {
1105 wfProfileOut( __METHOD__
);
1108 $text = $obj->getText();
1111 global $wgLegacyEncoding;
1112 if( $text !== false && $wgLegacyEncoding
1113 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1115 # Old revisions kept around in a legacy encoding?
1116 # Upconvert on demand.
1117 # ("utf8" checked for compatibility with some broken
1118 # conversion scripts 2008-12-30)
1120 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1123 wfProfileOut( __METHOD__
);
1128 * If $wgCompressRevisions is enabled, we will compress data.
1129 * The input string is modified in place.
1130 * Return value is the flags field: contains 'gzip' if the
1131 * data is compressed, and 'utf-8' if we're saving in UTF-8
1134 * @param $text Mixed: reference to a text
1137 public static function compressRevisionText( &$text ) {
1138 global $wgCompressRevisions;
1141 # Revisions not marked this way will be converted
1142 # on load if $wgLegacyCharset is set in the future.
1145 if( $wgCompressRevisions ) {
1146 if( function_exists( 'gzdeflate' ) ) {
1147 $text = gzdeflate( $text );
1150 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
1153 return implode( ',', $flags );
1157 * Insert a new revision into the database, returning the new revision ID
1158 * number on success and dies horribly on failure.
1160 * @param $dbw DatabaseBase: (master connection)
1163 public function insertOn( $dbw ) {
1164 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1166 wfProfileIn( __METHOD__
);
1168 $data = $this->mText
;
1169 $flags = Revision
::compressRevisionText( $data );
1171 # Write to external storage if required
1172 if( $wgDefaultExternalStore ) {
1173 // Store and get the URL
1174 $data = ExternalStore
::insertToDefault( $data );
1176 throw new MWException( "Unable to store text to external storage" );
1181 $flags .= 'external';
1184 # Record the text (or external storage URL) to the text table
1185 if( !isset( $this->mTextId
) ) {
1186 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1187 $dbw->insert( 'text',
1189 'old_id' => $old_id,
1190 'old_text' => $data,
1191 'old_flags' => $flags,
1194 $this->mTextId
= $dbw->insertId();
1197 if ( $this->mComment
=== null ) $this->mComment
= "";
1199 # Record the edit in revisions
1200 $rev_id = isset( $this->mId
)
1202 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1205 'rev_id' => $rev_id,
1206 'rev_page' => $this->mPage
,
1207 'rev_text_id' => $this->mTextId
,
1208 'rev_comment' => $this->mComment
,
1209 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1210 'rev_user' => $this->mUser
,
1211 'rev_user_text' => $this->mUserText
,
1212 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1213 'rev_deleted' => $this->mDeleted
,
1214 'rev_len' => $this->mSize
,
1215 'rev_parent_id' => is_null( $this->mParentId
)
1216 ?
$this->getPreviousRevisionId( $dbw )
1218 'rev_sha1' => is_null( $this->mSha1
)
1219 ? Revision
::base36Sha1( $this->mText
)
1223 if ( $wgContentHandlerUseDB ) {
1224 $row[ 'rev_content_model' ] = $this->getContentModel();
1225 $row[ 'rev_content_format' ] = $this->getContentFormat();
1228 $dbw->insert( 'revision', $row, __METHOD__
);
1230 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1232 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1234 wfProfileOut( __METHOD__
);
1239 * Get the base 36 SHA-1 value for a string of text
1240 * @param $text String
1243 public static function base36Sha1( $text ) {
1244 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1248 * Lazy-load the revision's text.
1249 * Currently hardcoded to the 'text' table storage engine.
1253 protected function loadText() {
1254 wfProfileIn( __METHOD__
);
1256 // Caching may be beneficial for massive use of external storage
1257 global $wgRevisionCacheExpiry, $wgMemc;
1258 $textId = $this->getTextId();
1259 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1260 if( $wgRevisionCacheExpiry ) {
1261 $text = $wgMemc->get( $key );
1262 if( is_string( $text ) ) {
1263 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1264 wfProfileOut( __METHOD__
);
1269 // If we kept data for lazy extraction, use it now...
1270 if ( isset( $this->mTextRow
) ) {
1271 $row = $this->mTextRow
;
1272 $this->mTextRow
= null;
1278 // Text data is immutable; check slaves first.
1279 $dbr = wfGetDB( DB_SLAVE
);
1280 $row = $dbr->selectRow( 'text',
1281 array( 'old_text', 'old_flags' ),
1282 array( 'old_id' => $this->getTextId() ),
1286 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1287 // Possible slave lag!
1288 $dbw = wfGetDB( DB_MASTER
);
1289 $row = $dbw->selectRow( 'text',
1290 array( 'old_text', 'old_flags' ),
1291 array( 'old_id' => $this->getTextId() ),
1295 $text = self
::getRevisionText( $row );
1297 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1298 if( $wgRevisionCacheExpiry && $text !== false ) {
1299 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1302 wfProfileOut( __METHOD__
);
1308 * Create a new null-revision for insertion into a page's
1309 * history. This will not re-save the text, but simply refer
1310 * to the text from the previous version.
1312 * Such revisions can for instance identify page rename
1313 * operations and other such meta-modifications.
1315 * @param $dbw DatabaseBase
1316 * @param $pageId Integer: ID number of the page to read from
1317 * @param $summary String: revision's summary
1318 * @param $minor Boolean: whether the revision should be considered as minor
1319 * @return Revision|null on error
1321 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1322 global $wgContentHandlerUseDB;
1324 wfProfileIn( __METHOD__
);
1326 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1327 'rev_text_id', 'rev_len', 'rev_sha1' );
1329 if ( $wgContentHandlerUseDB ) {
1330 $fields[] = 'rev_content_model';
1331 $fields[] = 'rev_content_format';
1334 $current = $dbw->selectRow(
1335 array( 'page', 'revision' ),
1338 'page_id' => $pageId,
1339 'page_latest=rev_id',
1346 'comment' => $summary,
1347 'minor_edit' => $minor,
1348 'text_id' => $current->rev_text_id
,
1349 'parent_id' => $current->page_latest
,
1350 'len' => $current->rev_len
,
1351 'sha1' => $current->rev_sha1
1354 if ( $wgContentHandlerUseDB ) {
1355 $row[ 'content_model' ] = $current->rev_content_model
;
1356 $row[ 'content_format' ] = $current->rev_content_format
;
1359 $revision = new Revision( $row );
1360 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1365 wfProfileOut( __METHOD__
);
1370 * Determine if the current user is allowed to view a particular
1371 * field of this revision, if it's marked as deleted.
1373 * @param $field Integer:one of self::DELETED_TEXT,
1374 * self::DELETED_COMMENT,
1375 * self::DELETED_USER
1376 * @param $user User object to check, or null to use $wgUser
1379 public function userCan( $field, User
$user = null ) {
1380 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1384 * Determine if the current user is allowed to view a particular
1385 * field of this revision, if it's marked as deleted. This is used
1386 * by various classes to avoid duplication.
1388 * @param $bitfield Integer: current field
1389 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1390 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1391 * self::DELETED_USER = File::DELETED_USER
1392 * @param $user User object to check, or null to use $wgUser
1395 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1396 if( $bitfield & $field ) { // aspect is deleted
1397 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1398 $permission = 'suppressrevision';
1399 } elseif ( $field & self
::DELETED_TEXT
) {
1400 $permission = 'deletedtext';
1402 $permission = 'deletedhistory';
1404 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1405 if ( $user === null ) {
1409 return $user->isAllowed( $permission );
1416 * Get rev_timestamp from rev_id, without loading the rest of the row
1418 * @param $title Title
1419 * @param $id Integer
1422 static function getTimestampFromId( $title, $id ) {
1423 $dbr = wfGetDB( DB_SLAVE
);
1424 // Casting fix for DB2
1428 $conds = array( 'rev_id' => $id );
1429 $conds['rev_page'] = $title->getArticleID();
1430 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1431 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1432 # Not in slave, try master
1433 $dbw = wfGetDB( DB_MASTER
);
1434 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1436 return wfTimestamp( TS_MW
, $timestamp );
1440 * Get count of revisions per page...not very efficient
1442 * @param $db DatabaseBase
1443 * @param $id Integer: page id
1446 static function countByPageId( $db, $id ) {
1447 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1448 array( 'rev_page' => $id ), __METHOD__
);
1450 return $row->revCount
;
1456 * Get count of revisions per page...not very efficient
1458 * @param $db DatabaseBase
1459 * @param $title Title
1462 static function countByTitle( $db, $title ) {
1463 $id = $title->getArticleID();
1465 return Revision
::countByPageId( $db, $id );