unbroke classic and cologne blue skins
[mediawiki.git] / includes / Revision.php
blob7c74bd0cc1f8b1ce528b476e458d28882e54b4dc
1 <?php
2 /**
3 * @package MediaWiki
4 * @todo document
5 */
7 /** */
8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
11 /**
12 * @package MediaWiki
13 * @todo document
15 class Revision {
16 /**
17 * Load a page revision from a given revision ID number.
18 * Returns null if no such revision can be found.
20 * @param int $id
21 * @static
22 * @access public
24 function &newFromId( $id ) {
25 return Revision::newFromConds(
26 array( 'page_id=rev_page',
27 'rev_id' => IntVal( $id ) ) );
30 /**
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.
35 * @param Title $title
36 * @param int $id
37 * @return Revision
38 * @access public
39 * @static
41 function &newFromTitle( &$title, $id = 0 ) {
42 if( $id ) {
43 $matchId = IntVal( $id );
44 } else {
45 $matchId = 'page_latest';
47 return Revision::newFromConds(
48 array( "rev_id=$matchId",
49 'page_id=rev_page',
50 'page_namespace' => $title->getNamespace(),
51 'page_title' => $title->getDbkey() ) );
54 /**
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.
59 * @param Database $db
60 * @param int $pageid
61 * @param int $id
62 * @return Revision
63 * @access public
65 function &loadFromPageId( &$db, $pageid, $id = 0 ) {
66 if( $id ) {
67 $matchId = IntVal( $id );
68 } else {
69 $matchId = 'page_latest';
71 return Revision::loadFromConds(
72 $db,
73 array( "rev_id=$matchId",
74 'rev_page' => IntVal( $pageid ),
75 'page_id=rev_page' ) );
78 /**
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.
83 * @param Database $db
84 * @param Title $title
85 * @param string $timestamp
86 * @return Revision
87 * @access public
88 * @static
90 function &loadFromTimestamp( &$db, &$title, $timestamp ) {
91 return Revision::loadFromConds(
92 $db,
93 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
94 'page_id=rev_page',
95 'page_namespace' => $title->getNamespace(),
96 'page_title' => $title->getDbkey() ) );
99 /**
100 * Given a set of conditions, fetch a revision.
102 * @param array $conditions
103 * @return Revision
104 * @static
105 * @access private
107 function &newFromConds( $conditions ) {
108 $db =& wfGetDB( DB_SLAVE );
109 $row = Revision::loadFromConds( $db, $conditions );
110 if( is_null( $row ) ) {
111 $dbw =& wfGetDB( DB_MASTER );
112 $row = Revision::loadFromConds( $dbw, $conditions );
114 return $row;
118 * Given a set of conditions, fetch a revision from
119 * the given database connection.
121 * @param Database $db
122 * @param array $conditions
123 * @return Revision
124 * @static
125 * @access private
127 function &loadFromConds( &$db, $conditions ) {
128 $res =& Revision::fetchFromConds( $db, $conditions );
129 if( $res ) {
130 $row = $res->fetchObject();
131 $res->free();
132 if( $row ) {
133 return new Revision( $row );
136 return null;
140 * Return a wrapper for a series of database rows to
141 * fetch all of a given page's revisions in turn.
142 * Each row can be fed to the constructor to get objects.
144 * @param Title $title
145 * @return ResultWrapper
146 * @static
147 * @access public
149 function &fetchAllRevisions( &$title ) {
150 return Revision::fetchFromConds(
151 wfGetDB( DB_SLAVE ),
152 array( 'page_namespace' => $title->getNamespace(),
153 'page_title' => $title->getDbkey(),
154 'page_id=rev_page' ) );
158 * Return a wrapper for a series of database rows to
159 * fetch all of a given page's revisions in turn.
160 * Each row can be fed to the constructor to get objects.
162 * @param Title $title
163 * @return ResultWrapper
164 * @static
165 * @access public
167 function &fetchRevision( &$title ) {
168 return Revision::fetchFromConds(
169 wfGetDB( DB_SLAVE ),
170 array( 'rev_id=page_latest',
171 'page_namespace' => $title->getNamespace(),
172 'page_title' => $title->getDbkey(),
173 'page_id=rev_page' ) );
177 * Given a set of conditions, return a ResultWrapper
178 * which will return matching database rows with the
179 * fields necessary to build Revision objects.
181 * @param Database $db
182 * @param array $conditions
183 * @return ResultWrapper
184 * @static
185 * @access private
187 function &fetchFromConds( &$db, $conditions ) {
188 $res = $db->select(
189 array( 'page', 'revision' ),
190 array( 'page_namespace',
191 'page_title',
192 'page_latest',
193 'rev_id',
194 'rev_page',
195 'rev_text_id',
196 'rev_comment',
197 'rev_user_text',
198 'rev_user',
199 'rev_minor_edit',
200 'rev_timestamp',
201 'rev_deleted' ),
202 $conditions,
203 'Revision::fetchRow',
204 array( 'LIMIT' => 1 ) );
205 return $db->resultObject( $res );
209 * @param object $row
210 * @access private
212 function Revision( $row ) {
213 if( is_object( $row ) ) {
214 $this->mId = IntVal( $row->rev_id );
215 $this->mPage = IntVal( $row->rev_page );
216 $this->mTextId = IntVal( $row->rev_text_id );
217 $this->mComment = $row->rev_comment;
218 $this->mUserText = $row->rev_user_text;
219 $this->mUser = IntVal( $row->rev_user );
220 $this->mMinorEdit = IntVal( $row->rev_minor_edit );
221 $this->mTimestamp = $row->rev_timestamp;
222 $this->mDeleted = IntVal( $row->rev_deleted );
224 $this->mCurrent = ( $row->rev_id == $row->page_latest );
225 $this->mTitle = Title::makeTitle( $row->page_namespace,
226 $row->page_title );
228 if( isset( $row->old_text ) ) {
229 $this->mText = $this->getRevisionText( $row );
230 } else {
231 $this->mText = null;
233 } elseif( is_array( $row ) ) {
234 // Build a new revision to be saved...
235 global $wgUser;
237 $this->mId = isset( $row['id'] ) ? IntVal( $row['id'] ) : null;
238 $this->mPage = isset( $row['page'] ) ? IntVal( $row['page'] ) : null;
239 $this->mTextId = isset( $row['text_id'] ) ? IntVal( $row['text_id'] ) : null;
240 $this->mUserText = isset( $row['user_text'] ) ? StrVal( $row['user_text'] ) : $wgUser->getName();
241 $this->mUser = isset( $row['user'] ) ? IntVal( $row['user'] ) : $wgUser->getId();
242 $this->mMinorEdit = isset( $row['minor_edit'] ) ? IntVal( $row['minor_edit'] ) : 0;
243 $this->mTimestamp = isset( $row['timestamp'] ) ? StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW );
244 $this->mDeleted = isset( $row['deleted'] ) ? IntVal( $row['deleted'] ) : 0;
246 // Enforce spacing trimming on supplied text
247 $this->mComment = isset( $row['comment'] ) ? trim( StrVal( $row['comment'] ) ) : null;
248 $this->mText = isset( $row['text'] ) ? rtrim( StrVal( $row['text'] ) ) : null;
250 $this->mTitle = null; # Load on demand if needed
251 $this->mCurrent = false;
252 } else {
253 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
257 /**#@+
258 * @access public
262 * @return int
264 function getId() {
265 return $this->mId;
269 * @return int
271 function getTextId() {
272 return $this->mTextId;
276 * Returns the title of the page associated with this entry.
277 * @return Title
279 function &getTitle() {
280 if( isset( $this->mTitle ) ) {
281 return $this->mTitle;
283 $dbr =& wfGetDB( DB_SLAVE );
284 $row = $dbr->selectRow(
285 array( 'page', 'revision' ),
286 array( 'page_namespace', 'page_title' ),
287 array( 'page_id=rev_page',
288 'rev_id' => $this->mId ),
289 'Revision::getTItle' );
290 if( $row ) {
291 $this->mTitle =& Title::makeTitle( $row->page_namespace,
292 $row->page_title );
294 return $this->mTitle;
298 * @return int
300 function getPage() {
301 return $this->mPage;
305 * @return int
307 function getUser() {
308 return $this->mUser;
312 * @return string
314 function getUserText() {
315 return $this->mUserText;
319 * @return string
321 function getComment() {
322 return $this->mComment;
326 * @return bool
328 function isMinor() {
329 return (bool)$this->mMinorEdit;
333 * @return bool
335 function isDeleted() {
336 return (bool)$this->mDeleted;
340 * @return string
342 function getText() {
343 if( is_null( $this->mText ) ) {
344 // Revision text is immutable. Load on demand:
345 $this->mText = $this->loadText();
347 return $this->mText;
351 * @return string
353 function getTimestamp() {
354 return $this->mTimestamp;
358 * @return bool
360 function isCurrent() {
361 return $this->mCurrent;
365 * @return Revision
367 function &getPrevious() {
368 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
369 return Revision::newFromTitle( $this->mTitle, $prev );
373 * @return Revision
375 function &getNext() {
376 $next = $this->mTitle->getNextRevisionID( $this->mId );
377 return Revision::newFromTitle( $this->mTitle, $next );
379 /**#@-*/
382 * Get revision text associated with an old or archive row
383 * $row is usually an object from wfFetchRow(), both the flags and the text
384 * field must be included
385 * @static
386 * @param integer $row Id of a row
387 * @param string $prefix table prefix (default 'old_')
388 * @return string $text|false the text requested
390 function getRevisionText( $row, $prefix = 'old_' ) {
391 $fname = 'Revision::getRevisionText';
392 wfProfileIn( $fname );
394 # Get data
395 $textField = $prefix . 'text';
396 $flagsField = $prefix . 'flags';
398 if( isset( $row->$flagsField ) ) {
399 $flags = explode( ',', $row->$flagsField );
400 } else {
401 $flags = array();
404 if( isset( $row->$textField ) ) {
405 $text = $row->$textField;
406 } else {
407 wfProfileOut( $fname );
408 return false;
411 # Use external methods for external objects, text in table is URL-only then
412 if ( in_array( 'external', $flags ) ) {
413 $url=$text;
414 @list($proto,$path)=explode('://',$url,2);
415 if ($path=="") {
416 wfProfileOut( $fname );
417 return false;
419 require_once('ExternalStore.php');
420 $text=ExternalStore::fetchFromURL($url);
423 if( in_array( 'gzip', $flags ) ) {
424 # Deal with optional compression of archived pages.
425 # This can be done periodically via maintenance/compressOld.php, and
426 # as pages are saved if $wgCompressRevisions is set.
427 $text = gzinflate( $text );
430 if( in_array( 'object', $flags ) ) {
431 # Generic compressed storage
432 $obj = unserialize( $text );
434 # Bugger, corrupted my test database by double-serializing
435 if ( !is_object( $obj ) ) {
436 $obj = unserialize( $obj );
439 $text = $obj->getText();
442 global $wgLegacyEncoding;
443 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
444 # Old revisions kept around in a legacy encoding?
445 # Upconvert on demand.
446 global $wgInputEncoding, $wgContLang;
447 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
449 wfProfileOut( $fname );
450 return $text;
454 * If $wgCompressRevisions is enabled, we will compress data.
455 * The input string is modified in place.
456 * Return value is the flags field: contains 'gzip' if the
457 * data is compressed, and 'utf-8' if we're saving in UTF-8
458 * mode.
460 * @static
461 * @param mixed $text reference to a text
462 * @return string
464 function compressRevisionText( &$text ) {
465 global $wgCompressRevisions;
466 $flags = array();
468 # Revisions not marked this way will be converted
469 # on load if $wgLegacyCharset is set in the future.
470 $flags[] = 'utf-8';
472 if( $wgCompressRevisions ) {
473 if( function_exists( 'gzdeflate' ) ) {
474 $text = gzdeflate( $text );
475 $flags[] = 'gzip';
476 } else {
477 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
480 return implode( ',', $flags );
484 * Insert a new revision into the database, returning the new revision ID
485 * number on success and dies horribly on failure.
487 * @param Database $dbw
488 * @return int
490 function insertOn( &$dbw ) {
491 $fname = 'Revision::insertOn';
492 wfProfileIn( $fname );
494 $mungedText = $this->mText;
495 $flags = Revision::compressRevisionText( $mungedText );
497 # Record the text to the text table
498 if( !isset( $this->mTextId ) ) {
499 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
500 $dbw->insert( 'text',
501 array(
502 'old_id' => $old_id,
503 'old_text' => $mungedText,
504 'old_flags' => $flags,
505 ), $fname
507 $this->mTextId = $dbw->insertId();
510 # Record the edit in revisions
511 $rev_id = isset( $this->mId )
512 ? $this->mId
513 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
514 $dbw->insert( 'revision',
515 array(
516 'rev_id' => $rev_id,
517 'rev_page' => $this->mPage,
518 'rev_text_id' => $this->mTextId,
519 'rev_comment' => $this->mComment,
520 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
521 'rev_user' => $this->mUser,
522 'rev_user_text' => $this->mUserText,
523 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
524 'rev_deleted' => $this->mDeleted,
525 ), $fname
528 $this->mId = $dbw->insertId();
530 wfProfileOut( $fname );
531 return $this->mId;
535 * Lazy-load the revision's text.
536 * Currently hardcoded to the 'text' table storage engine.
538 * @return string
539 * @access private
541 function loadText() {
542 $fname = 'Revision::loadText';
543 wfProfileIn( $fname );
545 $dbr =& wfGetDB( DB_SLAVE );
546 $row = $dbr->selectRow( 'text',
547 array( 'old_text', 'old_flags' ),
548 array( 'old_id' => $this->getTextId() ),
549 $fname);
551 if( !$row ) {
552 $dbw =& wfGetDB( DB_MASTER );
553 $row = $dbw->selectRow( 'text',
554 array( 'old_text', 'old_flags' ),
555 array( 'old_id' => $this->getTextId() ),
556 $fname);
559 $text = Revision::getRevisionText( $row );
560 wfProfileOut( $fname );
562 return $text;
566 * Create a new null-revision for insertion into a page's
567 * history. This will not re-save the text, but simply refer
568 * to the text from the previous version.
570 * Such revisions can for instance identify page rename
571 * operations and other such meta-modifications.
573 * @param Database $dbw
574 * @param int $pageId ID number of the page to read from
575 * @param string $summary
576 * @param bool $minor
577 * @return Revision
579 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
580 $fname = 'Revision::newNullRevision';
581 wfProfileIn( $fname );
583 $current = $dbw->selectRow(
584 array( 'page', 'revision' ),
585 array( 'page_latest', 'rev_text_id' ),
586 array(
587 'page_id' => $pageId,
588 'page_latest=rev_id',
590 $fname );
592 if( $current ) {
593 $revision = new Revision( array(
594 'page' => $pageId,
595 'comment' => $summary,
596 'minor_edit' => $minor,
597 'text_id' => $current->rev_text_id,
598 ) );
599 } else {
600 $revision = null;
603 wfProfileOut( $fname );
604 return $revision;