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