8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
16 const MW_REV_DELETED_TEXT
= 1;
17 const MW_REV_DELETED_COMMENT
= 2;
18 const MW_REV_DELETED_USER
= 4;
19 const MW_REV_DELETED_RESTRICTED
= 8;
22 * Load a page revision from a given revision ID number.
23 * Returns null if no such revision can be found.
29 function newFromId( $id ) {
30 return Revision
::newFromConds(
31 array( 'page_id=rev_page',
32 'rev_id' => intval( $id ) ) );
36 * Load either the current, or a specified, revision
37 * that's attached to a given title. If not attached
38 * to that title, will return null.
46 function newFromTitle( &$title, $id = 0 ) {
48 $matchId = intval( $id );
50 $matchId = 'page_latest';
52 return Revision
::newFromConds(
53 array( "rev_id=$matchId",
55 'page_namespace' => $title->getNamespace(),
56 'page_title' => $title->getDbkey() ) );
60 * Load either the current, or a specified, revision
61 * that's attached to a given page. If not attached
62 * to that page, will return null.
70 function loadFromPageId( &$db, $pageid, $id = 0 ) {
71 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
73 $conds['rev_id']=intval($id);
75 $conds[]='rev_id=page_latest';
77 return Revision
::loadFromConds( $db, $conds );
81 * Load either the current, or a specified, revision
82 * that's attached to a given page. If not attached
83 * to that page, will return null.
91 function loadFromTitle( &$db, $title, $id = 0 ) {
93 $matchId = intval( $id );
95 $matchId = 'page_latest';
97 return Revision
::loadFromConds(
99 array( "rev_id=$matchId",
101 'page_namespace' => $title->getNamespace(),
102 'page_title' => $title->getDbkey() ) );
106 * Load the revision for the given title with the given timestamp.
107 * WARNING: Timestamps may in some circumstances not be unique,
108 * so this isn't the best key to use.
110 * @param Database $db
111 * @param Title $title
112 * @param string $timestamp
117 function loadFromTimestamp( &$db, &$title, $timestamp ) {
118 return Revision
::loadFromConds(
120 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
122 'page_namespace' => $title->getNamespace(),
123 'page_title' => $title->getDbkey() ) );
127 * Given a set of conditions, fetch a revision.
129 * @param array $conditions
134 function newFromConds( $conditions ) {
135 $db =& wfGetDB( DB_SLAVE
);
136 $row = Revision
::loadFromConds( $db, $conditions );
137 if( is_null( $row ) ) {
138 $dbw =& wfGetDB( DB_MASTER
);
139 $row = Revision
::loadFromConds( $dbw, $conditions );
145 * Given a set of conditions, fetch a revision from
146 * the given database connection.
148 * @param Database $db
149 * @param array $conditions
154 function loadFromConds( &$db, $conditions ) {
155 $res = Revision
::fetchFromConds( $db, $conditions );
157 $row = $res->fetchObject();
160 $ret = new Revision( $row );
169 * Return a wrapper for a series of database rows to
170 * fetch all of a given page's revisions in turn.
171 * Each row can be fed to the constructor to get objects.
173 * @param Title $title
174 * @return ResultWrapper
178 function fetchAllRevisions( &$title ) {
179 return Revision
::fetchFromConds(
181 array( 'page_namespace' => $title->getNamespace(),
182 'page_title' => $title->getDbkey(),
183 'page_id=rev_page' ) );
187 * Return a wrapper for a series of database rows to
188 * fetch all of a given page's revisions in turn.
189 * Each row can be fed to the constructor to get objects.
191 * @param Title $title
192 * @return ResultWrapper
196 function fetchRevision( &$title ) {
197 return Revision
::fetchFromConds(
199 array( 'rev_id=page_latest',
200 'page_namespace' => $title->getNamespace(),
201 'page_title' => $title->getDbkey(),
202 'page_id=rev_page' ) );
206 * Given a set of conditions, return a ResultWrapper
207 * which will return matching database rows with the
208 * fields necessary to build Revision objects.
210 * @param Database $db
211 * @param array $conditions
212 * @return ResultWrapper
216 function fetchFromConds( &$db, $conditions ) {
218 array( 'page', 'revision' ),
219 array( 'page_namespace',
232 'Revision::fetchRow',
233 array( 'LIMIT' => 1 ) );
234 $ret = $db->resultObject( $res );
242 function Revision( $row ) {
243 if( is_object( $row ) ) {
244 $this->mId
= intval( $row->rev_id
);
245 $this->mPage
= intval( $row->rev_page
);
246 $this->mTextId
= intval( $row->rev_text_id
);
247 $this->mComment
= $row->rev_comment
;
248 $this->mUserText
= $row->rev_user_text
;
249 $this->mUser
= intval( $row->rev_user
);
250 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
251 $this->mTimestamp
= $row->rev_timestamp
;
252 $this->mDeleted
= intval( $row->rev_deleted
);
254 if( isset( $row->page_latest
) ) {
255 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
256 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
259 $this->mCurrent
= false;
260 $this->mTitle
= null;
263 if( isset( $row->old_text
) ) {
264 $this->mText
= $this->getRevisionText( $row );
268 } elseif( is_array( $row ) ) {
269 // Build a new revision to be saved...
272 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
273 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
274 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
275 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
276 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
277 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
278 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
279 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
281 // Enforce spacing trimming on supplied text
282 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
283 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
285 $this->mTitle
= null; # Load on demand if needed
286 $this->mCurrent
= false;
288 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
306 function getTextId() {
307 return $this->mTextId
;
311 * Returns the title of the page associated with this entry.
314 function getTitle() {
315 if( isset( $this->mTitle
) ) {
316 return $this->mTitle
;
318 $dbr =& wfGetDB( DB_SLAVE
);
319 $row = $dbr->selectRow(
320 array( 'page', 'revision' ),
321 array( 'page_namespace', 'page_title' ),
322 array( 'page_id=rev_page',
323 'rev_id' => $this->mId
),
324 'Revision::getTItle' );
326 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
329 return $this->mTitle
;
340 * Fetch revision's user id if it's available to all users
344 if( $this->isDeleted( self
::MW_REV_DELETED_USER
) ) {
352 * Fetch revision's user id without regard for the current user's permissions
355 function getRawUser() {
360 * Fetch revision's username if it's available to all users
363 function getUserText() {
364 if( $this->isDeleted( self
::MW_REV_DELETED_USER
) ) {
367 return $this->mUserText
;
372 * Fetch revision's username without regard for view restrictions
375 function getRawUserText() {
376 return $this->mUserText
;
380 * Fetch revision comment if it's available to all users
383 function getComment() {
384 if( $this->isDeleted( self
::MW_REV_DELETED_COMMENT
) ) {
387 return $this->mComment
;
392 * Fetch revision comment without regard for the current user's permissions
395 function getRawComment() {
396 return $this->mComment
;
403 return (bool)$this->mMinorEdit
;
407 * int $field one of MW_REV_DELETED_* bitfield constants
410 function isDeleted( $field ) {
411 return ($this->mDeleted
& $field) == $field;
415 * Fetch revision text if it's available to all users
419 if( $this->isDeleted( self
::MW_REV_DELETED_TEXT
) ) {
422 return $this->getRawText();
427 * Fetch revision text without regard for view restrictions
430 function getRawText() {
431 if( is_null( $this->mText
) ) {
432 // Revision text is immutable. Load on demand:
433 $this->mText
= $this->loadText();
441 function getTimestamp() {
442 return wfTimestamp(TS_MW
, $this->mTimestamp
);
448 function isCurrent() {
449 return $this->mCurrent
;
455 function getPrevious() {
456 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
458 return Revision
::newFromTitle( $this->mTitle
, $prev );
468 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
470 return Revision
::newFromTitle( $this->mTitle
, $next );
478 * Get revision text associated with an old or archive row
479 * $row is usually an object from wfFetchRow(), both the flags and the text
480 * field must be included
482 * @param integer $row Id of a row
483 * @param string $prefix table prefix (default 'old_')
484 * @return string $text|false the text requested
486 function getRevisionText( $row, $prefix = 'old_' ) {
487 $fname = 'Revision::getRevisionText';
488 wfProfileIn( $fname );
491 $textField = $prefix . 'text';
492 $flagsField = $prefix . 'flags';
494 if( isset( $row->$flagsField ) ) {
495 $flags = explode( ',', $row->$flagsField );
500 if( isset( $row->$textField ) ) {
501 $text = $row->$textField;
503 wfProfileOut( $fname );
507 # Use external methods for external objects, text in table is URL-only then
508 if ( in_array( 'external', $flags ) ) {
510 @list
($proto,$path)=explode('://',$url,2);
512 wfProfileOut( $fname );
515 require_once('ExternalStore.php');
516 $text=ExternalStore
::fetchFromURL($url);
519 // If the text was fetched without an error, convert it
520 if ( $text !== false ) {
521 if( in_array( 'gzip', $flags ) ) {
522 # Deal with optional compression of archived pages.
523 # This can be done periodically via maintenance/compressOld.php, and
524 # as pages are saved if $wgCompressRevisions is set.
525 $text = gzinflate( $text );
528 if( in_array( 'object', $flags ) ) {
529 # Generic compressed storage
530 $obj = unserialize( $text );
531 if ( !is_object( $obj ) ) {
533 wfProfileOut( $fname );
536 $text = $obj->getText();
539 global $wgLegacyEncoding;
540 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
541 # Old revisions kept around in a legacy encoding?
542 # Upconvert on demand.
543 global $wgInputEncoding, $wgContLang;
544 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
547 wfProfileOut( $fname );
552 * If $wgCompressRevisions is enabled, we will compress data.
553 * The input string is modified in place.
554 * Return value is the flags field: contains 'gzip' if the
555 * data is compressed, and 'utf-8' if we're saving in UTF-8
559 * @param mixed $text reference to a text
562 function compressRevisionText( &$text ) {
563 global $wgCompressRevisions;
566 # Revisions not marked this way will be converted
567 # on load if $wgLegacyCharset is set in the future.
570 if( $wgCompressRevisions ) {
571 if( function_exists( 'gzdeflate' ) ) {
572 $text = gzdeflate( $text );
575 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
578 return implode( ',', $flags );
582 * Insert a new revision into the database, returning the new revision ID
583 * number on success and dies horribly on failure.
585 * @param Database $dbw
588 function insertOn( &$dbw ) {
589 global $wgDefaultExternalStore;
591 $fname = 'Revision::insertOn';
592 wfProfileIn( $fname );
594 $data = $this->mText
;
595 $flags = Revision
::compressRevisionText( $data );
597 # Write to external storage if required
598 if ( $wgDefaultExternalStore ) {
599 if ( is_array( $wgDefaultExternalStore ) ) {
600 // Distribute storage across multiple clusters
601 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
603 $store = $wgDefaultExternalStore;
605 require_once('ExternalStore.php');
606 // Store and get the URL
607 $data = ExternalStore
::insert( $store, $data );
609 # This should only happen in the case of a configuration error, where the external store is not valid
610 wfDebugDieBacktrace( "Unable to store text to external storage $store" );
615 $flags .= 'external';
618 # Record the text (or external storage URL) to the text table
619 if( !isset( $this->mTextId
) ) {
620 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
621 $dbw->insert( 'text',
625 'old_flags' => $flags,
628 $this->mTextId
= $dbw->insertId();
631 # Record the edit in revisions
632 $rev_id = isset( $this->mId
)
634 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
635 $dbw->insert( 'revision',
638 'rev_page' => $this->mPage
,
639 'rev_text_id' => $this->mTextId
,
640 'rev_comment' => $this->mComment
,
641 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
642 'rev_user' => $this->mUser
,
643 'rev_user_text' => $this->mUserText
,
644 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
645 'rev_deleted' => $this->mDeleted
,
649 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
650 wfProfileOut( $fname );
655 * Lazy-load the revision's text.
656 * Currently hardcoded to the 'text' table storage engine.
661 function loadText() {
662 $fname = 'Revision::loadText';
663 wfProfileIn( $fname );
665 $dbr =& wfGetDB( DB_SLAVE
);
666 $row = $dbr->selectRow( 'text',
667 array( 'old_text', 'old_flags' ),
668 array( 'old_id' => $this->getTextId() ),
672 $dbw =& wfGetDB( DB_MASTER
);
673 $row = $dbw->selectRow( 'text',
674 array( 'old_text', 'old_flags' ),
675 array( 'old_id' => $this->getTextId() ),
679 $text = Revision
::getRevisionText( $row );
680 wfProfileOut( $fname );
686 * Create a new null-revision for insertion into a page's
687 * history. This will not re-save the text, but simply refer
688 * to the text from the previous version.
690 * Such revisions can for instance identify page rename
691 * operations and other such meta-modifications.
693 * @param Database $dbw
694 * @param int $pageId ID number of the page to read from
695 * @param string $summary
699 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
700 $fname = 'Revision::newNullRevision';
701 wfProfileIn( $fname );
703 $current = $dbw->selectRow(
704 array( 'page', 'revision' ),
705 array( 'page_latest', 'rev_text_id' ),
707 'page_id' => $pageId,
708 'page_latest=rev_id',
713 $revision = new Revision( array(
715 'comment' => $summary,
716 'minor_edit' => $minor,
717 'text_id' => $current->rev_text_id
,
723 wfProfileOut( $fname );
728 * Determine if the current user is allowed to view a particular
729 * field of this revision, if it's marked as deleted.
730 * @param int $field one of self::MW_REV_DELETED_TEXT,
731 * self::MW_REV_DELETED_COMMENT,
732 * self::MW_REV_DELETED_USER
735 function userCan( $field ) {
736 if( ( $this->mDeleted
& $field ) == $field ) {
738 $permission = ( $this->mDeleted
& self
::MW_REV_DELETED_RESTRICTED
) == self
::MW_REV_DELETED_RESTRICTED
741 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
742 return $wgUser->isAllowed( $permission );