8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
17 * Load a page revision from a given revision ID number.
18 * Returns null if no such revision can be found.
24 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 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 either the current, or a specified, revision
56 * that's attached to a given page. If not attached
57 * to that page, will return null.
65 function &loadFromPageId( &$db, $pageid, $id = 0 ) {
67 $matchId = IntVal( $id );
69 $matchId = 'page_latest';
71 return Revision
::loadFromConds(
73 array( "rev_id=$matchId",
74 'rev_page' => IntVal( $pageid ),
75 'page_id=rev_page' ) );
79 * Load the revision for the given title with the given timestamp.
80 * WARNING: Timestamps may in some circumstances not be unique,
81 * so this isn't the best key to use.
85 * @param string $timestamp
90 function &loadFromTimestamp( &$db, &$title, $timestamp ) {
91 return Revision
::loadFromConds(
93 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
95 'page_namespace' => $title->getNamespace(),
96 'page_title' => $title->getDbkey() ) );
100 * Given a set of conditions, fetch a revision.
102 * @param array $conditions
107 function &newFromConds( $conditions ) {
108 $db =& wfGetDB( DB_SLAVE
);
109 return Revision
::loadFromConds( $db, $conditions );
113 * Given a set of conditions, fetch a revision from
114 * the given database connection.
116 * @param Database $db
117 * @param array $conditions
122 function &loadFromConds( &$db, $conditions ) {
123 $res =& Revision
::fetchFromConds( $db, $conditions );
125 $row = $res->fetchObject();
128 return new Revision( $row );
135 * Return a wrapper for a series of database rows to
136 * fetch all of a given page's revisions in turn.
137 * Each row can be fed to the constructor to get objects.
139 * @param Title $title
140 * @return ResultWrapper
144 function &fetchAllRevisions( &$title ) {
145 return Revision
::fetchFromConds(
147 array( 'page_namespace' => $title->getNamespace(),
148 'page_title' => $title->getDbkey(),
149 'page_id=rev_page' ) );
153 * Return a wrapper for a series of database rows to
154 * fetch all of a given page's revisions in turn.
155 * Each row can be fed to the constructor to get objects.
157 * @param Title $title
158 * @return ResultWrapper
162 function &fetchRevision( &$title ) {
163 return Revision
::fetchFromConds(
165 array( 'rev_id=page_latest',
166 'page_namespace' => $title->getNamespace(),
167 'page_title' => $title->getDbkey(),
168 'page_id=rev_page' ) );
172 * Given a set of conditions, return a ResultWrapper
173 * which will return matching database rows with the
174 * fields necessary to build Revision objects.
176 * @param Database $db
177 * @param array $conditions
178 * @return ResultWrapper
182 function &fetchFromConds( &$db, $conditions ) {
184 array( 'page', 'revision' ),
185 array( 'page_namespace',
198 'Revision::fetchRow' );
199 return $db->resultObject( $res );
206 function Revision( $row ) {
207 if( is_object( $row ) ) {
208 $this->mId
= IntVal( $row->rev_id
);
209 $this->mPage
= IntVal( $row->rev_page
);
210 $this->mTextId
= IntVal( $row->rev_text_id
);
211 $this->mComment
= $row->rev_comment
;
212 $this->mUserText
= $row->rev_user_text
;
213 $this->mUser
= IntVal( $row->rev_user
);
214 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
215 $this->mTimestamp
= $row->rev_timestamp
;
216 $this->mDeleted
= IntVal( $row->rev_deleted
);
218 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
219 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
222 if( isset( $row->old_text
) ) {
223 $this->mText
= $this->getRevisionText( $row );
227 } elseif( is_array( $row ) ) {
228 // Build a new revision to be saved...
231 $this->mId
= isset( $row['id'] ) ?
IntVal( $row['id'] ) : null;
232 $this->mPage
= isset( $row['page'] ) ?
IntVal( $row['page'] ) : null;
233 $this->mTextId
= isset( $row['text_id'] ) ?
IntVal( $row['text_id'] ) : null;
234 $this->mComment
= isset( $row['comment'] ) ?
StrVal( $row['comment'] ) : null;
235 $this->mUserText
= isset( $row['user_text'] ) ?
StrVal( $row['user_text'] ) : $wgUser->getName();
236 $this->mUser
= isset( $row['user'] ) ?
IntVal( $row['user'] ) : $wgUser->getId();
237 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
IntVal( $row['minor_edit'] ) : 0;
238 $this->mTimestamp
= isset( $row['timestamp'] ) ?
StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW
);
239 $this->mDeleted
= isset( $row['deleted'] ) ?
IntVal( $row['deleted'] ) : 0;
240 $this->mText
= isset( $row['text'] ) ?
StrVal( $row['text'] ) : null;
242 $this->mTitle
= null; # Load on demand if needed
243 $this->mCurrent
= false;
245 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
263 function getTextId() {
264 return $this->mTextId
;
268 * Returns the title of the page associated with this entry.
271 function &getTitle() {
272 if( isset( $this->mTitle
) ) {
273 return $this->mTitle
;
275 $dbr =& wfGetDB( DB_SLAVE
);
276 $row = $dbr->selectRow(
277 array( 'page', 'revision' ),
278 array( 'page_namespace', 'page_title' ),
279 array( 'page_id=rev_page',
280 'rev_id' => $this->mId
),
281 'Revision::getTItle' );
283 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
286 return $this->mTitle
;
306 function getUserText() {
307 return $this->mUserText
;
313 function getComment() {
314 return $this->mComment
;
321 return (bool)$this->mMinorEdit
;
327 function isDeleted() {
328 return (bool)$this->mDeleted
;
335 if( is_null( $this->mText
) ) {
336 // Revision text is immutable. Load on demand:
337 $this->mText
= $this->loadText();
345 function getTimestamp() {
346 return $this->mTimestamp
;
352 function isCurrent() {
353 return $this->mCurrent
;
359 function &getPrevious() {
360 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
361 return Revision
::newFromTitle( $this->mTitle
, $prev );
367 function &getNext() {
368 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
369 return Revision
::newFromTitle( $this->mTitle
, $next );
374 * Get revision text associated with an old or archive row
375 * $row is usually an object from wfFetchRow(), both the flags and the text
376 * field must be included
378 * @param integer $row Id of a row
379 * @param string $prefix table prefix (default 'old_')
380 * @return string $text|false the text requested
382 function getRevisionText( $row, $prefix = 'old_' ) {
383 $fname = 'Revision::getRevisionText';
384 wfProfileIn( $fname );
387 $textField = $prefix . 'text';
388 $flagsField = $prefix . 'flags';
390 if( isset( $row->$flagsField ) ) {
391 $flags = explode( ',', $row->$flagsField );
396 if( isset( $row->$textField ) ) {
397 $text = $row->$textField;
399 wfProfileOut( $fname );
403 # Use external methods for external objects, text in table is URL-only then
404 if ( in_array( 'external', $flags ) ) {
406 @list
($proto,$path)=explode('://',$url,2);
408 wfProfileOut( $fname );
411 require_once('ExternalStore.php');
412 $text=ExternalStore
::fetchFromURL($url);
415 if( in_array( 'gzip', $flags ) ) {
416 # Deal with optional compression of archived pages.
417 # This can be done periodically via maintenance/compressOld.php, and
418 # as pages are saved if $wgCompressRevisions is set.
419 $text = gzinflate( $text );
422 if( in_array( 'object', $flags ) ) {
423 # Generic compressed storage
424 $obj = unserialize( $text );
426 # Bugger, corrupted my test database by double-serializing
427 if ( !is_object( $obj ) ) {
428 $obj = unserialize( $obj );
431 $text = $obj->getText();
434 global $wgLegacyEncoding;
435 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
436 # Old revisions kept around in a legacy encoding?
437 # Upconvert on demand.
438 global $wgInputEncoding, $wgContLang;
439 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
441 wfProfileOut( $fname );
446 * If $wgCompressRevisions is enabled, we will compress data.
447 * The input string is modified in place.
448 * Return value is the flags field: contains 'gzip' if the
449 * data is compressed, and 'utf-8' if we're saving in UTF-8
453 * @param mixed $text reference to a text
456 function compressRevisionText( &$text ) {
457 global $wgCompressRevisions;
460 # Revisions not marked this way will be converted
461 # on load if $wgLegacyCharset is set in the future.
464 if( $wgCompressRevisions ) {
465 if( function_exists( 'gzdeflate' ) ) {
466 $text = gzdeflate( $text );
469 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
472 return implode( ',', $flags );
476 * Insert a new revision into the database, returning the new revision ID
477 * number on success and dies horribly on failure.
479 * @param Database $dbw
482 function insertOn( &$dbw ) {
483 $fname = 'Revision::insertOn';
484 wfProfileIn( $fname );
486 $mungedText = $this->mText
;
487 $flags = Revision
::compressRevisionText( $mungedText );
489 # Record the text to the text table
490 if( !isset( $this->mTextId
) ) {
491 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
492 $dbw->insert( 'text',
495 'old_text' => $mungedText,
496 'old_flags' => $flags,
499 $this->mTextId
= $dbw->insertId();
502 # Record the edit in revisions
503 $rev_id = isset( $this->mId
)
505 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
506 $dbw->insert( 'revision',
509 'rev_page' => $this->mPage
,
510 'rev_text_id' => $this->mTextId
,
511 'rev_comment' => $this->mComment
,
512 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
513 'rev_user' => $this->mUser
,
514 'rev_user_text' => $this->mUserText
,
515 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
516 'rev_deleted' => $this->mDeleted
,
520 $this->mId
= $dbw->insertId();
522 wfProfileOut( $fname );
527 * Lazy-load the revision's text.
528 * Currently hardcoded to the 'text' table storage engine.
533 function loadText() {
534 $fname = 'Revision::loadText';
535 wfProfileIn( $fname );
537 $dbr =& wfGetDB( DB_SLAVE
);
538 $row = $dbr->selectRow( 'text',
539 array( 'old_text', 'old_flags' ),
540 array( 'old_id' => $this->getTextId() ),
543 $text = Revision
::getRevisionText( $row );
544 wfProfileOut( $fname );
550 * Create a new null-revision for insertion into a page's
551 * history. This will not re-save the text, but simply refer
552 * to the text from the previous version.
554 * Such revisions can for instance identify page rename
555 * operations and other such meta-modifications.
557 * @param Database $dbw
558 * @param int $pageId ID number of the page to read from
559 * @param string $summary
563 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
564 $fname = 'Revision::newNullRevision';
565 wfProfileIn( $fname );
567 $current = $dbw->selectRow(
568 array( 'page', 'revision' ),
569 array( 'page_latest', 'rev_text_id' ),
571 'page_id' => $pageId,
572 'page_latest=rev_id',
577 $revision = new Revision( array(
579 'comment' => $summary,
580 'minor_edit' => $minor,
581 'text_id' => $current->rev_text_id
,
587 wfProfileOut( $fname );