* (bug 6133) Update strip state as we work. This mostly fixes extensions
[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 define( 'MW_REV_DELETED_TEXT', 1 );
13 define( 'MW_REV_DELETED_COMMENT', 2 );
14 define( 'MW_REV_DELETED_USER', 4 );
15 define( 'MW_REV_DELETED_RESTRICTED', 8 );
16 /** @- */
17
18 /**
19 * @package MediaWiki
20 * @todo document
21 */
22 class Revision {
23 /**
24 * Load a page revision from a given revision ID number.
25 * Returns null if no such revision can be found.
26 *
27 * @param int $id
28 * @static
29 * @access public
30 */
31 function newFromId( $id ) {
32 return Revision::newFromConds(
33 array( 'page_id=rev_page',
34 'rev_id' => intval( $id ) ) );
35 }
36
37 /**
38 * Load either the current, or a specified, revision
39 * that's attached to a given title. If not attached
40 * to that title, will return null.
41 *
42 * @param Title $title
43 * @param int $id
44 * @return Revision
45 * @access public
46 * @static
47 */
48 function newFromTitle( &$title, $id = 0 ) {
49 if( $id ) {
50 $matchId = intval( $id );
51 } else {
52 $matchId = 'page_latest';
53 }
54 return Revision::newFromConds(
55 array( "rev_id=$matchId",
56 'page_id=rev_page',
57 'page_namespace' => $title->getNamespace(),
58 'page_title' => $title->getDbkey() ) );
59 }
60
61 /**
62 * Load either the current, or a specified, revision
63 * that's attached to a given page. If not attached
64 * to that page, will return null.
65 *
66 * @param Database $db
67 * @param int $pageid
68 * @param int $id
69 * @return Revision
70 * @access public
71 */
72 function loadFromPageId( &$db, $pageid, $id = 0 ) {
73 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
74 if( $id ) {
75 $conds['rev_id']=intval($id);
76 } else {
77 $conds[]='rev_id=page_latest';
78 }
79 return Revision::loadFromConds( $db, $conds );
80 }
81
82 /**
83 * Load either the current, or a specified, revision
84 * that's attached to a given page. If not attached
85 * to that page, will return null.
86 *
87 * @param Database $db
88 * @param Title $title
89 * @param int $id
90 * @return Revision
91 * @access public
92 */
93 function loadFromTitle( &$db, $title, $id = 0 ) {
94 if( $id ) {
95 $matchId = intval( $id );
96 } else {
97 $matchId = 'page_latest';
98 }
99 return Revision::loadFromConds(
100 $db,
101 array( "rev_id=$matchId",
102 'page_id=rev_page',
103 'page_namespace' => $title->getNamespace(),
104 'page_title' => $title->getDbkey() ) );
105 }
106
107 /**
108 * Load the revision for the given title with the given timestamp.
109 * WARNING: Timestamps may in some circumstances not be unique,
110 * so this isn't the best key to use.
111 *
112 * @param Database $db
113 * @param Title $title
114 * @param string $timestamp
115 * @return Revision
116 * @access public
117 * @static
118 */
119 function loadFromTimestamp( &$db, &$title, $timestamp ) {
120 return Revision::loadFromConds(
121 $db,
122 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
123 'page_id=rev_page',
124 'page_namespace' => $title->getNamespace(),
125 'page_title' => $title->getDbkey() ) );
126 }
127
128 /**
129 * Given a set of conditions, fetch a revision.
130 *
131 * @param array $conditions
132 * @return Revision
133 * @static
134 * @access private
135 */
136 function newFromConds( $conditions ) {
137 $db =& wfGetDB( DB_SLAVE );
138 $row = Revision::loadFromConds( $db, $conditions );
139 if( is_null( $row ) ) {
140 $dbw =& wfGetDB( DB_MASTER );
141 $row = Revision::loadFromConds( $dbw, $conditions );
142 }
143 return $row;
144 }
145
146 /**
147 * Given a set of conditions, fetch a revision from
148 * the given database connection.
149 *
150 * @param Database $db
151 * @param array $conditions
152 * @return Revision
153 * @static
154 * @access private
155 */
156 function loadFromConds( &$db, $conditions ) {
157 $res = Revision::fetchFromConds( $db, $conditions );
158 if( $res ) {
159 $row = $res->fetchObject();
160 $res->free();
161 if( $row ) {
162 $ret = new Revision( $row );
163 return $ret;
164 }
165 }
166 $ret = null;
167 return $ret;
168 }
169
170 /**
171 * Return a wrapper for a series of database rows to
172 * fetch all of a given page's revisions in turn.
173 * Each row can be fed to the constructor to get objects.
174 *
175 * @param Title $title
176 * @return ResultWrapper
177 * @static
178 * @access public
179 */
180 function fetchAllRevisions( &$title ) {
181 return Revision::fetchFromConds(
182 wfGetDB( DB_SLAVE ),
183 array( 'page_namespace' => $title->getNamespace(),
184 'page_title' => $title->getDbkey(),
185 'page_id=rev_page' ) );
186 }
187
188 /**
189 * Return a wrapper for a series of database rows to
190 * fetch all of a given page's revisions in turn.
191 * Each row can be fed to the constructor to get objects.
192 *
193 * @param Title $title
194 * @return ResultWrapper
195 * @static
196 * @access public
197 */
198 function fetchRevision( &$title ) {
199 return Revision::fetchFromConds(
200 wfGetDB( DB_SLAVE ),
201 array( 'rev_id=page_latest',
202 'page_namespace' => $title->getNamespace(),
203 'page_title' => $title->getDbkey(),
204 'page_id=rev_page' ) );
205 }
206
207 /**
208 * Given a set of conditions, return a ResultWrapper
209 * which will return matching database rows with the
210 * fields necessary to build Revision objects.
211 *
212 * @param Database $db
213 * @param array $conditions
214 * @return ResultWrapper
215 * @static
216 * @access private
217 */
218 function fetchFromConds( &$db, $conditions ) {
219 $res = $db->select(
220 array( 'page', 'revision' ),
221 array( 'page_namespace',
222 'page_title',
223 'page_latest',
224 'rev_id',
225 'rev_page',
226 'rev_text_id',
227 'rev_comment',
228 'rev_user_text',
229 'rev_user',
230 'rev_minor_edit',
231 'rev_timestamp',
232 'rev_deleted' ),
233 $conditions,
234 'Revision::fetchRow',
235 array( 'LIMIT' => 1 ) );
236 $ret = $db->resultObject( $res );
237 return $ret;
238 }
239
240 /**
241 * @param object $row
242 * @access private
243 */
244 function Revision( $row ) {
245 if( is_object( $row ) ) {
246 $this->mId = intval( $row->rev_id );
247 $this->mPage = intval( $row->rev_page );
248 $this->mTextId = intval( $row->rev_text_id );
249 $this->mComment = $row->rev_comment;
250 $this->mUserText = $row->rev_user_text;
251 $this->mUser = intval( $row->rev_user );
252 $this->mMinorEdit = intval( $row->rev_minor_edit );
253 $this->mTimestamp = $row->rev_timestamp;
254 $this->mDeleted = intval( $row->rev_deleted );
255
256 if( isset( $row->page_latest ) ) {
257 $this->mCurrent = ( $row->rev_id == $row->page_latest );
258 $this->mTitle = Title::makeTitle( $row->page_namespace,
259 $row->page_title );
260 } else {
261 $this->mCurrent = false;
262 $this->mTitle = null;
263 }
264
265 if( isset( $row->old_text ) ) {
266 $this->mText = $this->getRevisionText( $row );
267 } else {
268 $this->mText = 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 wfDebugDieBacktrace( '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 * @return int
336 */
337 function getPage() {
338 return $this->mPage;
339 }
340
341 /**
342 * Fetch revision's user id if it's available to all users
343 * @return int
344 */
345 function getUser() {
346 if( $this->isDeleted( MW_REV_DELETED_USER ) ) {
347 return 0;
348 } else {
349 return $this->mUser;
350 }
351 }
352
353 /**
354 * Fetch revision's user id without regard for the current user's permissions
355 * @return string
356 */
357 function getRawUser() {
358 return $this->mUser;
359 }
360
361 /**
362 * Fetch revision's username if it's available to all users
363 * @return string
364 */
365 function getUserText() {
366 if( $this->isDeleted( MW_REV_DELETED_USER ) ) {
367 return "";
368 } else {
369 return $this->mUserText;
370 }
371 }
372
373 /**
374 * Fetch revision's username without regard for view restrictions
375 * @return string
376 */
377 function getRawUserText() {
378 return $this->mUserText;
379 }
380
381 /**
382 * Fetch revision comment if it's available to all users
383 * @return string
384 */
385 function getComment() {
386 if( $this->isDeleted( MW_REV_DELETED_COMMENT ) ) {
387 return "";
388 } else {
389 return $this->mComment;
390 }
391 }
392
393 /**
394 * Fetch revision comment without regard for the current user's permissions
395 * @return string
396 */
397 function getRawComment() {
398 return $this->mComment;
399 }
400
401 /**
402 * @return bool
403 */
404 function isMinor() {
405 return (bool)$this->mMinorEdit;
406 }
407
408 /**
409 * int $field one of MW_REV_DELETED_* bitfield constants
410 * @return bool
411 */
412 function isDeleted( $field ) {
413 return ($this->mDeleted & $field) == $field;
414 }
415
416 /**
417 * Fetch revision text if it's available to all users
418 * @return string
419 */
420 function getText() {
421 if( $this->isDeleted( MW_REV_DELETED_TEXT ) ) {
422 return "";
423 } else {
424 return $this->getRawText();
425 }
426 }
427
428 /**
429 * Fetch revision text without regard for view restrictions
430 * @return string
431 */
432 function getRawText() {
433 if( is_null( $this->mText ) ) {
434 // Revision text is immutable. Load on demand:
435 $this->mText = $this->loadText();
436 }
437 return $this->mText;
438 }
439
440 /**
441 * @return string
442 */
443 function getTimestamp() {
444 return wfTimestamp(TS_MW, $this->mTimestamp);
445 }
446
447 /**
448 * @return bool
449 */
450 function isCurrent() {
451 return $this->mCurrent;
452 }
453
454 /**
455 * @return Revision
456 */
457 function getPrevious() {
458 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
459 if ( $prev ) {
460 return Revision::newFromTitle( $this->mTitle, $prev );
461 } else {
462 return null;
463 }
464 }
465
466 /**
467 * @return Revision
468 */
469 function getNext() {
470 $next = $this->mTitle->getNextRevisionID( $this->mId );
471 if ( $next ) {
472 return Revision::newFromTitle( $this->mTitle, $next );
473 } else {
474 return null;
475 }
476 }
477 /**#@-*/
478
479 /**
480 * Get revision text associated with an old or archive row
481 * $row is usually an object from wfFetchRow(), both the flags and the text
482 * field must be included
483 * @static
484 * @param integer $row Id of a row
485 * @param string $prefix table prefix (default 'old_')
486 * @return string $text|false the text requested
487 */
488 function getRevisionText( $row, $prefix = 'old_' ) {
489 $fname = 'Revision::getRevisionText';
490 wfProfileIn( $fname );
491
492 # Get data
493 $textField = $prefix . 'text';
494 $flagsField = $prefix . 'flags';
495
496 if( isset( $row->$flagsField ) ) {
497 $flags = explode( ',', $row->$flagsField );
498 } else {
499 $flags = array();
500 }
501
502 if( isset( $row->$textField ) ) {
503 $text = $row->$textField;
504 } else {
505 wfProfileOut( $fname );
506 return false;
507 }
508
509 # Use external methods for external objects, text in table is URL-only then
510 if ( in_array( 'external', $flags ) ) {
511 $url=$text;
512 @list($proto,$path)=explode('://',$url,2);
513 if ($path=="") {
514 wfProfileOut( $fname );
515 return false;
516 }
517 require_once('ExternalStore.php');
518 $text=ExternalStore::fetchFromURL($url);
519 }
520
521 // If the text was fetched without an error, convert it
522 if ( $text !== false ) {
523 if( in_array( 'gzip', $flags ) ) {
524 # Deal with optional compression of archived pages.
525 # This can be done periodically via maintenance/compressOld.php, and
526 # as pages are saved if $wgCompressRevisions is set.
527 $text = gzinflate( $text );
528 }
529
530 if( in_array( 'object', $flags ) ) {
531 # Generic compressed storage
532 $obj = unserialize( $text );
533 if ( !is_object( $obj ) ) {
534 // Invalid object
535 wfProfileOut( $fname );
536 return false;
537 }
538 $text = $obj->getText();
539 }
540
541 global $wgLegacyEncoding;
542 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
543 # Old revisions kept around in a legacy encoding?
544 # Upconvert on demand.
545 global $wgInputEncoding, $wgContLang;
546 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
547 }
548 }
549 wfProfileOut( $fname );
550 return $text;
551 }
552
553 /**
554 * If $wgCompressRevisions is enabled, we will compress data.
555 * The input string is modified in place.
556 * Return value is the flags field: contains 'gzip' if the
557 * data is compressed, and 'utf-8' if we're saving in UTF-8
558 * mode.
559 *
560 * @static
561 * @param mixed $text reference to a text
562 * @return string
563 */
564 function compressRevisionText( &$text ) {
565 global $wgCompressRevisions;
566 $flags = array();
567
568 # Revisions not marked this way will be converted
569 # on load if $wgLegacyCharset is set in the future.
570 $flags[] = 'utf-8';
571
572 if( $wgCompressRevisions ) {
573 if( function_exists( 'gzdeflate' ) ) {
574 $text = gzdeflate( $text );
575 $flags[] = 'gzip';
576 } else {
577 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
578 }
579 }
580 return implode( ',', $flags );
581 }
582
583 /**
584 * Insert a new revision into the database, returning the new revision ID
585 * number on success and dies horribly on failure.
586 *
587 * @param Database $dbw
588 * @return int
589 */
590 function insertOn( &$dbw ) {
591 global $wgDefaultExternalStore;
592
593 $fname = 'Revision::insertOn';
594 wfProfileIn( $fname );
595
596 $data = $this->mText;
597 $flags = Revision::compressRevisionText( $data );
598
599 # Write to external storage if required
600 if ( $wgDefaultExternalStore ) {
601 if ( is_array( $wgDefaultExternalStore ) ) {
602 // Distribute storage across multiple clusters
603 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
604 } else {
605 $store = $wgDefaultExternalStore;
606 }
607 require_once('ExternalStore.php');
608 // Store and get the URL
609 $data = ExternalStore::insert( $store, $data );
610 if ( !$data ) {
611 # This should only happen in the case of a configuration error, where the external store is not valid
612 wfDebugDieBacktrace( "Unable to store text to external storage $store" );
613 }
614 if ( $flags ) {
615 $flags .= ',';
616 }
617 $flags .= 'external';
618 }
619
620 # Record the text (or external storage URL) to the text table
621 if( !isset( $this->mTextId ) ) {
622 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
623 $dbw->insert( 'text',
624 array(
625 'old_id' => $old_id,
626 'old_text' => $data,
627 'old_flags' => $flags,
628 ), $fname
629 );
630 $this->mTextId = $dbw->insertId();
631 }
632
633 # Record the edit in revisions
634 $rev_id = isset( $this->mId )
635 ? $this->mId
636 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
637 $dbw->insert( 'revision',
638 array(
639 'rev_id' => $rev_id,
640 'rev_page' => $this->mPage,
641 'rev_text_id' => $this->mTextId,
642 'rev_comment' => $this->mComment,
643 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
644 'rev_user' => $this->mUser,
645 'rev_user_text' => $this->mUserText,
646 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
647 'rev_deleted' => $this->mDeleted,
648 ), $fname
649 );
650
651 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
652 wfProfileOut( $fname );
653 return $this->mId;
654 }
655
656 /**
657 * Lazy-load the revision's text.
658 * Currently hardcoded to the 'text' table storage engine.
659 *
660 * @return string
661 * @access private
662 */
663 function loadText() {
664 $fname = 'Revision::loadText';
665 wfProfileIn( $fname );
666
667 $dbr =& wfGetDB( DB_SLAVE );
668 $row = $dbr->selectRow( 'text',
669 array( 'old_text', 'old_flags' ),
670 array( 'old_id' => $this->getTextId() ),
671 $fname);
672
673 if( !$row ) {
674 $dbw =& wfGetDB( DB_MASTER );
675 $row = $dbw->selectRow( 'text',
676 array( 'old_text', 'old_flags' ),
677 array( 'old_id' => $this->getTextId() ),
678 $fname);
679 }
680
681 $text = Revision::getRevisionText( $row );
682 wfProfileOut( $fname );
683
684 return $text;
685 }
686
687 /**
688 * Create a new null-revision for insertion into a page's
689 * history. This will not re-save the text, but simply refer
690 * to the text from the previous version.
691 *
692 * Such revisions can for instance identify page rename
693 * operations and other such meta-modifications.
694 *
695 * @param Database $dbw
696 * @param int $pageId ID number of the page to read from
697 * @param string $summary
698 * @param bool $minor
699 * @return Revision
700 */
701 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
702 $fname = 'Revision::newNullRevision';
703 wfProfileIn( $fname );
704
705 $current = $dbw->selectRow(
706 array( 'page', 'revision' ),
707 array( 'page_latest', 'rev_text_id' ),
708 array(
709 'page_id' => $pageId,
710 'page_latest=rev_id',
711 ),
712 $fname );
713
714 if( $current ) {
715 $revision = new Revision( array(
716 'page' => $pageId,
717 'comment' => $summary,
718 'minor_edit' => $minor,
719 'text_id' => $current->rev_text_id,
720 ) );
721 } else {
722 $revision = null;
723 }
724
725 wfProfileOut( $fname );
726 return $revision;
727 }
728
729 /**
730 * Determine if the current user is allowed to view a particular
731 * field of this revision, if it's marked as deleted.
732 * @param int $field one of MW_REV_DELETED_TEXT,
733 * MW_REV_DELETED_COMMENT,
734 * MW_REV_DELETED_USER
735 * @return bool
736 */
737 function userCan( $field ) {
738 if( ( $this->mDeleted & $field ) == $field ) {
739 global $wgUser;
740 $permission = ( $this->mDeleted & MW_REV_DELETED_RESTRICTED ) == MW_REV_DELETED_RESTRICTED
741 ? 'hiderevision'
742 : 'deleterevision';
743 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
744 return $wgUser->isAllowed( $permission );
745 } else {
746 return true;
747 }
748 }
749
750 }
751
752 ?>