3 * Representation of a page version.
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.
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.
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
23 use MediaWiki\Revision\MutableRevisionRecord
;
24 use MediaWiki\Revision\RevisionAccessException
;
25 use MediaWiki\Revision\RevisionFactory
;
26 use MediaWiki\Revision\RevisionLookup
;
27 use MediaWiki\Revision\RevisionRecord
;
28 use MediaWiki\Revision\RevisionStore
;
29 use MediaWiki\Revision\RevisionStoreRecord
;
30 use MediaWiki\Revision\SlotRecord
;
31 use MediaWiki\Storage\SqlBlobStore
;
32 use Wikimedia\Rdbms\IDatabase
;
33 use MediaWiki\Linker\LinkTarget
;
34 use MediaWiki\MediaWikiServices
;
35 use Wikimedia\Rdbms\ResultWrapper
;
36 use Wikimedia\Rdbms\FakeResultWrapper
;
39 * @deprecated since 1.31, use RevisionRecord, RevisionStore, and BlobStore instead.
41 class Revision
implements IDBAccessObject
{
43 /** @var RevisionRecord */
46 // Revision deletion constants
47 const DELETED_TEXT
= RevisionRecord
::DELETED_TEXT
;
48 const DELETED_COMMENT
= RevisionRecord
::DELETED_COMMENT
;
49 const DELETED_USER
= RevisionRecord
::DELETED_USER
;
50 const DELETED_RESTRICTED
= RevisionRecord
::DELETED_RESTRICTED
;
51 const SUPPRESSED_USER
= RevisionRecord
::SUPPRESSED_USER
;
52 const SUPPRESSED_ALL
= RevisionRecord
::SUPPRESSED_ALL
;
54 // Audience options for accessors
55 const FOR_PUBLIC
= RevisionRecord
::FOR_PUBLIC
;
56 const FOR_THIS_USER
= RevisionRecord
::FOR_THIS_USER
;
57 const RAW
= RevisionRecord
::RAW
;
59 const TEXT_CACHE_GROUP
= SqlBlobStore
::TEXT_CACHE_GROUP
;
62 * @return RevisionStore
64 protected static function getRevisionStore() {
65 return MediaWikiServices
::getInstance()->getRevisionStore();
69 * @return RevisionLookup
71 protected static function getRevisionLookup() {
72 return MediaWikiServices
::getInstance()->getRevisionLookup();
76 * @return RevisionFactory
78 protected static function getRevisionFactory() {
79 return MediaWikiServices
::getInstance()->getRevisionFactory();
83 * @param bool|string $wiki The ID of the target wiki database. Use false for the local wiki.
85 * @return SqlBlobStore
87 protected static function getBlobStore( $wiki = false ) {
88 $store = MediaWikiServices
::getInstance()
89 ->getBlobStoreFactory()
90 ->newSqlBlobStore( $wiki );
92 if ( !$store instanceof SqlBlobStore
) {
93 throw new RuntimeException(
94 'The backwards compatibility code in Revision currently requires the BlobStore '
95 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
103 * Load a page revision from a given revision ID number.
104 * Returns null if no such revision can be found.
107 * Revision::READ_LATEST : Select the data from the master
108 * Revision::READ_LOCKING : Select & lock the data from the master
111 * @param int $flags (optional)
112 * @return Revision|null
114 public static function newFromId( $id, $flags = 0 ) {
115 $rec = self
::getRevisionLookup()->getRevisionById( $id, $flags );
116 return $rec === null ?
null : new Revision( $rec, $flags );
120 * Load either the current, or a specified, revision
121 * that's attached to a given link target. If not attached
122 * to that link target, will return null.
125 * Revision::READ_LATEST : Select the data from the master
126 * Revision::READ_LOCKING : Select & lock the data from the master
128 * @param LinkTarget $linkTarget
129 * @param int $id (optional)
130 * @param int $flags Bitfield (optional)
131 * @return Revision|null
133 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
134 $rec = self
::getRevisionLookup()->getRevisionByTitle( $linkTarget, $id, $flags );
135 return $rec === null ?
null : new Revision( $rec, $flags );
139 * Load either the current, or a specified, revision
140 * that's attached to a given page ID.
141 * Returns null if no such revision can be found.
144 * Revision::READ_LATEST : Select the data from the master (since 1.20)
145 * Revision::READ_LOCKING : Select & lock the data from the master
148 * @param int $revId (optional)
149 * @param int $flags Bitfield (optional)
150 * @return Revision|null
152 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
153 $rec = self
::getRevisionLookup()->getRevisionByPageId( $pageId, $revId, $flags );
154 return $rec === null ?
null : new Revision( $rec, $flags );
158 * Make a fake revision object from an archive table row. This is queried
159 * for permissions or even inserted (as in Special:Undelete)
162 * @param array $overrides
164 * @throws MWException
167 public static function newFromArchiveRow( $row, $overrides = [] ) {
169 * MCR Migration: https://phabricator.wikimedia.org/T183564
170 * This method used to overwrite attributes, then passed to Revision::__construct
171 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
172 * So do a conversion here.
174 if ( array_key_exists( 'page', $overrides ) ) {
175 $overrides['page_id'] = $overrides['page'];
176 unset( $overrides['page'] );
180 * We require a Title for both the Revision object and the RevisionRecord.
181 * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
182 * to fetch a title in order pass it into the Revision object.
185 if ( isset( $overrides['title'] ) ) {
186 if ( !( $overrides['title'] instanceof Title
) ) {
187 throw new MWException( 'title field override must contain a Title object.' );
190 $title = $overrides['title'];
192 if ( $title !== null ) {
193 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
194 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
196 throw new InvalidArgumentException(
197 'A Title or ar_namespace and ar_title must be given'
202 $rec = self
::getRevisionFactory()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
203 return new Revision( $rec, self
::READ_NORMAL
, $title );
209 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
210 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
211 * a MutableRevisionRecord should be constructed directly.
212 * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
213 * but should be avoided.
215 * @param object|array $row
218 public static function newFromRow( $row ) {
219 if ( is_array( $row ) ) {
220 $rec = self
::getRevisionFactory()->newMutableRevisionFromArray( $row );
222 $rec = self
::getRevisionFactory()->newRevisionFromRow( $row );
225 return new Revision( $rec );
229 * Load a page revision from a given revision ID number.
230 * Returns null if no such revision can be found.
232 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
234 * @param IDatabase $db
236 * @return Revision|null
238 public static function loadFromId( $db, $id ) {
239 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
240 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
241 return $rec === null ?
null : new Revision( $rec );
245 * Load either the current, or a specified, revision
246 * that's attached to a given page. If not attached
247 * to that page, will return null.
249 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
251 * @param IDatabase $db
254 * @return Revision|null
256 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
257 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
258 return $rec === null ?
null : new Revision( $rec );
262 * Load either the current, or a specified, revision
263 * that's attached to a given page. If not attached
264 * to that page, will return null.
266 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
268 * @param IDatabase $db
269 * @param Title $title
271 * @return Revision|null
273 public static function loadFromTitle( $db, $title, $id = 0 ) {
274 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
275 return $rec === null ?
null : new Revision( $rec );
279 * Load the revision for the given title with the given timestamp.
280 * WARNING: Timestamps may in some circumstances not be unique,
281 * so this isn't the best key to use.
283 * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
284 * or RevisionStore::loadRevisionFromTimestamp() instead.
286 * @param IDatabase $db
287 * @param Title $title
288 * @param string $timestamp
289 * @return Revision|null
291 public static function loadFromTimestamp( $db, $title, $timestamp ) {
292 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
293 return $rec === null ?
null : new Revision( $rec );
297 * Return a wrapper for a series of database rows to
298 * fetch all of a given page's revisions in turn.
299 * Each row can be fed to the constructor to get objects.
301 * @param LinkTarget $title
302 * @return ResultWrapper
303 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
305 public static function fetchRevision( LinkTarget
$title ) {
306 wfDeprecated( __METHOD__
, '1.31' );
307 return new FakeResultWrapper( [] );
311 * Return the value of a select() JOIN conds array for the user table.
312 * This will get user table rows for logged-in users.
314 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
317 public static function userJoinCond() {
318 global $wgActorTableSchemaMigrationStage;
320 wfDeprecated( __METHOD__
, '1.31' );
321 if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
322 // If code is using this instead of self::getQueryInfo(), there's
323 // no way the join it's trying to do can work once the old fields
324 // aren't being written anymore.
325 throw new BadMethodCallException(
326 'Cannot use ' . __METHOD__
. ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
330 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
334 * Return the value of a select() page conds array for the page table.
335 * This will assure that the revision(s) are not orphaned from live pages.
337 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
340 public static function pageJoinCond() {
341 wfDeprecated( __METHOD__
, '1.31' );
342 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
346 * Return the list of revision fields that should be selected to create
348 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
351 public static function selectFields() {
352 global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
353 global $wgMultiContentRevisionSchemaMigrationStage;
355 if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
356 // If code is using this instead of self::getQueryInfo(), there's a
357 // decent chance it's going to try to directly access
358 // $row->rev_user or $row->rev_user_text and we can't give it
359 // useful values here once those aren't being written anymore.
360 throw new BadMethodCallException(
361 'Cannot use ' . __METHOD__
. ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
365 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
366 // If code is using this instead of self::getQueryInfo(), there's a
367 // decent chance it's going to try to directly access
368 // $row->rev_text_id or $row->rev_content_model and we can't give it
369 // useful values here once those aren't being written anymore,
370 // and may not exist at all.
371 throw new BadMethodCallException(
372 'Cannot use ' . __METHOD__
. ' when $wgMultiContentRevisionSchemaMigrationStage '
373 . 'does not have SCHEMA_COMPAT_WRITE_OLD set.'
377 wfDeprecated( __METHOD__
, '1.31' );
386 'rev_actor' => 'NULL',
394 $fields +
= CommentStore
::getStore()->getFields( 'rev_comment' );
396 if ( $wgContentHandlerUseDB ) {
397 $fields[] = 'rev_content_format';
398 $fields[] = 'rev_content_model';
405 * Return the list of revision fields that should be selected to create
406 * a new revision from an archive row.
407 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
410 public static function selectArchiveFields() {
411 global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
412 global $wgMultiContentRevisionSchemaMigrationStage;
414 if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
415 // If code is using this instead of self::getQueryInfo(), there's a
416 // decent chance it's going to try to directly access
417 // $row->ar_user or $row->ar_user_text and we can't give it
418 // useful values here once those aren't being written anymore.
419 throw new BadMethodCallException(
420 'Cannot use ' . __METHOD__
. ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
424 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
425 // If code is using this instead of self::getQueryInfo(), there's a
426 // decent chance it's going to try to directly access
427 // $row->ar_text_id or $row->ar_content_model and we can't give it
428 // useful values here once those aren't being written anymore,
429 // and may not exist at all.
430 throw new BadMethodCallException(
431 'Cannot use ' . __METHOD__
. ' when $wgMultiContentRevisionSchemaMigrationStage '
432 . 'does not have SCHEMA_COMPAT_WRITE_OLD set.'
436 wfDeprecated( __METHOD__
, '1.31' );
446 'ar_actor' => 'NULL',
454 $fields +
= CommentStore
::getStore()->getFields( 'ar_comment' );
456 if ( $wgContentHandlerUseDB ) {
457 $fields[] = 'ar_content_format';
458 $fields[] = 'ar_content_model';
464 * Return the list of text fields that should be selected to read the
466 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
469 public static function selectTextFields() {
470 wfDeprecated( __METHOD__
, '1.31' );
478 * Return the list of page fields that should be selected from page table
479 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
482 public static function selectPageFields() {
483 wfDeprecated( __METHOD__
, '1.31' );
495 * Return the list of user fields that should be selected from user table
496 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
499 public static function selectUserFields() {
500 wfDeprecated( __METHOD__
, '1.31' );
501 return [ 'user_name' ];
505 * Return the tables, fields, and join conditions to be selected to create
506 * a new revision object.
508 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
509 * @param array $options Any combination of the following strings
510 * - 'page': Join with the page table, and select fields to identify the page
511 * - 'user': Join with the user table, and select the user name
512 * - 'text': Join with the text table, and select fields to load page text
513 * @return array With three keys:
514 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
515 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
516 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
518 public static function getQueryInfo( $options = [] ) {
519 return self
::getRevisionStore()->getQueryInfo( $options );
523 * Return the tables, fields, and join conditions to be selected to create
524 * a new archived revision object.
526 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
527 * @return array With three keys:
528 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
529 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
530 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
532 public static function getArchiveQueryInfo() {
533 return self
::getRevisionStore()->getArchiveQueryInfo();
537 * Do a batched query to get the parent revision lengths
539 * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
541 * @param IDatabase $db
542 * @param array $revIds
545 public static function getParentLengths( $db, array $revIds ) {
546 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
550 * @param object|array|RevisionRecord $row Either a database row or an array
551 * @param int $queryFlags
552 * @param Title|null $title
556 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
559 if ( $row instanceof RevisionRecord
) {
560 $this->mRecord
= $row;
561 } elseif ( is_array( $row ) ) {
562 // If no user is specified, fall back to using the global user object, to stay
563 // compatible with pre-1.31 behavior.
564 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
565 $row['user'] = $wgUser;
568 $this->mRecord
= self
::getRevisionFactory()->newMutableRevisionFromArray(
571 $this->ensureTitle( $row, $queryFlags, $title )
573 } elseif ( is_object( $row ) ) {
574 $this->mRecord
= self
::getRevisionFactory()->newRevisionFromRow(
577 $this->ensureTitle( $row, $queryFlags, $title )
580 throw new InvalidArgumentException(
581 '$row must be a row object, an associative array, or a RevisionRecord'
587 * Make sure we have *some* Title object for use by the constructor.
588 * For B/C, the constructor shouldn't fail even for a bad page ID or bad revision ID.
590 * @param array|object $row
591 * @param int $queryFlags
592 * @param Title|null $title
594 * @return Title $title if not null, or a Title constructed from information in $row.
596 private function ensureTitle( $row, $queryFlags, $title = null ) {
601 if ( is_array( $row ) ) {
602 if ( isset( $row['title'] ) ) {
603 if ( !( $row['title'] instanceof Title
) ) {
604 throw new MWException( 'title field must contain a Title object.' );
607 return $row['title'];
610 $pageId = $row['page'] ??
0;
611 $revId = $row['id'] ??
0;
613 $pageId = $row->rev_page ??
0;
614 $revId = $row->rev_id ??
0;
618 $title = self
::getRevisionStore()->getTitle( $pageId, $revId, $queryFlags );
619 } catch ( RevisionAccessException
$ex ) {
620 // construct a dummy title!
621 wfLogWarning( __METHOD__
. ': ' . $ex->getMessage() );
623 // NOTE: this Title will only be used inside RevisionRecord
624 $title = Title
::makeTitleSafe( NS_SPECIAL
, "Badtitle/ID=$pageId" );
625 $title->resetArticleID( $pageId );
632 * @return RevisionRecord
634 public function getRevisionRecord() {
635 return $this->mRecord
;
643 public function getId() {
644 return $this->mRecord
->getId();
648 * Set the revision ID
650 * This should only be used for proposed revisions that turn out to be null edits.
652 * @note Only supported on Revisions that were constructed based on associative arrays,
653 * since they are mutable.
656 * @param int|string $id
657 * @throws MWException
659 public function setId( $id ) {
660 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
661 $this->mRecord
->setId( intval( $id ) );
663 throw new MWException( __METHOD__
. ' is not supported on this instance' );
668 * Set the user ID/name
670 * This should only be used for proposed revisions that turn out to be null edits
672 * @note Only supported on Revisions that were constructed based on associative arrays,
673 * since they are mutable.
676 * @deprecated since 1.31, please reuse old Revision object
677 * @param int $id User ID
678 * @param string $name User name
679 * @throws MWException
681 public function setUserIdAndName( $id, $name ) {
682 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
683 $user = User
::newFromAnyId( intval( $id ), $name, null );
684 $this->mRecord
->setUser( $user );
686 throw new MWException( __METHOD__
. ' is not supported on this instance' );
693 private function getMainSlotRaw() {
694 return $this->mRecord
->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
698 * Get the ID of the row of the text table that contains the content of the
699 * revision's main slot, if that content is stored in the text table.
701 * If the content is stored elsewhere, this returns null.
703 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
704 * get that actual address that can be used with BlobStore::getBlob(); or use
705 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
709 public function getTextId() {
710 $slot = $this->getMainSlotRaw();
711 return $slot->hasAddress()
712 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
717 * Get parent revision ID (the original previous page revision)
719 * @return int|null The ID of the parent revision. 0 indicates that there is no
720 * parent revision. Null indicates that the parent revision is not known.
722 public function getParentId() {
723 return $this->mRecord
->getParentId();
727 * Returns the length of the text in this revision, or null if unknown.
731 public function getSize() {
733 return $this->mRecord
->getSize();
734 } catch ( RevisionAccessException
$ex ) {
740 * Returns the base36 sha1 of the content in this revision, or null if unknown.
742 * @return string|null
744 public function getSha1() {
746 return $this->mRecord
->getSha1();
747 } catch ( RevisionAccessException
$ex ) {
753 * Returns the title of the page associated with this entry.
754 * Since 1.31, this will never return null.
756 * Will do a query, when title is not set and id is given.
760 public function getTitle() {
761 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
762 return Title
::newFromLinkTarget( $linkTarget );
766 * Set the title of the revision
768 * @deprecated since 1.31, this is now a noop. Pass the Title to the constructor instead.
770 * @param Title $title
772 public function setTitle( $title ) {
773 if ( !$title->equals( $this->getTitle() ) ) {
774 throw new InvalidArgumentException(
775 $title->getPrefixedText()
776 . ' is not the same as '
777 . $this->mRecord
->getPageAsLinkTarget()->__toString()
787 public function getPage() {
788 return $this->mRecord
->getPageId();
792 * Fetch revision's user id if it's available to the specified audience.
793 * If the specified audience does not have access to it, zero will be
796 * @param int $audience One of:
797 * Revision::FOR_PUBLIC to be displayed to all users
798 * Revision::FOR_THIS_USER to be displayed to the given user
799 * Revision::RAW get the ID regardless of permissions
800 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
801 * to the $audience parameter
804 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
807 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
811 $user = $this->mRecord
->getUser( $audience, $user );
812 return $user ?
$user->getId() : 0;
816 * Fetch revision's username if it's available to the specified audience.
817 * If the specified audience does not have access to the username, an
818 * empty string will be returned.
820 * @param int $audience One of:
821 * Revision::FOR_PUBLIC to be displayed to all users
822 * Revision::FOR_THIS_USER to be displayed to the given user
823 * Revision::RAW get the text regardless of permissions
824 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
825 * to the $audience parameter
828 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
831 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
835 $user = $this->mRecord
->getUser( $audience, $user );
836 return $user ?
$user->getName() : '';
839 * Fetch revision comment if it's available to the specified audience.
840 * If the specified audience does not have access to the comment, an
841 * empty string will be returned.
843 * @param int $audience One of:
844 * Revision::FOR_PUBLIC to be displayed to all users
845 * Revision::FOR_THIS_USER to be displayed to the given user
846 * Revision::RAW get the text regardless of permissions
847 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
848 * to the $audience parameter
851 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
854 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
858 $comment = $this->mRecord
->getComment( $audience, $user );
859 return $comment === null ?
null : $comment->text
;
865 public function isMinor() {
866 return $this->mRecord
->isMinor();
870 * @return int Rcid of the unpatrolled row, zero if there isn't one
872 public function isUnpatrolled() {
873 return self
::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord
);
877 * Get the RC object belonging to the current revision, if there's one
879 * @param int $flags (optional) $flags include:
880 * Revision::READ_LATEST : Select the data from the master
883 * @return RecentChange|null
885 public function getRecentChange( $flags = 0 ) {
886 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
890 * @param int $field One of DELETED_* bitfield constants
894 public function isDeleted( $field ) {
895 return $this->mRecord
->isDeleted( $field );
899 * Get the deletion bitfield of the revision
903 public function getVisibility() {
904 return $this->mRecord
->getVisibility();
908 * Fetch revision content if it's available to the specified audience.
909 * If the specified audience does not have the ability to view this
910 * revision, or the content could not be loaded, null will be returned.
912 * @param int $audience One of:
913 * Revision::FOR_PUBLIC to be displayed to all users
914 * Revision::FOR_THIS_USER to be displayed to $user
915 * Revision::RAW get the text regardless of permissions
916 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
917 * to the $audience parameter
919 * @return Content|null
921 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
924 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
929 return $this->mRecord
->getContent( SlotRecord
::MAIN
, $audience, $user );
931 catch ( RevisionAccessException
$e ) {
937 * Get original serialized data (without checking view restrictions)
940 * @deprecated since 1.31, use BlobStore::getBlob instead.
944 public function getSerializedData() {
945 $slot = $this->getMainSlotRaw();
946 return $slot->getContent()->serialize();
950 * Returns the content model for the main slot of this revision.
952 * If no content model was stored in the database, the default content model for the title is
953 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
954 * is used as a last resort.
956 * @todo drop this, with MCR, there no longer is a single model associated with a revision.
958 * @return string The content model id associated with this revision,
959 * see the CONTENT_MODEL_XXX constants.
961 public function getContentModel() {
962 return $this->getMainSlotRaw()->getModel();
966 * Returns the content format for the main slot of this revision.
968 * If no content format was stored in the database, the default format for this
969 * revision's content model is returned.
971 * @todo drop this, the format is irrelevant to the revision!
973 * @return string The content format id associated with this revision,
974 * see the CONTENT_FORMAT_XXX constants.
976 public function getContentFormat() {
977 $format = $this->getMainSlotRaw()->getFormat();
979 if ( $format === null ) {
980 // if no format was stored along with the blob, fall back to default format
981 $format = $this->getContentHandler()->getDefaultFormat();
988 * Returns the content handler appropriate for this revision's content model.
990 * @throws MWException
991 * @return ContentHandler
993 public function getContentHandler() {
994 return ContentHandler
::getForModelID( $this->getContentModel() );
1000 public function getTimestamp() {
1001 return $this->mRecord
->getTimestamp();
1007 public function isCurrent() {
1008 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
1012 * Get previous revision for this title
1014 * @return Revision|null
1016 public function getPrevious() {
1017 $title = $this->getTitle();
1018 $rec = self
::getRevisionLookup()->getPreviousRevision( $this->mRecord
, $title );
1019 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
1023 * Get next revision for this title
1025 * @return Revision|null
1027 public function getNext() {
1028 $title = $this->getTitle();
1029 $rec = self
::getRevisionLookup()->getNextRevision( $this->mRecord
, $title );
1030 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
1034 * Get revision text associated with an old or archive row
1036 * Both the flags and the text field must be included. Including the old_id
1037 * field will activate cache usage as long as the $wiki parameter is not set.
1039 * @param stdClass $row The text data
1040 * @param string $prefix Table prefix (default 'old_')
1041 * @param string|bool $wiki The name of the wiki to load the revision text from
1042 * (same as the wiki $row was loaded from) or false to indicate the local
1043 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1044 * identifier as understood by the LoadBalancer class.
1045 * @return string|false Text the text requested or false on failure
1047 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1048 $textField = $prefix . 'text';
1049 $flagsField = $prefix . 'flags';
1051 if ( isset( $row->$flagsField ) ) {
1052 $flags = explode( ',', $row->$flagsField );
1057 if ( isset( $row->$textField ) ) {
1058 $text = $row->$textField;
1063 $cacheKey = isset( $row->old_id
)
1064 ? SqlBlobStore
::makeAddressFromTextId( $row->old_id
)
1067 $revisionText = self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
1069 if ( $revisionText === false ) {
1070 if ( isset( $row->old_id
) ) {
1071 wfLogWarning( __METHOD__
. ": Bad data in text row {$row->old_id}! " );
1073 wfLogWarning( __METHOD__
. ": Bad data in text row! " );
1078 return $revisionText;
1082 * If $wgCompressRevisions is enabled, we will compress data.
1083 * The input string is modified in place.
1084 * Return value is the flags field: contains 'gzip' if the
1085 * data is compressed, and 'utf-8' if we're saving in UTF-8
1088 * @param mixed &$text Reference to a text
1091 public static function compressRevisionText( &$text ) {
1092 return self
::getBlobStore()->compressData( $text );
1096 * Re-converts revision text according to it's flags.
1098 * @param mixed $text Reference to a text
1099 * @param array $flags Compression flags
1100 * @return string|bool Decompressed text, or false on failure
1102 public static function decompressRevisionText( $text, $flags ) {
1103 if ( $text === false ) {
1104 // Text failed to be fetched; nothing to do
1108 return self
::getBlobStore()->decompressData( $text, $flags );
1112 * Insert a new revision into the database, returning the new revision ID
1113 * number on success and dies horribly on failure.
1115 * @param IDatabase $dbw (master connection)
1116 * @throws MWException
1117 * @return int The revision ID
1119 public function insertOn( $dbw ) {
1122 // Note that $this->mRecord->getId() will typically return null here, but not always,
1123 // e.g. not when restoring a revision.
1125 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
1126 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
1127 $this->mRecord
->setUser( $wgUser );
1129 throw new MWException( 'Cannot insert revision with no associated user.' );
1133 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1135 $this->mRecord
= $rec;
1137 // Avoid PHP 7.1 warning of passing $this by reference
1140 return $rec->getId();
1144 * Get the base 36 SHA-1 value for a string of text
1145 * @param string $text
1148 public static function base36Sha1( $text ) {
1149 return SlotRecord
::base36Sha1( $text );
1153 * Create a new null-revision for insertion into a page's
1154 * history. This will not re-save the text, but simply refer
1155 * to the text from the previous version.
1157 * Such revisions can for instance identify page rename
1158 * operations and other such meta-modifications.
1160 * @param IDatabase $dbw
1161 * @param int $pageId ID number of the page to read from
1162 * @param string $summary Revision's summary
1163 * @param bool $minor Whether the revision should be considered as minor
1164 * @param User|null $user User object to use or null for $wgUser
1165 * @return Revision|null Revision or null on error
1167 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1173 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1175 $title = Title
::newFromID( $pageId, Title
::GAID_FOR_UPDATE
);
1176 if ( $title === null ) {
1180 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1182 return $rec ?
new Revision( $rec ) : null;
1186 * Determine if the current user is allowed to view a particular
1187 * field of this revision, if it's marked as deleted.
1189 * @param int $field One of self::DELETED_TEXT,
1190 * self::DELETED_COMMENT,
1191 * self::DELETED_USER
1192 * @param User|null $user User object to check, or null to use $wgUser
1195 public function userCan( $field, User
$user = null ) {
1196 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1200 * Determine if the current user is allowed to view a particular
1201 * field of this revision, if it's marked as deleted. This is used
1202 * by various classes to avoid duplication.
1204 * @param int $bitfield Current field
1205 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1206 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1207 * self::DELETED_USER = File::DELETED_USER
1208 * @param User|null $user User object to check, or null to use $wgUser
1209 * @param Title|null $title A Title object to check for per-page restrictions on,
1210 * instead of just plain userrights
1213 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1222 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1226 * Get rev_timestamp from rev_id, without loading the rest of the row
1228 * @param Title $title
1231 * @return string|bool False if not found
1233 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1234 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1238 * Get count of revisions per page...not very efficient
1240 * @param IDatabase $db
1241 * @param int $id Page id
1244 static function countByPageId( $db, $id ) {
1245 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1249 * Get count of revisions per page...not very efficient
1251 * @param IDatabase $db
1252 * @param Title $title
1255 static function countByTitle( $db, $title ) {
1256 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1260 * Check if no edits were made by other users since
1261 * the time a user started editing the page. Limit to
1262 * 50 revisions for the sake of performance.
1265 * @deprecated since 1.24
1267 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1268 * Database object or a database identifier usable with wfGetDB.
1269 * @param int $pageId The ID of the page in question
1270 * @param int $userId The ID of the user in question
1271 * @param string $since Look at edits since this time
1273 * @return bool True if the given user was the only one to edit since the given timestamp
1275 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1276 if ( is_int( $db ) ) {
1277 $db = wfGetDB( $db );
1280 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1284 * Load a revision based on a known page ID and current revision ID from the DB
1286 * This method allows for the use of caching, though accessing anything that normally
1287 * requires permission checks (aside from the text) will trigger a small DB lookup.
1288 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1290 * @param IDatabase $db ignored!
1291 * @param int|Title $pageIdOrTitle Page ID or Title object
1292 * @param int $revId Known current revision of this page. Determined automatically if not given.
1293 * @return Revision|bool Returns false if missing
1296 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1297 $title = $pageIdOrTitle instanceof Title
1299 : Title
::newFromID( $pageIdOrTitle );
1305 $record = self
::getRevisionLookup()->getKnownCurrentRevision( $title, $revId );
1306 return $record ?
new Revision( $record ) : false;