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.
40 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 either the current, or a specified, revision
55 * that's attached to a given page. If not attached
56 * to that page, will return null.
64 function &loadFromPageId( &$db, $pageid, $id = 0 ) {
66 $matchId = IntVal( $id );
68 $matchId = 'page_latest';
70 return Revision
::loadFromConds(
72 array( "rev_id=$matchId",
73 'rev_page' => IntVal( $pageid ),
74 'page_id=rev_page' ) );
78 * Given a set of conditions, fetch a revision.
80 * @param array $conditions
85 function &newFromConds( $conditions ) {
86 $db =& wfGetDB( DB_SLAVE
);
87 return Revision
::loadFromConds( $db, $conditions );
91 * Given a set of conditions, fetch a revision from
92 * the given database connection.
95 * @param array $conditions
100 function &loadFromConds( &$db, $conditions ) {
101 $res =& Revision
::fetchFromConds( $db, $conditions );
103 $row = $res->fetchObject();
106 return new Revision( $row );
113 * Return a wrapper for a series of database rows to
114 * fetch all of a given page's revisions in turn.
115 * Each row can be fed to the constructor to get objects.
117 * @param Title $title
118 * @return ResultWrapper
122 function &fetchAllRevisions( &$title ) {
123 return Revision
::fetchFromConds(
125 array( 'page_namespace' => $title->getNamespace(),
126 'page_title' => $title->getDbkey(),
127 'page_id=rev_page' ) );
131 * Return a wrapper for a series of database rows to
132 * fetch all of a given page's revisions in turn.
133 * Each row can be fed to the constructor to get objects.
135 * @param Title $title
136 * @return ResultWrapper
140 function &fetchRevision( &$title ) {
141 return Revision
::fetchFromConds(
143 array( 'rev_id=page_latest',
144 'page_namespace' => $title->getNamespace(),
145 'page_title' => $title->getDbkey(),
146 'page_id=rev_page' ) );
150 * Given a set of conditions, return a ResultWrapper
151 * which will return matching database rows with the
152 * fields necessary to build Revision objects.
154 * @param Database $db
155 * @param array $conditions
156 * @return ResultWrapper
160 function &fetchFromConds( &$db, $conditions ) {
162 array( 'page', 'revision' ),
163 array( 'page_namespace',
175 'Revision::fetchRow' );
176 return $db->resultObject( $res );
183 function Revision( $row ) {
184 if( is_object( $row ) ) {
185 $this->mId
= IntVal( $row->rev_id
);
186 $this->mPage
= IntVal( $row->rev_page
);
187 $this->mTextId
= IntVal( $row->rev_text_id
);
188 $this->mComment
= $row->rev_comment
;
189 $this->mUserText
= $row->rev_user_text
;
190 $this->mUser
= IntVal( $row->rev_user
);
191 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
192 $this->mTimestamp
= $row->rev_timestamp
;
194 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
195 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
198 if( isset( $row->old_text
) ) {
199 $this->mText
= $this->getRevisionText( $row );
203 } elseif( is_array( $row ) ) {
204 // Build a new revision to be saved...
207 $this->mId
= isset( $row['id'] ) ?
IntVal( $row['id'] ) : null;
208 $this->mPage
= isset( $row['page'] ) ?
IntVal( $row['page'] ) : null;
209 $this->mTextId
= isset( $row['text_id'] ) ?
IntVal( $row['text_id'] ) : null;
210 $this->mComment
= isset( $row['comment'] ) ?
StrVal( $row['comment'] ) : null;
211 $this->mUserText
= isset( $row['user_text'] ) ?
StrVal( $row['user_text'] ) : $wgUser->getName();
212 $this->mUser
= isset( $row['user'] ) ?
IntVal( $row['user'] ) : $wgUser->getId();
213 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
IntVal( $row['minor_edit'] ) : 0;
214 $this->mTimestamp
= isset( $row['timestamp'] ) ?
StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW
);
215 $this->mText
= isset( $row['text'] ) ?
StrVal( $row['text'] ) : null;
217 $this->mTitle
= null; # Load on demand if needed
218 $this->mCurrent
= false;
220 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
238 function getTextId() {
239 return $this->mTextId
;
243 * Returns the title of the page associated with this entry.
246 function &getTitle() {
247 if( isset( $this->mTitle
) ) {
248 return $this->mTitle
;
250 $dbr =& wfGetDB( DB_SLAVE
);
251 $row = $dbr->selectRow(
252 array( 'page', 'revision' ),
253 array( 'page_namespace', 'page_title' ),
254 array( 'page_id=rev_page',
255 'rev_id' => $this->mId
),
256 'Revision::getTItle' );
258 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
261 return $this->mTitle
;
281 function getUserText() {
282 return $this->mUserText
;
288 function getComment() {
289 return $this->mComment
;
296 return (bool)$this->mMinorEdit
;
303 if( is_null( $this->mText
) ) {
304 // Revision text is immutable. Load on demand:
305 $this->mText
= $this->loadText();
313 function getTimestamp() {
314 return $this->mTimestamp
;
320 function isCurrent() {
321 return $this->mCurrent
;
327 function &getPrevious() {
328 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
329 return Revision
::newFromTitle( $this->mTitle
, $prev );
335 function &getNext() {
336 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
337 return Revision
::newFromTitle( $this->mTitle
, $next );
342 * Get revision text associated with an old or archive row
343 * $row is usually an object from wfFetchRow(), both the flags and the text
344 * field must be included
346 * @param integer $row Id of a row
347 * @param string $prefix table prefix (default 'old_')
348 * @return string $text|false the text requested
350 function getRevisionText( $row, $prefix = 'old_' ) {
351 $fname = 'Revision::getRevisionText';
352 wfProfileIn( $fname );
355 $textField = $prefix . 'text';
356 $flagsField = $prefix . 'flags';
358 if( isset( $row->$flagsField ) ) {
359 $flags = explode( ',', $row->$flagsField );
364 if( isset( $row->$textField ) ) {
365 $text = $row->$textField;
367 wfProfileOut( $fname );
371 if( in_array( 'gzip', $flags ) ) {
372 # Deal with optional compression of archived pages.
373 # This can be done periodically via maintenance/compressOld.php, and
374 # as pages are saved if $wgCompressRevisions is set.
375 $text = gzinflate( $text );
378 if( in_array( 'object', $flags ) ) {
379 # Generic compressed storage
380 $obj = unserialize( $text );
382 # Bugger, corrupted my test database by double-serializing
383 if ( !is_object( $obj ) ) {
384 $obj = unserialize( $obj );
387 $text = $obj->getText();
390 global $wgLegacyEncoding;
391 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
392 # Old revisions kept around in a legacy encoding?
393 # Upconvert on demand.
394 global $wgInputEncoding, $wgContLang;
395 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
397 wfProfileOut( $fname );
402 * If $wgCompressRevisions is enabled, we will compress data.
403 * The input string is modified in place.
404 * Return value is the flags field: contains 'gzip' if the
405 * data is compressed, and 'utf-8' if we're saving in UTF-8
409 * @param mixed $text reference to a text
412 function compressRevisionText( &$text ) {
413 global $wgCompressRevisions;
416 # Revisions not marked this way will be converted
417 # on load if $wgLegacyCharset is set in the future.
420 if( $wgCompressRevisions ) {
421 if( function_exists( 'gzdeflate' ) ) {
422 $text = gzdeflate( $text );
425 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
428 return implode( ',', $flags );
432 * Insert a new revision into the database, returning the new revision ID
433 * number on success and dies horribly on failure.
435 * @param Database $dbw
438 function insertOn( &$dbw ) {
439 $fname = 'Revision::insertOn';
440 wfProfileIn( $fname );
442 $mungedText = $this->mText
;
443 $flags = Revision
::compressRevisionText( $mungedText );
445 # Record the text to the text table
446 if( !isset( $this->mTextId
) ) {
447 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
448 $dbw->insert( 'text',
451 'old_text' => $mungedText,
452 'old_flags' => $flags,
455 $this->mTextId
= $dbw->insertId();
458 # Record the edit in revisions
459 $rev_id = isset( $this->mId
)
461 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
462 $dbw->insert( 'revision',
465 'rev_page' => $this->mPage
,
466 'rev_text_id' => $this->mTextId
,
467 'rev_comment' => $this->mComment
,
468 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
469 'rev_user' => $this->mUser
,
470 'rev_user_text' => $this->mUserText
,
471 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
475 $this->mId
= $dbw->insertId();
477 wfProfileOut( $fname );
482 * Lazy-load the revision's text.
483 * Currently hardcoded to the 'text' table storage engine.
488 function loadText() {
489 $fname = 'Revision::loadText';
490 wfProfileIn( $fname );
492 $dbr =& wfGetDB( DB_SLAVE
);
493 $row = $dbr->selectRow( 'text',
494 array( 'old_text', 'old_flags' ),
495 array( 'old_id' => $this->getTextId() ),
498 $text = Revision
::getRevisionText( $row );
499 wfProfileOut( $fname );
505 * Create a new null-revision for insertion into a page's
506 * history. This will not re-save the text, but simply refer
507 * to the text from the previous version.
509 * Such revisions can for instance identify page rename
510 * operations and other such meta-modifications.
512 * @param Database $dbw
513 * @param int $pageId ID number of the page to read from
514 * @param string $summary
518 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
519 $fname = 'Revision::newNullRevision';
520 wfProfileIn( $fname );
522 $current = $dbw->selectRow(
523 array( 'page', 'revision' ),
524 array( 'page_latest', 'rev_text_id' ),
526 'page_id' => $pageId,
527 'page_latest=rev_id',
532 $revision = new Revision( array(
534 'comment' => $summary,
535 'minor_edit' => $minor,
536 'text_id' => $current->rev_text_id
,
542 wfProfileOut( $fname );