3 * Representation of a page version.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\Storage\MutableRevisionRecord
;
24 use MediaWiki\Storage\RevisionAccessException
;
25 use MediaWiki\Storage\RevisionRecord
;
26 use MediaWiki\Storage\RevisionStore
;
27 use MediaWiki\Storage\RevisionStoreRecord
;
28 use MediaWiki\Storage\SlotRecord
;
29 use MediaWiki\Storage\SqlBlobStore
;
30 use MediaWiki\User\UserIdentityValue
;
31 use Wikimedia\Rdbms\IDatabase
;
32 use MediaWiki\Linker\LinkTarget
;
33 use MediaWiki\MediaWikiServices
;
34 use Wikimedia\Rdbms\ResultWrapper
;
35 use Wikimedia\Rdbms\FakeResultWrapper
;
38 * @deprecated since 1.31, use RevisionRecord, RevisionStore, and BlobStore instead.
40 class Revision
implements IDBAccessObject
{
42 /** @var RevisionRecord */
45 // Revision deletion constants
46 const DELETED_TEXT
= RevisionRecord
::DELETED_TEXT
;
47 const DELETED_COMMENT
= RevisionRecord
::DELETED_COMMENT
;
48 const DELETED_USER
= RevisionRecord
::DELETED_USER
;
49 const DELETED_RESTRICTED
= RevisionRecord
::DELETED_RESTRICTED
;
50 const SUPPRESSED_USER
= RevisionRecord
::SUPPRESSED_USER
;
51 const SUPPRESSED_ALL
= RevisionRecord
::SUPPRESSED_ALL
;
53 // Audience options for accessors
54 const FOR_PUBLIC
= RevisionRecord
::FOR_PUBLIC
;
55 const FOR_THIS_USER
= RevisionRecord
::FOR_THIS_USER
;
56 const RAW
= RevisionRecord
::RAW
;
58 const TEXT_CACHE_GROUP
= SqlBlobStore
::TEXT_CACHE_GROUP
;
61 * @return RevisionStore
63 protected static function getRevisionStore() {
64 return MediaWikiServices
::getInstance()->getRevisionStore();
68 * @return SqlBlobStore
70 protected static function getBlobStore() {
71 $store = MediaWikiServices
::getInstance()->getBlobStore();
73 if ( !$store instanceof SqlBlobStore
) {
74 throw new RuntimeException(
75 'The backwards compatibility code in Revision currently requires the BlobStore '
76 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
84 * Load a page revision from a given revision ID number.
85 * Returns null if no such revision can be found.
88 * Revision::READ_LATEST : Select the data from the master
89 * Revision::READ_LOCKING : Select & lock the data from the master
92 * @param int $flags (optional)
93 * @param Title $title (optional) If known you can pass the Title in here.
94 * Passing no Title may result in another DB query if there are recent writes.
95 * @return Revision|null
97 public static function newFromId( $id, $flags = 0, Title
$title = null ) {
99 * MCR RevisionStore Compat
101 * If the title is not passed in as a param (already known) then select it here.
103 * Do the selection with MASTER if $flags includes READ_LATEST or recent changes
104 * have happened on our load balancer.
106 * If we select the title here and pass it down it will results in fewer queries
107 * further down the stack.
111 $flags & self
::READ_LATEST ||
112 wfGetLB()->hasOrMadeRecentMasterChanges()
114 $dbr = wfGetDB( DB_MASTER
);
116 $dbr = wfGetDB( DB_REPLICA
);
118 $row = $dbr->selectRow(
119 [ 'revision', 'page' ],
131 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
134 $title = Title
::newFromRow( $row );
136 wfGetLB()->reuseConnection( $dbr );
139 $rec = self
::getRevisionStore()->getRevisionById( $id, $flags, $title );
140 return $rec === null ?
null : new Revision( $rec, $flags, $title );
144 * Load either the current, or a specified, revision
145 * that's attached to a given link target. If not attached
146 * to that link target, will return null.
149 * Revision::READ_LATEST : Select the data from the master
150 * Revision::READ_LOCKING : Select & lock the data from the master
152 * @param LinkTarget $linkTarget
153 * @param int $id (optional)
154 * @param int $flags Bitfield (optional)
155 * @return Revision|null
157 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
158 $rec = self
::getRevisionStore()->getRevisionByTitle( $linkTarget, $id, $flags );
159 return $rec === null ?
null : new Revision( $rec, $flags );
163 * Load either the current, or a specified, revision
164 * that's attached to a given page ID.
165 * Returns null if no such revision can be found.
168 * Revision::READ_LATEST : Select the data from the master (since 1.20)
169 * Revision::READ_LOCKING : Select & lock the data from the master
172 * @param int $revId (optional)
173 * @param int $flags Bitfield (optional)
174 * @return Revision|null
176 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
177 $rec = self
::getRevisionStore()->getRevisionByPageId( $pageId, $revId, $flags );
178 return $rec === null ?
null : new Revision( $rec, $flags );
182 * Make a fake revision object from an archive table row. This is queried
183 * for permissions or even inserted (as in Special:Undelete)
186 * @param array $overrides
187 * @param Title $title (optional)
189 * @throws MWException
192 public static function newFromArchiveRow( $row, $overrides = [], Title
$title = null ) {
194 * MCR Migration: https://phabricator.wikimedia.org/T183564
195 * This method used to overwrite attributes, then passed to Revision::__construct
196 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
197 * So do a conversion here.
199 if ( array_key_exists( 'page', $overrides ) ) {
200 $overrides['page_id'] = $overrides['page'];
201 unset( $overrides['page'] );
204 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
205 return new Revision( $rec, self
::READ_NORMAL
, $title );
211 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
212 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
213 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
214 * can be used as a temporary replacement, but should be avoided.
216 * @param object|array $row
219 public static function newFromRow( $row ) {
220 if ( is_array( $row ) ) {
221 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
223 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
226 return new Revision( $rec );
230 * Load a page revision from a given revision ID number.
231 * Returns null if no such revision can be found.
233 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
235 * @param IDatabase $db
237 * @return Revision|null
239 public static function loadFromId( $db, $id ) {
240 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
241 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
242 return $rec === null ?
null : new Revision( $rec );
246 * Load either the current, or a specified, revision
247 * that's attached to a given page. If not attached
248 * to that page, will return null.
250 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
252 * @param IDatabase $db
255 * @return Revision|null
257 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
258 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
259 return $rec === null ?
null : new Revision( $rec );
263 * Load either the current, or a specified, revision
264 * that's attached to a given page. If not attached
265 * to that page, will return null.
267 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
269 * @param IDatabase $db
270 * @param Title $title
272 * @return Revision|null
274 public static function loadFromTitle( $db, $title, $id = 0 ) {
275 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
276 return $rec === null ?
null : new Revision( $rec );
280 * Load the revision for the given title with the given timestamp.
281 * WARNING: Timestamps may in some circumstances not be unique,
282 * so this isn't the best key to use.
284 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
286 * @param IDatabase $db
287 * @param Title $title
288 * @param string $timestamp
289 * @return Revision|null
291 public static function loadFromTimestamp( $db, $title, $timestamp ) {
292 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
293 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
294 return $rec === null ?
null : new Revision( $rec );
298 * Return a wrapper for a series of database rows to
299 * fetch all of a given page's revisions in turn.
300 * Each row can be fed to the constructor to get objects.
302 * @param LinkTarget $title
303 * @return ResultWrapper
304 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
306 public static function fetchRevision( LinkTarget
$title ) {
307 wfDeprecated( __METHOD__
, '1.31' );
308 return new FakeResultWrapper( [] );
312 * Return the value of a select() JOIN conds array for the user table.
313 * This will get user table rows for logged-in users.
315 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
318 public static function userJoinCond() {
319 wfDeprecated( __METHOD__
, '1.31' );
320 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
324 * Return the value of a select() page conds array for the page table.
325 * This will assure that the revision(s) are not orphaned from live pages.
327 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
330 public static function pageJoinCond() {
331 wfDeprecated( __METHOD__
, '1.31' );
332 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
336 * Return the list of revision fields that should be selected to create
338 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
341 public static function selectFields() {
342 global $wgContentHandlerUseDB;
344 wfDeprecated( __METHOD__
, '1.31' );
360 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
362 if ( $wgContentHandlerUseDB ) {
363 $fields[] = 'rev_content_format';
364 $fields[] = 'rev_content_model';
371 * Return the list of revision fields that should be selected to create
372 * a new revision from an archive row.
373 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
376 public static function selectArchiveFields() {
377 global $wgContentHandlerUseDB;
379 wfDeprecated( __METHOD__
, '1.31' );
397 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
399 if ( $wgContentHandlerUseDB ) {
400 $fields[] = 'ar_content_format';
401 $fields[] = 'ar_content_model';
407 * Return the list of text fields that should be selected to read the
409 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
412 public static function selectTextFields() {
413 wfDeprecated( __METHOD__
, '1.31' );
421 * Return the list of page fields that should be selected from page table
422 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
425 public static function selectPageFields() {
426 wfDeprecated( __METHOD__
, '1.31' );
438 * Return the list of user fields that should be selected from user table
439 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
442 public static function selectUserFields() {
443 wfDeprecated( __METHOD__
, '1.31' );
444 return [ 'user_name' ];
448 * Return the tables, fields, and join conditions to be selected to create
449 * a new revision object.
451 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
452 * @param array $options Any combination of the following strings
453 * - 'page': Join with the page table, and select fields to identify the page
454 * - 'user': Join with the user table, and select the user name
455 * - 'text': Join with the text table, and select fields to load page text
456 * @return array With three keys:
457 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
458 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
459 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
461 public static function getQueryInfo( $options = [] ) {
462 return self
::getRevisionStore()->getQueryInfo( $options );
466 * Return the tables, fields, and join conditions to be selected to create
467 * a new archived revision object.
469 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
470 * @return array With three keys:
471 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
472 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
473 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
475 public static function getArchiveQueryInfo() {
476 return self
::getRevisionStore()->getArchiveQueryInfo();
480 * Do a batched query to get the parent revision lengths
481 * @param IDatabase $db
482 * @param array $revIds
485 public static function getParentLengths( $db, array $revIds ) {
486 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
490 * @param object|array|RevisionRecord $row Either a database row or an array
491 * @param int $queryFlags
492 * @param Title|null $title
496 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
499 if ( $row instanceof RevisionRecord
) {
500 $this->mRecord
= $row;
501 } elseif ( is_array( $row ) ) {
502 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
503 $row['user'] = $wgUser;
506 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
511 } elseif ( is_object( $row ) ) {
512 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
518 throw new InvalidArgumentException(
519 '$row must be a row object, an associative array, or a RevisionRecord'
525 * @return RevisionRecord
527 public function getRevisionRecord() {
528 return $this->mRecord
;
536 public function getId() {
537 return $this->mRecord
->getId();
541 * Set the revision ID
543 * This should only be used for proposed revisions that turn out to be null edits.
545 * @note Only supported on Revisions that were constructed based on associative arrays,
546 * since they are mutable.
549 * @param int|string $id
550 * @throws MWException
552 public function setId( $id ) {
553 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
554 $this->mRecord
->setId( intval( $id ) );
556 throw new MWException( __METHOD__
. ' is not supported on this instance' );
561 * Set the user ID/name
563 * This should only be used for proposed revisions that turn out to be null edits
565 * @note Only supported on Revisions that were constructed based on associative arrays,
566 * since they are mutable.
569 * @deprecated since 1.31, please reuse old Revision object
570 * @param int $id User ID
571 * @param string $name User name
572 * @throws MWException
574 public function setUserIdAndName( $id, $name ) {
575 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
576 $user = new UserIdentityValue( intval( $id ), $name );
577 $this->mRecord
->setUser( $user );
579 throw new MWException( __METHOD__
. ' is not supported on this instance' );
586 private function getMainSlotRaw() {
587 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
591 * Get the ID of the row of the text table that contains the content of the
592 * revision's main slot, if that content is stored in the text table.
594 * If the content is stored elsewhere, this returns null.
596 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
597 * get that actual address that can be used with BlobStore::getBlob(); or use
598 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
602 public function getTextId() {
603 $slot = $this->getMainSlotRaw();
604 return $slot->hasAddress()
605 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
610 * Get parent revision ID (the original previous page revision)
612 * @return int|null The ID of the parent revision. 0 indicates that there is no
613 * parent revision. Null indicates that the parent revision is not known.
615 public function getParentId() {
616 return $this->mRecord
->getParentId();
620 * Returns the length of the text in this revision, or null if unknown.
624 public function getSize() {
625 return $this->mRecord
->getSize();
629 * Returns the base36 sha1 of the content in this revision, or null if unknown.
633 public function getSha1() {
634 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
635 return $this->mRecord
->getSha1();
639 * Returns the title of the page associated with this entry.
640 * Since 1.31, this will never return null.
642 * Will do a query, when title is not set and id is given.
646 public function getTitle() {
647 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
648 return Title
::newFromLinkTarget( $linkTarget );
652 * Set the title of the revision
654 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
656 * @param Title $title
658 public function setTitle( $title ) {
659 if ( !$title->equals( $this->getTitle() ) ) {
660 throw new InvalidArgumentException(
661 $title->getPrefixedText()
662 . ' is not the same as '
663 . $this->mRecord
->getPageAsLinkTarget()->__toString()
673 public function getPage() {
674 return $this->mRecord
->getPageId();
678 * Fetch revision's user id if it's available to the specified audience.
679 * If the specified audience does not have access to it, zero will be
682 * @param int $audience One of:
683 * Revision::FOR_PUBLIC to be displayed to all users
684 * Revision::FOR_THIS_USER to be displayed to the given user
685 * Revision::RAW get the ID regardless of permissions
686 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
687 * to the $audience parameter
690 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
693 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
697 $user = $this->mRecord
->getUser( $audience, $user );
698 return $user ?
$user->getId() : 0;
702 * Fetch revision's user id without regard for the current user's permissions
705 * @deprecated since 1.25, use getUser( Revision::RAW )
707 public function getRawUser() {
708 wfDeprecated( __METHOD__
, '1.25' );
709 return $this->getUser( self
::RAW
);
713 * Fetch revision's username if it's available to the specified audience.
714 * If the specified audience does not have access to the username, an
715 * empty string will be returned.
717 * @param int $audience One of:
718 * Revision::FOR_PUBLIC to be displayed to all users
719 * Revision::FOR_THIS_USER to be displayed to the given user
720 * Revision::RAW get the text regardless of permissions
721 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
722 * to the $audience parameter
725 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
728 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
732 $user = $this->mRecord
->getUser( $audience, $user );
733 return $user ?
$user->getName() : '';
737 * Fetch revision's username without regard for view restrictions
740 * @deprecated since 1.25, use getUserText( Revision::RAW )
742 public function getRawUserText() {
743 wfDeprecated( __METHOD__
, '1.25' );
744 return $this->getUserText( self
::RAW
);
748 * Fetch revision comment if it's available to the specified audience.
749 * If the specified audience does not have access to the comment, an
750 * empty string will be returned.
752 * @param int $audience One of:
753 * Revision::FOR_PUBLIC to be displayed to all users
754 * Revision::FOR_THIS_USER to be displayed to the given user
755 * Revision::RAW get the text regardless of permissions
756 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
757 * to the $audience parameter
760 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
763 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
767 $comment = $this->mRecord
->getComment( $audience, $user );
768 return $comment === null ?
null : $comment->text
;
772 * Fetch revision comment without regard for the current user's permissions
775 * @deprecated since 1.25, use getComment( Revision::RAW )
777 public function getRawComment() {
778 wfDeprecated( __METHOD__
, '1.25' );
779 return $this->getComment( self
::RAW
);
785 public function isMinor() {
786 return $this->mRecord
->isMinor();
790 * @return int Rcid of the unpatrolled row, zero if there isn't one
792 public function isUnpatrolled() {
793 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
797 * Get the RC object belonging to the current revision, if there's one
799 * @param int $flags (optional) $flags include:
800 * Revision::READ_LATEST : Select the data from the master
803 * @return RecentChange|null
805 public function getRecentChange( $flags = 0 ) {
806 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
810 * @param int $field One of DELETED_* bitfield constants
814 public function isDeleted( $field ) {
815 return $this->mRecord
->isDeleted( $field );
819 * Get the deletion bitfield of the revision
823 public function getVisibility() {
824 return $this->mRecord
->getVisibility();
828 * Fetch revision content if it's available to the specified audience.
829 * If the specified audience does not have the ability to view this
830 * revision, or the content could not be loaded, null will be returned.
832 * @param int $audience One of:
833 * Revision::FOR_PUBLIC to be displayed to all users
834 * Revision::FOR_THIS_USER to be displayed to $user
835 * Revision::RAW get the text regardless of permissions
836 * @param User $user User object to check for, only if FOR_THIS_USER is passed
837 * to the $audience parameter
839 * @return Content|null
841 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
844 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
849 return $this->mRecord
->getContent( 'main', $audience, $user );
851 catch ( RevisionAccessException
$e ) {
857 * Get original serialized data (without checking view restrictions)
860 * @deprecated since 1.31, use BlobStore::getBlob instead.
864 public function getSerializedData() {
865 $slot = $this->getMainSlotRaw();
866 return $slot->getContent()->serialize();
870 * Returns the content model for the main slot of this revision.
872 * If no content model was stored in the database, the default content model for the title is
873 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
874 * is used as a last resort.
876 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
878 * @return string The content model id associated with this revision,
879 * see the CONTENT_MODEL_XXX constants.
881 public function getContentModel() {
882 return $this->getMainSlotRaw()->getModel();
886 * Returns the content format for the main slot of this revision.
888 * If no content format was stored in the database, the default format for this
889 * revision's content model is returned.
891 * @todo: drop this, the format is irrelevant to the revision!
893 * @return string The content format id associated with this revision,
894 * see the CONTENT_FORMAT_XXX constants.
896 public function getContentFormat() {
897 $format = $this->getMainSlotRaw()->getFormat();
899 if ( $format === null ) {
900 // if no format was stored along with the blob, fall back to default format
901 $format = $this->getContentHandler()->getDefaultFormat();
908 * Returns the content handler appropriate for this revision's content model.
910 * @throws MWException
911 * @return ContentHandler
913 public function getContentHandler() {
914 return ContentHandler
::getForModelID( $this->getContentModel() );
920 public function getTimestamp() {
921 return $this->mRecord
->getTimestamp();
927 public function isCurrent() {
928 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
932 * Get previous revision for this title
934 * @return Revision|null
936 public function getPrevious() {
937 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $this->getTitle() );
940 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
944 * Get next revision for this title
946 * @return Revision|null
948 public function getNext() {
949 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $this->getTitle() );
952 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
956 * Get revision text associated with an old or archive row
958 * Both the flags and the text field must be included. Including the old_id
959 * field will activate cache usage as long as the $wiki parameter is not set.
961 * @param stdClass $row The text data
962 * @param string $prefix Table prefix (default 'old_')
963 * @param string|bool $wiki The name of the wiki to load the revision text from
964 * (same as the the wiki $row was loaded from) or false to indicate the local
965 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
966 * identifier as understood by the LoadBalancer class.
967 * @return string|false Text the text requested or false on failure
969 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
970 $textField = $prefix . 'text';
971 $flagsField = $prefix . 'flags';
973 if ( isset( $row->$flagsField ) ) {
974 $flags = explode( ',', $row->$flagsField );
979 if ( isset( $row->$textField ) ) {
980 $text = $row->$textField;
985 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
987 return self
::getBlobStore()->expandBlob( $text, $flags, $cacheKey );
991 * If $wgCompressRevisions is enabled, we will compress data.
992 * The input string is modified in place.
993 * Return value is the flags field: contains 'gzip' if the
994 * data is compressed, and 'utf-8' if we're saving in UTF-8
997 * @param mixed &$text Reference to a text
1000 public static function compressRevisionText( &$text ) {
1001 return self
::getBlobStore()->compressData( $text );
1005 * Re-converts revision text according to it's flags.
1007 * @param mixed $text Reference to a text
1008 * @param array $flags Compression flags
1009 * @return string|bool Decompressed text, or false on failure
1011 public static function decompressRevisionText( $text, $flags ) {
1012 return self
::getBlobStore()->decompressData( $text, $flags );
1016 * Insert a new revision into the database, returning the new revision ID
1017 * number on success and dies horribly on failure.
1019 * @param IDatabase $dbw (master connection)
1020 * @throws MWException
1021 * @return int The revision ID
1023 public function insertOn( $dbw ) {
1026 // Note that $this->mRecord->getId() will typically return null here, but not always,
1027 // e.g. not when restoring a revision.
1029 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
1030 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
1031 $this->mRecord
->setUser( $wgUser );
1033 throw new MWException( 'Cannot insert revision with no associated user.' );
1037 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1039 $this->mRecord
= $rec;
1041 // Avoid PHP 7.1 warning of passing $this by reference
1043 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1044 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1046 return $rec->getId();
1050 * Get the base 36 SHA-1 value for a string of text
1051 * @param string $text
1054 public static function base36Sha1( $text ) {
1055 return SlotRecord
::base36Sha1( $text );
1059 * Create a new null-revision for insertion into a page's
1060 * history. This will not re-save the text, but simply refer
1061 * to the text from the previous version.
1063 * Such revisions can for instance identify page rename
1064 * operations and other such meta-modifications.
1066 * @param IDatabase $dbw
1067 * @param int $pageId ID number of the page to read from
1068 * @param string $summary Revision's summary
1069 * @param bool $minor Whether the revision should be considered as minor
1070 * @param User|null $user User object to use or null for $wgUser
1071 * @return Revision|null Revision or null on error
1073 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1079 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1081 $title = Title
::newFromID( $pageId );
1082 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1084 return new Revision( $rec );
1088 * Determine if the current user is allowed to view a particular
1089 * field of this revision, if it's marked as deleted.
1091 * @param int $field One of self::DELETED_TEXT,
1092 * self::DELETED_COMMENT,
1093 * self::DELETED_USER
1094 * @param User|null $user User object to check, or null to use $wgUser
1097 public function userCan( $field, User
$user = null ) {
1098 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1102 * Determine if the current user is allowed to view a particular
1103 * field of this revision, if it's marked as deleted. This is used
1104 * by various classes to avoid duplication.
1106 * @param int $bitfield Current field
1107 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1108 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1109 * self::DELETED_USER = File::DELETED_USER
1110 * @param User|null $user User object to check, or null to use $wgUser
1111 * @param Title|null $title A Title object to check for per-page restrictions on,
1112 * instead of just plain userrights
1115 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1124 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1128 * Get rev_timestamp from rev_id, without loading the rest of the row
1130 * @param Title $title
1133 * @return string|bool False if not found
1135 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1136 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1140 * Get count of revisions per page...not very efficient
1142 * @param IDatabase $db
1143 * @param int $id Page id
1146 static function countByPageId( $db, $id ) {
1147 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1151 * Get count of revisions per page...not very efficient
1153 * @param IDatabase $db
1154 * @param Title $title
1157 static function countByTitle( $db, $title ) {
1158 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1162 * Check if no edits were made by other users since
1163 * the time a user started editing the page. Limit to
1164 * 50 revisions for the sake of performance.
1167 * @deprecated since 1.24
1169 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1170 * Database object or a database identifier usable with wfGetDB.
1171 * @param int $pageId The ID of the page in question
1172 * @param int $userId The ID of the user in question
1173 * @param string $since Look at edits since this time
1175 * @return bool True if the given user was the only one to edit since the given timestamp
1177 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1178 if ( is_int( $db ) ) {
1179 $db = wfGetDB( $db );
1182 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1186 * Load a revision based on a known page ID and current revision ID from the DB
1188 * This method allows for the use of caching, though accessing anything that normally
1189 * requires permission checks (aside from the text) will trigger a small DB lookup.
1190 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1192 * @param IDatabase $db ignored!
1193 * @param int|Title $pageIdOrTitle Page ID or Title object
1194 * @param int $revId Known current revision of this page. Determined automatically if not given.
1195 * @return Revision|bool Returns false if missing
1198 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1199 $title = $pageIdOrTitle instanceof Title
1201 : Title
::newFromID( $pageIdOrTitle );
1203 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1204 return $record ?
new Revision( $record ) : false;