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