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