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