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