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 * @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
148 * @param Title $title (optional)
150 * @throws MWException
153 public static function newFromArchiveRow( $row, $overrides = [], Title
$title = null ) {
155 * MCR Migration: https://phabricator.wikimedia.org/T183564
156 * This method used to overwrite attributes, then passed to Revision::__construct
157 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
158 * So do a conversion here.
160 if ( array_key_exists( 'page', $overrides ) ) {
161 $overrides['page_id'] = $overrides['page'];
162 unset( $overrides['page'] );
165 $rec = self
::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
166 return new Revision( $rec, self
::READ_NORMAL
, $title );
172 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
173 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
174 * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
175 * can be used as a temporary replacement, but should be avoided.
177 * @param object|array $row
180 public static function newFromRow( $row ) {
181 if ( is_array( $row ) ) {
182 $rec = self
::getRevisionStore()->newMutableRevisionFromArray( $row );
184 $rec = self
::getRevisionStore()->newRevisionFromRow( $row );
187 return new Revision( $rec );
191 * Load a page revision from a given revision ID number.
192 * Returns null if no such revision can be found.
194 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
196 * @param IDatabase $db
198 * @return Revision|null
200 public static function loadFromId( $db, $id ) {
201 wfDeprecated( __METHOD__
, '1.31' ); // no known callers
202 $rec = self
::getRevisionStore()->loadRevisionFromId( $db, $id );
203 return $rec === null ?
null : new Revision( $rec );
207 * Load either the current, or a specified, revision
208 * that's attached to a given page. If not attached
209 * to that page, will return null.
211 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
213 * @param IDatabase $db
216 * @return Revision|null
218 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
219 $rec = self
::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
220 return $rec === null ?
null : new Revision( $rec );
224 * Load either the current, or a specified, revision
225 * that's attached to a given page. If not attached
226 * to that page, will return null.
228 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
230 * @param IDatabase $db
231 * @param Title $title
233 * @return Revision|null
235 public static function loadFromTitle( $db, $title, $id = 0 ) {
236 $rec = self
::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
237 return $rec === null ?
null : new Revision( $rec );
241 * Load the revision for the given title with the given timestamp.
242 * WARNING: Timestamps may in some circumstances not be unique,
243 * so this isn't the best key to use.
245 * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
247 * @param IDatabase $db
248 * @param Title $title
249 * @param string $timestamp
250 * @return Revision|null
252 public static function loadFromTimestamp( $db, $title, $timestamp ) {
253 // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
254 $rec = self
::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
255 return $rec === null ?
null : new Revision( $rec );
259 * Return a wrapper for a series of database rows to
260 * fetch all of a given page's revisions in turn.
261 * Each row can be fed to the constructor to get objects.
263 * @param LinkTarget $title
264 * @return ResultWrapper
265 * @deprecated Since 1.28, no callers in core nor in known extensions. No-op since 1.31.
267 public static function fetchRevision( LinkTarget
$title ) {
268 wfDeprecated( __METHOD__
, '1.31' );
269 return new FakeResultWrapper( [] );
273 * Return the value of a select() JOIN conds array for the user table.
274 * This will get user table rows for logged-in users.
276 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
279 public static function userJoinCond() {
280 wfDeprecated( __METHOD__
, '1.31' );
281 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
285 * Return the value of a select() page conds array for the page table.
286 * This will assure that the revision(s) are not orphaned from live pages.
288 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
291 public static function pageJoinCond() {
292 wfDeprecated( __METHOD__
, '1.31' );
293 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
297 * Return the list of revision fields that should be selected to create
299 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
302 public static function selectFields() {
303 global $wgContentHandlerUseDB;
305 wfDeprecated( __METHOD__
, '1.31' );
321 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
323 if ( $wgContentHandlerUseDB ) {
324 $fields[] = 'rev_content_format';
325 $fields[] = 'rev_content_model';
332 * Return the list of revision fields that should be selected to create
333 * a new revision from an archive row.
334 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
337 public static function selectArchiveFields() {
338 global $wgContentHandlerUseDB;
340 wfDeprecated( __METHOD__
, '1.31' );
358 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
360 if ( $wgContentHandlerUseDB ) {
361 $fields[] = 'ar_content_format';
362 $fields[] = 'ar_content_model';
368 * Return the list of text fields that should be selected to read the
370 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'text' ] ) instead.
373 public static function selectTextFields() {
374 wfDeprecated( __METHOD__
, '1.31' );
382 * Return the list of page fields that should be selected from page table
383 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'page' ] ) instead.
386 public static function selectPageFields() {
387 wfDeprecated( __METHOD__
, '1.31' );
399 * Return the list of user fields that should be selected from user table
400 * @deprecated since 1.31, use RevisionStore::getQueryInfo( [ 'user' ] ) instead.
403 public static function selectUserFields() {
404 wfDeprecated( __METHOD__
, '1.31' );
405 return [ 'user_name' ];
409 * Return the tables, fields, and join conditions to be selected to create
410 * a new revision object.
412 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
413 * @param array $options Any combination of the following strings
414 * - 'page': Join with the page table, and select fields to identify the page
415 * - 'user': Join with the user table, and select the user name
416 * - 'text': Join with the text table, and select fields to load page text
417 * @return array With three keys:
418 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
419 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
420 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
422 public static function getQueryInfo( $options = [] ) {
423 return self
::getRevisionStore()->getQueryInfo( $options );
427 * Return the tables, fields, and join conditions to be selected to create
428 * a new archived revision object.
430 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
431 * @return array With three keys:
432 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
433 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
434 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
436 public static function getArchiveQueryInfo() {
437 return self
::getRevisionStore()->getArchiveQueryInfo();
441 * Do a batched query to get the parent revision lengths
442 * @param IDatabase $db
443 * @param array $revIds
446 public static function getParentLengths( $db, array $revIds ) {
447 return self
::getRevisionStore()->listRevisionSizes( $db, $revIds );
451 * @param object|array|RevisionRecord $row Either a database row or an array
452 * @param int $queryFlags
453 * @param Title|null $title
457 function __construct( $row, $queryFlags = 0, Title
$title = null ) {
460 if ( $row instanceof RevisionRecord
) {
461 $this->mRecord
= $row;
462 } elseif ( is_array( $row ) ) {
463 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
464 $row['user'] = $wgUser;
467 $this->mRecord
= self
::getRevisionStore()->newMutableRevisionFromArray(
472 } elseif ( is_object( $row ) ) {
473 $this->mRecord
= self
::getRevisionStore()->newRevisionFromRow(
479 throw new InvalidArgumentException(
480 '$row must be a row object, an associative array, or a RevisionRecord'
486 * @return RevisionRecord
488 public function getRevisionRecord() {
489 return $this->mRecord
;
497 public function getId() {
498 return $this->mRecord
->getId();
502 * Set the revision ID
504 * This should only be used for proposed revisions that turn out to be null edits.
506 * @note Only supported on Revisions that were constructed based on associative arrays,
507 * since they are mutable.
510 * @param int|string $id
511 * @throws MWException
513 public function setId( $id ) {
514 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
515 $this->mRecord
->setId( intval( $id ) );
517 throw new MWException( __METHOD__
. ' is not supported on this instance' );
522 * Set the user ID/name
524 * This should only be used for proposed revisions that turn out to be null edits
526 * @note Only supported on Revisions that were constructed based on associative arrays,
527 * since they are mutable.
530 * @deprecated since 1.31, please reuse old Revision object
531 * @param int $id User ID
532 * @param string $name User name
533 * @throws MWException
535 public function setUserIdAndName( $id, $name ) {
536 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
537 $user = new UserIdentityValue( intval( $id ), $name );
538 $this->mRecord
->setUser( $user );
540 throw new MWException( __METHOD__
. ' is not supported on this instance' );
547 private function getMainSlotRaw() {
548 return $this->mRecord
->getSlot( 'main', RevisionRecord
::RAW
);
552 * Get the ID of the row of the text table that contains the content of the
553 * revision's main slot, if that content is stored in the text table.
555 * If the content is stored elsewhere, this returns null.
557 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
558 * get that actual address that can be used with BlobStore::getBlob(); or use
559 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
563 public function getTextId() {
564 $slot = $this->getMainSlotRaw();
565 return $slot->hasAddress()
566 ? self
::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
571 * Get parent revision ID (the original previous page revision)
573 * @return int|null The ID of the parent revision. 0 indicates that there is no
574 * parent revision. Null indicates that the parent revision is not known.
576 public function getParentId() {
577 return $this->mRecord
->getParentId();
581 * Returns the length of the text in this revision, or null if unknown.
585 public function getSize() {
586 return $this->mRecord
->getSize();
590 * Returns the base36 sha1 of the content in this revision, or null if unknown.
594 public function getSha1() {
595 // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
596 return $this->mRecord
->getSha1();
600 * Returns the title of the page associated with this entry.
601 * Since 1.31, this will never return null.
603 * Will do a query, when title is not set and id is given.
607 public function getTitle() {
608 $linkTarget = $this->mRecord
->getPageAsLinkTarget();
609 return Title
::newFromLinkTarget( $linkTarget );
613 * Set the title of the revision
615 * @deprecated: since 1.31, this is now a noop. Pass the Title to the constructor instead.
617 * @param Title $title
619 public function setTitle( $title ) {
620 if ( !$title->equals( $this->getTitle() ) ) {
621 throw new InvalidArgumentException(
622 $title->getPrefixedText()
623 . ' is not the same as '
624 . $this->mRecord
->getPageAsLinkTarget()->__toString()
634 public function getPage() {
635 return $this->mRecord
->getPageId();
639 * Fetch revision's user id if it's available to the specified audience.
640 * If the specified audience does not have access to it, zero will be
643 * @param int $audience One of:
644 * Revision::FOR_PUBLIC to be displayed to all users
645 * Revision::FOR_THIS_USER to be displayed to the given user
646 * Revision::RAW get the ID regardless of permissions
647 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
648 * to the $audience parameter
651 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
654 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
658 $user = $this->mRecord
->getUser( $audience, $user );
659 return $user ?
$user->getId() : 0;
663 * Fetch revision's user id without regard for the current user's permissions
666 * @deprecated since 1.25, use getUser( Revision::RAW )
668 public function getRawUser() {
669 wfDeprecated( __METHOD__
, '1.25' );
670 return $this->getUser( self
::RAW
);
674 * Fetch revision's username if it's available to the specified audience.
675 * If the specified audience does not have access to the username, an
676 * empty string will be returned.
678 * @param int $audience One of:
679 * Revision::FOR_PUBLIC to be displayed to all users
680 * Revision::FOR_THIS_USER to be displayed to the given user
681 * Revision::RAW get the text regardless of permissions
682 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
683 * to the $audience parameter
686 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
689 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
693 $user = $this->mRecord
->getUser( $audience, $user );
694 return $user ?
$user->getName() : '';
698 * Fetch revision's username without regard for view restrictions
701 * @deprecated since 1.25, use getUserText( Revision::RAW )
703 public function getRawUserText() {
704 wfDeprecated( __METHOD__
, '1.25' );
705 return $this->getUserText( self
::RAW
);
709 * Fetch revision comment if it's available to the specified audience.
710 * If the specified audience does not have access to the comment, an
711 * empty string will be returned.
713 * @param int $audience One of:
714 * Revision::FOR_PUBLIC to be displayed to all users
715 * Revision::FOR_THIS_USER to be displayed to the given user
716 * Revision::RAW get the text regardless of permissions
717 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
718 * to the $audience parameter
721 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
724 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
728 $comment = $this->mRecord
->getComment( $audience, $user );
729 return $comment === null ?
null : $comment->text
;
733 * Fetch revision comment without regard for the current user's permissions
736 * @deprecated since 1.25, use getComment( Revision::RAW )
738 public function getRawComment() {
739 wfDeprecated( __METHOD__
, '1.25' );
740 return $this->getComment( self
::RAW
);
746 public function isMinor() {
747 return $this->mRecord
->isMinor();
751 * @return int Rcid of the unpatrolled row, zero if there isn't one
753 public function isUnpatrolled() {
754 return self
::getRevisionStore()->isUnpatrolled( $this->mRecord
);
758 * Get the RC object belonging to the current revision, if there's one
760 * @param int $flags (optional) $flags include:
761 * Revision::READ_LATEST : Select the data from the master
764 * @return RecentChange|null
766 public function getRecentChange( $flags = 0 ) {
767 return self
::getRevisionStore()->getRecentChange( $this->mRecord
, $flags );
771 * @param int $field One of DELETED_* bitfield constants
775 public function isDeleted( $field ) {
776 return $this->mRecord
->isDeleted( $field );
780 * Get the deletion bitfield of the revision
784 public function getVisibility() {
785 return $this->mRecord
->getVisibility();
789 * Fetch revision content if it's available to the specified audience.
790 * If the specified audience does not have the ability to view this
791 * revision, or the content could not be loaded, null will be returned.
793 * @param int $audience One of:
794 * Revision::FOR_PUBLIC to be displayed to all users
795 * Revision::FOR_THIS_USER to be displayed to $user
796 * Revision::RAW get the text regardless of permissions
797 * @param User $user User object to check for, only if FOR_THIS_USER is passed
798 * to the $audience parameter
800 * @return Content|null
802 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
805 if ( $audience === self
::FOR_THIS_USER
&& !$user ) {
810 return $this->mRecord
->getContent( 'main', $audience, $user );
812 catch ( RevisionAccessException
$e ) {
818 * Get original serialized data (without checking view restrictions)
821 * @deprecated since 1.31, use BlobStore::getBlob instead.
825 public function getSerializedData() {
826 $slot = $this->getMainSlotRaw();
827 return $slot->getContent()->serialize();
831 * Returns the content model for the main slot of this revision.
833 * If no content model was stored in the database, the default content model for the title is
834 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
835 * is used as a last resort.
837 * @todo: drop this, with MCR, there no longer is a single model associated with a revision.
839 * @return string The content model id associated with this revision,
840 * see the CONTENT_MODEL_XXX constants.
842 public function getContentModel() {
843 return $this->getMainSlotRaw()->getModel();
847 * Returns the content format for the main slot of this revision.
849 * If no content format was stored in the database, the default format for this
850 * revision's content model is returned.
852 * @todo: drop this, the format is irrelevant to the revision!
854 * @return string The content format id associated with this revision,
855 * see the CONTENT_FORMAT_XXX constants.
857 public function getContentFormat() {
858 $format = $this->getMainSlotRaw()->getFormat();
860 if ( $format === null ) {
861 // if no format was stored along with the blob, fall back to default format
862 $format = $this->getContentHandler()->getDefaultFormat();
869 * Returns the content handler appropriate for this revision's content model.
871 * @throws MWException
872 * @return ContentHandler
874 public function getContentHandler() {
875 return ContentHandler
::getForModelID( $this->getContentModel() );
881 public function getTimestamp() {
882 return $this->mRecord
->getTimestamp();
888 public function isCurrent() {
889 return ( $this->mRecord
instanceof RevisionStoreRecord
) && $this->mRecord
->isCurrent();
893 * Get previous revision for this title
895 * @return Revision|null
897 public function getPrevious() {
898 $rec = self
::getRevisionStore()->getPreviousRevision( $this->mRecord
, $this->getTitle() );
901 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
905 * Get next revision for this title
907 * @return Revision|null
909 public function getNext() {
910 $rec = self
::getRevisionStore()->getNextRevision( $this->mRecord
, $this->getTitle() );
913 : new Revision( $rec, self
::READ_NORMAL
, $this->getTitle() );
917 * Get revision text associated with an old or archive row
919 * Both the flags and the text field must be included. Including the old_id
920 * field will activate cache usage as long as the $wiki parameter is not set.
922 * @param stdClass $row The text data
923 * @param string $prefix Table prefix (default 'old_')
924 * @param string|bool $wiki The name of the wiki to load the revision text from
925 * (same as the the wiki $row was loaded from) or false to indicate the local
926 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
927 * identifier as understood by the LoadBalancer class.
928 * @return string|false Text the text requested or false on failure
930 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
931 $textField = $prefix . 'text';
932 $flagsField = $prefix . 'flags';
934 if ( isset( $row->$flagsField ) ) {
935 $flags = explode( ',', $row->$flagsField );
940 if ( isset( $row->$textField ) ) {
941 $text = $row->$textField;
946 $cacheKey = isset( $row->old_id
) ?
( 'tt:' . $row->old_id
) : null;
948 return self
::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
952 * If $wgCompressRevisions is enabled, we will compress data.
953 * The input string is modified in place.
954 * Return value is the flags field: contains 'gzip' if the
955 * data is compressed, and 'utf-8' if we're saving in UTF-8
958 * @param mixed &$text Reference to a text
961 public static function compressRevisionText( &$text ) {
962 return self
::getBlobStore()->compressData( $text );
966 * Re-converts revision text according to it's flags.
968 * @param mixed $text Reference to a text
969 * @param array $flags Compression flags
970 * @return string|bool Decompressed text, or false on failure
972 public static function decompressRevisionText( $text, $flags ) {
973 return self
::getBlobStore()->decompressData( $text, $flags );
977 * Insert a new revision into the database, returning the new revision ID
978 * number on success and dies horribly on failure.
980 * @param IDatabase $dbw (master connection)
981 * @throws MWException
982 * @return int The revision ID
984 public function insertOn( $dbw ) {
987 // Note that $this->mRecord->getId() will typically return null here, but not always,
988 // e.g. not when restoring a revision.
990 if ( $this->mRecord
->getUser( RevisionRecord
::RAW
) === null ) {
991 if ( $this->mRecord
instanceof MutableRevisionRecord
) {
992 $this->mRecord
->setUser( $wgUser );
994 throw new MWException( 'Cannot insert revision with no associated user.' );
998 $rec = self
::getRevisionStore()->insertRevisionOn( $this->mRecord
, $dbw );
1000 $this->mRecord
= $rec;
1002 // Avoid PHP 7.1 warning of passing $this by reference
1004 // TODO: hard-deprecate in 1.32 (or even 1.31?)
1005 Hooks
::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
1007 return $rec->getId();
1011 * Get the base 36 SHA-1 value for a string of text
1012 * @param string $text
1015 public static function base36Sha1( $text ) {
1016 return SlotRecord
::base36Sha1( $text );
1020 * Create a new null-revision for insertion into a page's
1021 * history. This will not re-save the text, but simply refer
1022 * to the text from the previous version.
1024 * Such revisions can for instance identify page rename
1025 * operations and other such meta-modifications.
1027 * @param IDatabase $dbw
1028 * @param int $pageId ID number of the page to read from
1029 * @param string $summary Revision's summary
1030 * @param bool $minor Whether the revision should be considered as minor
1031 * @param User|null $user User object to use or null for $wgUser
1032 * @return Revision|null Revision or null on error
1034 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1040 $comment = CommentStoreComment
::newUnsavedComment( $summary, null );
1042 $title = Title
::newFromID( $pageId );
1043 $rec = self
::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1045 return new Revision( $rec );
1049 * Determine if the current user is allowed to view a particular
1050 * field of this revision, if it's marked as deleted.
1052 * @param int $field One of self::DELETED_TEXT,
1053 * self::DELETED_COMMENT,
1054 * self::DELETED_USER
1055 * @param User|null $user User object to check, or null to use $wgUser
1058 public function userCan( $field, User
$user = null ) {
1059 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1063 * Determine if the current user is allowed to view a particular
1064 * field of this revision, if it's marked as deleted. This is used
1065 * by various classes to avoid duplication.
1067 * @param int $bitfield Current field
1068 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1069 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1070 * self::DELETED_USER = File::DELETED_USER
1071 * @param User|null $user User object to check, or null to use $wgUser
1072 * @param Title|null $title A Title object to check for per-page restrictions on,
1073 * instead of just plain userrights
1076 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1085 return RevisionRecord
::userCanBitfield( $bitfield, $field, $user, $title );
1089 * Get rev_timestamp from rev_id, without loading the rest of the row
1091 * @param Title $title
1094 * @return string|bool False if not found
1096 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1097 return self
::getRevisionStore()->getTimestampFromId( $title, $id, $flags );
1101 * Get count of revisions per page...not very efficient
1103 * @param IDatabase $db
1104 * @param int $id Page id
1107 static function countByPageId( $db, $id ) {
1108 return self
::getRevisionStore()->countRevisionsByPageId( $db, $id );
1112 * Get count of revisions per page...not very efficient
1114 * @param IDatabase $db
1115 * @param Title $title
1118 static function countByTitle( $db, $title ) {
1119 return self
::getRevisionStore()->countRevisionsByTitle( $db, $title );
1123 * Check if no edits were made by other users since
1124 * the time a user started editing the page. Limit to
1125 * 50 revisions for the sake of performance.
1128 * @deprecated since 1.24
1130 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1131 * Database object or a database identifier usable with wfGetDB.
1132 * @param int $pageId The ID of the page in question
1133 * @param int $userId The ID of the user in question
1134 * @param string $since Look at edits since this time
1136 * @return bool True if the given user was the only one to edit since the given timestamp
1138 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1139 if ( is_int( $db ) ) {
1140 $db = wfGetDB( $db );
1143 return self
::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1147 * Load a revision based on a known page ID and current revision ID from the DB
1149 * This method allows for the use of caching, though accessing anything that normally
1150 * requires permission checks (aside from the text) will trigger a small DB lookup.
1151 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1153 * @param IDatabase $db ignored!
1154 * @param int|Title $pageIdOrTitle Page ID or Title object
1155 * @param int $revId Known current revision of this page. Determined automatically if not given.
1156 * @return Revision|bool Returns false if missing
1159 public static function newKnownCurrent( IDatabase
$db, $pageIdOrTitle, $revId = 0 ) {
1160 $title = $pageIdOrTitle instanceof Title
1162 : Title
::newFromID( $pageIdOrTitle );
1164 $record = self
::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
1165 return $record ?
new Revision( $record ) : false;