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