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