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