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 the text was fetched without an error, convert it
453 if ( $text !== false ) {
454 if( in_array( 'gzip', $flags ) ) {
455 # Deal with optional compression of archived pages.
456 # This can be done periodically via maintenance/compressOld.php, and
457 # as pages are saved if $wgCompressRevisions is set.
458 $text = gzinflate( $text );
461 if( in_array( 'object', $flags ) ) {
462 # Generic compressed storage
463 $obj = unserialize( $text );
464 $text = $obj->getText();
467 global $wgLegacyEncoding;
468 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
469 # Old revisions kept around in a legacy encoding?
470 # Upconvert on demand.
471 global $wgInputEncoding, $wgContLang;
472 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
475 wfProfileOut( $fname );
480 * If $wgCompressRevisions is enabled, we will compress data.
481 * The input string is modified in place.
482 * Return value is the flags field: contains 'gzip' if the
483 * data is compressed, and 'utf-8' if we're saving in UTF-8
487 * @param mixed $text reference to a text
490 function compressRevisionText( &$text ) {
491 global $wgCompressRevisions;
494 # Revisions not marked this way will be converted
495 # on load if $wgLegacyCharset is set in the future.
498 if( $wgCompressRevisions ) {
499 if( function_exists( 'gzdeflate' ) ) {
500 $text = gzdeflate( $text );
503 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
506 return implode( ',', $flags );
510 * Insert a new revision into the database, returning the new revision ID
511 * number on success and dies horribly on failure.
513 * @param Database $dbw
516 function insertOn( &$dbw ) {
517 global $wgDefaultExternalStore;
519 $fname = 'Revision::insertOn';
520 wfProfileIn( $fname );
522 $data = $this->mText
;
523 $flags = Revision
::compressRevisionText( $data );
525 # Write to external storage if required
526 if ( $wgDefaultExternalStore ) {
527 require_once('ExternalStore.php');
528 // Store and get the URL
529 $data = ExternalStore
::insert( $wgDefaultExternalStore, $data );
531 # This should only happen in the case of a configuration error, where the external store is not valid
532 wfDebugDieBacktrace( "Unable to store text to external storage $wgDefaultExternalStore" );
537 $flags .= 'external';
540 # Record the text (or external storage URL) to the text table
541 if( !isset( $this->mTextId
) ) {
542 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
543 $dbw->insert( 'text',
547 'old_flags' => $flags,
550 $this->mTextId
= $dbw->insertId();
553 # Record the edit in revisions
554 $rev_id = isset( $this->mId
)
556 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
557 $dbw->insert( 'revision',
560 'rev_page' => $this->mPage
,
561 'rev_text_id' => $this->mTextId
,
562 'rev_comment' => $this->mComment
,
563 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
564 'rev_user' => $this->mUser
,
565 'rev_user_text' => $this->mUserText
,
566 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
567 'rev_deleted' => $this->mDeleted
,
571 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
573 wfProfileOut( $fname );
578 * Lazy-load the revision's text.
579 * Currently hardcoded to the 'text' table storage engine.
584 function loadText() {
585 $fname = 'Revision::loadText';
586 wfProfileIn( $fname );
588 $dbr =& wfGetDB( DB_SLAVE
);
589 $row = $dbr->selectRow( 'text',
590 array( 'old_text', 'old_flags' ),
591 array( 'old_id' => $this->getTextId() ),
595 $dbw =& wfGetDB( DB_MASTER
);
596 $row = $dbw->selectRow( 'text',
597 array( 'old_text', 'old_flags' ),
598 array( 'old_id' => $this->getTextId() ),
602 $text = Revision
::getRevisionText( $row );
603 wfProfileOut( $fname );
609 * Create a new null-revision for insertion into a page's
610 * history. This will not re-save the text, but simply refer
611 * to the text from the previous version.
613 * Such revisions can for instance identify page rename
614 * operations and other such meta-modifications.
616 * @param Database $dbw
617 * @param int $pageId ID number of the page to read from
618 * @param string $summary
622 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
623 $fname = 'Revision::newNullRevision';
624 wfProfileIn( $fname );
626 $current = $dbw->selectRow(
627 array( 'page', 'revision' ),
628 array( 'page_latest', 'rev_text_id' ),
630 'page_id' => $pageId,
631 'page_latest=rev_id',
636 $revision = new Revision( array(
638 'comment' => $summary,
639 'minor_edit' => $minor,
640 'text_id' => $current->rev_text_id
,
646 wfProfileOut( $fname );