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\Storage\MutableRevisionRecord
;
24 use MediaWiki\Storage\RevisionAccessException
;
25 use MediaWiki\Storage\RevisionFactory
;
26 use MediaWiki\Storage\RevisionLookup
;
27 use MediaWiki\Storage\RevisionRecord
;
28 use MediaWiki\Storage\RevisionStore
;
29 use MediaWiki\Storage\RevisionStoreRecord
;
30 use MediaWiki\Storage\SlotRecord
;
31 use MediaWiki\Storage\SqlBlobStore
;
32 use MediaWiki\User\UserIdentityValue
;
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() {
66 return MediaWikiServices
::getInstance()->getRevisionStore();
70 * @return RevisionLookup
72 protected static function getRevisionLookup() {
73 return MediaWikiServices
::getInstance()->getRevisionLookup();
77 * @return RevisionFactory
79 protected static function getRevisionFactory() {
80 return MediaWikiServices
::getInstance()->getRevisionFactory();
84 * @param bool|string $wiki The ID of the target wiki database. Use false for the local wiki.
86 * @return SqlBlobStore
88 protected static function getBlobStore( $wiki = false ) {
89 $store = MediaWikiServices
::getInstance()
90 ->getBlobStoreFactory()
91 ->newSqlBlobStore( $wiki );
93 if ( !$store instanceof SqlBlobStore
) {
94 throw new RuntimeException(
95 'The backwards compatibility code in Revision currently requires the BlobStore '
96 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
104 * Load a page revision from a given revision ID number.
105 * Returns null if no such revision can be found.
108 * Revision::READ_LATEST : Select the data from the master
109 * Revision::READ_LOCKING : Select & lock the data from the master
112 * @param int $flags (optional)
113 * @return Revision|null
115 public static function newFromId( $id, $flags = 0 ) {
116 $rec = self
::getRevisionLookup()->getRevisionById( $id, $flags );
117 return $rec === null ?
null : new Revision( $rec, $flags );
121 * Load either the current, or a specified, revision
122 * that's attached to a given link target. If not attached
123 * to that link target, will return null.
126 * Revision::READ_LATEST : Select the data from the master
127 * Revision::READ_LOCKING : Select & lock the data from the master
129 * @param LinkTarget $linkTarget
130 * @param int $id (optional)
131 * @param int $flags Bitfield (optional)
132 * @return Revision|null
134 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
135 $rec = self
::getRevisionLookup()->getRevisionByTitle( $linkTarget, $id, $flags );
136 return $rec === null ?
null : new Revision( $rec, $flags );
140 * Load either the current, or a specified, revision
141 * that's attached to a given page ID.
142 * Returns null if no such revision can be found.
145 * Revision::READ_LATEST : Select the data from the master (since 1.20)
146 * Revision::READ_LOCKING : Select & lock the data from the master
149 * @param int $revId (optional)
150 * @param int $flags Bitfield (optional)
151 * @return Revision|null
153 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
154 $rec = self
::getRevisionLookup()->getRevisionByPageId( $pageId, $revId, $flags );
155 return $rec === null ?
null : new Revision( $rec, $flags );
159 * Make a fake revision object from an archive table row. This is queried
160 * for permissions or even inserted (as in Special:Undelete)
163 * @param array $overrides
165 * @throws MWException
168 public static function newFromArchiveRow( $row, $overrides = [] ) {
170 * MCR Migration: https://phabricator.wikimedia.org/T183564
171 * This method used to overwrite attributes, then passed to Revision::__construct
172 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
173 * So do a conversion here.
175 if ( array_key_exists( 'page', $overrides ) ) {
176 $overrides['page_id'] = $overrides['page'];
177 unset( $overrides['page'] );
181 * We require a Title for both the Revision object and the RevisionRecord.
182 * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
183 * to fetch a title in order pass it into the Revision object.
186 if ( isset( $overrides['title'] ) ) {
187 if ( !( $overrides['title'] instanceof Title
) ) {
188 throw new MWException( 'title field override must contain a Title object.' );
191 $title = $overrides['title'];
193 if ( $title !== null ) {
194 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
195 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
197 throw new InvalidArgumentException(
198 'A Title or ar_namespace and ar_title must be given'
203 $rec = self
::getRevisionFactory()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
204 return new Revision( $rec, self
::READ_NORMAL
, $title );
210 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
211 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
212 * a MutableRevisionRecord should be constructed directly.
213 * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
214 * but should be avoided.
216 * @param object|array $row
219 public static function newFromRow( $row ) {
220 if ( is_array( $row ) ) {
221 $rec = self
::getRevisionFactory()->newMutableRevisionFromArray( $row );
223 $rec = self
::getRevisionFactory()->newRevisionFromRow( $row );
226 return new Revision( $rec );
230 * Load a page revision from a given revision ID number.
231 * Returns null if no such revision can be found.
233 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
235 * @param IDatabase $db
237 * @return Revision|null
239 public static function loadFromId( $db, $id ) {
240 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
241 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
242 return $rec === null ?
null : new Revision( $rec );
246 * Load either the current, or a specified, revision
247 * that's attached to a given page. If not attached
248 * to that page, will return null.
250 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
252 * @param IDatabase $db
255 * @return Revision|null
257 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
258 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
259 return $rec === null ?
null : new Revision( $rec );
263 * Load either the current, or a specified, revision
264 * that's attached to a given page. If not attached
265 * to that page, will return null.
267 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
269 * @param IDatabase $db
270 * @param Title $title
272 * @return Revision|null
274 public static function loadFromTitle( $db, $title, $id = 0 ) {
275 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
276 return $rec === null ?
null : new Revision( $rec );
280 * Load the revision for the given title with the given timestamp.
281 * WARNING: Timestamps may in some circumstances not be unique,
282 * so this isn't the best key to use.
284 * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
285 * or RevisionStore::loadRevisionFromTimestamp() instead.
287 * @param IDatabase $db
288 * @param Title $title
289 * @param string $timestamp
290 * @return Revision|null
292 public static function loadFromTimestamp( $db, $title, $timestamp ) {
293 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
294 return $rec === null ?
null : new Revision( $rec );
298 * Return a wrapper for a series of database rows to
299 * fetch all of a given page's revisions in turn.
300 * Each row can be fed to the constructor to get objects.
302 * @param LinkTarget $title
303 * @return ResultWrapper
304 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
306 public static function fetchRevision( LinkTarget
$title ) {
307 wfDeprecated( __METHOD__
, '1.31' );
308 return new FakeResultWrapper( [] );
312 * Return the value of a select() JOIN conds array for the user table.
313 * This will get user table rows for logged-in users.
315 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
318 public static function userJoinCond() {
319 wfDeprecated( __METHOD__
, '1.31' );
320 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
324 * Return the value of a select() page conds array for the page table.
325 * This will assure that the revision(s) are not orphaned from live pages.
327 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
330 public static function pageJoinCond() {
331 wfDeprecated( __METHOD__
, '1.31' );
332 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
336 * Return the list of revision fields that should be selected to create
338 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
341 public static function selectFields() {
342 global $wgContentHandlerUseDB;
344 wfDeprecated( __METHOD__
, '1.31' );
360 $fields +
= CommentStore
::getStore()->getFields( 'rev_comment' );
362 if ( $wgContentHandlerUseDB ) {
363 $fields[] = 'rev_content_format';
364 $fields[] = 'rev_content_model';
371 * Return the list of revision fields that should be selected to create
372 * a new revision from an archive row.
373 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
376 public static function selectArchiveFields() {
377 global $wgContentHandlerUseDB;
379 wfDeprecated( __METHOD__
, '1.31' );
397 $fields +
= CommentStore
::getStore()->getFields( 'ar_comment' );
399 if ( $wgContentHandlerUseDB ) {
400 $fields[] = 'ar_content_format';
401 $fields[] = 'ar_content_model';
407 * Return the list of text fields that should be selected to read the
409 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
412 public static function selectTextFields() {
413 wfDeprecated( __METHOD__
, '1.31' );
421 * Return the list of page fields that should be selected from page table
422 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
425 public static function selectPageFields() {
426 wfDeprecated( __METHOD__
, '1.31' );
438 * Return the list of user fields that should be selected from user table
439 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
442 public static function selectUserFields() {
443 wfDeprecated( __METHOD__
, '1.31' );
444 return [ 'user_name' ];
448 * Return the tables, fields, and join conditions to be selected to create
449 * a new revision object.
451 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
452 * @param array $options Any combination of the following strings
453 * - 'page': Join with the page table, and select fields to identify the page
454 * - 'user': Join with the user table, and select the user name
455 * - 'text': Join with the text table, and select fields to load page text
456 * @return array With three keys:
457 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
458 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
459 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
461 public static function getQueryInfo( $options = [] ) {
462 return self
::getRevisionStore()->getQueryInfo( $options );
466 * Return the tables, fields, and join conditions to be selected to create
467 * a new archived revision object.
469 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
470 * @return array With three keys:
471 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
472 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
473 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
475 public static function getArchiveQueryInfo() {
476 return self
::getRevisionStore()->getArchiveQueryInfo();
480 * Do a batched query to get the parent revision lengths
482 * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
484 * @param IDatabase $db
485 * @param array $revIds
488 public static function getParentLengths( $db, array $revIds ) {
489 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
493 * @param object|array|RevisionRecord $row Either a database row or an array
494 * @param int $queryFlags
495 * @param Title|null $title
499 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
502 if ( $row instanceof RevisionRecord
) {
503 $this->mRecord
= $row;
504 } elseif ( is_array( $row ) ) {
505 // If no user is specified, fall back to using the global user object, to stay
506 // compatible with pre-1.31 behavior.
507 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
508 $row['user'] = $wgUser;
511 $this->mRecord
= self
::getRevisionFactory()->newMutableRevisionFromArray(
514 $this->ensureTitle( $row, $queryFlags, $title )
516 } elseif ( is_object( $row ) ) {
517 $this->mRecord
= self
::getRevisionFactory()->newRevisionFromRow(
520 $this->ensureTitle( $row, $queryFlags, $title )
523 throw new InvalidArgumentException(
524 '$row must be a row object, an associative array, or a RevisionRecord'
530 * Make sure we have *some* Title object for use by the constructor.
531 * For B/C, the constructor shouldn't fail even for a bad page ID or bad revision ID.
533 * @param array|object $row
534 * @param int $queryFlags
535 * @param Title|null $title
537 * @return Title $title if not null, or a Title constructed from information in $row.
539 private function ensureTitle( $row, $queryFlags, $title = null ) {
544 if ( is_array( $row ) ) {
545 if ( isset( $row['title'] ) ) {
546 if ( !( $row['title'] instanceof Title
) ) {
547 throw new MWException( 'title field must contain a Title object.' );
550 return $row['title'];
553 $pageId = isset( $row['page'] ) ?
$row['page'] : 0;
554 $revId = isset( $row['id'] ) ?
$row['id'] : 0;
556 $pageId = isset( $row->rev_page
) ?
$row->rev_page
: 0;
557 $revId = isset( $row->rev_id
) ?
$row->rev_id
: 0;
561 $title = self
::getRevisionStore()->getTitle( $pageId, $revId, $queryFlags );
562 } catch ( RevisionAccessException
$ex ) {
563 // construct a dummy title!
564 wfLogWarning( __METHOD__
. ': ' . $ex->getMessage() );
566 // NOTE: this Title will only be used inside RevisionRecord
567 $title = Title
::makeTitleSafe( NS_SPECIAL
, "Badtitle/ID=$pageId" );
568 $title->resetArticleID( $pageId );
575 * @return RevisionRecord
577 public function getRevisionRecord() {
578 return $this->mRecord
;
586 public function getId() {
587 return $this->mRecord
->getId();
591 * Set the revision ID
593 * This should only be used for proposed revisions that turn out to be null edits.
595 * @note Only supported on Revisions that were constructed based on associative arrays,
596 * since they are mutable.
599 * @param int|string $id
600 * @throws MWException
602 public function setId( $id ) {
603 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
604 $this->mRecord
->setId( intval( $id ) );
606 throw new MWException( __METHOD__
. ' is not supported on this instance' );
611 * Set the user ID/name
613 * This should only be used for proposed revisions that turn out to be null edits
615 * @note Only supported on Revisions that were constructed based on associative arrays,
616 * since they are mutable.
619 * @deprecated since 1.31, please reuse old Revision object
620 * @param int $id User ID
621 * @param string $name User name
622 * @throws MWException
624 public function setUserIdAndName( $id, $name ) {
625 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
626 $user = new UserIdentityValue( intval( $id ), $name );
627 $this->mRecord
->setUser( $user );
629 throw new MWException( __METHOD__
. ' is not supported on this instance' );
636 private function getMainSlotRaw() {
637 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
641 * Get the ID of the row of the text table that contains the content of the
642 * revision's main slot, if that content is stored in the text table.
644 * If the content is stored elsewhere, this returns null.
646 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
647 * get that actual address that can be used with BlobStore::getBlob(); or use
648 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
652 public function getTextId() {
653 $slot = $this->getMainSlotRaw();
654 return $slot->hasAddress()
655 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
660 * Get parent revision ID (the original previous page revision)
662 * @return int|null The ID of the parent revision. 0 indicates that there is no
663 * parent revision. Null indicates that the parent revision is not known.
665 public function getParentId() {
666 return $this->mRecord
->getParentId();
670 * Returns the length of the text in this revision, or null if unknown.
674 public function getSize() {
676 return $this->mRecord
->getSize();
677 } catch ( RevisionAccessException
$ex ) {
683 * Returns the base36 sha1 of the content in this revision, or null if unknown.
685 * @return string|null
687 public function getSha1() {
689 return $this->mRecord
->getSha1();
690 } catch ( RevisionAccessException
$ex ) {
696 * Returns the title of the page associated with this entry.
697 * Since 1.31, this will never return null.
699 * Will do a query, when title is not set and id is given.
703 public function getTitle() {
704 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
705 return Title
::newFromLinkTarget( $linkTarget );
709 * Set the title of the revision
711 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
713 * @param Title $title
715 public function setTitle( $title ) {
716 if ( !$title->equals( $this->getTitle() ) ) {
717 throw new InvalidArgumentException(
718 $title->getPrefixedText()
719 . ' is not the same as '
720 . $this->mRecord
->getPageAsLinkTarget()->__toString()
730 public function getPage() {
731 return $this->mRecord
->getPageId();
735 * Fetch revision's user id if it's available to the specified audience.
736 * If the specified audience does not have access to it, zero will be
739 * @param int $audience One of:
740 * Revision::FOR_PUBLIC to be displayed to all users
741 * Revision::FOR_THIS_USER to be displayed to the given user
742 * Revision::RAW get the ID regardless of permissions
743 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
744 * to the $audience parameter
747 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
750 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
754 $user = $this->mRecord
->getUser( $audience, $user );
755 return $user ?
$user->getId() : 0;
759 * Fetch revision's user id without regard for the current user's permissions
762 * @deprecated since 1.25, use getUser( Revision::RAW )
764 public function getRawUser() {
765 wfDeprecated( __METHOD__
, '1.25' );
766 return $this->getUser( self
::RAW
);
770 * Fetch revision's username if it's available to the specified audience.
771 * If the specified audience does not have access to the username, an
772 * empty string will be returned.
774 * @param int $audience One of:
775 * Revision::FOR_PUBLIC to be displayed to all users
776 * Revision::FOR_THIS_USER to be displayed to the given user
777 * Revision::RAW get the text regardless of permissions
778 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
779 * to the $audience parameter
782 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
785 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
789 $user = $this->mRecord
->getUser( $audience, $user );
790 return $user ?
$user->getName() : '';
794 * Fetch revision's username without regard for view restrictions
797 * @deprecated since 1.25, use getUserText( Revision::RAW )
799 public function getRawUserText() {
800 wfDeprecated( __METHOD__
, '1.25' );
801 return $this->getUserText( self
::RAW
);
805 * Fetch revision comment if it's available to the specified audience.
806 * If the specified audience does not have access to the comment, an
807 * empty string will be returned.
809 * @param int $audience One of:
810 * Revision::FOR_PUBLIC to be displayed to all users
811 * Revision::FOR_THIS_USER to be displayed to the given user
812 * Revision::RAW get the text regardless of permissions
813 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
814 * to the $audience parameter
817 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
820 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
824 $comment = $this->mRecord
->getComment( $audience, $user );
825 return $comment === null ?
null : $comment->text
;
829 * Fetch revision comment without regard for the current user's permissions
832 * @deprecated since 1.25, use getComment( Revision::RAW )
834 public function getRawComment() {
835 wfDeprecated( __METHOD__
, '1.25' );
836 return $this->getComment( self
::RAW
);
842 public function isMinor() {
843 return $this->mRecord
->isMinor();
847 * @return int Rcid of the unpatrolled row, zero if there isn't one
849 public function isUnpatrolled() {
850 return self
::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord
);
854 * Get the RC object belonging to the current revision, if there's one
856 * @param int $flags (optional) $flags include:
857 * Revision::READ_LATEST : Select the data from the master
860 * @return RecentChange|null
862 public function getRecentChange( $flags = 0 ) {
863 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
867 * @param int $field One of DELETED_* bitfield constants
871 public function isDeleted( $field ) {
872 return $this->mRecord
->isDeleted( $field );
876 * Get the deletion bitfield of the revision
880 public function getVisibility() {
881 return $this->mRecord
->getVisibility();
885 * Fetch revision content if it's available to the specified audience.
886 * If the specified audience does not have the ability to view this
887 * revision, or the content could not be loaded, null will be returned.
889 * @param int $audience One of:
890 * Revision::FOR_PUBLIC to be displayed to all users
891 * Revision::FOR_THIS_USER to be displayed to $user
892 * Revision::RAW get the text regardless of permissions
893 * @param User $user User object to check for, only if FOR_THIS_USER is passed
894 * to the $audience parameter
896 * @return Content|null
898 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
901 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
906 return $this->mRecord
->getContent( 'main', $audience, $user );
908 catch ( RevisionAccessException
$e ) {
911 __METHOD__
. ": Cannot get content: " . $e->getMessage() .
912 "\n" . $e->getTraceAsString()
919 * Get original serialized data (without checking view restrictions)
922 * @deprecated since 1.31, use BlobStore::getBlob instead.
926 public function getSerializedData() {
927 $slot = $this->getMainSlotRaw();
928 return $slot->getContent()->serialize();
932 * Returns the content model for the main slot of this revision.
934 * If no content model was stored in the database, the default content model for the title is
935 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
936 * is used as a last resort.
938 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
940 * @return string The content model id associated with this revision,
941 * see the CONTENT_MODEL_XXX constants.
943 public function getContentModel() {
944 return $this->getMainSlotRaw()->getModel();
948 * Returns the content format for the main slot of this revision.
950 * If no content format was stored in the database, the default format for this
951 * revision's content model is returned.
953 * @todo: drop this, the format is irrelevant to the revision!
955 * @return string The content format id associated with this revision,
956 * see the CONTENT_FORMAT_XXX constants.
958 public function getContentFormat() {
959 $format = $this->getMainSlotRaw()->getFormat();
961 if ( $format === null ) {
962 // if no format was stored along with the blob, fall back to default format
963 $format = $this->getContentHandler()->getDefaultFormat();
970 * Returns the content handler appropriate for this revision's content model.
972 * @throws MWException
973 * @return ContentHandler
975 public function getContentHandler() {
976 return ContentHandler
::getForModelID( $this->getContentModel() );
982 public function getTimestamp() {
983 return $this->mRecord
->getTimestamp();
989 public function isCurrent() {
990 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
994 * Get previous revision for this title
996 * @return Revision|null
998 public function getPrevious() {
999 $title = $this->getTitle();
1000 $rec = self
::getRevisionLookup()->getPreviousRevision( $this->mRecord
, $title );
1001 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
1005 * Get next revision for this title
1007 * @return Revision|null
1009 public function getNext() {
1010 $title = $this->getTitle();
1011 $rec = self
::getRevisionLookup()->getNextRevision( $this->mRecord
, $title );
1012 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
1016 * Get revision text associated with an old or archive row
1018 * Both the flags and the text field must be included. Including the old_id
1019 * field will activate cache usage as long as the $wiki parameter is not set.
1021 * @param stdClass $row The text data
1022 * @param string $prefix Table prefix (default 'old_')
1023 * @param string|bool $wiki The name of the wiki to load the revision text from
1024 * (same as the the wiki $row was loaded from) or false to indicate the local
1025 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1026 * identifier as understood by the LoadBalancer class.
1027 * @return string|false Text the text requested or false on failure
1029 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1030 $textField = $prefix . 'text';
1031 $flagsField = $prefix . 'flags';
1033 if ( isset( $row->$flagsField ) ) {
1034 $flags = explode( ',', $row->$flagsField );
1039 if ( isset( $row->$textField ) ) {
1040 $text = $row->$textField;
1045 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
1047 return self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
1051 * If $wgCompressRevisions is enabled, we will compress data.
1052 * The input string is modified in place.
1053 * Return value is the flags field: contains 'gzip' if the
1054 * data is compressed, and 'utf-8' if we're saving in UTF-8
1057 * @param mixed &$text Reference to a text
1060 public static function compressRevisionText( &$text ) {
1061 return self
::getBlobStore()->compressData( $text );
1065 * Re-converts revision text according to it's flags.
1067 * @param mixed $text Reference to a text
1068 * @param array $flags Compression flags
1069 * @return string|bool Decompressed text, or false on failure
1071 public static function decompressRevisionText( $text, $flags ) {
1072 return self
::getBlobStore()->decompressData( $text, $flags );
1076 * Insert a new revision into the database, returning the new revision ID
1077 * number on success and dies horribly on failure.
1079 * @param IDatabase $dbw (master connection)
1080 * @throws MWException
1081 * @return int The revision ID
1083 public function insertOn( $dbw ) {
1086 // Note that $this->mRecord->getId() will typically return null here, but not always,
1087 // e.g. not when restoring a revision.
1089 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
1090 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
1091 $this->mRecord
->setUser( $wgUser );
1093 throw new MWException( 'Cannot insert revision with no associated user.' );
1097 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1099 $this->mRecord
= $rec;
1101 // Avoid PHP 7.1 warning of passing $this by reference
1103 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1104 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1106 return $rec->getId();
1110 * Get the base 36 SHA-1 value for a string of text
1111 * @param string $text
1114 public static function base36Sha1( $text ) {
1115 return SlotRecord
::base36Sha1( $text );
1119 * Create a new null-revision for insertion into a page's
1120 * history. This will not re-save the text, but simply refer
1121 * to the text from the previous version.
1123 * Such revisions can for instance identify page rename
1124 * operations and other such meta-modifications.
1126 * @param IDatabase $dbw
1127 * @param int $pageId ID number of the page to read from
1128 * @param string $summary Revision's summary
1129 * @param bool $minor Whether the revision should be considered as minor
1130 * @param User|null $user User object to use or null for $wgUser
1131 * @return Revision|null Revision or null on error
1133 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1139 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1141 $title = Title
::newFromID( $pageId, Title
::GAID_FOR_UPDATE
);
1142 if ( $title === null ) {
1146 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1148 return new Revision( $rec );
1152 * Determine if the current user is allowed to view a particular
1153 * field of this revision, if it's marked as deleted.
1155 * @param int $field One of self::DELETED_TEXT,
1156 * self::DELETED_COMMENT,
1157 * self::DELETED_USER
1158 * @param User|null $user User object to check, or null to use $wgUser
1161 public function userCan( $field, User
$user = null ) {
1162 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1166 * Determine if the current user is allowed to view a particular
1167 * field of this revision, if it's marked as deleted. This is used
1168 * by various classes to avoid duplication.
1170 * @param int $bitfield Current field
1171 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1172 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1173 * self::DELETED_USER = File::DELETED_USER
1174 * @param User|null $user User object to check, or null to use $wgUser
1175 * @param Title|null $title A Title object to check for per-page restrictions on,
1176 * instead of just plain userrights
1179 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1188 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1192 * Get rev_timestamp from rev_id, without loading the rest of the row
1194 * @param Title $title
1197 * @return string|bool False if not found
1199 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1200 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1204 * Get count of revisions per page...not very efficient
1206 * @param IDatabase $db
1207 * @param int $id Page id
1210 static function countByPageId( $db, $id ) {
1211 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1215 * Get count of revisions per page...not very efficient
1217 * @param IDatabase $db
1218 * @param Title $title
1221 static function countByTitle( $db, $title ) {
1222 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1226 * Check if no edits were made by other users since
1227 * the time a user started editing the page. Limit to
1228 * 50 revisions for the sake of performance.
1231 * @deprecated since 1.24
1233 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1234 * Database object or a database identifier usable with wfGetDB.
1235 * @param int $pageId The ID of the page in question
1236 * @param int $userId The ID of the user in question
1237 * @param string $since Look at edits since this time
1239 * @return bool True if the given user was the only one to edit since the given timestamp
1241 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1242 if ( is_int( $db ) ) {
1243 $db = wfGetDB( $db );
1246 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1250 * Load a revision based on a known page ID and current revision ID from the DB
1252 * This method allows for the use of caching, though accessing anything that normally
1253 * requires permission checks (aside from the text) will trigger a small DB lookup.
1254 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1256 * @param IDatabase $db ignored!
1257 * @param int|Title $pageIdOrTitle Page ID or Title object
1258 * @param int $revId Known current revision of this page. Determined automatically if not given.
1259 * @return Revision|bool Returns false if missing
1262 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1263 $title = $pageIdOrTitle instanceof Title
1265 : Title
::newFromID( $pageIdOrTitle );
1271 $record = self
::getRevisionLookup()->getKnownCurrentRevision( $title, $revId );
1272 return $record ?
new Revision( $record ) : false;