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