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;
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();
465 function getTimestamp() {
466 return wfTimestamp(TS_MW
, $this->mTimestamp
);
472 function isCurrent() {
473 return $this->mCurrent
;
479 function getPrevious() {
480 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
482 return Revision
::newFromTitle( $this->mTitle
, $prev );
492 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
494 return Revision
::newFromTitle( $this->mTitle
, $next );
502 * Get revision text associated with an old or archive row
503 * $row is usually an object from wfFetchRow(), both the flags and the text
504 * field must be included
506 * @param integer $row Id of a row
507 * @param string $prefix table prefix (default 'old_')
508 * @return string $text|false the text requested
510 function getRevisionText( $row, $prefix = 'old_' ) {
511 $fname = 'Revision::getRevisionText';
512 wfProfileIn( $fname );
515 $textField = $prefix . 'text';
516 $flagsField = $prefix . 'flags';
518 if( isset( $row->$flagsField ) ) {
519 $flags = explode( ',', $row->$flagsField );
524 if( isset( $row->$textField ) ) {
525 $text = $row->$textField;
527 wfProfileOut( $fname );
531 # Use external methods for external objects, text in table is URL-only then
532 if ( in_array( 'external', $flags ) ) {
534 @list
($proto,$path)=explode('://',$url,2);
536 wfProfileOut( $fname );
539 $text=ExternalStore
::fetchFromURL($url);
542 // If the text was fetched without an error, convert it
543 if ( $text !== false ) {
544 if( in_array( 'gzip', $flags ) ) {
545 # Deal with optional compression of archived pages.
546 # This can be done periodically via maintenance/compressOld.php, and
547 # as pages are saved if $wgCompressRevisions is set.
548 $text = gzinflate( $text );
551 if( in_array( 'object', $flags ) ) {
552 # Generic compressed storage
553 $obj = unserialize( $text );
554 if ( !is_object( $obj ) ) {
556 wfProfileOut( $fname );
559 $text = $obj->getText();
562 global $wgLegacyEncoding;
563 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
564 # Old revisions kept around in a legacy encoding?
565 # Upconvert on demand.
566 global $wgInputEncoding, $wgContLang;
567 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
570 wfProfileOut( $fname );
575 * If $wgCompressRevisions is enabled, we will compress data.
576 * The input string is modified in place.
577 * Return value is the flags field: contains 'gzip' if the
578 * data is compressed, and 'utf-8' if we're saving in UTF-8
582 * @param mixed $text reference to a text
585 function compressRevisionText( &$text ) {
586 global $wgCompressRevisions;
589 # Revisions not marked this way will be converted
590 # on load if $wgLegacyCharset is set in the future.
593 if( $wgCompressRevisions ) {
594 if( function_exists( 'gzdeflate' ) ) {
595 $text = gzdeflate( $text );
598 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
601 return implode( ',', $flags );
605 * Insert a new revision into the database, returning the new revision ID
606 * number on success and dies horribly on failure.
608 * @param Database $dbw
611 function insertOn( &$dbw ) {
612 global $wgDefaultExternalStore;
614 $fname = 'Revision::insertOn';
615 wfProfileIn( $fname );
617 $data = $this->mText
;
618 $flags = Revision
::compressRevisionText( $data );
620 # Write to external storage if required
621 if ( $wgDefaultExternalStore ) {
622 if ( is_array( $wgDefaultExternalStore ) ) {
623 // Distribute storage across multiple clusters
624 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
626 $store = $wgDefaultExternalStore;
628 // Store and get the URL
629 $data = ExternalStore
::insert( $store, $data );
631 # This should only happen in the case of a configuration error, where the external store is not valid
632 throw new MWException( "Unable to store text to external storage $store" );
637 $flags .= 'external';
640 # Record the text (or external storage URL) to the text table
641 if( !isset( $this->mTextId
) ) {
642 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
643 $dbw->insert( 'text',
647 'old_flags' => $flags,
650 $this->mTextId
= $dbw->insertId();
653 # Record the edit in revisions
654 $rev_id = isset( $this->mId
)
656 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
657 $dbw->insert( 'revision',
660 'rev_page' => $this->mPage
,
661 'rev_text_id' => $this->mTextId
,
662 'rev_comment' => $this->mComment
,
663 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
664 'rev_user' => $this->mUser
,
665 'rev_user_text' => $this->mUserText
,
666 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
667 'rev_deleted' => $this->mDeleted
,
671 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
672 wfProfileOut( $fname );
677 * Lazy-load the revision's text.
678 * Currently hardcoded to the 'text' table storage engine.
683 function loadText() {
684 $fname = 'Revision::loadText';
685 wfProfileIn( $fname );
687 // Caching may be beneficial for massive use of external storage
688 global $wgRevisionCacheExpiry, $wgMemc;
689 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
690 if( $wgRevisionCacheExpiry ) {
691 $text = $wgMemc->get( $key );
692 if( is_string( $text ) ) {
693 wfProfileOut( $fname );
698 // If we kept data for lazy extraction, use it now...
699 if ( isset( $this->mTextRow
) ) {
700 $row = $this->mTextRow
;
701 $this->mTextRow
= null;
707 // Text data is immutable; check slaves first.
708 $dbr =& wfGetDB( DB_SLAVE
);
709 $row = $dbr->selectRow( 'text',
710 array( 'old_text', 'old_flags' ),
711 array( 'old_id' => $this->getTextId() ),
716 // Possible slave lag!
717 $dbw =& wfGetDB( DB_MASTER
);
718 $row = $dbw->selectRow( 'text',
719 array( 'old_text', 'old_flags' ),
720 array( 'old_id' => $this->getTextId() ),
724 $text = Revision
::getRevisionText( $row );
726 if( $wgRevisionCacheExpiry ) {
727 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
730 wfProfileOut( $fname );
736 * Create a new null-revision for insertion into a page's
737 * history. This will not re-save the text, but simply refer
738 * to the text from the previous version.
740 * Such revisions can for instance identify page rename
741 * operations and other such meta-modifications.
743 * @param Database $dbw
744 * @param int $pageId ID number of the page to read from
745 * @param string $summary
749 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
750 $fname = 'Revision::newNullRevision';
751 wfProfileIn( $fname );
753 $current = $dbw->selectRow(
754 array( 'page', 'revision' ),
755 array( 'page_latest', 'rev_text_id' ),
757 'page_id' => $pageId,
758 'page_latest=rev_id',
763 $revision = new Revision( array(
765 'comment' => $summary,
766 'minor_edit' => $minor,
767 'text_id' => $current->rev_text_id
,
773 wfProfileOut( $fname );
778 * Determine if the current user is allowed to view a particular
779 * field of this revision, if it's marked as deleted.
780 * @param int $field one of self::DELETED_TEXT,
781 * self::DELETED_COMMENT,
785 function userCan( $field ) {
786 if( ( $this->mDeleted
& $field ) == $field ) {
788 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
791 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
792 return $wgUser->isAllowed( $permission );
800 * Get rev_timestamp from rev_id, without loading the rest of the row
803 static function getTimestampFromID( $id ) {
804 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
805 array( 'rev_id' => $id ), __METHOD__
);
806 if ( $timestamp === false ) {
807 # Not in slave, try master
808 $dbw =& wfGetDB( DB_MASTER
);
809 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
810 array( 'rev_id' => $id ), __METHOD__
);
815 static function countByPageId( $db, $id ) {
816 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
817 array( 'rev_page' => $id ), __METHOD__
);
819 return $row->revCount
;
824 static function countByTitle( $db, $title ) {
825 $id = $title->getArticleId();
827 return Revision
::countByPageId( $db, $id );
834 * Aliases for backwards compatibility with 1.6
836 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
837 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
838 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
839 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);