[bug 37746] string ids for content model and format.
[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 = strval( $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 = strval( $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'] ) ? strval( $row['content_model'] ) : null;
546 $this->mContentFormat = isset( $row['content_format'] ) ? strval( $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 $this->mTitle = isset( $row['title'] ) ? $row['title'] : null;
554
555 // if we have a Content object, override mText and mContentModel
556 if ( !empty( $row['content'] ) ) {
557 $handler = $this->getContentHandler();
558 $this->mContent = $row['content'];
559
560 $this->mContentModel = $this->mContent->getModel();
561 $this->mContentHandler = null;
562
563 $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
564 } elseif ( !is_null( $this->mText ) ) {
565 $handler = $this->getContentHandler();
566 $this->mContent = $handler->unserializeContent( $this->mText );
567 }
568
569 // if we have a Title object, override mPage. Useful for testing and convenience.
570 if ( isset( $row['title'] ) ) {
571 $this->mTitle = $row['title'];
572 $this->mPage = $this->mTitle->getArticleID();
573 } else {
574 $this->mTitle = null; // Load on demand if needed
575 }
576
577 $this->mCurrent = false; // @todo: XXX: really? we are about to create a revision. it will usually then be the current one.
578
579 // If we still have no length, see it we have the text to figure it out
580 if ( !$this->mSize ) {
581 if ( !is_null( $this->mContent ) ) {
582 $this->mSize = $this->mContent->getSize();
583 } else {
584 #NOTE: this should never happen if we have either text or content object!
585 $this->mSize = null;
586 }
587 }
588
589 // Same for sha1
590 if ( $this->mSha1 === null ) {
591 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
592 }
593
594 // force lazy init
595 $this->getContentModel();
596 $this->getContentFormat();
597 } else {
598 throw new MWException( 'Revision constructor passed invalid row format.' );
599 }
600 $this->mUnpatrolled = null;
601 }
602
603 /**
604 * Get revision ID
605 *
606 * @return Integer|null
607 */
608 public function getId() {
609 return $this->mId;
610 }
611
612 /**
613 * Set the revision ID
614 *
615 * @since 1.19
616 * @param $id Integer
617 */
618 public function setId( $id ) {
619 $this->mId = $id;
620 }
621
622 /**
623 * Get text row ID
624 *
625 * @return Integer|null
626 */
627 public function getTextId() {
628 return $this->mTextId;
629 }
630
631 /**
632 * Get parent revision ID (the original previous page revision)
633 *
634 * @return Integer|null
635 */
636 public function getParentId() {
637 return $this->mParentId;
638 }
639
640 /**
641 * Returns the length of the text in this revision, or null if unknown.
642 *
643 * @return Integer|null
644 */
645 public function getSize() {
646 return $this->mSize;
647 }
648
649 /**
650 * Returns the base36 sha1 of the text in this revision, or null if unknown.
651 *
652 * @return String|null
653 */
654 public function getSha1() {
655 return $this->mSha1;
656 }
657
658 /**
659 * Returns the title of the page associated with this entry or null.
660 *
661 * Will do a query, when title is not set and id is given.
662 *
663 * @return Title|null
664 */
665 public function getTitle() {
666 if( isset( $this->mTitle ) ) {
667 return $this->mTitle;
668 }
669 if( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
670 $dbr = wfGetDB( DB_SLAVE );
671 $row = $dbr->selectRow(
672 array( 'page', 'revision' ),
673 self::selectPageFields(),
674 array( 'page_id=rev_page',
675 'rev_id' => $this->mId ),
676 __METHOD__ );
677 if ( $row ) {
678 $this->mTitle = Title::newFromRow( $row );
679 }
680 }
681
682 if ( !$this->mTitle && !is_null( $this->mPage ) && $this->mPage > 0 ) {
683 $this->mTitle = Title::newFromID( $this->mPage );
684 }
685
686 return $this->mTitle;
687 }
688
689 /**
690 * Set the title of the revision
691 *
692 * @param $title Title
693 */
694 public function setTitle( $title ) {
695 $this->mTitle = $title;
696 }
697
698 /**
699 * Get the page ID
700 *
701 * @return Integer|null
702 */
703 public function getPage() {
704 return $this->mPage;
705 }
706
707 /**
708 * Fetch revision's user id if it's available to the specified audience.
709 * If the specified audience does not have access to it, zero will be
710 * returned.
711 *
712 * @param $audience Integer: one of:
713 * Revision::FOR_PUBLIC to be displayed to all users
714 * Revision::FOR_THIS_USER to be displayed to the given user
715 * Revision::RAW get the ID regardless of permissions
716 * @param $user User object to check for, only if FOR_THIS_USER is passed
717 * to the $audience parameter
718 * @return Integer
719 */
720 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
721 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
722 return 0;
723 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
724 return 0;
725 } else {
726 return $this->mUser;
727 }
728 }
729
730 /**
731 * Fetch revision's user id without regard for the current user's permissions
732 *
733 * @return String
734 */
735 public function getRawUser() {
736 return $this->mUser;
737 }
738
739 /**
740 * Fetch revision's username if it's available to the specified audience.
741 * If the specified audience does not have access to the username, an
742 * empty string will be returned.
743 *
744 * @param $audience Integer: one of:
745 * Revision::FOR_PUBLIC to be displayed to all users
746 * Revision::FOR_THIS_USER to be displayed to the given user
747 * Revision::RAW get the text regardless of permissions
748 * @param $user User object to check for, only if FOR_THIS_USER is passed
749 * to the $audience parameter
750 * @return string
751 */
752 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
753 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
754 return '';
755 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
756 return '';
757 } else {
758 return $this->getRawUserText();
759 }
760 }
761
762 /**
763 * Fetch revision's username without regard for view restrictions
764 *
765 * @return String
766 */
767 public function getRawUserText() {
768 if ( $this->mUserText === null ) {
769 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
770 if ( $this->mUserText === false ) {
771 # This shouldn't happen, but it can if the wiki was recovered
772 # via importing revs and there is no user table entry yet.
773 $this->mUserText = $this->mOrigUserText;
774 }
775 }
776 return $this->mUserText;
777 }
778
779 /**
780 * Fetch revision comment if it's available to the specified audience.
781 * If the specified audience does not have access to the comment, an
782 * empty string will be returned.
783 *
784 * @param $audience Integer: one of:
785 * Revision::FOR_PUBLIC to be displayed to all users
786 * Revision::FOR_THIS_USER to be displayed to the given user
787 * Revision::RAW get the text regardless of permissions
788 * @param $user User object to check for, only if FOR_THIS_USER is passed
789 * to the $audience parameter
790 * @return String
791 */
792 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
793 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
794 return '';
795 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
796 return '';
797 } else {
798 return $this->mComment;
799 }
800 }
801
802 /**
803 * Fetch revision comment without regard for the current user's permissions
804 *
805 * @return String
806 */
807 public function getRawComment() {
808 return $this->mComment;
809 }
810
811 /**
812 * @return Boolean
813 */
814 public function isMinor() {
815 return (bool)$this->mMinorEdit;
816 }
817
818 /**
819 * @return Integer rcid of the unpatrolled row, zero if there isn't one
820 */
821 public function isUnpatrolled() {
822 if( $this->mUnpatrolled !== null ) {
823 return $this->mUnpatrolled;
824 }
825 $dbr = wfGetDB( DB_SLAVE );
826 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
827 'rc_id',
828 array( // Add redundant user,timestamp condition so we can use the existing index
829 'rc_user_text' => $this->getRawUserText(),
830 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
831 'rc_this_oldid' => $this->getId(),
832 'rc_patrolled' => 0
833 ),
834 __METHOD__
835 );
836 return (int)$this->mUnpatrolled;
837 }
838
839 /**
840 * @param $field int one of DELETED_* bitfield constants
841 *
842 * @return Boolean
843 */
844 public function isDeleted( $field ) {
845 return ( $this->mDeleted & $field ) == $field;
846 }
847
848 /**
849 * Get the deletion bitfield of the revision
850 *
851 * @return int
852 */
853 public function getVisibility() {
854 return (int)$this->mDeleted;
855 }
856
857 /**
858 * Fetch revision text if it's available to the specified audience.
859 * If the specified audience does not have the ability to view this
860 * revision, an empty string will be returned.
861 *
862 * @param $audience Integer: one of:
863 * Revision::FOR_PUBLIC to be displayed to all users
864 * Revision::FOR_THIS_USER to be displayed to the given user
865 * Revision::RAW get the text regardless of permissions
866 * @param $user User object to check for, only if FOR_THIS_USER is passed
867 * to the $audience parameter
868 * @return String
869 * @deprecated in 1.WD, use getContent() instead
870 * @todo: replace usage in core
871 */
872 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
873 wfDeprecated( __METHOD__, '1.WD' );
874
875 $content = $this->getContent( $audience, $user );
876 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
877 }
878
879 /**
880 * Fetch revision content if it's available to the specified audience.
881 * If the specified audience does not have the ability to view this
882 * revision, null will be returned.
883 *
884 * @param $audience Integer: one of:
885 * Revision::FOR_PUBLIC to be displayed to all users
886 * Revision::FOR_THIS_USER to be displayed to $wgUser
887 * Revision::RAW get the text regardless of permissions
888 * @param $user User object to check for, only if FOR_THIS_USER is passed
889 * to the $audience parameter
890 * @return Content
891 *
892 * @since 1.WD
893 */
894 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
895 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
896 return null;
897 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
898 return null;
899 } else {
900 return $this->getContentInternal();
901 }
902 }
903
904 /**
905 * Alias for getText(Revision::FOR_THIS_USER)
906 *
907 * @deprecated since 1.17
908 * @return String
909 */
910 public function revText() {
911 wfDeprecated( __METHOD__, '1.17' );
912 return $this->getText( self::FOR_THIS_USER );
913 }
914
915 /**
916 * Fetch revision text without regard for view restrictions
917 *
918 * @return String
919 *
920 * @deprecated since 1.WD. Instead, use Revision::getContent( Revision::RAW ) or Revision::getSerializedData() as appropriate.
921 */
922 public function getRawText() {
923 wfDeprecated( __METHOD__, "1.WD" );
924
925 return $this->getText( self::RAW );
926 }
927
928 /**
929 * Fetch original serialized data without regard for view restrictions
930 *
931 * @return String
932 *
933 * @since 1.WD
934 */
935 public function getSerializedData() {
936 return $this->mText;
937 }
938
939 protected function getContentInternal() {
940 if( is_null( $this->mContent ) ) {
941 // Revision is immutable. Load on demand:
942
943 $handler = $this->getContentHandler();
944 $format = $this->getContentFormat();
945 $title = $this->getTitle();
946
947 if( is_null( $this->mText ) ) {
948 // Load text on demand:
949 $this->mText = $this->loadText();
950 }
951
952 $this->mContent = is_null( $this->mText ) ? null : $handler->unserializeContent( $this->mText, $format );
953 }
954
955 return $this->mContent;
956 }
957
958 /**
959 * Returns the content model for this revision.
960 *
961 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
962 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
963 * is used as a last resort.
964 *
965 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
966 **/
967 public function getContentModel() {
968 if ( !$this->mContentModel ) {
969 $title = $this->getTitle();
970 $this->mContentModel = ( $title ? $title->getContentModel() : CONTENT_MODEL_WIKITEXT );
971
972 assert( !empty( $this->mContentModel ) );
973 }
974
975 return $this->mContentModel;
976 }
977
978 /**
979 * Returns the content format for this revision.
980 *
981 * If no content format was stored in the database, the default format for this
982 * revision's content model is returned.
983 *
984 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
985 **/
986 public function getContentFormat() {
987 if ( !$this->mContentFormat ) {
988 $handler = $this->getContentHandler();
989 $this->mContentFormat = $handler->getDefaultFormat();
990
991 assert( !empty( $this->mContentFormat ) );
992 }
993
994 return $this->mContentFormat;
995 }
996
997 /**
998 * Returns the content handler appropriate for this revision's content model.
999 *
1000 * @return ContentHandler
1001 */
1002 public function getContentHandler() {
1003 if ( !$this->mContentHandler ) {
1004 $model = $this->getContentModel();
1005 $this->mContentHandler = ContentHandler::getForModelID( $model );
1006
1007 $format = $this->getContentFormat();
1008
1009 if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
1010 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1011 }
1012 }
1013
1014 return $this->mContentHandler;
1015 }
1016
1017 /**
1018 * @return String
1019 */
1020 public function getTimestamp() {
1021 return wfTimestamp( TS_MW, $this->mTimestamp );
1022 }
1023
1024 /**
1025 * @return Boolean
1026 */
1027 public function isCurrent() {
1028 return $this->mCurrent;
1029 }
1030
1031 /**
1032 * Get previous revision for this title
1033 *
1034 * @return Revision or null
1035 */
1036 public function getPrevious() {
1037 if( $this->getTitle() ) {
1038 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1039 if( $prev ) {
1040 return Revision::newFromTitle( $this->getTitle(), $prev );
1041 }
1042 }
1043 return null;
1044 }
1045
1046 /**
1047 * Get next revision for this title
1048 *
1049 * @return Revision or null
1050 */
1051 public function getNext() {
1052 if( $this->getTitle() ) {
1053 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1054 if ( $next ) {
1055 return Revision::newFromTitle( $this->getTitle(), $next );
1056 }
1057 }
1058 return null;
1059 }
1060
1061 /**
1062 * Get previous revision Id for this page_id
1063 * This is used to populate rev_parent_id on save
1064 *
1065 * @param $db DatabaseBase
1066 * @return Integer
1067 */
1068 private function getPreviousRevisionId( $db ) {
1069 if( is_null( $this->mPage ) ) {
1070 return 0;
1071 }
1072 # Use page_latest if ID is not given
1073 if( !$this->mId ) {
1074 $prevId = $db->selectField( 'page', 'page_latest',
1075 array( 'page_id' => $this->mPage ),
1076 __METHOD__ );
1077 } else {
1078 $prevId = $db->selectField( 'revision', 'rev_id',
1079 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
1080 __METHOD__,
1081 array( 'ORDER BY' => 'rev_id DESC' ) );
1082 }
1083 return intval( $prevId );
1084 }
1085
1086 /**
1087 * Get revision text associated with an old or archive row
1088 * $row is usually an object from wfFetchRow(), both the flags and the text
1089 * field must be included
1090 *
1091 * @param $row Object: the text data
1092 * @param $prefix String: table prefix (default 'old_')
1093 * @return String: text the text requested or false on failure
1094 */
1095 public static function getRevisionText( $row, $prefix = 'old_' ) {
1096 wfProfileIn( __METHOD__ );
1097
1098 # Get data
1099 $textField = $prefix . 'text';
1100 $flagsField = $prefix . 'flags';
1101
1102 if( isset( $row->$flagsField ) ) {
1103 $flags = explode( ',', $row->$flagsField );
1104 } else {
1105 $flags = array();
1106 }
1107
1108 if( isset( $row->$textField ) ) {
1109 $text = $row->$textField;
1110 } else {
1111 wfProfileOut( __METHOD__ );
1112 return false;
1113 }
1114
1115 # Use external methods for external objects, text in table is URL-only then
1116 if ( in_array( 'external', $flags ) ) {
1117 $url = $text;
1118 $parts = explode( '://', $url, 2 );
1119 if( count( $parts ) == 1 || $parts[1] == '' ) {
1120 wfProfileOut( __METHOD__ );
1121 return false;
1122 }
1123 $text = ExternalStore::fetchFromURL( $url );
1124 }
1125
1126 // If the text was fetched without an error, convert it
1127 if ( $text !== false ) {
1128 if( in_array( 'gzip', $flags ) ) {
1129 # Deal with optional compression of archived pages.
1130 # This can be done periodically via maintenance/compressOld.php, and
1131 # as pages are saved if $wgCompressRevisions is set.
1132 $text = gzinflate( $text );
1133 }
1134
1135 if( in_array( 'object', $flags ) ) {
1136 # Generic compressed storage
1137 $obj = unserialize( $text );
1138 if ( !is_object( $obj ) ) {
1139 // Invalid object
1140 wfProfileOut( __METHOD__ );
1141 return false;
1142 }
1143 $text = $obj->getText();
1144 }
1145
1146 global $wgLegacyEncoding;
1147 if( $text !== false && $wgLegacyEncoding
1148 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1149 {
1150 # Old revisions kept around in a legacy encoding?
1151 # Upconvert on demand.
1152 # ("utf8" checked for compatibility with some broken
1153 # conversion scripts 2008-12-30)
1154 global $wgContLang;
1155 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1156 }
1157 }
1158 wfProfileOut( __METHOD__ );
1159 return $text;
1160 }
1161
1162 /**
1163 * If $wgCompressRevisions is enabled, we will compress data.
1164 * The input string is modified in place.
1165 * Return value is the flags field: contains 'gzip' if the
1166 * data is compressed, and 'utf-8' if we're saving in UTF-8
1167 * mode.
1168 *
1169 * @param $text Mixed: reference to a text
1170 * @return String
1171 */
1172 public static function compressRevisionText( &$text ) {
1173 global $wgCompressRevisions;
1174 $flags = array();
1175
1176 # Revisions not marked this way will be converted
1177 # on load if $wgLegacyCharset is set in the future.
1178 $flags[] = 'utf-8';
1179
1180 if( $wgCompressRevisions ) {
1181 if( function_exists( 'gzdeflate' ) ) {
1182 $text = gzdeflate( $text );
1183 $flags[] = 'gzip';
1184 } else {
1185 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
1186 }
1187 }
1188 return implode( ',', $flags );
1189 }
1190
1191 /**
1192 * Insert a new revision into the database, returning the new revision ID
1193 * number on success and dies horribly on failure.
1194 *
1195 * @param $dbw DatabaseBase: (master connection)
1196 * @return Integer
1197 */
1198 public function insertOn( $dbw ) {
1199 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1200
1201 wfProfileIn( __METHOD__ );
1202
1203 $this->checkContentModel();
1204
1205 $data = $this->mText;
1206 $flags = Revision::compressRevisionText( $data );
1207
1208 # Write to external storage if required
1209 if( $wgDefaultExternalStore ) {
1210 // Store and get the URL
1211 $data = ExternalStore::insertToDefault( $data );
1212 if( !$data ) {
1213 throw new MWException( "Unable to store text to external storage" );
1214 }
1215 if( $flags ) {
1216 $flags .= ',';
1217 }
1218 $flags .= 'external';
1219 }
1220
1221 # Record the text (or external storage URL) to the text table
1222 if( !isset( $this->mTextId ) ) {
1223 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1224 $dbw->insert( 'text',
1225 array(
1226 'old_id' => $old_id,
1227 'old_text' => $data,
1228 'old_flags' => $flags,
1229 ), __METHOD__
1230 );
1231 $this->mTextId = $dbw->insertId();
1232 }
1233
1234 if ( $this->mComment === null ) $this->mComment = "";
1235
1236 # Record the edit in revisions
1237 $rev_id = isset( $this->mId )
1238 ? $this->mId
1239 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1240
1241 $row = array(
1242 'rev_id' => $rev_id,
1243 'rev_page' => $this->mPage,
1244 'rev_text_id' => $this->mTextId,
1245 'rev_comment' => $this->mComment,
1246 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1247 'rev_user' => $this->mUser,
1248 'rev_user_text' => $this->mUserText,
1249 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1250 'rev_deleted' => $this->mDeleted,
1251 'rev_len' => $this->mSize,
1252 'rev_parent_id' => is_null( $this->mParentId )
1253 ? $this->getPreviousRevisionId( $dbw )
1254 : $this->mParentId,
1255 'rev_sha1' => is_null( $this->mSha1 )
1256 ? Revision::base36Sha1( $this->mText )
1257 : $this->mSha1,
1258 );
1259
1260 if ( $wgContentHandlerUseDB ) {
1261 //NOTE: Store null for the default model and format, to save space.
1262 //XXX: Makes the DB sensitive to changed defaults. Make this behaviour optional? Only in miser mode?
1263
1264 $model = $this->getContentModel();
1265 $format = $this->getContentFormat();
1266
1267 $defaultModel = ContentHandler::getDefaultModelFor( $this->getTitle() );
1268 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
1269
1270 $row[ 'rev_content_model' ] = ( $model === $defaultModel ) ? null : $model;
1271 $row[ 'rev_content_format' ] = ( $format === $defaultFormat ) ? null : $format;
1272 }
1273
1274 $dbw->insert( 'revision', $row, __METHOD__ );
1275
1276 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1277
1278 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1279
1280 wfProfileOut( __METHOD__ );
1281 return $this->mId;
1282 }
1283
1284 protected function checkContentModel() {
1285 global $wgContentHandlerUseDB;
1286
1287 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1288
1289 $model = $this->getContentModel();
1290 $format = $this->getContentFormat();
1291 $handler = $this->getContentHandler();
1292
1293 if ( !$handler->isSupportedFormat( $format ) ) {
1294 $t = $title->getPrefixedDBkey();
1295
1296 throw new MWException( "Can't use format $format with content model $model on $t" );
1297 }
1298
1299 if ( !$wgContentHandlerUseDB && $title ) {
1300 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1301
1302 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1303 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1304 $defaultFormat = $defaultHandler->getDefaultFormat();
1305
1306 if ( $this->getContentModel() != $defaultModel ) {
1307 $t = $title->getPrefixedDBkey();
1308
1309 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: model is $model , default for $t is $defaultModel" );
1310 }
1311
1312 if ( $this->getContentFormat() != $defaultFormat ) {
1313 $t = $title->getPrefixedDBkey();
1314
1315 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: format is $format, default for $t is $defaultFormat" );
1316 }
1317 }
1318
1319 $content = $this->getContent( Revision::RAW );
1320
1321 if ( !$content->isValid() ) {
1322 $t = $title->getPrefixedDBkey();
1323
1324 throw new MWException( "Content of $t is not valid! Content model is $model" );
1325 }
1326 }
1327
1328 /**
1329 * Get the base 36 SHA-1 value for a string of text
1330 * @param $text String
1331 * @return String
1332 */
1333 public static function base36Sha1( $text ) {
1334 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1335 }
1336
1337 /**
1338 * Lazy-load the revision's text.
1339 * Currently hardcoded to the 'text' table storage engine.
1340 *
1341 * @return String
1342 */
1343 protected function loadText() {
1344 wfProfileIn( __METHOD__ );
1345
1346 // Caching may be beneficial for massive use of external storage
1347 global $wgRevisionCacheExpiry, $wgMemc;
1348 $textId = $this->getTextId();
1349 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1350 if( $wgRevisionCacheExpiry ) {
1351 $text = $wgMemc->get( $key );
1352 if( is_string( $text ) ) {
1353 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1354 wfProfileOut( __METHOD__ );
1355 return $text;
1356 }
1357 }
1358
1359 // If we kept data for lazy extraction, use it now...
1360 if ( isset( $this->mTextRow ) ) {
1361 $row = $this->mTextRow;
1362 $this->mTextRow = null;
1363 } else {
1364 $row = null;
1365 }
1366
1367 if( !$row ) {
1368 // Text data is immutable; check slaves first.
1369 $dbr = wfGetDB( DB_SLAVE );
1370 $row = $dbr->selectRow( 'text',
1371 array( 'old_text', 'old_flags' ),
1372 array( 'old_id' => $this->getTextId() ),
1373 __METHOD__ );
1374 }
1375
1376 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1377 // Possible slave lag!
1378 $dbw = wfGetDB( DB_MASTER );
1379 $row = $dbw->selectRow( 'text',
1380 array( 'old_text', 'old_flags' ),
1381 array( 'old_id' => $this->getTextId() ),
1382 __METHOD__ );
1383 }
1384
1385 $text = self::getRevisionText( $row );
1386
1387 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1388 if( $wgRevisionCacheExpiry && $text !== false ) {
1389 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1390 }
1391
1392 wfProfileOut( __METHOD__ );
1393
1394 return $text;
1395 }
1396
1397 /**
1398 * Create a new null-revision for insertion into a page's
1399 * history. This will not re-save the text, but simply refer
1400 * to the text from the previous version.
1401 *
1402 * Such revisions can for instance identify page rename
1403 * operations and other such meta-modifications.
1404 *
1405 * @param $dbw DatabaseBase
1406 * @param $pageId Integer: ID number of the page to read from
1407 * @param $summary String: revision's summary
1408 * @param $minor Boolean: whether the revision should be considered as minor
1409 * @return Revision|null on error
1410 */
1411 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1412 global $wgContentHandlerUseDB;
1413
1414 wfProfileIn( __METHOD__ );
1415
1416 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1417 'rev_text_id', 'rev_len', 'rev_sha1' );
1418
1419 if ( $wgContentHandlerUseDB ) {
1420 $fields[] = 'rev_content_model';
1421 $fields[] = 'rev_content_format';
1422 }
1423
1424 $current = $dbw->selectRow(
1425 array( 'page', 'revision' ),
1426 $fields,
1427 array(
1428 'page_id' => $pageId,
1429 'page_latest=rev_id',
1430 ),
1431 __METHOD__ );
1432
1433 if( $current ) {
1434 $row = array(
1435 'page' => $pageId,
1436 'comment' => $summary,
1437 'minor_edit' => $minor,
1438 'text_id' => $current->rev_text_id,
1439 'parent_id' => $current->page_latest,
1440 'len' => $current->rev_len,
1441 'sha1' => $current->rev_sha1
1442 );
1443
1444 if ( $wgContentHandlerUseDB ) {
1445 $row[ 'content_model' ] = $current->rev_content_model;
1446 $row[ 'content_format' ] = $current->rev_content_format;
1447 }
1448
1449 $revision = new Revision( $row );
1450 $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
1451 } else {
1452 $revision = null;
1453 }
1454
1455 wfProfileOut( __METHOD__ );
1456 return $revision;
1457 }
1458
1459 /**
1460 * Determine if the current user is allowed to view a particular
1461 * field of this revision, if it's marked as deleted.
1462 *
1463 * @param $field Integer:one of self::DELETED_TEXT,
1464 * self::DELETED_COMMENT,
1465 * self::DELETED_USER
1466 * @param $user User object to check, or null to use $wgUser
1467 * @return Boolean
1468 */
1469 public function userCan( $field, User $user = null ) {
1470 return self::userCanBitfield( $this->mDeleted, $field, $user );
1471 }
1472
1473 /**
1474 * Determine if the current user is allowed to view a particular
1475 * field of this revision, if it's marked as deleted. This is used
1476 * by various classes to avoid duplication.
1477 *
1478 * @param $bitfield Integer: current field
1479 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1480 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1481 * self::DELETED_USER = File::DELETED_USER
1482 * @param $user User object to check, or null to use $wgUser
1483 * @return Boolean
1484 */
1485 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1486 if( $bitfield & $field ) { // aspect is deleted
1487 if ( $bitfield & self::DELETED_RESTRICTED ) {
1488 $permission = 'suppressrevision';
1489 } elseif ( $field & self::DELETED_TEXT ) {
1490 $permission = 'deletedtext';
1491 } else {
1492 $permission = 'deletedhistory';
1493 }
1494 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1495 if ( $user === null ) {
1496 global $wgUser;
1497 $user = $wgUser;
1498 }
1499 return $user->isAllowed( $permission );
1500 } else {
1501 return true;
1502 }
1503 }
1504
1505 /**
1506 * Get rev_timestamp from rev_id, without loading the rest of the row
1507 *
1508 * @param $title Title
1509 * @param $id Integer
1510 * @return String
1511 */
1512 static function getTimestampFromId( $title, $id ) {
1513 $dbr = wfGetDB( DB_SLAVE );
1514 // Casting fix for DB2
1515 if ( $id == '' ) {
1516 $id = 0;
1517 }
1518 $conds = array( 'rev_id' => $id );
1519 $conds['rev_page'] = $title->getArticleID();
1520 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1521 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1522 # Not in slave, try master
1523 $dbw = wfGetDB( DB_MASTER );
1524 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1525 }
1526 return wfTimestamp( TS_MW, $timestamp );
1527 }
1528
1529 /**
1530 * Get count of revisions per page...not very efficient
1531 *
1532 * @param $db DatabaseBase
1533 * @param $id Integer: page id
1534 * @return Integer
1535 */
1536 static function countByPageId( $db, $id ) {
1537 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1538 array( 'rev_page' => $id ), __METHOD__ );
1539 if( $row ) {
1540 return $row->revCount;
1541 }
1542 return 0;
1543 }
1544
1545 /**
1546 * Get count of revisions per page...not very efficient
1547 *
1548 * @param $db DatabaseBase
1549 * @param $title Title
1550 * @return Integer
1551 */
1552 static function countByTitle( $db, $title ) {
1553 $id = $title->getArticleID();
1554 if( $id ) {
1555 return Revision::countByPageId( $db, $id );
1556 }
1557 return 0;
1558 }
1559 }