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