10 const DELETED_TEXT
= 1;
11 const DELETED_COMMENT
= 2;
12 const DELETED_USER
= 4;
13 const DELETED_RESTRICTED
= 8;
14 const DELETED_NAME
= 16;
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 ) {
230 array( 'page', 'revision' ),
231 array( 'page_namespace',
245 'Revision::fetchRow',
246 array( 'LIMIT' => 1 ) );
247 $ret = $db->resultObject( $res );
255 function Revision( $row ) {
256 if( is_object( $row ) ) {
257 $this->mId
= intval( $row->rev_id
);
258 $this->mPage
= intval( $row->rev_page
);
259 $this->mTextId
= intval( $row->rev_text_id
);
260 $this->mComment
= $row->rev_comment
;
261 $this->mUserText
= $row->rev_user_text
;
262 $this->mUser
= intval( $row->rev_user
);
263 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
264 $this->mTimestamp
= $row->rev_timestamp
;
265 $this->mDeleted
= intval( $row->rev_deleted
);
267 if( !isset( $row->rev_len
) )
270 $this->mSize
= intval( $row->rev_len
);
272 if( isset( $row->page_latest
) ) {
273 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
274 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
277 $this->mCurrent
= false;
278 $this->mTitle
= null;
281 // Lazy extraction...
283 if( isset( $row->old_text
) ) {
284 $this->mTextRow
= $row;
286 // 'text' table row entry will be lazy-loaded
287 $this->mTextRow
= null;
289 } elseif( is_array( $row ) ) {
290 // Build a new revision to be saved...
293 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
294 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
295 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
296 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
297 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
298 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
299 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
300 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
302 // Enforce spacing trimming on supplied text
303 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
304 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
305 $this->mTextRow
= null;
307 $this->mTitle
= null; # Load on demand if needed
308 $this->mCurrent
= false;
310 $this->mSize
= is_null($this->mText
) ?
null : mb_strlen($this->mText
);
312 throw new MWException( 'Revision constructor passed invalid row format.' );
330 function getTextId() {
331 return $this->mTextId
;
335 * Returns the length of the text in this revision, or null if unknown.
342 * Returns the title of the page associated with this entry.
345 function getTitle() {
346 if( isset( $this->mTitle
) ) {
347 return $this->mTitle
;
349 $dbr = wfGetDB( DB_SLAVE
);
350 $row = $dbr->selectRow(
351 array( 'page', 'revision' ),
352 array( 'page_namespace', 'page_title' ),
353 array( 'page_id=rev_page',
354 'rev_id' => $this->mId
),
355 'Revision::getTitle' );
357 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
360 return $this->mTitle
;
364 * Set the title of the revision
365 * @param Title $title
367 function setTitle( $title ) {
368 $this->mTitle
= $title;
379 * Fetch revision's user id if it's available to all users
383 if( $this->isDeleted( self
::DELETED_USER
) ) {
391 * Fetch revision's user id without regard for the current user's permissions
394 function getRawUser() {
399 * Fetch revision's username if it's available to all users
402 function getUserText() {
403 if( $this->isDeleted( self
::DELETED_USER
) ) {
406 return $this->mUserText
;
411 * Fetch revision's username without regard for view restrictions
414 function getRawUserText() {
415 return $this->mUserText
;
419 * Fetch revision comment if it's available to all users
422 function getComment() {
423 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
426 return $this->mComment
;
431 * Fetch revision comment without regard for the current user's permissions
434 function getRawComment() {
435 return $this->mComment
;
442 return (bool)$this->mMinorEdit
;
446 * int $field one of DELETED_* bitfield constants
449 function isDeleted( $field ) {
450 return ($this->mDeleted
& $field) == $field;
454 * Fetch revision text if it's available to all users
458 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
461 return $this->getRawText();
466 * Fetch revision text without regard for view restrictions
469 function getRawText() {
470 if( is_null( $this->mText
) ) {
471 // Revision text is immutable. Load on demand:
472 $this->mText
= $this->loadText();
478 * Fetch revision text if it's available to THIS user
482 if( !$this->userCan( self
::DELETED_TEXT
) ) {
485 return $this->getRawText();
492 function getTimestamp() {
493 return wfTimestamp(TS_MW
, $this->mTimestamp
);
499 function isCurrent() {
500 return $this->mCurrent
;
506 function getPrevious() {
507 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
509 return Revision
::newFromTitle( $this->mTitle
, $prev );
519 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
521 return Revision
::newFromTitle( $this->mTitle
, $next );
529 * Get revision text associated with an old or archive row
530 * $row is usually an object from wfFetchRow(), both the flags and the text
531 * field must be included
533 * @param integer $row Id of a row
534 * @param string $prefix table prefix (default 'old_')
535 * @return string $text|false the text requested
537 public static function getRevisionText( $row, $prefix = 'old_' ) {
538 $fname = 'Revision::getRevisionText';
539 wfProfileIn( $fname );
542 $textField = $prefix . 'text';
543 $flagsField = $prefix . 'flags';
545 if( isset( $row->$flagsField ) ) {
546 $flags = explode( ',', $row->$flagsField );
551 if( isset( $row->$textField ) ) {
552 $text = $row->$textField;
554 wfProfileOut( $fname );
558 # Use external methods for external objects, text in table is URL-only then
559 if ( in_array( 'external', $flags ) ) {
561 @list
(/* $proto */,$path)=explode('://',$url,2);
563 wfProfileOut( $fname );
566 $text=ExternalStore
::fetchFromURL($url);
569 // If the text was fetched without an error, convert it
570 if ( $text !== false ) {
571 if( in_array( 'gzip', $flags ) ) {
572 # Deal with optional compression of archived pages.
573 # This can be done periodically via maintenance/compressOld.php, and
574 # as pages are saved if $wgCompressRevisions is set.
575 $text = gzinflate( $text );
578 if( in_array( 'object', $flags ) ) {
579 # Generic compressed storage
580 $obj = unserialize( $text );
581 if ( !is_object( $obj ) ) {
583 wfProfileOut( $fname );
586 $text = $obj->getText();
589 global $wgLegacyEncoding;
590 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
591 # Old revisions kept around in a legacy encoding?
592 # Upconvert on demand.
593 global $wgInputEncoding, $wgContLang;
594 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
597 wfProfileOut( $fname );
602 * If $wgCompressRevisions is enabled, we will compress data.
603 * The input string is modified in place.
604 * Return value is the flags field: contains 'gzip' if the
605 * data is compressed, and 'utf-8' if we're saving in UTF-8
609 * @param mixed $text reference to a text
612 function compressRevisionText( &$text ) {
613 global $wgCompressRevisions;
616 # Revisions not marked this way will be converted
617 # on load if $wgLegacyCharset is set in the future.
620 if( $wgCompressRevisions ) {
621 if( function_exists( 'gzdeflate' ) ) {
622 $text = gzdeflate( $text );
625 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
628 return implode( ',', $flags );
632 * Insert a new revision into the database, returning the new revision ID
633 * number on success and dies horribly on failure.
635 * @param Database $dbw
638 function insertOn( &$dbw ) {
639 global $wgDefaultExternalStore;
641 $fname = 'Revision::insertOn';
642 wfProfileIn( $fname );
644 $data = $this->mText
;
645 $flags = Revision
::compressRevisionText( $data );
647 # Write to external storage if required
648 if ( $wgDefaultExternalStore ) {
649 if ( is_array( $wgDefaultExternalStore ) ) {
650 // Distribute storage across multiple clusters
651 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
653 $store = $wgDefaultExternalStore;
655 // Store and get the URL
656 $data = ExternalStore
::insert( $store, $data );
658 # This should only happen in the case of a configuration error, where the external store is not valid
659 throw new MWException( "Unable to store text to external storage $store" );
664 $flags .= 'external';
667 # Record the text (or external storage URL) to the text table
668 if( !isset( $this->mTextId
) ) {
669 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
670 $dbw->insert( 'text',
674 'old_flags' => $flags,
677 $this->mTextId
= $dbw->insertId();
680 # Record the edit in revisions
681 $rev_id = isset( $this->mId
)
683 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
684 $dbw->insert( 'revision',
687 'rev_page' => $this->mPage
,
688 'rev_text_id' => $this->mTextId
,
689 'rev_comment' => $this->mComment
,
690 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
691 'rev_user' => $this->mUser
,
692 'rev_user_text' => $this->mUserText
,
693 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
694 'rev_deleted' => $this->mDeleted
,
695 'rev_len' => mb_strlen($this->mText
),
699 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
700 wfProfileOut( $fname );
705 * Lazy-load the revision's text.
706 * Currently hardcoded to the 'text' table storage engine.
711 function loadText() {
712 $fname = 'Revision::loadText';
713 wfProfileIn( $fname );
715 // Caching may be beneficial for massive use of external storage
716 global $wgRevisionCacheExpiry, $wgMemc;
717 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
718 if( $wgRevisionCacheExpiry ) {
719 $text = $wgMemc->get( $key );
720 if( is_string( $text ) ) {
721 wfProfileOut( $fname );
726 // If we kept data for lazy extraction, use it now...
727 if ( isset( $this->mTextRow
) ) {
728 $row = $this->mTextRow
;
729 $this->mTextRow
= null;
735 // Text data is immutable; check slaves first.
736 $dbr = wfGetDB( DB_SLAVE
);
737 $row = $dbr->selectRow( 'text',
738 array( 'old_text', 'old_flags' ),
739 array( 'old_id' => $this->getTextId() ),
744 // Possible slave lag!
745 $dbw = wfGetDB( DB_MASTER
);
746 $row = $dbw->selectRow( 'text',
747 array( 'old_text', 'old_flags' ),
748 array( 'old_id' => $this->getTextId() ),
752 $text = Revision
::getRevisionText( $row );
754 if( $wgRevisionCacheExpiry ) {
755 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
758 wfProfileOut( $fname );
764 * Create a new null-revision for insertion into a page's
765 * history. This will not re-save the text, but simply refer
766 * to the text from the previous version.
768 * Such revisions can for instance identify page rename
769 * operations and other such meta-modifications.
771 * @param Database $dbw
772 * @param int $pageId ID number of the page to read from
773 * @param string $summary
777 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
778 $fname = 'Revision::newNullRevision';
779 wfProfileIn( $fname );
781 $current = $dbw->selectRow(
782 array( 'page', 'revision' ),
783 array( 'page_latest', 'rev_text_id' ),
785 'page_id' => $pageId,
786 'page_latest=rev_id',
791 $revision = new Revision( array(
793 'comment' => $summary,
794 'minor_edit' => $minor,
795 'text_id' => $current->rev_text_id
,
801 wfProfileOut( $fname );
806 * Determine if the current user is allowed to view a particular
807 * field of this revision, if it's marked as deleted.
808 * @param int $field one of self::DELETED_TEXT,
809 * self::DELETED_COMMENT,
813 function userCan( $field ) {
814 if( ( $this->mDeleted
& $field ) == $field ) {
816 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
819 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
820 return $wgUser->isAllowed( $permission );
828 * Get rev_timestamp from rev_id, without loading the rest of the row
831 static function getTimestampFromID( $id ) {
832 $dbr = wfGetDB( DB_SLAVE
);
833 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
834 array( 'rev_id' => $id ), __METHOD__
);
835 if ( $timestamp === false ) {
836 # Not in slave, try master
837 $dbw = wfGetDB( DB_MASTER
);
838 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
839 array( 'rev_id' => $id ), __METHOD__
);
844 static function countByPageId( $db, $id ) {
845 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
846 array( 'rev_page' => $id ), __METHOD__
);
848 return $row->revCount
;
853 static function countByTitle( $db, $title ) {
854 $id = $title->getArticleId();
856 return Revision
::countByPageId( $db, $id );
863 * Aliases for backwards compatibility with 1.6
865 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
866 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
867 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
868 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);