3d9cedd437944d4ff650e7f59384692efa3ff947
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.
40 function &newFromTitle( &$title, $id = 0 ) {
42 $matchId = IntVal( $id );
44 $matchId = 'page_latest';
46 return Revision
::newFromConds(
47 array( "rev_id=$matchId",
49 'page_namespace' => $title->getNamespace(),
50 'page_title' => $title->getDbkey() ) );
54 * Load either the current, or a specified, revision
55 * that's attached to a given page. If not attached
56 * to that page, will return null.
64 function &loadFromPageId( &$db, $pageid, $id = 0 ) {
66 $matchId = IntVal( $id );
68 $matchId = 'page_latest';
70 return Revision
::loadFromConds(
72 array( "rev_id=$matchId",
73 'rev_page' => IntVal( $pageid ),
74 'page_id=rev_page' ) );
78 * Given a set of conditions, fetch a revision.
80 * @param array $conditions
85 function &newFromConds( $conditions ) {
86 $db =& wfGetDB( DB_SLAVE
);
87 return Revision
::loadFromConds( $db, $conditions );
91 * Given a set of conditions, fetch a revision from
92 * the given database connection.
95 * @param array $conditions
100 function &loadFromConds( &$db, $conditions ) {
101 $res =& Revision
::fetchFromConds( $db, $conditions );
103 $row = $res->fetchObject();
106 return new Revision( $row );
113 * Return a wrapper for a series of database rows to
114 * fetch all of a given page's revisions in turn.
115 * Each row can be fed to the constructor to get objects.
117 * @param Title $title
118 * @return ResultWrapper
122 function &fetchAllRevisions( &$title ) {
123 return Revision
::fetchFromConds(
125 array( 'page_namespace' => $title->getNamespace(),
126 'page_title' => $title->getDbkey(),
127 'page_id=rev_page' ) );
131 * Return a wrapper for a series of database rows to
132 * fetch all of a given page's revisions in turn.
133 * Each row can be fed to the constructor to get objects.
135 * @param Title $title
136 * @return ResultWrapper
140 function &fetchRevision( &$title ) {
141 return Revision
::fetchFromConds(
143 array( 'rev_id=page_latest',
144 'page_namespace' => $title->getNamespace(),
145 'page_title' => $title->getDbkey(),
146 'page_id=rev_page' ) );
150 * Given a set of conditions, return a ResultWrapper
151 * which will return matching database rows with the
152 * fields necessary to build Revision objects.
154 * @param Database $db
155 * @param array $conditions
156 * @return ResultWrapper
160 function &fetchFromConds( &$db, $conditions ) {
162 array( 'page', 'revision' ),
163 array( 'page_namespace',
174 'Revision::fetchRow' );
175 return $db->resultObject( $res );
182 function Revision( $row ) {
183 if( is_object( $row ) ) {
184 $this->mId
= IntVal( $row->rev_id
);
185 $this->mPage
= IntVal( $row->rev_page
);
186 $this->mComment
= $row->rev_comment
;
187 $this->mUserText
= $row->rev_user_text
;
188 $this->mUser
= IntVal( $row->rev_user
);
189 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
190 $this->mTimestamp
= $row->rev_timestamp
;
192 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
193 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
196 if( isset( $row->old_text
) ) {
197 $this->mText
= $this->getRevisionText( $row );
201 } elseif( is_array( $row ) ) {
202 // Build a new revision to be saved...
205 $this->mId
= isset( $row['id'] ) ?
IntVal( $row['id'] ) : null;
206 $this->mPage
= isset( $row['page'] ) ?
IntVal( $row['page'] ) : null;
207 $this->mComment
= isset( $row['comment'] ) ?
StrVal( $row['comment'] ) : null;
208 $this->mUserText
= isset( $row['user_text'] ) ?
StrVal( $row['user_text'] ) : $wgUser->getName();
209 $this->mUser
= isset( $row['user'] ) ?
IntVal( $row['user'] ) : $wgUser->getId();
210 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
IntVal( $row['minor_edit'] ) : 0;
211 $this->mTimestamp
= isset( $row['timestamp'] ) ?
StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW
);
212 $this->mText
= isset( $row['text'] ) ?
StrVal( $row['text'] ) : null;
214 $this->mTitle
= null; # Load on demand if needed
215 $this->mCurrent
= false;
217 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
233 * Returns the title of the page associated with this entry.
236 function &getTitle() {
237 if( isset( $this->mTitle
) ) {
238 return $this->mTitle
;
240 $dbr =& wfGetDB( DB_SLAVE
);
241 $row = $dbr->selectRow(
242 array( 'page', 'revision' ),
243 array( 'page_namespace', 'page_title' ),
244 array( 'page_id=rev_page',
245 'rev_id' => $this->mId
),
246 'Revision::getTItle' );
248 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
251 return $this->mTitle
;
271 function getUserText() {
272 return $this->mUserText
;
278 function getComment() {
279 return $this->mComment
;
286 return (bool)$this->mMinorEdit
;
293 if( is_null( $this->mText
) ) {
294 // Revision text is immutable. Load on demand:
295 $this->mText
= $this->loadText();
303 function getTimestamp() {
304 return $this->mTimestamp
;
310 function isCurrent() {
311 return $this->mCurrent
;
317 function &getPrevious() {
318 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
319 return Revision
::newFromTitle( $this->mTitle
, $prev );
325 function &getNext() {
326 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
327 return Revision
::newFromTitle( $this->mTitle
, $next );
332 * Get revision text associated with an old or archive row
333 * $row is usually an object from wfFetchRow(), both the flags and the text
334 * field must be included
336 * @param integer $row Id of a row
337 * @param string $prefix table prefix (default 'old_')
338 * @return string $text|false the text requested
340 function getRevisionText( $row, $prefix = 'old_' ) {
341 $fname = 'Revision::getRevisionText';
342 wfProfileIn( $fname );
345 $textField = $prefix . 'text';
346 $flagsField = $prefix . 'flags';
348 if( isset( $row->$flagsField ) ) {
349 $flags = explode( ',', $row->$flagsField );
354 if( isset( $row->$textField ) ) {
355 $text = $row->$textField;
357 wfProfileOut( $fname );
361 if( in_array( 'gzip', $flags ) ) {
362 # Deal with optional compression of archived pages.
363 # This can be done periodically via maintenance/compressOld.php, and
364 # as pages are saved if $wgCompressRevisions is set.
365 $text = gzinflate( $text );
368 if( in_array( 'object', $flags ) ) {
369 # Generic compressed storage
370 $obj = unserialize( $text );
372 # Bugger, corrupted my test database by double-serializing
373 if ( !is_object( $obj ) ) {
374 $obj = unserialize( $obj );
377 $text = $obj->getText();
380 global $wgLegacyEncoding;
381 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
382 # Old revisions kept around in a legacy encoding?
383 # Upconvert on demand.
384 global $wgInputEncoding, $wgContLang;
385 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
387 wfProfileOut( $fname );
392 * If $wgCompressRevisions is enabled, we will compress data.
393 * The input string is modified in place.
394 * Return value is the flags field: contains 'gzip' if the
395 * data is compressed, and 'utf-8' if we're saving in UTF-8
399 * @param mixed $text reference to a text
402 function compressRevisionText( &$text ) {
403 global $wgCompressRevisions;
406 # Revisions not marked this way will be converted
407 # on load if $wgLegacyCharset is set in the future.
410 if( $wgCompressRevisions ) {
411 if( function_exists( 'gzdeflate' ) ) {
412 $text = gzdeflate( $text );
415 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
418 return implode( ',', $flags );
422 * Insert a new revision into the database, returning the new revision ID
423 * number on success and dies horribly on failure.
425 * @param Database $dbw
428 function insertOn( &$dbw ) {
429 $fname = 'Revision::insertOn';
430 wfProfileIn( $fname );
432 $mungedText = $this->mText
;
433 $flags = Revision
::compressRevisionText( $mungedText );
435 # Record the text to the text table
436 $old_id = isset( $this->mId
)
438 : $dbw->nextSequenceValue( 'text_old_id_val' );
439 $dbw->insert( 'text',
442 'old_text' => $mungedText,
443 'old_flags' => $flags,
446 $revisionId = $dbw->insertId();
448 # Record the edit in revisions
449 $dbw->insert( 'revision',
451 'rev_id' => $revisionId,
452 'rev_page' => $this->mPage
,
453 'rev_comment' => $this->mComment
,
454 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
455 'rev_user' => $this->mUser
,
456 'rev_user_text' => $this->mUserText
,
457 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
461 $this->mId
= $revisionId;
463 wfProfileOut( $fname );
468 * Lazy-load the revision's text.
469 * Currently hardcoded to the 'text' table storage engine.
474 function loadText() {
475 $fname = 'Revision::loadText';
476 wfProfileIn( $fname );
478 $dbr =& wfGetDB( DB_SLAVE
);
479 $row = $dbr->selectRow( 'text',
480 array( 'old_text', 'old_flags' ),
481 array( 'old_id' => $this->getId() ),
484 $text = Revision
::getRevisionText( $row );
485 wfProfileOut( $fname );