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