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