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