* Lazy extraction of text chunks in Revision objects, may reduce hits to
[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
10 /**
11 * @package MediaWiki
12 * @todo document
13 */
14 class Revision {
15 const DELETED_TEXT = 1;
16 const DELETED_COMMENT = 2;
17 const DELETED_USER = 4;
18 const DELETED_RESTRICTED = 8;
19
20 /**
21 * Load a page revision from a given revision ID number.
22 * Returns null if no such revision can be found.
23 *
24 * @param int $id
25 * @static
26 * @access public
27 */
28 public static function newFromId( $id ) {
29 return Revision::newFromConds(
30 array( 'page_id=rev_page',
31 'rev_id' => intval( $id ) ) );
32 }
33
34 /**
35 * Load either the current, or a specified, revision
36 * that's attached to a given title. If not attached
37 * to that title, will return null.
38 *
39 * @param Title $title
40 * @param int $id
41 * @return Revision
42 * @access public
43 * @static
44 */
45 public static function newFromTitle( &$title, $id = 0 ) {
46 if( $id ) {
47 $matchId = intval( $id );
48 } else {
49 $matchId = 'page_latest';
50 }
51 return Revision::newFromConds(
52 array( "rev_id=$matchId",
53 'page_id=rev_page',
54 'page_namespace' => $title->getNamespace(),
55 'page_title' => $title->getDbkey() ) );
56 }
57
58 /**
59 * Load either the current, or a specified, revision
60 * that's attached to a given page. If not attached
61 * to that page, will return null.
62 *
63 * @param Database $db
64 * @param int $pageid
65 * @param int $id
66 * @return Revision
67 * @access public
68 */
69 public static function loadFromPageId( &$db, $pageid, $id = 0 ) {
70 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
71 if( $id ) {
72 $conds['rev_id']=intval($id);
73 } else {
74 $conds[]='rev_id=page_latest';
75 }
76 return Revision::loadFromConds( $db, $conds );
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 private static 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 private static 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 public static 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 private static 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 if( isset( $row->page_latest ) ) {
254 $this->mCurrent = ( $row->rev_id == $row->page_latest );
255 $this->mTitle = Title::makeTitle( $row->page_namespace,
256 $row->page_title );
257 } else {
258 $this->mCurrent = false;
259 $this->mTitle = null;
260 }
261
262 // Lazy extraction...
263 $this->mText = null;
264 if( isset( $row->old_text ) ) {
265 $this->mTextRow = $row;
266 } else {
267 // 'text' table row entry will be lazy-loaded
268 $this->mTextRow = null;
269 }
270 } elseif( is_array( $row ) ) {
271 // Build a new revision to be saved...
272 global $wgUser;
273
274 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
275 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
276 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
277 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
278 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
279 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
280 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
281 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
282
283 // Enforce spacing trimming on supplied text
284 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
285 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
286
287 $this->mTitle = null; # Load on demand if needed
288 $this->mCurrent = false;
289 } else {
290 throw new MWException( 'Revision constructor passed invalid row format.' );
291 }
292 }
293
294 /**#@+
295 * @access public
296 */
297
298 /**
299 * @return int
300 */
301 function getId() {
302 return $this->mId;
303 }
304
305 /**
306 * @return int
307 */
308 function getTextId() {
309 return $this->mTextId;
310 }
311
312 /**
313 * Returns the title of the page associated with this entry.
314 * @return Title
315 */
316 function getTitle() {
317 if( isset( $this->mTitle ) ) {
318 return $this->mTitle;
319 }
320 $dbr =& wfGetDB( DB_SLAVE );
321 $row = $dbr->selectRow(
322 array( 'page', 'revision' ),
323 array( 'page_namespace', 'page_title' ),
324 array( 'page_id=rev_page',
325 'rev_id' => $this->mId ),
326 'Revision::getTitle' );
327 if( $row ) {
328 $this->mTitle = Title::makeTitle( $row->page_namespace,
329 $row->page_title );
330 }
331 return $this->mTitle;
332 }
333
334 /**
335 * Set the title of the revision
336 * @param Title $title
337 */
338 function setTitle( $title ) {
339 $this->mTitle = $title;
340 }
341
342 /**
343 * @return int
344 */
345 function getPage() {
346 return $this->mPage;
347 }
348
349 /**
350 * Fetch revision's user id if it's available to all users
351 * @return int
352 */
353 function getUser() {
354 if( $this->isDeleted( self::DELETED_USER ) ) {
355 return 0;
356 } else {
357 return $this->mUser;
358 }
359 }
360
361 /**
362 * Fetch revision's user id without regard for the current user's permissions
363 * @return string
364 */
365 function getRawUser() {
366 return $this->mUser;
367 }
368
369 /**
370 * Fetch revision's username if it's available to all users
371 * @return string
372 */
373 function getUserText() {
374 if( $this->isDeleted( self::DELETED_USER ) ) {
375 return "";
376 } else {
377 return $this->mUserText;
378 }
379 }
380
381 /**
382 * Fetch revision's username without regard for view restrictions
383 * @return string
384 */
385 function getRawUserText() {
386 return $this->mUserText;
387 }
388
389 /**
390 * Fetch revision comment if it's available to all users
391 * @return string
392 */
393 function getComment() {
394 if( $this->isDeleted( self::DELETED_COMMENT ) ) {
395 return "";
396 } else {
397 return $this->mComment;
398 }
399 }
400
401 /**
402 * Fetch revision comment without regard for the current user's permissions
403 * @return string
404 */
405 function getRawComment() {
406 return $this->mComment;
407 }
408
409 /**
410 * @return bool
411 */
412 function isMinor() {
413 return (bool)$this->mMinorEdit;
414 }
415
416 /**
417 * int $field one of DELETED_* bitfield constants
418 * @return bool
419 */
420 function isDeleted( $field ) {
421 return ($this->mDeleted & $field) == $field;
422 }
423
424 /**
425 * Fetch revision text if it's available to all users
426 * @return string
427 */
428 function getText() {
429 if( $this->isDeleted( self::DELETED_TEXT ) ) {
430 return "";
431 } else {
432 return $this->getRawText();
433 }
434 }
435
436 /**
437 * Fetch revision text without regard for view restrictions
438 * @return string
439 */
440 function getRawText() {
441 if( is_null( $this->mText ) ) {
442 // Revision text is immutable. Load on demand:
443 $this->mText = $this->loadText();
444 }
445 return $this->mText;
446 }
447
448 /**
449 * @return string
450 */
451 function getTimestamp() {
452 return wfTimestamp(TS_MW, $this->mTimestamp);
453 }
454
455 /**
456 * @return bool
457 */
458 function isCurrent() {
459 return $this->mCurrent;
460 }
461
462 /**
463 * @return Revision
464 */
465 function getPrevious() {
466 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
467 if ( $prev ) {
468 return Revision::newFromTitle( $this->mTitle, $prev );
469 } else {
470 return null;
471 }
472 }
473
474 /**
475 * @return Revision
476 */
477 function getNext() {
478 $next = $this->mTitle->getNextRevisionID( $this->mId );
479 if ( $next ) {
480 return Revision::newFromTitle( $this->mTitle, $next );
481 } else {
482 return null;
483 }
484 }
485 /**#@-*/
486
487 /**
488 * Get revision text associated with an old or archive row
489 * $row is usually an object from wfFetchRow(), both the flags and the text
490 * field must be included
491 * @static
492 * @param integer $row Id of a row
493 * @param string $prefix table prefix (default 'old_')
494 * @return string $text|false the text requested
495 */
496 function getRevisionText( $row, $prefix = 'old_' ) {
497 $fname = 'Revision::getRevisionText';
498 wfProfileIn( $fname );
499
500 # Get data
501 $textField = $prefix . 'text';
502 $flagsField = $prefix . 'flags';
503
504 if( isset( $row->$flagsField ) ) {
505 $flags = explode( ',', $row->$flagsField );
506 } else {
507 $flags = array();
508 }
509
510 if( isset( $row->$textField ) ) {
511 $text = $row->$textField;
512 } else {
513 wfProfileOut( $fname );
514 return false;
515 }
516
517 # Use external methods for external objects, text in table is URL-only then
518 if ( in_array( 'external', $flags ) ) {
519 $url=$text;
520 @list($proto,$path)=explode('://',$url,2);
521 if ($path=="") {
522 wfProfileOut( $fname );
523 return false;
524 }
525 require_once('ExternalStore.php');
526 $text=ExternalStore::fetchFromURL($url);
527 }
528
529 // If the text was fetched without an error, convert it
530 if ( $text !== false ) {
531 if( in_array( 'gzip', $flags ) ) {
532 # Deal with optional compression of archived pages.
533 # This can be done periodically via maintenance/compressOld.php, and
534 # as pages are saved if $wgCompressRevisions is set.
535 $text = gzinflate( $text );
536 }
537
538 if( in_array( 'object', $flags ) ) {
539 # Generic compressed storage
540 $obj = unserialize( $text );
541 if ( !is_object( $obj ) ) {
542 // Invalid object
543 wfProfileOut( $fname );
544 return false;
545 }
546 $text = $obj->getText();
547 }
548
549 global $wgLegacyEncoding;
550 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
551 # Old revisions kept around in a legacy encoding?
552 # Upconvert on demand.
553 global $wgInputEncoding, $wgContLang;
554 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
555 }
556 }
557 wfProfileOut( $fname );
558 return $text;
559 }
560
561 /**
562 * If $wgCompressRevisions is enabled, we will compress data.
563 * The input string is modified in place.
564 * Return value is the flags field: contains 'gzip' if the
565 * data is compressed, and 'utf-8' if we're saving in UTF-8
566 * mode.
567 *
568 * @static
569 * @param mixed $text reference to a text
570 * @return string
571 */
572 function compressRevisionText( &$text ) {
573 global $wgCompressRevisions;
574 $flags = array();
575
576 # Revisions not marked this way will be converted
577 # on load if $wgLegacyCharset is set in the future.
578 $flags[] = 'utf-8';
579
580 if( $wgCompressRevisions ) {
581 if( function_exists( 'gzdeflate' ) ) {
582 $text = gzdeflate( $text );
583 $flags[] = 'gzip';
584 } else {
585 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
586 }
587 }
588 return implode( ',', $flags );
589 }
590
591 /**
592 * Insert a new revision into the database, returning the new revision ID
593 * number on success and dies horribly on failure.
594 *
595 * @param Database $dbw
596 * @return int
597 */
598 function insertOn( &$dbw ) {
599 global $wgDefaultExternalStore;
600
601 $fname = 'Revision::insertOn';
602 wfProfileIn( $fname );
603
604 $data = $this->mText;
605 $flags = Revision::compressRevisionText( $data );
606
607 # Write to external storage if required
608 if ( $wgDefaultExternalStore ) {
609 if ( is_array( $wgDefaultExternalStore ) ) {
610 // Distribute storage across multiple clusters
611 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
612 } else {
613 $store = $wgDefaultExternalStore;
614 }
615 require_once('ExternalStore.php');
616 // Store and get the URL
617 $data = ExternalStore::insert( $store, $data );
618 if ( !$data ) {
619 # This should only happen in the case of a configuration error, where the external store is not valid
620 throw new MWException( "Unable to store text to external storage $store" );
621 }
622 if ( $flags ) {
623 $flags .= ',';
624 }
625 $flags .= 'external';
626 }
627
628 # Record the text (or external storage URL) to the text table
629 if( !isset( $this->mTextId ) ) {
630 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
631 $dbw->insert( 'text',
632 array(
633 'old_id' => $old_id,
634 'old_text' => $data,
635 'old_flags' => $flags,
636 ), $fname
637 );
638 $this->mTextId = $dbw->insertId();
639 }
640
641 # Record the edit in revisions
642 $rev_id = isset( $this->mId )
643 ? $this->mId
644 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
645 $dbw->insert( 'revision',
646 array(
647 'rev_id' => $rev_id,
648 'rev_page' => $this->mPage,
649 'rev_text_id' => $this->mTextId,
650 'rev_comment' => $this->mComment,
651 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
652 'rev_user' => $this->mUser,
653 'rev_user_text' => $this->mUserText,
654 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
655 'rev_deleted' => $this->mDeleted,
656 ), $fname
657 );
658
659 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
660 wfProfileOut( $fname );
661 return $this->mId;
662 }
663
664 /**
665 * Lazy-load the revision's text.
666 * Currently hardcoded to the 'text' table storage engine.
667 *
668 * @return string
669 * @access private
670 */
671 function loadText() {
672 $fname = 'Revision::loadText';
673 wfProfileIn( $fname );
674
675 // If we kept data for lazy extraction, use it now...
676 $row = $this->mTextRow;
677 $this->mTextRow = null;
678
679 if( !$row ) {
680 // Text data is immutable; check slaves first.
681 $dbr =& wfGetDB( DB_SLAVE );
682 $row = $dbr->selectRow( 'text',
683 array( 'old_text', 'old_flags' ),
684 array( 'old_id' => $this->getTextId() ),
685 $fname);
686 }
687
688 if( !$row ) {
689 // Possible slave lag!
690 $dbw =& wfGetDB( DB_MASTER );
691 $row = $dbw->selectRow( 'text',
692 array( 'old_text', 'old_flags' ),
693 array( 'old_id' => $this->getTextId() ),
694 $fname);
695 }
696
697 $text = Revision::getRevisionText( $row );
698 wfProfileOut( $fname );
699
700 return $text;
701 }
702
703 /**
704 * Create a new null-revision for insertion into a page's
705 * history. This will not re-save the text, but simply refer
706 * to the text from the previous version.
707 *
708 * Such revisions can for instance identify page rename
709 * operations and other such meta-modifications.
710 *
711 * @param Database $dbw
712 * @param int $pageId ID number of the page to read from
713 * @param string $summary
714 * @param bool $minor
715 * @return Revision
716 */
717 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
718 $fname = 'Revision::newNullRevision';
719 wfProfileIn( $fname );
720
721 $current = $dbw->selectRow(
722 array( 'page', 'revision' ),
723 array( 'page_latest', 'rev_text_id' ),
724 array(
725 'page_id' => $pageId,
726 'page_latest=rev_id',
727 ),
728 $fname );
729
730 if( $current ) {
731 $revision = new Revision( array(
732 'page' => $pageId,
733 'comment' => $summary,
734 'minor_edit' => $minor,
735 'text_id' => $current->rev_text_id,
736 ) );
737 } else {
738 $revision = null;
739 }
740
741 wfProfileOut( $fname );
742 return $revision;
743 }
744
745 /**
746 * Determine if the current user is allowed to view a particular
747 * field of this revision, if it's marked as deleted.
748 * @param int $field one of self::DELETED_TEXT,
749 * self::DELETED_COMMENT,
750 * self::DELETED_USER
751 * @return bool
752 */
753 function userCan( $field ) {
754 if( ( $this->mDeleted & $field ) == $field ) {
755 global $wgUser;
756 $permission = ( $this->mDeleted & self::DELETED_RESTRICTED ) == self::DELETED_RESTRICTED
757 ? 'hiderevision'
758 : 'deleterevision';
759 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
760 return $wgUser->isAllowed( $permission );
761 } else {
762 return true;
763 }
764 }
765
766
767 /**
768 * Get rev_timestamp from rev_id, without loading the rest of the row
769 * @param integer $id
770 */
771 static function getTimestampFromID( $id ) {
772 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
773 array( 'rev_id' => $id ), __METHOD__ );
774 if ( $timestamp === false ) {
775 # Not in slave, try master
776 $dbw =& wfGetDB( DB_MASTER );
777 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
778 array( 'rev_id' => $id ), __METHOD__ );
779 }
780 return $timestamp;
781 }
782
783 static function countByPageId( $db, $id ) {
784 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
785 array( 'rev_page' => $id ), __METHOD__ );
786 if( $row ) {
787 return $row->revCount;
788 }
789 return 0;
790 }
791
792 static function countByTitle( $db, $title ) {
793 $id = $title->getArticleId();
794 if( $id ) {
795 return Revision::countByPageId( $db, $id );
796 }
797 return 0;
798 }
799 }
800
801 /**
802 * Aliases for backwards compatibility with 1.6
803 */
804 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
805 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
806 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
807 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );
808
809
810 ?>