8 define( 'MW_REV_DELETED_TEXT', 1 );
9 define( 'MW_REV_DELETED_COMMENT', 2 );
10 define( 'MW_REV_DELETED_USER', 4 );
11 define( 'MW_REV_DELETED_RESTRICTED', 8 );
20 * Load a page revision from a given revision ID number.
21 * Returns null if no such revision can be found.
27 function newFromId( $id ) {
28 return Revision
::newFromConds(
29 array( 'page_id=rev_page',
30 'rev_id' => intval( $id ) ) );
34 * Load either the current, or a specified, revision
35 * that's attached to a given title. If not attached
36 * to that title, will return null.
44 function newFromTitle( &$title, $id = 0 ) {
46 $matchId = intval( $id );
48 $matchId = 'page_latest';
50 return Revision
::newFromConds(
51 array( "rev_id=$matchId",
53 'page_namespace' => $title->getNamespace(),
54 'page_title' => $title->getDbkey() ) );
58 * Load either the current, or a specified, revision
59 * that's attached to a given page. If not attached
60 * to that page, will return null.
68 function loadFromPageId( &$db, $pageid, $id = 0 ) {
69 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
71 $conds['rev_id']=intval($id);
73 $conds[]='rev_id=page_latest';
75 return Revision
::loadFromConds( $db, $conds );
79 * Load either the current, or a specified, revision
80 * that's attached to a given page. If not attached
81 * to that page, will return null.
89 function loadFromTitle( &$db, $title, $id = 0 ) {
91 $matchId = intval( $id );
93 $matchId = 'page_latest';
95 return Revision
::loadFromConds(
97 array( "rev_id=$matchId",
99 'page_namespace' => $title->getNamespace(),
100 'page_title' => $title->getDbkey() ) );
104 * Load the revision for the given title with the given timestamp.
105 * WARNING: Timestamps may in some circumstances not be unique,
106 * so this isn't the best key to use.
108 * @param Database $db
109 * @param Title $title
110 * @param string $timestamp
115 function loadFromTimestamp( &$db, &$title, $timestamp ) {
116 return Revision
::loadFromConds(
118 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
120 'page_namespace' => $title->getNamespace(),
121 'page_title' => $title->getDbkey() ) );
125 * Given a set of conditions, fetch a revision.
127 * @param array $conditions
132 function newFromConds( $conditions ) {
133 $db =& wfGetDB( DB_SLAVE
);
134 $row = Revision
::loadFromConds( $db, $conditions );
135 if( is_null( $row ) ) {
136 $dbw =& wfGetDB( DB_MASTER
);
137 $row = Revision
::loadFromConds( $dbw, $conditions );
143 * Given a set of conditions, fetch a revision from
144 * the given database connection.
146 * @param Database $db
147 * @param array $conditions
152 function loadFromConds( &$db, $conditions ) {
153 $res = Revision
::fetchFromConds( $db, $conditions );
155 $row = $res->fetchObject();
158 $ret = new Revision( $row );
167 * Return a wrapper for a series of database rows to
168 * fetch all of a given page's revisions in turn.
169 * Each row can be fed to the constructor to get objects.
171 * @param Title $title
172 * @return ResultWrapper
176 function fetchAllRevisions( &$title ) {
177 return Revision
::fetchFromConds(
179 array( 'page_namespace' => $title->getNamespace(),
180 'page_title' => $title->getDbkey(),
181 'page_id=rev_page' ) );
185 * Return a wrapper for a series of database rows to
186 * fetch all of a given page's revisions in turn.
187 * Each row can be fed to the constructor to get objects.
189 * @param Title $title
190 * @return ResultWrapper
194 function fetchRevision( &$title ) {
195 return Revision
::fetchFromConds(
197 array( 'rev_id=page_latest',
198 'page_namespace' => $title->getNamespace(),
199 'page_title' => $title->getDbkey(),
200 'page_id=rev_page' ) );
204 * Given a set of conditions, return a ResultWrapper
205 * which will return matching database rows with the
206 * fields necessary to build Revision objects.
208 * @param Database $db
209 * @param array $conditions
210 * @return ResultWrapper
214 function fetchFromConds( &$db, $conditions ) {
216 array( 'page', 'revision' ),
217 array( 'page_namespace',
230 'Revision::fetchRow',
231 array( 'LIMIT' => 1 ) );
232 $ret = $db->resultObject( $res );
240 function Revision( $row ) {
241 if( is_object( $row ) ) {
242 $this->mId
= intval( $row->rev_id
);
243 $this->mPage
= intval( $row->rev_page
);
244 $this->mTextId
= intval( $row->rev_text_id
);
245 $this->mComment
= $row->rev_comment
;
246 $this->mUserText
= $row->rev_user_text
;
247 $this->mUser
= intval( $row->rev_user
);
248 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
249 $this->mTimestamp
= $row->rev_timestamp
;
250 $this->mDeleted
= intval( $row->rev_deleted
);
252 if( isset( $row->page_latest
) ) {
253 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
254 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
257 $this->mCurrent
= false;
258 $this->mTitle
= null;
261 if( isset( $row->old_text
) ) {
262 $this->mText
= $this->getRevisionText( $row );
266 } elseif( is_array( $row ) ) {
267 // Build a new revision to be saved...
270 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
271 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
272 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
273 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
274 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
275 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
276 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
277 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
279 // Enforce spacing trimming on supplied text
280 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
281 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
283 $this->mTitle
= null; # Load on demand if needed
284 $this->mCurrent
= false;
286 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
304 function getTextId() {
305 return $this->mTextId
;
309 * Returns the title of the page associated with this entry.
312 function getTitle() {
313 if( isset( $this->mTitle
) ) {
314 return $this->mTitle
;
316 $dbr =& wfGetDB( DB_SLAVE
);
317 $row = $dbr->selectRow(
318 array( 'page', 'revision' ),
319 array( 'page_namespace', 'page_title' ),
320 array( 'page_id=rev_page',
321 'rev_id' => $this->mId
),
322 'Revision::getTItle' );
324 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
327 return $this->mTitle
;
338 * Fetch revision's user id if it's available to all users
342 if( $this->isDeleted( MW_REV_DELETED_USER
) ) {
350 * Fetch revision's user id without regard for the current user's permissions
353 function getRawUser() {
358 * Fetch revision's username if it's available to all users
361 function getUserText() {
362 if( $this->isDeleted( MW_REV_DELETED_USER
) ) {
365 return $this->mUserText
;
370 * Fetch revision's username without regard for view restrictions
373 function getRawUserText() {
374 return $this->mUserText
;
378 * Fetch revision comment if it's available to all users
381 function getComment() {
382 if( $this->isDeleted( MW_REV_DELETED_COMMENT
) ) {
385 return $this->mComment
;
390 * Fetch revision comment without regard for the current user's permissions
393 function getRawComment() {
394 return $this->mComment
;
401 return (bool)$this->mMinorEdit
;
405 * int $field one of MW_REV_DELETED_* bitfield constants
408 function isDeleted( $field ) {
409 return ($this->mDeleted
& $field) == $field;
413 * Fetch revision text if it's available to all users
417 if( $this->isDeleted( MW_REV_DELETED_TEXT
) ) {
420 return $this->getRawText();
425 * Fetch revision text without regard for view restrictions
428 function getRawText() {
429 if( is_null( $this->mText
) ) {
430 // Revision text is immutable. Load on demand:
431 $this->mText
= $this->loadText();
439 function getTimestamp() {
440 return wfTimestamp(TS_MW
, $this->mTimestamp
);
446 function isCurrent() {
447 return $this->mCurrent
;
453 function getPrevious() {
454 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
456 return Revision
::newFromTitle( $this->mTitle
, $prev );
466 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
468 return Revision
::newFromTitle( $this->mTitle
, $next );
476 * Get revision text associated with an old or archive row
477 * $row is usually an object from wfFetchRow(), both the flags and the text
478 * field must be included
480 * @param integer $row Id of a row
481 * @param string $prefix table prefix (default 'old_')
482 * @return string $text|false the text requested
484 function getRevisionText( $row, $prefix = 'old_' ) {
485 $fname = 'Revision::getRevisionText';
486 wfProfileIn( $fname );
489 $textField = $prefix . 'text';
490 $flagsField = $prefix . 'flags';
492 if( isset( $row->$flagsField ) ) {
493 $flags = explode( ',', $row->$flagsField );
498 if( isset( $row->$textField ) ) {
499 $text = $row->$textField;
501 wfProfileOut( $fname );
505 # Use external methods for external objects, text in table is URL-only then
506 if ( in_array( 'external', $flags ) ) {
508 @list
($proto,$path)=explode('://',$url,2);
510 wfProfileOut( $fname );
513 $text=ExternalStore
::fetchFromURL($url);
516 // If the text was fetched without an error, convert it
517 if ( $text !== false ) {
518 if( in_array( 'gzip', $flags ) ) {
519 # Deal with optional compression of archived pages.
520 # This can be done periodically via maintenance/compressOld.php, and
521 # as pages are saved if $wgCompressRevisions is set.
522 $text = gzinflate( $text );
525 if( in_array( 'object', $flags ) ) {
526 # Generic compressed storage
527 $obj = unserialize( $text );
528 if ( !is_object( $obj ) ) {
530 wfProfileOut( $fname );
533 $text = $obj->getText();
536 global $wgLegacyEncoding;
537 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
538 # Old revisions kept around in a legacy encoding?
539 # Upconvert on demand.
540 global $wgInputEncoding, $wgContLang;
541 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
544 wfProfileOut( $fname );
549 * If $wgCompressRevisions is enabled, we will compress data.
550 * The input string is modified in place.
551 * Return value is the flags field: contains 'gzip' if the
552 * data is compressed, and 'utf-8' if we're saving in UTF-8
556 * @param mixed $text reference to a text
559 function compressRevisionText( &$text ) {
560 global $wgCompressRevisions;
563 # Revisions not marked this way will be converted
564 # on load if $wgLegacyCharset is set in the future.
567 if( $wgCompressRevisions ) {
568 if( function_exists( 'gzdeflate' ) ) {
569 $text = gzdeflate( $text );
572 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
575 return implode( ',', $flags );
579 * Insert a new revision into the database, returning the new revision ID
580 * number on success and dies horribly on failure.
582 * @param Database $dbw
585 function insertOn( &$dbw ) {
586 global $wgDefaultExternalStore;
588 $fname = 'Revision::insertOn';
589 wfProfileIn( $fname );
591 $data = $this->mText
;
592 $flags = Revision
::compressRevisionText( $data );
594 # Write to external storage if required
595 if ( $wgDefaultExternalStore ) {
596 if ( is_array( $wgDefaultExternalStore ) ) {
597 // Distribute storage across multiple clusters
598 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
600 $store = $wgDefaultExternalStore;
602 // Store and get the URL
603 $data = ExternalStore
::insert( $store, $data );
605 # This should only happen in the case of a configuration error, where the external store is not valid
606 wfDebugDieBacktrace( "Unable to store text to external storage $store" );
611 $flags .= 'external';
614 # Record the text (or external storage URL) to the text table
615 if( !isset( $this->mTextId
) ) {
616 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
617 $dbw->insert( 'text',
621 'old_flags' => $flags,
624 $this->mTextId
= $dbw->insertId();
627 # Record the edit in revisions
628 $rev_id = isset( $this->mId
)
630 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
631 $dbw->insert( 'revision',
634 'rev_page' => $this->mPage
,
635 'rev_text_id' => $this->mTextId
,
636 'rev_comment' => $this->mComment
,
637 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
638 'rev_user' => $this->mUser
,
639 'rev_user_text' => $this->mUserText
,
640 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
641 'rev_deleted' => $this->mDeleted
,
645 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
646 wfProfileOut( $fname );
651 * Lazy-load the revision's text.
652 * Currently hardcoded to the 'text' table storage engine.
657 function loadText() {
658 $fname = 'Revision::loadText';
659 wfProfileIn( $fname );
661 $dbr =& wfGetDB( DB_SLAVE
);
662 $row = $dbr->selectRow( 'text',
663 array( 'old_text', 'old_flags' ),
664 array( 'old_id' => $this->getTextId() ),
668 $dbw =& wfGetDB( DB_MASTER
);
669 $row = $dbw->selectRow( 'text',
670 array( 'old_text', 'old_flags' ),
671 array( 'old_id' => $this->getTextId() ),
675 $text = Revision
::getRevisionText( $row );
676 wfProfileOut( $fname );
682 * Create a new null-revision for insertion into a page's
683 * history. This will not re-save the text, but simply refer
684 * to the text from the previous version.
686 * Such revisions can for instance identify page rename
687 * operations and other such meta-modifications.
689 * @param Database $dbw
690 * @param int $pageId ID number of the page to read from
691 * @param string $summary
695 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
696 $fname = 'Revision::newNullRevision';
697 wfProfileIn( $fname );
699 $current = $dbw->selectRow(
700 array( 'page', 'revision' ),
701 array( 'page_latest', 'rev_text_id' ),
703 'page_id' => $pageId,
704 'page_latest=rev_id',
709 $revision = new Revision( array(
711 'comment' => $summary,
712 'minor_edit' => $minor,
713 'text_id' => $current->rev_text_id
,
719 wfProfileOut( $fname );
724 * Determine if the current user is allowed to view a particular
725 * field of this revision, if it's marked as deleted.
726 * @param int $field one of MW_REV_DELETED_TEXT,
727 * MW_REV_DELETED_COMMENT,
728 * MW_REV_DELETED_USER
731 function userCan( $field ) {
732 if( ( $this->mDeleted
& $field ) == $field ) {
734 $permission = ( $this->mDeleted
& MW_REV_DELETED_RESTRICTED
) == MW_REV_DELETED_RESTRICTED
737 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
738 return $wgUser->isAllowed( $permission );