10 const DELETED_TEXT
= 1;
11 const DELETED_COMMENT
= 2;
12 const DELETED_USER
= 4;
13 const DELETED_RESTRICTED
= 8;
16 * Load a page revision from a given revision ID number.
17 * Returns null if no such revision can be found.
23 public static function newFromId( $id ) {
24 return Revision
::newFromConds(
25 array( 'page_id=rev_page',
26 'rev_id' => intval( $id ) ) );
30 * Load either the current, or a specified, revision
31 * that's attached to a given title. If not attached
32 * to that title, will return null.
40 public static function newFromTitle( &$title, $id = 0 ) {
42 $matchId = intval( $id );
44 $matchId = 'page_latest';
46 return Revision
::newFromConds(
47 array( "rev_id=$matchId",
49 'page_namespace' => $title->getNamespace(),
50 'page_title' => $title->getDBkey() ) );
54 * Load a page revision from a given revision ID number.
55 * Returns null if no such revision can be found.
62 public static function loadFromId( &$db, $id ) {
63 return Revision
::loadFromConds( $db,
64 array( 'page_id=rev_page',
65 'rev_id' => intval( $id ) ) );
69 * Load either the current, or a specified, revision
70 * that's attached to a given page. If not attached
71 * to that page, will return null.
80 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
81 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
83 $conds['rev_id']=intval($id);
85 $conds[]='rev_id=page_latest';
87 return Revision
::loadFromConds( $db, $conds );
91 * Load either the current, or a specified, revision
92 * that's attached to a given page. If not attached
93 * to that page, will return null.
102 public static function loadFromTitle( &$db, $title, $id = 0 ) {
104 $matchId = intval( $id );
106 $matchId = 'page_latest';
108 return Revision
::loadFromConds(
110 array( "rev_id=$matchId",
112 'page_namespace' => $title->getNamespace(),
113 'page_title' => $title->getDBkey() ) );
117 * Load the revision for the given title with the given timestamp.
118 * WARNING: Timestamps may in some circumstances not be unique,
119 * so this isn't the best key to use.
121 * @param Database $db
122 * @param Title $title
123 * @param string $timestamp
128 public static function loadFromTimestamp( &$db, &$title, $timestamp ) {
129 return Revision
::loadFromConds(
131 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
133 'page_namespace' => $title->getNamespace(),
134 'page_title' => $title->getDBkey() ) );
138 * Given a set of conditions, fetch a revision.
140 * @param array $conditions
145 private static function newFromConds( $conditions ) {
146 $db = wfGetDB( DB_SLAVE
);
147 $row = Revision
::loadFromConds( $db, $conditions );
148 if( is_null( $row ) ) {
149 $dbw = wfGetDB( DB_MASTER
);
150 $row = Revision
::loadFromConds( $dbw, $conditions );
156 * Given a set of conditions, fetch a revision from
157 * the given database connection.
159 * @param Database $db
160 * @param array $conditions
165 private static function loadFromConds( $db, $conditions ) {
166 $res = Revision
::fetchFromConds( $db, $conditions );
168 $row = $res->fetchObject();
171 $ret = new Revision( $row );
180 * Return a wrapper for a series of database rows to
181 * fetch all of a given page's revisions in turn.
182 * Each row can be fed to the constructor to get objects.
184 * @param Title $title
185 * @return ResultWrapper
189 public static function fetchAllRevisions( &$title ) {
190 return Revision
::fetchFromConds(
192 array( 'page_namespace' => $title->getNamespace(),
193 'page_title' => $title->getDBkey(),
194 'page_id=rev_page' ) );
198 * Return a wrapper for a series of database rows to
199 * fetch all of a given page's revisions in turn.
200 * Each row can be fed to the constructor to get objects.
202 * @param Title $title
203 * @return ResultWrapper
207 public static function fetchRevision( &$title ) {
208 return Revision
::fetchFromConds(
210 array( 'rev_id=page_latest',
211 'page_namespace' => $title->getNamespace(),
212 'page_title' => $title->getDBkey(),
213 'page_id=rev_page' ) );
217 * Given a set of conditions, return a ResultWrapper
218 * which will return matching database rows with the
219 * fields necessary to build Revision objects.
221 * @param Database $db
222 * @param array $conditions
223 * @return ResultWrapper
227 private static function fetchFromConds( $db, $conditions ) {
228 $fields = self
::selectFields();
229 $fields[] = 'page_namespace';
230 $fields[] = 'page_title';
231 $fields[] = 'page_latest';
233 array( 'page', 'revision' ),
236 'Revision::fetchRow',
237 array( 'LIMIT' => 1 ) );
238 $ret = $db->resultObject( $res );
243 * Return the list of revision fields that should be selected to create
246 static function selectFields() {
266 function Revision( $row ) {
267 if( is_object( $row ) ) {
268 $this->mId
= intval( $row->rev_id
);
269 $this->mPage
= intval( $row->rev_page
);
270 $this->mTextId
= intval( $row->rev_text_id
);
271 $this->mComment
= $row->rev_comment
;
272 $this->mUserText
= $row->rev_user_text
;
273 $this->mUser
= intval( $row->rev_user
);
274 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
275 $this->mTimestamp
= $row->rev_timestamp
;
276 $this->mDeleted
= intval( $row->rev_deleted
);
278 if( !isset( $row->rev_parent_id
) )
279 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
281 $this->mParentId
= intval( $row->rev_parent_id
);
283 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
286 $this->mSize
= intval( $row->rev_len
);
288 if( isset( $row->page_latest
) ) {
289 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
290 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
293 $this->mCurrent
= false;
294 $this->mTitle
= null;
297 // Lazy extraction...
299 if( isset( $row->old_text
) ) {
300 $this->mTextRow
= $row;
302 // 'text' table row entry will be lazy-loaded
303 $this->mTextRow
= null;
305 } elseif( is_array( $row ) ) {
306 // Build a new revision to be saved...
309 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
310 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
311 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
312 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
313 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
314 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
315 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
316 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
317 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
318 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
320 // Enforce spacing trimming on supplied text
321 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
322 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
323 $this->mTextRow
= null;
325 $this->mTitle
= null; # Load on demand if needed
326 $this->mCurrent
= false;
327 # If we still have no len_size, see it we have the text to figure it out
329 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
331 throw new MWException( 'Revision constructor passed invalid row format.' );
343 public function getId() {
351 public function getTextId() {
352 return $this->mTextId
;
356 * Get parent revision ID (the original previous page revision)
359 public function getParentId() {
360 return $this->mParentId
;
364 * Returns the length of the text in this revision, or null if unknown.
367 public function getSize() {
372 * Returns the title of the page associated with this entry.
375 public function getTitle() {
376 if( isset( $this->mTitle
) ) {
377 return $this->mTitle
;
379 $dbr = wfGetDB( DB_SLAVE
);
380 $row = $dbr->selectRow(
381 array( 'page', 'revision' ),
382 array( 'page_namespace', 'page_title' ),
383 array( 'page_id=rev_page',
384 'rev_id' => $this->mId
),
385 'Revision::getTitle' );
387 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
390 return $this->mTitle
;
394 * Set the title of the revision
395 * @param Title $title
397 public function setTitle( $title ) {
398 $this->mTitle
= $title;
405 public function getPage() {
410 * Fetch revision's user id if it's available to all users
413 public function getUser() {
414 if( $this->isDeleted( self
::DELETED_USER
) ) {
422 * Fetch revision's user id without regard for the current user's permissions
425 public function getRawUser() {
430 * Fetch revision's username if it's available to all users
433 public function getUserText() {
434 if( $this->isDeleted( self
::DELETED_USER
) ) {
437 return $this->mUserText
;
442 * Fetch revision's username without regard for view restrictions
445 public function getRawUserText() {
446 return $this->mUserText
;
450 * Fetch revision comment if it's available to all users
453 function getComment() {
454 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
457 return $this->mComment
;
462 * Fetch revision comment without regard for the current user's permissions
465 public function getRawComment() {
466 return $this->mComment
;
472 public function isMinor() {
473 return (bool)$this->mMinorEdit
;
477 * int $field one of DELETED_* bitfield constants
480 public function isDeleted( $field ) {
481 return ($this->mDeleted
& $field) == $field;
485 * Fetch revision text if it's available to all users
488 public function getText() {
489 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
492 return $this->getRawText();
497 * Fetch revision text without regard for view restrictions
500 public function getRawText() {
501 if( is_null( $this->mText
) ) {
502 // Revision text is immutable. Load on demand:
503 $this->mText
= $this->loadText();
509 * Fetch revision text if it's available to THIS user
512 public function revText() {
513 if( !$this->userCan( self
::DELETED_TEXT
) ) {
516 return $this->getRawText();
523 public function getTimestamp() {
524 return wfTimestamp(TS_MW
, $this->mTimestamp
);
530 public function isCurrent() {
531 return $this->mCurrent
;
535 * Get previous revision for this title
538 public function getPrevious() {
539 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
541 return Revision
::newFromTitle( $this->mTitle
, $prev );
550 public function getNext() {
551 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
553 return Revision
::newFromTitle( $this->mTitle
, $next );
560 * Get previous revision Id for this page_id
561 * This is used to populate rev_parent_id on save
562 * @param Database $db
565 private function getPreviousRevisionId( $db ) {
566 if( is_null($this->mPage
) ) {
569 # Use page_latest if ID is not given
571 $prevId = $db->selectField( 'page', 'page_latest',
572 array( 'page_id' => $this->mPage
),
575 $prevId = $db->selectField( 'revision', 'rev_id',
576 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
578 array( 'ORDER BY' => 'rev_id DESC' ) );
580 return intval($prevId);
584 * Get revision text associated with an old or archive row
585 * $row is usually an object from wfFetchRow(), both the flags and the text
586 * field must be included
588 * @param integer $row Id of a row
589 * @param string $prefix table prefix (default 'old_')
590 * @return string $text|false the text requested
592 public static function getRevisionText( $row, $prefix = 'old_' ) {
593 wfProfileIn( __METHOD__
);
596 $textField = $prefix . 'text';
597 $flagsField = $prefix . 'flags';
599 if( isset( $row->$flagsField ) ) {
600 $flags = explode( ',', $row->$flagsField );
605 if( isset( $row->$textField ) ) {
606 $text = $row->$textField;
608 wfProfileOut( __METHOD__
);
612 # Use external methods for external objects, text in table is URL-only then
613 if ( in_array( 'external', $flags ) ) {
615 @list
(/* $proto */,$path)=explode('://',$url,2);
617 wfProfileOut( __METHOD__
);
620 $text=ExternalStore
::fetchFromURL($url);
623 // If the text was fetched without an error, convert it
624 if ( $text !== false ) {
625 if( in_array( 'gzip', $flags ) ) {
626 # Deal with optional compression of archived pages.
627 # This can be done periodically via maintenance/compressOld.php, and
628 # as pages are saved if $wgCompressRevisions is set.
629 $text = gzinflate( $text );
632 if( in_array( 'object', $flags ) ) {
633 # Generic compressed storage
634 $obj = unserialize( $text );
635 if ( !is_object( $obj ) ) {
637 wfProfileOut( __METHOD__
);
640 $text = $obj->getText();
643 global $wgLegacyEncoding;
644 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
645 # Old revisions kept around in a legacy encoding?
646 # Upconvert on demand.
647 global $wgInputEncoding, $wgContLang;
648 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
651 wfProfileOut( __METHOD__
);
656 * If $wgCompressRevisions is enabled, we will compress data.
657 * The input string is modified in place.
658 * Return value is the flags field: contains 'gzip' if the
659 * data is compressed, and 'utf-8' if we're saving in UTF-8
662 * @param mixed $text reference to a text
665 public static function compressRevisionText( &$text ) {
666 global $wgCompressRevisions;
669 # Revisions not marked this way will be converted
670 # on load if $wgLegacyCharset is set in the future.
673 if( $wgCompressRevisions ) {
674 if( function_exists( 'gzdeflate' ) ) {
675 $text = gzdeflate( $text );
678 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
681 return implode( ',', $flags );
685 * Insert a new revision into the database, returning the new revision ID
686 * number on success and dies horribly on failure.
688 * @param Database $dbw
691 public function insertOn( &$dbw ) {
692 global $wgDefaultExternalStore;
694 wfProfileIn( __METHOD__
);
696 $data = $this->mText
;
697 $flags = Revision
::compressRevisionText( $data );
699 # Write to external storage if required
700 if ( $wgDefaultExternalStore ) {
701 if ( is_array( $wgDefaultExternalStore ) ) {
702 // Distribute storage across multiple clusters
703 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
705 $store = $wgDefaultExternalStore;
707 // Store and get the URL
708 $data = ExternalStore
::insert( $store, $data );
710 # This should only happen in the case of a configuration error, where the external store is not valid
711 throw new MWException( "Unable to store text to external storage $store" );
716 $flags .= 'external';
719 # Record the text (or external storage URL) to the text table
720 if( !isset( $this->mTextId
) ) {
721 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
722 $dbw->insert( 'text',
726 'old_flags' => $flags,
729 $this->mTextId
= $dbw->insertId();
732 # Record the edit in revisions
733 $rev_id = isset( $this->mId
)
735 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
736 $dbw->insert( 'revision',
739 'rev_page' => $this->mPage
,
740 'rev_text_id' => $this->mTextId
,
741 'rev_comment' => $this->mComment
,
742 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
743 'rev_user' => $this->mUser
,
744 'rev_user_text' => $this->mUserText
,
745 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
746 'rev_deleted' => $this->mDeleted
,
747 'rev_len' => $this->mSize
,
748 'rev_parent_id' => $this->mParentId ?
$this->mParentId
: $this->getPreviousRevisionId( $dbw )
752 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
754 wfRunHooks( 'RevisionInsertComplete', array( &$this ) );
756 wfProfileOut( __METHOD__
);
761 * Lazy-load the revision's text.
762 * Currently hardcoded to the 'text' table storage engine.
766 private function loadText() {
767 wfProfileIn( __METHOD__
);
769 // Caching may be beneficial for massive use of external storage
770 global $wgRevisionCacheExpiry, $wgMemc;
771 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
772 if( $wgRevisionCacheExpiry ) {
773 $text = $wgMemc->get( $key );
774 if( is_string( $text ) ) {
775 wfProfileOut( __METHOD__
);
780 // If we kept data for lazy extraction, use it now...
781 if ( isset( $this->mTextRow
) ) {
782 $row = $this->mTextRow
;
783 $this->mTextRow
= null;
789 // Text data is immutable; check slaves first.
790 $dbr = wfGetDB( DB_SLAVE
);
791 $row = $dbr->selectRow( 'text',
792 array( 'old_text', 'old_flags' ),
793 array( 'old_id' => $this->getTextId() ),
798 // Possible slave lag!
799 $dbw = wfGetDB( DB_MASTER
);
800 $row = $dbw->selectRow( 'text',
801 array( 'old_text', 'old_flags' ),
802 array( 'old_id' => $this->getTextId() ),
806 $text = self
::getRevisionText( $row );
808 if( $wgRevisionCacheExpiry ) {
809 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
812 wfProfileOut( __METHOD__
);
818 * Create a new null-revision for insertion into a page's
819 * history. This will not re-save the text, but simply refer
820 * to the text from the previous version.
822 * Such revisions can for instance identify page rename
823 * operations and other such meta-modifications.
825 * @param Database $dbw
826 * @param int $pageId ID number of the page to read from
827 * @param string $summary
831 public static function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
832 wfProfileIn( __METHOD__
);
834 $current = $dbw->selectRow(
835 array( 'page', 'revision' ),
836 array( 'page_latest', 'rev_text_id' ),
838 'page_id' => $pageId,
839 'page_latest=rev_id',
844 $revision = new Revision( array(
846 'comment' => $summary,
847 'minor_edit' => $minor,
848 'text_id' => $current->rev_text_id
,
849 'parent_id' => $current->page_latest
855 wfProfileOut( __METHOD__
);
860 * Determine if the current user is allowed to view a particular
861 * field of this revision, if it's marked as deleted.
862 * @param int $field one of self::DELETED_TEXT,
863 * self::DELETED_COMMENT,
867 public function userCan( $field ) {
868 if( ( $this->mDeleted
& $field ) == $field ) {
870 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
873 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
874 return $wgUser->isAllowed( $permission );
882 * Get rev_timestamp from rev_id, without loading the rest of the row
885 static function getTimestampFromID( $id ) {
886 $dbr = wfGetDB( DB_SLAVE
);
887 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
888 array( 'rev_id' => $id ), __METHOD__
);
889 if ( $timestamp === false ) {
890 # Not in slave, try master
891 $dbw = wfGetDB( DB_MASTER
);
892 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
893 array( 'rev_id' => $id ), __METHOD__
);
899 * Get count of revisions per page...not very efficient
900 * @param Database $db
901 * @param int $id, page id
903 static function countByPageId( $db, $id ) {
904 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
905 array( 'rev_page' => $id ), __METHOD__
);
907 return $row->revCount
;
913 * Get count of revisions per page...not very efficient
914 * @param Database $db
915 * @param Title $title
917 static function countByTitle( $db, $title ) {
918 $id = $title->getArticleId();
920 return Revision
::countByPageId( $db, $id );
927 * Aliases for backwards compatibility with 1.6
929 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
930 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
931 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
932 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);