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 $wiki The ID of the target wiki database. Use false for the local wiki.
70 * @return SqlBlobStore
72 protected static function getBlobStore( $wiki = false ) {
73 $store = MediaWikiServices
::getInstance()
74 ->getBlobStoreFactory()
75 ->newSqlBlobStore( $wiki );
77 if ( !$store instanceof SqlBlobStore
) {
78 throw new RuntimeException(
79 'The backwards compatibility code in Revision currently requires the BlobStore '
80 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
88 * Load a page revision from a given revision ID number.
89 * Returns null if no such revision can be found.
92 * Revision::READ_LATEST : Select the data from the master
93 * Revision::READ_LOCKING : Select & lock the data from the master
96 * @param int $flags (optional)
97 * @return Revision|null
99 public static function newFromId( $id, $flags = 0 ) {
100 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags );
101 return $rec === null ?
null : new Revision( $rec, $flags );
105 * Load either the current, or a specified, revision
106 * that's attached to a given link target. If not attached
107 * to that link target, will return null.
110 * Revision::READ_LATEST : Select the data from the master
111 * Revision::READ_LOCKING : Select & lock the data from the master
113 * @param LinkTarget $linkTarget
114 * @param int $id (optional)
115 * @param int $flags Bitfield (optional)
116 * @return Revision|null
118 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
119 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $id, $flags );
120 return $rec === null ?
null : new Revision( $rec, $flags );
124 * Load either the current, or a specified, revision
125 * that's attached to a given page ID.
126 * Returns null if no such revision can be found.
129 * Revision::READ_LATEST : Select the data from the master (since 1.20)
130 * Revision::READ_LOCKING : Select & lock the data from the master
133 * @param int $revId (optional)
134 * @param int $flags Bitfield (optional)
135 * @return Revision|null
137 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
138 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
139 return $rec === null ?
null : new Revision( $rec, $flags );
143 * Make a fake revision object from an archive table row. This is queried
144 * for permissions or even inserted (as in Special:Undelete)
147 * @param array $overrides
149 * @throws MWException
152 public static function newFromArchiveRow( $row, $overrides = [] ) {
154 * MCR Migration: https://phabricator.wikimedia.org/T183564
155 * This method used to overwrite attributes, then passed to Revision::__construct
156 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
157 * So do a conversion here.
159 if ( array_key_exists( 'page', $overrides ) ) {
160 $overrides['page_id'] = $overrides['page'];
161 unset( $overrides['page'] );
165 * We require a Title for both the Revision object and the RevisionRecord.
166 * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
167 * to fetch a title in order pass it into the Revision object.
170 if ( isset( $overrides['title'] ) ) {
171 if ( !( $overrides['title'] instanceof Title
) ) {
172 throw new MWException( 'title field override must contain a Title object.' );
175 $title = $overrides['title'];
177 if ( $title !== null ) {
178 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
179 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
181 throw new InvalidArgumentException(
182 'A Title or ar_namespace and ar_title must be given'
187 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
188 return new Revision( $rec, self
::READ_NORMAL
, $title );
194 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
195 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
196 * a MutableRevisionRecord should be constructed directly.
197 * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
198 * but should be avoided.
200 * @param object|array $row
203 public static function newFromRow( $row ) {
204 if ( is_array( $row ) ) {
205 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
207 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
210 return new Revision( $rec );
214 * Load a page revision from a given revision ID number.
215 * Returns null if no such revision can be found.
217 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
219 * @param IDatabase $db
221 * @return Revision|null
223 public static function loadFromId( $db, $id ) {
224 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
225 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
226 return $rec === null ?
null : new Revision( $rec );
230 * Load either the current, or a specified, revision
231 * that's attached to a given page. If not attached
232 * to that page, will return null.
234 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
236 * @param IDatabase $db
239 * @return Revision|null
241 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
242 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
243 return $rec === null ?
null : new Revision( $rec );
247 * Load either the current, or a specified, revision
248 * that's attached to a given page. If not attached
249 * to that page, will return null.
251 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
253 * @param IDatabase $db
254 * @param Title $title
256 * @return Revision|null
258 public static function loadFromTitle( $db, $title, $id = 0 ) {
259 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
260 return $rec === null ?
null : new Revision( $rec );
264 * Load the revision for the given title with the given timestamp.
265 * WARNING: Timestamps may in some circumstances not be unique,
266 * so this isn't the best key to use.
268 * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
269 * or RevisionStore::loadRevisionFromTimestamp() instead.
271 * @param IDatabase $db
272 * @param Title $title
273 * @param string $timestamp
274 * @return Revision|null
276 public static function loadFromTimestamp( $db, $title, $timestamp ) {
277 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
278 return $rec === null ?
null : new Revision( $rec );
282 * Return a wrapper for a series of database rows to
283 * fetch all of a given page's revisions in turn.
284 * Each row can be fed to the constructor to get objects.
286 * @param LinkTarget $title
287 * @return ResultWrapper
288 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
290 public static function fetchRevision( LinkTarget
$title ) {
291 wfDeprecated( __METHOD__
, '1.31' );
292 return new FakeResultWrapper( [] );
296 * Return the value of a select() JOIN conds array for the user table.
297 * This will get user table rows for logged-in users.
299 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
302 public static function userJoinCond() {
303 wfDeprecated( __METHOD__
, '1.31' );
304 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
308 * Return the value of a select() page conds array for the page table.
309 * This will assure that the revision(s) are not orphaned from live pages.
311 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
314 public static function pageJoinCond() {
315 wfDeprecated( __METHOD__
, '1.31' );
316 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
320 * Return the list of revision fields that should be selected to create
322 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
325 public static function selectFields() {
326 global $wgContentHandlerUseDB;
328 wfDeprecated( __METHOD__
, '1.31' );
344 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
346 if ( $wgContentHandlerUseDB ) {
347 $fields[] = 'rev_content_format';
348 $fields[] = 'rev_content_model';
355 * Return the list of revision fields that should be selected to create
356 * a new revision from an archive row.
357 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
360 public static function selectArchiveFields() {
361 global $wgContentHandlerUseDB;
363 wfDeprecated( __METHOD__
, '1.31' );
381 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
383 if ( $wgContentHandlerUseDB ) {
384 $fields[] = 'ar_content_format';
385 $fields[] = 'ar_content_model';
391 * Return the list of text fields that should be selected to read the
393 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
396 public static function selectTextFields() {
397 wfDeprecated( __METHOD__
, '1.31' );
405 * Return the list of page fields that should be selected from page table
406 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
409 public static function selectPageFields() {
410 wfDeprecated( __METHOD__
, '1.31' );
422 * Return the list of user fields that should be selected from user table
423 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
426 public static function selectUserFields() {
427 wfDeprecated( __METHOD__
, '1.31' );
428 return [ 'user_name' ];
432 * Return the tables, fields, and join conditions to be selected to create
433 * a new revision object.
435 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
436 * @param array $options Any combination of the following strings
437 * - 'page': Join with the page table, and select fields to identify the page
438 * - 'user': Join with the user table, and select the user name
439 * - 'text': Join with the text table, and select fields to load page text
440 * @return array With three keys:
441 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
442 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
443 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
445 public static function getQueryInfo( $options = [] ) {
446 return self
::getRevisionStore()->getQueryInfo( $options );
450 * Return the tables, fields, and join conditions to be selected to create
451 * a new archived revision object.
453 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
454 * @return array With three keys:
455 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
456 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
457 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
459 public static function getArchiveQueryInfo() {
460 return self
::getRevisionStore()->getArchiveQueryInfo();
464 * Do a batched query to get the parent revision lengths
466 * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
468 * @param IDatabase $db
469 * @param array $revIds
472 public static function getParentLengths( $db, array $revIds ) {
473 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
477 * @param object|array|RevisionRecord $row Either a database row or an array
478 * @param int $queryFlags
479 * @param Title|null $title
483 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
486 if ( $row instanceof RevisionRecord
) {
487 $this->mRecord
= $row;
488 } elseif ( is_array( $row ) ) {
489 // If no user is specified, fall back to using the global user object, to stay
490 // compatible with pre-1.31 behavior.
491 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
492 $row['user'] = $wgUser;
495 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
500 } elseif ( is_object( $row ) ) {
501 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
507 throw new InvalidArgumentException(
508 '$row must be a row object, an associative array, or a RevisionRecord'
514 * @return RevisionRecord
516 public function getRevisionRecord() {
517 return $this->mRecord
;
525 public function getId() {
526 return $this->mRecord
->getId();
530 * Set the revision ID
532 * This should only be used for proposed revisions that turn out to be null edits.
534 * @note Only supported on Revisions that were constructed based on associative arrays,
535 * since they are mutable.
538 * @param int|string $id
539 * @throws MWException
541 public function setId( $id ) {
542 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
543 $this->mRecord
->setId( intval( $id ) );
545 throw new MWException( __METHOD__
. ' is not supported on this instance' );
550 * Set the user ID/name
552 * This should only be used for proposed revisions that turn out to be null edits
554 * @note Only supported on Revisions that were constructed based on associative arrays,
555 * since they are mutable.
558 * @deprecated since 1.31, please reuse old Revision object
559 * @param int $id User ID
560 * @param string $name User name
561 * @throws MWException
563 public function setUserIdAndName( $id, $name ) {
564 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
565 $user = new UserIdentityValue( intval( $id ), $name );
566 $this->mRecord
->setUser( $user );
568 throw new MWException( __METHOD__
. ' is not supported on this instance' );
575 private function getMainSlotRaw() {
576 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
580 * Get the ID of the row of the text table that contains the content of the
581 * revision's main slot, if that content is stored in the text table.
583 * If the content is stored elsewhere, this returns null.
585 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
586 * get that actual address that can be used with BlobStore::getBlob(); or use
587 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
591 public function getTextId() {
592 $slot = $this->getMainSlotRaw();
593 return $slot->hasAddress()
594 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
599 * Get parent revision ID (the original previous page revision)
601 * @return int|null The ID of the parent revision. 0 indicates that there is no
602 * parent revision. Null indicates that the parent revision is not known.
604 public function getParentId() {
605 return $this->mRecord
->getParentId();
609 * Returns the length of the text in this revision, or null if unknown.
613 public function getSize() {
614 return $this->mRecord
->getSize();
618 * Returns the base36 sha1 of the content in this revision, or null if unknown.
622 public function getSha1() {
623 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
624 return $this->mRecord
->getSha1();
628 * Returns the title of the page associated with this entry.
629 * Since 1.31, this will never return null.
631 * Will do a query, when title is not set and id is given.
635 public function getTitle() {
636 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
637 return Title
::newFromLinkTarget( $linkTarget );
641 * Set the title of the revision
643 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
645 * @param Title $title
647 public function setTitle( $title ) {
648 if ( !$title->equals( $this->getTitle() ) ) {
649 throw new InvalidArgumentException(
650 $title->getPrefixedText()
651 . ' is not the same as '
652 . $this->mRecord
->getPageAsLinkTarget()->__toString()
662 public function getPage() {
663 return $this->mRecord
->getPageId();
667 * Fetch revision's user id if it's available to the specified audience.
668 * If the specified audience does not have access to it, zero will be
671 * @param int $audience One of:
672 * Revision::FOR_PUBLIC to be displayed to all users
673 * Revision::FOR_THIS_USER to be displayed to the given user
674 * Revision::RAW get the ID regardless of permissions
675 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
676 * to the $audience parameter
679 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
682 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
686 $user = $this->mRecord
->getUser( $audience, $user );
687 return $user ?
$user->getId() : 0;
691 * Fetch revision's user id without regard for the current user's permissions
694 * @deprecated since 1.25, use getUser( Revision::RAW )
696 public function getRawUser() {
697 wfDeprecated( __METHOD__
, '1.25' );
698 return $this->getUser( self
::RAW
);
702 * Fetch revision's username if it's available to the specified audience.
703 * If the specified audience does not have access to the username, an
704 * empty string will be returned.
706 * @param int $audience One of:
707 * Revision::FOR_PUBLIC to be displayed to all users
708 * Revision::FOR_THIS_USER to be displayed to the given user
709 * Revision::RAW get the text regardless of permissions
710 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
711 * to the $audience parameter
714 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
717 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
721 $user = $this->mRecord
->getUser( $audience, $user );
722 return $user ?
$user->getName() : '';
726 * Fetch revision's username without regard for view restrictions
729 * @deprecated since 1.25, use getUserText( Revision::RAW )
731 public function getRawUserText() {
732 wfDeprecated( __METHOD__
, '1.25' );
733 return $this->getUserText( self
::RAW
);
737 * Fetch revision comment if it's available to the specified audience.
738 * If the specified audience does not have access to the comment, an
739 * empty string will be returned.
741 * @param int $audience One of:
742 * Revision::FOR_PUBLIC to be displayed to all users
743 * Revision::FOR_THIS_USER to be displayed to the given user
744 * Revision::RAW get the text regardless of permissions
745 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
746 * to the $audience parameter
749 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
752 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
756 $comment = $this->mRecord
->getComment( $audience, $user );
757 return $comment === null ?
null : $comment->text
;
761 * Fetch revision comment without regard for the current user's permissions
764 * @deprecated since 1.25, use getComment( Revision::RAW )
766 public function getRawComment() {
767 wfDeprecated( __METHOD__
, '1.25' );
768 return $this->getComment( self
::RAW
);
774 public function isMinor() {
775 return $this->mRecord
->isMinor();
779 * @return int Rcid of the unpatrolled row, zero if there isn't one
781 public function isUnpatrolled() {
782 return self
::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord
);
786 * Get the RC object belonging to the current revision, if there's one
788 * @param int $flags (optional) $flags include:
789 * Revision::READ_LATEST : Select the data from the master
792 * @return RecentChange|null
794 public function getRecentChange( $flags = 0 ) {
795 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
799 * @param int $field One of DELETED_* bitfield constants
803 public function isDeleted( $field ) {
804 return $this->mRecord
->isDeleted( $field );
808 * Get the deletion bitfield of the revision
812 public function getVisibility() {
813 return $this->mRecord
->getVisibility();
817 * Fetch revision content if it's available to the specified audience.
818 * If the specified audience does not have the ability to view this
819 * revision, or the content could not be loaded, null will be returned.
821 * @param int $audience One of:
822 * Revision::FOR_PUBLIC to be displayed to all users
823 * Revision::FOR_THIS_USER to be displayed to $user
824 * Revision::RAW get the text regardless of permissions
825 * @param User $user User object to check for, only if FOR_THIS_USER is passed
826 * to the $audience parameter
828 * @return Content|null
830 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
833 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
838 return $this->mRecord
->getContent( 'main', $audience, $user );
840 catch ( RevisionAccessException
$e ) {
846 * Get original serialized data (without checking view restrictions)
849 * @deprecated since 1.31, use BlobStore::getBlob instead.
853 public function getSerializedData() {
854 $slot = $this->getMainSlotRaw();
855 return $slot->getContent()->serialize();
859 * Returns the content model for the main slot of this revision.
861 * If no content model was stored in the database, the default content model for the title is
862 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
863 * is used as a last resort.
865 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
867 * @return string The content model id associated with this revision,
868 * see the CONTENT_MODEL_XXX constants.
870 public function getContentModel() {
871 return $this->getMainSlotRaw()->getModel();
875 * Returns the content format for the main slot of this revision.
877 * If no content format was stored in the database, the default format for this
878 * revision's content model is returned.
880 * @todo: drop this, the format is irrelevant to the revision!
882 * @return string The content format id associated with this revision,
883 * see the CONTENT_FORMAT_XXX constants.
885 public function getContentFormat() {
886 $format = $this->getMainSlotRaw()->getFormat();
888 if ( $format === null ) {
889 // if no format was stored along with the blob, fall back to default format
890 $format = $this->getContentHandler()->getDefaultFormat();
897 * Returns the content handler appropriate for this revision's content model.
899 * @throws MWException
900 * @return ContentHandler
902 public function getContentHandler() {
903 return ContentHandler
::getForModelID( $this->getContentModel() );
909 public function getTimestamp() {
910 return $this->mRecord
->getTimestamp();
916 public function isCurrent() {
917 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
921 * Get previous revision for this title
923 * @return Revision|null
925 public function getPrevious() {
926 $title = $this->getTitle();
927 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $title );
928 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
932 * Get next revision for this title
934 * @return Revision|null
936 public function getNext() {
937 $title = $this->getTitle();
938 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $title );
939 return $rec === null ?
null : new Revision( $rec, self
::READ_NORMAL
, $title );
943 * Get revision text associated with an old or archive row
945 * Both the flags and the text field must be included. Including the old_id
946 * field will activate cache usage as long as the $wiki parameter is not set.
948 * @param stdClass $row The text data
949 * @param string $prefix Table prefix (default 'old_')
950 * @param string|bool $wiki The name of the wiki to load the revision text from
951 * (same as the the wiki $row was loaded from) or false to indicate the local
952 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
953 * identifier as understood by the LoadBalancer class.
954 * @return string|false Text the text requested or false on failure
956 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
957 $textField = $prefix . 'text';
958 $flagsField = $prefix . 'flags';
960 if ( isset( $row->$flagsField ) ) {
961 $flags = explode( ',', $row->$flagsField );
966 if ( isset( $row->$textField ) ) {
967 $text = $row->$textField;
972 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
974 return self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
978 * If $wgCompressRevisions is enabled, we will compress data.
979 * The input string is modified in place.
980 * Return value is the flags field: contains 'gzip' if the
981 * data is compressed, and 'utf-8' if we're saving in UTF-8
984 * @param mixed &$text Reference to a text
987 public static function compressRevisionText( &$text ) {
988 return self
::getBlobStore()->compressData( $text );
992 * Re-converts revision text according to it's flags.
994 * @param mixed $text Reference to a text
995 * @param array $flags Compression flags
996 * @return string|bool Decompressed text, or false on failure
998 public static function decompressRevisionText( $text, $flags ) {
999 return self
::getBlobStore()->decompressData( $text, $flags );
1003 * Insert a new revision into the database, returning the new revision ID
1004 * number on success and dies horribly on failure.
1006 * @param IDatabase $dbw (master connection)
1007 * @throws MWException
1008 * @return int The revision ID
1010 public function insertOn( $dbw ) {
1013 // Note that $this->mRecord->getId() will typically return null here, but not always,
1014 // e.g. not when restoring a revision.
1016 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
1017 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
1018 $this->mRecord
->setUser( $wgUser );
1020 throw new MWException( 'Cannot insert revision with no associated user.' );
1024 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1026 $this->mRecord
= $rec;
1028 // Avoid PHP 7.1 warning of passing $this by reference
1030 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1031 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1033 return $rec->getId();
1037 * Get the base 36 SHA-1 value for a string of text
1038 * @param string $text
1041 public static function base36Sha1( $text ) {
1042 return SlotRecord
::base36Sha1( $text );
1046 * Create a new null-revision for insertion into a page's
1047 * history. This will not re-save the text, but simply refer
1048 * to the text from the previous version.
1050 * Such revisions can for instance identify page rename
1051 * operations and other such meta-modifications.
1053 * @param IDatabase $dbw
1054 * @param int $pageId ID number of the page to read from
1055 * @param string $summary Revision's summary
1056 * @param bool $minor Whether the revision should be considered as minor
1057 * @param User|null $user User object to use or null for $wgUser
1058 * @return Revision|null Revision or null on error
1060 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1066 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1068 $title = Title
::newFromID( $pageId );
1069 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1071 return new Revision( $rec );
1075 * Determine if the current user is allowed to view a particular
1076 * field of this revision, if it's marked as deleted.
1078 * @param int $field One of self::DELETED_TEXT,
1079 * self::DELETED_COMMENT,
1080 * self::DELETED_USER
1081 * @param User|null $user User object to check, or null to use $wgUser
1084 public function userCan( $field, User
$user = null ) {
1085 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1089 * Determine if the current user is allowed to view a particular
1090 * field of this revision, if it's marked as deleted. This is used
1091 * by various classes to avoid duplication.
1093 * @param int $bitfield Current field
1094 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1095 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1096 * self::DELETED_USER = File::DELETED_USER
1097 * @param User|null $user User object to check, or null to use $wgUser
1098 * @param Title|null $title A Title object to check for per-page restrictions on,
1099 * instead of just plain userrights
1102 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1111 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1115 * Get rev_timestamp from rev_id, without loading the rest of the row
1117 * @param Title $title
1120 * @return string|bool False if not found
1122 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1123 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1127 * Get count of revisions per page...not very efficient
1129 * @param IDatabase $db
1130 * @param int $id Page id
1133 static function countByPageId( $db, $id ) {
1134 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1138 * Get count of revisions per page...not very efficient
1140 * @param IDatabase $db
1141 * @param Title $title
1144 static function countByTitle( $db, $title ) {
1145 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1149 * Check if no edits were made by other users since
1150 * the time a user started editing the page. Limit to
1151 * 50 revisions for the sake of performance.
1154 * @deprecated since 1.24
1156 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1157 * Database object or a database identifier usable with wfGetDB.
1158 * @param int $pageId The ID of the page in question
1159 * @param int $userId The ID of the user in question
1160 * @param string $since Look at edits since this time
1162 * @return bool True if the given user was the only one to edit since the given timestamp
1164 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1165 if ( is_int( $db ) ) {
1166 $db = wfGetDB( $db );
1169 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1173 * Load a revision based on a known page ID and current revision ID from the DB
1175 * This method allows for the use of caching, though accessing anything that normally
1176 * requires permission checks (aside from the text) will trigger a small DB lookup.
1177 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1179 * @param IDatabase $db ignored!
1180 * @param int|Title $pageIdOrTitle Page ID or Title object
1181 * @param int $revId Known current revision of this page. Determined automatically if not given.
1182 * @return Revision|bool Returns false if missing
1185 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1186 $title = $pageIdOrTitle instanceof Title
1188 : Title
::newFromID( $pageIdOrTitle );
1194 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1195 return $record ?
new Revision( $record ) : false;