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