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