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