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 array( 'LIMIT' => 1 ) );
200 return $db->resultObject( $res );
207 function Revision( $row ) {
208 if( is_object( $row ) ) {
209 $this->mId
= IntVal( $row->rev_id
);
210 $this->mPage
= IntVal( $row->rev_page
);
211 $this->mTextId
= IntVal( $row->rev_text_id
);
212 $this->mComment
= $row->rev_comment
;
213 $this->mUserText
= $row->rev_user_text
;
214 $this->mUser
= IntVal( $row->rev_user
);
215 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
216 $this->mTimestamp
= $row->rev_timestamp
;
217 $this->mDeleted
= IntVal( $row->rev_deleted
);
219 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
220 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
223 if( isset( $row->old_text
) ) {
224 $this->mText
= $this->getRevisionText( $row );
228 } elseif( is_array( $row ) ) {
229 // Build a new revision to be saved...
232 $this->mId
= isset( $row['id'] ) ?
IntVal( $row['id'] ) : null;
233 $this->mPage
= isset( $row['page'] ) ?
IntVal( $row['page'] ) : null;
234 $this->mTextId
= isset( $row['text_id'] ) ?
IntVal( $row['text_id'] ) : null;
235 $this->mComment
= isset( $row['comment'] ) ?
StrVal( $row['comment'] ) : null;
236 $this->mUserText
= isset( $row['user_text'] ) ?
StrVal( $row['user_text'] ) : $wgUser->getName();
237 $this->mUser
= isset( $row['user'] ) ?
IntVal( $row['user'] ) : $wgUser->getId();
238 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
IntVal( $row['minor_edit'] ) : 0;
239 $this->mTimestamp
= isset( $row['timestamp'] ) ?
StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW
);
240 $this->mDeleted
= isset( $row['deleted'] ) ?
IntVal( $row['deleted'] ) : 0;
241 $this->mText
= isset( $row['text'] ) ?
StrVal( $row['text'] ) : null;
243 $this->mTitle
= null; # Load on demand if needed
244 $this->mCurrent
= false;
246 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
264 function getTextId() {
265 return $this->mTextId
;
269 * Returns the title of the page associated with this entry.
272 function &getTitle() {
273 if( isset( $this->mTitle
) ) {
274 return $this->mTitle
;
276 $dbr =& wfGetDB( DB_SLAVE
);
277 $row = $dbr->selectRow(
278 array( 'page', 'revision' ),
279 array( 'page_namespace', 'page_title' ),
280 array( 'page_id=rev_page',
281 'rev_id' => $this->mId
),
282 'Revision::getTItle' );
284 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
287 return $this->mTitle
;
307 function getUserText() {
308 return $this->mUserText
;
314 function getComment() {
315 return $this->mComment
;
322 return (bool)$this->mMinorEdit
;
328 function isDeleted() {
329 return (bool)$this->mDeleted
;
336 if( is_null( $this->mText
) ) {
337 // Revision text is immutable. Load on demand:
338 $this->mText
= $this->loadText();
346 function getTimestamp() {
347 return $this->mTimestamp
;
353 function isCurrent() {
354 return $this->mCurrent
;
360 function &getPrevious() {
361 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
362 return Revision
::newFromTitle( $this->mTitle
, $prev );
368 function &getNext() {
369 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
370 return Revision
::newFromTitle( $this->mTitle
, $next );
375 * Get revision text associated with an old or archive row
376 * $row is usually an object from wfFetchRow(), both the flags and the text
377 * field must be included
379 * @param integer $row Id of a row
380 * @param string $prefix table prefix (default 'old_')
381 * @return string $text|false the text requested
383 function getRevisionText( $row, $prefix = 'old_' ) {
384 $fname = 'Revision::getRevisionText';
385 wfProfileIn( $fname );
388 $textField = $prefix . 'text';
389 $flagsField = $prefix . 'flags';
391 if( isset( $row->$flagsField ) ) {
392 $flags = explode( ',', $row->$flagsField );
397 if( isset( $row->$textField ) ) {
398 $text = $row->$textField;
400 wfProfileOut( $fname );
404 # Use external methods for external objects, text in table is URL-only then
405 if ( in_array( 'external', $flags ) ) {
407 @list
($proto,$path)=explode('://',$url,2);
409 wfProfileOut( $fname );
412 require_once('ExternalStore.php');
413 $text=ExternalStore
::fetchFromURL($url);
416 if( in_array( 'gzip', $flags ) ) {
417 # Deal with optional compression of archived pages.
418 # This can be done periodically via maintenance/compressOld.php, and
419 # as pages are saved if $wgCompressRevisions is set.
420 $text = gzinflate( $text );
423 if( in_array( 'object', $flags ) ) {
424 # Generic compressed storage
425 $obj = unserialize( $text );
427 # Bugger, corrupted my test database by double-serializing
428 if ( !is_object( $obj ) ) {
429 $obj = unserialize( $obj );
432 $text = $obj->getText();
435 global $wgLegacyEncoding;
436 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
437 # Old revisions kept around in a legacy encoding?
438 # Upconvert on demand.
439 global $wgInputEncoding, $wgContLang;
440 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
442 wfProfileOut( $fname );
447 * If $wgCompressRevisions is enabled, we will compress data.
448 * The input string is modified in place.
449 * Return value is the flags field: contains 'gzip' if the
450 * data is compressed, and 'utf-8' if we're saving in UTF-8
454 * @param mixed $text reference to a text
457 function compressRevisionText( &$text ) {
458 global $wgCompressRevisions;
461 # Revisions not marked this way will be converted
462 # on load if $wgLegacyCharset is set in the future.
465 if( $wgCompressRevisions ) {
466 if( function_exists( 'gzdeflate' ) ) {
467 $text = gzdeflate( $text );
470 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
473 return implode( ',', $flags );
477 * Insert a new revision into the database, returning the new revision ID
478 * number on success and dies horribly on failure.
480 * @param Database $dbw
483 function insertOn( &$dbw ) {
484 $fname = 'Revision::insertOn';
485 wfProfileIn( $fname );
487 $mungedText = $this->mText
;
488 $flags = Revision
::compressRevisionText( $mungedText );
490 # Record the text to the text table
491 if( !isset( $this->mTextId
) ) {
492 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
493 $dbw->insert( 'text',
496 'old_text' => $mungedText,
497 'old_flags' => $flags,
500 $this->mTextId
= $dbw->insertId();
503 # Record the edit in revisions
504 $rev_id = isset( $this->mId
)
506 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
507 $dbw->insert( 'revision',
510 'rev_page' => $this->mPage
,
511 'rev_text_id' => $this->mTextId
,
512 'rev_comment' => $this->mComment
,
513 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
514 'rev_user' => $this->mUser
,
515 'rev_user_text' => $this->mUserText
,
516 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
517 'rev_deleted' => $this->mDeleted
,
521 $this->mId
= $dbw->insertId();
523 wfProfileOut( $fname );
528 * Lazy-load the revision's text.
529 * Currently hardcoded to the 'text' table storage engine.
534 function loadText() {
535 $fname = 'Revision::loadText';
536 wfProfileIn( $fname );
538 $dbr =& wfGetDB( DB_SLAVE
);
539 $row = $dbr->selectRow( 'text',
540 array( 'old_text', 'old_flags' ),
541 array( 'old_id' => $this->getTextId() ),
544 $text = Revision
::getRevisionText( $row );
545 wfProfileOut( $fname );
551 * Create a new null-revision for insertion into a page's
552 * history. This will not re-save the text, but simply refer
553 * to the text from the previous version.
555 * Such revisions can for instance identify page rename
556 * operations and other such meta-modifications.
558 * @param Database $dbw
559 * @param int $pageId ID number of the page to read from
560 * @param string $summary
564 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
565 $fname = 'Revision::newNullRevision';
566 wfProfileIn( $fname );
568 $current = $dbw->selectRow(
569 array( 'page', 'revision' ),
570 array( 'page_latest', 'rev_text_id' ),
572 'page_id' => $pageId,
573 'page_latest=rev_id',
578 $revision = new Revision( array(
580 'comment' => $summary,
581 'minor_edit' => $minor,
582 'text_id' => $current->rev_text_id
,
588 wfProfileOut( $fname );