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