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',
244 'Revision::fetchRow',
245 array( 'LIMIT' => 1 ) );
246 $ret = $db->resultObject( $res );
254 function Revision( $row ) {
255 if( is_object( $row ) ) {
256 $this->mId
= intval( $row->rev_id
);
257 $this->mPage
= intval( $row->rev_page
);
258 $this->mTextId
= intval( $row->rev_text_id
);
259 $this->mComment
= $row->rev_comment
;
260 $this->mUserText
= $row->rev_user_text
;
261 $this->mUser
= intval( $row->rev_user
);
262 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
263 $this->mTimestamp
= $row->rev_timestamp
;
264 $this->mDeleted
= intval( $row->rev_deleted
);
266 if( isset( $row->page_latest
) ) {
267 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
268 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
271 $this->mCurrent
= false;
272 $this->mTitle
= null;
275 // Lazy extraction...
277 if( isset( $row->old_text
) ) {
278 $this->mTextRow
= $row;
280 // 'text' table row entry will be lazy-loaded
281 $this->mTextRow
= null;
283 } elseif( is_array( $row ) ) {
284 // Build a new revision to be saved...
287 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
288 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
289 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
290 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
291 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
292 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
293 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
294 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
296 // Enforce spacing trimming on supplied text
297 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
298 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
299 $this->mTextRow
= null;
301 $this->mTitle
= null; # Load on demand if needed
302 $this->mCurrent
= false;
304 throw new MWException( 'Revision constructor passed invalid row format.' );
322 function getTextId() {
323 return $this->mTextId
;
327 * Returns the title of the page associated with this entry.
330 function getTitle() {
331 if( isset( $this->mTitle
) ) {
332 return $this->mTitle
;
334 $dbr = wfGetDB( DB_SLAVE
);
335 $row = $dbr->selectRow(
336 array( 'page', 'revision' ),
337 array( 'page_namespace', 'page_title' ),
338 array( 'page_id=rev_page',
339 'rev_id' => $this->mId
),
340 'Revision::getTitle' );
342 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
345 return $this->mTitle
;
349 * Set the title of the revision
350 * @param Title $title
352 function setTitle( $title ) {
353 $this->mTitle
= $title;
364 * Fetch revision's user id if it's available to all users
368 if( $this->isDeleted( self
::DELETED_USER
) ) {
376 * Fetch revision's user id without regard for the current user's permissions
379 function getRawUser() {
384 * Fetch revision's username if it's available to all users
387 function getUserText() {
388 if( $this->isDeleted( self
::DELETED_USER
) ) {
391 return $this->mUserText
;
396 * Fetch revision's username without regard for view restrictions
399 function getRawUserText() {
400 return $this->mUserText
;
404 * Fetch revision comment if it's available to all users
407 function getComment() {
408 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
411 return $this->mComment
;
416 * Fetch revision comment without regard for the current user's permissions
419 function getRawComment() {
420 return $this->mComment
;
427 return (bool)$this->mMinorEdit
;
431 * int $field one of DELETED_* bitfield constants
434 function isDeleted( $field ) {
435 return ($this->mDeleted
& $field) == $field;
439 * Fetch revision text if it's available to all users
443 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
446 return $this->getRawText();
451 * Fetch revision text without regard for view restrictions
454 function getRawText() {
455 if( is_null( $this->mText
) ) {
456 // Revision text is immutable. Load on demand:
457 $this->mText
= $this->loadText();
463 * Fetch revision text if it's available to THIS user
467 if( !$this->userCan( self
::DELETED_TEXT
) ) {
470 return $this->getRawText();
477 function getTimestamp() {
478 return wfTimestamp(TS_MW
, $this->mTimestamp
);
484 function isCurrent() {
485 return $this->mCurrent
;
491 function getPrevious() {
492 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
494 return Revision
::newFromTitle( $this->mTitle
, $prev );
504 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
506 return Revision
::newFromTitle( $this->mTitle
, $next );
514 * Get revision text associated with an old or archive row
515 * $row is usually an object from wfFetchRow(), both the flags and the text
516 * field must be included
518 * @param integer $row Id of a row
519 * @param string $prefix table prefix (default 'old_')
520 * @return string $text|false the text requested
522 public static function getRevisionText( $row, $prefix = 'old_' ) {
523 $fname = 'Revision::getRevisionText';
524 wfProfileIn( $fname );
527 $textField = $prefix . 'text';
528 $flagsField = $prefix . 'flags';
530 if( isset( $row->$flagsField ) ) {
531 $flags = explode( ',', $row->$flagsField );
536 if( isset( $row->$textField ) ) {
537 $text = $row->$textField;
539 wfProfileOut( $fname );
543 # Use external methods for external objects, text in table is URL-only then
544 if ( in_array( 'external', $flags ) ) {
546 @list
(/* $proto */,$path)=explode('://',$url,2);
548 wfProfileOut( $fname );
551 $text=ExternalStore
::fetchFromURL($url);
554 // If the text was fetched without an error, convert it
555 if ( $text !== false ) {
556 if( in_array( 'gzip', $flags ) ) {
557 # Deal with optional compression of archived pages.
558 # This can be done periodically via maintenance/compressOld.php, and
559 # as pages are saved if $wgCompressRevisions is set.
560 $text = gzinflate( $text );
563 if( in_array( 'object', $flags ) ) {
564 # Generic compressed storage
565 $obj = unserialize( $text );
566 if ( !is_object( $obj ) ) {
568 wfProfileOut( $fname );
571 $text = $obj->getText();
574 global $wgLegacyEncoding;
575 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
576 # Old revisions kept around in a legacy encoding?
577 # Upconvert on demand.
578 global $wgInputEncoding, $wgContLang;
579 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
582 wfProfileOut( $fname );
587 * If $wgCompressRevisions is enabled, we will compress data.
588 * The input string is modified in place.
589 * Return value is the flags field: contains 'gzip' if the
590 * data is compressed, and 'utf-8' if we're saving in UTF-8
594 * @param mixed $text reference to a text
597 function compressRevisionText( &$text ) {
598 global $wgCompressRevisions;
601 # Revisions not marked this way will be converted
602 # on load if $wgLegacyCharset is set in the future.
605 if( $wgCompressRevisions ) {
606 if( function_exists( 'gzdeflate' ) ) {
607 $text = gzdeflate( $text );
610 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
613 return implode( ',', $flags );
617 * Insert a new revision into the database, returning the new revision ID
618 * number on success and dies horribly on failure.
620 * @param Database $dbw
623 function insertOn( &$dbw ) {
624 global $wgDefaultExternalStore;
626 $fname = 'Revision::insertOn';
627 wfProfileIn( $fname );
629 $data = $this->mText
;
630 $flags = Revision
::compressRevisionText( $data );
632 # Write to external storage if required
633 if ( $wgDefaultExternalStore ) {
634 if ( is_array( $wgDefaultExternalStore ) ) {
635 // Distribute storage across multiple clusters
636 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
638 $store = $wgDefaultExternalStore;
640 // Store and get the URL
641 $data = ExternalStore
::insert( $store, $data );
643 # This should only happen in the case of a configuration error, where the external store is not valid
644 throw new MWException( "Unable to store text to external storage $store" );
649 $flags .= 'external';
652 # Record the text (or external storage URL) to the text table
653 if( !isset( $this->mTextId
) ) {
654 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
655 $dbw->insert( 'text',
659 'old_flags' => $flags,
662 $this->mTextId
= $dbw->insertId();
665 # Record the edit in revisions
666 $rev_id = isset( $this->mId
)
668 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
669 $dbw->insert( 'revision',
672 'rev_page' => $this->mPage
,
673 'rev_text_id' => $this->mTextId
,
674 'rev_comment' => $this->mComment
,
675 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
676 'rev_user' => $this->mUser
,
677 'rev_user_text' => $this->mUserText
,
678 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
679 'rev_deleted' => $this->mDeleted
,
680 'rev_len' => strlen($this->mText
),
684 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
685 wfProfileOut( $fname );
690 * Lazy-load the revision's text.
691 * Currently hardcoded to the 'text' table storage engine.
696 function loadText() {
697 $fname = 'Revision::loadText';
698 wfProfileIn( $fname );
700 // Caching may be beneficial for massive use of external storage
701 global $wgRevisionCacheExpiry, $wgMemc;
702 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
703 if( $wgRevisionCacheExpiry ) {
704 $text = $wgMemc->get( $key );
705 if( is_string( $text ) ) {
706 wfProfileOut( $fname );
711 // If we kept data for lazy extraction, use it now...
712 if ( isset( $this->mTextRow
) ) {
713 $row = $this->mTextRow
;
714 $this->mTextRow
= null;
720 // Text data is immutable; check slaves first.
721 $dbr = wfGetDB( DB_SLAVE
);
722 $row = $dbr->selectRow( 'text',
723 array( 'old_text', 'old_flags' ),
724 array( 'old_id' => $this->getTextId() ),
729 // Possible slave lag!
730 $dbw = wfGetDB( DB_MASTER
);
731 $row = $dbw->selectRow( 'text',
732 array( 'old_text', 'old_flags' ),
733 array( 'old_id' => $this->getTextId() ),
737 $text = Revision
::getRevisionText( $row );
739 if( $wgRevisionCacheExpiry ) {
740 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
743 wfProfileOut( $fname );
749 * Create a new null-revision for insertion into a page's
750 * history. This will not re-save the text, but simply refer
751 * to the text from the previous version.
753 * Such revisions can for instance identify page rename
754 * operations and other such meta-modifications.
756 * @param Database $dbw
757 * @param int $pageId ID number of the page to read from
758 * @param string $summary
762 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
763 $fname = 'Revision::newNullRevision';
764 wfProfileIn( $fname );
766 $current = $dbw->selectRow(
767 array( 'page', 'revision' ),
768 array( 'page_latest', 'rev_text_id' ),
770 'page_id' => $pageId,
771 'page_latest=rev_id',
776 $revision = new Revision( array(
778 'comment' => $summary,
779 'minor_edit' => $minor,
780 'text_id' => $current->rev_text_id
,
786 wfProfileOut( $fname );
791 * Determine if the current user is allowed to view a particular
792 * field of this revision, if it's marked as deleted.
793 * @param int $field one of self::DELETED_TEXT,
794 * self::DELETED_COMMENT,
798 function userCan( $field ) {
799 if( ( $this->mDeleted
& $field ) == $field ) {
801 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
804 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
805 return $wgUser->isAllowed( $permission );
813 * Get rev_timestamp from rev_id, without loading the rest of the row
816 static function getTimestampFromID( $id ) {
817 $dbr = wfGetDB( DB_SLAVE
);
818 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
819 array( 'rev_id' => $id ), __METHOD__
);
820 if ( $timestamp === false ) {
821 # Not in slave, try master
822 $dbw = wfGetDB( DB_MASTER
);
823 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
824 array( 'rev_id' => $id ), __METHOD__
);
829 static function countByPageId( $db, $id ) {
830 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
831 array( 'rev_page' => $id ), __METHOD__
);
833 return $row->revCount
;
838 static function countByTitle( $db, $title ) {
839 $id = $title->getArticleId();
841 return Revision
::countByPageId( $db, $id );
848 * Aliases for backwards compatibility with 1.6
850 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
851 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
852 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
853 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);