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