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