* (bug 1949) Profiling typo in rare error case
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @todo document
5 */
6
7 /** */
8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
10
11 /**
12 * @package MediaWiki
13 * @todo document
14 */
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.
19 *
20 * @param int $id
21 * @static
22 * @access public
23 */
24 function &newFromId( $id ) {
25 return Revision::newFromConds(
26 array( 'page_id=rev_page',
27 'rev_id' => IntVal( $id ) ) );
28 }
29
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.
34 *
35 * @param Title $title
36 * @param int $id
37 * @return Revision
38 * @access public
39 */
40 function &newFromTitle( &$title, $id = 0 ) {
41 if( $id ) {
42 $matchId = IntVal( $id );
43 } else {
44 $matchId = 'page_latest';
45 }
46 return Revision::newFromConds(
47 array( "rev_id=$matchId",
48 'page_id=rev_page',
49 'page_namespace' => $title->getNamespace(),
50 'page_title' => $title->getDbkey() ) );
51 }
52
53 /**
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.
57 *
58 * @param Database $db
59 * @param int $pageid
60 * @param int $id
61 * @return Revision
62 * @access public
63 */
64 function &loadFromPageId( &$db, $pageid, $id = 0 ) {
65 if( $id ) {
66 $matchId = IntVal( $id );
67 } else {
68 $matchId = 'page_latest';
69 }
70 return Revision::loadFromConds(
71 $db,
72 array( "rev_id=$matchId",
73 'rev_page' => IntVal( $pageid ),
74 'page_id=rev_page' ) );
75 }
76
77 /**
78 * Given a set of conditions, fetch a revision.
79 *
80 * @param array $conditions
81 * @return Revision
82 * @static
83 * @access private
84 */
85 function &newFromConds( $conditions ) {
86 $db =& wfGetDB( DB_SLAVE );
87 return Revision::loadFromConds( $db, $conditions );
88 }
89
90 /**
91 * Given a set of conditions, fetch a revision from
92 * the given database connection.
93 *
94 * @param Database $db
95 * @param array $conditions
96 * @return Revision
97 * @static
98 * @access private
99 */
100 function &loadFromConds( &$db, $conditions ) {
101 $res =& Revision::fetchFromConds( $db, $conditions );
102 if( $res ) {
103 $row = $res->fetchObject();
104 $res->free();
105 if( $row ) {
106 return new Revision( $row );
107 }
108 }
109 return null;
110 }
111
112 /**
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.
116 *
117 * @param Title $title
118 * @return ResultWrapper
119 * @static
120 * @access public
121 */
122 function &fetchAllRevisions( &$title ) {
123 return Revision::fetchFromConds(
124 wfGetDB( DB_SLAVE ),
125 array( 'page_namespace' => $title->getNamespace(),
126 'page_title' => $title->getDbkey(),
127 'page_id=rev_page' ) );
128 }
129
130 /**
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.
134 *
135 * @param Title $title
136 * @return ResultWrapper
137 * @static
138 * @access public
139 */
140 function &fetchRevision( &$title ) {
141 return Revision::fetchFromConds(
142 wfGetDB( DB_SLAVE ),
143 array( 'rev_id=page_latest',
144 'page_namespace' => $title->getNamespace(),
145 'page_title' => $title->getDbkey(),
146 'page_id=rev_page' ) );
147 }
148
149 /**
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.
153 *
154 * @param Database $db
155 * @param array $conditions
156 * @return ResultWrapper
157 * @static
158 * @access private
159 */
160 function &fetchFromConds( &$db, $conditions ) {
161 $res = $db->select(
162 array( 'page', 'revision' ),
163 array( 'page_namespace',
164 'page_title',
165 'page_latest',
166 'rev_id',
167 'rev_page',
168 'rev_text_id',
169 'rev_comment',
170 'rev_user_text',
171 'rev_user',
172 'rev_minor_edit',
173 'rev_timestamp',
174 'rev_deleted' ),
175 $conditions,
176 'Revision::fetchRow' );
177 return $db->resultObject( $res );
178 }
179
180 /**
181 * @param object $row
182 * @access private
183 */
184 function Revision( $row ) {
185 if( is_object( $row ) ) {
186 $this->mId = IntVal( $row->rev_id );
187 $this->mPage = IntVal( $row->rev_page );
188 $this->mTextId = IntVal( $row->rev_text_id );
189 $this->mComment = $row->rev_comment;
190 $this->mUserText = $row->rev_user_text;
191 $this->mUser = IntVal( $row->rev_user );
192 $this->mMinorEdit = IntVal( $row->rev_minor_edit );
193 $this->mTimestamp = $row->rev_timestamp;
194 $this->mDeleted = IntVal( $row->rev_deleted );
195
196 $this->mCurrent = ( $row->rev_id == $row->page_latest );
197 $this->mTitle = Title::makeTitle( $row->page_namespace,
198 $row->page_title );
199
200 if( isset( $row->old_text ) ) {
201 $this->mText = $this->getRevisionText( $row );
202 } else {
203 $this->mText = null;
204 }
205 } elseif( is_array( $row ) ) {
206 // Build a new revision to be saved...
207 global $wgUser;
208
209 $this->mId = isset( $row['id'] ) ? IntVal( $row['id'] ) : null;
210 $this->mPage = isset( $row['page'] ) ? IntVal( $row['page'] ) : null;
211 $this->mTextId = isset( $row['text_id'] ) ? IntVal( $row['text_id'] ) : null;
212 $this->mComment = isset( $row['comment'] ) ? StrVal( $row['comment'] ) : null;
213 $this->mUserText = isset( $row['user_text'] ) ? StrVal( $row['user_text'] ) : $wgUser->getName();
214 $this->mUser = isset( $row['user'] ) ? IntVal( $row['user'] ) : $wgUser->getId();
215 $this->mMinorEdit = isset( $row['minor_edit'] ) ? IntVal( $row['minor_edit'] ) : 0;
216 $this->mTimestamp = isset( $row['timestamp'] ) ? StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW );
217 $this->mDeleted = isset( $row['deleted'] ) ? IntVal( $row['deleted'] ) : 0;
218 $this->mText = isset( $row['text'] ) ? StrVal( $row['text'] ) : null;
219
220 $this->mTitle = null; # Load on demand if needed
221 $this->mCurrent = false;
222 } else {
223 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
224 }
225 }
226
227 /**#@+
228 * @access public
229 */
230
231 /**
232 * @return int
233 */
234 function getId() {
235 return $this->mId;
236 }
237
238 /**
239 * @return int
240 */
241 function getTextId() {
242 return $this->mTextId;
243 }
244
245 /**
246 * Returns the title of the page associated with this entry.
247 * @return Title
248 */
249 function &getTitle() {
250 if( isset( $this->mTitle ) ) {
251 return $this->mTitle;
252 }
253 $dbr =& wfGetDB( DB_SLAVE );
254 $row = $dbr->selectRow(
255 array( 'page', 'revision' ),
256 array( 'page_namespace', 'page_title' ),
257 array( 'page_id=rev_page',
258 'rev_id' => $this->mId ),
259 'Revision::getTItle' );
260 if( $row ) {
261 $this->mTitle =& Title::makeTitle( $row->page_namespace,
262 $row->page_title );
263 }
264 return $this->mTitle;
265 }
266
267 /**
268 * @return int
269 */
270 function getPage() {
271 return $this->mPage;
272 }
273
274 /**
275 * @return int
276 */
277 function getUser() {
278 return $this->mUser;
279 }
280
281 /**
282 * @return string
283 */
284 function getUserText() {
285 return $this->mUserText;
286 }
287
288 /**
289 * @return string
290 */
291 function getComment() {
292 return $this->mComment;
293 }
294
295 /**
296 * @return bool
297 */
298 function isMinor() {
299 return (bool)$this->mMinorEdit;
300 }
301
302 /**
303 * @return bool
304 */
305 function isDeleted() {
306 return (bool)$this->mDeleted;
307 }
308
309 /**
310 * @return string
311 */
312 function getText() {
313 if( is_null( $this->mText ) ) {
314 // Revision text is immutable. Load on demand:
315 $this->mText = $this->loadText();
316 }
317 return $this->mText;
318 }
319
320 /**
321 * @return string
322 */
323 function getTimestamp() {
324 return $this->mTimestamp;
325 }
326
327 /**
328 * @return bool
329 */
330 function isCurrent() {
331 return $this->mCurrent;
332 }
333
334 /**
335 * @return Revision
336 */
337 function &getPrevious() {
338 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
339 return Revision::newFromTitle( $this->mTitle, $prev );
340 }
341
342 /**
343 * @return Revision
344 */
345 function &getNext() {
346 $next = $this->mTitle->getNextRevisionID( $this->mId );
347 return Revision::newFromTitle( $this->mTitle, $next );
348 }
349 /**#@-*/
350
351 /**
352 * Get revision text associated with an old or archive row
353 * $row is usually an object from wfFetchRow(), both the flags and the text
354 * field must be included
355 * @static
356 * @param integer $row Id of a row
357 * @param string $prefix table prefix (default 'old_')
358 * @return string $text|false the text requested
359 */
360 function getRevisionText( $row, $prefix = 'old_' ) {
361 $fname = 'Revision::getRevisionText';
362 wfProfileIn( $fname );
363
364 # Get data
365 $textField = $prefix . 'text';
366 $flagsField = $prefix . 'flags';
367
368 if( isset( $row->$flagsField ) ) {
369 $flags = explode( ',', $row->$flagsField );
370 } else {
371 $flags = array();
372 }
373
374 if( isset( $row->$textField ) ) {
375 $text = $row->$textField;
376 } else {
377 wfProfileOut( $fname );
378 return false;
379 }
380
381 # Use external methods for external objects, text in table is URL-only then
382 if ( in_array( 'external', $flags ) ) {
383 $url=$text;
384 @list($proto,$path)=explode('://',$url,2);
385 if ($path=="") {
386 wfProfileOut( $fname );
387 return false;
388 }
389 require_once('ExternalStore.php');
390 $text=ExternalStore::fetchFromURL($url);
391 }
392
393 if( in_array( 'gzip', $flags ) ) {
394 # Deal with optional compression of archived pages.
395 # This can be done periodically via maintenance/compressOld.php, and
396 # as pages are saved if $wgCompressRevisions is set.
397 $text = gzinflate( $text );
398 }
399
400 if( in_array( 'object', $flags ) ) {
401 # Generic compressed storage
402 $obj = unserialize( $text );
403
404 # Bugger, corrupted my test database by double-serializing
405 if ( !is_object( $obj ) ) {
406 $obj = unserialize( $obj );
407 }
408
409 $text = $obj->getText();
410 }
411
412 global $wgLegacyEncoding;
413 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
414 # Old revisions kept around in a legacy encoding?
415 # Upconvert on demand.
416 global $wgInputEncoding, $wgContLang;
417 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
418 }
419 wfProfileOut( $fname );
420 return $text;
421 }
422
423 /**
424 * If $wgCompressRevisions is enabled, we will compress data.
425 * The input string is modified in place.
426 * Return value is the flags field: contains 'gzip' if the
427 * data is compressed, and 'utf-8' if we're saving in UTF-8
428 * mode.
429 *
430 * @static
431 * @param mixed $text reference to a text
432 * @return string
433 */
434 function compressRevisionText( &$text ) {
435 global $wgCompressRevisions;
436 $flags = array();
437
438 # Revisions not marked this way will be converted
439 # on load if $wgLegacyCharset is set in the future.
440 $flags[] = 'utf-8';
441
442 if( $wgCompressRevisions ) {
443 if( function_exists( 'gzdeflate' ) ) {
444 $text = gzdeflate( $text );
445 $flags[] = 'gzip';
446 } else {
447 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
448 }
449 }
450 return implode( ',', $flags );
451 }
452
453 /**
454 * Insert a new revision into the database, returning the new revision ID
455 * number on success and dies horribly on failure.
456 *
457 * @param Database $dbw
458 * @return int
459 */
460 function insertOn( &$dbw ) {
461 $fname = 'Revision::insertOn';
462 wfProfileIn( $fname );
463
464 $mungedText = $this->mText;
465 $flags = Revision::compressRevisionText( $mungedText );
466
467 # Record the text to the text table
468 if( !isset( $this->mTextId ) ) {
469 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
470 $dbw->insert( 'text',
471 array(
472 'old_id' => $old_id,
473 'old_text' => $mungedText,
474 'old_flags' => $flags,
475 ), $fname
476 );
477 $this->mTextId = $dbw->insertId();
478 }
479
480 # Record the edit in revisions
481 $rev_id = isset( $this->mId )
482 ? $this->mId
483 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
484 $dbw->insert( 'revision',
485 array(
486 'rev_id' => $rev_id,
487 'rev_page' => $this->mPage,
488 'rev_text_id' => $this->mTextId,
489 'rev_comment' => $this->mComment,
490 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
491 'rev_user' => $this->mUser,
492 'rev_user_text' => $this->mUserText,
493 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
494 'rev_deleted' => $this->mDeleted,
495 ), $fname
496 );
497
498 $this->mId = $dbw->insertId();
499
500 wfProfileOut( $fname );
501 return $this->mId;
502 }
503
504 /**
505 * Lazy-load the revision's text.
506 * Currently hardcoded to the 'text' table storage engine.
507 *
508 * @return string
509 * @access private
510 */
511 function loadText() {
512 $fname = 'Revision::loadText';
513 wfProfileIn( $fname );
514
515 $dbr =& wfGetDB( DB_SLAVE );
516 $row = $dbr->selectRow( 'text',
517 array( 'old_text', 'old_flags' ),
518 array( 'old_id' => $this->getTextId() ),
519 $fname);
520
521 $text = Revision::getRevisionText( $row );
522 wfProfileOut( $fname );
523
524 return $text;
525 }
526
527 /**
528 * Create a new null-revision for insertion into a page's
529 * history. This will not re-save the text, but simply refer
530 * to the text from the previous version.
531 *
532 * Such revisions can for instance identify page rename
533 * operations and other such meta-modifications.
534 *
535 * @param Database $dbw
536 * @param int $pageId ID number of the page to read from
537 * @param string $summary
538 * @param bool $minor
539 * @return Revision
540 */
541 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
542 $fname = 'Revision::newNullRevision';
543 wfProfileIn( $fname );
544
545 $current = $dbw->selectRow(
546 array( 'page', 'revision' ),
547 array( 'page_latest', 'rev_text_id' ),
548 array(
549 'page_id' => $pageId,
550 'page_latest=rev_id',
551 ),
552 $fname );
553
554 if( $current ) {
555 $revision = new Revision( array(
556 'page' => $pageId,
557 'comment' => $summary,
558 'minor_edit' => $minor,
559 'text_id' => $current->rev_text_id,
560 ) );
561 } else {
562 $revision = null;
563 }
564
565 wfProfileOut( $fname );
566 return $revision;
567 }
568
569 }
570 ?>