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