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