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 );
252 * Return the list of revision fields that should be selected to create
255 static function selectFields() {
274 function Revision( $row ) {
275 if( is_object( $row ) ) {
276 $this->mId
= intval( $row->rev_id
);
277 $this->mPage
= intval( $row->rev_page
);
278 $this->mTextId
= intval( $row->rev_text_id
);
279 $this->mComment
= $row->rev_comment
;
280 $this->mUserText
= $row->rev_user_text
;
281 $this->mUser
= intval( $row->rev_user
);
282 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
283 $this->mTimestamp
= $row->rev_timestamp
;
284 $this->mDeleted
= intval( $row->rev_deleted
);
286 if( is_null( $row->rev_len
) )
289 $this->mSize
= intval( $row->rev_len
);
291 if( isset( $row->page_latest
) ) {
292 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
293 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
296 $this->mCurrent
= false;
297 $this->mTitle
= null;
300 // Lazy extraction...
302 if( isset( $row->old_text
) ) {
303 $this->mTextRow
= $row;
305 // 'text' table row entry will be lazy-loaded
306 $this->mTextRow
= null;
308 } elseif( is_array( $row ) ) {
309 // Build a new revision to be saved...
312 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
313 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
314 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
315 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
316 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
317 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
318 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
319 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
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;
329 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
331 throw new MWException( 'Revision constructor passed invalid row format.' );
349 function getTextId() {
350 return $this->mTextId
;
354 * Returns the length of the text in this revision, or null if unknown.
361 * Returns the title of the page associated with this entry.
364 function getTitle() {
365 if( isset( $this->mTitle
) ) {
366 return $this->mTitle
;
368 $dbr = wfGetDB( DB_SLAVE
);
369 $row = $dbr->selectRow(
370 array( 'page', 'revision' ),
371 array( 'page_namespace', 'page_title' ),
372 array( 'page_id=rev_page',
373 'rev_id' => $this->mId
),
374 'Revision::getTitle' );
376 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
379 return $this->mTitle
;
383 * Set the title of the revision
384 * @param Title $title
386 function setTitle( $title ) {
387 $this->mTitle
= $title;
398 * Fetch revision's user id if it's available to all users
402 if( $this->isDeleted( self
::DELETED_USER
) ) {
410 * Fetch revision's user id without regard for the current user's permissions
413 function getRawUser() {
418 * Fetch revision's username if it's available to all users
421 function getUserText() {
422 if( $this->isDeleted( self
::DELETED_USER
) ) {
425 return $this->mUserText
;
430 * Fetch revision's username without regard for view restrictions
433 function getRawUserText() {
434 return $this->mUserText
;
438 * Fetch revision comment if it's available to all users
441 function getComment() {
442 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
445 return $this->mComment
;
450 * Fetch revision comment without regard for the current user's permissions
453 function getRawComment() {
454 return $this->mComment
;
461 return (bool)$this->mMinorEdit
;
465 * int $field one of DELETED_* bitfield constants
468 function isDeleted( $field ) {
469 return ($this->mDeleted
& $field) == $field;
473 * Fetch revision text if it's available to all users
477 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
480 return $this->getRawText();
485 * Fetch revision text without regard for view restrictions
488 function getRawText() {
489 if( is_null( $this->mText
) ) {
490 // Revision text is immutable. Load on demand:
491 $this->mText
= $this->loadText();
497 * Fetch revision text if it's available to THIS user
501 if( !$this->userCan( self
::DELETED_TEXT
) ) {
504 return $this->getRawText();
511 function getTimestamp() {
512 return wfTimestamp(TS_MW
, $this->mTimestamp
);
518 function isCurrent() {
519 return $this->mCurrent
;
525 function getPrevious() {
526 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
528 return Revision
::newFromTitle( $this->mTitle
, $prev );
538 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
540 return Revision
::newFromTitle( $this->mTitle
, $next );
548 * Get revision text associated with an old or archive row
549 * $row is usually an object from wfFetchRow(), both the flags and the text
550 * field must be included
552 * @param integer $row Id of a row
553 * @param string $prefix table prefix (default 'old_')
554 * @return string $text|false the text requested
556 public static function getRevisionText( $row, $prefix = 'old_' ) {
557 $fname = 'Revision::getRevisionText';
558 wfProfileIn( $fname );
561 $textField = $prefix . 'text';
562 $flagsField = $prefix . 'flags';
564 if( isset( $row->$flagsField ) ) {
565 $flags = explode( ',', $row->$flagsField );
570 if( isset( $row->$textField ) ) {
571 $text = $row->$textField;
573 wfProfileOut( $fname );
577 # Use external methods for external objects, text in table is URL-only then
578 if ( in_array( 'external', $flags ) ) {
580 @list
(/* $proto */,$path)=explode('://',$url,2);
582 wfProfileOut( $fname );
585 $text=ExternalStore
::fetchFromURL($url);
588 // If the text was fetched without an error, convert it
589 if ( $text !== false ) {
590 if( in_array( 'gzip', $flags ) ) {
591 # Deal with optional compression of archived pages.
592 # This can be done periodically via maintenance/compressOld.php, and
593 # as pages are saved if $wgCompressRevisions is set.
594 $text = gzinflate( $text );
597 if( in_array( 'object', $flags ) ) {
598 # Generic compressed storage
599 $obj = unserialize( $text );
600 if ( !is_object( $obj ) ) {
602 wfProfileOut( $fname );
605 $text = $obj->getText();
608 global $wgLegacyEncoding;
609 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
610 # Old revisions kept around in a legacy encoding?
611 # Upconvert on demand.
612 global $wgInputEncoding, $wgContLang;
613 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
616 wfProfileOut( $fname );
621 * If $wgCompressRevisions is enabled, we will compress data.
622 * The input string is modified in place.
623 * Return value is the flags field: contains 'gzip' if the
624 * data is compressed, and 'utf-8' if we're saving in UTF-8
628 * @param mixed $text reference to a text
631 function compressRevisionText( &$text ) {
632 global $wgCompressRevisions;
635 # Revisions not marked this way will be converted
636 # on load if $wgLegacyCharset is set in the future.
639 if( $wgCompressRevisions ) {
640 if( function_exists( 'gzdeflate' ) ) {
641 $text = gzdeflate( $text );
644 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
647 return implode( ',', $flags );
651 * Insert a new revision into the database, returning the new revision ID
652 * number on success and dies horribly on failure.
654 * @param Database $dbw
657 function insertOn( &$dbw ) {
658 global $wgDefaultExternalStore;
660 $fname = 'Revision::insertOn';
661 wfProfileIn( $fname );
663 $data = $this->mText
;
664 $flags = Revision
::compressRevisionText( $data );
666 # Write to external storage if required
667 if ( $wgDefaultExternalStore ) {
668 if ( is_array( $wgDefaultExternalStore ) ) {
669 // Distribute storage across multiple clusters
670 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
672 $store = $wgDefaultExternalStore;
674 // Store and get the URL
675 $data = ExternalStore
::insert( $store, $data );
677 # This should only happen in the case of a configuration error, where the external store is not valid
678 throw new MWException( "Unable to store text to external storage $store" );
683 $flags .= 'external';
686 # Record the text (or external storage URL) to the text table
687 if( !isset( $this->mTextId
) ) {
688 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
689 $dbw->insert( 'text',
693 'old_flags' => $flags,
696 $this->mTextId
= $dbw->insertId();
699 # Record the edit in revisions
700 $rev_id = isset( $this->mId
)
702 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
703 $dbw->insert( 'revision',
706 'rev_page' => $this->mPage
,
707 'rev_text_id' => $this->mTextId
,
708 'rev_comment' => $this->mComment
,
709 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
710 'rev_user' => $this->mUser
,
711 'rev_user_text' => $this->mUserText
,
712 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
713 'rev_deleted' => $this->mDeleted
,
714 'rev_len' => strlen($this->mText
),
718 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
719 wfProfileOut( $fname );
724 * Lazy-load the revision's text.
725 * Currently hardcoded to the 'text' table storage engine.
730 function loadText() {
731 $fname = 'Revision::loadText';
732 wfProfileIn( $fname );
734 // Caching may be beneficial for massive use of external storage
735 global $wgRevisionCacheExpiry, $wgMemc;
736 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
737 if( $wgRevisionCacheExpiry ) {
738 $text = $wgMemc->get( $key );
739 if( is_string( $text ) ) {
740 wfProfileOut( $fname );
745 // If we kept data for lazy extraction, use it now...
746 if ( isset( $this->mTextRow
) ) {
747 $row = $this->mTextRow
;
748 $this->mTextRow
= null;
754 // Text data is immutable; check slaves first.
755 $dbr = wfGetDB( DB_SLAVE
);
756 $row = $dbr->selectRow( 'text',
757 array( 'old_text', 'old_flags' ),
758 array( 'old_id' => $this->getTextId() ),
763 // Possible slave lag!
764 $dbw = wfGetDB( DB_MASTER
);
765 $row = $dbw->selectRow( 'text',
766 array( 'old_text', 'old_flags' ),
767 array( 'old_id' => $this->getTextId() ),
771 $text = Revision
::getRevisionText( $row );
773 if( $wgRevisionCacheExpiry ) {
774 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
777 wfProfileOut( $fname );
783 * Create a new null-revision for insertion into a page's
784 * history. This will not re-save the text, but simply refer
785 * to the text from the previous version.
787 * Such revisions can for instance identify page rename
788 * operations and other such meta-modifications.
790 * @param Database $dbw
791 * @param int $pageId ID number of the page to read from
792 * @param string $summary
796 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
797 $fname = 'Revision::newNullRevision';
798 wfProfileIn( $fname );
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( $fname );
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
);