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