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