Merge "Selenium: record video of every test"
[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 MediaWiki\Revision\MutableRevisionRecord;
24 use MediaWiki\Revision\RevisionAccessException;
25 use MediaWiki\Revision\RevisionFactory;
26 use MediaWiki\Revision\RevisionLookup;
27 use MediaWiki\Revision\RevisionRecord;
28 use MediaWiki\Revision\RevisionStore;
29 use MediaWiki\Revision\RevisionStoreRecord;
30 use MediaWiki\Revision\SlotRecord;
31 use MediaWiki\Storage\SqlBlobStore;
32 use Wikimedia\Rdbms\IDatabase;
33 use MediaWiki\Linker\LinkTarget;
34 use MediaWiki\MediaWikiServices;
35 use Wikimedia\Rdbms\ResultWrapper;
36 use Wikimedia\Rdbms\FakeResultWrapper;
37
38 /**
39 * @deprecated since 1.31, use RevisionRecord, RevisionStore, and BlobStore instead.
40 */
41 class Revision implements IDBAccessObject {
42
43 /** @var RevisionRecord */
44 protected $mRecord;
45
46 // Revision deletion constants
47 const DELETED_TEXT = RevisionRecord::DELETED_TEXT;
48 const DELETED_COMMENT = RevisionRecord::DELETED_COMMENT;
49 const DELETED_USER = RevisionRecord::DELETED_USER;
50 const DELETED_RESTRICTED = RevisionRecord::DELETED_RESTRICTED;
51 const SUPPRESSED_USER = RevisionRecord::SUPPRESSED_USER;
52 const SUPPRESSED_ALL = RevisionRecord::SUPPRESSED_ALL;
53
54 // Audience options for accessors
55 const FOR_PUBLIC = RevisionRecord::FOR_PUBLIC;
56 const FOR_THIS_USER = RevisionRecord::FOR_THIS_USER;
57 const RAW = RevisionRecord::RAW;
58
59 const TEXT_CACHE_GROUP = SqlBlobStore::TEXT_CACHE_GROUP;
60
61 /**
62 * @return RevisionStore
63 */
64 protected static function getRevisionStore() {
65 return MediaWikiServices::getInstance()->getRevisionStore();
66 }
67
68 /**
69 * @return RevisionLookup
70 */
71 protected static function getRevisionLookup() {
72 return MediaWikiServices::getInstance()->getRevisionLookup();
73 }
74
75 /**
76 * @return RevisionFactory
77 */
78 protected static function getRevisionFactory() {
79 return MediaWikiServices::getInstance()->getRevisionFactory();
80 }
81
82 /**
83 * @param bool|string $wiki The ID of the target wiki database. Use false for the local wiki.
84 *
85 * @return SqlBlobStore
86 */
87 protected static function getBlobStore( $wiki = false ) {
88 $store = MediaWikiServices::getInstance()
89 ->getBlobStoreFactory()
90 ->newSqlBlobStore( $wiki );
91
92 if ( !$store instanceof SqlBlobStore ) {
93 throw new RuntimeException(
94 'The backwards compatibility code in Revision currently requires the BlobStore '
95 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
96 );
97 }
98
99 return $store;
100 }
101
102 /**
103 * Load a page revision from a given revision ID number.
104 * Returns null if no such revision can be found.
105 *
106 * $flags include:
107 * Revision::READ_LATEST : Select the data from the master
108 * Revision::READ_LOCKING : Select & lock the data from the master
109 *
110 * @param int $id
111 * @param int $flags (optional)
112 * @return Revision|null
113 */
114 public static function newFromId( $id, $flags = 0 ) {
115 $rec = self::getRevisionLookup()->getRevisionById( $id, $flags );
116 return $rec === null ? null : new Revision( $rec, $flags );
117 }
118
119 /**
120 * Load either the current, or a specified, revision
121 * that's attached to a given link target. If not attached
122 * to that link target, will return null.
123 *
124 * $flags include:
125 * Revision::READ_LATEST : Select the data from the master
126 * Revision::READ_LOCKING : Select & lock the data from the master
127 *
128 * @param LinkTarget $linkTarget
129 * @param int $id (optional)
130 * @param int $flags Bitfield (optional)
131 * @return Revision|null
132 */
133 public static function newFromTitle( LinkTarget $linkTarget, $id = 0, $flags = 0 ) {
134 $rec = self::getRevisionLookup()->getRevisionByTitle( $linkTarget, $id, $flags );
135 return $rec === null ? null : new Revision( $rec, $flags );
136 }
137
138 /**
139 * Load either the current, or a specified, revision
140 * that's attached to a given page ID.
141 * Returns null if no such revision can be found.
142 *
143 * $flags include:
144 * Revision::READ_LATEST : Select the data from the master (since 1.20)
145 * Revision::READ_LOCKING : Select & lock the data from the master
146 *
147 * @param int $pageId
148 * @param int $revId (optional)
149 * @param int $flags Bitfield (optional)
150 * @return Revision|null
151 */
152 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
153 $rec = self::getRevisionLookup()->getRevisionByPageId( $pageId, $revId, $flags );
154 return $rec === null ? null : new Revision( $rec, $flags );
155 }
156
157 /**
158 * Make a fake revision object from an archive table row. This is queried
159 * for permissions or even inserted (as in Special:Undelete)
160 *
161 * @param object $row
162 * @param array $overrides
163 *
164 * @throws MWException
165 * @return Revision
166 */
167 public static function newFromArchiveRow( $row, $overrides = [] ) {
168 /**
169 * MCR Migration: https://phabricator.wikimedia.org/T183564
170 * This method used to overwrite attributes, then passed to Revision::__construct
171 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
172 * So do a conversion here.
173 */
174 if ( array_key_exists( 'page', $overrides ) ) {
175 $overrides['page_id'] = $overrides['page'];
176 unset( $overrides['page'] );
177 }
178
179 /**
180 * We require a Title for both the Revision object and the RevisionRecord.
181 * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
182 * to fetch a title in order pass it into the Revision object.
183 */
184 $title = null;
185 if ( isset( $overrides['title'] ) ) {
186 if ( !( $overrides['title'] instanceof Title ) ) {
187 throw new MWException( 'title field override must contain a Title object.' );
188 }
189
190 $title = $overrides['title'];
191 }
192 if ( $title !== null ) {
193 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
194 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
195 } else {
196 throw new InvalidArgumentException(
197 'A Title or ar_namespace and ar_title must be given'
198 );
199 }
200 }
201
202 $rec = self::getRevisionFactory()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
203 return new Revision( $rec, self::READ_NORMAL, $title );
204 }
205
206 /**
207 * @since 1.19
208 *
209 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
210 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
211 * a MutableRevisionRecord should be constructed directly.
212 * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
213 * but should be avoided.
214 *
215 * @param object|array $row
216 * @return Revision
217 */
218 public static function newFromRow( $row ) {
219 if ( is_array( $row ) ) {
220 $rec = self::getRevisionFactory()->newMutableRevisionFromArray( $row );
221 } else {
222 $rec = self::getRevisionFactory()->newRevisionFromRow( $row );
223 }
224
225 return new Revision( $rec );
226 }
227
228 /**
229 * Load a page revision from a given revision ID number.
230 * Returns null if no such revision can be found.
231 *
232 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
233 *
234 * @param IDatabase $db
235 * @param int $id
236 * @return Revision|null
237 */
238 public static function loadFromId( $db, $id ) {
239 wfDeprecated( __METHOD__, '1.31' ); // no known callers
240 $rec = self::getRevisionStore()->loadRevisionFromId( $db, $id );
241 return $rec === null ? null : new Revision( $rec );
242 }
243
244 /**
245 * Load either the current, or a specified, revision
246 * that's attached to a given page. If not attached
247 * to that page, will return null.
248 *
249 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
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 $rec = self::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
258 return $rec === null ? null : new Revision( $rec );
259 }
260
261 /**
262 * Load either the current, or a specified, revision
263 * that's attached to a given page. If not attached
264 * to that page, will return null.
265 *
266 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
267 *
268 * @param IDatabase $db
269 * @param Title $title
270 * @param int $id
271 * @return Revision|null
272 */
273 public static function loadFromTitle( $db, $title, $id = 0 ) {
274 $rec = self::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
275 return $rec === null ? null : new Revision( $rec );
276 }
277
278 /**
279 * Load the revision for the given title with the given timestamp.
280 * WARNING: Timestamps may in some circumstances not be unique,
281 * so this isn't the best key to use.
282 *
283 * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
284 * or RevisionStore::loadRevisionFromTimestamp() instead.
285 *
286 * @param IDatabase $db
287 * @param Title $title
288 * @param string $timestamp
289 * @return Revision|null
290 */
291 public static function loadFromTimestamp( $db, $title, $timestamp ) {
292 $rec = self::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
293 return $rec === null ? null : new Revision( $rec );
294 }
295
296 /**
297 * Return a wrapper for a series of database rows to
298 * fetch all of a given page's revisions in turn.
299 * Each row can be fed to the constructor to get objects.
300 *
301 * @param LinkTarget $title
302 * @return ResultWrapper
303 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
304 */
305 public static function fetchRevision( LinkTarget $title ) {
306 wfDeprecated( __METHOD__, '1.31' );
307 return new FakeResultWrapper( [] );
308 }
309
310 /**
311 * Return the value of a select() JOIN conds array for the user table.
312 * This will get user table rows for logged-in users.
313 * @since 1.19
314 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
315 * @return array
316 */
317 public static function userJoinCond() {
318 global $wgActorTableSchemaMigrationStage;
319
320 wfDeprecated( __METHOD__, '1.31' );
321 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
322 // If code is using this instead of self::getQueryInfo(), there's
323 // no way the join it's trying to do can work once the old fields
324 // aren't being used anymore.
325 throw new BadMethodCallException(
326 'Cannot use ' . __METHOD__
327 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
328 );
329 }
330
331 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
332 }
333
334 /**
335 * Return the value of a select() page conds array for the page table.
336 * This will assure that the revision(s) are not orphaned from live pages.
337 * @since 1.19
338 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
339 * @return array
340 */
341 public static function pageJoinCond() {
342 wfDeprecated( __METHOD__, '1.31' );
343 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
344 }
345
346 /**
347 * Return the list of revision fields that should be selected to create
348 * a new revision.
349 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
350 * @return array
351 */
352 public static function selectFields() {
353 global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
354 global $wgMultiContentRevisionSchemaMigrationStage;
355
356 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
357 // If code is using this instead of self::getQueryInfo(), there's a
358 // decent chance it's going to try to directly access
359 // $row->rev_user or $row->rev_user_text and we can't give it
360 // useful values here once those aren't being used anymore.
361 throw new BadMethodCallException(
362 'Cannot use ' . __METHOD__
363 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
364 );
365 }
366
367 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
368 // If code is using this instead of self::getQueryInfo(), there's a
369 // decent chance it's going to try to directly access
370 // $row->rev_text_id or $row->rev_content_model and we can't give it
371 // useful values here once those aren't being written anymore,
372 // and may not exist at all.
373 throw new BadMethodCallException(
374 'Cannot use ' . __METHOD__ . ' when $wgMultiContentRevisionSchemaMigrationStage '
375 . 'does not have SCHEMA_COMPAT_WRITE_OLD set.'
376 );
377 }
378
379 wfDeprecated( __METHOD__, '1.31' );
380
381 $fields = [
382 'rev_id',
383 'rev_page',
384 'rev_text_id',
385 'rev_timestamp',
386 'rev_user_text',
387 'rev_user',
388 'rev_actor' => 'NULL',
389 'rev_minor_edit',
390 'rev_deleted',
391 'rev_len',
392 'rev_parent_id',
393 'rev_sha1',
394 ];
395
396 $fields += CommentStore::getStore()->getFields( 'rev_comment' );
397
398 if ( $wgContentHandlerUseDB ) {
399 $fields[] = 'rev_content_format';
400 $fields[] = 'rev_content_model';
401 }
402
403 return $fields;
404 }
405
406 /**
407 * Return the list of revision fields that should be selected to create
408 * a new revision from an archive row.
409 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
410 * @return array
411 */
412 public static function selectArchiveFields() {
413 global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
414 global $wgMultiContentRevisionSchemaMigrationStage;
415
416 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
417 // If code is using this instead of self::getQueryInfo(), there's a
418 // decent chance it's going to try to directly access
419 // $row->ar_user or $row->ar_user_text and we can't give it
420 // useful values here once those aren't being used anymore.
421 throw new BadMethodCallException(
422 'Cannot use ' . __METHOD__
423 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
424 );
425 }
426
427 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
428 // If code is using this instead of self::getQueryInfo(), there's a
429 // decent chance it's going to try to directly access
430 // $row->ar_text_id or $row->ar_content_model and we can't give it
431 // useful values here once those aren't being written anymore,
432 // and may not exist at all.
433 throw new BadMethodCallException(
434 'Cannot use ' . __METHOD__ . ' when $wgMultiContentRevisionSchemaMigrationStage '
435 . 'does not have SCHEMA_COMPAT_WRITE_OLD set.'
436 );
437 }
438
439 wfDeprecated( __METHOD__, '1.31' );
440
441 $fields = [
442 'ar_id',
443 'ar_page_id',
444 'ar_rev_id',
445 'ar_text_id',
446 'ar_timestamp',
447 'ar_user_text',
448 'ar_user',
449 'ar_actor' => 'NULL',
450 'ar_minor_edit',
451 'ar_deleted',
452 'ar_len',
453 'ar_parent_id',
454 'ar_sha1',
455 ];
456
457 $fields += CommentStore::getStore()->getFields( 'ar_comment' );
458
459 if ( $wgContentHandlerUseDB ) {
460 $fields[] = 'ar_content_format';
461 $fields[] = 'ar_content_model';
462 }
463 return $fields;
464 }
465
466 /**
467 * Return the list of text fields that should be selected to read the
468 * revision text
469 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
470 * @return array
471 */
472 public static function selectTextFields() {
473 wfDeprecated( __METHOD__, '1.31' );
474 return [
475 'old_text',
476 'old_flags'
477 ];
478 }
479
480 /**
481 * Return the list of page fields that should be selected from page table
482 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
483 * @return array
484 */
485 public static function selectPageFields() {
486 wfDeprecated( __METHOD__, '1.31' );
487 return [
488 'page_namespace',
489 'page_title',
490 'page_id',
491 'page_latest',
492 'page_is_redirect',
493 'page_len',
494 ];
495 }
496
497 /**
498 * Return the list of user fields that should be selected from user table
499 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
500 * @return array
501 */
502 public static function selectUserFields() {
503 wfDeprecated( __METHOD__, '1.31' );
504 return [ 'user_name' ];
505 }
506
507 /**
508 * Return the tables, fields, and join conditions to be selected to create
509 * a new revision object.
510 * @since 1.31
511 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
512 * @param array $options Any combination of the following strings
513 * - 'page': Join with the page table, and select fields to identify the page
514 * - 'user': Join with the user table, and select the user name
515 * - 'text': Join with the text table, and select fields to load page text
516 * @return array With three keys:
517 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
518 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
519 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
520 */
521 public static function getQueryInfo( $options = [] ) {
522 return self::getRevisionStore()->getQueryInfo( $options );
523 }
524
525 /**
526 * Return the tables, fields, and join conditions to be selected to create
527 * a new archived revision object.
528 * @since 1.31
529 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
530 * @return array With three keys:
531 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
532 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
533 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
534 */
535 public static function getArchiveQueryInfo() {
536 return self::getRevisionStore()->getArchiveQueryInfo();
537 }
538
539 /**
540 * Do a batched query to get the parent revision lengths
541 *
542 * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
543 *
544 * @param IDatabase $db
545 * @param array $revIds
546 * @return array
547 */
548 public static function getParentLengths( $db, array $revIds ) {
549 return self::getRevisionStore()->listRevisionSizes( $db, $revIds );
550 }
551
552 /**
553 * @param object|array|RevisionRecord $row Either a database row or an array
554 * @param int $queryFlags
555 * @param Title|null $title
556 *
557 * @access private
558 */
559 function __construct( $row, $queryFlags = 0, Title $title = null ) {
560 global $wgUser;
561
562 if ( $row instanceof RevisionRecord ) {
563 $this->mRecord = $row;
564 } elseif ( is_array( $row ) ) {
565 // If no user is specified, fall back to using the global user object, to stay
566 // compatible with pre-1.31 behavior.
567 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
568 $row['user'] = $wgUser;
569 }
570
571 $this->mRecord = self::getRevisionFactory()->newMutableRevisionFromArray(
572 $row,
573 $queryFlags,
574 $this->ensureTitle( $row, $queryFlags, $title )
575 );
576 } elseif ( is_object( $row ) ) {
577 $this->mRecord = self::getRevisionFactory()->newRevisionFromRow(
578 $row,
579 $queryFlags,
580 $this->ensureTitle( $row, $queryFlags, $title )
581 );
582 } else {
583 throw new InvalidArgumentException(
584 '$row must be a row object, an associative array, or a RevisionRecord'
585 );
586 }
587 }
588
589 /**
590 * Make sure we have *some* Title object for use by the constructor.
591 * For B/C, the constructor shouldn't fail even for a bad page ID or bad revision ID.
592 *
593 * @param array|object $row
594 * @param int $queryFlags
595 * @param Title|null $title
596 *
597 * @return Title $title if not null, or a Title constructed from information in $row.
598 */
599 private function ensureTitle( $row, $queryFlags, $title = null ) {
600 if ( $title ) {
601 return $title;
602 }
603
604 if ( is_array( $row ) ) {
605 if ( isset( $row['title'] ) ) {
606 if ( !( $row['title'] instanceof Title ) ) {
607 throw new MWException( 'title field must contain a Title object.' );
608 }
609
610 return $row['title'];
611 }
612
613 $pageId = $row['page'] ?? 0;
614 $revId = $row['id'] ?? 0;
615 } else {
616 $pageId = $row->rev_page ?? 0;
617 $revId = $row->rev_id ?? 0;
618 }
619
620 try {
621 $title = self::getRevisionStore()->getTitle( $pageId, $revId, $queryFlags );
622 } catch ( RevisionAccessException $ex ) {
623 // construct a dummy title!
624 wfLogWarning( __METHOD__ . ': ' . $ex->getMessage() );
625
626 // NOTE: this Title will only be used inside RevisionRecord
627 $title = Title::makeTitleSafe( NS_SPECIAL, "Badtitle/ID=$pageId" );
628 $title->resetArticleID( $pageId );
629 }
630
631 return $title;
632 }
633
634 /**
635 * @return RevisionRecord
636 */
637 public function getRevisionRecord() {
638 return $this->mRecord;
639 }
640
641 /**
642 * Get revision ID
643 *
644 * @return int|null
645 */
646 public function getId() {
647 return $this->mRecord->getId();
648 }
649
650 /**
651 * Set the revision ID
652 *
653 * This should only be used for proposed revisions that turn out to be null edits.
654 *
655 * @note Only supported on Revisions that were constructed based on associative arrays,
656 * since they are mutable.
657 *
658 * @since 1.19
659 * @param int|string $id
660 * @throws MWException
661 */
662 public function setId( $id ) {
663 if ( $this->mRecord instanceof MutableRevisionRecord ) {
664 $this->mRecord->setId( intval( $id ) );
665 } else {
666 throw new MWException( __METHOD__ . ' is not supported on this instance' );
667 }
668 }
669
670 /**
671 * Set the user ID/name
672 *
673 * This should only be used for proposed revisions that turn out to be null edits
674 *
675 * @note Only supported on Revisions that were constructed based on associative arrays,
676 * since they are mutable.
677 *
678 * @since 1.28
679 * @deprecated since 1.31, please reuse old Revision object
680 * @param int $id User ID
681 * @param string $name User name
682 * @throws MWException
683 */
684 public function setUserIdAndName( $id, $name ) {
685 if ( $this->mRecord instanceof MutableRevisionRecord ) {
686 $user = User::newFromAnyId( intval( $id ), $name, null );
687 $this->mRecord->setUser( $user );
688 } else {
689 throw new MWException( __METHOD__ . ' is not supported on this instance' );
690 }
691 }
692
693 /**
694 * @return SlotRecord
695 */
696 private function getMainSlotRaw() {
697 return $this->mRecord->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
698 }
699
700 /**
701 * Get the ID of the row of the text table that contains the content of the
702 * revision's main slot, if that content is stored in the text table.
703 *
704 * If the content is stored elsewhere, this returns null.
705 *
706 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
707 * get that actual address that can be used with BlobStore::getBlob(); or use
708 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
709 *
710 * @return int|null
711 */
712 public function getTextId() {
713 $slot = $this->getMainSlotRaw();
714 return $slot->hasAddress()
715 ? self::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
716 : null;
717 }
718
719 /**
720 * Get parent revision ID (the original previous page revision)
721 *
722 * @return int|null The ID of the parent revision. 0 indicates that there is no
723 * parent revision. Null indicates that the parent revision is not known.
724 */
725 public function getParentId() {
726 return $this->mRecord->getParentId();
727 }
728
729 /**
730 * Returns the length of the text in this revision, or null if unknown.
731 *
732 * @return int|null
733 */
734 public function getSize() {
735 try {
736 return $this->mRecord->getSize();
737 } catch ( RevisionAccessException $ex ) {
738 return null;
739 }
740 }
741
742 /**
743 * Returns the base36 sha1 of the content in this revision, or null if unknown.
744 *
745 * @return string|null
746 */
747 public function getSha1() {
748 try {
749 return $this->mRecord->getSha1();
750 } catch ( RevisionAccessException $ex ) {
751 return null;
752 }
753 }
754
755 /**
756 * Returns the title of the page associated with this entry.
757 * Since 1.31, this will never return null.
758 *
759 * Will do a query, when title is not set and id is given.
760 *
761 * @return Title
762 */
763 public function getTitle() {
764 $linkTarget = $this->mRecord->getPageAsLinkTarget();
765 return Title::newFromLinkTarget( $linkTarget );
766 }
767
768 /**
769 * Set the title of the revision
770 *
771 * @deprecated since 1.31, this is now a noop. Pass the Title to the constructor instead.
772 *
773 * @param Title $title
774 */
775 public function setTitle( $title ) {
776 if ( !$title->equals( $this->getTitle() ) ) {
777 throw new InvalidArgumentException(
778 $title->getPrefixedText()
779 . ' is not the same as '
780 . $this->mRecord->getPageAsLinkTarget()->__toString()
781 );
782 }
783 }
784
785 /**
786 * Get the page ID
787 *
788 * @return int|null
789 */
790 public function getPage() {
791 return $this->mRecord->getPageId();
792 }
793
794 /**
795 * Fetch revision's user id if it's available to the specified audience.
796 * If the specified audience does not have access to it, zero will be
797 * returned.
798 *
799 * @param int $audience One of:
800 * Revision::FOR_PUBLIC to be displayed to all users
801 * Revision::FOR_THIS_USER to be displayed to the given user
802 * Revision::RAW get the ID regardless of permissions
803 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
804 * to the $audience parameter
805 * @return int
806 */
807 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
808 global $wgUser;
809
810 if ( $audience === self::FOR_THIS_USER && !$user ) {
811 $user = $wgUser;
812 }
813
814 $user = $this->mRecord->getUser( $audience, $user );
815 return $user ? $user->getId() : 0;
816 }
817
818 /**
819 * Fetch revision's username if it's available to the specified audience.
820 * If the specified audience does not have access to the username, an
821 * empty string will be returned.
822 *
823 * @param int $audience One of:
824 * Revision::FOR_PUBLIC to be displayed to all users
825 * Revision::FOR_THIS_USER to be displayed to the given user
826 * Revision::RAW get the text regardless of permissions
827 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
828 * to the $audience parameter
829 * @return string
830 */
831 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
832 global $wgUser;
833
834 if ( $audience === self::FOR_THIS_USER && !$user ) {
835 $user = $wgUser;
836 }
837
838 $user = $this->mRecord->getUser( $audience, $user );
839 return $user ? $user->getName() : '';
840 }
841 /**
842 * Fetch revision comment if it's available to the specified audience.
843 * If the specified audience does not have access to the comment, an
844 * empty string will be returned.
845 *
846 * @param int $audience One of:
847 * Revision::FOR_PUBLIC to be displayed to all users
848 * Revision::FOR_THIS_USER to be displayed to the given user
849 * Revision::RAW get the text regardless of permissions
850 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
851 * to the $audience parameter
852 * @return string
853 */
854 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
855 global $wgUser;
856
857 if ( $audience === self::FOR_THIS_USER && !$user ) {
858 $user = $wgUser;
859 }
860
861 $comment = $this->mRecord->getComment( $audience, $user );
862 return $comment === null ? null : $comment->text;
863 }
864
865 /**
866 * @return bool
867 */
868 public function isMinor() {
869 return $this->mRecord->isMinor();
870 }
871
872 /**
873 * @return int Rcid of the unpatrolled row, zero if there isn't one
874 */
875 public function isUnpatrolled() {
876 return self::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord );
877 }
878
879 /**
880 * Get the RC object belonging to the current revision, if there's one
881 *
882 * @param int $flags (optional) $flags include:
883 * Revision::READ_LATEST : Select the data from the master
884 *
885 * @since 1.22
886 * @return RecentChange|null
887 */
888 public function getRecentChange( $flags = 0 ) {
889 return self::getRevisionStore()->getRecentChange( $this->mRecord, $flags );
890 }
891
892 /**
893 * @param int $field One of DELETED_* bitfield constants
894 *
895 * @return bool
896 */
897 public function isDeleted( $field ) {
898 return $this->mRecord->isDeleted( $field );
899 }
900
901 /**
902 * Get the deletion bitfield of the revision
903 *
904 * @return int
905 */
906 public function getVisibility() {
907 return $this->mRecord->getVisibility();
908 }
909
910 /**
911 * Fetch revision content if it's available to the specified audience.
912 * If the specified audience does not have the ability to view this
913 * revision, or the content could not be loaded, null will be returned.
914 *
915 * @param int $audience One of:
916 * Revision::FOR_PUBLIC to be displayed to all users
917 * Revision::FOR_THIS_USER to be displayed to $user
918 * Revision::RAW get the text regardless of permissions
919 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
920 * to the $audience parameter
921 * @since 1.21
922 * @return Content|null
923 */
924 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
925 global $wgUser;
926
927 if ( $audience === self::FOR_THIS_USER && !$user ) {
928 $user = $wgUser;
929 }
930
931 try {
932 return $this->mRecord->getContent( SlotRecord::MAIN, $audience, $user );
933 }
934 catch ( RevisionAccessException $e ) {
935 return null;
936 }
937 }
938
939 /**
940 * Get original serialized data (without checking view restrictions)
941 *
942 * @since 1.21
943 * @deprecated since 1.31, use BlobStore::getBlob instead.
944 *
945 * @return string
946 */
947 public function getSerializedData() {
948 $slot = $this->getMainSlotRaw();
949 return $slot->getContent()->serialize();
950 }
951
952 /**
953 * Returns the content model for the main slot of this revision.
954 *
955 * If no content model was stored in the database, the default content model for the title is
956 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
957 * is used as a last resort.
958 *
959 * @todo drop this, with MCR, there no longer is a single model associated with a revision.
960 *
961 * @return string The content model id associated with this revision,
962 * see the CONTENT_MODEL_XXX constants.
963 */
964 public function getContentModel() {
965 return $this->getMainSlotRaw()->getModel();
966 }
967
968 /**
969 * Returns the content format for the main slot of this revision.
970 *
971 * If no content format was stored in the database, the default format for this
972 * revision's content model is returned.
973 *
974 * @todo drop this, the format is irrelevant to the revision!
975 *
976 * @return string The content format id associated with this revision,
977 * see the CONTENT_FORMAT_XXX constants.
978 */
979 public function getContentFormat() {
980 $format = $this->getMainSlotRaw()->getFormat();
981
982 if ( $format === null ) {
983 // if no format was stored along with the blob, fall back to default format
984 $format = $this->getContentHandler()->getDefaultFormat();
985 }
986
987 return $format;
988 }
989
990 /**
991 * Returns the content handler appropriate for this revision's content model.
992 *
993 * @throws MWException
994 * @return ContentHandler
995 */
996 public function getContentHandler() {
997 return ContentHandler::getForModelID( $this->getContentModel() );
998 }
999
1000 /**
1001 * @return string
1002 */
1003 public function getTimestamp() {
1004 return $this->mRecord->getTimestamp();
1005 }
1006
1007 /**
1008 * @return bool
1009 */
1010 public function isCurrent() {
1011 return ( $this->mRecord instanceof RevisionStoreRecord ) && $this->mRecord->isCurrent();
1012 }
1013
1014 /**
1015 * Get previous revision for this title
1016 *
1017 * @return Revision|null
1018 */
1019 public function getPrevious() {
1020 $title = $this->getTitle();
1021 $rec = self::getRevisionLookup()->getPreviousRevision( $this->mRecord, $title );
1022 return $rec === null ? null : new Revision( $rec, self::READ_NORMAL, $title );
1023 }
1024
1025 /**
1026 * Get next revision for this title
1027 *
1028 * @return Revision|null
1029 */
1030 public function getNext() {
1031 $title = $this->getTitle();
1032 $rec = self::getRevisionLookup()->getNextRevision( $this->mRecord, $title );
1033 return $rec === null ? null : new Revision( $rec, self::READ_NORMAL, $title );
1034 }
1035
1036 /**
1037 * Get revision text associated with an old or archive row
1038 *
1039 * Both the flags and the text field must be included. Including the old_id
1040 * field will activate cache usage as long as the $wiki parameter is not set.
1041 *
1042 * @param stdClass $row The text data
1043 * @param string $prefix Table prefix (default 'old_')
1044 * @param string|bool $wiki The name of the wiki to load the revision text from
1045 * (same as the wiki $row was loaded from) or false to indicate the local
1046 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1047 * identifier as understood by the LoadBalancer class.
1048 * @return string|false Text the text requested or false on failure
1049 */
1050 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1051 $textField = $prefix . 'text';
1052 $flagsField = $prefix . 'flags';
1053
1054 if ( isset( $row->$flagsField ) ) {
1055 $flags = explode( ',', $row->$flagsField );
1056 } else {
1057 $flags = [];
1058 }
1059
1060 if ( isset( $row->$textField ) ) {
1061 $text = $row->$textField;
1062 } else {
1063 return false;
1064 }
1065
1066 $cacheKey = isset( $row->old_id )
1067 ? SqlBlobStore::makeAddressFromTextId( $row->old_id )
1068 : null;
1069
1070 $revisionText = self::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
1071
1072 if ( $revisionText === false ) {
1073 if ( isset( $row->old_id ) ) {
1074 wfLogWarning( __METHOD__ . ": Bad data in text row {$row->old_id}! " );
1075 } else {
1076 wfLogWarning( __METHOD__ . ": Bad data in text row! " );
1077 }
1078 return false;
1079 }
1080
1081 return $revisionText;
1082 }
1083
1084 /**
1085 * If $wgCompressRevisions is enabled, we will compress data.
1086 * The input string is modified in place.
1087 * Return value is the flags field: contains 'gzip' if the
1088 * data is compressed, and 'utf-8' if we're saving in UTF-8
1089 * mode.
1090 *
1091 * @param mixed &$text Reference to a text
1092 * @return string
1093 */
1094 public static function compressRevisionText( &$text ) {
1095 return self::getBlobStore()->compressData( $text );
1096 }
1097
1098 /**
1099 * Re-converts revision text according to it's flags.
1100 *
1101 * @param mixed $text Reference to a text
1102 * @param array $flags Compression flags
1103 * @return string|bool Decompressed text, or false on failure
1104 */
1105 public static function decompressRevisionText( $text, $flags ) {
1106 if ( $text === false ) {
1107 // Text failed to be fetched; nothing to do
1108 return false;
1109 }
1110
1111 return self::getBlobStore()->decompressData( $text, $flags );
1112 }
1113
1114 /**
1115 * Insert a new revision into the database, returning the new revision ID
1116 * number on success and dies horribly on failure.
1117 *
1118 * @param IDatabase $dbw (master connection)
1119 * @throws MWException
1120 * @return int The revision ID
1121 */
1122 public function insertOn( $dbw ) {
1123 global $wgUser;
1124
1125 // Note that $this->mRecord->getId() will typically return null here, but not always,
1126 // e.g. not when restoring a revision.
1127
1128 if ( $this->mRecord->getUser( RevisionRecord::RAW ) === null ) {
1129 if ( $this->mRecord instanceof MutableRevisionRecord ) {
1130 $this->mRecord->setUser( $wgUser );
1131 } else {
1132 throw new MWException( 'Cannot insert revision with no associated user.' );
1133 }
1134 }
1135
1136 $rec = self::getRevisionStore()->insertRevisionOn( $this->mRecord, $dbw );
1137
1138 $this->mRecord = $rec;
1139
1140 // Avoid PHP 7.1 warning of passing $this by reference
1141 $revision = $this;
1142
1143 return $rec->getId();
1144 }
1145
1146 /**
1147 * Get the base 36 SHA-1 value for a string of text
1148 * @param string $text
1149 * @return string
1150 */
1151 public static function base36Sha1( $text ) {
1152 return SlotRecord::base36Sha1( $text );
1153 }
1154
1155 /**
1156 * Create a new null-revision for insertion into a page's
1157 * history. This will not re-save the text, but simply refer
1158 * to the text from the previous version.
1159 *
1160 * Such revisions can for instance identify page rename
1161 * operations and other such meta-modifications.
1162 *
1163 * @param IDatabase $dbw
1164 * @param int $pageId ID number of the page to read from
1165 * @param string $summary Revision's summary
1166 * @param bool $minor Whether the revision should be considered as minor
1167 * @param User|null $user User object to use or null for $wgUser
1168 * @return Revision|null Revision or null on error
1169 */
1170 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1171 global $wgUser;
1172 if ( !$user ) {
1173 $user = $wgUser;
1174 }
1175
1176 $comment = CommentStoreComment::newUnsavedComment( $summary, null );
1177
1178 $title = Title::newFromID( $pageId, Title::GAID_FOR_UPDATE );
1179 if ( $title === null ) {
1180 return null;
1181 }
1182
1183 $rec = self::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1184
1185 return $rec ? new Revision( $rec ) : null;
1186 }
1187
1188 /**
1189 * Determine if the current user is allowed to view a particular
1190 * field of this revision, if it's marked as deleted.
1191 *
1192 * @param int $field One of self::DELETED_TEXT,
1193 * self::DELETED_COMMENT,
1194 * self::DELETED_USER
1195 * @param User|null $user User object to check, or null to use $wgUser
1196 * @return bool
1197 */
1198 public function userCan( $field, User $user = null ) {
1199 return self::userCanBitfield( $this->getVisibility(), $field, $user );
1200 }
1201
1202 /**
1203 * Determine if the current user is allowed to view a particular
1204 * field of this revision, if it's marked as deleted. This is used
1205 * by various classes to avoid duplication.
1206 *
1207 * @param int $bitfield Current field
1208 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1209 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1210 * self::DELETED_USER = File::DELETED_USER
1211 * @param User|null $user User object to check, or null to use $wgUser
1212 * @param Title|null $title A Title object to check for per-page restrictions on,
1213 * instead of just plain userrights
1214 * @return bool
1215 */
1216 public static function userCanBitfield( $bitfield, $field, User $user = null,
1217 Title $title = null
1218 ) {
1219 global $wgUser;
1220
1221 if ( !$user ) {
1222 $user = $wgUser;
1223 }
1224
1225 return RevisionRecord::userCanBitfield( $bitfield, $field, $user, $title );
1226 }
1227
1228 /**
1229 * Get rev_timestamp from rev_id, without loading the rest of the row
1230 *
1231 * @param Title $title
1232 * @param int $id
1233 * @param int $flags
1234 * @return string|bool False if not found
1235 */
1236 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1237 return self::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1238 }
1239
1240 /**
1241 * Get count of revisions per page...not very efficient
1242 *
1243 * @param IDatabase $db
1244 * @param int $id Page id
1245 * @return int
1246 */
1247 static function countByPageId( $db, $id ) {
1248 return self::getRevisionStore()->countRevisionsByPageId( $db, $id );
1249 }
1250
1251 /**
1252 * Get count of revisions per page...not very efficient
1253 *
1254 * @param IDatabase $db
1255 * @param Title $title
1256 * @return int
1257 */
1258 static function countByTitle( $db, $title ) {
1259 return self::getRevisionStore()->countRevisionsByTitle( $db, $title );
1260 }
1261
1262 /**
1263 * Check if no edits were made by other users since
1264 * the time a user started editing the page. Limit to
1265 * 50 revisions for the sake of performance.
1266 *
1267 * @since 1.20
1268 * @deprecated since 1.24
1269 *
1270 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1271 * Database object or a database identifier usable with wfGetDB.
1272 * @param int $pageId The ID of the page in question
1273 * @param int $userId The ID of the user in question
1274 * @param string $since Look at edits since this time
1275 *
1276 * @return bool True if the given user was the only one to edit since the given timestamp
1277 */
1278 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1279 if ( is_int( $db ) ) {
1280 $db = wfGetDB( $db );
1281 }
1282
1283 return self::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1284 }
1285
1286 /**
1287 * Load a revision based on a known page ID and current revision ID from the DB
1288 *
1289 * This method allows for the use of caching, though accessing anything that normally
1290 * requires permission checks (aside from the text) will trigger a small DB lookup.
1291 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1292 *
1293 * @param IDatabase $db ignored!
1294 * @param int|Title $pageIdOrTitle Page ID or Title object
1295 * @param int $revId Known current revision of this page. Determined automatically if not given.
1296 * @return Revision|bool Returns false if missing
1297 * @since 1.28
1298 */
1299 public static function newKnownCurrent( IDatabase $db, $pageIdOrTitle, $revId = 0 ) {
1300 $title = $pageIdOrTitle instanceof Title
1301 ? $pageIdOrTitle
1302 : Title::newFromID( $pageIdOrTitle );
1303
1304 if ( !$title ) {
1305 return false;
1306 }
1307
1308 $record = self::getRevisionLookup()->getKnownCurrentRevision( $title, $revId );
1309 return $record ? new Revision( $record ) : false;
1310 }
1311 }