Code cleanups & fixed strange comma
[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 }
403 } else {
404 throw new MWException( 'Revision constructor passed invalid row format.' );
405 }
406 $this->mUnpatrolled = null;
407 }
408
409 /**
410 * Get revision ID
411 *
412 * @return Integer
413 */
414 public function getId() {
415 return $this->mId;
416 }
417
418 /**
419 * Get text row ID
420 *
421 * @return Integer
422 */
423 public function getTextId() {
424 return $this->mTextId;
425 }
426
427 /**
428 * Get parent revision ID (the original previous page revision)
429 *
430 * @return Integer
431 */
432 public function getParentId() {
433 return $this->mParentId;
434 }
435
436 /**
437 * Returns the length of the text in this revision, or null if unknown.
438 *
439 * @return Integer
440 */
441 public function getSize() {
442 return $this->mSize;
443 }
444
445 /**
446 * Returns the title of the page associated with this entry.
447 *
448 * @return Title
449 */
450 public function getTitle() {
451 if( isset( $this->mTitle ) ) {
452 return $this->mTitle;
453 }
454 $dbr = wfGetDB( DB_SLAVE );
455 $row = $dbr->selectRow(
456 array( 'page', 'revision' ),
457 array( 'page_namespace', 'page_title' ),
458 array( 'page_id=rev_page',
459 'rev_id' => $this->mId ),
460 'Revision::getTitle' );
461 if( $row ) {
462 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
463 }
464 return $this->mTitle;
465 }
466
467 /**
468 * Set the title of the revision
469 *
470 * @param $title Title
471 */
472 public function setTitle( $title ) {
473 $this->mTitle = $title;
474 }
475
476 /**
477 * Get the page ID
478 *
479 * @return Integer
480 */
481 public function getPage() {
482 return $this->mPage;
483 }
484
485 /**
486 * Fetch revision's user id if it's available to the specified audience.
487 * If the specified audience does not have access to it, zero will be
488 * returned.
489 *
490 * @param $audience Integer: one of:
491 * Revision::FOR_PUBLIC to be displayed to all users
492 * Revision::FOR_THIS_USER to be displayed to $wgUser
493 * Revision::RAW get the ID regardless of permissions
494 * @param $user User object to check for, only if FOR_THIS_USER is passed
495 * to the $audience parameter
496 * @return Integer
497 */
498 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
499 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
500 return 0;
501 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
502 return 0;
503 } else {
504 return $this->mUser;
505 }
506 }
507
508 /**
509 * Fetch revision's user id without regard for the current user's permissions
510 *
511 * @return String
512 */
513 public function getRawUser() {
514 return $this->mUser;
515 }
516
517 /**
518 * Fetch revision's username if it's available to the specified audience.
519 * If the specified audience does not have access to the username, an
520 * empty string will be returned.
521 *
522 * @param $audience Integer: one of:
523 * Revision::FOR_PUBLIC to be displayed to all users
524 * Revision::FOR_THIS_USER to be displayed to $wgUser
525 * Revision::RAW get the text regardless of permissions
526 * @param $user User object to check for, only if FOR_THIS_USER is passed
527 * to the $audience parameter
528 * @return string
529 */
530 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
531 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
532 return '';
533 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
534 return '';
535 } else {
536 return $this->mUserText;
537 }
538 }
539
540 /**
541 * Fetch revision's username without regard for view restrictions
542 *
543 * @return String
544 */
545 public function getRawUserText() {
546 return $this->mUserText;
547 }
548
549 /**
550 * Fetch revision comment if it's available to the specified audience.
551 * If the specified audience does not have access to the comment, an
552 * empty string will be returned.
553 *
554 * @param $audience Integer: one of:
555 * Revision::FOR_PUBLIC to be displayed to all users
556 * Revision::FOR_THIS_USER to be displayed to $wgUser
557 * Revision::RAW get the text regardless of permissions
558 * @param $user User object to check for, only if FOR_THIS_USER is passed
559 * to the $audience parameter
560 * @return String
561 */
562 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
563 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
564 return '';
565 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
566 return '';
567 } else {
568 return $this->mComment;
569 }
570 }
571
572 /**
573 * Fetch revision comment without regard for the current user's permissions
574 *
575 * @return String
576 */
577 public function getRawComment() {
578 return $this->mComment;
579 }
580
581 /**
582 * @return Boolean
583 */
584 public function isMinor() {
585 return (bool)$this->mMinorEdit;
586 }
587
588 /**
589 * @return Integer rcid of the unpatrolled row, zero if there isn't one
590 */
591 public function isUnpatrolled() {
592 if( $this->mUnpatrolled !== null ) {
593 return $this->mUnpatrolled;
594 }
595 $dbr = wfGetDB( DB_SLAVE );
596 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
597 'rc_id',
598 array( // Add redundant user,timestamp condition so we can use the existing index
599 'rc_user_text' => $this->getRawUserText(),
600 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
601 'rc_this_oldid' => $this->getId(),
602 'rc_patrolled' => 0
603 ),
604 __METHOD__
605 );
606 return (int)$this->mUnpatrolled;
607 }
608
609 /**
610 * @param $field int one of DELETED_* bitfield constants
611 *
612 * @return Boolean
613 */
614 public function isDeleted( $field ) {
615 return ( $this->mDeleted & $field ) == $field;
616 }
617
618 /**
619 * Get the deletion bitfield of the revision
620 *
621 * @return int
622 */
623 public function getVisibility() {
624 return (int)$this->mDeleted;
625 }
626
627 /**
628 * Fetch revision text if it's available to the specified audience.
629 * If the specified audience does not have the ability to view this
630 * revision, an empty string will be returned.
631 *
632 * @param $audience Integer: one of:
633 * Revision::FOR_PUBLIC to be displayed to all users
634 * Revision::FOR_THIS_USER to be displayed to $wgUser
635 * Revision::RAW get the text regardless of permissions
636 * @param $user User object to check for, only if FOR_THIS_USER is passed
637 * to the $audience parameter
638 * @return String
639 */
640 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
641 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
642 return '';
643 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
644 return '';
645 } else {
646 return $this->getRawText();
647 }
648 }
649
650 /**
651 * Alias for getText(Revision::FOR_THIS_USER)
652 *
653 * @deprecated since 1.17
654 * @return String
655 */
656 public function revText() {
657 wfDeprecated( __METHOD__ );
658 return $this->getText( self::FOR_THIS_USER );
659 }
660
661 /**
662 * Fetch revision text without regard for view restrictions
663 *
664 * @return String
665 */
666 public function getRawText() {
667 if( is_null( $this->mText ) ) {
668 // Revision text is immutable. Load on demand:
669 $this->mText = $this->loadText();
670 }
671 return $this->mText;
672 }
673
674 /**
675 * @return String
676 */
677 public function getTimestamp() {
678 return wfTimestamp( TS_MW, $this->mTimestamp );
679 }
680
681 /**
682 * @return Boolean
683 */
684 public function isCurrent() {
685 return $this->mCurrent;
686 }
687
688 /**
689 * Get previous revision for this title
690 *
691 * @return Revision or null
692 */
693 public function getPrevious() {
694 if( $this->getTitle() ) {
695 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
696 if( $prev ) {
697 return Revision::newFromTitle( $this->getTitle(), $prev );
698 }
699 }
700 return null;
701 }
702
703 /**
704 * Get next revision for this title
705 *
706 * @return Revision or null
707 */
708 public function getNext() {
709 if( $this->getTitle() ) {
710 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
711 if ( $next ) {
712 return Revision::newFromTitle( $this->getTitle(), $next );
713 }
714 }
715 return null;
716 }
717
718 /**
719 * Get previous revision Id for this page_id
720 * This is used to populate rev_parent_id on save
721 *
722 * @param $db DatabaseBase
723 * @return Integer
724 */
725 private function getPreviousRevisionId( $db ) {
726 if( is_null( $this->mPage ) ) {
727 return 0;
728 }
729 # Use page_latest if ID is not given
730 if( !$this->mId ) {
731 $prevId = $db->selectField( 'page', 'page_latest',
732 array( 'page_id' => $this->mPage ),
733 __METHOD__ );
734 } else {
735 $prevId = $db->selectField( 'revision', 'rev_id',
736 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
737 __METHOD__,
738 array( 'ORDER BY' => 'rev_id DESC' ) );
739 }
740 return intval( $prevId );
741 }
742
743 /**
744 * Get revision text associated with an old or archive row
745 * $row is usually an object from wfFetchRow(), both the flags and the text
746 * field must be included
747 *
748 * @param $row Object: the text data
749 * @param $prefix String: table prefix (default 'old_')
750 * @return String: text the text requested or false on failure
751 */
752 public static function getRevisionText( $row, $prefix = 'old_' ) {
753 wfProfileIn( __METHOD__ );
754
755 # Get data
756 $textField = $prefix . 'text';
757 $flagsField = $prefix . 'flags';
758
759 if( isset( $row->$flagsField ) ) {
760 $flags = explode( ',', $row->$flagsField );
761 } else {
762 $flags = array();
763 }
764
765 if( isset( $row->$textField ) ) {
766 $text = $row->$textField;
767 } else {
768 wfProfileOut( __METHOD__ );
769 return false;
770 }
771
772 # Use external methods for external objects, text in table is URL-only then
773 if ( in_array( 'external', $flags ) ) {
774 $url = $text;
775 $parts = explode( '://', $url, 2 );
776 if( count( $parts ) == 1 || $parts[1] == '' ) {
777 wfProfileOut( __METHOD__ );
778 return false;
779 }
780 $text = ExternalStore::fetchFromURL( $url );
781 }
782
783 // If the text was fetched without an error, convert it
784 if ( $text !== false ) {
785 if( in_array( 'gzip', $flags ) ) {
786 # Deal with optional compression of archived pages.
787 # This can be done periodically via maintenance/compressOld.php, and
788 # as pages are saved if $wgCompressRevisions is set.
789 $text = gzinflate( $text );
790 }
791
792 if( in_array( 'object', $flags ) ) {
793 # Generic compressed storage
794 $obj = unserialize( $text );
795 if ( !is_object( $obj ) ) {
796 // Invalid object
797 wfProfileOut( __METHOD__ );
798 return false;
799 }
800 $text = $obj->getText();
801 }
802
803 global $wgLegacyEncoding;
804 if( $text !== false && $wgLegacyEncoding
805 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
806 {
807 # Old revisions kept around in a legacy encoding?
808 # Upconvert on demand.
809 # ("utf8" checked for compatibility with some broken
810 # conversion scripts 2008-12-30)
811 global $wgContLang;
812 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
813 }
814 }
815 wfProfileOut( __METHOD__ );
816 return $text;
817 }
818
819 /**
820 * If $wgCompressRevisions is enabled, we will compress data.
821 * The input string is modified in place.
822 * Return value is the flags field: contains 'gzip' if the
823 * data is compressed, and 'utf-8' if we're saving in UTF-8
824 * mode.
825 *
826 * @param $text Mixed: reference to a text
827 * @return String
828 */
829 public static function compressRevisionText( &$text ) {
830 global $wgCompressRevisions;
831 $flags = array();
832
833 # Revisions not marked this way will be converted
834 # on load if $wgLegacyCharset is set in the future.
835 $flags[] = 'utf-8';
836
837 if( $wgCompressRevisions ) {
838 if( function_exists( 'gzdeflate' ) ) {
839 $text = gzdeflate( $text );
840 $flags[] = 'gzip';
841 } else {
842 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
843 }
844 }
845 return implode( ',', $flags );
846 }
847
848 /**
849 * Insert a new revision into the database, returning the new revision ID
850 * number on success and dies horribly on failure.
851 *
852 * @param $dbw DatabaseBase: (master connection)
853 * @return Integer
854 */
855 public function insertOn( $dbw ) {
856 global $wgDefaultExternalStore;
857
858 wfProfileIn( __METHOD__ );
859
860 $data = $this->mText;
861 $flags = Revision::compressRevisionText( $data );
862
863 # Write to external storage if required
864 if( $wgDefaultExternalStore ) {
865 // Store and get the URL
866 $data = ExternalStore::insertToDefault( $data );
867 if( !$data ) {
868 throw new MWException( "Unable to store text to external storage" );
869 }
870 if( $flags ) {
871 $flags .= ',';
872 }
873 $flags .= 'external';
874 }
875
876 # Record the text (or external storage URL) to the text table
877 if( !isset( $this->mTextId ) ) {
878 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
879 $dbw->insert( 'text',
880 array(
881 'old_id' => $old_id,
882 'old_text' => $data,
883 'old_flags' => $flags,
884 ), __METHOD__
885 );
886 $this->mTextId = $dbw->insertId();
887 }
888
889 if ( $this->mComment === null ) $this->mComment = "";
890
891 # Record the edit in revisions
892 $rev_id = isset( $this->mId )
893 ? $this->mId
894 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
895 $dbw->insert( 'revision',
896 array(
897 'rev_id' => $rev_id,
898 'rev_page' => $this->mPage,
899 'rev_text_id' => $this->mTextId,
900 'rev_comment' => $this->mComment,
901 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
902 'rev_user' => $this->mUser,
903 'rev_user_text' => $this->mUserText,
904 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
905 'rev_deleted' => $this->mDeleted,
906 'rev_len' => $this->mSize,
907 'rev_parent_id' => is_null($this->mParentId) ?
908 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
909 ), __METHOD__
910 );
911
912 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
913
914 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
915
916 wfProfileOut( __METHOD__ );
917 return $this->mId;
918 }
919
920 /**
921 * Lazy-load the revision's text.
922 * Currently hardcoded to the 'text' table storage engine.
923 *
924 * @return String
925 */
926 protected function loadText() {
927 wfProfileIn( __METHOD__ );
928
929 // Caching may be beneficial for massive use of external storage
930 global $wgRevisionCacheExpiry, $wgMemc;
931 $textId = $this->getTextId();
932 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
933 if( $wgRevisionCacheExpiry ) {
934 $text = $wgMemc->get( $key );
935 if( is_string( $text ) ) {
936 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
937 wfProfileOut( __METHOD__ );
938 return $text;
939 }
940 }
941
942 // If we kept data for lazy extraction, use it now...
943 if ( isset( $this->mTextRow ) ) {
944 $row = $this->mTextRow;
945 $this->mTextRow = null;
946 } else {
947 $row = null;
948 }
949
950 if( !$row ) {
951 // Text data is immutable; check slaves first.
952 $dbr = wfGetDB( DB_SLAVE );
953 $row = $dbr->selectRow( 'text',
954 array( 'old_text', 'old_flags' ),
955 array( 'old_id' => $this->getTextId() ),
956 __METHOD__ );
957 }
958
959 if( !$row && wfGetLB()->getServerCount() > 1 ) {
960 // Possible slave lag!
961 $dbw = wfGetDB( DB_MASTER );
962 $row = $dbw->selectRow( 'text',
963 array( 'old_text', 'old_flags' ),
964 array( 'old_id' => $this->getTextId() ),
965 __METHOD__ );
966 }
967
968 $text = self::getRevisionText( $row );
969
970 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
971 if( $wgRevisionCacheExpiry && $text !== false ) {
972 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
973 }
974
975 wfProfileOut( __METHOD__ );
976
977 return $text;
978 }
979
980 /**
981 * Create a new null-revision for insertion into a page's
982 * history. This will not re-save the text, but simply refer
983 * to the text from the previous version.
984 *
985 * Such revisions can for instance identify page rename
986 * operations and other such meta-modifications.
987 *
988 * @param $dbw DatabaseBase
989 * @param $pageId Integer: ID number of the page to read from
990 * @param $summary String: revision's summary
991 * @param $minor Boolean: whether the revision should be considered as minor
992 * @return Revision|null on error
993 */
994 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
995 wfProfileIn( __METHOD__ );
996
997 $current = $dbw->selectRow(
998 array( 'page', 'revision' ),
999 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1000 array(
1001 'page_id' => $pageId,
1002 'page_latest=rev_id',
1003 ),
1004 __METHOD__ );
1005
1006 if( $current ) {
1007 $revision = new Revision( array(
1008 'page' => $pageId,
1009 'comment' => $summary,
1010 'minor_edit' => $minor,
1011 'text_id' => $current->rev_text_id,
1012 'parent_id' => $current->page_latest,
1013 'len' => $current->rev_len
1014 ) );
1015 } else {
1016 $revision = null;
1017 }
1018
1019 wfProfileOut( __METHOD__ );
1020 return $revision;
1021 }
1022
1023 /**
1024 * Determine if the current user is allowed to view a particular
1025 * field of this revision, if it's marked as deleted.
1026 *
1027 * @param $field Integer:one of self::DELETED_TEXT,
1028 * self::DELETED_COMMENT,
1029 * self::DELETED_USER
1030 * @param $user User object to check, or null to use $wgUser
1031 * @return Boolean
1032 */
1033 public function userCan( $field, User $user = null ) {
1034 return self::userCanBitfield( $this->mDeleted, $field, $user );
1035 }
1036
1037 /**
1038 * Determine if the current user is allowed to view a particular
1039 * field of this revision, if it's marked as deleted. This is used
1040 * by various classes to avoid duplication.
1041 *
1042 * @param $bitfield Integer: current field
1043 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1044 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1045 * self::DELETED_USER = File::DELETED_USER
1046 * @param $user User object to check, or null to use $wgUser
1047 * @return Boolean
1048 */
1049 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1050 if( $bitfield & $field ) { // aspect is deleted
1051 if ( $bitfield & self::DELETED_RESTRICTED ) {
1052 $permission = 'suppressrevision';
1053 } elseif ( $field & self::DELETED_TEXT ) {
1054 $permission = 'deletedtext';
1055 } else {
1056 $permission = 'deletedhistory';
1057 }
1058 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1059 if ( $user === null ) {
1060 global $wgUser;
1061 $user = $wgUser;
1062 }
1063 return $user->isAllowed( $permission );
1064 } else {
1065 return true;
1066 }
1067 }
1068
1069 /**
1070 * Get rev_timestamp from rev_id, without loading the rest of the row
1071 *
1072 * @param $title Title
1073 * @param $id Integer
1074 * @return String
1075 */
1076 static function getTimestampFromId( $title, $id ) {
1077 $dbr = wfGetDB( DB_SLAVE );
1078 // Casting fix for DB2
1079 if ( $id == '' ) {
1080 $id = 0;
1081 }
1082 $conds = array( 'rev_id' => $id );
1083 $conds['rev_page'] = $title->getArticleId();
1084 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1085 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1086 # Not in slave, try master
1087 $dbw = wfGetDB( DB_MASTER );
1088 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1089 }
1090 return wfTimestamp( TS_MW, $timestamp );
1091 }
1092
1093 /**
1094 * Get count of revisions per page...not very efficient
1095 *
1096 * @param $db DatabaseBase
1097 * @param $id Integer: page id
1098 * @return Integer
1099 */
1100 static function countByPageId( $db, $id ) {
1101 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1102 array( 'rev_page' => $id ), __METHOD__ );
1103 if( $row ) {
1104 return $row->revCount;
1105 }
1106 return 0;
1107 }
1108
1109 /**
1110 * Get count of revisions per page...not very efficient
1111 *
1112 * @param $db DatabaseBase
1113 * @param $title Title
1114 * @return Integer
1115 */
1116 static function countByTitle( $db, $title ) {
1117 $id = $title->getArticleId();
1118 if( $id ) {
1119 return Revision::countByPageId( $db, $id );
1120 }
1121 return 0;
1122 }
1123 }