8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
12 define( 'MW_REV_DELETED_TEXT', 1 );
13 define( 'MW_REV_DELETED_COMMENT', 2 );
14 define( 'MW_REV_DELETED_USER', 4 );
15 define( 'MW_REV_DELETED_RESTRICTED', 8 );
24 * Load a page revision from a given revision ID number.
25 * Returns null if no such revision can be found.
31 function newFromId( $id ) {
32 return Revision
::newFromConds(
33 array( 'page_id=rev_page',
34 'rev_id' => intval( $id ) ) );
38 * Load either the current, or a specified, revision
39 * that's attached to a given title. If not attached
40 * to that title, will return null.
48 function newFromTitle( &$title, $id = 0 ) {
50 $matchId = intval( $id );
52 $matchId = 'page_latest';
54 return Revision
::newFromConds(
55 array( "rev_id=$matchId",
57 'page_namespace' => $title->getNamespace(),
58 'page_title' => $title->getDbkey() ) );
62 * Load either the current, or a specified, revision
63 * that's attached to a given page. If not attached
64 * to that page, will return null.
72 function loadFromPageId( &$db, $pageid, $id = 0 ) {
73 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
75 $conds['rev_id']=intval($id);
77 $conds[]='rev_id=page_latest';
79 return Revision
::loadFromConds( $db, $conds );
83 * Load either the current, or a specified, revision
84 * that's attached to a given page. If not attached
85 * to that page, will return null.
93 function loadFromTitle( &$db, $title, $id = 0 ) {
95 $matchId = intval( $id );
97 $matchId = 'page_latest';
99 return Revision
::loadFromConds(
101 array( "rev_id=$matchId",
103 'page_namespace' => $title->getNamespace(),
104 'page_title' => $title->getDbkey() ) );
108 * Load the revision for the given title with the given timestamp.
109 * WARNING: Timestamps may in some circumstances not be unique,
110 * so this isn't the best key to use.
112 * @param Database $db
113 * @param Title $title
114 * @param string $timestamp
119 function loadFromTimestamp( &$db, &$title, $timestamp ) {
120 return Revision
::loadFromConds(
122 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
124 'page_namespace' => $title->getNamespace(),
125 'page_title' => $title->getDbkey() ) );
129 * Given a set of conditions, fetch a revision.
131 * @param array $conditions
136 function newFromConds( $conditions ) {
137 $db =& wfGetDB( DB_SLAVE
);
138 $row = Revision
::loadFromConds( $db, $conditions );
139 if( is_null( $row ) ) {
140 $dbw =& wfGetDB( DB_MASTER
);
141 $row = Revision
::loadFromConds( $dbw, $conditions );
147 * Given a set of conditions, fetch a revision from
148 * the given database connection.
150 * @param Database $db
151 * @param array $conditions
156 function loadFromConds( &$db, $conditions ) {
157 $res = Revision
::fetchFromConds( $db, $conditions );
159 $row = $res->fetchObject();
162 $ret = new Revision( $row );
171 * Return a wrapper for a series of database rows to
172 * fetch all of a given page's revisions in turn.
173 * Each row can be fed to the constructor to get objects.
175 * @param Title $title
176 * @return ResultWrapper
180 function fetchAllRevisions( &$title ) {
181 return Revision
::fetchFromConds(
183 array( 'page_namespace' => $title->getNamespace(),
184 'page_title' => $title->getDbkey(),
185 'page_id=rev_page' ) );
189 * Return a wrapper for a series of database rows to
190 * fetch all of a given page's revisions in turn.
191 * Each row can be fed to the constructor to get objects.
193 * @param Title $title
194 * @return ResultWrapper
198 function fetchRevision( &$title ) {
199 return Revision
::fetchFromConds(
201 array( 'rev_id=page_latest',
202 'page_namespace' => $title->getNamespace(),
203 'page_title' => $title->getDbkey(),
204 'page_id=rev_page' ) );
208 * Given a set of conditions, return a ResultWrapper
209 * which will return matching database rows with the
210 * fields necessary to build Revision objects.
212 * @param Database $db
213 * @param array $conditions
214 * @return ResultWrapper
218 function fetchFromConds( &$db, $conditions ) {
220 array( 'page', 'revision' ),
221 array( 'page_namespace',
234 'Revision::fetchRow',
235 array( 'LIMIT' => 1 ) );
236 $ret = $db->resultObject( $res );
244 function Revision( $row ) {
245 if( is_object( $row ) ) {
246 $this->mId
= intval( $row->rev_id
);
247 $this->mPage
= intval( $row->rev_page
);
248 $this->mTextId
= intval( $row->rev_text_id
);
249 $this->mComment
= $row->rev_comment
;
250 $this->mUserText
= $row->rev_user_text
;
251 $this->mUser
= intval( $row->rev_user
);
252 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
253 $this->mTimestamp
= $row->rev_timestamp
;
254 $this->mDeleted
= intval( $row->rev_deleted
);
256 if( isset( $row->page_latest
) ) {
257 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
258 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
261 $this->mCurrent
= false;
262 $this->mTitle
= null;
265 if( isset( $row->old_text
) ) {
266 $this->mText
= $this->getRevisionText( $row );
270 } elseif( is_array( $row ) ) {
271 // Build a new revision to be saved...
274 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
275 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
276 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
277 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
278 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
279 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
280 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
281 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
283 // Enforce spacing trimming on supplied text
284 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
285 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
287 $this->mTitle
= null; # Load on demand if needed
288 $this->mCurrent
= false;
290 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
308 function getTextId() {
309 return $this->mTextId
;
313 * Returns the title of the page associated with this entry.
316 function getTitle() {
317 if( isset( $this->mTitle
) ) {
318 return $this->mTitle
;
320 $dbr =& wfGetDB( DB_SLAVE
);
321 $row = $dbr->selectRow(
322 array( 'page', 'revision' ),
323 array( 'page_namespace', 'page_title' ),
324 array( 'page_id=rev_page',
325 'rev_id' => $this->mId
),
326 'Revision::getTItle' );
328 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
331 return $this->mTitle
;
342 * Fetch revision's user id if it's available to all users
346 if( $this->isDeleted( MW_REV_DELETED_USER
) ) {
354 * Fetch revision's user id without regard for the current user's permissions
357 function getRawUser() {
362 * Fetch revision's username if it's available to all users
365 function getUserText() {
366 if( $this->isDeleted( MW_REV_DELETED_USER
) ) {
369 return $this->mUserText
;
374 * Fetch revision's username without regard for view restrictions
377 function getRawUserText() {
378 return $this->mUserText
;
382 * Fetch revision comment if it's available to all users
385 function getComment() {
386 if( $this->isDeleted( MW_REV_DELETED_COMMENT
) ) {
389 return $this->mComment
;
394 * Fetch revision comment without regard for the current user's permissions
397 function getRawComment() {
398 return $this->mComment
;
405 return (bool)$this->mMinorEdit
;
409 * int $field one of MW_REV_DELETED_* bitfield constants
412 function isDeleted( $field ) {
413 return ($this->mDeleted
& $field) == $field;
417 * Fetch revision text if it's available to all users
421 if( $this->isDeleted( MW_REV_DELETED_TEXT
) ) {
424 return $this->getRawText();
429 * Fetch revision text without regard for view restrictions
432 function getRawText() {
433 if( is_null( $this->mText
) ) {
434 // Revision text is immutable. Load on demand:
435 $this->mText
= $this->loadText();
443 function getTimestamp() {
444 return wfTimestamp(TS_MW
, $this->mTimestamp
);
450 function isCurrent() {
451 return $this->mCurrent
;
457 function getPrevious() {
458 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
460 return Revision
::newFromTitle( $this->mTitle
, $prev );
470 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
472 return Revision
::newFromTitle( $this->mTitle
, $next );
480 * Get revision text associated with an old or archive row
481 * $row is usually an object from wfFetchRow(), both the flags and the text
482 * field must be included
484 * @param integer $row Id of a row
485 * @param string $prefix table prefix (default 'old_')
486 * @return string $text|false the text requested
488 function getRevisionText( $row, $prefix = 'old_' ) {
489 $fname = 'Revision::getRevisionText';
490 wfProfileIn( $fname );
493 $textField = $prefix . 'text';
494 $flagsField = $prefix . 'flags';
496 if( isset( $row->$flagsField ) ) {
497 $flags = explode( ',', $row->$flagsField );
502 if( isset( $row->$textField ) ) {
503 $text = $row->$textField;
505 wfProfileOut( $fname );
509 # Use external methods for external objects, text in table is URL-only then
510 if ( in_array( 'external', $flags ) ) {
512 @list
($proto,$path)=explode('://',$url,2);
514 wfProfileOut( $fname );
517 require_once('ExternalStore.php');
518 $text=ExternalStore
::fetchFromURL($url);
521 // If the text was fetched without an error, convert it
522 if ( $text !== false ) {
523 if( in_array( 'gzip', $flags ) ) {
524 # Deal with optional compression of archived pages.
525 # This can be done periodically via maintenance/compressOld.php, and
526 # as pages are saved if $wgCompressRevisions is set.
527 $text = gzinflate( $text );
530 if( in_array( 'object', $flags ) ) {
531 # Generic compressed storage
532 $obj = unserialize( $text );
533 if ( !is_object( $obj ) ) {
535 wfProfileOut( $fname );
538 $text = $obj->getText();
541 global $wgLegacyEncoding;
542 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
543 # Old revisions kept around in a legacy encoding?
544 # Upconvert on demand.
545 global $wgInputEncoding, $wgContLang;
546 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
549 wfProfileOut( $fname );
554 * If $wgCompressRevisions is enabled, we will compress data.
555 * The input string is modified in place.
556 * Return value is the flags field: contains 'gzip' if the
557 * data is compressed, and 'utf-8' if we're saving in UTF-8
561 * @param mixed $text reference to a text
564 function compressRevisionText( &$text ) {
565 global $wgCompressRevisions;
568 # Revisions not marked this way will be converted
569 # on load if $wgLegacyCharset is set in the future.
572 if( $wgCompressRevisions ) {
573 if( function_exists( 'gzdeflate' ) ) {
574 $text = gzdeflate( $text );
577 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
580 return implode( ',', $flags );
584 * Insert a new revision into the database, returning the new revision ID
585 * number on success and dies horribly on failure.
587 * @param Database $dbw
590 function insertOn( &$dbw ) {
591 global $wgDefaultExternalStore;
593 $fname = 'Revision::insertOn';
594 wfProfileIn( $fname );
596 $data = $this->mText
;
597 $flags = Revision
::compressRevisionText( $data );
599 # Write to external storage if required
600 if ( $wgDefaultExternalStore ) {
601 if ( is_array( $wgDefaultExternalStore ) ) {
602 // Distribute storage across multiple clusters
603 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
605 $store = $wgDefaultExternalStore;
607 require_once('ExternalStore.php');
608 // Store and get the URL
609 $data = ExternalStore
::insert( $store, $data );
611 # This should only happen in the case of a configuration error, where the external store is not valid
612 wfDebugDieBacktrace( "Unable to store text to external storage $store" );
617 $flags .= 'external';
620 # Record the text (or external storage URL) to the text table
621 if( !isset( $this->mTextId
) ) {
622 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
623 $dbw->insert( 'text',
627 'old_flags' => $flags,
630 $this->mTextId
= $dbw->insertId();
633 # Record the edit in revisions
634 $rev_id = isset( $this->mId
)
636 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
637 $dbw->insert( 'revision',
640 'rev_page' => $this->mPage
,
641 'rev_text_id' => $this->mTextId
,
642 'rev_comment' => $this->mComment
,
643 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
644 'rev_user' => $this->mUser
,
645 'rev_user_text' => $this->mUserText
,
646 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
647 'rev_deleted' => $this->mDeleted
,
651 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
652 wfProfileOut( $fname );
657 * Lazy-load the revision's text.
658 * Currently hardcoded to the 'text' table storage engine.
663 function loadText() {
664 $fname = 'Revision::loadText';
665 wfProfileIn( $fname );
667 $dbr =& wfGetDB( DB_SLAVE
);
668 $row = $dbr->selectRow( 'text',
669 array( 'old_text', 'old_flags' ),
670 array( 'old_id' => $this->getTextId() ),
674 $dbw =& wfGetDB( DB_MASTER
);
675 $row = $dbw->selectRow( 'text',
676 array( 'old_text', 'old_flags' ),
677 array( 'old_id' => $this->getTextId() ),
681 $text = Revision
::getRevisionText( $row );
682 wfProfileOut( $fname );
688 * Create a new null-revision for insertion into a page's
689 * history. This will not re-save the text, but simply refer
690 * to the text from the previous version.
692 * Such revisions can for instance identify page rename
693 * operations and other such meta-modifications.
695 * @param Database $dbw
696 * @param int $pageId ID number of the page to read from
697 * @param string $summary
701 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
702 $fname = 'Revision::newNullRevision';
703 wfProfileIn( $fname );
705 $current = $dbw->selectRow(
706 array( 'page', 'revision' ),
707 array( 'page_latest', 'rev_text_id' ),
709 'page_id' => $pageId,
710 'page_latest=rev_id',
715 $revision = new Revision( array(
717 'comment' => $summary,
718 'minor_edit' => $minor,
719 'text_id' => $current->rev_text_id
,
725 wfProfileOut( $fname );
730 * Determine if the current user is allowed to view a particular
731 * field of this revision, if it's marked as deleted.
732 * @param int $field one of MW_REV_DELETED_TEXT,
733 * MW_REV_DELETED_COMMENT,
734 * MW_REV_DELETED_USER
737 function userCan( $field ) {
738 if( ( $this->mDeleted
& $field ) == $field ) {
740 $permission = ( $this->mDeleted
& MW_REV_DELETED_RESTRICTED
) == MW_REV_DELETED_RESTRICTED
743 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
744 return $wgUser->isAllowed( $permission );