* Made pageDataFromTitle protected (unused elsewhere)
[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 DatabaseBase
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 public 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 wfDeprecated();
611 return $this->getText( self::FOR_THIS_USER );
612 }
613
614 /**
615 * Fetch revision text without regard for view restrictions
616 *
617 * @return String
618 */
619 public function getRawText() {
620 if( is_null( $this->mText ) ) {
621 // Revision text is immutable. Load on demand:
622 $this->mText = $this->loadText();
623 }
624 return $this->mText;
625 }
626
627 /**
628 * @return String
629 */
630 public function getTimestamp() {
631 return wfTimestamp( TS_MW, $this->mTimestamp );
632 }
633
634 /**
635 * @return Boolean
636 */
637 public function isCurrent() {
638 return $this->mCurrent;
639 }
640
641 /**
642 * Get previous revision for this title
643 *
644 * @return Revision or null
645 */
646 public function getPrevious() {
647 if( $this->getTitle() ) {
648 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
649 if( $prev ) {
650 return Revision::newFromTitle( $this->getTitle(), $prev );
651 }
652 }
653 return null;
654 }
655
656 /**
657 * Get next revision for this title
658 *
659 * @return Revision or null
660 */
661 public function getNext() {
662 if( $this->getTitle() ) {
663 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
664 if ( $next ) {
665 return Revision::newFromTitle( $this->getTitle(), $next );
666 }
667 }
668 return null;
669 }
670
671 /**
672 * Get previous revision Id for this page_id
673 * This is used to populate rev_parent_id on save
674 *
675 * @param $db DatabaseBase
676 * @return Integer
677 */
678 private function getPreviousRevisionId( $db ) {
679 if( is_null( $this->mPage ) ) {
680 return 0;
681 }
682 # Use page_latest if ID is not given
683 if( !$this->mId ) {
684 $prevId = $db->selectField( 'page', 'page_latest',
685 array( 'page_id' => $this->mPage ),
686 __METHOD__ );
687 } else {
688 $prevId = $db->selectField( 'revision', 'rev_id',
689 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
690 __METHOD__,
691 array( 'ORDER BY' => 'rev_id DESC' ) );
692 }
693 return intval( $prevId );
694 }
695
696 /**
697 * Get revision text associated with an old or archive row
698 * $row is usually an object from wfFetchRow(), both the flags and the text
699 * field must be included
700 *
701 * @param $row Object: the text data
702 * @param $prefix String: table prefix (default 'old_')
703 * @return String: text the text requested or false on failure
704 */
705 public static function getRevisionText( $row, $prefix = 'old_' ) {
706 wfProfileIn( __METHOD__ );
707
708 # Get data
709 $textField = $prefix . 'text';
710 $flagsField = $prefix . 'flags';
711
712 if( isset( $row->$flagsField ) ) {
713 $flags = explode( ',', $row->$flagsField );
714 } else {
715 $flags = array();
716 }
717
718 if( isset( $row->$textField ) ) {
719 $text = $row->$textField;
720 } else {
721 wfProfileOut( __METHOD__ );
722 return false;
723 }
724
725 # Use external methods for external objects, text in table is URL-only then
726 if ( in_array( 'external', $flags ) ) {
727 $url = $text;
728 @list(/* $proto */, $path ) = explode( '://', $url, 2 );
729 if( $path == '' ) {
730 wfProfileOut( __METHOD__ );
731 return false;
732 }
733 $text = ExternalStore::fetchFromURL( $url );
734 }
735
736 // If the text was fetched without an error, convert it
737 if ( $text !== false ) {
738 if( in_array( 'gzip', $flags ) ) {
739 # Deal with optional compression of archived pages.
740 # This can be done periodically via maintenance/compressOld.php, and
741 # as pages are saved if $wgCompressRevisions is set.
742 $text = gzinflate( $text );
743 }
744
745 if( in_array( 'object', $flags ) ) {
746 # Generic compressed storage
747 $obj = unserialize( $text );
748 if ( !is_object( $obj ) ) {
749 // Invalid object
750 wfProfileOut( __METHOD__ );
751 return false;
752 }
753 $text = $obj->getText();
754 }
755
756 global $wgLegacyEncoding;
757 if( $text !== false && $wgLegacyEncoding
758 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
759 {
760 # Old revisions kept around in a legacy encoding?
761 # Upconvert on demand.
762 # ("utf8" checked for compatibility with some broken
763 # conversion scripts 2008-12-30)
764 global $wgInputEncoding, $wgContLang;
765 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
766 }
767 }
768 wfProfileOut( __METHOD__ );
769 return $text;
770 }
771
772 /**
773 * If $wgCompressRevisions is enabled, we will compress data.
774 * The input string is modified in place.
775 * Return value is the flags field: contains 'gzip' if the
776 * data is compressed, and 'utf-8' if we're saving in UTF-8
777 * mode.
778 *
779 * @param $text Mixed: reference to a text
780 * @return String
781 */
782 public static function compressRevisionText( &$text ) {
783 global $wgCompressRevisions;
784 $flags = array();
785
786 # Revisions not marked this way will be converted
787 # on load if $wgLegacyCharset is set in the future.
788 $flags[] = 'utf-8';
789
790 if( $wgCompressRevisions ) {
791 if( function_exists( 'gzdeflate' ) ) {
792 $text = gzdeflate( $text );
793 $flags[] = 'gzip';
794 } else {
795 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
796 }
797 }
798 return implode( ',', $flags );
799 }
800
801 /**
802 * Insert a new revision into the database, returning the new revision ID
803 * number on success and dies horribly on failure.
804 *
805 * @param $dbw DatabaseBase: (master connection)
806 * @return Integer
807 */
808 public function insertOn( $dbw ) {
809 global $wgDefaultExternalStore;
810
811 wfProfileIn( __METHOD__ );
812
813 $data = $this->mText;
814 $flags = Revision::compressRevisionText( $data );
815
816 # Write to external storage if required
817 if( $wgDefaultExternalStore ) {
818 // Store and get the URL
819 $data = ExternalStore::insertToDefault( $data );
820 if( !$data ) {
821 throw new MWException( "Unable to store text to external storage" );
822 }
823 if( $flags ) {
824 $flags .= ',';
825 }
826 $flags .= 'external';
827 }
828
829 # Record the text (or external storage URL) to the text table
830 if( !isset( $this->mTextId ) ) {
831 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
832 $dbw->insert( 'text',
833 array(
834 'old_id' => $old_id,
835 'old_text' => $data,
836 'old_flags' => $flags,
837 ), __METHOD__
838 );
839 $this->mTextId = $dbw->insertId();
840 }
841
842 if ( $this->mComment === null ) $this->mComment = "";
843
844 # Record the edit in revisions
845 $rev_id = isset( $this->mId )
846 ? $this->mId
847 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
848 $dbw->insert( 'revision',
849 array(
850 'rev_id' => $rev_id,
851 'rev_page' => $this->mPage,
852 'rev_text_id' => $this->mTextId,
853 'rev_comment' => $this->mComment,
854 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
855 'rev_user' => $this->mUser,
856 'rev_user_text' => $this->mUserText,
857 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
858 'rev_deleted' => $this->mDeleted,
859 'rev_len' => $this->mSize,
860 'rev_parent_id' => is_null($this->mParentId) ?
861 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
862 ), __METHOD__
863 );
864
865 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
866
867 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
868
869 wfProfileOut( __METHOD__ );
870 return $this->mId;
871 }
872
873 /**
874 * Lazy-load the revision's text.
875 * Currently hardcoded to the 'text' table storage engine.
876 *
877 * @return String
878 */
879 protected function loadText() {
880 wfProfileIn( __METHOD__ );
881
882 // Caching may be beneficial for massive use of external storage
883 global $wgRevisionCacheExpiry, $wgMemc;
884 $textId = $this->getTextId();
885 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
886 if( $wgRevisionCacheExpiry ) {
887 $text = $wgMemc->get( $key );
888 if( is_string( $text ) ) {
889 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
890 wfProfileOut( __METHOD__ );
891 return $text;
892 }
893 }
894
895 // If we kept data for lazy extraction, use it now...
896 if ( isset( $this->mTextRow ) ) {
897 $row = $this->mTextRow;
898 $this->mTextRow = null;
899 } else {
900 $row = null;
901 }
902
903 if( !$row ) {
904 // Text data is immutable; check slaves first.
905 $dbr = wfGetDB( DB_SLAVE );
906 $row = $dbr->selectRow( 'text',
907 array( 'old_text', 'old_flags' ),
908 array( 'old_id' => $this->getTextId() ),
909 __METHOD__ );
910 }
911
912 if( !$row && wfGetLB()->getServerCount() > 1 ) {
913 // Possible slave lag!
914 $dbw = wfGetDB( DB_MASTER );
915 $row = $dbw->selectRow( 'text',
916 array( 'old_text', 'old_flags' ),
917 array( 'old_id' => $this->getTextId() ),
918 __METHOD__ );
919 }
920
921 $text = self::getRevisionText( $row );
922
923 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
924 if( $wgRevisionCacheExpiry && $text !== false ) {
925 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
926 }
927
928 wfProfileOut( __METHOD__ );
929
930 return $text;
931 }
932
933 /**
934 * Create a new null-revision for insertion into a page's
935 * history. This will not re-save the text, but simply refer
936 * to the text from the previous version.
937 *
938 * Such revisions can for instance identify page rename
939 * operations and other such meta-modifications.
940 *
941 * @param $dbw DatabaseBase
942 * @param $pageId Integer: ID number of the page to read from
943 * @param $summary String: revision's summary
944 * @param $minor Boolean: whether the revision should be considered as minor
945 * @return Mixed: Revision, or null on error
946 */
947 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
948 wfProfileIn( __METHOD__ );
949
950 $current = $dbw->selectRow(
951 array( 'page', 'revision' ),
952 array( 'page_latest', 'rev_text_id', 'rev_len' ),
953 array(
954 'page_id' => $pageId,
955 'page_latest=rev_id',
956 ),
957 __METHOD__ );
958
959 if( $current ) {
960 $revision = new Revision( array(
961 'page' => $pageId,
962 'comment' => $summary,
963 'minor_edit' => $minor,
964 'text_id' => $current->rev_text_id,
965 'parent_id' => $current->page_latest,
966 'len' => $current->rev_len
967 ) );
968 } else {
969 $revision = null;
970 }
971
972 wfProfileOut( __METHOD__ );
973 return $revision;
974 }
975
976 /**
977 * Determine if the current user is allowed to view a particular
978 * field of this revision, if it's marked as deleted.
979 *
980 * @param $field Integer:one of self::DELETED_TEXT,
981 * self::DELETED_COMMENT,
982 * self::DELETED_USER
983 * @return Boolean
984 */
985 public function userCan( $field ) {
986 return self::userCanBitfield( $this->mDeleted, $field );
987 }
988
989 /**
990 * Determine if the current user is allowed to view a particular
991 * field of this revision, if it's marked as deleted. This is used
992 * by various classes to avoid duplication.
993 *
994 * @param $bitfield Integer: current field
995 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
996 * self::DELETED_COMMENT = File::DELETED_COMMENT,
997 * self::DELETED_USER = File::DELETED_USER
998 * @return Boolean
999 */
1000 public static function userCanBitfield( $bitfield, $field ) {
1001 if( $bitfield & $field ) { // aspect is deleted
1002 global $wgUser;
1003 if ( $bitfield & self::DELETED_RESTRICTED ) {
1004 $permission = 'suppressrevision';
1005 } elseif ( $field & self::DELETED_TEXT ) {
1006 $permission = 'deletedtext';
1007 } else {
1008 $permission = 'deletedhistory';
1009 }
1010 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1011 return $wgUser->isAllowed( $permission );
1012 } else {
1013 return true;
1014 }
1015 }
1016
1017 /**
1018 * Get rev_timestamp from rev_id, without loading the rest of the row
1019 *
1020 * @param $title Title
1021 * @param $id Integer
1022 * @return String
1023 */
1024 static function getTimestampFromId( $title, $id ) {
1025 $dbr = wfGetDB( DB_SLAVE );
1026 // Casting fix for DB2
1027 if ( $id == '' ) {
1028 $id = 0;
1029 }
1030 $conds = array( 'rev_id' => $id );
1031 $conds['rev_page'] = $title->getArticleId();
1032 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1033 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1034 # Not in slave, try master
1035 $dbw = wfGetDB( DB_MASTER );
1036 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1037 }
1038 return wfTimestamp( TS_MW, $timestamp );
1039 }
1040
1041 /**
1042 * Get count of revisions per page...not very efficient
1043 *
1044 * @param $db DatabaseBase
1045 * @param $id Integer: page id
1046 * @return Integer
1047 */
1048 static function countByPageId( $db, $id ) {
1049 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1050 array( 'rev_page' => $id ), __METHOD__ );
1051 if( $row ) {
1052 return $row->revCount;
1053 }
1054 return 0;
1055 }
1056
1057 /**
1058 * Get count of revisions per page...not very efficient
1059 *
1060 * @param $db DatabaseBase
1061 * @param $title Title
1062 * @return Integer
1063 */
1064 static function countByTitle( $db, $title ) {
1065 $id = $title->getArticleId();
1066 if( $id ) {
1067 return Revision::countByPageId( $db, $id );
1068 }
1069 return 0;
1070 }
1071 }
1072
1073 /**
1074 * Aliases for backwards compatibility with 1.6
1075 */
1076 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
1077 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
1078 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
1079 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );