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