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