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