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 $row = Revision
::loadFromConds( $db, $conditions );
110 if( is_null( $row ) ) {
111 $dbw =& wfGetDB( DB_MASTER
);
112 $row = Revision
::loadFromConds( $dbw, $conditions );
118 * Given a set of conditions, fetch a revision from
119 * the given database connection.
121 * @param Database $db
122 * @param array $conditions
127 function &loadFromConds( &$db, $conditions ) {
128 $res =& Revision
::fetchFromConds( $db, $conditions );
130 $row = $res->fetchObject();
133 return new Revision( $row );
140 * Return a wrapper for a series of database rows to
141 * fetch all of a given page's revisions in turn.
142 * Each row can be fed to the constructor to get objects.
144 * @param Title $title
145 * @return ResultWrapper
149 function &fetchAllRevisions( &$title ) {
150 return Revision
::fetchFromConds(
152 array( 'page_namespace' => $title->getNamespace(),
153 'page_title' => $title->getDbkey(),
154 'page_id=rev_page' ) );
158 * Return a wrapper for a series of database rows to
159 * fetch all of a given page's revisions in turn.
160 * Each row can be fed to the constructor to get objects.
162 * @param Title $title
163 * @return ResultWrapper
167 function &fetchRevision( &$title ) {
168 return Revision
::fetchFromConds(
170 array( 'rev_id=page_latest',
171 'page_namespace' => $title->getNamespace(),
172 'page_title' => $title->getDbkey(),
173 'page_id=rev_page' ) );
177 * Given a set of conditions, return a ResultWrapper
178 * which will return matching database rows with the
179 * fields necessary to build Revision objects.
181 * @param Database $db
182 * @param array $conditions
183 * @return ResultWrapper
187 function &fetchFromConds( &$db, $conditions ) {
189 array( 'page', 'revision' ),
190 array( 'page_namespace',
203 'Revision::fetchRow',
204 array( 'LIMIT' => 1 ) );
205 return $db->resultObject( $res );
212 function Revision( $row ) {
213 if( is_object( $row ) ) {
214 $this->mId
= IntVal( $row->rev_id
);
215 $this->mPage
= IntVal( $row->rev_page
);
216 $this->mTextId
= IntVal( $row->rev_text_id
);
217 $this->mComment
= $row->rev_comment
;
218 $this->mUserText
= $row->rev_user_text
;
219 $this->mUser
= IntVal( $row->rev_user
);
220 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
221 $this->mTimestamp
= $row->rev_timestamp
;
222 $this->mDeleted
= IntVal( $row->rev_deleted
);
224 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
225 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
228 if( isset( $row->old_text
) ) {
229 $this->mText
= $this->getRevisionText( $row );
233 } elseif( is_array( $row ) ) {
234 // Build a new revision to be saved...
237 $this->mId
= isset( $row['id'] ) ?
IntVal( $row['id'] ) : null;
238 $this->mPage
= isset( $row['page'] ) ?
IntVal( $row['page'] ) : null;
239 $this->mTextId
= isset( $row['text_id'] ) ?
IntVal( $row['text_id'] ) : null;
240 $this->mUserText
= isset( $row['user_text'] ) ?
StrVal( $row['user_text'] ) : $wgUser->getName();
241 $this->mUser
= isset( $row['user'] ) ?
IntVal( $row['user'] ) : $wgUser->getId();
242 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
IntVal( $row['minor_edit'] ) : 0;
243 $this->mTimestamp
= isset( $row['timestamp'] ) ?
StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW
);
244 $this->mDeleted
= isset( $row['deleted'] ) ?
IntVal( $row['deleted'] ) : 0;
246 // Enforce spacing trimming on supplied text
247 $this->mComment
= isset( $row['comment'] ) ?
trim( StrVal( $row['comment'] ) ) : null;
248 $this->mText
= isset( $row['text'] ) ?
rtrim( StrVal( $row['text'] ) ) : null;
250 $this->mTitle
= null; # Load on demand if needed
251 $this->mCurrent
= false;
253 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
271 function getTextId() {
272 return $this->mTextId
;
276 * Returns the title of the page associated with this entry.
279 function &getTitle() {
280 if( isset( $this->mTitle
) ) {
281 return $this->mTitle
;
283 $dbr =& wfGetDB( DB_SLAVE
);
284 $row = $dbr->selectRow(
285 array( 'page', 'revision' ),
286 array( 'page_namespace', 'page_title' ),
287 array( 'page_id=rev_page',
288 'rev_id' => $this->mId
),
289 'Revision::getTItle' );
291 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
294 return $this->mTitle
;
314 function getUserText() {
315 return $this->mUserText
;
321 function getComment() {
322 return $this->mComment
;
329 return (bool)$this->mMinorEdit
;
335 function isDeleted() {
336 return (bool)$this->mDeleted
;
343 if( is_null( $this->mText
) ) {
344 // Revision text is immutable. Load on demand:
345 $this->mText
= $this->loadText();
353 function getTimestamp() {
354 return $this->mTimestamp
;
360 function isCurrent() {
361 return $this->mCurrent
;
367 function &getPrevious() {
368 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
369 return Revision
::newFromTitle( $this->mTitle
, $prev );
375 function &getNext() {
376 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
377 return Revision
::newFromTitle( $this->mTitle
, $next );
382 * Get revision text associated with an old or archive row
383 * $row is usually an object from wfFetchRow(), both the flags and the text
384 * field must be included
386 * @param integer $row Id of a row
387 * @param string $prefix table prefix (default 'old_')
388 * @return string $text|false the text requested
390 function getRevisionText( $row, $prefix = 'old_' ) {
391 $fname = 'Revision::getRevisionText';
392 wfProfileIn( $fname );
395 $textField = $prefix . 'text';
396 $flagsField = $prefix . 'flags';
398 if( isset( $row->$flagsField ) ) {
399 $flags = explode( ',', $row->$flagsField );
404 if( isset( $row->$textField ) ) {
405 $text = $row->$textField;
407 wfProfileOut( $fname );
411 # Use external methods for external objects, text in table is URL-only then
412 if ( in_array( 'external', $flags ) ) {
414 @list
($proto,$path)=explode('://',$url,2);
416 wfProfileOut( $fname );
419 require_once('ExternalStore.php');
420 $text=ExternalStore
::fetchFromURL($url);
423 if( in_array( 'gzip', $flags ) ) {
424 # Deal with optional compression of archived pages.
425 # This can be done periodically via maintenance/compressOld.php, and
426 # as pages are saved if $wgCompressRevisions is set.
427 $text = gzinflate( $text );
430 if( in_array( 'object', $flags ) ) {
431 # Generic compressed storage
432 $obj = unserialize( $text );
434 # Bugger, corrupted my test database by double-serializing
435 if ( !is_object( $obj ) ) {
436 $obj = unserialize( $obj );
439 $text = $obj->getText();
442 global $wgLegacyEncoding;
443 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
444 # Old revisions kept around in a legacy encoding?
445 # Upconvert on demand.
446 global $wgInputEncoding, $wgContLang;
447 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
449 wfProfileOut( $fname );
454 * If $wgCompressRevisions is enabled, we will compress data.
455 * The input string is modified in place.
456 * Return value is the flags field: contains 'gzip' if the
457 * data is compressed, and 'utf-8' if we're saving in UTF-8
461 * @param mixed $text reference to a text
464 function compressRevisionText( &$text ) {
465 global $wgCompressRevisions;
468 # Revisions not marked this way will be converted
469 # on load if $wgLegacyCharset is set in the future.
472 if( $wgCompressRevisions ) {
473 if( function_exists( 'gzdeflate' ) ) {
474 $text = gzdeflate( $text );
477 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
480 return implode( ',', $flags );
484 * Insert a new revision into the database, returning the new revision ID
485 * number on success and dies horribly on failure.
487 * @param Database $dbw
490 function insertOn( &$dbw ) {
491 $fname = 'Revision::insertOn';
492 wfProfileIn( $fname );
494 $mungedText = $this->mText
;
495 $flags = Revision
::compressRevisionText( $mungedText );
497 # Record the text to the text table
498 if( !isset( $this->mTextId
) ) {
499 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
500 $dbw->insert( 'text',
503 'old_text' => $mungedText,
504 'old_flags' => $flags,
507 $this->mTextId
= $dbw->insertId();
510 # Record the edit in revisions
511 $rev_id = isset( $this->mId
)
513 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
514 $dbw->insert( 'revision',
517 'rev_page' => $this->mPage
,
518 'rev_text_id' => $this->mTextId
,
519 'rev_comment' => $this->mComment
,
520 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
521 'rev_user' => $this->mUser
,
522 'rev_user_text' => $this->mUserText
,
523 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
524 'rev_deleted' => $this->mDeleted
,
528 $this->mId
= $dbw->insertId();
530 wfProfileOut( $fname );
535 * Lazy-load the revision's text.
536 * Currently hardcoded to the 'text' table storage engine.
541 function loadText() {
542 $fname = 'Revision::loadText';
543 wfProfileIn( $fname );
545 $dbr =& wfGetDB( DB_SLAVE
);
546 $row = $dbr->selectRow( 'text',
547 array( 'old_text', 'old_flags' ),
548 array( 'old_id' => $this->getTextId() ),
552 $dbw =& wfGetDB( DB_MASTER
);
553 $row = $dbw->selectRow( 'text',
554 array( 'old_text', 'old_flags' ),
555 array( 'old_id' => $this->getTextId() ),
559 $text = Revision
::getRevisionText( $row );
560 wfProfileOut( $fname );
566 * Create a new null-revision for insertion into a page's
567 * history. This will not re-save the text, but simply refer
568 * to the text from the previous version.
570 * Such revisions can for instance identify page rename
571 * operations and other such meta-modifications.
573 * @param Database $dbw
574 * @param int $pageId ID number of the page to read from
575 * @param string $summary
579 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
580 $fname = 'Revision::newNullRevision';
581 wfProfileIn( $fname );
583 $current = $dbw->selectRow(
584 array( 'page', 'revision' ),
585 array( 'page_latest', 'rev_text_id' ),
587 'page_id' => $pageId,
588 'page_latest=rev_id',
593 $revision = new Revision( array(
595 'comment' => $summary,
596 'minor_edit' => $minor,
597 'text_id' => $current->rev_text_id
,
603 wfProfileOut( $fname );