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 * @return Revision|null
95 public static function newFromId( $id, $flags = 0 ) {
96 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags );
97 return $rec === null ?
null : new Revision( $rec, $flags );
101 * Load either the current, or a specified, revision
102 * that's attached to a given link target. If not attached
103 * to that link target, will return null.
106 * Revision::READ_LATEST : Select the data from the master
107 * Revision::READ_LOCKING : Select & lock the data from the master
109 * @param LinkTarget $linkTarget
110 * @param int $id (optional)
111 * @param int $flags Bitfield (optional)
112 * @return Revision|null
114 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
115 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $id, $flags );
116 return $rec === null ?
null : new Revision( $rec, $flags );
120 * Load either the current, or a specified, revision
121 * that's attached to a given page ID.
122 * Returns null if no such revision can be found.
125 * Revision::READ_LATEST : Select the data from the master (since 1.20)
126 * Revision::READ_LOCKING : Select & lock the data from the master
129 * @param int $revId (optional)
130 * @param int $flags Bitfield (optional)
131 * @return Revision|null
133 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
134 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
135 return $rec === null ?
null : new Revision( $rec, $flags );
139 * Make a fake revision object from an archive table row. This is queried
140 * for permissions or even inserted (as in Special:Undelete)
143 * @param array $overrides
145 * @throws MWException
148 public static function newFromArchiveRow( $row, $overrides = [] ) {
149 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, null, $overrides );
150 return new Revision( $rec );
156 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
157 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
158 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
159 * can be used as a temporary replacement, but should be avoided.
161 * @param object|array $row
164 public static function newFromRow( $row ) {
165 if ( is_array( $row ) ) {
166 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
168 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
171 return new Revision( $rec );
175 * Load a page revision from a given revision ID number.
176 * Returns null if no such revision can be found.
178 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
180 * @param IDatabase $db
182 * @return Revision|null
184 public static function loadFromId( $db, $id ) {
185 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
186 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
187 return $rec === null ?
null : new Revision( $rec );
191 * Load either the current, or a specified, revision
192 * that's attached to a given page. If not attached
193 * to that page, will return null.
195 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
197 * @param IDatabase $db
200 * @return Revision|null
202 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
203 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
204 return $rec === null ?
null : new Revision( $rec );
208 * Load either the current, or a specified, revision
209 * that's attached to a given page. If not attached
210 * to that page, will return null.
212 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
214 * @param IDatabase $db
215 * @param Title $title
217 * @return Revision|null
219 public static function loadFromTitle( $db, $title, $id = 0 ) {
220 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
221 return $rec === null ?
null : new Revision( $rec );
225 * Load the revision for the given title with the given timestamp.
226 * WARNING: Timestamps may in some circumstances not be unique,
227 * so this isn't the best key to use.
229 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
231 * @param IDatabase $db
232 * @param Title $title
233 * @param string $timestamp
234 * @return Revision|null
236 public static function loadFromTimestamp( $db, $title, $timestamp ) {
237 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
238 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
239 return $rec === null ?
null : new Revision( $rec );
243 * Return a wrapper for a series of database rows to
244 * fetch all of a given page's revisions in turn.
245 * Each row can be fed to the constructor to get objects.
247 * @param LinkTarget $title
248 * @return ResultWrapper
249 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
251 public static function fetchRevision( LinkTarget
$title ) {
252 wfDeprecated( __METHOD__
, '1.31' );
253 return new FakeResultWrapper( [] );
257 * Return the value of a select() JOIN conds array for the user table.
258 * This will get user table rows for logged-in users.
260 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
263 public static function userJoinCond() {
264 wfDeprecated( __METHOD__
, '1.31' );
265 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
269 * Return the value of a select() page conds array for the page table.
270 * This will assure that the revision(s) are not orphaned from live pages.
272 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
275 public static function pageJoinCond() {
276 wfDeprecated( __METHOD__
, '1.31' );
277 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
281 * Return the list of revision fields that should be selected to create
283 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
286 public static function selectFields() {
287 global $wgContentHandlerUseDB;
289 wfDeprecated( __METHOD__
, '1.31' );
305 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
307 if ( $wgContentHandlerUseDB ) {
308 $fields[] = 'rev_content_format';
309 $fields[] = 'rev_content_model';
316 * Return the list of revision fields that should be selected to create
317 * a new revision from an archive row.
318 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
321 public static function selectArchiveFields() {
322 global $wgContentHandlerUseDB;
324 wfDeprecated( __METHOD__
, '1.31' );
342 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
344 if ( $wgContentHandlerUseDB ) {
345 $fields[] = 'ar_content_format';
346 $fields[] = 'ar_content_model';
352 * Return the list of text fields that should be selected to read the
354 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
357 public static function selectTextFields() {
358 wfDeprecated( __METHOD__
, '1.31' );
366 * Return the list of page fields that should be selected from page table
367 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
370 public static function selectPageFields() {
371 wfDeprecated( __METHOD__
, '1.31' );
383 * Return the list of user fields that should be selected from user table
384 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
387 public static function selectUserFields() {
388 wfDeprecated( __METHOD__
, '1.31' );
389 return [ 'user_name' ];
393 * Return the tables, fields, and join conditions to be selected to create
394 * a new revision object.
396 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
397 * @param array $options Any combination of the following strings
398 * - 'page': Join with the page table, and select fields to identify the page
399 * - 'user': Join with the user table, and select the user name
400 * - 'text': Join with the text table, and select fields to load page text
401 * @return array With three keys:
402 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
403 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
404 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
406 public static function getQueryInfo( $options = [] ) {
407 return self
::getRevisionStore()->getQueryInfo( $options );
411 * Return the tables, fields, and join conditions to be selected to create
412 * a new archived revision object.
414 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
415 * @return array With three keys:
416 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
417 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
418 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
420 public static function getArchiveQueryInfo() {
421 return self
::getRevisionStore()->getArchiveQueryInfo();
425 * Do a batched query to get the parent revision lengths
426 * @param IDatabase $db
427 * @param array $revIds
430 public static function getParentLengths( $db, array $revIds ) {
431 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
435 * @param object|array|RevisionRecord $row Either a database row or an array
436 * @param int $queryFlags
437 * @param Title|null $title
441 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
444 if ( $row instanceof RevisionRecord
) {
445 $this->mRecord
= $row;
446 } elseif ( is_array( $row ) ) {
447 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
448 $row['user'] = $wgUser;
451 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
456 } elseif ( is_object( $row ) ) {
457 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
463 throw new InvalidArgumentException(
464 '$row must be a row object, an associative array, or a RevisionRecord'
470 * @return RevisionRecord
472 public function getRevisionRecord() {
473 return $this->mRecord
;
481 public function getId() {
482 return $this->mRecord
->getId();
486 * Set the revision ID
488 * This should only be used for proposed revisions that turn out to be null edits.
490 * @note Only supported on Revisions that were constructed based on associative arrays,
491 * since they are mutable.
494 * @param int|string $id
495 * @throws MWException
497 public function setId( $id ) {
498 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
499 $this->mRecord
->setId( intval( $id ) );
501 throw new MWException( __METHOD__
. ' is not supported on this instance' );
506 * Set the user ID/name
508 * This should only be used for proposed revisions that turn out to be null edits
510 * @note Only supported on Revisions that were constructed based on associative arrays,
511 * since they are mutable.
514 * @deprecated since 1.31, please reuse old Revision object
515 * @param int $id User ID
516 * @param string $name User name
517 * @throws MWException
519 public function setUserIdAndName( $id, $name ) {
520 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
521 $user = new UserIdentityValue( intval( $id ), $name );
522 $this->mRecord
->setUser( $user );
524 throw new MWException( __METHOD__
. ' is not supported on this instance' );
531 private function getMainSlotRaw() {
532 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
536 * Get the ID of the row of the text table that contains the content of the
537 * revision's main slot, if that content is stored in the text table.
539 * If the content is stored elsewhere, this returns null.
541 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
542 * get that actual address that can be used with BlobStore::getBlob(); or use
543 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
547 public function getTextId() {
548 $slot = $this->getMainSlotRaw();
549 return $slot->hasAddress()
550 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
555 * Get parent revision ID (the original previous page revision)
557 * @return int|null The ID of the parent revision. 0 indicates that there is no
558 * parent revision. Null indicates that the parent revision is not known.
560 public function getParentId() {
561 return $this->mRecord
->getParentId();
565 * Returns the length of the text in this revision, or null if unknown.
569 public function getSize() {
570 return $this->mRecord
->getSize();
574 * Returns the base36 sha1 of the content in this revision, or null if unknown.
578 public function getSha1() {
579 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
580 return $this->mRecord
->getSha1();
584 * Returns the title of the page associated with this entry.
585 * Since 1.31, this will never return null.
587 * Will do a query, when title is not set and id is given.
591 public function getTitle() {
592 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
593 return Title
::newFromLinkTarget( $linkTarget );
597 * Set the title of the revision
599 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
601 * @param Title $title
603 public function setTitle( $title ) {
604 if ( !$title->equals( $this->getTitle() ) ) {
605 throw new InvalidArgumentException(
606 $title->getPrefixedText()
607 . ' is not the same as '
608 . $this->mRecord
->getPageAsLinkTarget()->__toString()
618 public function getPage() {
619 return $this->mRecord
->getPageId();
623 * Fetch revision's user id if it's available to the specified audience.
624 * If the specified audience does not have access to it, zero will be
627 * @param int $audience One of:
628 * Revision::FOR_PUBLIC to be displayed to all users
629 * Revision::FOR_THIS_USER to be displayed to the given user
630 * Revision::RAW get the ID regardless of permissions
631 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
632 * to the $audience parameter
635 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
638 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
642 $user = $this->mRecord
->getUser( $audience, $user );
643 return $user ?
$user->getId() : 0;
647 * Fetch revision's user id without regard for the current user's permissions
650 * @deprecated since 1.25, use getUser( Revision::RAW )
652 public function getRawUser() {
653 wfDeprecated( __METHOD__
, '1.25' );
654 return $this->getUser( self
::RAW
);
658 * Fetch revision's username if it's available to the specified audience.
659 * If the specified audience does not have access to the username, an
660 * empty string will be returned.
662 * @param int $audience One of:
663 * Revision::FOR_PUBLIC to be displayed to all users
664 * Revision::FOR_THIS_USER to be displayed to the given user
665 * Revision::RAW get the text regardless of permissions
666 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
667 * to the $audience parameter
670 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
673 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
677 $user = $this->mRecord
->getUser( $audience, $user );
678 return $user ?
$user->getName() : '';
682 * Fetch revision's username without regard for view restrictions
685 * @deprecated since 1.25, use getUserText( Revision::RAW )
687 public function getRawUserText() {
688 wfDeprecated( __METHOD__
, '1.25' );
689 return $this->getUserText( self
::RAW
);
693 * Fetch revision comment if it's available to the specified audience.
694 * If the specified audience does not have access to the comment, an
695 * empty string will be returned.
697 * @param int $audience One of:
698 * Revision::FOR_PUBLIC to be displayed to all users
699 * Revision::FOR_THIS_USER to be displayed to the given user
700 * Revision::RAW get the text regardless of permissions
701 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
702 * to the $audience parameter
705 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
708 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
712 $comment = $this->mRecord
->getComment( $audience, $user );
713 return $comment === null ?
null : $comment->text
;
717 * Fetch revision comment without regard for the current user's permissions
720 * @deprecated since 1.25, use getComment( Revision::RAW )
722 public function getRawComment() {
723 wfDeprecated( __METHOD__
, '1.25' );
724 return $this->getComment( self
::RAW
);
730 public function isMinor() {
731 return $this->mRecord
->isMinor();
735 * @return int Rcid of the unpatrolled row, zero if there isn't one
737 public function isUnpatrolled() {
738 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
742 * Get the RC object belonging to the current revision, if there's one
744 * @param int $flags (optional) $flags include:
745 * Revision::READ_LATEST : Select the data from the master
748 * @return RecentChange|null
750 public function getRecentChange( $flags = 0 ) {
751 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
755 * @param int $field One of DELETED_* bitfield constants
759 public function isDeleted( $field ) {
760 return $this->mRecord
->isDeleted( $field );
764 * Get the deletion bitfield of the revision
768 public function getVisibility() {
769 return $this->mRecord
->getVisibility();
773 * Fetch revision content if it's available to the specified audience.
774 * If the specified audience does not have the ability to view this
775 * revision, or the content could not be loaded, null will be returned.
777 * @param int $audience One of:
778 * Revision::FOR_PUBLIC to be displayed to all users
779 * Revision::FOR_THIS_USER to be displayed to $user
780 * Revision::RAW get the text regardless of permissions
781 * @param User $user User object to check for, only if FOR_THIS_USER is passed
782 * to the $audience parameter
784 * @return Content|null
786 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
789 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
794 return $this->mRecord
->getContent( 'main', $audience, $user );
796 catch ( RevisionAccessException
$e ) {
802 * Get original serialized data (without checking view restrictions)
805 * @deprecated since 1.31, use BlobStore::getBlob instead.
809 public function getSerializedData() {
810 $slot = $this->getMainSlotRaw();
811 return $slot->getContent()->serialize();
815 * Returns the content model for the main slot of this revision.
817 * If no content model was stored in the database, the default content model for the title is
818 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
819 * is used as a last resort.
821 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
823 * @return string The content model id associated with this revision,
824 * see the CONTENT_MODEL_XXX constants.
826 public function getContentModel() {
827 return $this->getMainSlotRaw()->getModel();
831 * Returns the content format for the main slot of this revision.
833 * If no content format was stored in the database, the default format for this
834 * revision's content model is returned.
836 * @todo: drop this, the format is irrelevant to the revision!
838 * @return string The content format id associated with this revision,
839 * see the CONTENT_FORMAT_XXX constants.
841 public function getContentFormat() {
842 $format = $this->getMainSlotRaw()->getFormat();
844 if ( $format === null ) {
845 // if no format was stored along with the blob, fall back to default format
846 $format = $this->getContentHandler()->getDefaultFormat();
853 * Returns the content handler appropriate for this revision's content model.
855 * @throws MWException
856 * @return ContentHandler
858 public function getContentHandler() {
859 return ContentHandler
::getForModelID( $this->getContentModel() );
865 public function getTimestamp() {
866 return $this->mRecord
->getTimestamp();
872 public function isCurrent() {
873 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
877 * Get previous revision for this title
879 * @return Revision|null
881 public function getPrevious() {
882 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
);
883 return $rec === null ?
null : new Revision( $rec );
887 * Get next revision for this title
889 * @return Revision|null
891 public function getNext() {
892 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
);
893 return $rec === null ?
null : new Revision( $rec );
897 * Get revision text associated with an old or archive row
899 * Both the flags and the text field must be included. Including the old_id
900 * field will activate cache usage as long as the $wiki parameter is not set.
902 * @param stdClass $row The text data
903 * @param string $prefix Table prefix (default 'old_')
904 * @param string|bool $wiki The name of the wiki to load the revision text from
905 * (same as the the wiki $row was loaded from) or false to indicate the local
906 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
907 * identifier as understood by the LoadBalancer class.
908 * @return string|false Text the text requested or false on failure
910 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
911 $textField = $prefix . 'text';
912 $flagsField = $prefix . 'flags';
914 if ( isset( $row->$flagsField ) ) {
915 $flags = explode( ',', $row->$flagsField );
920 if ( isset( $row->$textField ) ) {
921 $text = $row->$textField;
926 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
928 return self
::getBlobStore()->expandBlob( $text, $flags, $cacheKey );
932 * If $wgCompressRevisions is enabled, we will compress data.
933 * The input string is modified in place.
934 * Return value is the flags field: contains 'gzip' if the
935 * data is compressed, and 'utf-8' if we're saving in UTF-8
938 * @param mixed &$text Reference to a text
941 public static function compressRevisionText( &$text ) {
942 return self
::getBlobStore()->compressData( $text );
946 * Re-converts revision text according to it's flags.
948 * @param mixed $text Reference to a text
949 * @param array $flags Compression flags
950 * @return string|bool Decompressed text, or false on failure
952 public static function decompressRevisionText( $text, $flags ) {
953 return self
::getBlobStore()->decompressData( $text, $flags );
957 * Insert a new revision into the database, returning the new revision ID
958 * number on success and dies horribly on failure.
960 * @param IDatabase $dbw (master connection)
961 * @throws MWException
962 * @return int The revision ID
964 public function insertOn( $dbw ) {
967 // Note that $this->mRecord->getId() will typically return null here, but not always,
968 // e.g. not when restoring a revision.
970 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
971 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
972 $this->mRecord
->setUser( $wgUser );
974 throw new MWException( 'Cannot insert revision with no associated user.' );
978 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
980 $this->mRecord
= $rec;
982 // TODO: hard-deprecate in 1.32 (or even 1.31?)
983 Hooks
::run( 'RevisionInsertComplete', [ $this, null, null ] );
985 return $rec->getId();
989 * Get the base 36 SHA-1 value for a string of text
990 * @param string $text
993 public static function base36Sha1( $text ) {
994 return SlotRecord
::base36Sha1( $text );
998 * Create a new null-revision for insertion into a page's
999 * history. This will not re-save the text, but simply refer
1000 * to the text from the previous version.
1002 * Such revisions can for instance identify page rename
1003 * operations and other such meta-modifications.
1005 * @param IDatabase $dbw
1006 * @param int $pageId ID number of the page to read from
1007 * @param string $summary Revision's summary
1008 * @param bool $minor Whether the revision should be considered as minor
1009 * @param User|null $user User object to use or null for $wgUser
1010 * @return Revision|null Revision or null on error
1012 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1018 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1020 $title = Title
::newFromID( $pageId );
1021 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1023 return new Revision( $rec );
1027 * Determine if the current user is allowed to view a particular
1028 * field of this revision, if it's marked as deleted.
1030 * @param int $field One of self::DELETED_TEXT,
1031 * self::DELETED_COMMENT,
1032 * self::DELETED_USER
1033 * @param User|null $user User object to check, or null to use $wgUser
1036 public function userCan( $field, User
$user = null ) {
1037 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1041 * Determine if the current user is allowed to view a particular
1042 * field of this revision, if it's marked as deleted. This is used
1043 * by various classes to avoid duplication.
1045 * @param int $bitfield Current field
1046 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1047 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1048 * self::DELETED_USER = File::DELETED_USER
1049 * @param User|null $user User object to check, or null to use $wgUser
1050 * @param Title|null $title A Title object to check for per-page restrictions on,
1051 * instead of just plain userrights
1054 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1063 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1067 * Get rev_timestamp from rev_id, without loading the rest of the row
1069 * @param Title $title
1072 * @return string|bool False if not found
1074 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1075 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1079 * Get count of revisions per page...not very efficient
1081 * @param IDatabase $db
1082 * @param int $id Page id
1085 static function countByPageId( $db, $id ) {
1086 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1090 * Get count of revisions per page...not very efficient
1092 * @param IDatabase $db
1093 * @param Title $title
1096 static function countByTitle( $db, $title ) {
1097 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1101 * Check if no edits were made by other users since
1102 * the time a user started editing the page. Limit to
1103 * 50 revisions for the sake of performance.
1106 * @deprecated since 1.24
1108 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1109 * Database object or a database identifier usable with wfGetDB.
1110 * @param int $pageId The ID of the page in question
1111 * @param int $userId The ID of the user in question
1112 * @param string $since Look at edits since this time
1114 * @return bool True if the given user was the only one to edit since the given timestamp
1116 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1117 if ( is_int( $db ) ) {
1118 $db = wfGetDB( $db );
1121 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1125 * Load a revision based on a known page ID and current revision ID from the DB
1127 * This method allows for the use of caching, though accessing anything that normally
1128 * requires permission checks (aside from the text) will trigger a small DB lookup.
1129 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1131 * @param IDatabase $db ignored!
1132 * @param int|Title $pageIdOrTitle Page ID or Title object
1133 * @param int $revId Known current revision of this page. Determined automatically if not given.
1134 * @return Revision|bool Returns false if missing
1137 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1138 $title = $pageIdOrTitle instanceof Title
1140 : Title
::newFromID( $pageIdOrTitle );
1142 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1143 return $record ?
new Revision( $record ) : false;