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