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