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