* Document a bit
[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 * @todo 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 /**
297 * Return the list of page fields that should be selected from page table
298 */
299 static function selectPageFields() {
300 return array(
301 'page_namespace',
302 'page_title',
303 'page_latest'
304 );
305 }
306
307 /**
308 * @param object $row
309 * @access private
310 */
311 function Revision( $row ) {
312 if( is_object( $row ) ) {
313 $this->mId = intval( $row->rev_id );
314 $this->mPage = intval( $row->rev_page );
315 $this->mTextId = intval( $row->rev_text_id );
316 $this->mComment = $row->rev_comment;
317 $this->mUserText = $row->rev_user_text;
318 $this->mUser = intval( $row->rev_user );
319 $this->mMinorEdit = intval( $row->rev_minor_edit );
320 $this->mTimestamp = $row->rev_timestamp;
321 $this->mDeleted = intval( $row->rev_deleted );
322
323 if( !isset( $row->rev_parent_id ) )
324 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
325 else
326 $this->mParentId = intval( $row->rev_parent_id );
327
328 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) )
329 $this->mSize = null;
330 else
331 $this->mSize = intval( $row->rev_len );
332
333 if( isset( $row->page_latest ) ) {
334 $this->mCurrent = ( $row->rev_id == $row->page_latest );
335 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
336 $this->mTitle->resetArticleID( $this->mPage );
337 } else {
338 $this->mCurrent = false;
339 $this->mTitle = null;
340 }
341
342 // Lazy extraction...
343 $this->mText = null;
344 if( isset( $row->old_text ) ) {
345 $this->mTextRow = $row;
346 } else {
347 // 'text' table row entry will be lazy-loaded
348 $this->mTextRow = null;
349 }
350 } elseif( is_array( $row ) ) {
351 // Build a new revision to be saved...
352 global $wgUser;
353
354 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
355 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
356 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
357 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
358 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
359 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
360 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
361 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
362 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
363 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
364
365 // Enforce spacing trimming on supplied text
366 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
367 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
368 $this->mTextRow = null;
369
370 $this->mTitle = null; # Load on demand if needed
371 $this->mCurrent = false;
372 # If we still have no len_size, see it we have the text to figure it out
373 if ( !$this->mSize )
374 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
375 } else {
376 throw new MWException( 'Revision constructor passed invalid row format.' );
377 }
378 $this->mUnpatrolled = null;
379 }
380
381 /**#@+
382 * @access public
383 */
384
385 /**
386 * Get revision ID
387 * @return int
388 */
389 public function getId() {
390 return $this->mId;
391 }
392
393 /**
394 * Get text row ID
395 * @return int
396 */
397 public function getTextId() {
398 return $this->mTextId;
399 }
400
401 /**
402 * Get parent revision ID (the original previous page revision)
403 * @return int
404 */
405 public function getParentId() {
406 return $this->mParentId;
407 }
408
409 /**
410 * Returns the length of the text in this revision, or null if unknown.
411 * @return int
412 */
413 public function getSize() {
414 return $this->mSize;
415 }
416
417 /**
418 * Returns the title of the page associated with this entry.
419 * @return Title
420 */
421 public function getTitle() {
422 if( isset( $this->mTitle ) ) {
423 return $this->mTitle;
424 }
425 $dbr = wfGetDB( DB_SLAVE );
426 $row = $dbr->selectRow(
427 array( 'page', 'revision' ),
428 array( 'page_namespace', 'page_title' ),
429 array( 'page_id=rev_page',
430 'rev_id' => $this->mId ),
431 'Revision::getTitle' );
432 if( $row ) {
433 $this->mTitle = Title::makeTitle( $row->page_namespace,
434 $row->page_title );
435 }
436 return $this->mTitle;
437 }
438
439 /**
440 * Set the title of the revision
441 * @param Title $title
442 */
443 public function setTitle( $title ) {
444 $this->mTitle = $title;
445 }
446
447 /**
448 * Get the page ID
449 * @return int
450 */
451 public function getPage() {
452 return $this->mPage;
453 }
454
455 /**
456 * Fetch revision's user id if it's available to the specified audience.
457 * If the specified audience does not have access to it, zero will be
458 * returned.
459 *
460 * @param integer $audience One of:
461 * Revision::FOR_PUBLIC to be displayed to all users
462 * Revision::FOR_THIS_USER to be displayed to $wgUser
463 * Revision::RAW get the ID regardless of permissions
464 *
465 *
466 * @return int
467 */
468 public function getUser( $audience = self::FOR_PUBLIC ) {
469 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
470 return 0;
471 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
472 return 0;
473 } else {
474 return $this->mUser;
475 }
476 }
477
478 /**
479 * Fetch revision's user id without regard for the current user's permissions
480 * @return string
481 */
482 public function getRawUser() {
483 return $this->mUser;
484 }
485
486 /**
487 * Fetch revision's username if it's available to the specified audience.
488 * If the specified audience does not have access to the username, an
489 * empty string will be returned.
490 *
491 * @param integer $audience One of:
492 * Revision::FOR_PUBLIC to be displayed to all users
493 * Revision::FOR_THIS_USER to be displayed to $wgUser
494 * Revision::RAW get the text regardless of permissions
495 *
496 * @return string
497 */
498 public function getUserText( $audience = self::FOR_PUBLIC ) {
499 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
500 return '';
501 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
502 return '';
503 } else {
504 return $this->mUserText;
505 }
506 }
507
508 /**
509 * Fetch revision's username without regard for view restrictions
510 * @return string
511 */
512 public function getRawUserText() {
513 return $this->mUserText;
514 }
515
516 /**
517 * Fetch revision comment if it's available to the specified audience.
518 * If the specified audience does not have access to the comment, an
519 * empty string will be returned.
520 *
521 * @param integer $audience One of:
522 * Revision::FOR_PUBLIC to be displayed to all users
523 * Revision::FOR_THIS_USER to be displayed to $wgUser
524 * Revision::RAW get the text regardless of permissions
525 *
526 * @return string
527 */
528 function getComment( $audience = self::FOR_PUBLIC ) {
529 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
530 return '';
531 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT ) ) {
532 return '';
533 } else {
534 return $this->mComment;
535 }
536 }
537
538 /**
539 * Fetch revision comment without regard for the current user's permissions
540 * @return string
541 */
542 public function getRawComment() {
543 return $this->mComment;
544 }
545
546 /**
547 * @return bool
548 */
549 public function isMinor() {
550 return (bool)$this->mMinorEdit;
551 }
552
553 /**
554 * @return int rcid of the unpatrolled row, zero if there isn't one
555 */
556 public function isUnpatrolled() {
557 if( $this->mUnpatrolled !== null ) {
558 return $this->mUnpatrolled;
559 }
560 $dbr = wfGetDB( DB_SLAVE );
561 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
562 'rc_id',
563 array( // Add redundant user,timestamp condition so we can use the existing index
564 'rc_user_text' => $this->getRawUserText(),
565 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
566 'rc_this_oldid' => $this->getId(),
567 'rc_patrolled' => 0
568 ),
569 __METHOD__
570 );
571 return (int)$this->mUnpatrolled;
572 }
573
574 /**
575 * int $field one of DELETED_* bitfield constants
576 * @return bool
577 */
578 public function isDeleted( $field ) {
579 return ( $this->mDeleted & $field ) == $field;
580 }
581
582 /**
583 * Get the deletion bitfield of the revision
584 */
585 public function getVisibility() {
586 return (int)$this->mDeleted;
587 }
588
589 /**
590 * Fetch revision text if it's available to the specified audience.
591 * If the specified audience does not have the ability to view this
592 * revision, an empty string will be returned.
593 *
594 * @param integer $audience One of:
595 * Revision::FOR_PUBLIC to be displayed to all users
596 * Revision::FOR_THIS_USER to be displayed to $wgUser
597 * Revision::RAW get the text regardless of permissions
598 *
599 *
600 * @return string
601 */
602 public function getText( $audience = self::FOR_PUBLIC ) {
603 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
604 return '';
605 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT ) ) {
606 return '';
607 } else {
608 return $this->getRawText();
609 }
610 }
611
612 /**
613 * Alias for getText(Revision::FOR_THIS_USER)
614 */
615 public function revText() {
616 return $this->getText( self::FOR_THIS_USER );
617 }
618
619 /**
620 * Fetch revision text without regard for view restrictions
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 bool
640 */
641 public function isCurrent() {
642 return $this->mCurrent;
643 }
644
645 /**
646 * Get previous revision for this title
647 * @return Revision
648 */
649 public function getPrevious() {
650 if( $this->getTitle() ) {
651 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
652 if( $prev ) {
653 return Revision::newFromTitle( $this->getTitle(), $prev );
654 }
655 }
656 return null;
657 }
658
659 /**
660 * @return Revision
661 */
662 public function getNext() {
663 if( $this->getTitle() ) {
664 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
665 if ( $next ) {
666 return Revision::newFromTitle( $this->getTitle(), $next );
667 }
668 }
669 return null;
670 }
671
672 /**
673 * Get previous revision Id for this page_id
674 * This is used to populate rev_parent_id on save
675 * @param Database $db
676 * @return int
677 */
678 private function getPreviousRevisionId( $db ) {
679 if( is_null( $this->mPage ) ) {
680 return 0;
681 }
682 # Use page_latest if ID is not given
683 if( !$this->mId ) {
684 $prevId = $db->selectField( 'page', 'page_latest',
685 array( 'page_id' => $this->mPage ),
686 __METHOD__ );
687 } else {
688 $prevId = $db->selectField( 'revision', 'rev_id',
689 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
690 __METHOD__,
691 array( 'ORDER BY' => 'rev_id DESC' ) );
692 }
693 return intval( $prevId );
694 }
695
696 /**
697 * Get revision text associated with an old or archive row
698 * $row is usually an object from wfFetchRow(), both the flags and the text
699 * field must be included
700 *
701 * @param object $row The text data
702 * @param string $prefix table prefix (default 'old_')
703 * @return string $text|false the text requested
704 */
705 public static function getRevisionText( $row, $prefix = 'old_' ) {
706 wfProfileIn( __METHOD__ );
707
708 # Get data
709 $textField = $prefix . 'text';
710 $flagsField = $prefix . 'flags';
711
712 if( isset( $row->$flagsField ) ) {
713 $flags = explode( ',', $row->$flagsField );
714 } else {
715 $flags = array();
716 }
717
718 if( isset( $row->$textField ) ) {
719 $text = $row->$textField;
720 } else {
721 wfProfileOut( __METHOD__ );
722 return false;
723 }
724
725 # Use external methods for external objects, text in table is URL-only then
726 if ( in_array( 'external', $flags ) ) {
727 $url = $text;
728 @list(/* $proto */, $path ) = explode( '://', $url, 2 );
729 if( $path == '' ) {
730 wfProfileOut( __METHOD__ );
731 return false;
732 }
733 $text = ExternalStore::fetchFromURL( $url );
734 }
735
736 // If the text was fetched without an error, convert it
737 if ( $text !== false ) {
738 if( in_array( 'gzip', $flags ) ) {
739 # Deal with optional compression of archived pages.
740 # This can be done periodically via maintenance/compressOld.php, and
741 # as pages are saved if $wgCompressRevisions is set.
742 $text = gzinflate( $text );
743 }
744
745 if( in_array( 'object', $flags ) ) {
746 # Generic compressed storage
747 $obj = unserialize( $text );
748 if ( !is_object( $obj ) ) {
749 // Invalid object
750 wfProfileOut( __METHOD__ );
751 return false;
752 }
753 $text = $obj->getText();
754 }
755
756 global $wgLegacyEncoding;
757 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) ) {
758 # Old revisions kept around in a legacy encoding?
759 # Upconvert on demand.
760 # ("utf8" checked for compatibility with some broken
761 # conversion scripts 2008-12-30)
762 global $wgInputEncoding, $wgContLang;
763 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
764 }
765 }
766 wfProfileOut( __METHOD__ );
767 return $text;
768 }
769
770 /**
771 * If $wgCompressRevisions is enabled, we will compress data.
772 * The input string is modified in place.
773 * Return value is the flags field: contains 'gzip' if the
774 * data is compressed, and 'utf-8' if we're saving in UTF-8
775 * mode.
776 *
777 * @param mixed $text reference to a text
778 * @return string
779 */
780 public static function compressRevisionText( &$text ) {
781 global $wgCompressRevisions;
782 $flags = array();
783
784 # Revisions not marked this way will be converted
785 # on load if $wgLegacyCharset is set in the future.
786 $flags[] = 'utf-8';
787
788 if( $wgCompressRevisions ) {
789 if( function_exists( 'gzdeflate' ) ) {
790 $text = gzdeflate( $text );
791 $flags[] = 'gzip';
792 } else {
793 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
794 }
795 }
796 return implode( ',', $flags );
797 }
798
799 /**
800 * Insert a new revision into the database, returning the new revision ID
801 * number on success and dies horribly on failure.
802 *
803 * @param Database $dbw
804 * @return int
805 */
806 public function insertOn( $dbw ) {
807 global $wgDefaultExternalStore;
808
809 wfProfileIn( __METHOD__ );
810
811 $data = $this->mText;
812 $flags = Revision::compressRevisionText( $data );
813
814 # Write to external storage if required
815 if( $wgDefaultExternalStore ) {
816 // Store and get the URL
817 $data = ExternalStore::insertToDefault( $data );
818 if( !$data ) {
819 throw new MWException( "Unable to store text to external storage" );
820 }
821 if( $flags ) {
822 $flags .= ',';
823 }
824 $flags .= 'external';
825 }
826
827 # Record the text (or external storage URL) to the text table
828 if( !isset( $this->mTextId ) ) {
829 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
830 $dbw->insert( 'text',
831 array(
832 'old_id' => $old_id,
833 'old_text' => $data,
834 'old_flags' => $flags,
835 ), __METHOD__
836 );
837 $this->mTextId = $dbw->insertId();
838 }
839
840 # Record the edit in revisions
841 $rev_id = isset( $this->mId )
842 ? $this->mId
843 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
844 $dbw->insert( 'revision',
845 array(
846 'rev_id' => $rev_id,
847 'rev_page' => $this->mPage,
848 'rev_text_id' => $this->mTextId,
849 'rev_comment' => $this->mComment,
850 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
851 'rev_user' => $this->mUser,
852 'rev_user_text' => $this->mUserText,
853 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
854 'rev_deleted' => $this->mDeleted,
855 'rev_len' => $this->mSize,
856 'rev_parent_id' => is_null($this->mParentId) ?
857 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
858 ), __METHOD__
859 );
860
861 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
862
863 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
864
865 wfProfileOut( __METHOD__ );
866 return $this->mId;
867 }
868
869 /**
870 * Lazy-load the revision's text.
871 * Currently hardcoded to the 'text' table storage engine.
872 *
873 * @return string
874 */
875 protected function loadText() {
876 wfProfileIn( __METHOD__ );
877
878 // Caching may be beneficial for massive use of external storage
879 global $wgRevisionCacheExpiry, $wgMemc;
880 $textId = $this->getTextId();
881 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
882 if( $wgRevisionCacheExpiry ) {
883 $text = $wgMemc->get( $key );
884 if( is_string( $text ) ) {
885 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
886 wfProfileOut( __METHOD__ );
887 return $text;
888 }
889 }
890
891 // If we kept data for lazy extraction, use it now...
892 if ( isset( $this->mTextRow ) ) {
893 $row = $this->mTextRow;
894 $this->mTextRow = null;
895 } else {
896 $row = null;
897 }
898
899 if( !$row ) {
900 // Text data is immutable; check slaves first.
901 $dbr = wfGetDB( DB_SLAVE );
902 $row = $dbr->selectRow( 'text',
903 array( 'old_text', 'old_flags' ),
904 array( 'old_id' => $this->getTextId() ),
905 __METHOD__ );
906 }
907
908 if( !$row && wfGetLB()->getServerCount() > 1 ) {
909 // Possible slave lag!
910 $dbw = wfGetDB( DB_MASTER );
911 $row = $dbw->selectRow( 'text',
912 array( 'old_text', 'old_flags' ),
913 array( 'old_id' => $this->getTextId() ),
914 __METHOD__ );
915 }
916
917 $text = self::getRevisionText( $row );
918
919 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
920 if( $wgRevisionCacheExpiry && $text !== false ) {
921 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
922 }
923
924 wfProfileOut( __METHOD__ );
925
926 return $text;
927 }
928
929 /**
930 * Create a new null-revision for insertion into a page's
931 * history. This will not re-save the text, but simply refer
932 * to the text from the previous version.
933 *
934 * Such revisions can for instance identify page rename
935 * operations and other such meta-modifications.
936 *
937 * @param Database $dbw
938 * @param int $pageId ID number of the page to read from
939 * @param string $summary
940 * @param bool $minor
941 * @return mixed Revision, or null on error
942 */
943 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
944 wfProfileIn( __METHOD__ );
945
946 $current = $dbw->selectRow(
947 array( 'page', 'revision' ),
948 array( 'page_latest', 'rev_text_id', 'rev_len' ),
949 array(
950 'page_id' => $pageId,
951 'page_latest=rev_id',
952 ),
953 __METHOD__ );
954
955 if( $current ) {
956 $revision = new Revision( array(
957 'page' => $pageId,
958 'comment' => $summary,
959 'minor_edit' => $minor,
960 'text_id' => $current->rev_text_id,
961 'parent_id' => $current->page_latest,
962 'len' => $current->rev_len
963 ) );
964 } else {
965 $revision = null;
966 }
967
968 wfProfileOut( __METHOD__ );
969 return $revision;
970 }
971
972 /**
973 * Determine if the current user is allowed to view a particular
974 * field of this revision, if it's marked as deleted.
975 * @param int $field one of self::DELETED_TEXT,
976 * self::DELETED_COMMENT,
977 * self::DELETED_USER
978 * @return bool
979 */
980 public function userCan( $field ) {
981 return self::userCanBitfield( $this->mDeleted, $field );
982 }
983
984 /**
985 * Determine if the current user is allowed to view a particular
986 * field of this revision, if it's marked as deleted. This is used
987 * by various classes to avoid duplication.
988 * @param int $bitfield (current field)
989 * @param int $field one of self::DELETED_TEXT = File::DELETED_FILE,
990 * self::DELETED_COMMENT = File::DELETED_COMMENT,
991 * self::DELETED_USER = File::DELETED_USER
992 * @return bool
993 */
994 public static function userCanBitfield( $bitfield, $field ) {
995 if( $bitfield & $field ) { // aspect is deleted
996 global $wgUser;
997 $permission = '';
998 if ( $bitfield & self::DELETED_RESTRICTED ) {
999 $permission = 'suppressrevision';
1000 } elseif ( $field & self::DELETED_TEXT ) {
1001 $permission = 'deletedtext';
1002 } else {
1003 $permission = 'deletedhistory';
1004 }
1005 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1006 return $wgUser->isAllowed( $permission );
1007 } else {
1008 return true;
1009 }
1010 }
1011
1012 /**
1013 * Get rev_timestamp from rev_id, without loading the rest of the row
1014 * @param Title $title
1015 * @param integer $id
1016 */
1017 static function getTimestampFromId( $title, $id ) {
1018 $dbr = wfGetDB( DB_SLAVE );
1019 // Casting fix for DB2
1020 if ( $id == '' ) {
1021 $id = 0;
1022 }
1023 $conds = array( 'rev_id' => $id );
1024 $conds['rev_page'] = $title->getArticleId();
1025 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1026 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1027 # Not in slave, try master
1028 $dbw = wfGetDB( DB_MASTER );
1029 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1030 }
1031 return wfTimestamp( TS_MW, $timestamp );
1032 }
1033
1034 /**
1035 * Get count of revisions per page...not very efficient
1036 * @param Database $db
1037 * @param int $id, page id
1038 */
1039 static function countByPageId( $db, $id ) {
1040 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1041 array( 'rev_page' => $id ), __METHOD__ );
1042 if( $row ) {
1043 return $row->revCount;
1044 }
1045 return 0;
1046 }
1047
1048 /**
1049 * Get count of revisions per page...not very efficient
1050 * @param Database $db
1051 * @param Title $title
1052 */
1053 static function countByTitle( $db, $title ) {
1054 $id = $title->getArticleId();
1055 if( $id ) {
1056 return Revision::countByPageId( $db, $id );
1057 }
1058 return 0;
1059 }
1060 }
1061
1062 /**
1063 * Aliases for backwards compatibility with 1.6
1064 */
1065 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
1066 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
1067 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
1068 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );