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 * @param Title $title (optional)
98 * @return Revision|null
100 public static function newFromId( $id, $flags = 0, Title
$title = null ) {
101 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags, $title );
102 return $rec === null ?
null : new Revision( $rec, $flags, $title );
106 * Load either the current, or a specified, revision
107 * that's attached to a given link target. If not attached
108 * to that link target, will return null.
111 * Revision::READ_LATEST : Select the data from the master
112 * Revision::READ_LOCKING : Select & lock the data from the master
114 * @param LinkTarget $linkTarget
115 * @param int $id (optional)
116 * @param int $flags Bitfield (optional)
117 * @return Revision|null
119 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
120 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $id, $flags );
121 return $rec === null ?
null : new Revision( $rec, $flags );
125 * Load either the current, or a specified, revision
126 * that's attached to a given page ID.
127 * Returns null if no such revision can be found.
130 * Revision::READ_LATEST : Select the data from the master (since 1.20)
131 * Revision::READ_LOCKING : Select & lock the data from the master
134 * @param int $revId (optional)
135 * @param int $flags Bitfield (optional)
136 * @return Revision|null
138 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
139 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
140 return $rec === null ?
null : new Revision( $rec, $flags );
144 * Make a fake revision object from an archive table row. This is queried
145 * for permissions or even inserted (as in Special:Undelete)
148 * @param array $overrides
149 * @param Title $title (optional)
151 * @throws MWException
154 public static function newFromArchiveRow( $row, $overrides = [], Title
$title = null ) {
156 * MCR Migration: https://phabricator.wikimedia.org/T183564
157 * This method used to overwrite attributes, then passed to Revision::__construct
158 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
159 * So do a conversion here.
161 if ( array_key_exists( 'page', $overrides ) ) {
162 $overrides['page_id'] = $overrides['page'];
163 unset( $overrides['page'] );
166 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
167 return new Revision( $rec, self
::READ_NORMAL
, $title );
173 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
174 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
175 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
176 * can be used as a temporary replacement, but should be avoided.
178 * @param object|array $row
181 public static function newFromRow( $row ) {
182 if ( is_array( $row ) ) {
183 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
185 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
188 return new Revision( $rec );
192 * Load a page revision from a given revision ID number.
193 * Returns null if no such revision can be found.
195 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
197 * @param IDatabase $db
199 * @return Revision|null
201 public static function loadFromId( $db, $id ) {
202 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
203 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $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::getRevisionByPageId() instead.
214 * @param IDatabase $db
217 * @return Revision|null
219 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
220 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
221 return $rec === null ?
null : new Revision( $rec );
225 * Load either the current, or a specified, revision
226 * that's attached to a given page. If not attached
227 * to that page, will return null.
229 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
231 * @param IDatabase $db
232 * @param Title $title
234 * @return Revision|null
236 public static function loadFromTitle( $db, $title, $id = 0 ) {
237 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
238 return $rec === null ?
null : new Revision( $rec );
242 * Load the revision for the given title with the given timestamp.
243 * WARNING: Timestamps may in some circumstances not be unique,
244 * so this isn't the best key to use.
246 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
248 * @param IDatabase $db
249 * @param Title $title
250 * @param string $timestamp
251 * @return Revision|null
253 public static function loadFromTimestamp( $db, $title, $timestamp ) {
254 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
255 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
256 return $rec === null ?
null : new Revision( $rec );
260 * Return a wrapper for a series of database rows to
261 * fetch all of a given page's revisions in turn.
262 * Each row can be fed to the constructor to get objects.
264 * @param LinkTarget $title
265 * @return ResultWrapper
266 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
268 public static function fetchRevision( LinkTarget
$title ) {
269 wfDeprecated( __METHOD__
, '1.31' );
270 return new FakeResultWrapper( [] );
274 * Return the value of a select() JOIN conds array for the user table.
275 * This will get user table rows for logged-in users.
277 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
280 public static function userJoinCond() {
281 wfDeprecated( __METHOD__
, '1.31' );
282 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
286 * Return the value of a select() page conds array for the page table.
287 * This will assure that the revision(s) are not orphaned from live pages.
289 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
292 public static function pageJoinCond() {
293 wfDeprecated( __METHOD__
, '1.31' );
294 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
298 * Return the list of revision fields that should be selected to create
300 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
303 public static function selectFields() {
304 global $wgContentHandlerUseDB;
306 wfDeprecated( __METHOD__
, '1.31' );
322 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
324 if ( $wgContentHandlerUseDB ) {
325 $fields[] = 'rev_content_format';
326 $fields[] = 'rev_content_model';
333 * Return the list of revision fields that should be selected to create
334 * a new revision from an archive row.
335 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
338 public static function selectArchiveFields() {
339 global $wgContentHandlerUseDB;
341 wfDeprecated( __METHOD__
, '1.31' );
359 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
361 if ( $wgContentHandlerUseDB ) {
362 $fields[] = 'ar_content_format';
363 $fields[] = 'ar_content_model';
369 * Return the list of text fields that should be selected to read the
371 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
374 public static function selectTextFields() {
375 wfDeprecated( __METHOD__
, '1.31' );
383 * Return the list of page fields that should be selected from page table
384 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
387 public static function selectPageFields() {
388 wfDeprecated( __METHOD__
, '1.31' );
400 * Return the list of user fields that should be selected from user table
401 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
404 public static function selectUserFields() {
405 wfDeprecated( __METHOD__
, '1.31' );
406 return [ 'user_name' ];
410 * Return the tables, fields, and join conditions to be selected to create
411 * a new revision object.
413 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
414 * @param array $options Any combination of the following strings
415 * - 'page': Join with the page table, and select fields to identify the page
416 * - 'user': Join with the user table, and select the user name
417 * - 'text': Join with the text table, and select fields to load page text
418 * @return array With three keys:
419 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
420 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
421 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
423 public static function getQueryInfo( $options = [] ) {
424 return self
::getRevisionStore()->getQueryInfo( $options );
428 * Return the tables, fields, and join conditions to be selected to create
429 * a new archived revision object.
431 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
432 * @return array With three keys:
433 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
434 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
435 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
437 public static function getArchiveQueryInfo() {
438 return self
::getRevisionStore()->getArchiveQueryInfo();
442 * Do a batched query to get the parent revision lengths
443 * @param IDatabase $db
444 * @param array $revIds
447 public static function getParentLengths( $db, array $revIds ) {
448 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
452 * @param object|array|RevisionRecord $row Either a database row or an array
453 * @param int $queryFlags
454 * @param Title|null $title
458 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
461 if ( $row instanceof RevisionRecord
) {
462 $this->mRecord
= $row;
463 } elseif ( is_array( $row ) ) {
464 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
465 $row['user'] = $wgUser;
468 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
473 } elseif ( is_object( $row ) ) {
474 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
480 throw new InvalidArgumentException(
481 '$row must be a row object, an associative array, or a RevisionRecord'
487 * @return RevisionRecord
489 public function getRevisionRecord() {
490 return $this->mRecord
;
498 public function getId() {
499 return $this->mRecord
->getId();
503 * Set the revision ID
505 * This should only be used for proposed revisions that turn out to be null edits.
507 * @note Only supported on Revisions that were constructed based on associative arrays,
508 * since they are mutable.
511 * @param int|string $id
512 * @throws MWException
514 public function setId( $id ) {
515 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
516 $this->mRecord
->setId( intval( $id ) );
518 throw new MWException( __METHOD__
. ' is not supported on this instance' );
523 * Set the user ID/name
525 * This should only be used for proposed revisions that turn out to be null edits
527 * @note Only supported on Revisions that were constructed based on associative arrays,
528 * since they are mutable.
531 * @deprecated since 1.31, please reuse old Revision object
532 * @param int $id User ID
533 * @param string $name User name
534 * @throws MWException
536 public function setUserIdAndName( $id, $name ) {
537 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
538 $user = new UserIdentityValue( intval( $id ), $name );
539 $this->mRecord
->setUser( $user );
541 throw new MWException( __METHOD__
. ' is not supported on this instance' );
548 private function getMainSlotRaw() {
549 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
553 * Get the ID of the row of the text table that contains the content of the
554 * revision's main slot, if that content is stored in the text table.
556 * If the content is stored elsewhere, this returns null.
558 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
559 * get that actual address that can be used with BlobStore::getBlob(); or use
560 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
564 public function getTextId() {
565 $slot = $this->getMainSlotRaw();
566 return $slot->hasAddress()
567 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
572 * Get parent revision ID (the original previous page revision)
574 * @return int|null The ID of the parent revision. 0 indicates that there is no
575 * parent revision. Null indicates that the parent revision is not known.
577 public function getParentId() {
578 return $this->mRecord
->getParentId();
582 * Returns the length of the text in this revision, or null if unknown.
586 public function getSize() {
587 return $this->mRecord
->getSize();
591 * Returns the base36 sha1 of the content in this revision, or null if unknown.
595 public function getSha1() {
596 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
597 return $this->mRecord
->getSha1();
601 * Returns the title of the page associated with this entry.
602 * Since 1.31, this will never return null.
604 * Will do a query, when title is not set and id is given.
608 public function getTitle() {
609 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
610 return Title
::newFromLinkTarget( $linkTarget );
614 * Set the title of the revision
616 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
618 * @param Title $title
620 public function setTitle( $title ) {
621 if ( !$title->equals( $this->getTitle() ) ) {
622 throw new InvalidArgumentException(
623 $title->getPrefixedText()
624 . ' is not the same as '
625 . $this->mRecord
->getPageAsLinkTarget()->__toString()
635 public function getPage() {
636 return $this->mRecord
->getPageId();
640 * Fetch revision's user id if it's available to the specified audience.
641 * If the specified audience does not have access to it, zero will be
644 * @param int $audience One of:
645 * Revision::FOR_PUBLIC to be displayed to all users
646 * Revision::FOR_THIS_USER to be displayed to the given user
647 * Revision::RAW get the ID regardless of permissions
648 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
649 * to the $audience parameter
652 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
655 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
659 $user = $this->mRecord
->getUser( $audience, $user );
660 return $user ?
$user->getId() : 0;
664 * Fetch revision's user id without regard for the current user's permissions
667 * @deprecated since 1.25, use getUser( Revision::RAW )
669 public function getRawUser() {
670 wfDeprecated( __METHOD__
, '1.25' );
671 return $this->getUser( self
::RAW
);
675 * Fetch revision's username if it's available to the specified audience.
676 * If the specified audience does not have access to the username, an
677 * empty string will be returned.
679 * @param int $audience One of:
680 * Revision::FOR_PUBLIC to be displayed to all users
681 * Revision::FOR_THIS_USER to be displayed to the given user
682 * Revision::RAW get the text regardless of permissions
683 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
684 * to the $audience parameter
687 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
690 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
694 $user = $this->mRecord
->getUser( $audience, $user );
695 return $user ?
$user->getName() : '';
699 * Fetch revision's username without regard for view restrictions
702 * @deprecated since 1.25, use getUserText( Revision::RAW )
704 public function getRawUserText() {
705 wfDeprecated( __METHOD__
, '1.25' );
706 return $this->getUserText( self
::RAW
);
710 * Fetch revision comment if it's available to the specified audience.
711 * If the specified audience does not have access to the comment, an
712 * empty string will be returned.
714 * @param int $audience One of:
715 * Revision::FOR_PUBLIC to be displayed to all users
716 * Revision::FOR_THIS_USER to be displayed to the given user
717 * Revision::RAW get the text regardless of permissions
718 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
719 * to the $audience parameter
722 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
725 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
729 $comment = $this->mRecord
->getComment( $audience, $user );
730 return $comment === null ?
null : $comment->text
;
734 * Fetch revision comment without regard for the current user's permissions
737 * @deprecated since 1.25, use getComment( Revision::RAW )
739 public function getRawComment() {
740 wfDeprecated( __METHOD__
, '1.25' );
741 return $this->getComment( self
::RAW
);
747 public function isMinor() {
748 return $this->mRecord
->isMinor();
752 * @return int Rcid of the unpatrolled row, zero if there isn't one
754 public function isUnpatrolled() {
755 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
759 * Get the RC object belonging to the current revision, if there's one
761 * @param int $flags (optional) $flags include:
762 * Revision::READ_LATEST : Select the data from the master
765 * @return RecentChange|null
767 public function getRecentChange( $flags = 0 ) {
768 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
772 * @param int $field One of DELETED_* bitfield constants
776 public function isDeleted( $field ) {
777 return $this->mRecord
->isDeleted( $field );
781 * Get the deletion bitfield of the revision
785 public function getVisibility() {
786 return $this->mRecord
->getVisibility();
790 * Fetch revision content if it's available to the specified audience.
791 * If the specified audience does not have the ability to view this
792 * revision, or the content could not be loaded, null will be returned.
794 * @param int $audience One of:
795 * Revision::FOR_PUBLIC to be displayed to all users
796 * Revision::FOR_THIS_USER to be displayed to $user
797 * Revision::RAW get the text regardless of permissions
798 * @param User $user User object to check for, only if FOR_THIS_USER is passed
799 * to the $audience parameter
801 * @return Content|null
803 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
806 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
811 return $this->mRecord
->getContent( 'main', $audience, $user );
813 catch ( RevisionAccessException
$e ) {
819 * Get original serialized data (without checking view restrictions)
822 * @deprecated since 1.31, use BlobStore::getBlob instead.
826 public function getSerializedData() {
827 $slot = $this->getMainSlotRaw();
828 return $slot->getContent()->serialize();
832 * Returns the content model for the main slot of this revision.
834 * If no content model was stored in the database, the default content model for the title is
835 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
836 * is used as a last resort.
838 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
840 * @return string The content model id associated with this revision,
841 * see the CONTENT_MODEL_XXX constants.
843 public function getContentModel() {
844 return $this->getMainSlotRaw()->getModel();
848 * Returns the content format for the main slot of this revision.
850 * If no content format was stored in the database, the default format for this
851 * revision's content model is returned.
853 * @todo: drop this, the format is irrelevant to the revision!
855 * @return string The content format id associated with this revision,
856 * see the CONTENT_FORMAT_XXX constants.
858 public function getContentFormat() {
859 $format = $this->getMainSlotRaw()->getFormat();
861 if ( $format === null ) {
862 // if no format was stored along with the blob, fall back to default format
863 $format = $this->getContentHandler()->getDefaultFormat();
870 * Returns the content handler appropriate for this revision's content model.
872 * @throws MWException
873 * @return ContentHandler
875 public function getContentHandler() {
876 return ContentHandler
::getForModelID( $this->getContentModel() );
882 public function getTimestamp() {
883 return $this->mRecord
->getTimestamp();
889 public function isCurrent() {
890 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
894 * Get previous revision for this title
896 * @return Revision|null
898 public function getPrevious() {
899 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $this->getTitle() );
902 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
906 * Get next revision for this title
908 * @return Revision|null
910 public function getNext() {
911 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $this->getTitle() );
914 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
918 * Get revision text associated with an old or archive row
920 * Both the flags and the text field must be included. Including the old_id
921 * field will activate cache usage as long as the $wiki parameter is not set.
923 * @param stdClass $row The text data
924 * @param string $prefix Table prefix (default 'old_')
925 * @param string|bool $wiki The name of the wiki to load the revision text from
926 * (same as the the wiki $row was loaded from) or false to indicate the local
927 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
928 * identifier as understood by the LoadBalancer class.
929 * @return string|false Text the text requested or false on failure
931 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
932 $textField = $prefix . 'text';
933 $flagsField = $prefix . 'flags';
935 if ( isset( $row->$flagsField ) ) {
936 $flags = explode( ',', $row->$flagsField );
941 if ( isset( $row->$textField ) ) {
942 $text = $row->$textField;
947 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
949 return self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
953 * If $wgCompressRevisions is enabled, we will compress data.
954 * The input string is modified in place.
955 * Return value is the flags field: contains 'gzip' if the
956 * data is compressed, and 'utf-8' if we're saving in UTF-8
959 * @param mixed &$text Reference to a text
962 public static function compressRevisionText( &$text ) {
963 return self
::getBlobStore()->compressData( $text );
967 * Re-converts revision text according to it's flags.
969 * @param mixed $text Reference to a text
970 * @param array $flags Compression flags
971 * @return string|bool Decompressed text, or false on failure
973 public static function decompressRevisionText( $text, $flags ) {
974 return self
::getBlobStore()->decompressData( $text, $flags );
978 * Insert a new revision into the database, returning the new revision ID
979 * number on success and dies horribly on failure.
981 * @param IDatabase $dbw (master connection)
982 * @throws MWException
983 * @return int The revision ID
985 public function insertOn( $dbw ) {
988 // Note that $this->mRecord->getId() will typically return null here, but not always,
989 // e.g. not when restoring a revision.
991 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
992 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
993 $this->mRecord
->setUser( $wgUser );
995 throw new MWException( 'Cannot insert revision with no associated user.' );
999 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1001 $this->mRecord
= $rec;
1003 // Avoid PHP 7.1 warning of passing $this by reference
1005 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1006 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1008 return $rec->getId();
1012 * Get the base 36 SHA-1 value for a string of text
1013 * @param string $text
1016 public static function base36Sha1( $text ) {
1017 return SlotRecord
::base36Sha1( $text );
1021 * Create a new null-revision for insertion into a page's
1022 * history. This will not re-save the text, but simply refer
1023 * to the text from the previous version.
1025 * Such revisions can for instance identify page rename
1026 * operations and other such meta-modifications.
1028 * @param IDatabase $dbw
1029 * @param int $pageId ID number of the page to read from
1030 * @param string $summary Revision's summary
1031 * @param bool $minor Whether the revision should be considered as minor
1032 * @param User|null $user User object to use or null for $wgUser
1033 * @return Revision|null Revision or null on error
1035 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1041 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1043 $title = Title
::newFromID( $pageId );
1044 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1046 return new Revision( $rec );
1050 * Determine if the current user is allowed to view a particular
1051 * field of this revision, if it's marked as deleted.
1053 * @param int $field One of self::DELETED_TEXT,
1054 * self::DELETED_COMMENT,
1055 * self::DELETED_USER
1056 * @param User|null $user User object to check, or null to use $wgUser
1059 public function userCan( $field, User
$user = null ) {
1060 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1064 * Determine if the current user is allowed to view a particular
1065 * field of this revision, if it's marked as deleted. This is used
1066 * by various classes to avoid duplication.
1068 * @param int $bitfield Current field
1069 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1070 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1071 * self::DELETED_USER = File::DELETED_USER
1072 * @param User|null $user User object to check, or null to use $wgUser
1073 * @param Title|null $title A Title object to check for per-page restrictions on,
1074 * instead of just plain userrights
1077 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1086 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1090 * Get rev_timestamp from rev_id, without loading the rest of the row
1092 * @param Title $title
1095 * @return string|bool False if not found
1097 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1098 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1102 * Get count of revisions per page...not very efficient
1104 * @param IDatabase $db
1105 * @param int $id Page id
1108 static function countByPageId( $db, $id ) {
1109 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1113 * Get count of revisions per page...not very efficient
1115 * @param IDatabase $db
1116 * @param Title $title
1119 static function countByTitle( $db, $title ) {
1120 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1124 * Check if no edits were made by other users since
1125 * the time a user started editing the page. Limit to
1126 * 50 revisions for the sake of performance.
1129 * @deprecated since 1.24
1131 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1132 * Database object or a database identifier usable with wfGetDB.
1133 * @param int $pageId The ID of the page in question
1134 * @param int $userId The ID of the user in question
1135 * @param string $since Look at edits since this time
1137 * @return bool True if the given user was the only one to edit since the given timestamp
1139 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1140 if ( is_int( $db ) ) {
1141 $db = wfGetDB( $db );
1144 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1148 * Load a revision based on a known page ID and current revision ID from the DB
1150 * This method allows for the use of caching, though accessing anything that normally
1151 * requires permission checks (aside from the text) will trigger a small DB lookup.
1152 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1154 * @param IDatabase $db ignored!
1155 * @param int|Title $pageIdOrTitle Page ID or Title object
1156 * @param int $revId Known current revision of this page. Determined automatically if not given.
1157 * @return Revision|bool Returns false if missing
1160 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1161 $title = $pageIdOrTitle instanceof Title
1163 : Title
::newFromID( $pageIdOrTitle );
1165 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1166 return $record ?
new Revision( $record ) : false;