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