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 ) {
229 array( 'page', 'revision' ),
230 array( 'page_namespace',
244 'Revision::fetchRow',
245 array( 'LIMIT' => 1 ) );
246 $ret = $db->resultObject( $res );
251 * Return the list of revision fields that should be selected to create
254 static function selectFields() {
273 function Revision( $row ) {
274 if( is_object( $row ) ) {
275 $this->mId
= intval( $row->rev_id
);
276 $this->mPage
= intval( $row->rev_page
);
277 $this->mTextId
= intval( $row->rev_text_id
);
278 $this->mComment
= $row->rev_comment
;
279 $this->mUserText
= $row->rev_user_text
;
280 $this->mUser
= intval( $row->rev_user
);
281 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
282 $this->mTimestamp
= $row->rev_timestamp
;
283 $this->mDeleted
= intval( $row->rev_deleted
);
285 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
288 $this->mSize
= intval( $row->rev_len
);
290 if( isset( $row->page_latest
) ) {
291 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
292 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
295 $this->mCurrent
= false;
296 $this->mTitle
= null;
299 // Lazy extraction...
301 if( isset( $row->old_text
) ) {
302 $this->mTextRow
= $row;
304 // 'text' table row entry will be lazy-loaded
305 $this->mTextRow
= null;
307 } elseif( is_array( $row ) ) {
308 // Build a new revision to be saved...
311 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
312 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
313 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
314 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
315 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
316 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
317 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
318 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
319 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
321 // Enforce spacing trimming on supplied text
322 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
323 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
324 $this->mTextRow
= null;
326 $this->mTitle
= null; # Load on demand if needed
327 $this->mCurrent
= false;
328 # If we still have no len_size, see it we have the text to figure it out
330 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
332 throw new MWException( 'Revision constructor passed invalid row format.' );
350 function getTextId() {
351 return $this->mTextId
;
355 * Returns the length of the text in this revision, or null if unknown.
362 * Returns the title of the page associated with this entry.
365 function getTitle() {
366 if( isset( $this->mTitle
) ) {
367 return $this->mTitle
;
369 $dbr = wfGetDB( DB_SLAVE
);
370 $row = $dbr->selectRow(
371 array( 'page', 'revision' ),
372 array( 'page_namespace', 'page_title' ),
373 array( 'page_id=rev_page',
374 'rev_id' => $this->mId
),
375 'Revision::getTitle' );
377 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
380 return $this->mTitle
;
384 * Set the title of the revision
385 * @param Title $title
387 function setTitle( $title ) {
388 $this->mTitle
= $title;
399 * Fetch revision's user id if it's available to all users
403 if( $this->isDeleted( self
::DELETED_USER
) ) {
411 * Fetch revision's user id without regard for the current user's permissions
414 function getRawUser() {
419 * Fetch revision's username if it's available to all users
422 function getUserText() {
423 if( $this->isDeleted( self
::DELETED_USER
) ) {
426 return $this->mUserText
;
431 * Fetch revision's username without regard for view restrictions
434 function getRawUserText() {
435 return $this->mUserText
;
439 * Fetch revision comment if it's available to all users
442 function getComment() {
443 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
446 return $this->mComment
;
451 * Fetch revision comment without regard for the current user's permissions
454 function getRawComment() {
455 return $this->mComment
;
462 return (bool)$this->mMinorEdit
;
466 * int $field one of DELETED_* bitfield constants
469 function isDeleted( $field ) {
470 return ($this->mDeleted
& $field) == $field;
474 * Fetch revision text if it's available to all users
478 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
481 return $this->getRawText();
486 * Fetch revision text without regard for view restrictions
489 function getRawText() {
490 if( is_null( $this->mText
) ) {
491 // Revision text is immutable. Load on demand:
492 $this->mText
= $this->loadText();
498 * Fetch revision text if it's available to THIS user
502 if( !$this->userCan( self
::DELETED_TEXT
) ) {
505 return $this->getRawText();
512 function getTimestamp() {
513 return wfTimestamp(TS_MW
, $this->mTimestamp
);
519 function isCurrent() {
520 return $this->mCurrent
;
526 function getPrevious() {
527 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
529 return Revision
::newFromTitle( $this->mTitle
, $prev );
539 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
541 return Revision
::newFromTitle( $this->mTitle
, $next );
549 * Get revision text associated with an old or archive row
550 * $row is usually an object from wfFetchRow(), both the flags and the text
551 * field must be included
553 * @param integer $row Id of a row
554 * @param string $prefix table prefix (default 'old_')
555 * @return string $text|false the text requested
557 public static function getRevisionText( $row, $prefix = 'old_' ) {
558 $fname = 'Revision::getRevisionText';
559 wfProfileIn( $fname );
562 $textField = $prefix . 'text';
563 $flagsField = $prefix . 'flags';
565 if( isset( $row->$flagsField ) ) {
566 $flags = explode( ',', $row->$flagsField );
571 if( isset( $row->$textField ) ) {
572 $text = $row->$textField;
574 wfProfileOut( $fname );
578 # Use external methods for external objects, text in table is URL-only then
579 if ( in_array( 'external', $flags ) ) {
581 @list
(/* $proto */,$path)=explode('://',$url,2);
583 wfProfileOut( $fname );
586 $text=ExternalStore
::fetchFromURL($url);
589 // If the text was fetched without an error, convert it
590 if ( $text !== false ) {
591 if( in_array( 'gzip', $flags ) ) {
592 # Deal with optional compression of archived pages.
593 # This can be done periodically via maintenance/compressOld.php, and
594 # as pages are saved if $wgCompressRevisions is set.
595 $text = gzinflate( $text );
598 if( in_array( 'object', $flags ) ) {
599 # Generic compressed storage
600 $obj = unserialize( $text );
601 if ( !is_object( $obj ) ) {
603 wfProfileOut( $fname );
606 $text = $obj->getText();
609 global $wgLegacyEncoding;
610 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
611 # Old revisions kept around in a legacy encoding?
612 # Upconvert on demand.
613 global $wgInputEncoding, $wgContLang;
614 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
617 wfProfileOut( $fname );
622 * If $wgCompressRevisions is enabled, we will compress data.
623 * The input string is modified in place.
624 * Return value is the flags field: contains 'gzip' if the
625 * data is compressed, and 'utf-8' if we're saving in UTF-8
629 * @param mixed $text reference to a text
632 function compressRevisionText( &$text ) {
633 global $wgCompressRevisions;
636 # Revisions not marked this way will be converted
637 # on load if $wgLegacyCharset is set in the future.
640 if( $wgCompressRevisions ) {
641 if( function_exists( 'gzdeflate' ) ) {
642 $text = gzdeflate( $text );
645 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
648 return implode( ',', $flags );
652 * Insert a new revision into the database, returning the new revision ID
653 * number on success and dies horribly on failure.
655 * @param Database $dbw
658 function insertOn( &$dbw ) {
659 global $wgDefaultExternalStore;
661 $fname = 'Revision::insertOn';
662 wfProfileIn( $fname );
664 $data = $this->mText
;
665 $flags = Revision
::compressRevisionText( $data );
667 # Write to external storage if required
668 if ( $wgDefaultExternalStore ) {
669 if ( is_array( $wgDefaultExternalStore ) ) {
670 // Distribute storage across multiple clusters
671 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
673 $store = $wgDefaultExternalStore;
675 // Store and get the URL
676 $data = ExternalStore
::insert( $store, $data );
678 # This should only happen in the case of a configuration error, where the external store is not valid
679 throw new MWException( "Unable to store text to external storage $store" );
684 $flags .= 'external';
687 # Record the text (or external storage URL) to the text table
688 if( !isset( $this->mTextId
) ) {
689 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
690 $dbw->insert( 'text',
694 'old_flags' => $flags,
697 $this->mTextId
= $dbw->insertId();
700 # Record the edit in revisions
701 $rev_id = isset( $this->mId
)
703 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
704 $dbw->insert( 'revision',
707 'rev_page' => $this->mPage
,
708 'rev_text_id' => $this->mTextId
,
709 'rev_comment' => $this->mComment
,
710 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
711 'rev_user' => $this->mUser
,
712 'rev_user_text' => $this->mUserText
,
713 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
714 'rev_deleted' => $this->mDeleted
,
715 'rev_len' => $this->mSize
,
719 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
720 wfProfileOut( $fname );
725 * Lazy-load the revision's text.
726 * Currently hardcoded to the 'text' table storage engine.
731 function loadText() {
732 $fname = 'Revision::loadText';
733 wfProfileIn( $fname );
735 // Caching may be beneficial for massive use of external storage
736 global $wgRevisionCacheExpiry, $wgMemc;
737 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
738 if( $wgRevisionCacheExpiry ) {
739 $text = $wgMemc->get( $key );
740 if( is_string( $text ) ) {
741 wfProfileOut( $fname );
746 // If we kept data for lazy extraction, use it now...
747 if ( isset( $this->mTextRow
) ) {
748 $row = $this->mTextRow
;
749 $this->mTextRow
= null;
755 // Text data is immutable; check slaves first.
756 $dbr = wfGetDB( DB_SLAVE
);
757 $row = $dbr->selectRow( 'text',
758 array( 'old_text', 'old_flags' ),
759 array( 'old_id' => $this->getTextId() ),
764 // Possible slave lag!
765 $dbw = wfGetDB( DB_MASTER
);
766 $row = $dbw->selectRow( 'text',
767 array( 'old_text', 'old_flags' ),
768 array( 'old_id' => $this->getTextId() ),
772 $text = Revision
::getRevisionText( $row );
774 if( $wgRevisionCacheExpiry ) {
775 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
778 wfProfileOut( $fname );
784 * Create a new null-revision for insertion into a page's
785 * history. This will not re-save the text, but simply refer
786 * to the text from the previous version.
788 * Such revisions can for instance identify page rename
789 * operations and other such meta-modifications.
791 * @param Database $dbw
792 * @param int $pageId ID number of the page to read from
793 * @param string $summary
797 public static function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
798 wfProfileIn( __METHOD__
);
800 $current = $dbw->selectRow(
801 array( 'page', 'revision' ),
802 array( 'page_latest', 'rev_text_id' ),
804 'page_id' => $pageId,
805 'page_latest=rev_id',
810 $revision = new Revision( array(
812 'comment' => $summary,
813 'minor_edit' => $minor,
814 'text_id' => $current->rev_text_id
,
820 wfProfileOut( __METHOD__
);
825 * Determine if the current user is allowed to view a particular
826 * field of this revision, if it's marked as deleted.
827 * @param int $field one of self::DELETED_TEXT,
828 * self::DELETED_COMMENT,
832 function userCan( $field ) {
833 if( ( $this->mDeleted
& $field ) == $field ) {
835 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
838 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
839 return $wgUser->isAllowed( $permission );
847 * Get rev_timestamp from rev_id, without loading the rest of the row
850 static function getTimestampFromID( $id ) {
851 $dbr = wfGetDB( DB_SLAVE
);
852 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
853 array( 'rev_id' => $id ), __METHOD__
);
854 if ( $timestamp === false ) {
855 # Not in slave, try master
856 $dbw = wfGetDB( DB_MASTER
);
857 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
858 array( 'rev_id' => $id ), __METHOD__
);
863 static function countByPageId( $db, $id ) {
864 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
865 array( 'rev_page' => $id ), __METHOD__
);
867 return $row->revCount
;
872 static function countByTitle( $db, $title ) {
873 $id = $title->getArticleId();
875 return Revision
::countByPageId( $db, $id );
882 * Aliases for backwards compatibility with 1.6
884 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
885 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
886 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
887 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);