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