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\RevisionRecord
;
26 use MediaWiki\Storage\RevisionStore
;
27 use MediaWiki\Storage\RevisionStoreRecord
;
28 use MediaWiki\Storage\SlotRecord
;
29 use MediaWiki\Storage\SqlBlobStore
;
30 use MediaWiki\User\UserIdentityValue
;
31 use Wikimedia\Rdbms\IDatabase
;
32 use MediaWiki\Linker\LinkTarget
;
33 use MediaWiki\MediaWikiServices
;
34 use Wikimedia\Rdbms\ResultWrapper
;
35 use Wikimedia\Rdbms\FakeResultWrapper
;
38 * @deprecated since 1.31, use RevisionRecord, RevisionStore, and BlobStore instead.
40 class Revision
implements IDBAccessObject
{
42 /** @var RevisionRecord */
45 // Revision deletion constants
46 const DELETED_TEXT
= RevisionRecord
::DELETED_TEXT
;
47 const DELETED_COMMENT
= RevisionRecord
::DELETED_COMMENT
;
48 const DELETED_USER
= RevisionRecord
::DELETED_USER
;
49 const DELETED_RESTRICTED
= RevisionRecord
::DELETED_RESTRICTED
;
50 const SUPPRESSED_USER
= RevisionRecord
::SUPPRESSED_USER
;
51 const SUPPRESSED_ALL
= RevisionRecord
::SUPPRESSED_ALL
;
53 // Audience options for accessors
54 const FOR_PUBLIC
= RevisionRecord
::FOR_PUBLIC
;
55 const FOR_THIS_USER
= RevisionRecord
::FOR_THIS_USER
;
56 const RAW
= RevisionRecord
::RAW
;
58 const TEXT_CACHE_GROUP
= SqlBlobStore
::TEXT_CACHE_GROUP
;
61 * @return RevisionStore
63 protected static function getRevisionStore() {
64 return MediaWikiServices
::getInstance()->getRevisionStore();
68 * @return SqlBlobStore
70 protected static function getBlobStore() {
71 $store = MediaWikiServices
::getInstance()->getBlobStore();
73 if ( !$store instanceof SqlBlobStore
) {
74 throw new RuntimeException(
75 'The backwards compatibility code in Revision currently requires the BlobStore '
76 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
84 * Load a page revision from a given revision ID number.
85 * Returns null if no such revision can be found.
88 * Revision::READ_LATEST : Select the data from the master
89 * Revision::READ_LOCKING : Select & lock the data from the master
92 * @param int $flags (optional)
93 * @param Title $title (optional)
94 * @return Revision|null
96 public static function newFromId( $id, $flags = 0, Title
$title = null ) {
97 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags, $title );
98 return $rec === null ?
null : new Revision( $rec, $flags, $title );
102 * Load either the current, or a specified, revision
103 * that's attached to a given link target. If not attached
104 * to that link target, will return null.
107 * Revision::READ_LATEST : Select the data from the master
108 * Revision::READ_LOCKING : Select & lock the data from the master
110 * @param LinkTarget $linkTarget
111 * @param int $id (optional)
112 * @param int $flags Bitfield (optional)
113 * @return Revision|null
115 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
116 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $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 page ID.
123 * Returns null if no such revision can be found.
126 * Revision::READ_LATEST : Select the data from the master (since 1.20)
127 * Revision::READ_LOCKING : Select & lock the data from the master
130 * @param int $revId (optional)
131 * @param int $flags Bitfield (optional)
132 * @return Revision|null
134 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
135 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
136 return $rec === null ?
null : new Revision( $rec, $flags );
140 * Make a fake revision object from an archive table row. This is queried
141 * for permissions or even inserted (as in Special:Undelete)
144 * @param array $overrides
146 * @throws MWException
149 public static function newFromArchiveRow( $row, $overrides = [] ) {
150 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, null, $overrides );
151 return new Revision( $rec );
157 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
158 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
159 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
160 * can be used as a temporary replacement, but should be avoided.
162 * @param object|array $row
165 public static function newFromRow( $row ) {
166 if ( is_array( $row ) ) {
167 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
169 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
172 return new Revision( $rec );
176 * Load a page revision from a given revision ID number.
177 * Returns null if no such revision can be found.
179 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
181 * @param IDatabase $db
183 * @return Revision|null
185 public static function loadFromId( $db, $id ) {
186 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
187 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
188 return $rec === null ?
null : new Revision( $rec );
192 * Load either the current, or a specified, revision
193 * that's attached to a given page. If not attached
194 * to that page, will return null.
196 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
198 * @param IDatabase $db
201 * @return Revision|null
203 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
204 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
205 return $rec === null ?
null : new Revision( $rec );
209 * Load either the current, or a specified, revision
210 * that's attached to a given page. If not attached
211 * to that page, will return null.
213 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
215 * @param IDatabase $db
216 * @param Title $title
218 * @return Revision|null
220 public static function loadFromTitle( $db, $title, $id = 0 ) {
221 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
222 return $rec === null ?
null : new Revision( $rec );
226 * Load the revision for the given title with the given timestamp.
227 * WARNING: Timestamps may in some circumstances not be unique,
228 * so this isn't the best key to use.
230 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
232 * @param IDatabase $db
233 * @param Title $title
234 * @param string $timestamp
235 * @return Revision|null
237 public static function loadFromTimestamp( $db, $title, $timestamp ) {
238 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
239 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
240 return $rec === null ?
null : new Revision( $rec );
244 * Return a wrapper for a series of database rows to
245 * fetch all of a given page's revisions in turn.
246 * Each row can be fed to the constructor to get objects.
248 * @param LinkTarget $title
249 * @return ResultWrapper
250 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
252 public static function fetchRevision( LinkTarget
$title ) {
253 wfDeprecated( __METHOD__
, '1.31' );
254 return new FakeResultWrapper( [] );
258 * Return the value of a select() JOIN conds array for the user table.
259 * This will get user table rows for logged-in users.
261 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
264 public static function userJoinCond() {
265 wfDeprecated( __METHOD__
, '1.31' );
266 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
270 * Return the value of a select() page conds array for the page table.
271 * This will assure that the revision(s) are not orphaned from live pages.
273 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
276 public static function pageJoinCond() {
277 wfDeprecated( __METHOD__
, '1.31' );
278 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
282 * Return the list of revision fields that should be selected to create
284 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
287 public static function selectFields() {
288 global $wgContentHandlerUseDB;
290 wfDeprecated( __METHOD__
, '1.31' );
306 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
308 if ( $wgContentHandlerUseDB ) {
309 $fields[] = 'rev_content_format';
310 $fields[] = 'rev_content_model';
317 * Return the list of revision fields that should be selected to create
318 * a new revision from an archive row.
319 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
322 public static function selectArchiveFields() {
323 global $wgContentHandlerUseDB;
325 wfDeprecated( __METHOD__
, '1.31' );
343 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
345 if ( $wgContentHandlerUseDB ) {
346 $fields[] = 'ar_content_format';
347 $fields[] = 'ar_content_model';
353 * Return the list of text fields that should be selected to read the
355 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
358 public static function selectTextFields() {
359 wfDeprecated( __METHOD__
, '1.31' );
367 * Return the list of page fields that should be selected from page table
368 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
371 public static function selectPageFields() {
372 wfDeprecated( __METHOD__
, '1.31' );
384 * Return the list of user fields that should be selected from user table
385 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
388 public static function selectUserFields() {
389 wfDeprecated( __METHOD__
, '1.31' );
390 return [ 'user_name' ];
394 * Return the tables, fields, and join conditions to be selected to create
395 * a new revision object.
397 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
398 * @param array $options Any combination of the following strings
399 * - 'page': Join with the page table, and select fields to identify the page
400 * - 'user': Join with the user table, and select the user name
401 * - 'text': Join with the text table, and select fields to load page text
402 * @return array With three keys:
403 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
404 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
405 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
407 public static function getQueryInfo( $options = [] ) {
408 return self
::getRevisionStore()->getQueryInfo( $options );
412 * Return the tables, fields, and join conditions to be selected to create
413 * a new archived revision object.
415 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
416 * @return array With three keys:
417 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
418 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
419 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
421 public static function getArchiveQueryInfo() {
422 return self
::getRevisionStore()->getArchiveQueryInfo();
426 * Do a batched query to get the parent revision lengths
427 * @param IDatabase $db
428 * @param array $revIds
431 public static function getParentLengths( $db, array $revIds ) {
432 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
436 * @param object|array|RevisionRecord $row Either a database row or an array
437 * @param int $queryFlags
438 * @param Title|null $title
442 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
445 if ( $row instanceof RevisionRecord
) {
446 $this->mRecord
= $row;
447 } elseif ( is_array( $row ) ) {
448 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
449 $row['user'] = $wgUser;
452 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
457 } elseif ( is_object( $row ) ) {
458 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
464 throw new InvalidArgumentException(
465 '$row must be a row object, an associative array, or a RevisionRecord'
471 * @return RevisionRecord
473 public function getRevisionRecord() {
474 return $this->mRecord
;
482 public function getId() {
483 return $this->mRecord
->getId();
487 * Set the revision ID
489 * This should only be used for proposed revisions that turn out to be null edits.
491 * @note Only supported on Revisions that were constructed based on associative arrays,
492 * since they are mutable.
495 * @param int|string $id
496 * @throws MWException
498 public function setId( $id ) {
499 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
500 $this->mRecord
->setId( intval( $id ) );
502 throw new MWException( __METHOD__
. ' is not supported on this instance' );
507 * Set the user ID/name
509 * This should only be used for proposed revisions that turn out to be null edits
511 * @note Only supported on Revisions that were constructed based on associative arrays,
512 * since they are mutable.
515 * @deprecated since 1.31, please reuse old Revision object
516 * @param int $id User ID
517 * @param string $name User name
518 * @throws MWException
520 public function setUserIdAndName( $id, $name ) {
521 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
522 $user = new UserIdentityValue( intval( $id ), $name );
523 $this->mRecord
->setUser( $user );
525 throw new MWException( __METHOD__
. ' is not supported on this instance' );
532 private function getMainSlotRaw() {
533 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
537 * Get the ID of the row of the text table that contains the content of the
538 * revision's main slot, if that content is stored in the text table.
540 * If the content is stored elsewhere, this returns null.
542 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
543 * get that actual address that can be used with BlobStore::getBlob(); or use
544 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
548 public function getTextId() {
549 $slot = $this->getMainSlotRaw();
550 return $slot->hasAddress()
551 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
556 * Get parent revision ID (the original previous page revision)
558 * @return int|null The ID of the parent revision. 0 indicates that there is no
559 * parent revision. Null indicates that the parent revision is not known.
561 public function getParentId() {
562 return $this->mRecord
->getParentId();
566 * Returns the length of the text in this revision, or null if unknown.
570 public function getSize() {
571 return $this->mRecord
->getSize();
575 * Returns the base36 sha1 of the content in this revision, or null if unknown.
579 public function getSha1() {
580 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
581 return $this->mRecord
->getSha1();
585 * Returns the title of the page associated with this entry.
586 * Since 1.31, this will never return null.
588 * Will do a query, when title is not set and id is given.
592 public function getTitle() {
593 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
594 return Title
::newFromLinkTarget( $linkTarget );
598 * Set the title of the revision
600 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
602 * @param Title $title
604 public function setTitle( $title ) {
605 if ( !$title->equals( $this->getTitle() ) ) {
606 throw new InvalidArgumentException(
607 $title->getPrefixedText()
608 . ' is not the same as '
609 . $this->mRecord
->getPageAsLinkTarget()->__toString()
619 public function getPage() {
620 return $this->mRecord
->getPageId();
624 * Fetch revision's user id if it's available to the specified audience.
625 * If the specified audience does not have access to it, zero will be
628 * @param int $audience One of:
629 * Revision::FOR_PUBLIC to be displayed to all users
630 * Revision::FOR_THIS_USER to be displayed to the given user
631 * Revision::RAW get the ID regardless of permissions
632 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
633 * to the $audience parameter
636 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
639 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
643 $user = $this->mRecord
->getUser( $audience, $user );
644 return $user ?
$user->getId() : 0;
648 * Fetch revision's user id without regard for the current user's permissions
651 * @deprecated since 1.25, use getUser( Revision::RAW )
653 public function getRawUser() {
654 wfDeprecated( __METHOD__
, '1.25' );
655 return $this->getUser( self
::RAW
);
659 * Fetch revision's username if it's available to the specified audience.
660 * If the specified audience does not have access to the username, an
661 * empty string will be returned.
663 * @param int $audience One of:
664 * Revision::FOR_PUBLIC to be displayed to all users
665 * Revision::FOR_THIS_USER to be displayed to the given user
666 * Revision::RAW get the text regardless of permissions
667 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
668 * to the $audience parameter
671 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
674 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
678 $user = $this->mRecord
->getUser( $audience, $user );
679 return $user ?
$user->getName() : '';
683 * Fetch revision's username without regard for view restrictions
686 * @deprecated since 1.25, use getUserText( Revision::RAW )
688 public function getRawUserText() {
689 wfDeprecated( __METHOD__
, '1.25' );
690 return $this->getUserText( self
::RAW
);
694 * Fetch revision comment if it's available to the specified audience.
695 * If the specified audience does not have access to the comment, an
696 * empty string will be returned.
698 * @param int $audience One of:
699 * Revision::FOR_PUBLIC to be displayed to all users
700 * Revision::FOR_THIS_USER to be displayed to the given user
701 * Revision::RAW get the text regardless of permissions
702 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
703 * to the $audience parameter
706 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
709 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
713 $comment = $this->mRecord
->getComment( $audience, $user );
714 return $comment === null ?
null : $comment->text
;
718 * Fetch revision comment without regard for the current user's permissions
721 * @deprecated since 1.25, use getComment( Revision::RAW )
723 public function getRawComment() {
724 wfDeprecated( __METHOD__
, '1.25' );
725 return $this->getComment( self
::RAW
);
731 public function isMinor() {
732 return $this->mRecord
->isMinor();
736 * @return int Rcid of the unpatrolled row, zero if there isn't one
738 public function isUnpatrolled() {
739 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
743 * Get the RC object belonging to the current revision, if there's one
745 * @param int $flags (optional) $flags include:
746 * Revision::READ_LATEST : Select the data from the master
749 * @return RecentChange|null
751 public function getRecentChange( $flags = 0 ) {
752 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
756 * @param int $field One of DELETED_* bitfield constants
760 public function isDeleted( $field ) {
761 return $this->mRecord
->isDeleted( $field );
765 * Get the deletion bitfield of the revision
769 public function getVisibility() {
770 return $this->mRecord
->getVisibility();
774 * Fetch revision content if it's available to the specified audience.
775 * If the specified audience does not have the ability to view this
776 * revision, or the content could not be loaded, null will be returned.
778 * @param int $audience One of:
779 * Revision::FOR_PUBLIC to be displayed to all users
780 * Revision::FOR_THIS_USER to be displayed to $user
781 * Revision::RAW get the text regardless of permissions
782 * @param User $user User object to check for, only if FOR_THIS_USER is passed
783 * to the $audience parameter
785 * @return Content|null
787 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
790 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
795 return $this->mRecord
->getContent( 'main', $audience, $user );
797 catch ( RevisionAccessException
$e ) {
803 * Get original serialized data (without checking view restrictions)
806 * @deprecated since 1.31, use BlobStore::getBlob instead.
810 public function getSerializedData() {
811 $slot = $this->getMainSlotRaw();
812 return $slot->getContent()->serialize();
816 * Returns the content model for the main slot of this revision.
818 * If no content model was stored in the database, the default content model for the title is
819 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
820 * is used as a last resort.
822 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
824 * @return string The content model id associated with this revision,
825 * see the CONTENT_MODEL_XXX constants.
827 public function getContentModel() {
828 return $this->getMainSlotRaw()->getModel();
832 * Returns the content format for the main slot of this revision.
834 * If no content format was stored in the database, the default format for this
835 * revision's content model is returned.
837 * @todo: drop this, the format is irrelevant to the revision!
839 * @return string The content format id associated with this revision,
840 * see the CONTENT_FORMAT_XXX constants.
842 public function getContentFormat() {
843 $format = $this->getMainSlotRaw()->getFormat();
845 if ( $format === null ) {
846 // if no format was stored along with the blob, fall back to default format
847 $format = $this->getContentHandler()->getDefaultFormat();
854 * Returns the content handler appropriate for this revision's content model.
856 * @throws MWException
857 * @return ContentHandler
859 public function getContentHandler() {
860 return ContentHandler
::getForModelID( $this->getContentModel() );
866 public function getTimestamp() {
867 return $this->mRecord
->getTimestamp();
873 public function isCurrent() {
874 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
878 * Get previous revision for this title
880 * @return Revision|null
882 public function getPrevious() {
883 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
);
884 return $rec === null ?
null : new Revision( $rec );
888 * Get next revision for this title
890 * @return Revision|null
892 public function getNext() {
893 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
);
894 return $rec === null ?
null : new Revision( $rec );
898 * Get revision text associated with an old or archive row
900 * Both the flags and the text field must be included. Including the old_id
901 * field will activate cache usage as long as the $wiki parameter is not set.
903 * @param stdClass $row The text data
904 * @param string $prefix Table prefix (default 'old_')
905 * @param string|bool $wiki The name of the wiki to load the revision text from
906 * (same as the the wiki $row was loaded from) or false to indicate the local
907 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
908 * identifier as understood by the LoadBalancer class.
909 * @return string|false Text the text requested or false on failure
911 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
912 $textField = $prefix . 'text';
913 $flagsField = $prefix . 'flags';
915 if ( isset( $row->$flagsField ) ) {
916 $flags = explode( ',', $row->$flagsField );
921 if ( isset( $row->$textField ) ) {
922 $text = $row->$textField;
927 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
929 return self
::getBlobStore()->expandBlob( $text, $flags, $cacheKey );
933 * If $wgCompressRevisions is enabled, we will compress data.
934 * The input string is modified in place.
935 * Return value is the flags field: contains 'gzip' if the
936 * data is compressed, and 'utf-8' if we're saving in UTF-8
939 * @param mixed &$text Reference to a text
942 public static function compressRevisionText( &$text ) {
943 return self
::getBlobStore()->compressData( $text );
947 * Re-converts revision text according to it's flags.
949 * @param mixed $text Reference to a text
950 * @param array $flags Compression flags
951 * @return string|bool Decompressed text, or false on failure
953 public static function decompressRevisionText( $text, $flags ) {
954 return self
::getBlobStore()->decompressData( $text, $flags );
958 * Insert a new revision into the database, returning the new revision ID
959 * number on success and dies horribly on failure.
961 * @param IDatabase $dbw (master connection)
962 * @throws MWException
963 * @return int The revision ID
965 public function insertOn( $dbw ) {
968 // Note that $this->mRecord->getId() will typically return null here, but not always,
969 // e.g. not when restoring a revision.
971 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
972 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
973 $this->mRecord
->setUser( $wgUser );
975 throw new MWException( 'Cannot insert revision with no associated user.' );
979 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
981 $this->mRecord
= $rec;
983 // Avoid PHP 7.1 warning of passing $this by reference
985 // TODO: hard-deprecate in 1.32 (or even 1.31?)
986 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
988 return $rec->getId();
992 * Get the base 36 SHA-1 value for a string of text
993 * @param string $text
996 public static function base36Sha1( $text ) {
997 return SlotRecord
::base36Sha1( $text );
1001 * Create a new null-revision for insertion into a page's
1002 * history. This will not re-save the text, but simply refer
1003 * to the text from the previous version.
1005 * Such revisions can for instance identify page rename
1006 * operations and other such meta-modifications.
1008 * @param IDatabase $dbw
1009 * @param int $pageId ID number of the page to read from
1010 * @param string $summary Revision's summary
1011 * @param bool $minor Whether the revision should be considered as minor
1012 * @param User|null $user User object to use or null for $wgUser
1013 * @return Revision|null Revision or null on error
1015 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1021 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1023 $title = Title
::newFromID( $pageId );
1024 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1026 return new Revision( $rec );
1030 * Determine if the current user is allowed to view a particular
1031 * field of this revision, if it's marked as deleted.
1033 * @param int $field One of self::DELETED_TEXT,
1034 * self::DELETED_COMMENT,
1035 * self::DELETED_USER
1036 * @param User|null $user User object to check, or null to use $wgUser
1039 public function userCan( $field, User
$user = null ) {
1040 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1044 * Determine if the current user is allowed to view a particular
1045 * field of this revision, if it's marked as deleted. This is used
1046 * by various classes to avoid duplication.
1048 * @param int $bitfield Current field
1049 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1050 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1051 * self::DELETED_USER = File::DELETED_USER
1052 * @param User|null $user User object to check, or null to use $wgUser
1053 * @param Title|null $title A Title object to check for per-page restrictions on,
1054 * instead of just plain userrights
1057 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1066 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1070 * Get rev_timestamp from rev_id, without loading the rest of the row
1072 * @param Title $title
1075 * @return string|bool False if not found
1077 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1078 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1082 * Get count of revisions per page...not very efficient
1084 * @param IDatabase $db
1085 * @param int $id Page id
1088 static function countByPageId( $db, $id ) {
1089 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1093 * Get count of revisions per page...not very efficient
1095 * @param IDatabase $db
1096 * @param Title $title
1099 static function countByTitle( $db, $title ) {
1100 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1104 * Check if no edits were made by other users since
1105 * the time a user started editing the page. Limit to
1106 * 50 revisions for the sake of performance.
1109 * @deprecated since 1.24
1111 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1112 * Database object or a database identifier usable with wfGetDB.
1113 * @param int $pageId The ID of the page in question
1114 * @param int $userId The ID of the user in question
1115 * @param string $since Look at edits since this time
1117 * @return bool True if the given user was the only one to edit since the given timestamp
1119 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1120 if ( is_int( $db ) ) {
1121 $db = wfGetDB( $db );
1124 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1128 * Load a revision based on a known page ID and current revision ID from the DB
1130 * This method allows for the use of caching, though accessing anything that normally
1131 * requires permission checks (aside from the text) will trigger a small DB lookup.
1132 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1134 * @param IDatabase $db ignored!
1135 * @param int|Title $pageIdOrTitle Page ID or Title object
1136 * @param int $revId Known current revision of this page. Determined automatically if not given.
1137 * @return Revision|bool Returns false if missing
1140 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1141 $title = $pageIdOrTitle instanceof Title
1143 : Title
::newFromID( $pageIdOrTitle );
1145 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1146 return $record ?
new Revision( $record ) : false;