10 const DELETED_TEXT
= 1;
11 const DELETED_COMMENT
= 2;
12 const DELETED_USER
= 4;
13 const DELETED_RESTRICTED
= 8;
16 * Load a page revision from a given revision ID number.
17 * Returns null if no such revision can be found.
23 public static function newFromId( $id ) {
24 return Revision
::newFromConds(
25 array( 'page_id=rev_page',
26 'rev_id' => intval( $id ) ) );
30 * Load either the current, or a specified, revision
31 * that's attached to a given title. If not attached
32 * to that title, will return null.
40 public static function newFromTitle( &$title, $id = 0 ) {
42 $matchId = intval( $id );
44 $matchId = 'page_latest';
46 return Revision
::newFromConds(
47 array( "rev_id=$matchId",
49 'page_namespace' => $title->getNamespace(),
50 'page_title' => $title->getDbkey() ) );
54 * Load a page revision from a given revision ID number.
55 * Returns null if no such revision can be found.
62 public static function loadFromId( &$db, $id ) {
63 return Revision
::loadFromConds( $db,
64 array( 'page_id=rev_page',
65 'rev_id' => intval( $id ) ) );
69 * Load either the current, or a specified, revision
70 * that's attached to a given page. If not attached
71 * to that page, will return null.
80 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
81 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
83 $conds['rev_id']=intval($id);
85 $conds[]='rev_id=page_latest';
87 return Revision
::loadFromConds( $db, $conds );
91 * Load either the current, or a specified, revision
92 * that's attached to a given page. If not attached
93 * to that page, will return null.
102 public static function loadFromTitle( &$db, $title, $id = 0 ) {
104 $matchId = intval( $id );
106 $matchId = 'page_latest';
108 return Revision
::loadFromConds(
110 array( "rev_id=$matchId",
112 'page_namespace' => $title->getNamespace(),
113 'page_title' => $title->getDbkey() ) );
117 * Load the revision for the given title with the given timestamp.
118 * WARNING: Timestamps may in some circumstances not be unique,
119 * so this isn't the best key to use.
121 * @param Database $db
122 * @param Title $title
123 * @param string $timestamp
128 public static function loadFromTimestamp( &$db, &$title, $timestamp ) {
129 return Revision
::loadFromConds(
131 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
133 'page_namespace' => $title->getNamespace(),
134 'page_title' => $title->getDbkey() ) );
138 * Given a set of conditions, fetch a revision.
140 * @param array $conditions
145 private static function newFromConds( $conditions ) {
146 $db = wfGetDB( DB_SLAVE
);
147 $row = Revision
::loadFromConds( $db, $conditions );
148 if( is_null( $row ) ) {
149 $dbw = wfGetDB( DB_MASTER
);
150 $row = Revision
::loadFromConds( $dbw, $conditions );
156 * Given a set of conditions, fetch a revision from
157 * the given database connection.
159 * @param Database $db
160 * @param array $conditions
165 private static function loadFromConds( $db, $conditions ) {
166 $res = Revision
::fetchFromConds( $db, $conditions );
168 $row = $res->fetchObject();
171 $ret = new Revision( $row );
180 * Return a wrapper for a series of database rows to
181 * fetch all of a given page's revisions in turn.
182 * Each row can be fed to the constructor to get objects.
184 * @param Title $title
185 * @return ResultWrapper
189 public static function fetchAllRevisions( &$title ) {
190 return Revision
::fetchFromConds(
192 array( 'page_namespace' => $title->getNamespace(),
193 'page_title' => $title->getDbkey(),
194 'page_id=rev_page' ) );
198 * Return a wrapper for a series of database rows to
199 * fetch all of a given page's revisions in turn.
200 * Each row can be fed to the constructor to get objects.
202 * @param Title $title
203 * @return ResultWrapper
207 public static function fetchRevision( &$title ) {
208 return Revision
::fetchFromConds(
210 array( 'rev_id=page_latest',
211 'page_namespace' => $title->getNamespace(),
212 'page_title' => $title->getDbkey(),
213 'page_id=rev_page' ) );
217 * Given a set of conditions, return a ResultWrapper
218 * which will return matching database rows with the
219 * fields necessary to build Revision objects.
221 * @param Database $db
222 * @param array $conditions
223 * @return ResultWrapper
227 private static function fetchFromConds( $db, $conditions ) {
229 array( 'page', 'revision' ),
230 array( 'page_namespace',
243 'Revision::fetchRow',
244 array( 'LIMIT' => 1 ) );
245 $ret = $db->resultObject( $res );
253 function Revision( $row ) {
254 if( is_object( $row ) ) {
255 $this->mId
= intval( $row->rev_id
);
256 $this->mPage
= intval( $row->rev_page
);
257 $this->mTextId
= intval( $row->rev_text_id
);
258 $this->mComment
= $row->rev_comment
;
259 $this->mUserText
= $row->rev_user_text
;
260 $this->mUser
= intval( $row->rev_user
);
261 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
262 $this->mTimestamp
= $row->rev_timestamp
;
263 $this->mDeleted
= intval( $row->rev_deleted
);
265 if( isset( $row->page_latest
) ) {
266 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
267 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
270 $this->mCurrent
= false;
271 $this->mTitle
= null;
274 // Lazy extraction...
276 if( isset( $row->old_text
) ) {
277 $this->mTextRow
= $row;
279 // 'text' table row entry will be lazy-loaded
280 $this->mTextRow
= null;
282 } elseif( is_array( $row ) ) {
283 // Build a new revision to be saved...
286 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
287 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
288 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
289 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
290 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
291 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
292 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
293 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
295 // Enforce spacing trimming on supplied text
296 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
297 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
298 $this->mTextRow
= null;
300 $this->mTitle
= null; # Load on demand if needed
301 $this->mCurrent
= false;
303 throw new MWException( 'Revision constructor passed invalid row format.' );
321 function getTextId() {
322 return $this->mTextId
;
326 * Returns the title of the page associated with this entry.
329 function getTitle() {
330 if( isset( $this->mTitle
) ) {
331 return $this->mTitle
;
333 $dbr = wfGetDB( DB_SLAVE
);
334 $row = $dbr->selectRow(
335 array( 'page', 'revision' ),
336 array( 'page_namespace', 'page_title' ),
337 array( 'page_id=rev_page',
338 'rev_id' => $this->mId
),
339 'Revision::getTitle' );
341 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
344 return $this->mTitle
;
348 * Set the title of the revision
349 * @param Title $title
351 function setTitle( $title ) {
352 $this->mTitle
= $title;
363 * Fetch revision's user id if it's available to all users
367 if( $this->isDeleted( self
::DELETED_USER
) ) {
375 * Fetch revision's user id without regard for the current user's permissions
378 function getRawUser() {
383 * Fetch revision's username if it's available to all users
386 function getUserText() {
387 if( $this->isDeleted( self
::DELETED_USER
) ) {
390 return $this->mUserText
;
395 * Fetch revision's username without regard for view restrictions
398 function getRawUserText() {
399 return $this->mUserText
;
403 * Fetch revision comment if it's available to all users
406 function getComment() {
407 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
410 return $this->mComment
;
415 * Fetch revision comment without regard for the current user's permissions
418 function getRawComment() {
419 return $this->mComment
;
426 return (bool)$this->mMinorEdit
;
430 * int $field one of DELETED_* bitfield constants
433 function isDeleted( $field ) {
434 return ($this->mDeleted
& $field) == $field;
438 * Fetch revision text if it's available to all users
442 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
445 return $this->getRawText();
450 * Fetch revision text without regard for view restrictions
453 function getRawText() {
454 if( is_null( $this->mText
) ) {
455 // Revision text is immutable. Load on demand:
456 $this->mText
= $this->loadText();
464 function getTimestamp() {
465 return wfTimestamp(TS_MW
, $this->mTimestamp
);
471 function isCurrent() {
472 return $this->mCurrent
;
478 function getPrevious() {
479 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
481 return Revision
::newFromTitle( $this->mTitle
, $prev );
491 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
493 return Revision
::newFromTitle( $this->mTitle
, $next );
501 * Get revision text associated with an old or archive row
502 * $row is usually an object from wfFetchRow(), both the flags and the text
503 * field must be included
505 * @param integer $row Id of a row
506 * @param string $prefix table prefix (default 'old_')
507 * @return string $text|false the text requested
509 public static function getRevisionText( $row, $prefix = 'old_' ) {
510 $fname = 'Revision::getRevisionText';
511 wfProfileIn( $fname );
514 $textField = $prefix . 'text';
515 $flagsField = $prefix . 'flags';
517 if( isset( $row->$flagsField ) ) {
518 $flags = explode( ',', $row->$flagsField );
523 if( isset( $row->$textField ) ) {
524 $text = $row->$textField;
526 wfProfileOut( $fname );
530 # Use external methods for external objects, text in table is URL-only then
531 if ( in_array( 'external', $flags ) ) {
533 @list
(/* $proto */,$path)=explode('://',$url,2);
535 wfProfileOut( $fname );
538 $text=ExternalStore
::fetchFromURL($url);
541 // If the text was fetched without an error, convert it
542 if ( $text !== false ) {
543 if( in_array( 'gzip', $flags ) ) {
544 # Deal with optional compression of archived pages.
545 # This can be done periodically via maintenance/compressOld.php, and
546 # as pages are saved if $wgCompressRevisions is set.
547 $text = gzinflate( $text );
550 if( in_array( 'object', $flags ) ) {
551 # Generic compressed storage
552 $obj = unserialize( $text );
553 if ( !is_object( $obj ) ) {
555 wfProfileOut( $fname );
558 $text = $obj->getText();
561 global $wgLegacyEncoding;
562 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
563 # Old revisions kept around in a legacy encoding?
564 # Upconvert on demand.
565 global $wgInputEncoding, $wgContLang;
566 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
569 wfProfileOut( $fname );
574 * If $wgCompressRevisions is enabled, we will compress data.
575 * The input string is modified in place.
576 * Return value is the flags field: contains 'gzip' if the
577 * data is compressed, and 'utf-8' if we're saving in UTF-8
581 * @param mixed $text reference to a text
584 function compressRevisionText( &$text ) {
585 global $wgCompressRevisions;
588 # Revisions not marked this way will be converted
589 # on load if $wgLegacyCharset is set in the future.
592 if( $wgCompressRevisions ) {
593 if( function_exists( 'gzdeflate' ) ) {
594 $text = gzdeflate( $text );
597 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
600 return implode( ',', $flags );
604 * Insert a new revision into the database, returning the new revision ID
605 * number on success and dies horribly on failure.
607 * @param Database $dbw
610 function insertOn( &$dbw ) {
611 global $wgDefaultExternalStore;
613 $fname = 'Revision::insertOn';
614 wfProfileIn( $fname );
616 $data = $this->mText
;
617 $flags = Revision
::compressRevisionText( $data );
619 # Write to external storage if required
620 if ( $wgDefaultExternalStore ) {
621 if ( is_array( $wgDefaultExternalStore ) ) {
622 // Distribute storage across multiple clusters
623 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
625 $store = $wgDefaultExternalStore;
627 // Store and get the URL
628 $data = ExternalStore
::insert( $store, $data );
630 # This should only happen in the case of a configuration error, where the external store is not valid
631 throw new MWException( "Unable to store text to external storage $store" );
636 $flags .= 'external';
639 # Record the text (or external storage URL) to the text table
640 if( !isset( $this->mTextId
) ) {
641 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
642 $dbw->insert( 'text',
646 'old_flags' => $flags,
649 $this->mTextId
= $dbw->insertId();
652 # Record the edit in revisions
653 $rev_id = isset( $this->mId
)
655 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
656 $dbw->insert( 'revision',
659 'rev_page' => $this->mPage
,
660 'rev_text_id' => $this->mTextId
,
661 'rev_comment' => $this->mComment
,
662 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
663 'rev_user' => $this->mUser
,
664 'rev_user_text' => $this->mUserText
,
665 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
666 'rev_deleted' => $this->mDeleted
,
670 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
671 wfProfileOut( $fname );
676 * Lazy-load the revision's text.
677 * Currently hardcoded to the 'text' table storage engine.
682 function loadText() {
683 $fname = 'Revision::loadText';
684 wfProfileIn( $fname );
686 // Caching may be beneficial for massive use of external storage
687 global $wgRevisionCacheExpiry, $wgMemc;
688 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
689 if( $wgRevisionCacheExpiry ) {
690 $text = $wgMemc->get( $key );
691 if( is_string( $text ) ) {
692 wfProfileOut( $fname );
697 // If we kept data for lazy extraction, use it now...
698 if ( isset( $this->mTextRow
) ) {
699 $row = $this->mTextRow
;
700 $this->mTextRow
= null;
706 // Text data is immutable; check slaves first.
707 $dbr = wfGetDB( DB_SLAVE
);
708 $row = $dbr->selectRow( 'text',
709 array( 'old_text', 'old_flags' ),
710 array( 'old_id' => $this->getTextId() ),
715 // Possible slave lag!
716 $dbw = wfGetDB( DB_MASTER
);
717 $row = $dbw->selectRow( 'text',
718 array( 'old_text', 'old_flags' ),
719 array( 'old_id' => $this->getTextId() ),
723 $text = Revision
::getRevisionText( $row );
725 if( $wgRevisionCacheExpiry ) {
726 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
729 wfProfileOut( $fname );
735 * Create a new null-revision for insertion into a page's
736 * history. This will not re-save the text, but simply refer
737 * to the text from the previous version.
739 * Such revisions can for instance identify page rename
740 * operations and other such meta-modifications.
742 * @param Database $dbw
743 * @param int $pageId ID number of the page to read from
744 * @param string $summary
748 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
749 $fname = 'Revision::newNullRevision';
750 wfProfileIn( $fname );
752 $current = $dbw->selectRow(
753 array( 'page', 'revision' ),
754 array( 'page_latest', 'rev_text_id' ),
756 'page_id' => $pageId,
757 'page_latest=rev_id',
762 $revision = new Revision( array(
764 'comment' => $summary,
765 'minor_edit' => $minor,
766 'text_id' => $current->rev_text_id
,
772 wfProfileOut( $fname );
777 * Determine if the current user is allowed to view a particular
778 * field of this revision, if it's marked as deleted.
779 * @param int $field one of self::DELETED_TEXT,
780 * self::DELETED_COMMENT,
784 function userCan( $field ) {
785 if( ( $this->mDeleted
& $field ) == $field ) {
787 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
790 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
791 return $wgUser->isAllowed( $permission );
799 * Get rev_timestamp from rev_id, without loading the rest of the row
802 static function getTimestampFromID( $id ) {
803 $dbr = wfGetDB( DB_SLAVE
);
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
);