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