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