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) If known you can pass the Title in here.
98 * Passing no Title may result in another DB query if there are recent writes.
99 * @return Revision|null
101 public static function newFromId( $id, $flags = 0, Title
$title = null ) {
103 * MCR RevisionStore Compat
105 * If the title is not passed in as a param (already known) then select it here.
107 * Do the selection with MASTER if $flags includes READ_LATEST or recent changes
108 * have happened on our load balancer.
110 * If we select the title here and pass it down it will results in fewer queries
111 * further down the stack.
115 $flags & self
::READ_LATEST ||
116 wfGetLB()->hasOrMadeRecentMasterChanges()
118 $dbr = wfGetDB( DB_MASTER
);
120 $dbr = wfGetDB( DB_REPLICA
);
122 $row = $dbr->selectRow(
123 [ 'revision', 'page' ],
135 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
138 $title = Title
::newFromRow( $row );
140 wfGetLB()->reuseConnection( $dbr );
143 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags, $title );
144 return $rec === null ?
null : new Revision( $rec, $flags, $title );
148 * Load either the current, or a specified, revision
149 * that's attached to a given link target. If not attached
150 * to that link target, will return null.
153 * Revision::READ_LATEST : Select the data from the master
154 * Revision::READ_LOCKING : Select & lock the data from the master
156 * @param LinkTarget $linkTarget
157 * @param int $id (optional)
158 * @param int $flags Bitfield (optional)
159 * @return Revision|null
161 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
162 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $id, $flags );
163 return $rec === null ?
null : new Revision( $rec, $flags );
167 * Load either the current, or a specified, revision
168 * that's attached to a given page ID.
169 * Returns null if no such revision can be found.
172 * Revision::READ_LATEST : Select the data from the master (since 1.20)
173 * Revision::READ_LOCKING : Select & lock the data from the master
176 * @param int $revId (optional)
177 * @param int $flags Bitfield (optional)
178 * @return Revision|null
180 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
181 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
182 return $rec === null ?
null : new Revision( $rec, $flags );
186 * Make a fake revision object from an archive table row. This is queried
187 * for permissions or even inserted (as in Special:Undelete)
190 * @param array $overrides
191 * @param Title $title (optional)
193 * @throws MWException
196 public static function newFromArchiveRow( $row, $overrides = [], Title
$title = null ) {
198 * MCR Migration: https://phabricator.wikimedia.org/T183564
199 * This method used to overwrite attributes, then passed to Revision::__construct
200 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
201 * So do a conversion here.
203 if ( array_key_exists( 'page', $overrides ) ) {
204 $overrides['page_id'] = $overrides['page'];
205 unset( $overrides['page'] );
208 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
209 return new Revision( $rec, self
::READ_NORMAL
, $title );
215 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
216 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
217 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
218 * can be used as a temporary replacement, but should be avoided.
220 * @param object|array $row
223 public static function newFromRow( $row ) {
224 if ( is_array( $row ) ) {
225 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
227 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
230 return new Revision( $rec );
234 * Load a page revision from a given revision ID number.
235 * Returns null if no such revision can be found.
237 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
239 * @param IDatabase $db
241 * @return Revision|null
243 public static function loadFromId( $db, $id ) {
244 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
245 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
246 return $rec === null ?
null : new Revision( $rec );
250 * Load either the current, or a specified, revision
251 * that's attached to a given page. If not attached
252 * to that page, will return null.
254 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
256 * @param IDatabase $db
259 * @return Revision|null
261 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
262 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
263 return $rec === null ?
null : new Revision( $rec );
267 * Load either the current, or a specified, revision
268 * that's attached to a given page. If not attached
269 * to that page, will return null.
271 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
273 * @param IDatabase $db
274 * @param Title $title
276 * @return Revision|null
278 public static function loadFromTitle( $db, $title, $id = 0 ) {
279 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
280 return $rec === null ?
null : new Revision( $rec );
284 * Load the revision for the given title with the given timestamp.
285 * WARNING: Timestamps may in some circumstances not be unique,
286 * so this isn't the best key to use.
288 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
290 * @param IDatabase $db
291 * @param Title $title
292 * @param string $timestamp
293 * @return Revision|null
295 public static function loadFromTimestamp( $db, $title, $timestamp ) {
296 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
297 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
298 return $rec === null ?
null : new Revision( $rec );
302 * Return a wrapper for a series of database rows to
303 * fetch all of a given page's revisions in turn.
304 * Each row can be fed to the constructor to get objects.
306 * @param LinkTarget $title
307 * @return ResultWrapper
308 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
310 public static function fetchRevision( LinkTarget
$title ) {
311 wfDeprecated( __METHOD__
, '1.31' );
312 return new FakeResultWrapper( [] );
316 * Return the value of a select() JOIN conds array for the user table.
317 * This will get user table rows for logged-in users.
319 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
322 public static function userJoinCond() {
323 wfDeprecated( __METHOD__
, '1.31' );
324 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
328 * Return the value of a select() page conds array for the page table.
329 * This will assure that the revision(s) are not orphaned from live pages.
331 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
334 public static function pageJoinCond() {
335 wfDeprecated( __METHOD__
, '1.31' );
336 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
340 * Return the list of revision fields that should be selected to create
342 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
345 public static function selectFields() {
346 global $wgContentHandlerUseDB;
348 wfDeprecated( __METHOD__
, '1.31' );
364 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
366 if ( $wgContentHandlerUseDB ) {
367 $fields[] = 'rev_content_format';
368 $fields[] = 'rev_content_model';
375 * Return the list of revision fields that should be selected to create
376 * a new revision from an archive row.
377 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
380 public static function selectArchiveFields() {
381 global $wgContentHandlerUseDB;
383 wfDeprecated( __METHOD__
, '1.31' );
401 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
403 if ( $wgContentHandlerUseDB ) {
404 $fields[] = 'ar_content_format';
405 $fields[] = 'ar_content_model';
411 * Return the list of text fields that should be selected to read the
413 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
416 public static function selectTextFields() {
417 wfDeprecated( __METHOD__
, '1.31' );
425 * Return the list of page fields that should be selected from page table
426 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
429 public static function selectPageFields() {
430 wfDeprecated( __METHOD__
, '1.31' );
442 * Return the list of user fields that should be selected from user table
443 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
446 public static function selectUserFields() {
447 wfDeprecated( __METHOD__
, '1.31' );
448 return [ 'user_name' ];
452 * Return the tables, fields, and join conditions to be selected to create
453 * a new revision object.
455 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
456 * @param array $options Any combination of the following strings
457 * - 'page': Join with the page table, and select fields to identify the page
458 * - 'user': Join with the user table, and select the user name
459 * - 'text': Join with the text table, and select fields to load page text
460 * @return array With three keys:
461 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
462 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
463 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
465 public static function getQueryInfo( $options = [] ) {
466 return self
::getRevisionStore()->getQueryInfo( $options );
470 * Return the tables, fields, and join conditions to be selected to create
471 * a new archived revision object.
473 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
474 * @return array With three keys:
475 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
476 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
477 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
479 public static function getArchiveQueryInfo() {
480 return self
::getRevisionStore()->getArchiveQueryInfo();
484 * Do a batched query to get the parent revision lengths
485 * @param IDatabase $db
486 * @param array $revIds
489 public static function getParentLengths( $db, array $revIds ) {
490 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
494 * @param object|array|RevisionRecord $row Either a database row or an array
495 * @param int $queryFlags
496 * @param Title|null $title
500 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
503 if ( $row instanceof RevisionRecord
) {
504 $this->mRecord
= $row;
505 } elseif ( is_array( $row ) ) {
506 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
507 $row['user'] = $wgUser;
510 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
515 } elseif ( is_object( $row ) ) {
516 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
522 throw new InvalidArgumentException(
523 '$row must be a row object, an associative array, or a RevisionRecord'
529 * @return RevisionRecord
531 public function getRevisionRecord() {
532 return $this->mRecord
;
540 public function getId() {
541 return $this->mRecord
->getId();
545 * Set the revision ID
547 * This should only be used for proposed revisions that turn out to be null edits.
549 * @note Only supported on Revisions that were constructed based on associative arrays,
550 * since they are mutable.
553 * @param int|string $id
554 * @throws MWException
556 public function setId( $id ) {
557 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
558 $this->mRecord
->setId( intval( $id ) );
560 throw new MWException( __METHOD__
. ' is not supported on this instance' );
565 * Set the user ID/name
567 * This should only be used for proposed revisions that turn out to be null edits
569 * @note Only supported on Revisions that were constructed based on associative arrays,
570 * since they are mutable.
573 * @deprecated since 1.31, please reuse old Revision object
574 * @param int $id User ID
575 * @param string $name User name
576 * @throws MWException
578 public function setUserIdAndName( $id, $name ) {
579 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
580 $user = new UserIdentityValue( intval( $id ), $name );
581 $this->mRecord
->setUser( $user );
583 throw new MWException( __METHOD__
. ' is not supported on this instance' );
590 private function getMainSlotRaw() {
591 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
595 * Get the ID of the row of the text table that contains the content of the
596 * revision's main slot, if that content is stored in the text table.
598 * If the content is stored elsewhere, this returns null.
600 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
601 * get that actual address that can be used with BlobStore::getBlob(); or use
602 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
606 public function getTextId() {
607 $slot = $this->getMainSlotRaw();
608 return $slot->hasAddress()
609 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
614 * Get parent revision ID (the original previous page revision)
616 * @return int|null The ID of the parent revision. 0 indicates that there is no
617 * parent revision. Null indicates that the parent revision is not known.
619 public function getParentId() {
620 return $this->mRecord
->getParentId();
624 * Returns the length of the text in this revision, or null if unknown.
628 public function getSize() {
629 return $this->mRecord
->getSize();
633 * Returns the base36 sha1 of the content in this revision, or null if unknown.
637 public function getSha1() {
638 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
639 return $this->mRecord
->getSha1();
643 * Returns the title of the page associated with this entry.
644 * Since 1.31, this will never return null.
646 * Will do a query, when title is not set and id is given.
650 public function getTitle() {
651 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
652 return Title
::newFromLinkTarget( $linkTarget );
656 * Set the title of the revision
658 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
660 * @param Title $title
662 public function setTitle( $title ) {
663 if ( !$title->equals( $this->getTitle() ) ) {
664 throw new InvalidArgumentException(
665 $title->getPrefixedText()
666 . ' is not the same as '
667 . $this->mRecord
->getPageAsLinkTarget()->__toString()
677 public function getPage() {
678 return $this->mRecord
->getPageId();
682 * Fetch revision's user id if it's available to the specified audience.
683 * If the specified audience does not have access to it, zero will be
686 * @param int $audience One of:
687 * Revision::FOR_PUBLIC to be displayed to all users
688 * Revision::FOR_THIS_USER to be displayed to the given user
689 * Revision::RAW get the ID regardless of permissions
690 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
691 * to the $audience parameter
694 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
697 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
701 $user = $this->mRecord
->getUser( $audience, $user );
702 return $user ?
$user->getId() : 0;
706 * Fetch revision's user id without regard for the current user's permissions
709 * @deprecated since 1.25, use getUser( Revision::RAW )
711 public function getRawUser() {
712 wfDeprecated( __METHOD__
, '1.25' );
713 return $this->getUser( self
::RAW
);
717 * Fetch revision's username if it's available to the specified audience.
718 * If the specified audience does not have access to the username, an
719 * empty string will be returned.
721 * @param int $audience One of:
722 * Revision::FOR_PUBLIC to be displayed to all users
723 * Revision::FOR_THIS_USER to be displayed to the given user
724 * Revision::RAW get the text regardless of permissions
725 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
726 * to the $audience parameter
729 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
732 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
736 $user = $this->mRecord
->getUser( $audience, $user );
737 return $user ?
$user->getName() : '';
741 * Fetch revision's username without regard for view restrictions
744 * @deprecated since 1.25, use getUserText( Revision::RAW )
746 public function getRawUserText() {
747 wfDeprecated( __METHOD__
, '1.25' );
748 return $this->getUserText( self
::RAW
);
752 * Fetch revision comment if it's available to the specified audience.
753 * If the specified audience does not have access to the comment, an
754 * empty string will be returned.
756 * @param int $audience One of:
757 * Revision::FOR_PUBLIC to be displayed to all users
758 * Revision::FOR_THIS_USER to be displayed to the given user
759 * Revision::RAW get the text regardless of permissions
760 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
761 * to the $audience parameter
764 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
767 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
771 $comment = $this->mRecord
->getComment( $audience, $user );
772 return $comment === null ?
null : $comment->text
;
776 * Fetch revision comment without regard for the current user's permissions
779 * @deprecated since 1.25, use getComment( Revision::RAW )
781 public function getRawComment() {
782 wfDeprecated( __METHOD__
, '1.25' );
783 return $this->getComment( self
::RAW
);
789 public function isMinor() {
790 return $this->mRecord
->isMinor();
794 * @return int Rcid of the unpatrolled row, zero if there isn't one
796 public function isUnpatrolled() {
797 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
801 * Get the RC object belonging to the current revision, if there's one
803 * @param int $flags (optional) $flags include:
804 * Revision::READ_LATEST : Select the data from the master
807 * @return RecentChange|null
809 public function getRecentChange( $flags = 0 ) {
810 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
814 * @param int $field One of DELETED_* bitfield constants
818 public function isDeleted( $field ) {
819 return $this->mRecord
->isDeleted( $field );
823 * Get the deletion bitfield of the revision
827 public function getVisibility() {
828 return $this->mRecord
->getVisibility();
832 * Fetch revision content if it's available to the specified audience.
833 * If the specified audience does not have the ability to view this
834 * revision, or the content could not be loaded, null will be returned.
836 * @param int $audience One of:
837 * Revision::FOR_PUBLIC to be displayed to all users
838 * Revision::FOR_THIS_USER to be displayed to $user
839 * Revision::RAW get the text regardless of permissions
840 * @param User $user User object to check for, only if FOR_THIS_USER is passed
841 * to the $audience parameter
843 * @return Content|null
845 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
848 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
853 return $this->mRecord
->getContent( 'main', $audience, $user );
855 catch ( RevisionAccessException
$e ) {
861 * Get original serialized data (without checking view restrictions)
864 * @deprecated since 1.31, use BlobStore::getBlob instead.
868 public function getSerializedData() {
869 $slot = $this->getMainSlotRaw();
870 return $slot->getContent()->serialize();
874 * Returns the content model for the main slot of this revision.
876 * If no content model was stored in the database, the default content model for the title is
877 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
878 * is used as a last resort.
880 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
882 * @return string The content model id associated with this revision,
883 * see the CONTENT_MODEL_XXX constants.
885 public function getContentModel() {
886 return $this->getMainSlotRaw()->getModel();
890 * Returns the content format for the main slot of this revision.
892 * If no content format was stored in the database, the default format for this
893 * revision's content model is returned.
895 * @todo: drop this, the format is irrelevant to the revision!
897 * @return string The content format id associated with this revision,
898 * see the CONTENT_FORMAT_XXX constants.
900 public function getContentFormat() {
901 $format = $this->getMainSlotRaw()->getFormat();
903 if ( $format === null ) {
904 // if no format was stored along with the blob, fall back to default format
905 $format = $this->getContentHandler()->getDefaultFormat();
912 * Returns the content handler appropriate for this revision's content model.
914 * @throws MWException
915 * @return ContentHandler
917 public function getContentHandler() {
918 return ContentHandler
::getForModelID( $this->getContentModel() );
924 public function getTimestamp() {
925 return $this->mRecord
->getTimestamp();
931 public function isCurrent() {
932 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
936 * Get previous revision for this title
938 * @return Revision|null
940 public function getPrevious() {
941 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $this->getTitle() );
944 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
948 * Get next revision for this title
950 * @return Revision|null
952 public function getNext() {
953 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $this->getTitle() );
956 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
960 * Get revision text associated with an old or archive row
962 * Both the flags and the text field must be included. Including the old_id
963 * field will activate cache usage as long as the $wiki parameter is not set.
965 * @param stdClass $row The text data
966 * @param string $prefix Table prefix (default 'old_')
967 * @param string|bool $wiki The name of the wiki to load the revision text from
968 * (same as the the wiki $row was loaded from) or false to indicate the local
969 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
970 * identifier as understood by the LoadBalancer class.
971 * @return string|false Text the text requested or false on failure
973 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
974 $textField = $prefix . 'text';
975 $flagsField = $prefix . 'flags';
977 if ( isset( $row->$flagsField ) ) {
978 $flags = explode( ',', $row->$flagsField );
983 if ( isset( $row->$textField ) ) {
984 $text = $row->$textField;
989 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
991 return self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
995 * If $wgCompressRevisions is enabled, we will compress data.
996 * The input string is modified in place.
997 * Return value is the flags field: contains 'gzip' if the
998 * data is compressed, and 'utf-8' if we're saving in UTF-8
1001 * @param mixed &$text Reference to a text
1004 public static function compressRevisionText( &$text ) {
1005 return self
::getBlobStore()->compressData( $text );
1009 * Re-converts revision text according to it's flags.
1011 * @param mixed $text Reference to a text
1012 * @param array $flags Compression flags
1013 * @return string|bool Decompressed text, or false on failure
1015 public static function decompressRevisionText( $text, $flags ) {
1016 return self
::getBlobStore()->decompressData( $text, $flags );
1020 * Insert a new revision into the database, returning the new revision ID
1021 * number on success and dies horribly on failure.
1023 * @param IDatabase $dbw (master connection)
1024 * @throws MWException
1025 * @return int The revision ID
1027 public function insertOn( $dbw ) {
1030 // Note that $this->mRecord->getId() will typically return null here, but not always,
1031 // e.g. not when restoring a revision.
1033 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
1034 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
1035 $this->mRecord
->setUser( $wgUser );
1037 throw new MWException( 'Cannot insert revision with no associated user.' );
1041 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1043 $this->mRecord
= $rec;
1045 // Avoid PHP 7.1 warning of passing $this by reference
1047 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1048 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1050 return $rec->getId();
1054 * Get the base 36 SHA-1 value for a string of text
1055 * @param string $text
1058 public static function base36Sha1( $text ) {
1059 return SlotRecord
::base36Sha1( $text );
1063 * Create a new null-revision for insertion into a page's
1064 * history. This will not re-save the text, but simply refer
1065 * to the text from the previous version.
1067 * Such revisions can for instance identify page rename
1068 * operations and other such meta-modifications.
1070 * @param IDatabase $dbw
1071 * @param int $pageId ID number of the page to read from
1072 * @param string $summary Revision's summary
1073 * @param bool $minor Whether the revision should be considered as minor
1074 * @param User|null $user User object to use or null for $wgUser
1075 * @return Revision|null Revision or null on error
1077 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1083 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1085 $title = Title
::newFromID( $pageId );
1086 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1088 return new Revision( $rec );
1092 * Determine if the current user is allowed to view a particular
1093 * field of this revision, if it's marked as deleted.
1095 * @param int $field One of self::DELETED_TEXT,
1096 * self::DELETED_COMMENT,
1097 * self::DELETED_USER
1098 * @param User|null $user User object to check, or null to use $wgUser
1101 public function userCan( $field, User
$user = null ) {
1102 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1106 * Determine if the current user is allowed to view a particular
1107 * field of this revision, if it's marked as deleted. This is used
1108 * by various classes to avoid duplication.
1110 * @param int $bitfield Current field
1111 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1112 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1113 * self::DELETED_USER = File::DELETED_USER
1114 * @param User|null $user User object to check, or null to use $wgUser
1115 * @param Title|null $title A Title object to check for per-page restrictions on,
1116 * instead of just plain userrights
1119 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1128 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1132 * Get rev_timestamp from rev_id, without loading the rest of the row
1134 * @param Title $title
1137 * @return string|bool False if not found
1139 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1140 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1144 * Get count of revisions per page...not very efficient
1146 * @param IDatabase $db
1147 * @param int $id Page id
1150 static function countByPageId( $db, $id ) {
1151 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1155 * Get count of revisions per page...not very efficient
1157 * @param IDatabase $db
1158 * @param Title $title
1161 static function countByTitle( $db, $title ) {
1162 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1166 * Check if no edits were made by other users since
1167 * the time a user started editing the page. Limit to
1168 * 50 revisions for the sake of performance.
1171 * @deprecated since 1.24
1173 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1174 * Database object or a database identifier usable with wfGetDB.
1175 * @param int $pageId The ID of the page in question
1176 * @param int $userId The ID of the user in question
1177 * @param string $since Look at edits since this time
1179 * @return bool True if the given user was the only one to edit since the given timestamp
1181 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1182 if ( is_int( $db ) ) {
1183 $db = wfGetDB( $db );
1186 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1190 * Load a revision based on a known page ID and current revision ID from the DB
1192 * This method allows for the use of caching, though accessing anything that normally
1193 * requires permission checks (aside from the text) will trigger a small DB lookup.
1194 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1196 * @param IDatabase $db ignored!
1197 * @param int|Title $pageIdOrTitle Page ID or Title object
1198 * @param int $revId Known current revision of this page. Determined automatically if not given.
1199 * @return Revision|bool Returns false if missing
1202 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1203 $title = $pageIdOrTitle instanceof Title
1205 : Title
::newFromID( $pageIdOrTitle );
1207 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1208 return $record ?
new Revision( $record ) : false;