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