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 $ret = Revision
::loadFromConds(
73 array( "rev_id=$matchId",
74 'rev_page' => intval( $pageid ),
75 'page_id=rev_page' ) );
80 * Load either the current, or a specified, revision
81 * that's attached to a given page. If not attached
82 * to that page, will return null.
90 function loadFromTitle( &$db, $title, $id = 0 ) {
92 $matchId = intval( $id );
94 $matchId = 'page_latest';
96 return Revision
::loadFromConds(
98 array( "rev_id=$matchId",
100 'page_namespace' => $title->getNamespace(),
101 'page_title' => $title->getDbkey() ) );
105 * Load the revision for the given title with the given timestamp.
106 * WARNING: Timestamps may in some circumstances not be unique,
107 * so this isn't the best key to use.
109 * @param Database $db
110 * @param Title $title
111 * @param string $timestamp
116 function loadFromTimestamp( &$db, &$title, $timestamp ) {
117 return Revision
::loadFromConds(
119 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
121 'page_namespace' => $title->getNamespace(),
122 'page_title' => $title->getDbkey() ) );
126 * Given a set of conditions, fetch a revision.
128 * @param array $conditions
133 function newFromConds( $conditions ) {
134 $db =& wfGetDB( DB_SLAVE
);
135 $row = Revision
::loadFromConds( $db, $conditions );
136 if( is_null( $row ) ) {
137 $dbw =& wfGetDB( DB_MASTER
);
138 $row = Revision
::loadFromConds( $dbw, $conditions );
144 * Given a set of conditions, fetch a revision from
145 * the given database connection.
147 * @param Database $db
148 * @param array $conditions
153 function loadFromConds( &$db, $conditions ) {
154 $res = Revision
::fetchFromConds( $db, $conditions );
156 $row = $res->fetchObject();
159 $ret = new Revision( $row );
168 * Return a wrapper for a series of database rows to
169 * fetch all of a given page's revisions in turn.
170 * Each row can be fed to the constructor to get objects.
172 * @param Title $title
173 * @return ResultWrapper
177 function fetchAllRevisions( &$title ) {
178 return Revision
::fetchFromConds(
180 array( 'page_namespace' => $title->getNamespace(),
181 'page_title' => $title->getDbkey(),
182 'page_id=rev_page' ) );
186 * Return a wrapper for a series of database rows to
187 * fetch all of a given page's revisions in turn.
188 * Each row can be fed to the constructor to get objects.
190 * @param Title $title
191 * @return ResultWrapper
195 function fetchRevision( &$title ) {
196 return Revision
::fetchFromConds(
198 array( 'rev_id=page_latest',
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDbkey(),
201 'page_id=rev_page' ) );
205 * Given a set of conditions, return a ResultWrapper
206 * which will return matching database rows with the
207 * fields necessary to build Revision objects.
209 * @param Database $db
210 * @param array $conditions
211 * @return ResultWrapper
215 function fetchFromConds( &$db, $conditions ) {
217 array( 'page', 'revision' ),
218 array( 'page_namespace',
231 'Revision::fetchRow',
232 array( 'LIMIT' => 1 ) );
233 $ret = $db->resultObject( $res );
241 function Revision( $row ) {
242 if( is_object( $row ) ) {
243 $this->mId
= intval( $row->rev_id
);
244 $this->mPage
= intval( $row->rev_page
);
245 $this->mTextId
= intval( $row->rev_text_id
);
246 $this->mComment
= $row->rev_comment
;
247 $this->mUserText
= $row->rev_user_text
;
248 $this->mUser
= intval( $row->rev_user
);
249 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
250 $this->mTimestamp
= $row->rev_timestamp
;
251 $this->mDeleted
= intval( $row->rev_deleted
);
253 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
254 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
257 if( isset( $row->old_text
) ) {
258 $this->mText
= $this->getRevisionText( $row );
262 } elseif( is_array( $row ) ) {
263 // Build a new revision to be saved...
266 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
267 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
268 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
269 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
270 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
271 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
272 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
273 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
275 // Enforce spacing trimming on supplied text
276 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
277 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
279 $this->mTitle
= null; # Load on demand if needed
280 $this->mCurrent
= false;
282 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
300 function getTextId() {
301 return $this->mTextId
;
305 * Returns the title of the page associated with this entry.
308 function getTitle() {
309 if( isset( $this->mTitle
) ) {
310 return $this->mTitle
;
312 $dbr =& wfGetDB( DB_SLAVE
);
313 $row = $dbr->selectRow(
314 array( 'page', 'revision' ),
315 array( 'page_namespace', 'page_title' ),
316 array( 'page_id=rev_page',
317 'rev_id' => $this->mId
),
318 'Revision::getTItle' );
320 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
323 return $this->mTitle
;
343 function getUserText() {
344 return $this->mUserText
;
350 function getComment() {
351 return $this->mComment
;
358 return (bool)$this->mMinorEdit
;
364 function isDeleted() {
365 return (bool)$this->mDeleted
;
372 if( is_null( $this->mText
) ) {
373 // Revision text is immutable. Load on demand:
374 $this->mText
= $this->loadText();
382 function getTimestamp() {
383 return wfTimestamp(TS_MW
, $this->mTimestamp
);
389 function isCurrent() {
390 return $this->mCurrent
;
396 function getPrevious() {
397 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
398 return Revision
::newFromTitle( $this->mTitle
, $prev );
405 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
406 return Revision
::newFromTitle( $this->mTitle
, $next );
411 * Get revision text associated with an old or archive row
412 * $row is usually an object from wfFetchRow(), both the flags and the text
413 * field must be included
415 * @param integer $row Id of a row
416 * @param string $prefix table prefix (default 'old_')
417 * @return string $text|false the text requested
419 function getRevisionText( $row, $prefix = 'old_' ) {
420 $fname = 'Revision::getRevisionText';
421 wfProfileIn( $fname );
424 $textField = $prefix . 'text';
425 $flagsField = $prefix . 'flags';
427 if( isset( $row->$flagsField ) ) {
428 $flags = explode( ',', $row->$flagsField );
433 if( isset( $row->$textField ) ) {
434 $text = $row->$textField;
436 wfProfileOut( $fname );
440 # Use external methods for external objects, text in table is URL-only then
441 if ( in_array( 'external', $flags ) ) {
443 @list
($proto,$path)=explode('://',$url,2);
445 wfProfileOut( $fname );
448 require_once('ExternalStore.php');
449 $text=ExternalStore
::fetchFromURL($url);
452 if( in_array( 'gzip', $flags ) ) {
453 # Deal with optional compression of archived pages.
454 # This can be done periodically via maintenance/compressOld.php, and
455 # as pages are saved if $wgCompressRevisions is set.
456 $text = gzinflate( $text );
459 if( in_array( 'object', $flags ) ) {
460 # Generic compressed storage
461 $obj = unserialize( $text );
463 # Bugger, corrupted my test database by double-serializing
464 if ( !is_object( $obj ) ) {
465 $obj = unserialize( $obj );
468 $text = $obj->getText();
471 global $wgLegacyEncoding;
472 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
473 # Old revisions kept around in a legacy encoding?
474 # Upconvert on demand.
475 global $wgInputEncoding, $wgContLang;
476 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
478 wfProfileOut( $fname );
483 * If $wgCompressRevisions is enabled, we will compress data.
484 * The input string is modified in place.
485 * Return value is the flags field: contains 'gzip' if the
486 * data is compressed, and 'utf-8' if we're saving in UTF-8
490 * @param mixed $text reference to a text
493 function compressRevisionText( &$text ) {
494 global $wgCompressRevisions;
497 # Revisions not marked this way will be converted
498 # on load if $wgLegacyCharset is set in the future.
501 if( $wgCompressRevisions ) {
502 if( function_exists( 'gzdeflate' ) ) {
503 $text = gzdeflate( $text );
506 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
509 return implode( ',', $flags );
513 * Insert a new revision into the database, returning the new revision ID
514 * number on success and dies horribly on failure.
516 * @param Database $dbw
519 function insertOn( &$dbw ) {
520 global $wgDefaultExternalStore;
522 $fname = 'Revision::insertOn';
523 wfProfileIn( $fname );
525 $data = $this->mText
;
526 $flags = Revision
::compressRevisionText( $data );
528 # Write to external storage if required
529 if ( $wgDefaultExternalStore ) {
530 require_once('ExternalStore.php');
531 // Store and get the URL
532 $data = ExternalStore
::insert( $wgDefaultExternalStore, $data );
534 # This should only happen in the case of a configuration error, where the external store is not valid
535 wfDebugDieBacktrace( "Unable to store text to external storage $wgDefaultExternalStore" );
540 $flags .= 'external';
543 # Record the text (or external storage URL) to the text table
544 if( !isset( $this->mTextId
) ) {
545 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
546 $dbw->insert( 'text',
550 'old_flags' => $flags,
553 $this->mTextId
= $dbw->insertId();
556 # Record the edit in revisions
557 $rev_id = isset( $this->mId
)
559 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
560 $dbw->insert( 'revision',
563 'rev_page' => $this->mPage
,
564 'rev_text_id' => $this->mTextId
,
565 'rev_comment' => $this->mComment
,
566 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
567 'rev_user' => $this->mUser
,
568 'rev_user_text' => $this->mUserText
,
569 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
570 'rev_deleted' => $this->mDeleted
,
574 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
576 wfProfileOut( $fname );
581 * Lazy-load the revision's text.
582 * Currently hardcoded to the 'text' table storage engine.
587 function loadText() {
588 $fname = 'Revision::loadText';
589 wfProfileIn( $fname );
591 $dbr =& wfGetDB( DB_SLAVE
);
592 $row = $dbr->selectRow( 'text',
593 array( 'old_text', 'old_flags' ),
594 array( 'old_id' => $this->getTextId() ),
598 $dbw =& wfGetDB( DB_MASTER
);
599 $row = $dbw->selectRow( 'text',
600 array( 'old_text', 'old_flags' ),
601 array( 'old_id' => $this->getTextId() ),
605 $text = Revision
::getRevisionText( $row );
606 wfProfileOut( $fname );
612 * Create a new null-revision for insertion into a page's
613 * history. This will not re-save the text, but simply refer
614 * to the text from the previous version.
616 * Such revisions can for instance identify page rename
617 * operations and other such meta-modifications.
619 * @param Database $dbw
620 * @param int $pageId ID number of the page to read from
621 * @param string $summary
625 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
626 $fname = 'Revision::newNullRevision';
627 wfProfileIn( $fname );
629 $current = $dbw->selectRow(
630 array( 'page', 'revision' ),
631 array( 'page_latest', 'rev_text_id' ),
633 'page_id' => $pageId,
634 'page_latest=rev_id',
639 $revision = new Revision( array(
641 'comment' => $summary,
642 'minor_edit' => $minor,
643 'text_id' => $current->rev_text_id
,
649 wfProfileOut( $fname );