12 const DELETED_TEXT
= 1;
13 const DELETED_COMMENT
= 2;
14 const DELETED_USER
= 4;
15 const DELETED_RESTRICTED
= 8;
18 * Load a page revision from a given revision ID number.
19 * Returns null if no such revision can be found.
25 public static function newFromId( $id ) {
26 return Revision
::newFromConds(
27 array( 'page_id=rev_page',
28 'rev_id' => intval( $id ) ) );
32 * Load either the current, or a specified, revision
33 * that's attached to a given title. If not attached
34 * to that title, will return null.
42 public static function newFromTitle( &$title, $id = 0 ) {
44 $matchId = intval( $id );
46 $matchId = 'page_latest';
48 return Revision
::newFromConds(
49 array( "rev_id=$matchId",
51 'page_namespace' => $title->getNamespace(),
52 'page_title' => $title->getDbkey() ) );
56 * Load a page revision from a given revision ID number.
57 * Returns null if no such revision can be found.
64 public static function loadFromId( &$db, $id ) {
65 return Revision
::loadFromConds( $db,
66 array( 'page_id=rev_page',
67 'rev_id' => intval( $id ) ) );
71 * Load either the current, or a specified, revision
72 * that's attached to a given page. If not attached
73 * to that page, will return null.
82 public static function loadFromPageId( &$db, $pageid, $id = 0 ) {
83 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
85 $conds['rev_id']=intval($id);
87 $conds[]='rev_id=page_latest';
89 return Revision
::loadFromConds( $db, $conds );
93 * Load either the current, or a specified, revision
94 * that's attached to a given page. If not attached
95 * to that page, will return null.
104 public static function loadFromTitle( &$db, $title, $id = 0 ) {
106 $matchId = intval( $id );
108 $matchId = 'page_latest';
110 return Revision
::loadFromConds(
112 array( "rev_id=$matchId",
114 'page_namespace' => $title->getNamespace(),
115 'page_title' => $title->getDbkey() ) );
119 * Load the revision for the given title with the given timestamp.
120 * WARNING: Timestamps may in some circumstances not be unique,
121 * so this isn't the best key to use.
123 * @param Database $db
124 * @param Title $title
125 * @param string $timestamp
130 public static function loadFromTimestamp( &$db, &$title, $timestamp ) {
131 return Revision
::loadFromConds(
133 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
135 'page_namespace' => $title->getNamespace(),
136 'page_title' => $title->getDbkey() ) );
140 * Given a set of conditions, fetch a revision.
142 * @param array $conditions
147 private static function newFromConds( $conditions ) {
148 $db =& wfGetDB( DB_SLAVE
);
149 $row = Revision
::loadFromConds( $db, $conditions );
150 if( is_null( $row ) ) {
151 $dbw =& wfGetDB( DB_MASTER
);
152 $row = Revision
::loadFromConds( $dbw, $conditions );
158 * Given a set of conditions, fetch a revision from
159 * the given database connection.
161 * @param Database $db
162 * @param array $conditions
167 private static function loadFromConds( &$db, $conditions ) {
168 $res = Revision
::fetchFromConds( $db, $conditions );
170 $row = $res->fetchObject();
173 $ret = new Revision( $row );
182 * Return a wrapper for a series of database rows to
183 * fetch all of a given page's revisions in turn.
184 * Each row can be fed to the constructor to get objects.
186 * @param Title $title
187 * @return ResultWrapper
191 public static function fetchAllRevisions( &$title ) {
192 return Revision
::fetchFromConds(
194 array( 'page_namespace' => $title->getNamespace(),
195 'page_title' => $title->getDbkey(),
196 'page_id=rev_page' ) );
200 * Return a wrapper for a series of database rows to
201 * fetch all of a given page's revisions in turn.
202 * Each row can be fed to the constructor to get objects.
204 * @param Title $title
205 * @return ResultWrapper
209 public static function fetchRevision( &$title ) {
210 return Revision
::fetchFromConds(
212 array( 'rev_id=page_latest',
213 'page_namespace' => $title->getNamespace(),
214 'page_title' => $title->getDbkey(),
215 'page_id=rev_page' ) );
219 * Given a set of conditions, return a ResultWrapper
220 * which will return matching database rows with the
221 * fields necessary to build Revision objects.
223 * @param Database $db
224 * @param array $conditions
225 * @return ResultWrapper
229 private static function fetchFromConds( &$db, $conditions ) {
231 array( 'page', 'revision' ),
232 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->page_latest
) ) {
268 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
269 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
272 $this->mCurrent
= false;
273 $this->mTitle
= null;
276 // Lazy extraction...
278 if( isset( $row->old_text
) ) {
279 $this->mTextRow
= $row;
281 // 'text' table row entry will be lazy-loaded
282 $this->mTextRow
= null;
284 } elseif( is_array( $row ) ) {
285 // Build a new revision to be saved...
288 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
289 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
290 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
291 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
292 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
293 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
294 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
295 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
297 // Enforce spacing trimming on supplied text
298 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
299 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
300 $this->mTextRow
= null;
302 $this->mTitle
= null; # Load on demand if needed
303 $this->mCurrent
= false;
305 throw new MWException( 'Revision constructor passed invalid row format.' );
323 function getTextId() {
324 return $this->mTextId
;
328 * Returns the title of the page associated with this entry.
331 function getTitle() {
332 if( isset( $this->mTitle
) ) {
333 return $this->mTitle
;
335 $dbr =& wfGetDB( DB_SLAVE
);
336 $row = $dbr->selectRow(
337 array( 'page', 'revision' ),
338 array( 'page_namespace', 'page_title' ),
339 array( 'page_id=rev_page',
340 'rev_id' => $this->mId
),
341 'Revision::getTitle' );
343 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
346 return $this->mTitle
;
350 * Set the title of the revision
351 * @param Title $title
353 function setTitle( $title ) {
354 $this->mTitle
= $title;
365 * Fetch revision's user id if it's available to all users
369 if( $this->isDeleted( self
::DELETED_USER
) ) {
377 * Fetch revision's user id without regard for the current user's permissions
380 function getRawUser() {
385 * Fetch revision's username if it's available to all users
388 function getUserText() {
389 if( $this->isDeleted( self
::DELETED_USER
) ) {
392 return $this->mUserText
;
397 * Fetch revision's username without regard for view restrictions
400 function getRawUserText() {
401 return $this->mUserText
;
405 * Fetch revision comment if it's available to all users
408 function getComment() {
409 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
412 return $this->mComment
;
417 * Fetch revision comment without regard for the current user's permissions
420 function getRawComment() {
421 return $this->mComment
;
428 return (bool)$this->mMinorEdit
;
432 * int $field one of DELETED_* bitfield constants
435 function isDeleted( $field ) {
436 return ($this->mDeleted
& $field) == $field;
440 * Fetch revision text if it's available to all users
444 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
447 return $this->getRawText();
452 * Fetch revision text without regard for view restrictions
455 function getRawText() {
456 if( is_null( $this->mText
) ) {
457 // Revision text is immutable. Load on demand:
458 $this->mText
= $this->loadText();
466 function getTimestamp() {
467 return wfTimestamp(TS_MW
, $this->mTimestamp
);
473 function isCurrent() {
474 return $this->mCurrent
;
480 function getPrevious() {
481 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
483 return Revision
::newFromTitle( $this->mTitle
, $prev );
493 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
495 return Revision
::newFromTitle( $this->mTitle
, $next );
503 * Get revision text associated with an old or archive row
504 * $row is usually an object from wfFetchRow(), both the flags and the text
505 * field must be included
507 * @param integer $row Id of a row
508 * @param string $prefix table prefix (default 'old_')
509 * @return string $text|false the text requested
511 function getRevisionText( $row, $prefix = 'old_' ) {
512 $fname = 'Revision::getRevisionText';
513 wfProfileIn( $fname );
516 $textField = $prefix . 'text';
517 $flagsField = $prefix . 'flags';
519 if( isset( $row->$flagsField ) ) {
520 $flags = explode( ',', $row->$flagsField );
525 if( isset( $row->$textField ) ) {
526 $text = $row->$textField;
528 wfProfileOut( $fname );
532 # Use external methods for external objects, text in table is URL-only then
533 if ( in_array( 'external', $flags ) ) {
535 @list
(/* $proto */,$path)=explode('://',$url,2);
537 wfProfileOut( $fname );
540 $text=ExternalStore
::fetchFromURL($url);
543 // If the text was fetched without an error, convert it
544 if ( $text !== false ) {
545 if( in_array( 'gzip', $flags ) ) {
546 # Deal with optional compression of archived pages.
547 # This can be done periodically via maintenance/compressOld.php, and
548 # as pages are saved if $wgCompressRevisions is set.
549 $text = gzinflate( $text );
552 if( in_array( 'object', $flags ) ) {
553 # Generic compressed storage
554 $obj = unserialize( $text );
555 if ( !is_object( $obj ) ) {
557 wfProfileOut( $fname );
560 $text = $obj->getText();
563 global $wgLegacyEncoding;
564 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
565 # Old revisions kept around in a legacy encoding?
566 # Upconvert on demand.
567 global $wgInputEncoding, $wgContLang;
568 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
571 wfProfileOut( $fname );
576 * If $wgCompressRevisions is enabled, we will compress data.
577 * The input string is modified in place.
578 * Return value is the flags field: contains 'gzip' if the
579 * data is compressed, and 'utf-8' if we're saving in UTF-8
583 * @param mixed $text reference to a text
586 function compressRevisionText( &$text ) {
587 global $wgCompressRevisions;
590 # Revisions not marked this way will be converted
591 # on load if $wgLegacyCharset is set in the future.
594 if( $wgCompressRevisions ) {
595 if( function_exists( 'gzdeflate' ) ) {
596 $text = gzdeflate( $text );
599 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
602 return implode( ',', $flags );
606 * Insert a new revision into the database, returning the new revision ID
607 * number on success and dies horribly on failure.
609 * @param Database $dbw
612 function insertOn( &$dbw ) {
613 global $wgDefaultExternalStore;
615 $fname = 'Revision::insertOn';
616 wfProfileIn( $fname );
618 $data = $this->mText
;
619 $flags = Revision
::compressRevisionText( $data );
621 # Write to external storage if required
622 if ( $wgDefaultExternalStore ) {
623 if ( is_array( $wgDefaultExternalStore ) ) {
624 // Distribute storage across multiple clusters
625 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
627 $store = $wgDefaultExternalStore;
629 // Store and get the URL
630 $data = ExternalStore
::insert( $store, $data );
632 # This should only happen in the case of a configuration error, where the external store is not valid
633 throw new MWException( "Unable to store text to external storage $store" );
638 $flags .= 'external';
641 # Record the text (or external storage URL) to the text table
642 if( !isset( $this->mTextId
) ) {
643 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
644 $dbw->insert( 'text',
648 'old_flags' => $flags,
651 $this->mTextId
= $dbw->insertId();
654 # Record the edit in revisions
655 $rev_id = isset( $this->mId
)
657 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
658 $dbw->insert( 'revision',
661 'rev_page' => $this->mPage
,
662 'rev_text_id' => $this->mTextId
,
663 'rev_comment' => $this->mComment
,
664 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
665 'rev_user' => $this->mUser
,
666 'rev_user_text' => $this->mUserText
,
667 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
668 'rev_deleted' => $this->mDeleted
,
672 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
673 wfProfileOut( $fname );
678 * Lazy-load the revision's text.
679 * Currently hardcoded to the 'text' table storage engine.
684 function loadText() {
685 $fname = 'Revision::loadText';
686 wfProfileIn( $fname );
688 // Caching may be beneficial for massive use of external storage
689 global $wgRevisionCacheExpiry, $wgMemc;
690 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
691 if( $wgRevisionCacheExpiry ) {
692 $text = $wgMemc->get( $key );
693 if( is_string( $text ) ) {
694 wfProfileOut( $fname );
699 // If we kept data for lazy extraction, use it now...
700 if ( isset( $this->mTextRow
) ) {
701 $row = $this->mTextRow
;
702 $this->mTextRow
= null;
708 // Text data is immutable; check slaves first.
709 $dbr =& wfGetDB( DB_SLAVE
);
710 $row = $dbr->selectRow( 'text',
711 array( 'old_text', 'old_flags' ),
712 array( 'old_id' => $this->getTextId() ),
717 // Possible slave lag!
718 $dbw =& wfGetDB( DB_MASTER
);
719 $row = $dbw->selectRow( 'text',
720 array( 'old_text', 'old_flags' ),
721 array( 'old_id' => $this->getTextId() ),
725 $text = Revision
::getRevisionText( $row );
727 if( $wgRevisionCacheExpiry ) {
728 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
731 wfProfileOut( $fname );
737 * Create a new null-revision for insertion into a page's
738 * history. This will not re-save the text, but simply refer
739 * to the text from the previous version.
741 * Such revisions can for instance identify page rename
742 * operations and other such meta-modifications.
744 * @param Database $dbw
745 * @param int $pageId ID number of the page to read from
746 * @param string $summary
750 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
751 $fname = 'Revision::newNullRevision';
752 wfProfileIn( $fname );
754 $current = $dbw->selectRow(
755 array( 'page', 'revision' ),
756 array( 'page_latest', 'rev_text_id' ),
758 'page_id' => $pageId,
759 'page_latest=rev_id',
764 $revision = new Revision( array(
766 'comment' => $summary,
767 'minor_edit' => $minor,
768 'text_id' => $current->rev_text_id
,
774 wfProfileOut( $fname );
779 * Determine if the current user is allowed to view a particular
780 * field of this revision, if it's marked as deleted.
781 * @param int $field one of self::DELETED_TEXT,
782 * self::DELETED_COMMENT,
786 function userCan( $field ) {
787 if( ( $this->mDeleted
& $field ) == $field ) {
789 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
792 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
793 return $wgUser->isAllowed( $permission );
801 * Get rev_timestamp from rev_id, without loading the rest of the row
804 static function getTimestampFromID( $id ) {
805 $dbr =& wfGetDB( DB_SLAVE
);
806 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
807 array( 'rev_id' => $id ), __METHOD__
);
808 if ( $timestamp === false ) {
809 # Not in slave, try master
810 $dbw =& wfGetDB( DB_MASTER
);
811 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
812 array( 'rev_id' => $id ), __METHOD__
);
817 static function countByPageId( $db, $id ) {
818 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
819 array( 'rev_page' => $id ), __METHOD__
);
821 return $row->revCount
;
826 static function countByTitle( $db, $title ) {
827 $id = $title->getArticleId();
829 return Revision
::countByPageId( $db, $id );
836 * Aliases for backwards compatibility with 1.6
838 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
839 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
840 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
841 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);