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