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