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