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