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 * @param bool|string $wikiId The ID of the target wiki database. Use false for the local wiki.
70 * @return SqlBlobStore
72 protected static function getBlobStore( $wiki = false ) {
73 $store = MediaWikiServices
::getInstance()
74 ->getBlobStoreFactory()
75 ->newSqlBlobStore( $wiki );
77 if ( !$store instanceof SqlBlobStore
) {
78 throw new RuntimeException(
79 'The backwards compatibility code in Revision currently requires the BlobStore '
80 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
88 * Load a page revision from a given revision ID number.
89 * Returns null if no such revision can be found.
92 * Revision::READ_LATEST : Select the data from the master
93 * Revision::READ_LOCKING : Select & lock the data from the master
96 * @param int $flags (optional)
97 * @return Revision|null
99 public static function newFromId( $id, $flags = 0 ) {
100 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags );
101 return $rec === null ?
null : new Revision( $rec, $flags );
105 * Load either the current, or a specified, revision
106 * that's attached to a given link target. If not attached
107 * to that link target, will return null.
110 * Revision::READ_LATEST : Select the data from the master
111 * Revision::READ_LOCKING : Select & lock the data from the master
113 * @param LinkTarget $linkTarget
114 * @param int $id (optional)
115 * @param int $flags Bitfield (optional)
116 * @return Revision|null
118 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
119 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $id, $flags );
120 return $rec === null ?
null : new Revision( $rec, $flags );
124 * Load either the current, or a specified, revision
125 * that's attached to a given page ID.
126 * Returns null if no such revision can be found.
129 * Revision::READ_LATEST : Select the data from the master (since 1.20)
130 * Revision::READ_LOCKING : Select & lock the data from the master
133 * @param int $revId (optional)
134 * @param int $flags Bitfield (optional)
135 * @return Revision|null
137 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
138 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
139 return $rec === null ?
null : new Revision( $rec, $flags );
143 * Make a fake revision object from an archive table row. This is queried
144 * for permissions or even inserted (as in Special:Undelete)
147 * @param array $overrides
149 * @throws MWException
152 public static function newFromArchiveRow( $row, $overrides = [], Title
$title = null ) {
154 * MCR Migration: https://phabricator.wikimedia.org/T183564
155 * This method used to overwrite attributes, then passed to Revision::__construct
156 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
157 * So do a conversion here.
159 if ( array_key_exists( 'page', $overrides ) ) {
160 $overrides['page_id'] = $overrides['page'];
161 unset( $overrides['page'] );
164 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
165 return new Revision( $rec, self
::READ_NORMAL
, $title );
171 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
172 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
173 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
174 * can be used as a temporary replacement, but should be avoided.
176 * @param object|array $row
179 public static function newFromRow( $row ) {
180 if ( is_array( $row ) ) {
181 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
183 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
186 return new Revision( $rec );
190 * Load a page revision from a given revision ID number.
191 * Returns null if no such revision can be found.
193 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
195 * @param IDatabase $db
197 * @return Revision|null
199 public static function loadFromId( $db, $id ) {
200 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
201 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
202 return $rec === null ?
null : new Revision( $rec );
206 * Load either the current, or a specified, revision
207 * that's attached to a given page. If not attached
208 * to that page, will return null.
210 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
212 * @param IDatabase $db
215 * @return Revision|null
217 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
218 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
219 return $rec === null ?
null : new Revision( $rec );
223 * Load either the current, or a specified, revision
224 * that's attached to a given page. If not attached
225 * to that page, will return null.
227 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
229 * @param IDatabase $db
230 * @param Title $title
232 * @return Revision|null
234 public static function loadFromTitle( $db, $title, $id = 0 ) {
235 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
236 return $rec === null ?
null : new Revision( $rec );
240 * Load the revision for the given title with the given timestamp.
241 * WARNING: Timestamps may in some circumstances not be unique,
242 * so this isn't the best key to use.
244 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
246 * @param IDatabase $db
247 * @param Title $title
248 * @param string $timestamp
249 * @return Revision|null
251 public static function loadFromTimestamp( $db, $title, $timestamp ) {
252 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
253 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
254 return $rec === null ?
null : new Revision( $rec );
258 * Return a wrapper for a series of database rows to
259 * fetch all of a given page's revisions in turn.
260 * Each row can be fed to the constructor to get objects.
262 * @param LinkTarget $title
263 * @return ResultWrapper
264 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
266 public static function fetchRevision( LinkTarget
$title ) {
267 wfDeprecated( __METHOD__
, '1.31' );
268 return new FakeResultWrapper( [] );
272 * Return the value of a select() JOIN conds array for the user table.
273 * This will get user table rows for logged-in users.
275 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
278 public static function userJoinCond() {
279 wfDeprecated( __METHOD__
, '1.31' );
280 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
284 * Return the value of a select() page conds array for the page table.
285 * This will assure that the revision(s) are not orphaned from live pages.
287 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
290 public static function pageJoinCond() {
291 wfDeprecated( __METHOD__
, '1.31' );
292 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
296 * Return the list of revision fields that should be selected to create
298 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
301 public static function selectFields() {
302 global $wgContentHandlerUseDB;
304 wfDeprecated( __METHOD__
, '1.31' );
320 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
322 if ( $wgContentHandlerUseDB ) {
323 $fields[] = 'rev_content_format';
324 $fields[] = 'rev_content_model';
331 * Return the list of revision fields that should be selected to create
332 * a new revision from an archive row.
333 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
336 public static function selectArchiveFields() {
337 global $wgContentHandlerUseDB;
339 wfDeprecated( __METHOD__
, '1.31' );
357 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
359 if ( $wgContentHandlerUseDB ) {
360 $fields[] = 'ar_content_format';
361 $fields[] = 'ar_content_model';
367 * Return the list of text fields that should be selected to read the
369 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
372 public static function selectTextFields() {
373 wfDeprecated( __METHOD__
, '1.31' );
381 * Return the list of page fields that should be selected from page table
382 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
385 public static function selectPageFields() {
386 wfDeprecated( __METHOD__
, '1.31' );
398 * Return the list of user fields that should be selected from user table
399 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
402 public static function selectUserFields() {
403 wfDeprecated( __METHOD__
, '1.31' );
404 return [ 'user_name' ];
408 * Return the tables, fields, and join conditions to be selected to create
409 * a new revision object.
411 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
412 * @param array $options Any combination of the following strings
413 * - 'page': Join with the page table, and select fields to identify the page
414 * - 'user': Join with the user table, and select the user name
415 * - 'text': Join with the text table, and select fields to load page text
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 getQueryInfo( $options = [] ) {
422 return self
::getRevisionStore()->getQueryInfo( $options );
426 * Return the tables, fields, and join conditions to be selected to create
427 * a new archived revision object.
429 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
430 * @return array With three keys:
431 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
432 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
433 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
435 public static function getArchiveQueryInfo() {
436 return self
::getRevisionStore()->getArchiveQueryInfo();
440 * Do a batched query to get the parent revision lengths
441 * @param IDatabase $db
442 * @param array $revIds
445 public static function getParentLengths( $db, array $revIds ) {
446 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
450 * @param object|array|RevisionRecord $row Either a database row or an array
451 * @param int $queryFlags
452 * @param Title|null $title
456 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
459 if ( $row instanceof RevisionRecord
) {
460 $this->mRecord
= $row;
461 } elseif ( is_array( $row ) ) {
462 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
463 $row['user'] = $wgUser;
466 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
471 } elseif ( is_object( $row ) ) {
472 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
478 throw new InvalidArgumentException(
479 '$row must be a row object, an associative array, or a RevisionRecord'
485 * @return RevisionRecord
487 public function getRevisionRecord() {
488 return $this->mRecord
;
496 public function getId() {
497 return $this->mRecord
->getId();
501 * Set the revision ID
503 * This should only be used for proposed revisions that turn out to be null edits.
505 * @note Only supported on Revisions that were constructed based on associative arrays,
506 * since they are mutable.
509 * @param int|string $id
510 * @throws MWException
512 public function setId( $id ) {
513 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
514 $this->mRecord
->setId( intval( $id ) );
516 throw new MWException( __METHOD__
. ' is not supported on this instance' );
521 * Set the user ID/name
523 * This should only be used for proposed revisions that turn out to be null edits
525 * @note Only supported on Revisions that were constructed based on associative arrays,
526 * since they are mutable.
529 * @deprecated since 1.31, please reuse old Revision object
530 * @param int $id User ID
531 * @param string $name User name
532 * @throws MWException
534 public function setUserIdAndName( $id, $name ) {
535 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
536 $user = new UserIdentityValue( intval( $id ), $name );
537 $this->mRecord
->setUser( $user );
539 throw new MWException( __METHOD__
. ' is not supported on this instance' );
546 private function getMainSlotRaw() {
547 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
551 * Get the ID of the row of the text table that contains the content of the
552 * revision's main slot, if that content is stored in the text table.
554 * If the content is stored elsewhere, this returns null.
556 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
557 * get that actual address that can be used with BlobStore::getBlob(); or use
558 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
562 public function getTextId() {
563 $slot = $this->getMainSlotRaw();
564 return $slot->hasAddress()
565 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
570 * Get parent revision ID (the original previous page revision)
572 * @return int|null The ID of the parent revision. 0 indicates that there is no
573 * parent revision. Null indicates that the parent revision is not known.
575 public function getParentId() {
576 return $this->mRecord
->getParentId();
580 * Returns the length of the text in this revision, or null if unknown.
584 public function getSize() {
585 return $this->mRecord
->getSize();
589 * Returns the base36 sha1 of the content in this revision, or null if unknown.
593 public function getSha1() {
594 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
595 return $this->mRecord
->getSha1();
599 * Returns the title of the page associated with this entry.
600 * Since 1.31, this will never return null.
602 * Will do a query, when title is not set and id is given.
606 public function getTitle() {
607 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
608 return Title
::newFromLinkTarget( $linkTarget );
612 * Set the title of the revision
614 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
616 * @param Title $title
618 public function setTitle( $title ) {
619 if ( !$title->equals( $this->getTitle() ) ) {
620 throw new InvalidArgumentException(
621 $title->getPrefixedText()
622 . ' is not the same as '
623 . $this->mRecord
->getPageAsLinkTarget()->__toString()
633 public function getPage() {
634 return $this->mRecord
->getPageId();
638 * Fetch revision's user id if it's available to the specified audience.
639 * If the specified audience does not have access to it, zero will be
642 * @param int $audience One of:
643 * Revision::FOR_PUBLIC to be displayed to all users
644 * Revision::FOR_THIS_USER to be displayed to the given user
645 * Revision::RAW get the ID regardless of permissions
646 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
647 * to the $audience parameter
650 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
653 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
657 $user = $this->mRecord
->getUser( $audience, $user );
658 return $user ?
$user->getId() : 0;
662 * Fetch revision's user id without regard for the current user's permissions
665 * @deprecated since 1.25, use getUser( Revision::RAW )
667 public function getRawUser() {
668 wfDeprecated( __METHOD__
, '1.25' );
669 return $this->getUser( self
::RAW
);
673 * Fetch revision's username if it's available to the specified audience.
674 * If the specified audience does not have access to the username, an
675 * empty string will be returned.
677 * @param int $audience One of:
678 * Revision::FOR_PUBLIC to be displayed to all users
679 * Revision::FOR_THIS_USER to be displayed to the given user
680 * Revision::RAW get the text regardless of permissions
681 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
682 * to the $audience parameter
685 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
688 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
692 $user = $this->mRecord
->getUser( $audience, $user );
693 return $user ?
$user->getName() : '';
697 * Fetch revision's username without regard for view restrictions
700 * @deprecated since 1.25, use getUserText( Revision::RAW )
702 public function getRawUserText() {
703 wfDeprecated( __METHOD__
, '1.25' );
704 return $this->getUserText( self
::RAW
);
708 * Fetch revision comment if it's available to the specified audience.
709 * If the specified audience does not have access to the comment, an
710 * empty string will be returned.
712 * @param int $audience One of:
713 * Revision::FOR_PUBLIC to be displayed to all users
714 * Revision::FOR_THIS_USER to be displayed to the given user
715 * Revision::RAW get the text regardless of permissions
716 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
717 * to the $audience parameter
720 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
723 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
727 $comment = $this->mRecord
->getComment( $audience, $user );
728 return $comment === null ?
null : $comment->text
;
732 * Fetch revision comment without regard for the current user's permissions
735 * @deprecated since 1.25, use getComment( Revision::RAW )
737 public function getRawComment() {
738 wfDeprecated( __METHOD__
, '1.25' );
739 return $this->getComment( self
::RAW
);
745 public function isMinor() {
746 return $this->mRecord
->isMinor();
750 * @return int Rcid of the unpatrolled row, zero if there isn't one
752 public function isUnpatrolled() {
753 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
757 * Get the RC object belonging to the current revision, if there's one
759 * @param int $flags (optional) $flags include:
760 * Revision::READ_LATEST : Select the data from the master
763 * @return RecentChange|null
765 public function getRecentChange( $flags = 0 ) {
766 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
770 * @param int $field One of DELETED_* bitfield constants
774 public function isDeleted( $field ) {
775 return $this->mRecord
->isDeleted( $field );
779 * Get the deletion bitfield of the revision
783 public function getVisibility() {
784 return $this->mRecord
->getVisibility();
788 * Fetch revision content if it's available to the specified audience.
789 * If the specified audience does not have the ability to view this
790 * revision, or the content could not be loaded, null will be returned.
792 * @param int $audience One of:
793 * Revision::FOR_PUBLIC to be displayed to all users
794 * Revision::FOR_THIS_USER to be displayed to $user
795 * Revision::RAW get the text regardless of permissions
796 * @param User $user User object to check for, only if FOR_THIS_USER is passed
797 * to the $audience parameter
799 * @return Content|null
801 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
804 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
809 return $this->mRecord
->getContent( 'main', $audience, $user );
811 catch ( RevisionAccessException
$e ) {
817 * Get original serialized data (without checking view restrictions)
820 * @deprecated since 1.31, use BlobStore::getBlob instead.
824 public function getSerializedData() {
825 $slot = $this->getMainSlotRaw();
826 return $slot->getContent()->serialize();
830 * Returns the content model for the main slot of this revision.
832 * If no content model was stored in the database, the default content model for the title is
833 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
834 * is used as a last resort.
836 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
838 * @return string The content model id associated with this revision,
839 * see the CONTENT_MODEL_XXX constants.
841 public function getContentModel() {
842 return $this->getMainSlotRaw()->getModel();
846 * Returns the content format for the main slot of this revision.
848 * If no content format was stored in the database, the default format for this
849 * revision's content model is returned.
851 * @todo: drop this, the format is irrelevant to the revision!
853 * @return string The content format id associated with this revision,
854 * see the CONTENT_FORMAT_XXX constants.
856 public function getContentFormat() {
857 $format = $this->getMainSlotRaw()->getFormat();
859 if ( $format === null ) {
860 // if no format was stored along with the blob, fall back to default format
861 $format = $this->getContentHandler()->getDefaultFormat();
868 * Returns the content handler appropriate for this revision's content model.
870 * @throws MWException
871 * @return ContentHandler
873 public function getContentHandler() {
874 return ContentHandler
::getForModelID( $this->getContentModel() );
880 public function getTimestamp() {
881 return $this->mRecord
->getTimestamp();
887 public function isCurrent() {
888 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
892 * Get previous revision for this title
894 * @return Revision|null
896 public function getPrevious() {
897 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
);
898 return $rec === null ?
null : new Revision( $rec );
902 * Get next revision for this title
904 * @return Revision|null
906 public function getNext() {
907 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
);
908 return $rec === null ?
null : new Revision( $rec );
912 * Get revision text associated with an old or archive row
914 * Both the flags and the text field must be included. Including the old_id
915 * field will activate cache usage as long as the $wiki parameter is not set.
917 * @param stdClass $row The text data
918 * @param string $prefix Table prefix (default 'old_')
919 * @param string|bool $wiki The name of the wiki to load the revision text from
920 * (same as the the wiki $row was loaded from) or false to indicate the local
921 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
922 * identifier as understood by the LoadBalancer class.
923 * @return string|false Text the text requested or false on failure
925 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
926 $textField = $prefix . 'text';
927 $flagsField = $prefix . 'flags';
929 if ( isset( $row->$flagsField ) ) {
930 $flags = explode( ',', $row->$flagsField );
935 if ( isset( $row->$textField ) ) {
936 $text = $row->$textField;
941 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
943 return self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
947 * If $wgCompressRevisions is enabled, we will compress data.
948 * The input string is modified in place.
949 * Return value is the flags field: contains 'gzip' if the
950 * data is compressed, and 'utf-8' if we're saving in UTF-8
953 * @param mixed &$text Reference to a text
956 public static function compressRevisionText( &$text ) {
957 return self
::getBlobStore()->compressData( $text );
961 * Re-converts revision text according to it's flags.
963 * @param mixed $text Reference to a text
964 * @param array $flags Compression flags
965 * @return string|bool Decompressed text, or false on failure
967 public static function decompressRevisionText( $text, $flags ) {
968 return self
::getBlobStore()->decompressData( $text, $flags );
972 * Insert a new revision into the database, returning the new revision ID
973 * number on success and dies horribly on failure.
975 * @param IDatabase $dbw (master connection)
976 * @throws MWException
977 * @return int The revision ID
979 public function insertOn( $dbw ) {
982 // Note that $this->mRecord->getId() will typically return null here, but not always,
983 // e.g. not when restoring a revision.
985 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
986 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
987 $this->mRecord
->setUser( $wgUser );
989 throw new MWException( 'Cannot insert revision with no associated user.' );
993 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
995 $this->mRecord
= $rec;
997 // Avoid PHP 7.1 warning of passing $this by reference
999 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1000 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1002 return $rec->getId();
1006 * Get the base 36 SHA-1 value for a string of text
1007 * @param string $text
1010 public static function base36Sha1( $text ) {
1011 return SlotRecord
::base36Sha1( $text );
1015 * Create a new null-revision for insertion into a page's
1016 * history. This will not re-save the text, but simply refer
1017 * to the text from the previous version.
1019 * Such revisions can for instance identify page rename
1020 * operations and other such meta-modifications.
1022 * @param IDatabase $dbw
1023 * @param int $pageId ID number of the page to read from
1024 * @param string $summary Revision's summary
1025 * @param bool $minor Whether the revision should be considered as minor
1026 * @param User|null $user User object to use or null for $wgUser
1027 * @return Revision|null Revision or null on error
1029 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1035 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1037 $title = Title
::newFromID( $pageId );
1038 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1040 return new Revision( $rec );
1044 * Determine if the current user is allowed to view a particular
1045 * field of this revision, if it's marked as deleted.
1047 * @param int $field One of self::DELETED_TEXT,
1048 * self::DELETED_COMMENT,
1049 * self::DELETED_USER
1050 * @param User|null $user User object to check, or null to use $wgUser
1053 public function userCan( $field, User
$user = null ) {
1054 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1058 * Determine if the current user is allowed to view a particular
1059 * field of this revision, if it's marked as deleted. This is used
1060 * by various classes to avoid duplication.
1062 * @param int $bitfield Current field
1063 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1064 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1065 * self::DELETED_USER = File::DELETED_USER
1066 * @param User|null $user User object to check, or null to use $wgUser
1067 * @param Title|null $title A Title object to check for per-page restrictions on,
1068 * instead of just plain userrights
1071 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1080 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1084 * Get rev_timestamp from rev_id, without loading the rest of the row
1086 * @param Title $title
1089 * @return string|bool False if not found
1091 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1092 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1096 * Get count of revisions per page...not very efficient
1098 * @param IDatabase $db
1099 * @param int $id Page id
1102 static function countByPageId( $db, $id ) {
1103 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1107 * Get count of revisions per page...not very efficient
1109 * @param IDatabase $db
1110 * @param Title $title
1113 static function countByTitle( $db, $title ) {
1114 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1118 * Check if no edits were made by other users since
1119 * the time a user started editing the page. Limit to
1120 * 50 revisions for the sake of performance.
1123 * @deprecated since 1.24
1125 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1126 * Database object or a database identifier usable with wfGetDB.
1127 * @param int $pageId The ID of the page in question
1128 * @param int $userId The ID of the user in question
1129 * @param string $since Look at edits since this time
1131 * @return bool True if the given user was the only one to edit since the given timestamp
1133 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1134 if ( is_int( $db ) ) {
1135 $db = wfGetDB( $db );
1138 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1142 * Load a revision based on a known page ID and current revision ID from the DB
1144 * This method allows for the use of caching, though accessing anything that normally
1145 * requires permission checks (aside from the text) will trigger a small DB lookup.
1146 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1148 * @param IDatabase $db ignored!
1149 * @param int|Title $pageIdOrTitle Page ID or Title object
1150 * @param int $revId Known current revision of this page. Determined automatically if not given.
1151 * @return Revision|bool Returns false if missing
1154 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1155 $title = $pageIdOrTitle instanceof Title
1157 : Title
::newFromID( $pageIdOrTitle );
1159 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1160 return $record ?
new Revision( $record ) : false;