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
);
399 return Revision
::newFromTitle( $this->mTitle
, $prev );
409 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
411 return Revision
::newFromTitle( $this->mTitle
, $next );
419 * Get revision text associated with an old or archive row
420 * $row is usually an object from wfFetchRow(), both the flags and the text
421 * field must be included
423 * @param integer $row Id of a row
424 * @param string $prefix table prefix (default 'old_')
425 * @return string $text|false the text requested
427 function getRevisionText( $row, $prefix = 'old_' ) {
428 $fname = 'Revision::getRevisionText';
429 wfProfileIn( $fname );
432 $textField = $prefix . 'text';
433 $flagsField = $prefix . 'flags';
435 if( isset( $row->$flagsField ) ) {
436 $flags = explode( ',', $row->$flagsField );
441 if( isset( $row->$textField ) ) {
442 $text = $row->$textField;
444 wfProfileOut( $fname );
448 # Use external methods for external objects, text in table is URL-only then
449 if ( in_array( 'external', $flags ) ) {
451 @list
($proto,$path)=explode('://',$url,2);
453 wfProfileOut( $fname );
456 require_once('ExternalStore.php');
457 $text=ExternalStore
::fetchFromURL($url);
460 // If the text was fetched without an error, convert it
461 if ( $text !== false ) {
462 if( in_array( 'gzip', $flags ) ) {
463 # Deal with optional compression of archived pages.
464 # This can be done periodically via maintenance/compressOld.php, and
465 # as pages are saved if $wgCompressRevisions is set.
466 $text = gzinflate( $text );
469 if( in_array( 'object', $flags ) ) {
470 # Generic compressed storage
471 $obj = unserialize( $text );
472 $text = $obj->getText();
475 global $wgLegacyEncoding;
476 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
477 # Old revisions kept around in a legacy encoding?
478 # Upconvert on demand.
479 global $wgInputEncoding, $wgContLang;
480 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
483 wfProfileOut( $fname );
488 * If $wgCompressRevisions is enabled, we will compress data.
489 * The input string is modified in place.
490 * Return value is the flags field: contains 'gzip' if the
491 * data is compressed, and 'utf-8' if we're saving in UTF-8
495 * @param mixed $text reference to a text
498 function compressRevisionText( &$text ) {
499 global $wgCompressRevisions;
502 # Revisions not marked this way will be converted
503 # on load if $wgLegacyCharset is set in the future.
506 if( $wgCompressRevisions ) {
507 if( function_exists( 'gzdeflate' ) ) {
508 $text = gzdeflate( $text );
511 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
514 return implode( ',', $flags );
518 * Insert a new revision into the database, returning the new revision ID
519 * number on success and dies horribly on failure.
521 * @param Database $dbw
524 function insertOn( &$dbw ) {
525 global $wgDefaultExternalStore;
527 $fname = 'Revision::insertOn';
528 wfProfileIn( $fname );
530 $data = $this->mText
;
531 $flags = Revision
::compressRevisionText( $data );
533 # Write to external storage if required
534 if ( $wgDefaultExternalStore ) {
535 require_once('ExternalStore.php');
536 // Store and get the URL
537 $data = ExternalStore
::insert( $wgDefaultExternalStore, $data );
539 # This should only happen in the case of a configuration error, where the external store is not valid
540 wfDebugDieBacktrace( "Unable to store text to external storage $wgDefaultExternalStore" );
545 $flags .= 'external';
548 # Record the text (or external storage URL) to the text table
549 if( !isset( $this->mTextId
) ) {
550 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
551 $dbw->insert( 'text',
555 'old_flags' => $flags,
558 $this->mTextId
= $dbw->insertId();
561 # Record the edit in revisions
562 $rev_id = isset( $this->mId
)
564 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
565 $dbw->insert( 'revision',
568 'rev_page' => $this->mPage
,
569 'rev_text_id' => $this->mTextId
,
570 'rev_comment' => $this->mComment
,
571 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
572 'rev_user' => $this->mUser
,
573 'rev_user_text' => $this->mUserText
,
574 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
575 'rev_deleted' => $this->mDeleted
,
579 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
581 wfProfileOut( $fname );
586 * Lazy-load the revision's text.
587 * Currently hardcoded to the 'text' table storage engine.
592 function loadText() {
593 $fname = 'Revision::loadText';
594 wfProfileIn( $fname );
596 $dbr =& wfGetDB( DB_SLAVE
);
597 $row = $dbr->selectRow( 'text',
598 array( 'old_text', 'old_flags' ),
599 array( 'old_id' => $this->getTextId() ),
603 $dbw =& wfGetDB( DB_MASTER
);
604 $row = $dbw->selectRow( 'text',
605 array( 'old_text', 'old_flags' ),
606 array( 'old_id' => $this->getTextId() ),
610 $text = Revision
::getRevisionText( $row );
611 wfProfileOut( $fname );
617 * Create a new null-revision for insertion into a page's
618 * history. This will not re-save the text, but simply refer
619 * to the text from the previous version.
621 * Such revisions can for instance identify page rename
622 * operations and other such meta-modifications.
624 * @param Database $dbw
625 * @param int $pageId ID number of the page to read from
626 * @param string $summary
630 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
631 $fname = 'Revision::newNullRevision';
632 wfProfileIn( $fname );
634 $current = $dbw->selectRow(
635 array( 'page', 'revision' ),
636 array( 'page_latest', 'rev_text_id' ),
638 'page_id' => $pageId,
639 'page_latest=rev_id',
644 $revision = new Revision( array(
646 'comment' => $summary,
647 'minor_edit' => $minor,
648 'text_id' => $current->rev_text_id
,
654 wfProfileOut( $fname );