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 ) {
152 * MCR Migration: https://phabricator.wikimedia.org/T183564
153 * This method used to overwrite attributes, then passed to Revision::__construct
154 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
155 * So do a conversion here.
157 if ( array_key_exists( 'page', $overrides ) ) {
158 $overrides['page_id'] = $overrides['page'];
159 unset( $overrides['page'] );
162 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
163 return new Revision( $rec, self
::READ_NORMAL
, $title );
169 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
170 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
171 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
172 * can be used as a temporary replacement, but should be avoided.
174 * @param object|array $row
177 public static function newFromRow( $row ) {
178 if ( is_array( $row ) ) {
179 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
181 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
184 return new Revision( $rec );
188 * Load a page revision from a given revision ID number.
189 * Returns null if no such revision can be found.
191 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
193 * @param IDatabase $db
195 * @return Revision|null
197 public static function loadFromId( $db, $id ) {
198 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
199 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
200 return $rec === null ?
null : new Revision( $rec );
204 * Load either the current, or a specified, revision
205 * that's attached to a given page. If not attached
206 * to that page, will return null.
208 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
210 * @param IDatabase $db
213 * @return Revision|null
215 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
216 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
217 return $rec === null ?
null : new Revision( $rec );
221 * Load either the current, or a specified, revision
222 * that's attached to a given page. If not attached
223 * to that page, will return null.
225 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
227 * @param IDatabase $db
228 * @param Title $title
230 * @return Revision|null
232 public static function loadFromTitle( $db, $title, $id = 0 ) {
233 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
234 return $rec === null ?
null : new Revision( $rec );
238 * Load the revision for the given title with the given timestamp.
239 * WARNING: Timestamps may in some circumstances not be unique,
240 * so this isn't the best key to use.
242 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
244 * @param IDatabase $db
245 * @param Title $title
246 * @param string $timestamp
247 * @return Revision|null
249 public static function loadFromTimestamp( $db, $title, $timestamp ) {
250 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
251 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
252 return $rec === null ?
null : new Revision( $rec );
256 * Return a wrapper for a series of database rows to
257 * fetch all of a given page's revisions in turn.
258 * Each row can be fed to the constructor to get objects.
260 * @param LinkTarget $title
261 * @return ResultWrapper
262 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
264 public static function fetchRevision( LinkTarget
$title ) {
265 wfDeprecated( __METHOD__
, '1.31' );
266 return new FakeResultWrapper( [] );
270 * Return the value of a select() JOIN conds array for the user table.
271 * This will get user table rows for logged-in users.
273 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
276 public static function userJoinCond() {
277 wfDeprecated( __METHOD__
, '1.31' );
278 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
282 * Return the value of a select() page conds array for the page table.
283 * This will assure that the revision(s) are not orphaned from live pages.
285 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
288 public static function pageJoinCond() {
289 wfDeprecated( __METHOD__
, '1.31' );
290 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
294 * Return the list of revision fields that should be selected to create
296 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
299 public static function selectFields() {
300 global $wgContentHandlerUseDB;
302 wfDeprecated( __METHOD__
, '1.31' );
318 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
320 if ( $wgContentHandlerUseDB ) {
321 $fields[] = 'rev_content_format';
322 $fields[] = 'rev_content_model';
329 * Return the list of revision fields that should be selected to create
330 * a new revision from an archive row.
331 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
334 public static function selectArchiveFields() {
335 global $wgContentHandlerUseDB;
337 wfDeprecated( __METHOD__
, '1.31' );
355 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
357 if ( $wgContentHandlerUseDB ) {
358 $fields[] = 'ar_content_format';
359 $fields[] = 'ar_content_model';
365 * Return the list of text fields that should be selected to read the
367 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
370 public static function selectTextFields() {
371 wfDeprecated( __METHOD__
, '1.31' );
379 * Return the list of page fields that should be selected from page table
380 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
383 public static function selectPageFields() {
384 wfDeprecated( __METHOD__
, '1.31' );
396 * Return the list of user fields that should be selected from user table
397 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
400 public static function selectUserFields() {
401 wfDeprecated( __METHOD__
, '1.31' );
402 return [ 'user_name' ];
406 * Return the tables, fields, and join conditions to be selected to create
407 * a new revision object.
409 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
410 * @param array $options Any combination of the following strings
411 * - 'page': Join with the page table, and select fields to identify the page
412 * - 'user': Join with the user table, and select the user name
413 * - 'text': Join with the text table, and select fields to load page text
414 * @return array With three keys:
415 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
416 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
417 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
419 public static function getQueryInfo( $options = [] ) {
420 return self
::getRevisionStore()->getQueryInfo( $options );
424 * Return the tables, fields, and join conditions to be selected to create
425 * a new archived revision object.
427 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
428 * @return array With three keys:
429 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
430 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
431 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
433 public static function getArchiveQueryInfo() {
434 return self
::getRevisionStore()->getArchiveQueryInfo();
438 * Do a batched query to get the parent revision lengths
439 * @param IDatabase $db
440 * @param array $revIds
443 public static function getParentLengths( $db, array $revIds ) {
444 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
448 * @param object|array|RevisionRecord $row Either a database row or an array
449 * @param int $queryFlags
450 * @param Title|null $title
454 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
457 if ( $row instanceof RevisionRecord
) {
458 $this->mRecord
= $row;
459 } elseif ( is_array( $row ) ) {
460 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
461 $row['user'] = $wgUser;
464 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
469 } elseif ( is_object( $row ) ) {
470 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
476 throw new InvalidArgumentException(
477 '$row must be a row object, an associative array, or a RevisionRecord'
483 * @return RevisionRecord
485 public function getRevisionRecord() {
486 return $this->mRecord
;
494 public function getId() {
495 return $this->mRecord
->getId();
499 * Set the revision ID
501 * This should only be used for proposed revisions that turn out to be null edits.
503 * @note Only supported on Revisions that were constructed based on associative arrays,
504 * since they are mutable.
507 * @param int|string $id
508 * @throws MWException
510 public function setId( $id ) {
511 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
512 $this->mRecord
->setId( intval( $id ) );
514 throw new MWException( __METHOD__
. ' is not supported on this instance' );
519 * Set the user ID/name
521 * This should only be used for proposed revisions that turn out to be null edits
523 * @note Only supported on Revisions that were constructed based on associative arrays,
524 * since they are mutable.
527 * @deprecated since 1.31, please reuse old Revision object
528 * @param int $id User ID
529 * @param string $name User name
530 * @throws MWException
532 public function setUserIdAndName( $id, $name ) {
533 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
534 $user = new UserIdentityValue( intval( $id ), $name );
535 $this->mRecord
->setUser( $user );
537 throw new MWException( __METHOD__
. ' is not supported on this instance' );
544 private function getMainSlotRaw() {
545 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
549 * Get the ID of the row of the text table that contains the content of the
550 * revision's main slot, if that content is stored in the text table.
552 * If the content is stored elsewhere, this returns null.
554 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
555 * get that actual address that can be used with BlobStore::getBlob(); or use
556 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
560 public function getTextId() {
561 $slot = $this->getMainSlotRaw();
562 return $slot->hasAddress()
563 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
568 * Get parent revision ID (the original previous page revision)
570 * @return int|null The ID of the parent revision. 0 indicates that there is no
571 * parent revision. Null indicates that the parent revision is not known.
573 public function getParentId() {
574 return $this->mRecord
->getParentId();
578 * Returns the length of the text in this revision, or null if unknown.
582 public function getSize() {
583 return $this->mRecord
->getSize();
587 * Returns the base36 sha1 of the content in this revision, or null if unknown.
591 public function getSha1() {
592 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
593 return $this->mRecord
->getSha1();
597 * Returns the title of the page associated with this entry.
598 * Since 1.31, this will never return null.
600 * Will do a query, when title is not set and id is given.
604 public function getTitle() {
605 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
606 return Title
::newFromLinkTarget( $linkTarget );
610 * Set the title of the revision
612 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
614 * @param Title $title
616 public function setTitle( $title ) {
617 if ( !$title->equals( $this->getTitle() ) ) {
618 throw new InvalidArgumentException(
619 $title->getPrefixedText()
620 . ' is not the same as '
621 . $this->mRecord
->getPageAsLinkTarget()->__toString()
631 public function getPage() {
632 return $this->mRecord
->getPageId();
636 * Fetch revision's user id if it's available to the specified audience.
637 * If the specified audience does not have access to it, zero will be
640 * @param int $audience One of:
641 * Revision::FOR_PUBLIC to be displayed to all users
642 * Revision::FOR_THIS_USER to be displayed to the given user
643 * Revision::RAW get the ID regardless of permissions
644 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
645 * to the $audience parameter
648 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
651 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
655 $user = $this->mRecord
->getUser( $audience, $user );
656 return $user ?
$user->getId() : 0;
660 * Fetch revision's user id without regard for the current user's permissions
663 * @deprecated since 1.25, use getUser( Revision::RAW )
665 public function getRawUser() {
666 wfDeprecated( __METHOD__
, '1.25' );
667 return $this->getUser( self
::RAW
);
671 * Fetch revision's username if it's available to the specified audience.
672 * If the specified audience does not have access to the username, an
673 * empty string will be returned.
675 * @param int $audience One of:
676 * Revision::FOR_PUBLIC to be displayed to all users
677 * Revision::FOR_THIS_USER to be displayed to the given user
678 * Revision::RAW get the text regardless of permissions
679 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
680 * to the $audience parameter
683 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
686 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
690 $user = $this->mRecord
->getUser( $audience, $user );
691 return $user ?
$user->getName() : '';
695 * Fetch revision's username without regard for view restrictions
698 * @deprecated since 1.25, use getUserText( Revision::RAW )
700 public function getRawUserText() {
701 wfDeprecated( __METHOD__
, '1.25' );
702 return $this->getUserText( self
::RAW
);
706 * Fetch revision comment if it's available to the specified audience.
707 * If the specified audience does not have access to the comment, an
708 * empty string will be returned.
710 * @param int $audience One of:
711 * Revision::FOR_PUBLIC to be displayed to all users
712 * Revision::FOR_THIS_USER to be displayed to the given user
713 * Revision::RAW get the text regardless of permissions
714 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
715 * to the $audience parameter
718 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
721 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
725 $comment = $this->mRecord
->getComment( $audience, $user );
726 return $comment === null ?
null : $comment->text
;
730 * Fetch revision comment without regard for the current user's permissions
733 * @deprecated since 1.25, use getComment( Revision::RAW )
735 public function getRawComment() {
736 wfDeprecated( __METHOD__
, '1.25' );
737 return $this->getComment( self
::RAW
);
743 public function isMinor() {
744 return $this->mRecord
->isMinor();
748 * @return int Rcid of the unpatrolled row, zero if there isn't one
750 public function isUnpatrolled() {
751 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
755 * Get the RC object belonging to the current revision, if there's one
757 * @param int $flags (optional) $flags include:
758 * Revision::READ_LATEST : Select the data from the master
761 * @return RecentChange|null
763 public function getRecentChange( $flags = 0 ) {
764 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
768 * @param int $field One of DELETED_* bitfield constants
772 public function isDeleted( $field ) {
773 return $this->mRecord
->isDeleted( $field );
777 * Get the deletion bitfield of the revision
781 public function getVisibility() {
782 return $this->mRecord
->getVisibility();
786 * Fetch revision content if it's available to the specified audience.
787 * If the specified audience does not have the ability to view this
788 * revision, or the content could not be loaded, null will be returned.
790 * @param int $audience One of:
791 * Revision::FOR_PUBLIC to be displayed to all users
792 * Revision::FOR_THIS_USER to be displayed to $user
793 * Revision::RAW get the text regardless of permissions
794 * @param User $user User object to check for, only if FOR_THIS_USER is passed
795 * to the $audience parameter
797 * @return Content|null
799 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
802 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
807 return $this->mRecord
->getContent( 'main', $audience, $user );
809 catch ( RevisionAccessException
$e ) {
815 * Get original serialized data (without checking view restrictions)
818 * @deprecated since 1.31, use BlobStore::getBlob instead.
822 public function getSerializedData() {
823 $slot = $this->getMainSlotRaw();
824 return $slot->getContent()->serialize();
828 * Returns the content model for the main slot of this revision.
830 * If no content model was stored in the database, the default content model for the title is
831 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
832 * is used as a last resort.
834 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
836 * @return string The content model id associated with this revision,
837 * see the CONTENT_MODEL_XXX constants.
839 public function getContentModel() {
840 return $this->getMainSlotRaw()->getModel();
844 * Returns the content format for the main slot of this revision.
846 * If no content format was stored in the database, the default format for this
847 * revision's content model is returned.
849 * @todo: drop this, the format is irrelevant to the revision!
851 * @return string The content format id associated with this revision,
852 * see the CONTENT_FORMAT_XXX constants.
854 public function getContentFormat() {
855 $format = $this->getMainSlotRaw()->getFormat();
857 if ( $format === null ) {
858 // if no format was stored along with the blob, fall back to default format
859 $format = $this->getContentHandler()->getDefaultFormat();
866 * Returns the content handler appropriate for this revision's content model.
868 * @throws MWException
869 * @return ContentHandler
871 public function getContentHandler() {
872 return ContentHandler
::getForModelID( $this->getContentModel() );
878 public function getTimestamp() {
879 return $this->mRecord
->getTimestamp();
885 public function isCurrent() {
886 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
890 * Get previous revision for this title
892 * @return Revision|null
894 public function getPrevious() {
895 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $this->getTitle() );
898 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
902 * Get next revision for this title
904 * @return Revision|null
906 public function getNext() {
907 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $this->getTitle() );
910 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
914 * Get revision text associated with an old or archive row
916 * Both the flags and the text field must be included. Including the old_id
917 * field will activate cache usage as long as the $wiki parameter is not set.
919 * @param stdClass $row The text data
920 * @param string $prefix Table prefix (default 'old_')
921 * @param string|bool $wiki The name of the wiki to load the revision text from
922 * (same as the the wiki $row was loaded from) or false to indicate the local
923 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
924 * identifier as understood by the LoadBalancer class.
925 * @return string|false Text the text requested or false on failure
927 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
928 $textField = $prefix . 'text';
929 $flagsField = $prefix . 'flags';
931 if ( isset( $row->$flagsField ) ) {
932 $flags = explode( ',', $row->$flagsField );
937 if ( isset( $row->$textField ) ) {
938 $text = $row->$textField;
943 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
945 return self
::getBlobStore()->expandBlob( $text, $flags, $cacheKey );
949 * If $wgCompressRevisions is enabled, we will compress data.
950 * The input string is modified in place.
951 * Return value is the flags field: contains 'gzip' if the
952 * data is compressed, and 'utf-8' if we're saving in UTF-8
955 * @param mixed &$text Reference to a text
958 public static function compressRevisionText( &$text ) {
959 return self
::getBlobStore()->compressData( $text );
963 * Re-converts revision text according to it's flags.
965 * @param mixed $text Reference to a text
966 * @param array $flags Compression flags
967 * @return string|bool Decompressed text, or false on failure
969 public static function decompressRevisionText( $text, $flags ) {
970 return self
::getBlobStore()->decompressData( $text, $flags );
974 * Insert a new revision into the database, returning the new revision ID
975 * number on success and dies horribly on failure.
977 * @param IDatabase $dbw (master connection)
978 * @throws MWException
979 * @return int The revision ID
981 public function insertOn( $dbw ) {
984 // Note that $this->mRecord->getId() will typically return null here, but not always,
985 // e.g. not when restoring a revision.
987 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
988 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
989 $this->mRecord
->setUser( $wgUser );
991 throw new MWException( 'Cannot insert revision with no associated user.' );
995 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
997 $this->mRecord
= $rec;
999 // Avoid PHP 7.1 warning of passing $this by reference
1001 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1002 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1004 return $rec->getId();
1008 * Get the base 36 SHA-1 value for a string of text
1009 * @param string $text
1012 public static function base36Sha1( $text ) {
1013 return SlotRecord
::base36Sha1( $text );
1017 * Create a new null-revision for insertion into a page's
1018 * history. This will not re-save the text, but simply refer
1019 * to the text from the previous version.
1021 * Such revisions can for instance identify page rename
1022 * operations and other such meta-modifications.
1024 * @param IDatabase $dbw
1025 * @param int $pageId ID number of the page to read from
1026 * @param string $summary Revision's summary
1027 * @param bool $minor Whether the revision should be considered as minor
1028 * @param User|null $user User object to use or null for $wgUser
1029 * @return Revision|null Revision or null on error
1031 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1037 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1039 $title = Title
::newFromID( $pageId );
1040 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1042 return new Revision( $rec );
1046 * Determine if the current user is allowed to view a particular
1047 * field of this revision, if it's marked as deleted.
1049 * @param int $field One of self::DELETED_TEXT,
1050 * self::DELETED_COMMENT,
1051 * self::DELETED_USER
1052 * @param User|null $user User object to check, or null to use $wgUser
1055 public function userCan( $field, User
$user = null ) {
1056 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1060 * Determine if the current user is allowed to view a particular
1061 * field of this revision, if it's marked as deleted. This is used
1062 * by various classes to avoid duplication.
1064 * @param int $bitfield Current field
1065 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1066 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1067 * self::DELETED_USER = File::DELETED_USER
1068 * @param User|null $user User object to check, or null to use $wgUser
1069 * @param Title|null $title A Title object to check for per-page restrictions on,
1070 * instead of just plain userrights
1073 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1082 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1086 * Get rev_timestamp from rev_id, without loading the rest of the row
1088 * @param Title $title
1091 * @return string|bool False if not found
1093 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1094 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1098 * Get count of revisions per page...not very efficient
1100 * @param IDatabase $db
1101 * @param int $id Page id
1104 static function countByPageId( $db, $id ) {
1105 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1109 * Get count of revisions per page...not very efficient
1111 * @param IDatabase $db
1112 * @param Title $title
1115 static function countByTitle( $db, $title ) {
1116 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1120 * Check if no edits were made by other users since
1121 * the time a user started editing the page. Limit to
1122 * 50 revisions for the sake of performance.
1125 * @deprecated since 1.24
1127 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1128 * Database object or a database identifier usable with wfGetDB.
1129 * @param int $pageId The ID of the page in question
1130 * @param int $userId The ID of the user in question
1131 * @param string $since Look at edits since this time
1133 * @return bool True if the given user was the only one to edit since the given timestamp
1135 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1136 if ( is_int( $db ) ) {
1137 $db = wfGetDB( $db );
1140 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1144 * Load a revision based on a known page ID and current revision ID from the DB
1146 * This method allows for the use of caching, though accessing anything that normally
1147 * requires permission checks (aside from the text) will trigger a small DB lookup.
1148 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1150 * @param IDatabase $db ignored!
1151 * @param int|Title $pageIdOrTitle Page ID or Title object
1152 * @param int $revId Known current revision of this page. Determined automatically if not given.
1153 * @return Revision|bool Returns false if missing
1156 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1157 $title = $pageIdOrTitle instanceof Title
1159 : Title
::newFromID( $pageIdOrTitle );
1161 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1162 return $record ?
new Revision( $record ) : false;