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
145 * @param Title $title (optional)
147 * @throws MWException
150 public static function newFromArchiveRow( $row, $overrides = [], Title
$title = null ) {
151 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
152 return new Revision( $rec, self
::READ_NORMAL
, $title );
158 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
159 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
160 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
161 * can be used as a temporary replacement, but should be avoided.
163 * @param object|array $row
166 public static function newFromRow( $row ) {
167 if ( is_array( $row ) ) {
168 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
170 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
173 return new Revision( $rec );
177 * Load a page revision from a given revision ID number.
178 * Returns null if no such revision can be found.
180 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
182 * @param IDatabase $db
184 * @return Revision|null
186 public static function loadFromId( $db, $id ) {
187 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
188 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
189 return $rec === null ?
null : new Revision( $rec );
193 * Load either the current, or a specified, revision
194 * that's attached to a given page. If not attached
195 * to that page, will return null.
197 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
199 * @param IDatabase $db
202 * @return Revision|null
204 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
205 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
206 return $rec === null ?
null : new Revision( $rec );
210 * Load either the current, or a specified, revision
211 * that's attached to a given page. If not attached
212 * to that page, will return null.
214 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
216 * @param IDatabase $db
217 * @param Title $title
219 * @return Revision|null
221 public static function loadFromTitle( $db, $title, $id = 0 ) {
222 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
223 return $rec === null ?
null : new Revision( $rec );
227 * Load the revision for the given title with the given timestamp.
228 * WARNING: Timestamps may in some circumstances not be unique,
229 * so this isn't the best key to use.
231 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
233 * @param IDatabase $db
234 * @param Title $title
235 * @param string $timestamp
236 * @return Revision|null
238 public static function loadFromTimestamp( $db, $title, $timestamp ) {
239 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
240 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
241 return $rec === null ?
null : new Revision( $rec );
245 * Return a wrapper for a series of database rows to
246 * fetch all of a given page's revisions in turn.
247 * Each row can be fed to the constructor to get objects.
249 * @param LinkTarget $title
250 * @return ResultWrapper
251 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
253 public static function fetchRevision( LinkTarget
$title ) {
254 wfDeprecated( __METHOD__
, '1.31' );
255 return new FakeResultWrapper( [] );
259 * Return the value of a select() JOIN conds array for the user table.
260 * This will get user table rows for logged-in users.
262 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
265 public static function userJoinCond() {
266 wfDeprecated( __METHOD__
, '1.31' );
267 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
271 * Return the value of a select() page conds array for the page table.
272 * This will assure that the revision(s) are not orphaned from live pages.
274 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
277 public static function pageJoinCond() {
278 wfDeprecated( __METHOD__
, '1.31' );
279 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
283 * Return the list of revision fields that should be selected to create
285 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
288 public static function selectFields() {
289 global $wgContentHandlerUseDB;
291 wfDeprecated( __METHOD__
, '1.31' );
307 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
309 if ( $wgContentHandlerUseDB ) {
310 $fields[] = 'rev_content_format';
311 $fields[] = 'rev_content_model';
318 * Return the list of revision fields that should be selected to create
319 * a new revision from an archive row.
320 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
323 public static function selectArchiveFields() {
324 global $wgContentHandlerUseDB;
326 wfDeprecated( __METHOD__
, '1.31' );
344 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
346 if ( $wgContentHandlerUseDB ) {
347 $fields[] = 'ar_content_format';
348 $fields[] = 'ar_content_model';
354 * Return the list of text fields that should be selected to read the
356 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
359 public static function selectTextFields() {
360 wfDeprecated( __METHOD__
, '1.31' );
368 * Return the list of page fields that should be selected from page table
369 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
372 public static function selectPageFields() {
373 wfDeprecated( __METHOD__
, '1.31' );
385 * Return the list of user fields that should be selected from user table
386 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
389 public static function selectUserFields() {
390 wfDeprecated( __METHOD__
, '1.31' );
391 return [ 'user_name' ];
395 * Return the tables, fields, and join conditions to be selected to create
396 * a new revision object.
398 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
399 * @param array $options Any combination of the following strings
400 * - 'page': Join with the page table, and select fields to identify the page
401 * - 'user': Join with the user table, and select the user name
402 * - 'text': Join with the text table, and select fields to load page text
403 * @return array With three keys:
404 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
405 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
406 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
408 public static function getQueryInfo( $options = [] ) {
409 return self
::getRevisionStore()->getQueryInfo( $options );
413 * Return the tables, fields, and join conditions to be selected to create
414 * a new archived revision object.
416 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
417 * @return array With three keys:
418 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
419 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
420 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
422 public static function getArchiveQueryInfo() {
423 return self
::getRevisionStore()->getArchiveQueryInfo();
427 * Do a batched query to get the parent revision lengths
428 * @param IDatabase $db
429 * @param array $revIds
432 public static function getParentLengths( $db, array $revIds ) {
433 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
437 * @param object|array|RevisionRecord $row Either a database row or an array
438 * @param int $queryFlags
439 * @param Title|null $title
443 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
446 if ( $row instanceof RevisionRecord
) {
447 $this->mRecord
= $row;
448 } elseif ( is_array( $row ) ) {
449 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
450 $row['user'] = $wgUser;
453 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
458 } elseif ( is_object( $row ) ) {
459 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
465 throw new InvalidArgumentException(
466 '$row must be a row object, an associative array, or a RevisionRecord'
472 * @return RevisionRecord
474 public function getRevisionRecord() {
475 return $this->mRecord
;
483 public function getId() {
484 return $this->mRecord
->getId();
488 * Set the revision ID
490 * This should only be used for proposed revisions that turn out to be null edits.
492 * @note Only supported on Revisions that were constructed based on associative arrays,
493 * since they are mutable.
496 * @param int|string $id
497 * @throws MWException
499 public function setId( $id ) {
500 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
501 $this->mRecord
->setId( intval( $id ) );
503 throw new MWException( __METHOD__
. ' is not supported on this instance' );
508 * Set the user ID/name
510 * This should only be used for proposed revisions that turn out to be null edits
512 * @note Only supported on Revisions that were constructed based on associative arrays,
513 * since they are mutable.
516 * @deprecated since 1.31, please reuse old Revision object
517 * @param int $id User ID
518 * @param string $name User name
519 * @throws MWException
521 public function setUserIdAndName( $id, $name ) {
522 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
523 $user = new UserIdentityValue( intval( $id ), $name );
524 $this->mRecord
->setUser( $user );
526 throw new MWException( __METHOD__
. ' is not supported on this instance' );
533 private function getMainSlotRaw() {
534 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
538 * Get the ID of the row of the text table that contains the content of the
539 * revision's main slot, if that content is stored in the text table.
541 * If the content is stored elsewhere, this returns null.
543 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
544 * get that actual address that can be used with BlobStore::getBlob(); or use
545 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
549 public function getTextId() {
550 $slot = $this->getMainSlotRaw();
551 return $slot->hasAddress()
552 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
557 * Get parent revision ID (the original previous page revision)
559 * @return int|null The ID of the parent revision. 0 indicates that there is no
560 * parent revision. Null indicates that the parent revision is not known.
562 public function getParentId() {
563 return $this->mRecord
->getParentId();
567 * Returns the length of the text in this revision, or null if unknown.
571 public function getSize() {
572 return $this->mRecord
->getSize();
576 * Returns the base36 sha1 of the content in this revision, or null if unknown.
580 public function getSha1() {
581 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
582 return $this->mRecord
->getSha1();
586 * Returns the title of the page associated with this entry.
587 * Since 1.31, this will never return null.
589 * Will do a query, when title is not set and id is given.
593 public function getTitle() {
594 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
595 return Title
::newFromLinkTarget( $linkTarget );
599 * Set the title of the revision
601 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
603 * @param Title $title
605 public function setTitle( $title ) {
606 if ( !$title->equals( $this->getTitle() ) ) {
607 throw new InvalidArgumentException(
608 $title->getPrefixedText()
609 . ' is not the same as '
610 . $this->mRecord
->getPageAsLinkTarget()->__toString()
620 public function getPage() {
621 return $this->mRecord
->getPageId();
625 * Fetch revision's user id if it's available to the specified audience.
626 * If the specified audience does not have access to it, zero will be
629 * @param int $audience One of:
630 * Revision::FOR_PUBLIC to be displayed to all users
631 * Revision::FOR_THIS_USER to be displayed to the given user
632 * Revision::RAW get the ID regardless of permissions
633 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
634 * to the $audience parameter
637 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
640 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
644 $user = $this->mRecord
->getUser( $audience, $user );
645 return $user ?
$user->getId() : 0;
649 * Fetch revision's user id without regard for the current user's permissions
652 * @deprecated since 1.25, use getUser( Revision::RAW )
654 public function getRawUser() {
655 wfDeprecated( __METHOD__
, '1.25' );
656 return $this->getUser( self
::RAW
);
660 * Fetch revision's username if it's available to the specified audience.
661 * If the specified audience does not have access to the username, an
662 * empty string will be returned.
664 * @param int $audience One of:
665 * Revision::FOR_PUBLIC to be displayed to all users
666 * Revision::FOR_THIS_USER to be displayed to the given user
667 * Revision::RAW get the text regardless of permissions
668 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
669 * to the $audience parameter
672 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
675 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
679 $user = $this->mRecord
->getUser( $audience, $user );
680 return $user ?
$user->getName() : '';
684 * Fetch revision's username without regard for view restrictions
687 * @deprecated since 1.25, use getUserText( Revision::RAW )
689 public function getRawUserText() {
690 wfDeprecated( __METHOD__
, '1.25' );
691 return $this->getUserText( self
::RAW
);
695 * Fetch revision comment if it's available to the specified audience.
696 * If the specified audience does not have access to the comment, an
697 * empty string will be returned.
699 * @param int $audience One of:
700 * Revision::FOR_PUBLIC to be displayed to all users
701 * Revision::FOR_THIS_USER to be displayed to the given user
702 * Revision::RAW get the text regardless of permissions
703 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
704 * to the $audience parameter
707 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
710 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
714 $comment = $this->mRecord
->getComment( $audience, $user );
715 return $comment === null ?
null : $comment->text
;
719 * Fetch revision comment without regard for the current user's permissions
722 * @deprecated since 1.25, use getComment( Revision::RAW )
724 public function getRawComment() {
725 wfDeprecated( __METHOD__
, '1.25' );
726 return $this->getComment( self
::RAW
);
732 public function isMinor() {
733 return $this->mRecord
->isMinor();
737 * @return int Rcid of the unpatrolled row, zero if there isn't one
739 public function isUnpatrolled() {
740 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
744 * Get the RC object belonging to the current revision, if there's one
746 * @param int $flags (optional) $flags include:
747 * Revision::READ_LATEST : Select the data from the master
750 * @return RecentChange|null
752 public function getRecentChange( $flags = 0 ) {
753 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
757 * @param int $field One of DELETED_* bitfield constants
761 public function isDeleted( $field ) {
762 return $this->mRecord
->isDeleted( $field );
766 * Get the deletion bitfield of the revision
770 public function getVisibility() {
771 return $this->mRecord
->getVisibility();
775 * Fetch revision content if it's available to the specified audience.
776 * If the specified audience does not have the ability to view this
777 * revision, or the content could not be loaded, null will be returned.
779 * @param int $audience One of:
780 * Revision::FOR_PUBLIC to be displayed to all users
781 * Revision::FOR_THIS_USER to be displayed to $user
782 * Revision::RAW get the text regardless of permissions
783 * @param User $user User object to check for, only if FOR_THIS_USER is passed
784 * to the $audience parameter
786 * @return Content|null
788 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
791 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
796 return $this->mRecord
->getContent( 'main', $audience, $user );
798 catch ( RevisionAccessException
$e ) {
804 * Get original serialized data (without checking view restrictions)
807 * @deprecated since 1.31, use BlobStore::getBlob instead.
811 public function getSerializedData() {
812 $slot = $this->getMainSlotRaw();
813 return $slot->getContent()->serialize();
817 * Returns the content model for the main slot of this revision.
819 * If no content model was stored in the database, the default content model for the title is
820 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
821 * is used as a last resort.
823 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
825 * @return string The content model id associated with this revision,
826 * see the CONTENT_MODEL_XXX constants.
828 public function getContentModel() {
829 return $this->getMainSlotRaw()->getModel();
833 * Returns the content format for the main slot of this revision.
835 * If no content format was stored in the database, the default format for this
836 * revision's content model is returned.
838 * @todo: drop this, the format is irrelevant to the revision!
840 * @return string The content format id associated with this revision,
841 * see the CONTENT_FORMAT_XXX constants.
843 public function getContentFormat() {
844 $format = $this->getMainSlotRaw()->getFormat();
846 if ( $format === null ) {
847 // if no format was stored along with the blob, fall back to default format
848 $format = $this->getContentHandler()->getDefaultFormat();
855 * Returns the content handler appropriate for this revision's content model.
857 * @throws MWException
858 * @return ContentHandler
860 public function getContentHandler() {
861 return ContentHandler
::getForModelID( $this->getContentModel() );
867 public function getTimestamp() {
868 return $this->mRecord
->getTimestamp();
874 public function isCurrent() {
875 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
879 * Get previous revision for this title
881 * @return Revision|null
883 public function getPrevious() {
884 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $this->getTitle() );
887 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
891 * Get next revision for this title
893 * @return Revision|null
895 public function getNext() {
896 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $this->getTitle() );
899 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
903 * Get revision text associated with an old or archive row
905 * Both the flags and the text field must be included. Including the old_id
906 * field will activate cache usage as long as the $wiki parameter is not set.
908 * @param stdClass $row The text data
909 * @param string $prefix Table prefix (default 'old_')
910 * @param string|bool $wiki The name of the wiki to load the revision text from
911 * (same as the the wiki $row was loaded from) or false to indicate the local
912 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
913 * identifier as understood by the LoadBalancer class.
914 * @return string|false Text the text requested or false on failure
916 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
917 $textField = $prefix . 'text';
918 $flagsField = $prefix . 'flags';
920 if ( isset( $row->$flagsField ) ) {
921 $flags = explode( ',', $row->$flagsField );
926 if ( isset( $row->$textField ) ) {
927 $text = $row->$textField;
932 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
934 return self
::getBlobStore()->expandBlob( $text, $flags, $cacheKey );
938 * If $wgCompressRevisions is enabled, we will compress data.
939 * The input string is modified in place.
940 * Return value is the flags field: contains 'gzip' if the
941 * data is compressed, and 'utf-8' if we're saving in UTF-8
944 * @param mixed &$text Reference to a text
947 public static function compressRevisionText( &$text ) {
948 return self
::getBlobStore()->compressData( $text );
952 * Re-converts revision text according to it's flags.
954 * @param mixed $text Reference to a text
955 * @param array $flags Compression flags
956 * @return string|bool Decompressed text, or false on failure
958 public static function decompressRevisionText( $text, $flags ) {
959 return self
::getBlobStore()->decompressData( $text, $flags );
963 * Insert a new revision into the database, returning the new revision ID
964 * number on success and dies horribly on failure.
966 * @param IDatabase $dbw (master connection)
967 * @throws MWException
968 * @return int The revision ID
970 public function insertOn( $dbw ) {
973 // Note that $this->mRecord->getId() will typically return null here, but not always,
974 // e.g. not when restoring a revision.
976 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
977 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
978 $this->mRecord
->setUser( $wgUser );
980 throw new MWException( 'Cannot insert revision with no associated user.' );
984 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
986 $this->mRecord
= $rec;
988 // Avoid PHP 7.1 warning of passing $this by reference
990 // TODO: hard-deprecate in 1.32 (or even 1.31?)
991 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
993 return $rec->getId();
997 * Get the base 36 SHA-1 value for a string of text
998 * @param string $text
1001 public static function base36Sha1( $text ) {
1002 return SlotRecord
::base36Sha1( $text );
1006 * Create a new null-revision for insertion into a page's
1007 * history. This will not re-save the text, but simply refer
1008 * to the text from the previous version.
1010 * Such revisions can for instance identify page rename
1011 * operations and other such meta-modifications.
1013 * @param IDatabase $dbw
1014 * @param int $pageId ID number of the page to read from
1015 * @param string $summary Revision's summary
1016 * @param bool $minor Whether the revision should be considered as minor
1017 * @param User|null $user User object to use or null for $wgUser
1018 * @return Revision|null Revision or null on error
1020 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1026 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1028 $title = Title
::newFromID( $pageId );
1029 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1031 return new Revision( $rec );
1035 * Determine if the current user is allowed to view a particular
1036 * field of this revision, if it's marked as deleted.
1038 * @param int $field One of self::DELETED_TEXT,
1039 * self::DELETED_COMMENT,
1040 * self::DELETED_USER
1041 * @param User|null $user User object to check, or null to use $wgUser
1044 public function userCan( $field, User
$user = null ) {
1045 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1049 * Determine if the current user is allowed to view a particular
1050 * field of this revision, if it's marked as deleted. This is used
1051 * by various classes to avoid duplication.
1053 * @param int $bitfield Current field
1054 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1055 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1056 * self::DELETED_USER = File::DELETED_USER
1057 * @param User|null $user User object to check, or null to use $wgUser
1058 * @param Title|null $title A Title object to check for per-page restrictions on,
1059 * instead of just plain userrights
1062 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1071 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1075 * Get rev_timestamp from rev_id, without loading the rest of the row
1077 * @param Title $title
1080 * @return string|bool False if not found
1082 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1083 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1087 * Get count of revisions per page...not very efficient
1089 * @param IDatabase $db
1090 * @param int $id Page id
1093 static function countByPageId( $db, $id ) {
1094 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1098 * Get count of revisions per page...not very efficient
1100 * @param IDatabase $db
1101 * @param Title $title
1104 static function countByTitle( $db, $title ) {
1105 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1109 * Check if no edits were made by other users since
1110 * the time a user started editing the page. Limit to
1111 * 50 revisions for the sake of performance.
1114 * @deprecated since 1.24
1116 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1117 * Database object or a database identifier usable with wfGetDB.
1118 * @param int $pageId The ID of the page in question
1119 * @param int $userId The ID of the user in question
1120 * @param string $since Look at edits since this time
1122 * @return bool True if the given user was the only one to edit since the given timestamp
1124 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1125 if ( is_int( $db ) ) {
1126 $db = wfGetDB( $db );
1129 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1133 * Load a revision based on a known page ID and current revision ID from the DB
1135 * This method allows for the use of caching, though accessing anything that normally
1136 * requires permission checks (aside from the text) will trigger a small DB lookup.
1137 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1139 * @param IDatabase $db ignored!
1140 * @param int|Title $pageIdOrTitle Page ID or Title object
1141 * @param int $revId Known current revision of this page. Determined automatically if not given.
1142 * @return Revision|bool Returns false if missing
1145 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1146 $title = $pageIdOrTitle instanceof Title
1148 : Title
::newFromID( $pageIdOrTitle );
1150 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1151 return $record ?
new Revision( $record ) : false;