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',
176 'Revision::fetchRow' );
177 return $db->resultObject( $res );
184 function Revision( $row ) {
185 if( is_object( $row ) ) {
186 $this->mId
= IntVal( $row->rev_id
);
187 $this->mPage
= IntVal( $row->rev_page
);
188 $this->mTextId
= IntVal( $row->rev_text_id
);
189 $this->mComment
= $row->rev_comment
;
190 $this->mUserText
= $row->rev_user_text
;
191 $this->mUser
= IntVal( $row->rev_user
);
192 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
193 $this->mTimestamp
= $row->rev_timestamp
;
194 $this->mDeleted
= IntVal( $row->rev_deleted
);
196 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
197 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
200 if( isset( $row->old_text
) ) {
201 $this->mText
= $this->getRevisionText( $row );
205 } elseif( is_array( $row ) ) {
206 // Build a new revision to be saved...
209 $this->mId
= isset( $row['id'] ) ?
IntVal( $row['id'] ) : null;
210 $this->mPage
= isset( $row['page'] ) ?
IntVal( $row['page'] ) : null;
211 $this->mTextId
= isset( $row['text_id'] ) ?
IntVal( $row['text_id'] ) : null;
212 $this->mComment
= isset( $row['comment'] ) ?
StrVal( $row['comment'] ) : null;
213 $this->mUserText
= isset( $row['user_text'] ) ?
StrVal( $row['user_text'] ) : $wgUser->getName();
214 $this->mUser
= isset( $row['user'] ) ?
IntVal( $row['user'] ) : $wgUser->getId();
215 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
IntVal( $row['minor_edit'] ) : 0;
216 $this->mTimestamp
= isset( $row['timestamp'] ) ?
StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW
);
217 $this->mDeleted
= isset( $row['deleted'] ) ?
IntVal( $row['deleted'] ) : 0;
218 $this->mText
= isset( $row['text'] ) ?
StrVal( $row['text'] ) : null;
220 $this->mTitle
= null; # Load on demand if needed
221 $this->mCurrent
= false;
223 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
241 function getTextId() {
242 return $this->mTextId
;
246 * Returns the title of the page associated with this entry.
249 function &getTitle() {
250 if( isset( $this->mTitle
) ) {
251 return $this->mTitle
;
253 $dbr =& wfGetDB( DB_SLAVE
);
254 $row = $dbr->selectRow(
255 array( 'page', 'revision' ),
256 array( 'page_namespace', 'page_title' ),
257 array( 'page_id=rev_page',
258 'rev_id' => $this->mId
),
259 'Revision::getTItle' );
261 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
264 return $this->mTitle
;
284 function getUserText() {
285 return $this->mUserText
;
291 function getComment() {
292 return $this->mComment
;
299 return (bool)$this->mMinorEdit
;
305 function isDeleted() {
306 return (bool)$this->mDeleted
;
313 if( is_null( $this->mText
) ) {
314 // Revision text is immutable. Load on demand:
315 $this->mText
= $this->loadText();
323 function getTimestamp() {
324 return $this->mTimestamp
;
330 function isCurrent() {
331 return $this->mCurrent
;
337 function &getPrevious() {
338 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
339 return Revision
::newFromTitle( $this->mTitle
, $prev );
345 function &getNext() {
346 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
347 return Revision
::newFromTitle( $this->mTitle
, $next );
352 * Get revision text associated with an old or archive row
353 * $row is usually an object from wfFetchRow(), both the flags and the text
354 * field must be included
356 * @param integer $row Id of a row
357 * @param string $prefix table prefix (default 'old_')
358 * @return string $text|false the text requested
360 function getRevisionText( $row, $prefix = 'old_' ) {
361 $fname = 'Revision::getRevisionText';
362 wfProfileIn( $fname );
365 $textField = $prefix . 'text';
366 $flagsField = $prefix . 'flags';
368 if( isset( $row->$flagsField ) ) {
369 $flags = explode( ',', $row->$flagsField );
374 if( isset( $row->$textField ) ) {
375 $text = $row->$textField;
377 wfProfileOut( $fname );
381 if( in_array( 'gzip', $flags ) ) {
382 # Deal with optional compression of archived pages.
383 # This can be done periodically via maintenance/compressOld.php, and
384 # as pages are saved if $wgCompressRevisions is set.
385 $text = gzinflate( $text );
388 if( in_array( 'object', $flags ) ) {
389 # Generic compressed storage
390 $obj = unserialize( $text );
392 # Bugger, corrupted my test database by double-serializing
393 if ( !is_object( $obj ) ) {
394 $obj = unserialize( $obj );
397 $text = $obj->getText();
400 global $wgLegacyEncoding;
401 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
402 # Old revisions kept around in a legacy encoding?
403 # Upconvert on demand.
404 global $wgInputEncoding, $wgContLang;
405 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
407 wfProfileOut( $fname );
412 * If $wgCompressRevisions is enabled, we will compress data.
413 * The input string is modified in place.
414 * Return value is the flags field: contains 'gzip' if the
415 * data is compressed, and 'utf-8' if we're saving in UTF-8
419 * @param mixed $text reference to a text
422 function compressRevisionText( &$text ) {
423 global $wgCompressRevisions;
426 # Revisions not marked this way will be converted
427 # on load if $wgLegacyCharset is set in the future.
430 if( $wgCompressRevisions ) {
431 if( function_exists( 'gzdeflate' ) ) {
432 $text = gzdeflate( $text );
435 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
438 return implode( ',', $flags );
442 * Insert a new revision into the database, returning the new revision ID
443 * number on success and dies horribly on failure.
445 * @param Database $dbw
448 function insertOn( &$dbw ) {
449 $fname = 'Revision::insertOn';
450 wfProfileIn( $fname );
452 $mungedText = $this->mText
;
453 $flags = Revision
::compressRevisionText( $mungedText );
455 # Record the text to the text table
456 if( !isset( $this->mTextId
) ) {
457 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
458 $dbw->insert( 'text',
461 'old_text' => $mungedText,
462 'old_flags' => $flags,
465 $this->mTextId
= $dbw->insertId();
468 # Record the edit in revisions
469 $rev_id = isset( $this->mId
)
471 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
472 $dbw->insert( 'revision',
475 'rev_page' => $this->mPage
,
476 'rev_text_id' => $this->mTextId
,
477 'rev_comment' => $this->mComment
,
478 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
479 'rev_user' => $this->mUser
,
480 'rev_user_text' => $this->mUserText
,
481 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
482 'rev_deleted' => $this->mDeleted
,
486 $this->mId
= $dbw->insertId();
488 wfProfileOut( $fname );
493 * Lazy-load the revision's text.
494 * Currently hardcoded to the 'text' table storage engine.
499 function loadText() {
500 $fname = 'Revision::loadText';
501 wfProfileIn( $fname );
503 $dbr =& wfGetDB( DB_SLAVE
);
504 $row = $dbr->selectRow( 'text',
505 array( 'old_text', 'old_flags' ),
506 array( 'old_id' => $this->getTextId() ),
509 $text = Revision
::getRevisionText( $row );
510 wfProfileOut( $fname );
516 * Create a new null-revision for insertion into a page's
517 * history. This will not re-save the text, but simply refer
518 * to the text from the previous version.
520 * Such revisions can for instance identify page rename
521 * operations and other such meta-modifications.
523 * @param Database $dbw
524 * @param int $pageId ID number of the page to read from
525 * @param string $summary
529 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
530 $fname = 'Revision::newNullRevision';
531 wfProfileIn( $fname );
533 $current = $dbw->selectRow(
534 array( 'page', 'revision' ),
535 array( 'page_latest', 'rev_text_id' ),
537 'page_id' => $pageId,
538 'page_latest=rev_id',
543 $revision = new Revision( array(
545 'comment' => $summary,
546 'minor_edit' => $minor,
547 'text_id' => $current->rev_text_id
,
553 wfProfileOut( $fname );