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.
39 public static function newFromTitle( $title, $id = 0 ) {
41 $matchId = intval( $id );
43 $matchId = 'page_latest';
45 return Revision
::newFromConds(
46 array( "rev_id=$matchId",
48 'page_namespace' => $title->getNamespace(),
49 'page_title' => $title->getDBkey() ) );
53 * Load a page revision from a given revision ID number.
54 * Returns null if no such revision can be found.
61 public static function loadFromId( $db, $id ) {
62 return Revision
::loadFromConds( $db,
63 array( 'page_id=rev_page',
64 'rev_id' => intval( $id ) ) );
68 * Load either the current, or a specified, revision
69 * that's attached to a given page. If not attached
70 * to that page, will return null.
79 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
80 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
82 $conds['rev_id']=intval($id);
84 $conds[]='rev_id=page_latest';
86 return Revision
::loadFromConds( $db, $conds );
90 * Load either the current, or a specified, revision
91 * that's attached to a given page. If not attached
92 * to that page, will return null.
101 public static function loadFromTitle( $db, $title, $id = 0 ) {
103 $matchId = intval( $id );
105 $matchId = 'page_latest';
107 return Revision
::loadFromConds(
109 array( "rev_id=$matchId",
111 'page_namespace' => $title->getNamespace(),
112 'page_title' => $title->getDBkey() ) );
116 * Load the revision for the given title with the given timestamp.
117 * WARNING: Timestamps may in some circumstances not be unique,
118 * so this isn't the best key to use.
120 * @param Database $db
121 * @param Title $title
122 * @param string $timestamp
127 public static function loadFromTimestamp( $db, $title, $timestamp ) {
128 return Revision
::loadFromConds(
130 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
132 'page_namespace' => $title->getNamespace(),
133 'page_title' => $title->getDBkey() ) );
137 * Given a set of conditions, fetch a revision.
139 * @param array $conditions
144 private static function newFromConds( $conditions ) {
145 $db = wfGetDB( DB_SLAVE
);
146 $row = Revision
::loadFromConds( $db, $conditions );
147 if( is_null( $row ) ) {
148 $dbw = wfGetDB( DB_MASTER
);
149 $row = Revision
::loadFromConds( $dbw, $conditions );
155 * Given a set of conditions, fetch a revision from
156 * the given database connection.
158 * @param Database $db
159 * @param array $conditions
164 private static function loadFromConds( $db, $conditions ) {
165 $res = Revision
::fetchFromConds( $db, $conditions );
167 $row = $res->fetchObject();
170 $ret = new Revision( $row );
179 * Return a wrapper for a series of database rows to
180 * fetch all of a given page's revisions in turn.
181 * Each row can be fed to the constructor to get objects.
183 * @param Title $title
184 * @return ResultWrapper
188 public static function fetchAllRevisions( $title ) {
189 return Revision
::fetchFromConds(
191 array( 'page_namespace' => $title->getNamespace(),
192 'page_title' => $title->getDBkey(),
193 'page_id=rev_page' ) );
197 * Return a wrapper for a series of database rows to
198 * fetch all of a given page's revisions in turn.
199 * Each row can be fed to the constructor to get objects.
201 * @param Title $title
202 * @return ResultWrapper
206 public static function fetchRevision( $title ) {
207 return Revision
::fetchFromConds(
209 array( 'rev_id=page_latest',
210 'page_namespace' => $title->getNamespace(),
211 'page_title' => $title->getDBkey(),
212 'page_id=rev_page' ) );
216 * Given a set of conditions, return a ResultWrapper
217 * which will return matching database rows with the
218 * fields necessary to build Revision objects.
220 * @param Database $db
221 * @param array $conditions
222 * @return ResultWrapper
226 private static function fetchFromConds( $db, $conditions ) {
227 $fields = self
::selectFields();
228 $fields[] = 'page_namespace';
229 $fields[] = 'page_title';
230 $fields[] = 'page_latest';
232 array( 'page', 'revision' ),
235 'Revision::fetchRow',
236 array( 'LIMIT' => 1 ) );
237 $ret = $db->resultObject( $res );
242 * Return the list of revision fields that should be selected to create
245 static function selectFields() {
262 * Return the list of text fields that should be selected to read the
265 static function selectTextFields() {
272 * Return the list of page fields that should be selected from page table
274 static function selectPageFields() {
286 function Revision( $row ) {
287 if( is_object( $row ) ) {
288 $this->mId
= intval( $row->rev_id
);
289 $this->mPage
= intval( $row->rev_page
);
290 $this->mTextId
= intval( $row->rev_text_id
);
291 $this->mComment
= $row->rev_comment
;
292 $this->mUserText
= $row->rev_user_text
;
293 $this->mUser
= intval( $row->rev_user
);
294 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
295 $this->mTimestamp
= $row->rev_timestamp
;
296 $this->mDeleted
= intval( $row->rev_deleted
);
298 if( !isset( $row->rev_parent_id
) )
299 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
301 $this->mParentId
= intval( $row->rev_parent_id
);
303 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
306 $this->mSize
= intval( $row->rev_len
);
308 if( isset( $row->page_latest
) ) {
309 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
310 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
313 $this->mCurrent
= false;
314 $this->mTitle
= null;
317 // Lazy extraction...
319 if( isset( $row->old_text
) ) {
320 $this->mTextRow
= $row;
322 // 'text' table row entry will be lazy-loaded
323 $this->mTextRow
= null;
325 } elseif( is_array( $row ) ) {
326 // Build a new revision to be saved...
329 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
330 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
331 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
332 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
333 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
334 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
335 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
336 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
337 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
338 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
340 // Enforce spacing trimming on supplied text
341 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
342 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
343 $this->mTextRow
= null;
345 $this->mTitle
= null; # Load on demand if needed
346 $this->mCurrent
= false;
347 # If we still have no len_size, see it we have the text to figure it out
349 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
351 throw new MWException( 'Revision constructor passed invalid row format.' );
363 public function getId() {
371 public function getTextId() {
372 return $this->mTextId
;
376 * Get parent revision ID (the original previous page revision)
379 public function getParentId() {
380 return $this->mParentId
;
384 * Returns the length of the text in this revision, or null if unknown.
387 public function getSize() {
392 * Returns the title of the page associated with this entry.
395 public function getTitle() {
396 if( isset( $this->mTitle
) ) {
397 return $this->mTitle
;
399 $dbr = wfGetDB( DB_SLAVE
);
400 $row = $dbr->selectRow(
401 array( 'page', 'revision' ),
402 array( 'page_namespace', 'page_title' ),
403 array( 'page_id=rev_page',
404 'rev_id' => $this->mId
),
405 'Revision::getTitle' );
407 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
410 return $this->mTitle
;
414 * Set the title of the revision
415 * @param Title $title
417 public function setTitle( $title ) {
418 $this->mTitle
= $title;
425 public function getPage() {
430 * Fetch revision's user id if it's available to all users
433 public function getUser() {
434 if( $this->isDeleted( self
::DELETED_USER
) ) {
442 * Fetch revision's user id without regard for the current user's permissions
445 public function getRawUser() {
450 * Fetch revision's username if it's available to all users
453 public function getUserText() {
454 if( $this->isDeleted( self
::DELETED_USER
) ) {
457 return $this->mUserText
;
462 * Fetch revision's username without regard for view restrictions
465 public function getRawUserText() {
466 return $this->mUserText
;
470 * Fetch revision comment if it's available to all users
473 function getComment() {
474 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
477 return $this->mComment
;
482 * Fetch revision comment without regard for the current user's permissions
485 public function getRawComment() {
486 return $this->mComment
;
492 public function isMinor() {
493 return (bool)$this->mMinorEdit
;
497 * int $field one of DELETED_* bitfield constants
500 public function isDeleted( $field ) {
501 return ($this->mDeleted
& $field) == $field;
505 * Fetch revision text if it's available to all users
508 public function getText( $isPublic = true ) {
509 if( $isPublic && $this->isDeleted( self
::DELETED_TEXT
) ) {
511 } else if( !$this->userCan( 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();
533 public function getTimestamp() {
534 return wfTimestamp(TS_MW
, $this->mTimestamp
);
540 public function isCurrent() {
541 return $this->mCurrent
;
545 * Get previous revision for this title
548 public function getPrevious() {
549 if( $this->getTitle() ) {
550 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
552 return Revision
::newFromTitle( $this->getTitle(), $prev );
561 public function getNext() {
562 if( $this->getTitle() ) {
563 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
565 return Revision
::newFromTitle( $this->getTitle(), $next );
572 * Get previous revision Id for this page_id
573 * This is used to populate rev_parent_id on save
574 * @param Database $db
577 private function getPreviousRevisionId( $db ) {
578 if( is_null($this->mPage
) ) {
581 # Use page_latest if ID is not given
583 $prevId = $db->selectField( 'page', 'page_latest',
584 array( 'page_id' => $this->mPage
),
587 $prevId = $db->selectField( 'revision', 'rev_id',
588 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
590 array( 'ORDER BY' => 'rev_id DESC' ) );
592 return intval($prevId);
596 * Get revision text associated with an old or archive row
597 * $row is usually an object from wfFetchRow(), both the flags and the text
598 * field must be included
600 * @param integer $row Id of a row
601 * @param string $prefix table prefix (default 'old_')
602 * @return string $text|false the text requested
604 public static function getRevisionText( $row, $prefix = 'old_' ) {
605 wfProfileIn( __METHOD__
);
608 $textField = $prefix . 'text';
609 $flagsField = $prefix . 'flags';
611 if( isset( $row->$flagsField ) ) {
612 $flags = explode( ',', $row->$flagsField );
617 if( isset( $row->$textField ) ) {
618 $text = $row->$textField;
620 wfProfileOut( __METHOD__
);
624 # Use external methods for external objects, text in table is URL-only then
625 if ( in_array( 'external', $flags ) ) {
627 @list
(/* $proto */,$path)=explode('://',$url,2);
629 wfProfileOut( __METHOD__
);
632 $text=ExternalStore
::fetchFromURL($url);
635 // If the text was fetched without an error, convert it
636 if ( $text !== false ) {
637 if( in_array( 'gzip', $flags ) ) {
638 # Deal with optional compression of archived pages.
639 # This can be done periodically via maintenance/compressOld.php, and
640 # as pages are saved if $wgCompressRevisions is set.
641 $text = gzinflate( $text );
644 if( in_array( 'object', $flags ) ) {
645 # Generic compressed storage
646 $obj = unserialize( $text );
647 if ( !is_object( $obj ) ) {
649 wfProfileOut( __METHOD__
);
652 $text = $obj->getText();
655 global $wgLegacyEncoding;
656 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
657 # Old revisions kept around in a legacy encoding?
658 # Upconvert on demand.
659 global $wgInputEncoding, $wgContLang;
660 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
663 wfProfileOut( __METHOD__
);
668 * If $wgCompressRevisions is enabled, we will compress data.
669 * The input string is modified in place.
670 * Return value is the flags field: contains 'gzip' if the
671 * data is compressed, and 'utf-8' if we're saving in UTF-8
674 * @param mixed $text reference to a text
677 public static function compressRevisionText( &$text ) {
678 global $wgCompressRevisions;
681 # Revisions not marked this way will be converted
682 # on load if $wgLegacyCharset is set in the future.
685 if( $wgCompressRevisions ) {
686 if( function_exists( 'gzdeflate' ) ) {
687 $text = gzdeflate( $text );
690 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
693 return implode( ',', $flags );
697 * Insert a new revision into the database, returning the new revision ID
698 * number on success and dies horribly on failure.
700 * @param Database $dbw
703 public function insertOn( $dbw ) {
704 global $wgDefaultExternalStore;
706 wfProfileIn( __METHOD__
);
708 $data = $this->mText
;
709 $flags = Revision
::compressRevisionText( $data );
711 # Write to external storage if required
712 if( $wgDefaultExternalStore ) {
713 // Store and get the URL
714 $data = ExternalStore
::randomInsert( $data );
716 throw new MWException( "Unable to store text to external storage" );
721 $flags .= 'external';
724 # Record the text (or external storage URL) to the text table
725 if( !isset( $this->mTextId
) ) {
726 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
727 $dbw->insert( 'text',
731 'old_flags' => $flags,
734 $this->mTextId
= $dbw->insertId();
737 # Record the edit in revisions
738 $rev_id = isset( $this->mId
)
740 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
741 $dbw->insert( 'revision',
744 'rev_page' => $this->mPage
,
745 'rev_text_id' => $this->mTextId
,
746 'rev_comment' => $this->mComment
,
747 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
748 'rev_user' => $this->mUser
,
749 'rev_user_text' => $this->mUserText
,
750 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
751 'rev_deleted' => $this->mDeleted
,
752 'rev_len' => $this->mSize
,
753 'rev_parent_id' => $this->mParentId ?
$this->mParentId
: $this->getPreviousRevisionId( $dbw )
757 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
759 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
761 wfProfileOut( __METHOD__
);
766 * Lazy-load the revision's text.
767 * Currently hardcoded to the 'text' table storage engine.
771 private function loadText() {
772 wfProfileIn( __METHOD__
);
774 // Caching may be beneficial for massive use of external storage
775 global $wgRevisionCacheExpiry, $wgMemc;
776 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
777 if( $wgRevisionCacheExpiry ) {
778 $text = $wgMemc->get( $key );
779 if( is_string( $text ) ) {
780 wfProfileOut( __METHOD__
);
785 // If we kept data for lazy extraction, use it now...
786 if ( isset( $this->mTextRow
) ) {
787 $row = $this->mTextRow
;
788 $this->mTextRow
= null;
794 // Text data is immutable; check slaves first.
795 $dbr = wfGetDB( DB_SLAVE
);
796 $row = $dbr->selectRow( 'text',
797 array( 'old_text', 'old_flags' ),
798 array( 'old_id' => $this->getTextId() ),
803 // Possible slave lag!
804 $dbw = wfGetDB( DB_MASTER
);
805 $row = $dbw->selectRow( 'text',
806 array( 'old_text', 'old_flags' ),
807 array( 'old_id' => $this->getTextId() ),
811 $text = self
::getRevisionText( $row );
813 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
814 if( $wgRevisionCacheExpiry && $text !== false ) {
815 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
818 wfProfileOut( __METHOD__
);
824 * Create a new null-revision for insertion into a page's
825 * history. This will not re-save the text, but simply refer
826 * to the text from the previous version.
828 * Such revisions can for instance identify page rename
829 * operations and other such meta-modifications.
831 * @param Database $dbw
832 * @param int $pageId ID number of the page to read from
833 * @param string $summary
837 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
838 wfProfileIn( __METHOD__
);
840 $current = $dbw->selectRow(
841 array( 'page', 'revision' ),
842 array( 'page_latest', 'rev_text_id' ),
844 'page_id' => $pageId,
845 'page_latest=rev_id',
850 $revision = new Revision( array(
852 'comment' => $summary,
853 'minor_edit' => $minor,
854 'text_id' => $current->rev_text_id
,
855 'parent_id' => $current->page_latest
861 wfProfileOut( __METHOD__
);
866 * Determine if the current user is allowed to view a particular
867 * field of this revision, if it's marked as deleted.
868 * @param int $field one of self::DELETED_TEXT,
869 * self::DELETED_COMMENT,
873 public function userCan( $field ) {
874 if( ( $this->mDeleted
& $field ) == $field ) {
876 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
879 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
880 return $wgUser->isAllowed( $permission );
888 * Get rev_timestamp from rev_id, without loading the rest of the row
890 * @param integer $pageid, optional
892 static function getTimestampFromId( $id, $pageId = 0 ) {
893 $dbr = wfGetDB( DB_SLAVE
);
894 $conds = array( 'rev_id' => $id );
896 $conds['rev_page'] = $pageId;
898 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
899 if ( $timestamp === false ) {
900 # Not in slave, try master
901 $dbw = wfGetDB( DB_MASTER
);
902 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
904 return wfTimestamp( TS_MW
, $timestamp );
908 * Get count of revisions per page...not very efficient
909 * @param Database $db
910 * @param int $id, page id
912 static function countByPageId( $db, $id ) {
913 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
914 array( 'rev_page' => $id ), __METHOD__
);
916 return $row->revCount
;
922 * Get count of revisions per page...not very efficient
923 * @param Database $db
924 * @param Title $title
926 static function countByTitle( $db, $title ) {
927 $id = $title->getArticleId();
929 return Revision
::countByPageId( $db, $id );
936 * Aliases for backwards compatibility with 1.6
938 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
939 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
940 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
941 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);