11 const DELETED_TEXT
= 1;
12 const DELETED_COMMENT
= 2;
13 const DELETED_USER
= 4;
14 const DELETED_RESTRICTED
= 8;
17 * Load a page revision from a given revision ID number.
18 * Returns null if no such revision can be found.
24 public static function newFromId( $id ) {
25 return Revision
::newFromConds(
26 array( 'page_id=rev_page',
27 'rev_id' => intval( $id ) ) );
31 * Load either the current, or a specified, revision
32 * that's attached to a given title. If not attached
33 * to that title, will return null.
41 public static function newFromTitle( &$title, $id = 0 ) {
43 $matchId = intval( $id );
45 $matchId = 'page_latest';
47 return Revision
::newFromConds(
48 array( "rev_id=$matchId",
50 'page_namespace' => $title->getNamespace(),
51 'page_title' => $title->getDBkey() ) );
55 * Load a page revision from a given revision ID number.
56 * Returns null if no such revision can be found.
63 public static function loadFromId( &$db, $id ) {
64 return Revision
::loadFromConds( $db,
65 array( 'page_id=rev_page',
66 'rev_id' => intval( $id ) ) );
70 * Load either the current, or a specified, revision
71 * that's attached to a given page. If not attached
72 * to that page, will return null.
81 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
82 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
84 $conds['rev_id']=intval($id);
86 $conds[]='rev_id=page_latest';
88 return Revision
::loadFromConds( $db, $conds );
92 * Load either the current, or a specified, revision
93 * that's attached to a given page. If not attached
94 * to that page, will return null.
103 public static function loadFromTitle( &$db, $title, $id = 0 ) {
105 $matchId = intval( $id );
107 $matchId = 'page_latest';
109 return Revision
::loadFromConds(
111 array( "rev_id=$matchId",
113 'page_namespace' => $title->getNamespace(),
114 'page_title' => $title->getDBkey() ) );
118 * Load the revision for the given title with the given timestamp.
119 * WARNING: Timestamps may in some circumstances not be unique,
120 * so this isn't the best key to use.
122 * @param Database $db
123 * @param Title $title
124 * @param string $timestamp
129 public static function loadFromTimestamp( &$db, &$title, $timestamp ) {
130 return Revision
::loadFromConds(
132 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
134 'page_namespace' => $title->getNamespace(),
135 'page_title' => $title->getDBkey() ) );
139 * Given a set of conditions, fetch a revision.
141 * @param array $conditions
146 private static function newFromConds( $conditions ) {
147 $db = wfGetDB( DB_SLAVE
);
148 $row = Revision
::loadFromConds( $db, $conditions );
149 if( is_null( $row ) ) {
150 $dbw = wfGetDB( DB_MASTER
);
151 $row = Revision
::loadFromConds( $dbw, $conditions );
157 * Given a set of conditions, fetch a revision from
158 * the given database connection.
160 * @param Database $db
161 * @param array $conditions
166 private static function loadFromConds( $db, $conditions ) {
167 $res = Revision
::fetchFromConds( $db, $conditions );
169 $row = $res->fetchObject();
172 $ret = new Revision( $row );
181 * Return a wrapper for a series of database rows to
182 * fetch all of a given page's revisions in turn.
183 * Each row can be fed to the constructor to get objects.
185 * @param Title $title
186 * @return ResultWrapper
190 public static function fetchAllRevisions( &$title ) {
191 return Revision
::fetchFromConds(
193 array( 'page_namespace' => $title->getNamespace(),
194 'page_title' => $title->getDBkey(),
195 'page_id=rev_page' ) );
199 * Return a wrapper for a series of database rows to
200 * fetch all of a given page's revisions in turn.
201 * Each row can be fed to the constructor to get objects.
203 * @param Title $title
204 * @return ResultWrapper
208 public static function fetchRevision( &$title ) {
209 return Revision
::fetchFromConds(
211 array( 'rev_id=page_latest',
212 'page_namespace' => $title->getNamespace(),
213 'page_title' => $title->getDBkey(),
214 'page_id=rev_page' ) );
218 * Given a set of conditions, return a ResultWrapper
219 * which will return matching database rows with the
220 * fields necessary to build Revision objects.
222 * @param Database $db
223 * @param array $conditions
224 * @return ResultWrapper
228 private static function fetchFromConds( $db, $conditions ) {
229 $fields = self
::selectFields();
230 $fields[] = 'page_namespace';
231 $fields[] = 'page_title';
232 $fields[] = 'page_latest';
234 array( 'page', 'revision' ),
237 'Revision::fetchRow',
238 array( 'LIMIT' => 1 ) );
239 $ret = $db->resultObject( $res );
244 * Return the list of revision fields that should be selected to create
247 static function selectFields() {
264 * Return the list of text fields that should be selected to read the
267 static function selectTextFields() {
274 * Return the list of page fields that should be selected from page table
276 static function selectPageFields() {
288 function Revision( $row ) {
289 if( is_object( $row ) ) {
290 $this->mId
= intval( $row->rev_id
);
291 $this->mPage
= intval( $row->rev_page
);
292 $this->mTextId
= intval( $row->rev_text_id
);
293 $this->mComment
= $row->rev_comment
;
294 $this->mUserText
= $row->rev_user_text
;
295 $this->mUser
= intval( $row->rev_user
);
296 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
297 $this->mTimestamp
= $row->rev_timestamp
;
298 $this->mDeleted
= intval( $row->rev_deleted
);
300 if( !isset( $row->rev_parent_id
) )
301 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
303 $this->mParentId
= intval( $row->rev_parent_id
);
305 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
308 $this->mSize
= intval( $row->rev_len
);
310 if( isset( $row->page_latest
) ) {
311 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
312 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
315 $this->mCurrent
= false;
316 $this->mTitle
= null;
319 // Lazy extraction...
321 if( isset( $row->old_text
) ) {
322 $this->mTextRow
= $row;
324 // 'text' table row entry will be lazy-loaded
325 $this->mTextRow
= null;
327 } elseif( is_array( $row ) ) {
328 // Build a new revision to be saved...
331 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
332 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
333 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
334 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
335 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
336 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
337 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
338 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
339 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
340 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
342 // Enforce spacing trimming on supplied text
343 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
344 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
345 $this->mTextRow
= null;
347 $this->mTitle
= null; # Load on demand if needed
348 $this->mCurrent
= false;
349 # If we still have no len_size, see it we have the text to figure it out
351 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
353 throw new MWException( 'Revision constructor passed invalid row format.' );
365 public function getId() {
373 public function getTextId() {
374 return $this->mTextId
;
378 * Get parent revision ID (the original previous page revision)
381 public function getParentId() {
382 return $this->mParentId
;
386 * Returns the length of the text in this revision, or null if unknown.
389 public function getSize() {
394 * Returns the title of the page associated with this entry.
397 public function getTitle() {
398 if( isset( $this->mTitle
) ) {
399 return $this->mTitle
;
401 $dbr = wfGetDB( DB_SLAVE
);
402 $row = $dbr->selectRow(
403 array( 'page', 'revision' ),
404 array( 'page_namespace', 'page_title' ),
405 array( 'page_id=rev_page',
406 'rev_id' => $this->mId
),
407 'Revision::getTitle' );
409 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
412 return $this->mTitle
;
416 * Set the title of the revision
417 * @param Title $title
419 public function setTitle( $title ) {
420 $this->mTitle
= $title;
427 public function getPage() {
432 * Fetch revision's user id if it's available to all users
435 public function getUser() {
436 if( $this->isDeleted( self
::DELETED_USER
) ) {
444 * Fetch revision's user id without regard for the current user's permissions
447 public function getRawUser() {
452 * Fetch revision's username if it's available to all users
455 public function getUserText() {
456 if( $this->isDeleted( self
::DELETED_USER
) ) {
459 return $this->mUserText
;
464 * Fetch revision's username without regard for view restrictions
467 public function getRawUserText() {
468 return $this->mUserText
;
472 * Fetch revision comment if it's available to all users
475 function getComment() {
476 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
479 return $this->mComment
;
484 * Fetch revision comment without regard for the current user's permissions
487 public function getRawComment() {
488 return $this->mComment
;
494 public function isMinor() {
495 return (bool)$this->mMinorEdit
;
499 * int $field one of DELETED_* bitfield constants
502 public function isDeleted( $field ) {
503 return ($this->mDeleted
& $field) == $field;
507 * Fetch revision text if it's available to all users
510 public function getText() {
511 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
514 return $this->getRawText();
519 * Fetch revision text without regard for view restrictions
522 public function getRawText() {
523 if( is_null( $this->mText
) ) {
524 // Revision text is immutable. Load on demand:
525 $this->mText
= $this->loadText();
531 * Fetch revision text if it's available to THIS user
534 public function revText() {
535 if( !$this->userCan( self
::DELETED_TEXT
) ) {
538 return $this->getRawText();
545 public function getTimestamp() {
546 return wfTimestamp(TS_MW
, $this->mTimestamp
);
552 public function isCurrent() {
553 return $this->mCurrent
;
557 * Get previous revision for this title
560 public function getPrevious() {
561 $prev = $this->getTitle()->getPreviousRevisionID( $this->mId
);
563 return Revision
::newFromTitle( $this->mTitle
, $prev );
572 public function getNext() {
573 $next = $this->getTitle()->getNextRevisionID( $this->mId
);
575 return Revision
::newFromTitle( $this->mTitle
, $next );
582 * Get previous revision Id for this page_id
583 * This is used to populate rev_parent_id on save
584 * @param Database $db
587 private function getPreviousRevisionId( $db ) {
588 if( is_null($this->mPage
) ) {
591 # Use page_latest if ID is not given
593 $prevId = $db->selectField( 'page', 'page_latest',
594 array( 'page_id' => $this->mPage
),
597 $prevId = $db->selectField( 'revision', 'rev_id',
598 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
600 array( 'ORDER BY' => 'rev_id DESC' ) );
602 return intval($prevId);
606 * Get revision text associated with an old or archive row
607 * $row is usually an object from wfFetchRow(), both the flags and the text
608 * field must be included
610 * @param integer $row Id of a row
611 * @param string $prefix table prefix (default 'old_')
612 * @return string $text|false the text requested
614 public static function getRevisionText( $row, $prefix = 'old_' ) {
615 wfProfileIn( __METHOD__
);
618 $textField = $prefix . 'text';
619 $flagsField = $prefix . 'flags';
621 if( isset( $row->$flagsField ) ) {
622 $flags = explode( ',', $row->$flagsField );
627 if( isset( $row->$textField ) ) {
628 $text = $row->$textField;
630 wfProfileOut( __METHOD__
);
634 # Use external methods for external objects, text in table is URL-only then
635 if ( in_array( 'external', $flags ) ) {
637 @list
(/* $proto */,$path)=explode('://',$url,2);
639 wfProfileOut( __METHOD__
);
642 $text=ExternalStore
::fetchFromURL($url);
645 // If the text was fetched without an error, convert it
646 if ( $text !== false ) {
647 if( in_array( 'gzip', $flags ) ) {
648 # Deal with optional compression of archived pages.
649 # This can be done periodically via maintenance/compressOld.php, and
650 # as pages are saved if $wgCompressRevisions is set.
651 $text = gzinflate( $text );
654 if( in_array( 'object', $flags ) ) {
655 # Generic compressed storage
656 $obj = unserialize( $text );
657 if ( !is_object( $obj ) ) {
659 wfProfileOut( __METHOD__
);
662 $text = $obj->getText();
665 global $wgLegacyEncoding;
666 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
667 # Old revisions kept around in a legacy encoding?
668 # Upconvert on demand.
669 global $wgInputEncoding, $wgContLang;
670 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
673 wfProfileOut( __METHOD__
);
678 * If $wgCompressRevisions is enabled, we will compress data.
679 * The input string is modified in place.
680 * Return value is the flags field: contains 'gzip' if the
681 * data is compressed, and 'utf-8' if we're saving in UTF-8
684 * @param mixed $text reference to a text
687 public static function compressRevisionText( &$text ) {
688 global $wgCompressRevisions;
691 # Revisions not marked this way will be converted
692 # on load if $wgLegacyCharset is set in the future.
695 if( $wgCompressRevisions ) {
696 if( function_exists( 'gzdeflate' ) ) {
697 $text = gzdeflate( $text );
700 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
703 return implode( ',', $flags );
707 * Insert a new revision into the database, returning the new revision ID
708 * number on success and dies horribly on failure.
710 * @param Database $dbw
713 public function insertOn( &$dbw ) {
714 global $wgDefaultExternalStore;
716 wfProfileIn( __METHOD__
);
718 $data = $this->mText
;
719 $flags = Revision
::compressRevisionText( $data );
721 # Write to external storage if required
722 if ( $wgDefaultExternalStore ) {
723 if ( is_array( $wgDefaultExternalStore ) ) {
724 // Distribute storage across multiple clusters
725 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
727 $store = $wgDefaultExternalStore;
729 // Store and get the URL
730 $data = ExternalStore
::insert( $store, $data );
732 # This should only happen in the case of a configuration error, where the external store is not valid
733 throw new MWException( "Unable to store text to external storage $store" );
738 $flags .= 'external';
741 # Record the text (or external storage URL) to the text table
742 if( !isset( $this->mTextId
) ) {
743 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
744 $dbw->insert( 'text',
748 'old_flags' => $flags,
751 $this->mTextId
= $dbw->insertId();
754 # Record the edit in revisions
755 $rev_id = isset( $this->mId
)
757 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
758 $dbw->insert( 'revision',
761 'rev_page' => $this->mPage
,
762 'rev_text_id' => $this->mTextId
,
763 'rev_comment' => $this->mComment
,
764 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
765 'rev_user' => $this->mUser
,
766 'rev_user_text' => $this->mUserText
,
767 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
768 'rev_deleted' => $this->mDeleted
,
769 'rev_len' => $this->mSize
,
770 'rev_parent_id' => $this->mParentId ?
$this->mParentId
: $this->getPreviousRevisionId( $dbw )
774 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
776 wfRunHooks( 'RevisionInsertComplete', array( &$this ) );
778 wfProfileOut( __METHOD__
);
783 * Lazy-load the revision's text.
784 * Currently hardcoded to the 'text' table storage engine.
788 private function loadText() {
789 wfProfileIn( __METHOD__
);
791 // Caching may be beneficial for massive use of external storage
792 global $wgRevisionCacheExpiry, $wgMemc;
793 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
794 if( $wgRevisionCacheExpiry ) {
795 $text = $wgMemc->get( $key );
796 if( is_string( $text ) ) {
797 wfProfileOut( __METHOD__
);
802 // If we kept data for lazy extraction, use it now...
803 if ( isset( $this->mTextRow
) ) {
804 $row = $this->mTextRow
;
805 $this->mTextRow
= null;
811 // Text data is immutable; check slaves first.
812 $dbr = wfGetDB( DB_SLAVE
);
813 $row = $dbr->selectRow( 'text',
814 array( 'old_text', 'old_flags' ),
815 array( 'old_id' => $this->getTextId() ),
820 // Possible slave lag!
821 $dbw = wfGetDB( DB_MASTER
);
822 $row = $dbw->selectRow( 'text',
823 array( 'old_text', 'old_flags' ),
824 array( 'old_id' => $this->getTextId() ),
828 $text = self
::getRevisionText( $row );
830 if( $wgRevisionCacheExpiry ) {
831 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
834 wfProfileOut( __METHOD__
);
840 * Create a new null-revision for insertion into a page's
841 * history. This will not re-save the text, but simply refer
842 * to the text from the previous version.
844 * Such revisions can for instance identify page rename
845 * operations and other such meta-modifications.
847 * @param Database $dbw
848 * @param int $pageId ID number of the page to read from
849 * @param string $summary
853 public static function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
854 wfProfileIn( __METHOD__
);
856 $current = $dbw->selectRow(
857 array( 'page', 'revision' ),
858 array( 'page_latest', 'rev_text_id' ),
860 'page_id' => $pageId,
861 'page_latest=rev_id',
866 $revision = new Revision( array(
868 'comment' => $summary,
869 'minor_edit' => $minor,
870 'text_id' => $current->rev_text_id
,
871 'parent_id' => $current->page_latest
877 wfProfileOut( __METHOD__
);
882 * Determine if the current user is allowed to view a particular
883 * field of this revision, if it's marked as deleted.
884 * @param int $field one of self::DELETED_TEXT,
885 * self::DELETED_COMMENT,
889 public function userCan( $field ) {
890 if( ( $this->mDeleted
& $field ) == $field ) {
892 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
895 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
896 return $wgUser->isAllowed( $permission );
904 * Get rev_timestamp from rev_id, without loading the rest of the row
907 static function getTimestampFromID( $id ) {
908 $dbr = wfGetDB( DB_SLAVE
);
909 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
910 array( 'rev_id' => $id ), __METHOD__
);
911 if ( $timestamp === false ) {
912 # Not in slave, try master
913 $dbw = wfGetDB( DB_MASTER
);
914 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
915 array( 'rev_id' => $id ), __METHOD__
);
921 * Get count of revisions per page...not very efficient
922 * @param Database $db
923 * @param int $id, page id
925 static function countByPageId( $db, $id ) {
926 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
927 array( 'rev_page' => $id ), __METHOD__
);
929 return $row->revCount
;
935 * Get count of revisions per page...not very efficient
936 * @param Database $db
937 * @param Title $title
939 static function countByTitle( $db, $title ) {
940 $id = $title->getArticleId();
942 return Revision
::countByPageId( $db, $id );
949 * Aliases for backwards compatibility with 1.6
951 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
952 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
953 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
954 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);