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