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\Assert\Assert
;
33 use Wikimedia\Rdbms\IDatabase
;
34 use MediaWiki\Linker\LinkTarget
;
35 use MediaWiki\MediaWikiServices
;
36 use Wikimedia\Rdbms\ResultWrapper
;
37 use Wikimedia\Rdbms\FakeResultWrapper
;
40 * @deprecated since 1.31, use RevisionRecord, RevisionStore, and BlobStore instead.
42 class Revision
implements IDBAccessObject
{
44 /** @var RevisionRecord */
47 // Revision deletion constants
48 const DELETED_TEXT
= RevisionRecord
::DELETED_TEXT
;
49 const DELETED_COMMENT
= RevisionRecord
::DELETED_COMMENT
;
50 const DELETED_USER
= RevisionRecord
::DELETED_USER
;
51 const DELETED_RESTRICTED
= RevisionRecord
::DELETED_RESTRICTED
;
52 const SUPPRESSED_USER
= RevisionRecord
::SUPPRESSED_USER
;
53 const SUPPRESSED_ALL
= RevisionRecord
::SUPPRESSED_ALL
;
55 // Audience options for accessors
56 const FOR_PUBLIC
= RevisionRecord
::FOR_PUBLIC
;
57 const FOR_THIS_USER
= RevisionRecord
::FOR_THIS_USER
;
58 const RAW
= RevisionRecord
::RAW
;
60 const TEXT_CACHE_GROUP
= SqlBlobStore
::TEXT_CACHE_GROUP
;
63 * @return RevisionStore
65 protected static function getRevisionStore( $wiki = false ) {
67 return MediaWikiServices
::getInstance()->getRevisionStoreFactory()
68 ->getRevisionStore( $wiki );
70 return MediaWikiServices
::getInstance()->getRevisionStore();
75 * @return RevisionLookup
77 protected static function getRevisionLookup() {
78 return MediaWikiServices
::getInstance()->getRevisionLookup();
82 * @return RevisionFactory
84 protected static function getRevisionFactory() {
85 return MediaWikiServices
::getInstance()->getRevisionFactory();
89 * @param bool|string $wiki The ID of the target wiki database. Use false for the local wiki.
91 * @return SqlBlobStore
93 protected static function getBlobStore( $wiki = false ) {
94 $store = MediaWikiServices
::getInstance()
95 ->getBlobStoreFactory()
96 ->newSqlBlobStore( $wiki );
98 if ( !$store instanceof SqlBlobStore
) {
99 throw new RuntimeException(
100 'The backwards compatibility code in Revision currently requires the BlobStore '
101 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
109 * Load a page revision from a given revision ID number.
110 * Returns null if no such revision can be found.
113 * Revision::READ_LATEST : Select the data from the master
114 * Revision::READ_LOCKING : Select & lock the data from the master
117 * @param int $flags (optional)
118 * @return Revision|null
120 public static function newFromId( $id, $flags = 0 ) {
121 $rec = self
::getRevisionLookup()->getRevisionById( $id, $flags );
122 return $rec === null ?
null : new Revision( $rec, $flags );
126 * Load either the current, or a specified, revision
127 * that's attached to a given link target. If not attached
128 * to that link target, will return null.
131 * Revision::READ_LATEST : Select the data from the master
132 * Revision::READ_LOCKING : Select & lock the data from the master
134 * @param LinkTarget $linkTarget
135 * @param int $id (optional)
136 * @param int $flags Bitfield (optional)
137 * @return Revision|null
139 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
140 $rec = self
::getRevisionLookup()->getRevisionByTitle( $linkTarget, $id, $flags );
141 return $rec === null ?
null : new Revision( $rec, $flags );
145 * Load either the current, or a specified, revision
146 * that's attached to a given page ID.
147 * Returns null if no such revision can be found.
150 * Revision::READ_LATEST : Select the data from the master (since 1.20)
151 * Revision::READ_LOCKING : Select & lock the data from the master
154 * @param int $revId (optional)
155 * @param int $flags Bitfield (optional)
156 * @return Revision|null
158 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
159 $rec = self
::getRevisionLookup()->getRevisionByPageId( $pageId, $revId, $flags );
160 return $rec === null ?
null : new Revision( $rec, $flags );
164 * Make a fake revision object from an archive table row. This is queried
165 * for permissions or even inserted (as in Special:Undelete)
168 * @param array $overrides
170 * @throws MWException
173 public static function newFromArchiveRow( $row, $overrides = [] ) {
175 * MCR Migration: https://phabricator.wikimedia.org/T183564
176 * This method used to overwrite attributes, then passed to Revision::__construct
177 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
178 * So do a conversion here.
180 if ( array_key_exists( 'page', $overrides ) ) {
181 $overrides['page_id'] = $overrides['page'];
182 unset( $overrides['page'] );
186 * We require a Title for both the Revision object and the RevisionRecord.
187 * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
188 * to fetch a title in order pass it into the Revision object.
191 if ( isset( $overrides['title'] ) ) {
192 if ( !( $overrides['title'] instanceof Title
) ) {
193 throw new MWException( 'title field override must contain a Title object.' );
196 $title = $overrides['title'];
198 if ( $title !== null ) {
199 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
200 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
202 throw new InvalidArgumentException(
203 'A Title or ar_namespace and ar_title must be given'
208 $rec = self
::getRevisionFactory()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
209 return new Revision( $rec, self
::READ_NORMAL
, $title );
215 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
216 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
217 * a MutableRevisionRecord should be constructed directly.
218 * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
219 * but should be avoided.
221 * @param object|array $row
224 public static function newFromRow( $row ) {
225 if ( is_array( $row ) ) {
226 $rec = self
::getRevisionFactory()->newMutableRevisionFromArray( $row );
228 $rec = self
::getRevisionFactory()->newRevisionFromRow( $row );
231 return new Revision( $rec );
235 * Load a page revision from a given revision ID number.
236 * Returns null if no such revision can be found.
238 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
240 * @param IDatabase $db
242 * @return Revision|null
244 public static function loadFromId( $db, $id ) {
245 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
246 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
247 return $rec === null ?
null : new Revision( $rec );
251 * Load either the current, or a specified, revision
252 * that's attached to a given page. If not attached
253 * to that page, will return null.
255 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
257 * @param IDatabase $db
260 * @return Revision|null
262 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
263 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
264 return $rec === null ?
null : new Revision( $rec );
268 * Load either the current, or a specified, revision
269 * that's attached to a given page. If not attached
270 * to that page, will return null.
272 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
274 * @param IDatabase $db
275 * @param Title $title
277 * @return Revision|null
279 public static function loadFromTitle( $db, $title, $id = 0 ) {
280 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
281 return $rec === null ?
null : new Revision( $rec );
285 * Load the revision for the given title with the given timestamp.
286 * WARNING: Timestamps may in some circumstances not be unique,
287 * so this isn't the best key to use.
289 * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
290 * or RevisionStore::loadRevisionFromTimestamp() instead.
292 * @param IDatabase $db
293 * @param Title $title
294 * @param string $timestamp
295 * @return Revision|null
297 public static function loadFromTimestamp( $db, $title, $timestamp ) {
298 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
299 return $rec === null ?
null : new Revision( $rec );
303 * Return a wrapper for a series of database rows to
304 * fetch all of a given page's revisions in turn.
305 * Each row can be fed to the constructor to get objects.
307 * @param LinkTarget $title
308 * @return ResultWrapper
309 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
311 public static function fetchRevision( LinkTarget
$title ) {
312 wfDeprecated( __METHOD__
, '1.31' );
313 return new FakeResultWrapper( [] );
317 * Return the value of a select() JOIN conds array for the user table.
318 * This will get user table rows for logged-in users.
320 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
323 public static function userJoinCond() {
324 global $wgActorTableSchemaMigrationStage;
326 wfDeprecated( __METHOD__
, '1.31' );
327 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
328 // If code is using this instead of self::getQueryInfo(), there's
329 // no way the join it's trying to do can work once the old fields
330 // aren't being used anymore.
331 throw new BadMethodCallException(
332 'Cannot use ' . __METHOD__
333 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
337 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
341 * Return the value of a select() page conds array for the page table.
342 * This will assure that the revision(s) are not orphaned from live pages.
344 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
347 public static function pageJoinCond() {
348 wfDeprecated( __METHOD__
, '1.31' );
349 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
353 * Return the list of revision fields that should be selected to create
355 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
358 public static function selectFields() {
359 global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
360 global $wgMultiContentRevisionSchemaMigrationStage;
362 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
363 // If code is using this instead of self::getQueryInfo(), there's a
364 // decent chance it's going to try to directly access
365 // $row->rev_user or $row->rev_user_text and we can't give it
366 // useful values here once those aren't being used anymore.
367 throw new BadMethodCallException(
368 'Cannot use ' . __METHOD__
369 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
373 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
374 // If code is using this instead of self::getQueryInfo(), there's a
375 // decent chance it's going to try to directly access
376 // $row->rev_text_id or $row->rev_content_model and we can't give it
377 // useful values here once those aren't being written anymore,
378 // and may not exist at all.
379 throw new BadMethodCallException(
380 'Cannot use ' . __METHOD__
. ' when $wgMultiContentRevisionSchemaMigrationStage '
381 . 'does not have SCHEMA_COMPAT_WRITE_OLD set.'
385 wfDeprecated( __METHOD__
, '1.31' );
394 'rev_actor' => 'NULL',
402 $fields +
= CommentStore
::getStore()->getFields( 'rev_comment' );
404 if ( $wgContentHandlerUseDB ) {
405 $fields[] = 'rev_content_format';
406 $fields[] = 'rev_content_model';
413 * Return the list of revision fields that should be selected to create
414 * a new revision from an archive row.
415 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
418 public static function selectArchiveFields() {
419 global $wgContentHandlerUseDB, $wgActorTableSchemaMigrationStage;
420 global $wgMultiContentRevisionSchemaMigrationStage;
422 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
423 // If code is using this instead of self::getQueryInfo(), there's a
424 // decent chance it's going to try to directly access
425 // $row->ar_user or $row->ar_user_text and we can't give it
426 // useful values here once those aren't being used anymore.
427 throw new BadMethodCallException(
428 'Cannot use ' . __METHOD__
429 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
433 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
434 // If code is using this instead of self::getQueryInfo(), there's a
435 // decent chance it's going to try to directly access
436 // $row->ar_text_id or $row->ar_content_model and we can't give it
437 // useful values here once those aren't being written anymore,
438 // and may not exist at all.
439 throw new BadMethodCallException(
440 'Cannot use ' . __METHOD__
. ' when $wgMultiContentRevisionSchemaMigrationStage '
441 . 'does not have SCHEMA_COMPAT_WRITE_OLD set.'
445 wfDeprecated( __METHOD__
, '1.31' );
455 'ar_actor' => 'NULL',
463 $fields +
= CommentStore
::getStore()->getFields( 'ar_comment' );
465 if ( $wgContentHandlerUseDB ) {
466 $fields[] = 'ar_content_format';
467 $fields[] = 'ar_content_model';
473 * Return the list of text fields that should be selected to read the
475 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
478 public static function selectTextFields() {
479 wfDeprecated( __METHOD__
, '1.31' );
487 * Return the list of page fields that should be selected from page table
488 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
491 public static function selectPageFields() {
492 wfDeprecated( __METHOD__
, '1.31' );
504 * Return the list of user fields that should be selected from user table
505 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
508 public static function selectUserFields() {
509 wfDeprecated( __METHOD__
, '1.31' );
510 return [ 'user_name' ];
514 * Return the tables, fields, and join conditions to be selected to create
515 * a new revision object.
517 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
518 * @param array $options Any combination of the following strings
519 * - 'page': Join with the page table, and select fields to identify the page
520 * - 'user': Join with the user table, and select the user name
521 * - 'text': Join with the text table, and select fields to load page text
522 * @return array With three keys:
523 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
524 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
525 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
527 public static function getQueryInfo( $options = [] ) {
528 return self
::getRevisionStore()->getQueryInfo( $options );
532 * Return the tables, fields, and join conditions to be selected to create
533 * a new archived revision object.
535 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
536 * @return array With three keys:
537 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
538 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
539 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
541 public static function getArchiveQueryInfo() {
542 return self
::getRevisionStore()->getArchiveQueryInfo();
546 * Do a batched query to get the parent revision lengths
548 * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
550 * @param IDatabase $db
551 * @param array $revIds
554 public static function getParentLengths( $db, array $revIds ) {
555 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
559 * @param object|array|RevisionRecord $row Either a database row or an array
560 * @param int $queryFlags
561 * @param Title|null $title
565 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
568 if ( $row instanceof RevisionRecord
) {
569 $this->mRecord
= $row;
570 } elseif ( is_array( $row ) ) {
571 // If no user is specified, fall back to using the global user object, to stay
572 // compatible with pre-1.31 behavior.
573 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
574 $row['user'] = $wgUser;
577 $this->mRecord
= self
::getRevisionFactory()->newMutableRevisionFromArray(
580 $this->ensureTitle( $row, $queryFlags, $title )
582 } elseif ( is_object( $row ) ) {
583 $this->mRecord
= self
::getRevisionFactory()->newRevisionFromRow(
586 $this->ensureTitle( $row, $queryFlags, $title )
589 throw new InvalidArgumentException(
590 '$row must be a row object, an associative array, or a RevisionRecord'
594 Assert
::postcondition( $this->mRecord
!== null, 'Failed to construct a RevisionRecord' );
598 * Make sure we have *some* Title object for use by the constructor.
599 * For B/C, the constructor shouldn't fail even for a bad page ID or bad revision ID.
601 * @param array|object $row
602 * @param int $queryFlags
603 * @param Title|null $title
605 * @return Title $title if not null, or a Title constructed from information in $row.
607 private function ensureTitle( $row, $queryFlags, $title = null ) {
612 if ( is_array( $row ) ) {
613 if ( isset( $row['title'] ) ) {
614 if ( !( $row['title'] instanceof Title
) ) {
615 throw new MWException( 'title field must contain a Title object.' );
618 return $row['title'];
621 $pageId = $row['page'] ??
0;
622 $revId = $row['id'] ??
0;
624 $pageId = $row->rev_page ??
0;
625 $revId = $row->rev_id ??
0;
629 $title = self
::getRevisionStore()->getTitle( $pageId, $revId, $queryFlags );
630 } catch ( RevisionAccessException
$ex ) {
631 // construct a dummy title!
632 wfLogWarning( __METHOD__
. ': ' . $ex->getMessage() );
634 // NOTE: this Title will only be used inside RevisionRecord
635 $title = Title
::makeTitleSafe( NS_SPECIAL
, "Badtitle/ID=$pageId" );
636 $title->resetArticleID( $pageId );
643 * @return RevisionRecord
645 public function getRevisionRecord() {
646 return $this->mRecord
;
654 public function getId() {
655 return $this->mRecord
->getId();
659 * Set the revision ID
661 * This should only be used for proposed revisions that turn out to be null edits.
663 * @note Only supported on Revisions that were constructed based on associative arrays,
664 * since they are mutable.
667 * @param int|string $id
668 * @throws MWException
670 public function setId( $id ) {
671 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
672 $this->mRecord
->setId( intval( $id ) );
674 throw new MWException( __METHOD__
. ' is not supported on this instance' );
679 * Set the user ID/name
681 * This should only be used for proposed revisions that turn out to be null edits
683 * @note Only supported on Revisions that were constructed based on associative arrays,
684 * since they are mutable.
687 * @deprecated since 1.31, please reuse old Revision object
688 * @param int $id User ID
689 * @param string $name User name
690 * @throws MWException
692 public function setUserIdAndName( $id, $name ) {
693 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
694 $user = User
::newFromAnyId( intval( $id ), $name, null );
695 $this->mRecord
->setUser( $user );
697 throw new MWException( __METHOD__
. ' is not supported on this instance' );
704 private function getMainSlotRaw() {
705 return $this->mRecord
->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
709 * Get the ID of the row of the text table that contains the content of the
710 * revision's main slot, if that content is stored in the text table.
712 * If the content is stored elsewhere, this returns null.
714 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
715 * get that actual address that can be used with BlobStore::getBlob(); or use
716 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
720 public function getTextId() {
721 $slot = $this->getMainSlotRaw();
722 return $slot->hasAddress()
723 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
728 * Get parent revision ID (the original previous page revision)
730 * @return int|null The ID of the parent revision. 0 indicates that there is no
731 * parent revision. Null indicates that the parent revision is not known.
733 public function getParentId() {
734 return $this->mRecord
->getParentId();
738 * Returns the length of the text in this revision, or null if unknown.
742 public function getSize() {
744 return $this->mRecord
->getSize();
745 } catch ( RevisionAccessException
$ex ) {
751 * Returns the base36 sha1 of the content in this revision, or null if unknown.
753 * @return string|null
755 public function getSha1() {
757 return $this->mRecord
->getSha1();
758 } catch ( RevisionAccessException
$ex ) {
764 * Returns the title of the page associated with this entry.
765 * Since 1.31, this will never return null.
767 * Will do a query, when title is not set and id is given.
771 public function getTitle() {
772 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
773 return Title
::newFromLinkTarget( $linkTarget );
777 * Set the title of the revision
779 * @deprecated since 1.31, this is now a noop. Pass the Title to the constructor instead.
781 * @param Title $title
783 public function setTitle( $title ) {
784 if ( !$title->equals( $this->getTitle() ) ) {
785 throw new InvalidArgumentException(
786 $title->getPrefixedText()
787 . ' is not the same as '
788 . $this->mRecord
->getPageAsLinkTarget()->__toString()
798 public function getPage() {
799 return $this->mRecord
->getPageId();
803 * Fetch revision's user id if it's available to the specified audience.
804 * If the specified audience does not have access to it, zero will be
807 * @param int $audience One of:
808 * Revision::FOR_PUBLIC to be displayed to all users
809 * Revision::FOR_THIS_USER to be displayed to the given user
810 * Revision::RAW get the ID regardless of permissions
811 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
812 * to the $audience parameter
815 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
818 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
822 $user = $this->mRecord
->getUser( $audience, $user );
823 return $user ?
$user->getId() : 0;
827 * Fetch revision's username if it's available to the specified audience.
828 * If the specified audience does not have access to the username, an
829 * empty string will be returned.
831 * @param int $audience One of:
832 * Revision::FOR_PUBLIC to be displayed to all users
833 * Revision::FOR_THIS_USER to be displayed to the given user
834 * Revision::RAW get the text regardless of permissions
835 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
836 * to the $audience parameter
839 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
842 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
846 $user = $this->mRecord
->getUser( $audience, $user );
847 return $user ?
$user->getName() : '';
851 * Fetch revision comment if it's available to the specified audience.
852 * If the specified audience does not have access to the comment, an
853 * empty string will be returned.
855 * @param int $audience One of:
856 * Revision::FOR_PUBLIC to be displayed to all users
857 * Revision::FOR_THIS_USER to be displayed to the given user
858 * Revision::RAW get the text regardless of permissions
859 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
860 * to the $audience parameter
863 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
866 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
870 $comment = $this->mRecord
->getComment( $audience, $user );
871 return $comment === null ?
null : $comment->text
;
877 public function isMinor() {
878 return $this->mRecord
->isMinor();
882 * @return int Rcid of the unpatrolled row, zero if there isn't one
884 public function isUnpatrolled() {
885 return self
::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord
);
889 * Get the RC object belonging to the current revision, if there's one
891 * @param int $flags (optional) $flags include:
892 * Revision::READ_LATEST : Select the data from the master
895 * @return RecentChange|null
897 public function getRecentChange( $flags = 0 ) {
898 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
902 * @param int $field One of DELETED_* bitfield constants
906 public function isDeleted( $field ) {
907 return $this->mRecord
->isDeleted( $field );
911 * Get the deletion bitfield of the revision
915 public function getVisibility() {
916 return $this->mRecord
->getVisibility();
920 * Fetch revision content if it's available to the specified audience.
921 * If the specified audience does not have the ability to view this
922 * revision, or the content could not be loaded, null will be returned.
924 * @param int $audience One of:
925 * Revision::FOR_PUBLIC to be displayed to all users
926 * Revision::FOR_THIS_USER to be displayed to $user
927 * Revision::RAW get the text regardless of permissions
928 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
929 * to the $audience parameter
931 * @return Content|null
933 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
936 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
941 return $this->mRecord
->getContent( SlotRecord
::MAIN
, $audience, $user );
943 catch ( RevisionAccessException
$e ) {
949 * Get original serialized data (without checking view restrictions)
952 * @deprecated since 1.31, use BlobStore::getBlob instead.
956 public function getSerializedData() {
957 $slot = $this->getMainSlotRaw();
958 return $slot->getContent()->serialize();
962 * Returns the content model for the main slot of this revision.
964 * If no content model was stored in the database, the default content model for the title is
965 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
966 * is used as a last resort.
968 * @todo drop this, with MCR, there no longer is a single model associated with a revision.
970 * @return string The content model id associated with this revision,
971 * see the CONTENT_MODEL_XXX constants.
973 public function getContentModel() {
974 return $this->getMainSlotRaw()->getModel();
978 * Returns the content format for the main slot of this revision.
980 * If no content format was stored in the database, the default format for this
981 * revision's content model is returned.
983 * @todo drop this, the format is irrelevant to the revision!
985 * @return string The content format id associated with this revision,
986 * see the CONTENT_FORMAT_XXX constants.
988 public function getContentFormat() {
989 $format = $this->getMainSlotRaw()->getFormat();
991 if ( $format === null ) {
992 // if no format was stored along with the blob, fall back to default format
993 $format = $this->getContentHandler()->getDefaultFormat();
1000 * Returns the content handler appropriate for this revision's content model.
1002 * @throws MWException
1003 * @return ContentHandler
1005 public function getContentHandler() {
1006 return ContentHandler
::getForModelID( $this->getContentModel() );
1012 public function getTimestamp() {
1013 return $this->mRecord
->getTimestamp();
1019 public function isCurrent() {
1020 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
1024 * Get previous revision for this title
1026 * @return Revision|null
1028 public function getPrevious() {
1029 $title = $this->getTitle();
1030 $rec = self
::getRevisionLookup()->getPreviousRevision( $this->mRecord
, $title );
1031 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
1035 * Get next revision for this title
1037 * @return Revision|null
1039 public function getNext() {
1040 $title = $this->getTitle();
1041 $rec = self
::getRevisionLookup()->getNextRevision( $this->mRecord
, $title );
1042 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
1046 * Get revision text associated with an old or archive row
1048 * If the text field is not included, this uses RevisionStore to load the appropriate slot
1049 * and return its serialized content. This is the default backwards-compatibility behavior
1050 * when reading from the MCR aware database schema is enabled. For this to work, either
1051 * the revision ID or the page ID must be included in the row.
1053 * When using the old text field, the flags field must also be set. Including the old_id
1054 * field will activate cache usage as long as the $wiki parameter is not set.
1056 * @deprecated since 1.32, use RevisionStore::newRevisionFromRow instead.
1058 * @param stdClass $row The text data. If a falsy value is passed instead, false is returned.
1059 * @param string $prefix Table prefix (default 'old_')
1060 * @param string|bool $wiki The name of the wiki to load the revision text from
1061 * (same as the wiki $row was loaded from) or false to indicate the local
1062 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1063 * identifier as understood by the LoadBalancer class.
1064 * @return string|false Text the text requested or false on failure
1066 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1067 global $wgMultiContentRevisionSchemaMigrationStage;
1073 $textField = $prefix . 'text';
1074 $flagsField = $prefix . 'flags';
1076 if ( isset( $row->$textField ) ) {
1077 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
1078 // The text field was read, but it's no longer being populated!
1079 // We could gloss over this by using the text when it's there and loading
1080 // if when it's not, but it seems preferable to complain loudly about a
1081 // query that is no longer guaranteed to work reliably.
1082 throw new LogicException(
1083 'Cannot use ' . __METHOD__
. ' with the ' . $textField . ' field when'
1084 . ' $wgMultiContentRevisionSchemaMigrationStage does not include'
1085 . ' SCHEMA_COMPAT_WRITE_OLD. The field may not be populated for all revisions!'
1089 $text = $row->$textField;
1091 // Missing text field, we are probably looking at the MCR-enabled DB schema.
1093 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
1094 // This method should no longer be used with the new schema. Ideally, we
1095 // would already trigger a deprecation warning when SCHEMA_COMPAT_READ_NEW is set.
1096 wfDeprecated( __METHOD__
. ' (MCR without SCHEMA_COMPAT_WRITE_OLD)', '1.32' );
1099 $store = self
::getRevisionStore( $wiki );
1100 $rev = $prefix === 'ar_'
1101 ?
$store->newRevisionFromArchiveRow( $row )
1102 : $store->newRevisionFromRow( $row );
1104 $content = $rev->getContent( SlotRecord
::MAIN
);
1105 return $content ?
$content->serialize() : false;
1108 if ( isset( $row->$flagsField ) ) {
1109 $flags = explode( ',', $row->$flagsField );
1114 $cacheKey = isset( $row->old_id
)
1115 ? SqlBlobStore
::makeAddressFromTextId( $row->old_id
)
1118 $revisionText = self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
1120 if ( $revisionText === false ) {
1121 if ( isset( $row->old_id
) ) {
1122 wfLogWarning( __METHOD__
. ": Bad data in text row {$row->old_id}! " );
1124 wfLogWarning( __METHOD__
. ": Bad data in text row! " );
1129 return $revisionText;
1133 * If $wgCompressRevisions is enabled, we will compress data.
1134 * The input string is modified in place.
1135 * Return value is the flags field: contains 'gzip' if the
1136 * data is compressed, and 'utf-8' if we're saving in UTF-8
1139 * @param mixed &$text Reference to a text
1142 public static function compressRevisionText( &$text ) {
1143 return self
::getBlobStore()->compressData( $text );
1147 * Re-converts revision text according to it's flags.
1149 * @param mixed $text Reference to a text
1150 * @param array $flags Compression flags
1151 * @return string|bool Decompressed text, or false on failure
1153 public static function decompressRevisionText( $text, $flags ) {
1154 if ( $text === false ) {
1155 // Text failed to be fetched; nothing to do
1159 return self
::getBlobStore()->decompressData( $text, $flags );
1163 * Insert a new revision into the database, returning the new revision ID
1164 * number on success and dies horribly on failure.
1166 * @param IDatabase $dbw (master connection)
1167 * @throws MWException
1168 * @return int The revision ID
1170 public function insertOn( $dbw ) {
1173 // Note that $this->mRecord->getId() will typically return null here, but not always,
1174 // e.g. not when restoring a revision.
1176 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
1177 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
1178 $this->mRecord
->setUser( $wgUser );
1180 throw new MWException( 'Cannot insert revision with no associated user.' );
1184 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1186 $this->mRecord
= $rec;
1187 Assert
::postcondition( $this->mRecord
!== null, 'Failed to acquire a RevisionRecord' );
1189 return $rec->getId();
1193 * Get the base 36 SHA-1 value for a string of text
1194 * @param string $text
1197 public static function base36Sha1( $text ) {
1198 return SlotRecord
::base36Sha1( $text );
1202 * Create a new null-revision for insertion into a page's
1203 * history. This will not re-save the text, but simply refer
1204 * to the text from the previous version.
1206 * Such revisions can for instance identify page rename
1207 * operations and other such meta-modifications.
1209 * @param IDatabase $dbw
1210 * @param int $pageId ID number of the page to read from
1211 * @param string $summary Revision's summary
1212 * @param bool $minor Whether the revision should be considered as minor
1213 * @param User|null $user User object to use or null for $wgUser
1214 * @return Revision|null Revision or null on error
1216 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1222 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1224 $title = Title
::newFromID( $pageId, Title
::GAID_FOR_UPDATE
);
1225 if ( $title === null ) {
1229 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1231 return $rec ?
new Revision( $rec ) : null;
1235 * Determine if the current user is allowed to view a particular
1236 * field of this revision, if it's marked as deleted.
1238 * @param int $field One of self::DELETED_TEXT,
1239 * self::DELETED_COMMENT,
1240 * self::DELETED_USER
1241 * @param User|null $user User object to check, or null to use $wgUser
1244 public function userCan( $field, User
$user = null ) {
1245 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1249 * Determine if the current user is allowed to view a particular
1250 * field of this revision, if it's marked as deleted. This is used
1251 * by various classes to avoid duplication.
1253 * @param int $bitfield Current field
1254 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1255 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1256 * self::DELETED_USER = File::DELETED_USER
1257 * @param User|null $user User object to check, or null to use $wgUser
1258 * @param Title|null $title A Title object to check for per-page restrictions on,
1259 * instead of just plain userrights
1262 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1271 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1275 * Get rev_timestamp from rev_id, without loading the rest of the row
1277 * @param Title $title
1280 * @return string|bool False if not found
1282 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1283 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1287 * Get count of revisions per page...not very efficient
1289 * @param IDatabase $db
1290 * @param int $id Page id
1293 static function countByPageId( $db, $id ) {
1294 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1298 * Get count of revisions per page...not very efficient
1300 * @param IDatabase $db
1301 * @param Title $title
1304 static function countByTitle( $db, $title ) {
1305 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1309 * Check if no edits were made by other users since
1310 * the time a user started editing the page. Limit to
1311 * 50 revisions for the sake of performance.
1314 * @deprecated since 1.24
1316 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1317 * Database object or a database identifier usable with wfGetDB.
1318 * @param int $pageId The ID of the page in question
1319 * @param int $userId The ID of the user in question
1320 * @param string $since Look at edits since this time
1322 * @return bool True if the given user was the only one to edit since the given timestamp
1324 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1325 if ( is_int( $db ) ) {
1326 $db = wfGetDB( $db );
1329 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1333 * Load a revision based on a known page ID and current revision ID from the DB
1335 * This method allows for the use of caching, though accessing anything that normally
1336 * requires permission checks (aside from the text) will trigger a small DB lookup.
1337 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1339 * @param IDatabase $db ignored!
1340 * @param int|Title $pageIdOrTitle Page ID or Title object
1341 * @param int $revId Known current revision of this page. Determined automatically if not given.
1342 * @return Revision|bool Returns false if missing
1345 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1346 $title = $pageIdOrTitle instanceof Title
1348 : Title
::newFromID( $pageIdOrTitle );
1354 $record = self
::getRevisionLookup()->getKnownCurrentRevision( $title, $revId );
1355 return $record ?
new Revision( $record ) : false;