* Added User paremeter to Revision::userCan(), Revision::userCanBitfield(), LogEvents...
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2
3 /**
4 * @todo document
5 */
6 class Revision {
7 const DELETED_TEXT = 1;
8 const DELETED_COMMENT = 2;
9 const DELETED_USER = 4;
10 const DELETED_RESTRICTED = 8;
11 // Convenience field
12 const SUPPRESSED_USER = 12;
13 // Audience options for Revision::getText()
14 const FOR_PUBLIC = 1;
15 const FOR_THIS_USER = 2;
16 const RAW = 3;
17
18 /**
19 * Load a page revision from a given revision ID number.
20 * Returns null if no such revision can be found.
21 *
22 * @param $id Integer
23 * @return Revision or null
24 */
25 public static function newFromId( $id ) {
26 return Revision::newFromConds(
27 array( 'page_id=rev_page',
28 'rev_id' => intval( $id ) ) );
29 }
30
31 /**
32 * Load either the current, or a specified, revision
33 * that's attached to a given title. If not attached
34 * to that title, will return null.
35 *
36 * @param $title Title
37 * @param $id Integer (optional)
38 * @return Revision or null
39 */
40 public static function newFromTitle( $title, $id = 0 ) {
41 $conds = array(
42 'page_namespace' => $title->getNamespace(),
43 'page_title' => $title->getDBkey()
44 );
45 if ( $id ) {
46 // Use the specified ID
47 $conds['rev_id'] = $id;
48 } elseif ( wfGetLB()->getServerCount() > 1 ) {
49 // Get the latest revision ID from the master
50 $dbw = wfGetDB( DB_MASTER );
51 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
52 if ( $latest === false ) {
53 return null; // page does not exist
54 }
55 $conds['rev_id'] = $latest;
56 } else {
57 // Use a join to get the latest revision
58 $conds[] = 'rev_id=page_latest';
59 }
60 $conds[] = 'page_id=rev_page';
61 return Revision::newFromConds( $conds );
62 }
63
64 /**
65 * Load either the current, or a specified, revision
66 * that's attached to a given page ID.
67 * Returns null if no such revision can be found.
68 *
69 * @param $revId Integer
70 * @param $pageId Integer (optional)
71 * @return Revision or null
72 */
73 public static function newFromPageId( $pageId, $revId = 0 ) {
74 $conds = array( 'page_id' => $pageId );
75 if ( $revId ) {
76 $conds['rev_id'] = $revId;
77 } elseif ( wfGetLB()->getServerCount() > 1 ) {
78 // Get the latest revision ID from the master
79 $dbw = wfGetDB( DB_MASTER );
80 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
81 if ( $latest === false ) {
82 return null; // page does not exist
83 }
84 $conds['rev_id'] = $latest;
85 } else {
86 $conds[] = 'rev_id = page_latest';
87 }
88 $conds[] = 'page_id=rev_page';
89 return Revision::newFromConds( $conds );
90 }
91
92 /**
93 * Make a fake revision object from an archive table row. This is queried
94 * for permissions or even inserted (as in Special:Undelete)
95 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
96 *
97 * @param $row
98 * @param $overrides array
99 *
100 * @return Revision
101 */
102 public static function newFromArchiveRow( $row, $overrides = array() ) {
103 $attribs = $overrides + array(
104 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
105 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
106 'comment' => $row->ar_comment,
107 'user' => $row->ar_user,
108 'user_text' => $row->ar_user_text,
109 'timestamp' => $row->ar_timestamp,
110 'minor_edit' => $row->ar_minor_edit,
111 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
112 'deleted' => $row->ar_deleted,
113 'len' => $row->ar_len
114 );
115 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
116 // Pre-1.5 ar_text row
117 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
118 if ( $attribs['text'] === false ) {
119 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
120 }
121 }
122 return new self( $attribs );
123 }
124
125 /**
126 * @since 1.19
127 *
128 * @param $row
129 * @return Revision
130 */
131 public static function newFromRow( $row ) {
132 return new self( $row );
133 }
134
135 /**
136 * Load a page revision from a given revision ID number.
137 * Returns null if no such revision can be found.
138 *
139 * @param $db DatabaseBase
140 * @param $id Integer
141 * @return Revision or null
142 */
143 public static function loadFromId( $db, $id ) {
144 return Revision::loadFromConds( $db,
145 array( 'page_id=rev_page',
146 'rev_id' => intval( $id ) ) );
147 }
148
149 /**
150 * Load either the current, or a specified, revision
151 * that's attached to a given page. If not attached
152 * to that page, will return null.
153 *
154 * @param $db DatabaseBase
155 * @param $pageid Integer
156 * @param $id Integer
157 * @return Revision or null
158 */
159 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
160 $conds = array( 'page_id=rev_page','rev_page' => intval( $pageid ), 'page_id'=>intval( $pageid ) );
161 if( $id ) {
162 $conds['rev_id'] = intval( $id );
163 } else {
164 $conds[] = 'rev_id=page_latest';
165 }
166 return Revision::loadFromConds( $db, $conds );
167 }
168
169 /**
170 * Load either the current, or a specified, revision
171 * that's attached to a given page. If not attached
172 * to that page, will return null.
173 *
174 * @param $db DatabaseBase
175 * @param $title Title
176 * @param $id Integer
177 * @return Revision or null
178 */
179 public static function loadFromTitle( $db, $title, $id = 0 ) {
180 if( $id ) {
181 $matchId = intval( $id );
182 } else {
183 $matchId = 'page_latest';
184 }
185 return Revision::loadFromConds(
186 $db,
187 array( "rev_id=$matchId",
188 'page_id=rev_page',
189 'page_namespace' => $title->getNamespace(),
190 'page_title' => $title->getDBkey() ) );
191 }
192
193 /**
194 * Load the revision for the given title with the given timestamp.
195 * WARNING: Timestamps may in some circumstances not be unique,
196 * so this isn't the best key to use.
197 *
198 * @param $db DatabaseBase
199 * @param $title Title
200 * @param $timestamp String
201 * @return Revision or null
202 */
203 public static function loadFromTimestamp( $db, $title, $timestamp ) {
204 return Revision::loadFromConds(
205 $db,
206 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
207 'page_id=rev_page',
208 'page_namespace' => $title->getNamespace(),
209 'page_title' => $title->getDBkey() ) );
210 }
211
212 /**
213 * Given a set of conditions, fetch a revision.
214 *
215 * @param $conditions Array
216 * @return Revision or null
217 */
218 public static function newFromConds( $conditions ) {
219 $db = wfGetDB( DB_SLAVE );
220 $row = Revision::loadFromConds( $db, $conditions );
221 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
222 $dbw = wfGetDB( DB_MASTER );
223 $row = Revision::loadFromConds( $dbw, $conditions );
224 }
225 return $row;
226 }
227
228 /**
229 * Given a set of conditions, fetch a revision from
230 * the given database connection.
231 *
232 * @param $db DatabaseBase
233 * @param $conditions Array
234 * @return Revision or null
235 */
236 private static function loadFromConds( $db, $conditions ) {
237 $res = Revision::fetchFromConds( $db, $conditions );
238 if( $res ) {
239 $row = $res->fetchObject();
240 $res->free();
241 if( $row ) {
242 $ret = new Revision( $row );
243 return $ret;
244 }
245 }
246 $ret = null;
247 return $ret;
248 }
249
250 /**
251 * Return a wrapper for a series of database rows to
252 * fetch all of a given page's revisions in turn.
253 * Each row can be fed to the constructor to get objects.
254 *
255 * @param $title Title
256 * @return ResultWrapper
257 */
258 public static function fetchRevision( $title ) {
259 return Revision::fetchFromConds(
260 wfGetDB( DB_SLAVE ),
261 array( 'rev_id=page_latest',
262 'page_namespace' => $title->getNamespace(),
263 'page_title' => $title->getDBkey(),
264 'page_id=rev_page' ) );
265 }
266
267 /**
268 * Given a set of conditions, return a ResultWrapper
269 * which will return matching database rows with the
270 * fields necessary to build Revision objects.
271 *
272 * @param $db DatabaseBase
273 * @param $conditions Array
274 * @return ResultWrapper
275 */
276 private static function fetchFromConds( $db, $conditions ) {
277 $fields = self::selectFields();
278 $fields[] = 'page_namespace';
279 $fields[] = 'page_title';
280 $fields[] = 'page_latest';
281 return $db->select(
282 array( 'page', 'revision' ),
283 $fields,
284 $conditions,
285 __METHOD__,
286 array( 'LIMIT' => 1 ) );
287 }
288
289 /**
290 * Return the list of revision fields that should be selected to create
291 * a new revision.
292 */
293 public static function selectFields() {
294 return array(
295 'rev_id',
296 'rev_page',
297 'rev_text_id',
298 'rev_timestamp',
299 'rev_comment',
300 'rev_user_text,'.
301 'rev_user',
302 'rev_minor_edit',
303 'rev_deleted',
304 'rev_len',
305 'rev_parent_id'
306 );
307 }
308
309 /**
310 * Return the list of text fields that should be selected to read the
311 * revision text
312 */
313 static function selectTextFields() {
314 return array(
315 'old_text',
316 'old_flags'
317 );
318 }
319
320 /**
321 * Return the list of page fields that should be selected from page table
322 */
323 static function selectPageFields() {
324 return array(
325 'page_namespace',
326 'page_title',
327 'page_latest'
328 );
329 }
330
331 /**
332 * Constructor
333 *
334 * @param $row Mixed: either a database row or an array
335 * @access private
336 */
337 function __construct( $row ) {
338 if( is_object( $row ) ) {
339 $this->mId = intval( $row->rev_id );
340 $this->mPage = intval( $row->rev_page );
341 $this->mTextId = intval( $row->rev_text_id );
342 $this->mComment = $row->rev_comment;
343 $this->mUserText = $row->rev_user_text;
344 $this->mUser = intval( $row->rev_user );
345 $this->mMinorEdit = intval( $row->rev_minor_edit );
346 $this->mTimestamp = $row->rev_timestamp;
347 $this->mDeleted = intval( $row->rev_deleted );
348
349 if( !isset( $row->rev_parent_id ) ) {
350 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
351 } else {
352 $this->mParentId = intval( $row->rev_parent_id );
353 }
354
355 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) ) {
356 $this->mSize = null;
357 } else {
358 $this->mSize = intval( $row->rev_len );
359 }
360
361 if( isset( $row->page_latest ) ) {
362 $this->mCurrent = ( $row->rev_id == $row->page_latest );
363 $this->mTitle = Title::newFromRow( $row );
364 } else {
365 $this->mCurrent = false;
366 $this->mTitle = null;
367 }
368
369 // Lazy extraction...
370 $this->mText = null;
371 if( isset( $row->old_text ) ) {
372 $this->mTextRow = $row;
373 } else {
374 // 'text' table row entry will be lazy-loaded
375 $this->mTextRow = null;
376 }
377 } elseif( is_array( $row ) ) {
378 // Build a new revision to be saved...
379 global $wgUser;
380
381 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
382 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
383 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
384 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
385 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
386 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
387 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
388 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
389 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
390 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
391
392 // Enforce spacing trimming on supplied text
393 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
394 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
395 $this->mTextRow = null;
396
397 $this->mTitle = null; # Load on demand if needed
398 $this->mCurrent = false;
399 # If we still have no len_size, see it we have the text to figure it out
400 if ( !$this->mSize )
401 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
402 } else {
403 throw new MWException( 'Revision constructor passed invalid row format.' );
404 }
405 $this->mUnpatrolled = null;
406 }
407
408 /**
409 * Get revision ID
410 *
411 * @return Integer
412 */
413 public function getId() {
414 return $this->mId;
415 }
416
417 /**
418 * Get text row ID
419 *
420 * @return Integer
421 */
422 public function getTextId() {
423 return $this->mTextId;
424 }
425
426 /**
427 * Get parent revision ID (the original previous page revision)
428 *
429 * @return Integer
430 */
431 public function getParentId() {
432 return $this->mParentId;
433 }
434
435 /**
436 * Returns the length of the text in this revision, or null if unknown.
437 *
438 * @return Integer
439 */
440 public function getSize() {
441 return $this->mSize;
442 }
443
444 /**
445 * Returns the title of the page associated with this entry.
446 *
447 * @return Title
448 */
449 public function getTitle() {
450 if( isset( $this->mTitle ) ) {
451 return $this->mTitle;
452 }
453 $dbr = wfGetDB( DB_SLAVE );
454 $row = $dbr->selectRow(
455 array( 'page', 'revision' ),
456 array( 'page_namespace', 'page_title' ),
457 array( 'page_id=rev_page',
458 'rev_id' => $this->mId ),
459 'Revision::getTitle' );
460 if( $row ) {
461 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
462 }
463 return $this->mTitle;
464 }
465
466 /**
467 * Set the title of the revision
468 *
469 * @param $title Title
470 */
471 public function setTitle( $title ) {
472 $this->mTitle = $title;
473 }
474
475 /**
476 * Get the page ID
477 *
478 * @return Integer
479 */
480 public function getPage() {
481 return $this->mPage;
482 }
483
484 /**
485 * Fetch revision's user id if it's available to the specified audience.
486 * If the specified audience does not have access to it, zero will be
487 * returned.
488 *
489 * @param $audience Integer: one of:
490 * Revision::FOR_PUBLIC to be displayed to all users
491 * Revision::FOR_THIS_USER to be displayed to $wgUser
492 * Revision::RAW get the ID regardless of permissions
493 * @param $user User object to check for, only if FOR_THIS_USER is passed
494 * to the $audience parameter
495 * @return Integer
496 */
497 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
498 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
499 return 0;
500 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
501 return 0;
502 } else {
503 return $this->mUser;
504 }
505 }
506
507 /**
508 * Fetch revision's user id without regard for the current user's permissions
509 *
510 * @return String
511 */
512 public function getRawUser() {
513 return $this->mUser;
514 }
515
516 /**
517 * Fetch revision's username if it's available to the specified audience.
518 * If the specified audience does not have access to the username, an
519 * empty string will be returned.
520 *
521 * @param $audience Integer: one of:
522 * Revision::FOR_PUBLIC to be displayed to all users
523 * Revision::FOR_THIS_USER to be displayed to $wgUser
524 * Revision::RAW get the text regardless of permissions
525 * @param $user User object to check for, only if FOR_THIS_USER is passed
526 * to the $audience parameter
527 * @return string
528 */
529 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
530 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
531 return '';
532 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
533 return '';
534 } else {
535 return $this->mUserText;
536 }
537 }
538
539 /**
540 * Fetch revision's username without regard for view restrictions
541 *
542 * @return String
543 */
544 public function getRawUserText() {
545 return $this->mUserText;
546 }
547
548 /**
549 * Fetch revision comment if it's available to the specified audience.
550 * If the specified audience does not have access to the comment, an
551 * empty string will be returned.
552 *
553 * @param $audience Integer: one of:
554 * Revision::FOR_PUBLIC to be displayed to all users
555 * Revision::FOR_THIS_USER to be displayed to $wgUser
556 * Revision::RAW get the text regardless of permissions
557 * @param $user User object to check for, only if FOR_THIS_USER is passed
558 * to the $audience parameter
559 * @return String
560 */
561 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
562 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
563 return '';
564 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
565 return '';
566 } else {
567 return $this->mComment;
568 }
569 }
570
571 /**
572 * Fetch revision comment without regard for the current user's permissions
573 *
574 * @return String
575 */
576 public function getRawComment() {
577 return $this->mComment;
578 }
579
580 /**
581 * @return Boolean
582 */
583 public function isMinor() {
584 return (bool)$this->mMinorEdit;
585 }
586
587 /**
588 * @return Integer rcid of the unpatrolled row, zero if there isn't one
589 */
590 public function isUnpatrolled() {
591 if( $this->mUnpatrolled !== null ) {
592 return $this->mUnpatrolled;
593 }
594 $dbr = wfGetDB( DB_SLAVE );
595 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
596 'rc_id',
597 array( // Add redundant user,timestamp condition so we can use the existing index
598 'rc_user_text' => $this->getRawUserText(),
599 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
600 'rc_this_oldid' => $this->getId(),
601 'rc_patrolled' => 0
602 ),
603 __METHOD__
604 );
605 return (int)$this->mUnpatrolled;
606 }
607
608 /**
609 * @param $field int one of DELETED_* bitfield constants
610 *
611 * @return Boolean
612 */
613 public function isDeleted( $field ) {
614 return ( $this->mDeleted & $field ) == $field;
615 }
616
617 /**
618 * Get the deletion bitfield of the revision
619 *
620 * @return int
621 */
622 public function getVisibility() {
623 return (int)$this->mDeleted;
624 }
625
626 /**
627 * Fetch revision text if it's available to the specified audience.
628 * If the specified audience does not have the ability to view this
629 * revision, an empty string will be returned.
630 *
631 * @param $audience Integer: one of:
632 * Revision::FOR_PUBLIC to be displayed to all users
633 * Revision::FOR_THIS_USER to be displayed to $wgUser
634 * Revision::RAW get the text regardless of permissions
635 * @param $user User object to check for, only if FOR_THIS_USER is passed
636 * to the $audience parameter
637 * @return String
638 */
639 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
640 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
641 return '';
642 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
643 return '';
644 } else {
645 return $this->getRawText();
646 }
647 }
648
649 /**
650 * Alias for getText(Revision::FOR_THIS_USER)
651 *
652 * @deprecated since 1.17
653 * @return String
654 */
655 public function revText() {
656 wfDeprecated( __METHOD__ );
657 return $this->getText( self::FOR_THIS_USER );
658 }
659
660 /**
661 * Fetch revision text without regard for view restrictions
662 *
663 * @return String
664 */
665 public function getRawText() {
666 if( is_null( $this->mText ) ) {
667 // Revision text is immutable. Load on demand:
668 $this->mText = $this->loadText();
669 }
670 return $this->mText;
671 }
672
673 /**
674 * @return String
675 */
676 public function getTimestamp() {
677 return wfTimestamp( TS_MW, $this->mTimestamp );
678 }
679
680 /**
681 * @return Boolean
682 */
683 public function isCurrent() {
684 return $this->mCurrent;
685 }
686
687 /**
688 * Get previous revision for this title
689 *
690 * @return Revision or null
691 */
692 public function getPrevious() {
693 if( $this->getTitle() ) {
694 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
695 if( $prev ) {
696 return Revision::newFromTitle( $this->getTitle(), $prev );
697 }
698 }
699 return null;
700 }
701
702 /**
703 * Get next revision for this title
704 *
705 * @return Revision or null
706 */
707 public function getNext() {
708 if( $this->getTitle() ) {
709 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
710 if ( $next ) {
711 return Revision::newFromTitle( $this->getTitle(), $next );
712 }
713 }
714 return null;
715 }
716
717 /**
718 * Get previous revision Id for this page_id
719 * This is used to populate rev_parent_id on save
720 *
721 * @param $db DatabaseBase
722 * @return Integer
723 */
724 private function getPreviousRevisionId( $db ) {
725 if( is_null( $this->mPage ) ) {
726 return 0;
727 }
728 # Use page_latest if ID is not given
729 if( !$this->mId ) {
730 $prevId = $db->selectField( 'page', 'page_latest',
731 array( 'page_id' => $this->mPage ),
732 __METHOD__ );
733 } else {
734 $prevId = $db->selectField( 'revision', 'rev_id',
735 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
736 __METHOD__,
737 array( 'ORDER BY' => 'rev_id DESC' ) );
738 }
739 return intval( $prevId );
740 }
741
742 /**
743 * Get revision text associated with an old or archive row
744 * $row is usually an object from wfFetchRow(), both the flags and the text
745 * field must be included
746 *
747 * @param $row Object: the text data
748 * @param $prefix String: table prefix (default 'old_')
749 * @return String: text the text requested or false on failure
750 */
751 public static function getRevisionText( $row, $prefix = 'old_' ) {
752 wfProfileIn( __METHOD__ );
753
754 # Get data
755 $textField = $prefix . 'text';
756 $flagsField = $prefix . 'flags';
757
758 if( isset( $row->$flagsField ) ) {
759 $flags = explode( ',', $row->$flagsField );
760 } else {
761 $flags = array();
762 }
763
764 if( isset( $row->$textField ) ) {
765 $text = $row->$textField;
766 } else {
767 wfProfileOut( __METHOD__ );
768 return false;
769 }
770
771 # Use external methods for external objects, text in table is URL-only then
772 if ( in_array( 'external', $flags ) ) {
773 $url = $text;
774 $parts = explode( '://', $url, 2 );
775 if( count( $parts ) == 1 || $parts[1] == '' ) {
776 wfProfileOut( __METHOD__ );
777 return false;
778 }
779 $text = ExternalStore::fetchFromURL( $url );
780 }
781
782 // If the text was fetched without an error, convert it
783 if ( $text !== false ) {
784 if( in_array( 'gzip', $flags ) ) {
785 # Deal with optional compression of archived pages.
786 # This can be done periodically via maintenance/compressOld.php, and
787 # as pages are saved if $wgCompressRevisions is set.
788 $text = gzinflate( $text );
789 }
790
791 if( in_array( 'object', $flags ) ) {
792 # Generic compressed storage
793 $obj = unserialize( $text );
794 if ( !is_object( $obj ) ) {
795 // Invalid object
796 wfProfileOut( __METHOD__ );
797 return false;
798 }
799 $text = $obj->getText();
800 }
801
802 global $wgLegacyEncoding;
803 if( $text !== false && $wgLegacyEncoding
804 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
805 {
806 # Old revisions kept around in a legacy encoding?
807 # Upconvert on demand.
808 # ("utf8" checked for compatibility with some broken
809 # conversion scripts 2008-12-30)
810 global $wgContLang;
811 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
812 }
813 }
814 wfProfileOut( __METHOD__ );
815 return $text;
816 }
817
818 /**
819 * If $wgCompressRevisions is enabled, we will compress data.
820 * The input string is modified in place.
821 * Return value is the flags field: contains 'gzip' if the
822 * data is compressed, and 'utf-8' if we're saving in UTF-8
823 * mode.
824 *
825 * @param $text Mixed: reference to a text
826 * @return String
827 */
828 public static function compressRevisionText( &$text ) {
829 global $wgCompressRevisions;
830 $flags = array();
831
832 # Revisions not marked this way will be converted
833 # on load if $wgLegacyCharset is set in the future.
834 $flags[] = 'utf-8';
835
836 if( $wgCompressRevisions ) {
837 if( function_exists( 'gzdeflate' ) ) {
838 $text = gzdeflate( $text );
839 $flags[] = 'gzip';
840 } else {
841 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
842 }
843 }
844 return implode( ',', $flags );
845 }
846
847 /**
848 * Insert a new revision into the database, returning the new revision ID
849 * number on success and dies horribly on failure.
850 *
851 * @param $dbw DatabaseBase: (master connection)
852 * @return Integer
853 */
854 public function insertOn( $dbw ) {
855 global $wgDefaultExternalStore;
856
857 wfProfileIn( __METHOD__ );
858
859 $data = $this->mText;
860 $flags = Revision::compressRevisionText( $data );
861
862 # Write to external storage if required
863 if( $wgDefaultExternalStore ) {
864 // Store and get the URL
865 $data = ExternalStore::insertToDefault( $data );
866 if( !$data ) {
867 throw new MWException( "Unable to store text to external storage" );
868 }
869 if( $flags ) {
870 $flags .= ',';
871 }
872 $flags .= 'external';
873 }
874
875 # Record the text (or external storage URL) to the text table
876 if( !isset( $this->mTextId ) ) {
877 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
878 $dbw->insert( 'text',
879 array(
880 'old_id' => $old_id,
881 'old_text' => $data,
882 'old_flags' => $flags,
883 ), __METHOD__
884 );
885 $this->mTextId = $dbw->insertId();
886 }
887
888 if ( $this->mComment === null ) $this->mComment = "";
889
890 # Record the edit in revisions
891 $rev_id = isset( $this->mId )
892 ? $this->mId
893 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
894 $dbw->insert( 'revision',
895 array(
896 'rev_id' => $rev_id,
897 'rev_page' => $this->mPage,
898 'rev_text_id' => $this->mTextId,
899 'rev_comment' => $this->mComment,
900 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
901 'rev_user' => $this->mUser,
902 'rev_user_text' => $this->mUserText,
903 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
904 'rev_deleted' => $this->mDeleted,
905 'rev_len' => $this->mSize,
906 'rev_parent_id' => is_null($this->mParentId) ?
907 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
908 ), __METHOD__
909 );
910
911 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
912
913 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
914
915 wfProfileOut( __METHOD__ );
916 return $this->mId;
917 }
918
919 /**
920 * Lazy-load the revision's text.
921 * Currently hardcoded to the 'text' table storage engine.
922 *
923 * @return String
924 */
925 protected function loadText() {
926 wfProfileIn( __METHOD__ );
927
928 // Caching may be beneficial for massive use of external storage
929 global $wgRevisionCacheExpiry, $wgMemc;
930 $textId = $this->getTextId();
931 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
932 if( $wgRevisionCacheExpiry ) {
933 $text = $wgMemc->get( $key );
934 if( is_string( $text ) ) {
935 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
936 wfProfileOut( __METHOD__ );
937 return $text;
938 }
939 }
940
941 // If we kept data for lazy extraction, use it now...
942 if ( isset( $this->mTextRow ) ) {
943 $row = $this->mTextRow;
944 $this->mTextRow = null;
945 } else {
946 $row = null;
947 }
948
949 if( !$row ) {
950 // Text data is immutable; check slaves first.
951 $dbr = wfGetDB( DB_SLAVE );
952 $row = $dbr->selectRow( 'text',
953 array( 'old_text', 'old_flags' ),
954 array( 'old_id' => $this->getTextId() ),
955 __METHOD__ );
956 }
957
958 if( !$row && wfGetLB()->getServerCount() > 1 ) {
959 // Possible slave lag!
960 $dbw = wfGetDB( DB_MASTER );
961 $row = $dbw->selectRow( 'text',
962 array( 'old_text', 'old_flags' ),
963 array( 'old_id' => $this->getTextId() ),
964 __METHOD__ );
965 }
966
967 $text = self::getRevisionText( $row );
968
969 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
970 if( $wgRevisionCacheExpiry && $text !== false ) {
971 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
972 }
973
974 wfProfileOut( __METHOD__ );
975
976 return $text;
977 }
978
979 /**
980 * Create a new null-revision for insertion into a page's
981 * history. This will not re-save the text, but simply refer
982 * to the text from the previous version.
983 *
984 * Such revisions can for instance identify page rename
985 * operations and other such meta-modifications.
986 *
987 * @param $dbw DatabaseBase
988 * @param $pageId Integer: ID number of the page to read from
989 * @param $summary String: revision's summary
990 * @param $minor Boolean: whether the revision should be considered as minor
991 * @return Revision|null on error
992 */
993 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
994 wfProfileIn( __METHOD__ );
995
996 $current = $dbw->selectRow(
997 array( 'page', 'revision' ),
998 array( 'page_latest', 'rev_text_id', 'rev_len' ),
999 array(
1000 'page_id' => $pageId,
1001 'page_latest=rev_id',
1002 ),
1003 __METHOD__ );
1004
1005 if( $current ) {
1006 $revision = new Revision( array(
1007 'page' => $pageId,
1008 'comment' => $summary,
1009 'minor_edit' => $minor,
1010 'text_id' => $current->rev_text_id,
1011 'parent_id' => $current->page_latest,
1012 'len' => $current->rev_len
1013 ) );
1014 } else {
1015 $revision = null;
1016 }
1017
1018 wfProfileOut( __METHOD__ );
1019 return $revision;
1020 }
1021
1022 /**
1023 * Determine if the current user is allowed to view a particular
1024 * field of this revision, if it's marked as deleted.
1025 *
1026 * @param $field Integer:one of self::DELETED_TEXT,
1027 * self::DELETED_COMMENT,
1028 * self::DELETED_USER
1029 * @param $user User object to check, or null to use $wgUser
1030 * @return Boolean
1031 */
1032 public function userCan( $field, User $user = null ) {
1033 return self::userCanBitfield( $this->mDeleted, $field, $user );
1034 }
1035
1036 /**
1037 * Determine if the current user is allowed to view a particular
1038 * field of this revision, if it's marked as deleted. This is used
1039 * by various classes to avoid duplication.
1040 *
1041 * @param $bitfield Integer: current field
1042 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1043 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1044 * self::DELETED_USER = File::DELETED_USER
1045 * @param $user User object to check, or null to use $wgUser
1046 * @return Boolean
1047 */
1048 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1049 if( $bitfield & $field ) { // aspect is deleted
1050 if ( $bitfield & self::DELETED_RESTRICTED ) {
1051 $permission = 'suppressrevision';
1052 } elseif ( $field & self::DELETED_TEXT ) {
1053 $permission = 'deletedtext';
1054 } else {
1055 $permission = 'deletedhistory';
1056 }
1057 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1058 if ( $user === null ) {
1059 global $wgUser;
1060 $user = $wgUser;
1061 }
1062 return $user->isAllowed( $permission );
1063 } else {
1064 return true;
1065 }
1066 }
1067
1068 /**
1069 * Get rev_timestamp from rev_id, without loading the rest of the row
1070 *
1071 * @param $title Title
1072 * @param $id Integer
1073 * @return String
1074 */
1075 static function getTimestampFromId( $title, $id ) {
1076 $dbr = wfGetDB( DB_SLAVE );
1077 // Casting fix for DB2
1078 if ( $id == '' ) {
1079 $id = 0;
1080 }
1081 $conds = array( 'rev_id' => $id );
1082 $conds['rev_page'] = $title->getArticleId();
1083 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1084 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1085 # Not in slave, try master
1086 $dbw = wfGetDB( DB_MASTER );
1087 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1088 }
1089 return wfTimestamp( TS_MW, $timestamp );
1090 }
1091
1092 /**
1093 * Get count of revisions per page...not very efficient
1094 *
1095 * @param $db DatabaseBase
1096 * @param $id Integer: page id
1097 * @return Integer
1098 */
1099 static function countByPageId( $db, $id ) {
1100 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1101 array( 'rev_page' => $id ), __METHOD__ );
1102 if( $row ) {
1103 return $row->revCount;
1104 }
1105 return 0;
1106 }
1107
1108 /**
1109 * Get count of revisions per page...not very efficient
1110 *
1111 * @param $db DatabaseBase
1112 * @param $title Title
1113 * @return Integer
1114 */
1115 static function countByTitle( $db, $title ) {
1116 $id = $title->getArticleId();
1117 if( $id ) {
1118 return Revision::countByPageId( $db, $id );
1119 }
1120 return 0;
1121 }
1122 }