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 Wikimedia\Rdbms\Database
;
24 use Wikimedia\Rdbms\IDatabase
;
25 use MediaWiki\Linker\LinkTarget
;
26 use MediaWiki\MediaWikiServices
;
27 use Wikimedia\Rdbms\ResultWrapper
;
28 use Wikimedia\Rdbms\FakeResultWrapper
;
33 class Revision
implements IDBAccessObject
{
41 protected $mOrigUserText;
45 protected $mMinorEdit;
47 protected $mTimestamp;
63 protected $mUnpatrolled;
65 /** @var stdClass|null */
68 /** @var null|Title */
73 protected $mContentModel;
75 protected $mContentFormat;
77 /** @var Content|null|bool */
79 /** @var null|ContentHandler */
80 protected $mContentHandler;
83 protected $mQueryFlags = 0;
84 /** @var bool Used for cached values to reload user text and rev_deleted */
85 protected $mRefreshMutableFields = false;
86 /** @var string Wiki ID; false means the current wiki */
87 protected $mWiki = false;
89 // Revision deletion constants
90 const DELETED_TEXT
= 1;
91 const DELETED_COMMENT
= 2;
92 const DELETED_USER
= 4;
93 const DELETED_RESTRICTED
= 8;
94 const SUPPRESSED_USER
= 12; // convenience
95 const SUPPRESSED_ALL
= 15; // convenience
97 // Audience options for accessors
99 const FOR_THIS_USER
= 2;
102 const TEXT_CACHE_GROUP
= 'revisiontext:10'; // process cache name and max key count
105 * Load a page revision from a given revision ID number.
106 * Returns null if no such revision can be found.
109 * Revision::READ_LATEST : Select the data from the master
110 * Revision::READ_LOCKING : Select & lock the data from the master
113 * @param int $flags (optional)
114 * @return Revision|null
116 public static function newFromId( $id, $flags = 0 ) {
117 return self
::newFromConds( [ 'rev_id' => intval( $id ) ], $flags );
121 * Load either the current, or a specified, revision
122 * that's attached to a given link target. If not attached
123 * to that link target, will return null.
126 * Revision::READ_LATEST : Select the data from the master
127 * Revision::READ_LOCKING : Select & lock the data from the master
129 * @param LinkTarget $linkTarget
130 * @param int $id (optional)
131 * @param int $flags Bitfield (optional)
132 * @return Revision|null
134 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
136 'page_namespace' => $linkTarget->getNamespace(),
137 'page_title' => $linkTarget->getDBkey()
140 // Use the specified ID
141 $conds['rev_id'] = $id;
142 return self
::newFromConds( $conds, $flags );
144 // Use a join to get the latest revision
145 $conds[] = 'rev_id=page_latest';
146 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
147 return self
::loadFromConds( $db, $conds, $flags );
152 * Load either the current, or a specified, revision
153 * that's attached to a given page ID.
154 * Returns null if no such revision can be found.
157 * Revision::READ_LATEST : Select the data from the master (since 1.20)
158 * Revision::READ_LOCKING : Select & lock the data from the master
161 * @param int $revId (optional)
162 * @param int $flags Bitfield (optional)
163 * @return Revision|null
165 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
166 $conds = [ 'page_id' => $pageId ];
168 $conds['rev_id'] = $revId;
169 return self
::newFromConds( $conds, $flags );
171 // Use a join to get the latest revision
172 $conds[] = 'rev_id = page_latest';
173 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
174 return self
::loadFromConds( $db, $conds, $flags );
179 * Make a fake revision object from an archive table row. This is queried
180 * for permissions or even inserted (as in Special:Undelete)
181 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
184 * @param array $overrides
186 * @throws MWException
189 public static function newFromArchiveRow( $row, $overrides = [] ) {
190 global $wgContentHandlerUseDB;
192 $attribs = $overrides +
[
193 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
194 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
195 'comment' => CommentStore
::newKey( 'ar_comment' )
196 // Legacy because $row probably came from self::selectArchiveFields()
197 ->getCommentLegacy( wfGetDB( DB_REPLICA
), $row, true )->text
,
198 'user' => $row->ar_user
,
199 'user_text' => $row->ar_user_text
,
200 'timestamp' => $row->ar_timestamp
,
201 'minor_edit' => $row->ar_minor_edit
,
202 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
203 'deleted' => $row->ar_deleted
,
204 'len' => $row->ar_len
,
205 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
206 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
207 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
210 if ( !$wgContentHandlerUseDB ) {
211 unset( $attribs['content_model'] );
212 unset( $attribs['content_format'] );
215 if ( !isset( $attribs['title'] )
216 && isset( $row->ar_namespace
)
217 && isset( $row->ar_title
)
219 $attribs['title'] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
222 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
223 // Pre-1.5 ar_text row
224 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
225 if ( $attribs['text'] === false ) {
226 throw new MWException( 'Unable to load text from archive row (possibly T24624)' );
229 return new self( $attribs );
238 public static function newFromRow( $row ) {
239 return new self( $row );
243 * Load a page revision from a given revision ID number.
244 * Returns null if no such revision can be found.
246 * @param IDatabase $db
248 * @return Revision|null
250 public static function loadFromId( $db, $id ) {
251 return self
::loadFromConds( $db, [ 'rev_id' => intval( $id ) ] );
255 * Load either the current, or a specified, revision
256 * that's attached to a given page. If not attached
257 * to that page, will return null.
259 * @param IDatabase $db
262 * @return Revision|null
264 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
265 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
267 $conds['rev_id'] = intval( $id );
269 $conds[] = 'rev_id=page_latest';
271 return self
::loadFromConds( $db, $conds );
275 * Load either the current, or a specified, revision
276 * that's attached to a given page. If not attached
277 * to that page, will return null.
279 * @param IDatabase $db
280 * @param Title $title
282 * @return Revision|null
284 public static function loadFromTitle( $db, $title, $id = 0 ) {
286 $matchId = intval( $id );
288 $matchId = 'page_latest';
290 return self
::loadFromConds( $db,
293 'page_namespace' => $title->getNamespace(),
294 'page_title' => $title->getDBkey()
300 * Load the revision for the given title with the given timestamp.
301 * WARNING: Timestamps may in some circumstances not be unique,
302 * so this isn't the best key to use.
304 * @param IDatabase $db
305 * @param Title $title
306 * @param string $timestamp
307 * @return Revision|null
309 public static function loadFromTimestamp( $db, $title, $timestamp ) {
310 return self
::loadFromConds( $db,
312 'rev_timestamp' => $db->timestamp( $timestamp ),
313 'page_namespace' => $title->getNamespace(),
314 'page_title' => $title->getDBkey()
320 * Given a set of conditions, fetch a revision
322 * This method is used then a revision ID is qualified and
323 * will incorporate some basic replica DB/master fallback logic
325 * @param array $conditions
326 * @param int $flags (optional)
327 * @return Revision|null
329 private static function newFromConds( $conditions, $flags = 0 ) {
330 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
332 $rev = self
::loadFromConds( $db, $conditions, $flags );
333 // Make sure new pending/committed revision are visibile later on
334 // within web requests to certain avoid bugs like T93866 and T94407.
336 && !( $flags & self
::READ_LATEST
)
337 && wfGetLB()->getServerCount() > 1
338 && wfGetLB()->hasOrMadeRecentMasterChanges()
340 $flags = self
::READ_LATEST
;
341 $db = wfGetDB( DB_MASTER
);
342 $rev = self
::loadFromConds( $db, $conditions, $flags );
346 $rev->mQueryFlags
= $flags;
353 * Given a set of conditions, fetch a revision from
354 * the given database connection.
356 * @param IDatabase $db
357 * @param array $conditions
358 * @param int $flags (optional)
359 * @return Revision|null
361 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
362 $row = self
::fetchFromConds( $db, $conditions, $flags );
364 $rev = new Revision( $row );
365 $rev->mWiki
= $db->getDomainID();
374 * Return a wrapper for a series of database rows to
375 * fetch all of a given page's revisions in turn.
376 * Each row can be fed to the constructor to get objects.
378 * @param LinkTarget $title
379 * @return ResultWrapper
380 * @deprecated Since 1.28
382 public static function fetchRevision( LinkTarget
$title ) {
383 $row = self
::fetchFromConds(
384 wfGetDB( DB_REPLICA
),
386 'rev_id=page_latest',
387 'page_namespace' => $title->getNamespace(),
388 'page_title' => $title->getDBkey()
392 return new FakeResultWrapper( $row ?
[ $row ] : [] );
396 * Given a set of conditions, return a ResultWrapper
397 * which will return matching database rows with the
398 * fields necessary to build Revision objects.
400 * @param IDatabase $db
401 * @param array $conditions
402 * @param int $flags (optional)
405 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
406 $fields = array_merge(
407 self
::selectFields(),
408 self
::selectPageFields(),
409 self
::selectUserFields()
412 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
413 $options[] = 'FOR UPDATE';
415 return $db->selectRow(
416 [ 'revision', 'page', 'user' ],
421 [ 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() ]
426 * Return the value of a select() JOIN conds array for the user table.
427 * This will get user table rows for logged-in users.
431 public static function userJoinCond() {
432 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
436 * Return the value of a select() page conds array for the page table.
437 * This will assure that the revision(s) are not orphaned from live pages.
441 public static function pageJoinCond() {
442 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
446 * Return the list of revision fields that should be selected to create
448 * @todo Deprecate this in favor of a method that returns tables and joins
449 * as well, and use CommentStore::getJoin().
452 public static function selectFields() {
453 global $wgContentHandlerUseDB;
469 $fields +
= CommentStore
::newKey( 'rev_comment' )->getFields();
471 if ( $wgContentHandlerUseDB ) {
472 $fields[] = 'rev_content_format';
473 $fields[] = 'rev_content_model';
480 * Return the list of revision fields that should be selected to create
481 * a new revision from an archive row.
482 * @todo Deprecate this in favor of a method that returns tables and joins
483 * as well, and use CommentStore::getJoin().
486 public static function selectArchiveFields() {
487 global $wgContentHandlerUseDB;
504 $fields +
= CommentStore
::newKey( 'ar_comment' )->getFields();
506 if ( $wgContentHandlerUseDB ) {
507 $fields[] = 'ar_content_format';
508 $fields[] = 'ar_content_model';
514 * Return the list of text fields that should be selected to read the
518 public static function selectTextFields() {
526 * Return the list of page fields that should be selected from page table
529 public static function selectPageFields() {
541 * Return the list of user fields that should be selected from user table
544 public static function selectUserFields() {
545 return [ 'user_name' ];
549 * Do a batched query to get the parent revision lengths
550 * @param IDatabase $db
551 * @param array $revIds
554 public static function getParentLengths( $db, array $revIds ) {
557 return $revLens; // empty
559 $res = $db->select( 'revision',
560 [ 'rev_id', 'rev_len' ],
561 [ 'rev_id' => $revIds ],
563 foreach ( $res as $row ) {
564 $revLens[$row->rev_id
] = $row->rev_len
;
570 * @param object|array $row Either a database row or an array
571 * @throws MWException
574 public function __construct( $row ) {
575 if ( is_object( $row ) ) {
576 $this->constructFromDbRowObject( $row );
577 } elseif ( is_array( $row ) ) {
578 $this->constructFromRowArray( $row );
580 throw new MWException( 'Revision constructor passed invalid row format.' );
582 $this->mUnpatrolled
= null;
588 private function constructFromDbRowObject( $row ) {
589 $this->mId
= intval( $row->rev_id
);
590 $this->mPage
= intval( $row->rev_page
);
591 $this->mTextId
= intval( $row->rev_text_id
);
592 $this->mComment
= CommentStore
::newKey( 'rev_comment' )
593 // Legacy because $row probably came from self::selectFields()
594 ->getCommentLegacy( wfGetDB( DB_REPLICA
), $row, true )->text
;
595 $this->mUser
= intval( $row->rev_user
);
596 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
597 $this->mTimestamp
= $row->rev_timestamp
;
598 $this->mDeleted
= intval( $row->rev_deleted
);
600 if ( !isset( $row->rev_parent_id
) ) {
601 $this->mParentId
= null;
603 $this->mParentId
= intval( $row->rev_parent_id
);
606 if ( !isset( $row->rev_len
) ) {
609 $this->mSize
= intval( $row->rev_len
);
612 if ( !isset( $row->rev_sha1
) ) {
615 $this->mSha1
= $row->rev_sha1
;
618 if ( isset( $row->page_latest
) ) {
619 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
620 $this->mTitle
= Title
::newFromRow( $row );
622 $this->mCurrent
= false;
623 $this->mTitle
= null;
626 if ( !isset( $row->rev_content_model
) ) {
627 $this->mContentModel
= null; # determine on demand if needed
629 $this->mContentModel
= strval( $row->rev_content_model
);
632 if ( !isset( $row->rev_content_format
) ) {
633 $this->mContentFormat
= null; # determine on demand if needed
635 $this->mContentFormat
= strval( $row->rev_content_format
);
638 // Lazy extraction...
640 if ( isset( $row->old_text
) ) {
641 $this->mTextRow
= $row;
643 // 'text' table row entry will be lazy-loaded
644 $this->mTextRow
= null;
647 // Use user_name for users and rev_user_text for IPs...
648 $this->mUserText
= null; // lazy load if left null
649 if ( $this->mUser
== 0 ) {
650 $this->mUserText
= $row->rev_user_text
; // IP user
651 } elseif ( isset( $row->user_name
) ) {
652 $this->mUserText
= $row->user_name
; // logged-in user
654 $this->mOrigUserText
= $row->rev_user_text
;
660 * @throws MWException
662 private function constructFromRowArray( array $row ) {
663 // Build a new revision to be saved...
664 global $wgUser; // ugh
666 # if we have a content object, use it to set the model and type
667 if ( !empty( $row['content'] ) ) {
668 if ( !( $row['content'] instanceof Content
) ) {
669 throw new MWException( '`content` field must contain a Content object.' );
672 // @todo when is that set? test with external store setup! check out insertOn() [dk]
673 if ( !empty( $row['text_id'] ) ) {
674 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
675 "can't serialize content object" );
678 $row['content_model'] = $row['content']->getModel();
679 # note: mContentFormat is initializes later accordingly
680 # note: content is serialized later in this method!
681 # also set text to null?
684 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
685 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
686 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
687 $this->mUserText
= isset( $row['user_text'] )
688 ?
strval( $row['user_text'] ) : $wgUser->getName();
689 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
690 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
691 $this->mTimestamp
= isset( $row['timestamp'] )
692 ?
strval( $row['timestamp'] ) : wfTimestampNow();
693 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
694 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
695 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
696 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
698 $this->mContentModel
= isset( $row['content_model'] )
699 ?
strval( $row['content_model'] ) : null;
700 $this->mContentFormat
= isset( $row['content_format'] )
701 ?
strval( $row['content_format'] ) : null;
703 // Enforce spacing trimming on supplied text
704 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
705 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
706 $this->mTextRow
= null;
708 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
710 // if we have a Content object, override mText and mContentModel
711 if ( !empty( $row['content'] ) ) {
712 $handler = $this->getContentHandler();
713 $this->mContent
= $row['content'];
715 $this->mContentModel
= $this->mContent
->getModel();
716 $this->mContentHandler
= null;
718 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
719 } elseif ( $this->mText
!== null ) {
720 $handler = $this->getContentHandler();
721 $this->mContent
= $handler->unserializeContent( $this->mText
);
724 // If we have a Title object, make sure it is consistent with mPage.
725 if ( $this->mTitle
&& $this->mTitle
->exists() ) {
726 if ( $this->mPage
=== null ) {
727 // if the page ID wasn't known, set it now
728 $this->mPage
= $this->mTitle
->getArticleID();
729 } elseif ( $this->mTitle
->getArticleID() !== $this->mPage
) {
730 // Got different page IDs. This may be legit (e.g. during undeletion),
731 // but it seems worth mentioning it in the log.
732 wfDebug( "Page ID " . $this->mPage
. " mismatches the ID " .
733 $this->mTitle
->getArticleID() . " provided by the Title object." );
737 $this->mCurrent
= false;
739 // If we still have no length, see it we have the text to figure it out
740 if ( !$this->mSize
&& $this->mContent
!== null ) {
741 $this->mSize
= $this->mContent
->getSize();
745 if ( $this->mSha1
=== null ) {
746 $this->mSha1
= $this->mText
=== null ?
null : self
::base36Sha1( $this->mText
);
750 $this->getContentModel();
751 $this->getContentFormat();
759 public function getId() {
764 * Set the revision ID
766 * This should only be used for proposed revisions that turn out to be null edits
771 public function setId( $id ) {
772 $this->mId
= (int)$id;
776 * Set the user ID/name
778 * This should only be used for proposed revisions that turn out to be null edits
781 * @param int $id User ID
782 * @param string $name User name
784 public function setUserIdAndName( $id, $name ) {
785 $this->mUser
= (int)$id;
786 $this->mUserText
= $name;
787 $this->mOrigUserText
= $name;
795 public function getTextId() {
796 return $this->mTextId
;
800 * Get parent revision ID (the original previous page revision)
804 public function getParentId() {
805 return $this->mParentId
;
809 * Returns the length of the text in this revision, or null if unknown.
813 public function getSize() {
818 * Returns the base36 sha1 of the text in this revision, or null if unknown.
820 * @return string|null
822 public function getSha1() {
827 * Returns the title of the page associated with this entry or null.
829 * Will do a query, when title is not set and id is given.
833 public function getTitle() {
834 if ( $this->mTitle
!== null ) {
835 return $this->mTitle
;
837 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
838 if ( $this->mId
!== null ) {
839 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
840 $row = $dbr->selectRow(
841 [ 'page', 'revision' ],
842 self
::selectPageFields(),
843 [ 'page_id=rev_page', 'rev_id' => $this->mId
],
847 // @TODO: better foreign title handling
848 $this->mTitle
= Title
::newFromRow( $row );
852 if ( $this->mWiki
=== false ||
$this->mWiki
=== wfWikiID() ) {
853 // Loading by ID is best, though not possible for foreign titles
854 if ( !$this->mTitle
&& $this->mPage
!== null && $this->mPage
> 0 ) {
855 $this->mTitle
= Title
::newFromID( $this->mPage
);
859 return $this->mTitle
;
863 * Set the title of the revision
865 * @param Title $title
867 public function setTitle( $title ) {
868 $this->mTitle
= $title;
876 public function getPage() {
881 * Fetch revision's user id if it's available to the specified audience.
882 * If the specified audience does not have access to it, zero will be
885 * @param int $audience One of:
886 * Revision::FOR_PUBLIC to be displayed to all users
887 * Revision::FOR_THIS_USER to be displayed to the given user
888 * Revision::RAW get the ID regardless of permissions
889 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
890 * to the $audience parameter
893 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
894 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
896 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
904 * Fetch revision's user id without regard for the current user's permissions
907 * @deprecated since 1.25, use getUser( Revision::RAW )
909 public function getRawUser() {
910 wfDeprecated( __METHOD__
, '1.25' );
911 return $this->getUser( self
::RAW
);
915 * Fetch revision's username if it's available to the specified audience.
916 * If the specified audience does not have access to the username, an
917 * empty string will be returned.
919 * @param int $audience One of:
920 * Revision::FOR_PUBLIC to be displayed to all users
921 * Revision::FOR_THIS_USER to be displayed to the given user
922 * Revision::RAW get the text regardless of permissions
923 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
924 * to the $audience parameter
927 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
928 $this->loadMutableFields();
930 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
932 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
935 if ( $this->mUserText
=== null ) {
936 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
937 if ( $this->mUserText
=== false ) {
938 # This shouldn't happen, but it can if the wiki was recovered
939 # via importing revs and there is no user table entry yet.
940 $this->mUserText
= $this->mOrigUserText
;
943 return $this->mUserText
;
948 * Fetch revision's username without regard for view restrictions
951 * @deprecated since 1.25, use getUserText( Revision::RAW )
953 public function getRawUserText() {
954 wfDeprecated( __METHOD__
, '1.25' );
955 return $this->getUserText( self
::RAW
);
959 * Fetch revision comment if it's available to the specified audience.
960 * If the specified audience does not have access to the comment, an
961 * empty string will be returned.
963 * @param int $audience One of:
964 * Revision::FOR_PUBLIC to be displayed to all users
965 * Revision::FOR_THIS_USER to be displayed to the given user
966 * Revision::RAW get the text regardless of permissions
967 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
968 * to the $audience parameter
971 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
972 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
974 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
977 return $this->mComment
;
982 * Fetch revision comment without regard for the current user's permissions
985 * @deprecated since 1.25, use getComment( Revision::RAW )
987 public function getRawComment() {
988 wfDeprecated( __METHOD__
, '1.25' );
989 return $this->getComment( self
::RAW
);
995 public function isMinor() {
996 return (bool)$this->mMinorEdit
;
1000 * @return int Rcid of the unpatrolled row, zero if there isn't one
1002 public function isUnpatrolled() {
1003 if ( $this->mUnpatrolled
!== null ) {
1004 return $this->mUnpatrolled
;
1006 $rc = $this->getRecentChange();
1007 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
1008 $this->mUnpatrolled
= $rc->getAttribute( 'rc_id' );
1010 $this->mUnpatrolled
= 0;
1012 return $this->mUnpatrolled
;
1016 * Get the RC object belonging to the current revision, if there's one
1018 * @param int $flags (optional) $flags include:
1019 * Revision::READ_LATEST : Select the data from the master
1022 * @return RecentChange|null
1024 public function getRecentChange( $flags = 0 ) {
1025 $dbr = wfGetDB( DB_REPLICA
);
1027 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
1029 return RecentChange
::newFromConds(
1031 'rc_user_text' => $this->getUserText( self
::RAW
),
1032 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
1033 'rc_this_oldid' => $this->getId()
1041 * @param int $field One of DELETED_* bitfield constants
1045 public function isDeleted( $field ) {
1046 if ( $this->isCurrent() && $field === self
::DELETED_TEXT
) {
1047 // Current revisions of pages cannot have the content hidden. Skipping this
1048 // check is very useful for Parser as it fetches templates using newKnownCurrent().
1049 // Calling getVisibility() in that case triggers a verification database query.
1050 return false; // no need to check
1053 return ( $this->getVisibility() & $field ) == $field;
1057 * Get the deletion bitfield of the revision
1061 public function getVisibility() {
1062 $this->loadMutableFields();
1064 return (int)$this->mDeleted
;
1068 * Fetch revision content if it's available to the specified audience.
1069 * If the specified audience does not have the ability to view this
1070 * revision, null will be returned.
1072 * @param int $audience One of:
1073 * Revision::FOR_PUBLIC to be displayed to all users
1074 * Revision::FOR_THIS_USER to be displayed to $wgUser
1075 * Revision::RAW get the text regardless of permissions
1076 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1077 * to the $audience parameter
1079 * @return Content|null
1081 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1082 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
1084 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
1087 return $this->getContentInternal();
1092 * Get original serialized data (without checking view restrictions)
1097 public function getSerializedData() {
1098 if ( $this->mText
=== null ) {
1099 // Revision is immutable. Load on demand.
1100 $this->mText
= $this->loadText();
1103 return $this->mText
;
1107 * Gets the content object for the revision (or null on failure).
1109 * Note that for mutable Content objects, each call to this method will return a
1113 * @return Content|null The Revision's content, or null on failure.
1115 protected function getContentInternal() {
1116 if ( $this->mContent
=== null ) {
1117 $text = $this->getSerializedData();
1119 if ( $text !== null && $text !== false ) {
1120 // Unserialize content
1121 $handler = $this->getContentHandler();
1122 $format = $this->getContentFormat();
1124 $this->mContent
= $handler->unserializeContent( $text, $format );
1128 // NOTE: copy() will return $this for immutable content objects
1129 return $this->mContent ?
$this->mContent
->copy() : null;
1133 * Returns the content model for this revision.
1135 * If no content model was stored in the database, the default content model for the title is
1136 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1137 * is used as a last resort.
1139 * @return string The content model id associated with this revision,
1140 * see the CONTENT_MODEL_XXX constants.
1142 public function getContentModel() {
1143 if ( !$this->mContentModel
) {
1144 $title = $this->getTitle();
1146 $this->mContentModel
= ContentHandler
::getDefaultModelFor( $title );
1148 $this->mContentModel
= CONTENT_MODEL_WIKITEXT
;
1151 assert( !empty( $this->mContentModel
) );
1154 return $this->mContentModel
;
1158 * Returns the content format for this revision.
1160 * If no content format was stored in the database, the default format for this
1161 * revision's content model is returned.
1163 * @return string The content format id associated with this revision,
1164 * see the CONTENT_FORMAT_XXX constants.
1166 public function getContentFormat() {
1167 if ( !$this->mContentFormat
) {
1168 $handler = $this->getContentHandler();
1169 $this->mContentFormat
= $handler->getDefaultFormat();
1171 assert( !empty( $this->mContentFormat
) );
1174 return $this->mContentFormat
;
1178 * Returns the content handler appropriate for this revision's content model.
1180 * @throws MWException
1181 * @return ContentHandler
1183 public function getContentHandler() {
1184 if ( !$this->mContentHandler
) {
1185 $model = $this->getContentModel();
1186 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1188 $format = $this->getContentFormat();
1190 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1191 throw new MWException( "Oops, the content format $format is not supported for "
1192 . "this content model, $model" );
1196 return $this->mContentHandler
;
1202 public function getTimestamp() {
1203 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1209 public function isCurrent() {
1210 return $this->mCurrent
;
1214 * Get previous revision for this title
1216 * @return Revision|null
1218 public function getPrevious() {
1219 if ( $this->getTitle() ) {
1220 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1222 return self
::newFromTitle( $this->getTitle(), $prev );
1229 * Get next revision for this title
1231 * @return Revision|null
1233 public function getNext() {
1234 if ( $this->getTitle() ) {
1235 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1237 return self
::newFromTitle( $this->getTitle(), $next );
1244 * Get previous revision Id for this page_id
1245 * This is used to populate rev_parent_id on save
1247 * @param IDatabase $db
1250 private function getPreviousRevisionId( $db ) {
1251 if ( $this->mPage
=== null ) {
1254 # Use page_latest if ID is not given
1255 if ( !$this->mId
) {
1256 $prevId = $db->selectField( 'page', 'page_latest',
1257 [ 'page_id' => $this->mPage
],
1260 $prevId = $db->selectField( 'revision', 'rev_id',
1261 [ 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
],
1263 [ 'ORDER BY' => 'rev_id DESC' ] );
1265 return intval( $prevId );
1269 * Get revision text associated with an old or archive row
1271 * Both the flags and the text field must be included. Including the old_id
1272 * field will activate cache usage as long as the $wiki parameter is not set.
1274 * @param stdClass $row The text data
1275 * @param string $prefix Table prefix (default 'old_')
1276 * @param string|bool $wiki The name of the wiki to load the revision text from
1277 * (same as the the wiki $row was loaded from) or false to indicate the local
1278 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1279 * identifier as understood by the LoadBalancer class.
1280 * @return string|false Text the text requested or false on failure
1282 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1283 $textField = $prefix . 'text';
1284 $flagsField = $prefix . 'flags';
1286 if ( isset( $row->$flagsField ) ) {
1287 $flags = explode( ',', $row->$flagsField );
1292 if ( isset( $row->$textField ) ) {
1293 $text = $row->$textField;
1298 // Use external methods for external objects, text in table is URL-only then
1299 if ( in_array( 'external', $flags ) ) {
1301 $parts = explode( '://', $url, 2 );
1302 if ( count( $parts ) == 1 ||
$parts[1] == '' ) {
1306 if ( isset( $row->old_id
) && $wiki === false ) {
1307 // Make use of the wiki-local revision text cache
1308 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1309 // The cached value should be decompressed, so handle that and return here
1310 return $cache->getWithSetCallback(
1311 $cache->makeKey( 'revisiontext', 'textid', $row->old_id
),
1312 self
::getCacheTTL( $cache ),
1313 function () use ( $url, $wiki, $flags ) {
1314 // No negative caching per Revision::loadText()
1315 $text = ExternalStore
::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1317 return self
::decompressRevisionText( $text, $flags );
1319 [ 'pcGroup' => self
::TEXT_CACHE_GROUP
, 'pcTTL' => $cache::TTL_PROC_LONG
]
1322 $text = ExternalStore
::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1326 return self
::decompressRevisionText( $text, $flags );
1330 * If $wgCompressRevisions is enabled, we will compress data.
1331 * The input string is modified in place.
1332 * Return value is the flags field: contains 'gzip' if the
1333 * data is compressed, and 'utf-8' if we're saving in UTF-8
1336 * @param mixed &$text Reference to a text
1339 public static function compressRevisionText( &$text ) {
1340 global $wgCompressRevisions;
1343 # Revisions not marked this way will be converted
1344 # on load if $wgLegacyCharset is set in the future.
1347 if ( $wgCompressRevisions ) {
1348 if ( function_exists( 'gzdeflate' ) ) {
1349 $deflated = gzdeflate( $text );
1351 if ( $deflated === false ) {
1352 wfLogWarning( __METHOD__
. ': gzdeflate() failed' );
1358 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1361 return implode( ',', $flags );
1365 * Re-converts revision text according to it's flags.
1367 * @param mixed $text Reference to a text
1368 * @param array $flags Compression flags
1369 * @return string|bool Decompressed text, or false on failure
1371 public static function decompressRevisionText( $text, $flags ) {
1372 global $wgLegacyEncoding, $wgContLang;
1374 if ( $text === false ) {
1375 // Text failed to be fetched; nothing to do
1379 if ( in_array( 'gzip', $flags ) ) {
1380 # Deal with optional compression of archived pages.
1381 # This can be done periodically via maintenance/compressOld.php, and
1382 # as pages are saved if $wgCompressRevisions is set.
1383 $text = gzinflate( $text );
1385 if ( $text === false ) {
1386 wfLogWarning( __METHOD__
. ': gzinflate() failed' );
1391 if ( in_array( 'object', $flags ) ) {
1392 # Generic compressed storage
1393 $obj = unserialize( $text );
1394 if ( !is_object( $obj ) ) {
1398 $text = $obj->getText();
1401 if ( $text !== false && $wgLegacyEncoding
1402 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags )
1404 # Old revisions kept around in a legacy encoding?
1405 # Upconvert on demand.
1406 # ("utf8" checked for compatibility with some broken
1407 # conversion scripts 2008-12-30)
1408 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1415 * Insert a new revision into the database, returning the new revision ID
1416 * number on success and dies horribly on failure.
1418 * @param IDatabase $dbw (master connection)
1419 * @throws MWException
1420 * @return int The revision ID
1422 public function insertOn( $dbw ) {
1423 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1425 // We're inserting a new revision, so we have to use master anyway.
1426 // If it's a null revision, it may have references to rows that
1427 // are not in the replica yet (the text row).
1428 $this->mQueryFlags |
= self
::READ_LATEST
;
1430 // Not allowed to have rev_page equal to 0, false, etc.
1431 if ( !$this->mPage
) {
1432 $title = $this->getTitle();
1433 if ( $title instanceof Title
) {
1434 $titleText = ' for page ' . $title->getPrefixedText();
1438 throw new MWException( "Cannot insert revision$titleText: page ID must be nonzero" );
1441 $this->checkContentModel();
1443 $data = $this->mText
;
1444 $flags = self
::compressRevisionText( $data );
1446 # Write to external storage if required
1447 if ( $wgDefaultExternalStore ) {
1448 // Store and get the URL
1449 $data = ExternalStore
::insertToDefault( $data );
1451 throw new MWException( "Unable to store text to external storage" );
1456 $flags .= 'external';
1459 # Record the text (or external storage URL) to the text table
1460 if ( $this->mTextId
=== null ) {
1461 $dbw->insert( 'text',
1463 'old_text' => $data,
1464 'old_flags' => $flags,
1467 $this->mTextId
= $dbw->insertId();
1470 if ( $this->mComment
=== null ) {
1471 $this->mComment
= "";
1474 # Record the edit in revisions
1476 'rev_page' => $this->mPage
,
1477 'rev_text_id' => $this->mTextId
,
1478 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1479 'rev_user' => $this->mUser
,
1480 'rev_user_text' => $this->mUserText
,
1481 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1482 'rev_deleted' => $this->mDeleted
,
1483 'rev_len' => $this->mSize
,
1484 'rev_parent_id' => $this->mParentId
=== null
1485 ?
$this->getPreviousRevisionId( $dbw )
1487 'rev_sha1' => $this->mSha1
=== null
1488 ? self
::base36Sha1( $this->mText
)
1491 if ( $this->mId
!== null ) {
1492 $row['rev_id'] = $this->mId
;
1495 list( $commentFields, $commentCallback ) =
1496 CommentStore
::newKey( 'rev_comment' )->insertWithTempTable( $dbw, $this->mComment
);
1497 $row +
= $commentFields;
1499 if ( $wgContentHandlerUseDB ) {
1500 // NOTE: Store null for the default model and format, to save space.
1501 // XXX: Makes the DB sensitive to changed defaults.
1502 // Make this behavior optional? Only in miser mode?
1504 $model = $this->getContentModel();
1505 $format = $this->getContentFormat();
1507 $title = $this->getTitle();
1509 if ( $title === null ) {
1510 throw new MWException( "Insufficient information to determine the title of the "
1511 . "revision's page!" );
1514 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1515 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1517 $row['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
1518 $row['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
1521 $dbw->insert( 'revision', $row, __METHOD__
);
1523 if ( $this->mId
=== null ) {
1524 // Only if auto-increment was used
1525 $this->mId
= $dbw->insertId();
1527 $commentCallback( $this->mId
);
1529 // Assertion to try to catch T92046
1530 if ( (int)$this->mId
=== 0 ) {
1531 throw new UnexpectedValueException(
1532 'After insert, Revision mId is ' . var_export( $this->mId
, 1 ) . ': ' .
1533 var_export( $row, 1 )
1537 // Insert IP revision into ip_changes for use when querying for a range.
1538 if ( $this->mUser
=== 0 && IP
::isValid( $this->mUserText
) ) {
1540 'ipc_rev_id' => $this->mId
,
1541 'ipc_rev_timestamp' => $row['rev_timestamp'],
1542 'ipc_hex' => IP
::toHex( $row['rev_user_text'] ),
1544 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__
);
1547 // Avoid PHP 7.1 warning of passing $this by reference
1549 Hooks
::run( 'RevisionInsertComplete', [ &$revision, $data, $flags ] );
1554 protected function checkContentModel() {
1555 global $wgContentHandlerUseDB;
1557 // Note: may return null for revisions that have not yet been inserted
1558 $title = $this->getTitle();
1560 $model = $this->getContentModel();
1561 $format = $this->getContentFormat();
1562 $handler = $this->getContentHandler();
1564 if ( !$handler->isSupportedFormat( $format ) ) {
1565 $t = $title->getPrefixedDBkey();
1567 throw new MWException( "Can't use format $format with content model $model on $t" );
1570 if ( !$wgContentHandlerUseDB && $title ) {
1571 // if $wgContentHandlerUseDB is not set,
1572 // all revisions must use the default content model and format.
1574 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1575 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1576 $defaultFormat = $defaultHandler->getDefaultFormat();
1578 if ( $this->getContentModel() != $defaultModel ) {
1579 $t = $title->getPrefixedDBkey();
1581 throw new MWException( "Can't save non-default content model with "
1582 . "\$wgContentHandlerUseDB disabled: model is $model, "
1583 . "default for $t is $defaultModel" );
1586 if ( $this->getContentFormat() != $defaultFormat ) {
1587 $t = $title->getPrefixedDBkey();
1589 throw new MWException( "Can't use non-default content format with "
1590 . "\$wgContentHandlerUseDB disabled: format is $format, "
1591 . "default for $t is $defaultFormat" );
1595 $content = $this->getContent( self
::RAW
);
1596 $prefixedDBkey = $title->getPrefixedDBkey();
1597 $revId = $this->mId
;
1600 throw new MWException(
1601 "Content of revision $revId ($prefixedDBkey) could not be loaded for validation!"
1604 if ( !$content->isValid() ) {
1605 throw new MWException(
1606 "Content of revision $revId ($prefixedDBkey) is not valid! Content model is $model"
1612 * Get the base 36 SHA-1 value for a string of text
1613 * @param string $text
1616 public static function base36Sha1( $text ) {
1617 return Wikimedia\base_convert
( sha1( $text ), 16, 36, 31 );
1621 * Get the text cache TTL
1623 * @param WANObjectCache $cache
1626 private static function getCacheTTL( WANObjectCache
$cache ) {
1627 global $wgRevisionCacheExpiry;
1629 if ( $cache->getQoS( $cache::ATTR_EMULATION
) <= $cache::QOS_EMULATION_SQL
) {
1630 // Do not cache RDBMs blobs in...the RDBMs store
1631 $ttl = $cache::TTL_UNCACHEABLE
;
1633 $ttl = $wgRevisionCacheExpiry ?
: $cache::TTL_UNCACHEABLE
;
1640 * Lazy-load the revision's text.
1641 * Currently hardcoded to the 'text' table storage engine.
1643 * @return string|bool The revision's text, or false on failure
1645 private function loadText() {
1646 $cache = ObjectCache
::getMainWANInstance();
1648 // No negative caching; negative hits on text rows may be due to corrupted replica DBs
1649 return $cache->getWithSetCallback(
1650 $cache->makeKey( 'revisiontext', 'textid', $this->getTextId() ),
1651 self
::getCacheTTL( $cache ),
1653 return $this->fetchText();
1655 [ 'pcGroup' => self
::TEXT_CACHE_GROUP
, 'pcTTL' => $cache::TTL_PROC_LONG
]
1659 private function fetchText() {
1660 $textId = $this->getTextId();
1662 // If we kept data for lazy extraction, use it now...
1663 if ( $this->mTextRow
!== null ) {
1664 $row = $this->mTextRow
;
1665 $this->mTextRow
= null;
1670 // Callers doing updates will pass in READ_LATEST as usual. Since the text/blob tables
1671 // do not normally get rows changed around, set READ_LATEST_IMMUTABLE in those cases.
1672 $flags = $this->mQueryFlags
;
1673 $flags |
= DBAccessObjectUtils
::hasFlags( $flags, self
::READ_LATEST
)
1674 ? self
::READ_LATEST_IMMUTABLE
1677 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
1678 DBAccessObjectUtils
::getDBOptions( $flags );
1681 // Text data is immutable; check replica DBs first.
1682 $row = wfGetDB( $index )->selectRow(
1684 [ 'old_text', 'old_flags' ],
1685 [ 'old_id' => $textId ],
1691 // Fallback to DB_MASTER in some cases if the row was not found
1692 if ( !$row && $fallbackIndex !== null ) {
1693 // Use FOR UPDATE if it was used to fetch this revision. This avoids missing the row
1694 // due to REPEATABLE-READ. Also fallback to the master if READ_LATEST is provided.
1695 $row = wfGetDB( $fallbackIndex )->selectRow(
1697 [ 'old_text', 'old_flags' ],
1698 [ 'old_id' => $textId ],
1705 wfDebugLog( 'Revision', "No text row with ID '$textId' (revision {$this->getId()})." );
1708 $text = self
::getRevisionText( $row );
1709 if ( $row && $text === false ) {
1710 wfDebugLog( 'Revision', "No blob for text row '$textId' (revision {$this->getId()})." );
1713 return is_string( $text ) ?
$text : false;
1717 * Create a new null-revision for insertion into a page's
1718 * history. This will not re-save the text, but simply refer
1719 * to the text from the previous version.
1721 * Such revisions can for instance identify page rename
1722 * operations and other such meta-modifications.
1724 * @param IDatabase $dbw
1725 * @param int $pageId ID number of the page to read from
1726 * @param string $summary Revision's summary
1727 * @param bool $minor Whether the revision should be considered as minor
1728 * @param User|null $user User object to use or null for $wgUser
1729 * @return Revision|null Revision or null on error
1731 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1732 global $wgContentHandlerUseDB;
1734 $fields = [ 'page_latest', 'page_namespace', 'page_title',
1735 'rev_text_id', 'rev_len', 'rev_sha1' ];
1737 if ( $wgContentHandlerUseDB ) {
1738 $fields[] = 'rev_content_model';
1739 $fields[] = 'rev_content_format';
1742 $current = $dbw->selectRow(
1743 [ 'page', 'revision' ],
1746 'page_id' => $pageId,
1747 'page_latest=rev_id',
1750 [ 'FOR UPDATE' ] // T51581
1761 'user_text' => $user->getName(),
1762 'user' => $user->getId(),
1763 'comment' => $summary,
1764 'minor_edit' => $minor,
1765 'text_id' => $current->rev_text_id
,
1766 'parent_id' => $current->page_latest
,
1767 'len' => $current->rev_len
,
1768 'sha1' => $current->rev_sha1
1771 if ( $wgContentHandlerUseDB ) {
1772 $row['content_model'] = $current->rev_content_model
;
1773 $row['content_format'] = $current->rev_content_format
;
1776 $row['title'] = Title
::makeTitle( $current->page_namespace
, $current->page_title
);
1778 $revision = new Revision( $row );
1787 * Determine if the current user is allowed to view a particular
1788 * field of this revision, if it's marked as deleted.
1790 * @param int $field One of self::DELETED_TEXT,
1791 * self::DELETED_COMMENT,
1792 * self::DELETED_USER
1793 * @param User|null $user User object to check, or null to use $wgUser
1796 public function userCan( $field, User
$user = null ) {
1797 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1801 * Determine if the current user is allowed to view a particular
1802 * field of this revision, if it's marked as deleted. This is used
1803 * by various classes to avoid duplication.
1805 * @param int $bitfield Current field
1806 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1807 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1808 * self::DELETED_USER = File::DELETED_USER
1809 * @param User|null $user User object to check, or null to use $wgUser
1810 * @param Title|null $title A Title object to check for per-page restrictions on,
1811 * instead of just plain userrights
1814 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1817 if ( $bitfield & $field ) { // aspect is deleted
1818 if ( $user === null ) {
1822 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1823 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
1824 } elseif ( $field & self
::DELETED_TEXT
) {
1825 $permissions = [ 'deletedtext' ];
1827 $permissions = [ 'deletedhistory' ];
1829 $permissionlist = implode( ', ', $permissions );
1830 if ( $title === null ) {
1831 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
1832 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
1834 $text = $title->getPrefixedText();
1835 wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" );
1836 foreach ( $permissions as $perm ) {
1837 if ( $title->userCan( $perm, $user ) ) {
1849 * Get rev_timestamp from rev_id, without loading the rest of the row
1851 * @param Title $title
1854 * @return string|bool False if not found
1856 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1857 $db = ( $flags & self
::READ_LATEST
)
1858 ?
wfGetDB( DB_MASTER
)
1859 : wfGetDB( DB_REPLICA
);
1860 // Casting fix for databases that can't take '' for rev_id
1864 $conds = [ 'rev_id' => $id ];
1865 $conds['rev_page'] = $title->getArticleID();
1866 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1868 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
1872 * Get count of revisions per page...not very efficient
1874 * @param IDatabase $db
1875 * @param int $id Page id
1878 static function countByPageId( $db, $id ) {
1879 $row = $db->selectRow( 'revision', [ 'revCount' => 'COUNT(*)' ],
1880 [ 'rev_page' => $id ], __METHOD__
);
1882 return $row->revCount
;
1888 * Get count of revisions per page...not very efficient
1890 * @param IDatabase $db
1891 * @param Title $title
1894 static function countByTitle( $db, $title ) {
1895 $id = $title->getArticleID();
1897 return self
::countByPageId( $db, $id );
1903 * Check if no edits were made by other users since
1904 * the time a user started editing the page. Limit to
1905 * 50 revisions for the sake of performance.
1908 * @deprecated since 1.24
1910 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1911 * Database object or a database identifier usable with wfGetDB.
1912 * @param int $pageId The ID of the page in question
1913 * @param int $userId The ID of the user in question
1914 * @param string $since Look at edits since this time
1916 * @return bool True if the given user was the only one to edit since the given timestamp
1918 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1923 if ( is_int( $db ) ) {
1924 $db = wfGetDB( $db );
1927 $res = $db->select( 'revision',
1930 'rev_page' => $pageId,
1931 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1934 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ] );
1935 foreach ( $res as $row ) {
1936 if ( $row->rev_user
!= $userId ) {
1944 * Load a revision based on a known page ID and current revision ID from the DB
1946 * This method allows for the use of caching, though accessing anything that normally
1947 * requires permission checks (aside from the text) will trigger a small DB lookup.
1948 * The title will also be lazy loaded, though setTitle() can be used to preload it.
1950 * @param IDatabase $db
1951 * @param int $pageId Page ID
1952 * @param int $revId Known current revision of this page
1953 * @return Revision|bool Returns false if missing
1956 public static function newKnownCurrent( IDatabase
$db, $pageId, $revId ) {
1957 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1958 return $cache->getWithSetCallback(
1959 // Page/rev IDs passed in from DB to reflect history merges
1960 $cache->makeGlobalKey( 'revision', $db->getDomainID(), $pageId, $revId ),
1962 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1963 $setOpts +
= Database
::getCacheSetOptions( $db );
1965 $rev = Revision
::loadFromPageId( $db, $pageId, $revId );
1966 // Reflect revision deletion and user renames
1968 $rev->mTitle
= null; // mutable; lazy-load
1969 $rev->mRefreshMutableFields
= true;
1972 return $rev ?
: false; // don't cache negatives
1978 * For cached revisions, make sure the user name and rev_deleted is up-to-date
1980 private function loadMutableFields() {
1981 if ( !$this->mRefreshMutableFields
) {
1982 return; // not needed
1985 $this->mRefreshMutableFields
= false;
1986 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
1987 $row = $dbr->selectRow(
1988 [ 'revision', 'user' ],
1989 [ 'rev_deleted', 'user_name' ],
1990 [ 'rev_id' => $this->mId
, 'user_id = rev_user' ],
1993 if ( $row ) { // update values
1994 $this->mDeleted
= (int)$row->rev_deleted
;
1995 $this->mUserText
= $row->user_name
;