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' => $row->ar_comment
,
196 'user' => $row->ar_user
,
197 'user_text' => $row->ar_user_text
,
198 'timestamp' => $row->ar_timestamp
,
199 'minor_edit' => $row->ar_minor_edit
,
200 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
201 'deleted' => $row->ar_deleted
,
202 'len' => $row->ar_len
,
203 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
204 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
205 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
208 if ( !$wgContentHandlerUseDB ) {
209 unset( $attribs['content_model'] );
210 unset( $attribs['content_format'] );
213 if ( !isset( $attribs['title'] )
214 && isset( $row->ar_namespace
)
215 && isset( $row->ar_title
)
217 $attribs['title'] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
220 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
221 // Pre-1.5 ar_text row
222 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
223 if ( $attribs['text'] === false ) {
224 throw new MWException( 'Unable to load text from archive row (possibly T24624)' );
227 return new self( $attribs );
236 public static function newFromRow( $row ) {
237 return new self( $row );
241 * Load a page revision from a given revision ID number.
242 * Returns null if no such revision can be found.
244 * @param IDatabase $db
246 * @return Revision|null
248 public static function loadFromId( $db, $id ) {
249 return self
::loadFromConds( $db, [ 'rev_id' => intval( $id ) ] );
253 * Load either the current, or a specified, revision
254 * that's attached to a given page. If not attached
255 * to that page, will return null.
257 * @param IDatabase $db
260 * @return Revision|null
262 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
263 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
265 $conds['rev_id'] = intval( $id );
267 $conds[] = 'rev_id=page_latest';
269 return self
::loadFromConds( $db, $conds );
273 * Load either the current, or a specified, revision
274 * that's attached to a given page. If not attached
275 * to that page, will return null.
277 * @param IDatabase $db
278 * @param Title $title
280 * @return Revision|null
282 public static function loadFromTitle( $db, $title, $id = 0 ) {
284 $matchId = intval( $id );
286 $matchId = 'page_latest';
288 return self
::loadFromConds( $db,
291 'page_namespace' => $title->getNamespace(),
292 'page_title' => $title->getDBkey()
298 * Load the revision for the given title with the given timestamp.
299 * WARNING: Timestamps may in some circumstances not be unique,
300 * so this isn't the best key to use.
302 * @param IDatabase $db
303 * @param Title $title
304 * @param string $timestamp
305 * @return Revision|null
307 public static function loadFromTimestamp( $db, $title, $timestamp ) {
308 return self
::loadFromConds( $db,
310 'rev_timestamp' => $db->timestamp( $timestamp ),
311 'page_namespace' => $title->getNamespace(),
312 'page_title' => $title->getDBkey()
318 * Given a set of conditions, fetch a revision
320 * This method is used then a revision ID is qualified and
321 * will incorporate some basic replica DB/master fallback logic
323 * @param array $conditions
324 * @param int $flags (optional)
325 * @return Revision|null
327 private static function newFromConds( $conditions, $flags = 0 ) {
328 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
330 $rev = self
::loadFromConds( $db, $conditions, $flags );
331 // Make sure new pending/committed revision are visibile later on
332 // within web requests to certain avoid bugs like T93866 and T94407.
334 && !( $flags & self
::READ_LATEST
)
335 && wfGetLB()->getServerCount() > 1
336 && wfGetLB()->hasOrMadeRecentMasterChanges()
338 $flags = self
::READ_LATEST
;
339 $db = wfGetDB( DB_MASTER
);
340 $rev = self
::loadFromConds( $db, $conditions, $flags );
344 $rev->mQueryFlags
= $flags;
351 * Given a set of conditions, fetch a revision from
352 * the given database connection.
354 * @param IDatabase $db
355 * @param array $conditions
356 * @param int $flags (optional)
357 * @return Revision|null
359 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
360 $row = self
::fetchFromConds( $db, $conditions, $flags );
362 $rev = new Revision( $row );
363 $rev->mWiki
= $db->getWikiID();
372 * Return a wrapper for a series of database rows to
373 * fetch all of a given page's revisions in turn.
374 * Each row can be fed to the constructor to get objects.
376 * @param LinkTarget $title
377 * @return ResultWrapper
378 * @deprecated Since 1.28
380 public static function fetchRevision( LinkTarget
$title ) {
381 $row = self
::fetchFromConds(
382 wfGetDB( DB_REPLICA
),
384 'rev_id=page_latest',
385 'page_namespace' => $title->getNamespace(),
386 'page_title' => $title->getDBkey()
390 return new FakeResultWrapper( $row ?
[ $row ] : [] );
394 * Given a set of conditions, return a ResultWrapper
395 * which will return matching database rows with the
396 * fields necessary to build Revision objects.
398 * @param IDatabase $db
399 * @param array $conditions
400 * @param int $flags (optional)
403 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
404 $fields = array_merge(
405 self
::selectFields(),
406 self
::selectPageFields(),
407 self
::selectUserFields()
410 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
411 $options[] = 'FOR UPDATE';
413 return $db->selectRow(
414 [ 'revision', 'page', 'user' ],
419 [ 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() ]
424 * Return the value of a select() JOIN conds array for the user table.
425 * This will get user table rows for logged-in users.
429 public static function userJoinCond() {
430 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
434 * Return the value of a select() page conds array for the page table.
435 * This will assure that the revision(s) are not orphaned from live pages.
439 public static function pageJoinCond() {
440 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
444 * Return the list of revision fields that should be selected to create
448 public static function selectFields() {
449 global $wgContentHandlerUseDB;
466 if ( $wgContentHandlerUseDB ) {
467 $fields[] = 'rev_content_format';
468 $fields[] = 'rev_content_model';
475 * Return the list of revision fields that should be selected to create
476 * a new revision from an archive row.
479 public static function selectArchiveFields() {
480 global $wgContentHandlerUseDB;
498 if ( $wgContentHandlerUseDB ) {
499 $fields[] = 'ar_content_format';
500 $fields[] = 'ar_content_model';
506 * Return the list of text fields that should be selected to read the
510 public static function selectTextFields() {
518 * Return the list of page fields that should be selected from page table
521 public static function selectPageFields() {
533 * Return the list of user fields that should be selected from user table
536 public static function selectUserFields() {
537 return [ 'user_name' ];
541 * Do a batched query to get the parent revision lengths
542 * @param IDatabase $db
543 * @param array $revIds
546 public static function getParentLengths( $db, array $revIds ) {
549 return $revLens; // empty
551 $res = $db->select( 'revision',
552 [ 'rev_id', 'rev_len' ],
553 [ 'rev_id' => $revIds ],
555 foreach ( $res as $row ) {
556 $revLens[$row->rev_id
] = $row->rev_len
;
564 * @param object|array $row Either a database row or an array
565 * @throws MWException
568 function __construct( $row ) {
569 if ( is_object( $row ) ) {
570 $this->mId
= intval( $row->rev_id
);
571 $this->mPage
= intval( $row->rev_page
);
572 $this->mTextId
= intval( $row->rev_text_id
);
573 $this->mComment
= $row->rev_comment
;
574 $this->mUser
= intval( $row->rev_user
);
575 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
576 $this->mTimestamp
= $row->rev_timestamp
;
577 $this->mDeleted
= intval( $row->rev_deleted
);
579 if ( !isset( $row->rev_parent_id
) ) {
580 $this->mParentId
= null;
582 $this->mParentId
= intval( $row->rev_parent_id
);
585 if ( !isset( $row->rev_len
) ) {
588 $this->mSize
= intval( $row->rev_len
);
591 if ( !isset( $row->rev_sha1
) ) {
594 $this->mSha1
= $row->rev_sha1
;
597 if ( isset( $row->page_latest
) ) {
598 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
599 $this->mTitle
= Title
::newFromRow( $row );
601 $this->mCurrent
= false;
602 $this->mTitle
= null;
605 if ( !isset( $row->rev_content_model
) ) {
606 $this->mContentModel
= null; # determine on demand if needed
608 $this->mContentModel
= strval( $row->rev_content_model
);
611 if ( !isset( $row->rev_content_format
) ) {
612 $this->mContentFormat
= null; # determine on demand if needed
614 $this->mContentFormat
= strval( $row->rev_content_format
);
617 // Lazy extraction...
619 if ( isset( $row->old_text
) ) {
620 $this->mTextRow
= $row;
622 // 'text' table row entry will be lazy-loaded
623 $this->mTextRow
= null;
626 // Use user_name for users and rev_user_text for IPs...
627 $this->mUserText
= null; // lazy load if left null
628 if ( $this->mUser
== 0 ) {
629 $this->mUserText
= $row->rev_user_text
; // IP user
630 } elseif ( isset( $row->user_name
) ) {
631 $this->mUserText
= $row->user_name
; // logged-in user
633 $this->mOrigUserText
= $row->rev_user_text
;
634 } elseif ( is_array( $row ) ) {
635 // Build a new revision to be saved...
636 global $wgUser; // ugh
638 # if we have a content object, use it to set the model and type
639 if ( !empty( $row['content'] ) ) {
640 // @todo when is that set? test with external store setup! check out insertOn() [dk]
641 if ( !empty( $row['text_id'] ) ) {
642 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
643 "can't serialize content object" );
646 $row['content_model'] = $row['content']->getModel();
647 # note: mContentFormat is initializes later accordingly
648 # note: content is serialized later in this method!
649 # also set text to null?
652 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
653 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
654 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
655 $this->mUserText
= isset( $row['user_text'] )
656 ?
strval( $row['user_text'] ) : $wgUser->getName();
657 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
658 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
659 $this->mTimestamp
= isset( $row['timestamp'] )
660 ?
strval( $row['timestamp'] ) : wfTimestampNow();
661 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
662 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
663 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
664 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
666 $this->mContentModel
= isset( $row['content_model'] )
667 ?
strval( $row['content_model'] ) : null;
668 $this->mContentFormat
= isset( $row['content_format'] )
669 ?
strval( $row['content_format'] ) : null;
671 // Enforce spacing trimming on supplied text
672 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
673 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
674 $this->mTextRow
= null;
676 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
678 // if we have a Content object, override mText and mContentModel
679 if ( !empty( $row['content'] ) ) {
680 if ( !( $row['content'] instanceof Content
) ) {
681 throw new MWException( '`content` field must contain a Content object.' );
684 $handler = $this->getContentHandler();
685 $this->mContent
= $row['content'];
687 $this->mContentModel
= $this->mContent
->getModel();
688 $this->mContentHandler
= null;
690 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
691 } elseif ( $this->mText
!== null ) {
692 $handler = $this->getContentHandler();
693 $this->mContent
= $handler->unserializeContent( $this->mText
);
696 // If we have a Title object, make sure it is consistent with mPage.
697 if ( $this->mTitle
&& $this->mTitle
->exists() ) {
698 if ( $this->mPage
=== null ) {
699 // if the page ID wasn't known, set it now
700 $this->mPage
= $this->mTitle
->getArticleID();
701 } elseif ( $this->mTitle
->getArticleID() !== $this->mPage
) {
702 // Got different page IDs. This may be legit (e.g. during undeletion),
703 // but it seems worth mentioning it in the log.
704 wfDebug( "Page ID " . $this->mPage
. " mismatches the ID " .
705 $this->mTitle
->getArticleID() . " provided by the Title object." );
709 $this->mCurrent
= false;
711 // If we still have no length, see it we have the text to figure it out
712 if ( !$this->mSize
&& $this->mContent
!== null ) {
713 $this->mSize
= $this->mContent
->getSize();
717 if ( $this->mSha1
=== null ) {
718 $this->mSha1
= $this->mText
=== null ?
null : self
::base36Sha1( $this->mText
);
722 $this->getContentModel();
723 $this->getContentFormat();
725 throw new MWException( 'Revision constructor passed invalid row format.' );
727 $this->mUnpatrolled
= null;
735 public function getId() {
740 * Set the revision ID
742 * This should only be used for proposed revisions that turn out to be null edits
747 public function setId( $id ) {
748 $this->mId
= (int)$id;
752 * Set the user ID/name
754 * This should only be used for proposed revisions that turn out to be null edits
757 * @param integer $id User ID
758 * @param string $name User name
760 public function setUserIdAndName( $id, $name ) {
761 $this->mUser
= (int)$id;
762 $this->mUserText
= $name;
763 $this->mOrigUserText
= $name;
771 public function getTextId() {
772 return $this->mTextId
;
776 * Get parent revision ID (the original previous page revision)
780 public function getParentId() {
781 return $this->mParentId
;
785 * Returns the length of the text in this revision, or null if unknown.
789 public function getSize() {
794 * Returns the base36 sha1 of the text in this revision, or null if unknown.
796 * @return string|null
798 public function getSha1() {
803 * Returns the title of the page associated with this entry or null.
805 * Will do a query, when title is not set and id is given.
809 public function getTitle() {
810 if ( $this->mTitle
!== null ) {
811 return $this->mTitle
;
813 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
814 if ( $this->mId
!== null ) {
815 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
816 $row = $dbr->selectRow(
817 [ 'page', 'revision' ],
818 self
::selectPageFields(),
819 [ 'page_id=rev_page', 'rev_id' => $this->mId
],
823 // @TODO: better foreign title handling
824 $this->mTitle
= Title
::newFromRow( $row );
828 if ( $this->mWiki
=== false ||
$this->mWiki
=== wfWikiID() ) {
829 // Loading by ID is best, though not possible for foreign titles
830 if ( !$this->mTitle
&& $this->mPage
!== null && $this->mPage
> 0 ) {
831 $this->mTitle
= Title
::newFromID( $this->mPage
);
835 return $this->mTitle
;
839 * Set the title of the revision
841 * @param Title $title
843 public function setTitle( $title ) {
844 $this->mTitle
= $title;
852 public function getPage() {
857 * Fetch revision's user id if it's available to the specified audience.
858 * If the specified audience does not have access to it, zero will be
861 * @param int $audience One of:
862 * Revision::FOR_PUBLIC to be displayed to all users
863 * Revision::FOR_THIS_USER to be displayed to the given user
864 * Revision::RAW get the ID regardless of permissions
865 * @param User $user User object to check for, only if FOR_THIS_USER is passed
866 * to the $audience parameter
869 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
870 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
872 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
880 * Fetch revision's user id without regard for the current user's permissions
883 * @deprecated since 1.25, use getUser( Revision::RAW )
885 public function getRawUser() {
886 wfDeprecated( __METHOD__
, '1.25' );
887 return $this->getUser( self
::RAW
);
891 * Fetch revision's username if it's available to the specified audience.
892 * If the specified audience does not have access to the username, an
893 * empty string will be returned.
895 * @param int $audience One of:
896 * Revision::FOR_PUBLIC to be displayed to all users
897 * Revision::FOR_THIS_USER to be displayed to the given user
898 * Revision::RAW get the text regardless of permissions
899 * @param User $user User object to check for, only if FOR_THIS_USER is passed
900 * to the $audience parameter
903 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
904 $this->loadMutableFields();
906 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
908 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
911 if ( $this->mUserText
=== null ) {
912 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
913 if ( $this->mUserText
=== false ) {
914 # This shouldn't happen, but it can if the wiki was recovered
915 # via importing revs and there is no user table entry yet.
916 $this->mUserText
= $this->mOrigUserText
;
919 return $this->mUserText
;
924 * Fetch revision's username without regard for view restrictions
927 * @deprecated since 1.25, use getUserText( Revision::RAW )
929 public function getRawUserText() {
930 wfDeprecated( __METHOD__
, '1.25' );
931 return $this->getUserText( self
::RAW
);
935 * Fetch revision comment if it's available to the specified audience.
936 * If the specified audience does not have access to the comment, an
937 * empty string will be returned.
939 * @param int $audience One of:
940 * Revision::FOR_PUBLIC to be displayed to all users
941 * Revision::FOR_THIS_USER to be displayed to the given user
942 * Revision::RAW get the text regardless of permissions
943 * @param User $user User object to check for, only if FOR_THIS_USER is passed
944 * to the $audience parameter
947 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
948 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
950 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
953 return $this->mComment
;
958 * Fetch revision comment without regard for the current user's permissions
961 * @deprecated since 1.25, use getComment( Revision::RAW )
963 public function getRawComment() {
964 wfDeprecated( __METHOD__
, '1.25' );
965 return $this->getComment( self
::RAW
);
971 public function isMinor() {
972 return (bool)$this->mMinorEdit
;
976 * @return int Rcid of the unpatrolled row, zero if there isn't one
978 public function isUnpatrolled() {
979 if ( $this->mUnpatrolled
!== null ) {
980 return $this->mUnpatrolled
;
982 $rc = $this->getRecentChange();
983 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
984 $this->mUnpatrolled
= $rc->getAttribute( 'rc_id' );
986 $this->mUnpatrolled
= 0;
988 return $this->mUnpatrolled
;
992 * Get the RC object belonging to the current revision, if there's one
994 * @param int $flags (optional) $flags include:
995 * Revision::READ_LATEST : Select the data from the master
998 * @return RecentChange|null
1000 public function getRecentChange( $flags = 0 ) {
1001 $dbr = wfGetDB( DB_REPLICA
);
1003 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
1005 return RecentChange
::newFromConds(
1007 'rc_user_text' => $this->getUserText( Revision
::RAW
),
1008 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
1009 'rc_this_oldid' => $this->getId()
1017 * @param int $field One of DELETED_* bitfield constants
1021 public function isDeleted( $field ) {
1022 if ( $this->isCurrent() && $field === self
::DELETED_TEXT
) {
1023 // Current revisions of pages cannot have the content hidden. Skipping this
1024 // check is very useful for Parser as it fetches templates using newKnownCurrent().
1025 // Calling getVisibility() in that case triggers a verification database query.
1026 return false; // no need to check
1029 return ( $this->getVisibility() & $field ) == $field;
1033 * Get the deletion bitfield of the revision
1037 public function getVisibility() {
1038 $this->loadMutableFields();
1040 return (int)$this->mDeleted
;
1044 * Fetch revision content if it's available to the specified audience.
1045 * If the specified audience does not have the ability to view this
1046 * revision, null will be returned.
1048 * @param int $audience One of:
1049 * Revision::FOR_PUBLIC to be displayed to all users
1050 * Revision::FOR_THIS_USER to be displayed to $wgUser
1051 * Revision::RAW get the text regardless of permissions
1052 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1053 * to the $audience parameter
1055 * @return Content|null
1057 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1058 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
1060 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
1063 return $this->getContentInternal();
1068 * Get original serialized data (without checking view restrictions)
1073 public function getSerializedData() {
1074 if ( $this->mText
=== null ) {
1075 // Revision is immutable. Load on demand.
1076 $this->mText
= $this->loadText();
1079 return $this->mText
;
1083 * Gets the content object for the revision (or null on failure).
1085 * Note that for mutable Content objects, each call to this method will return a
1089 * @return Content|null The Revision's content, or null on failure.
1091 protected function getContentInternal() {
1092 if ( $this->mContent
=== null ) {
1093 $text = $this->getSerializedData();
1095 if ( $text !== null && $text !== false ) {
1096 // Unserialize content
1097 $handler = $this->getContentHandler();
1098 $format = $this->getContentFormat();
1100 $this->mContent
= $handler->unserializeContent( $text, $format );
1104 // NOTE: copy() will return $this for immutable content objects
1105 return $this->mContent ?
$this->mContent
->copy() : null;
1109 * Returns the content model for this revision.
1111 * If no content model was stored in the database, the default content model for the title is
1112 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1113 * is used as a last resort.
1115 * @return string The content model id associated with this revision,
1116 * see the CONTENT_MODEL_XXX constants.
1118 public function getContentModel() {
1119 if ( !$this->mContentModel
) {
1120 $title = $this->getTitle();
1122 $this->mContentModel
= ContentHandler
::getDefaultModelFor( $title );
1124 $this->mContentModel
= CONTENT_MODEL_WIKITEXT
;
1127 assert( !empty( $this->mContentModel
) );
1130 return $this->mContentModel
;
1134 * Returns the content format for this revision.
1136 * If no content format was stored in the database, the default format for this
1137 * revision's content model is returned.
1139 * @return string The content format id associated with this revision,
1140 * see the CONTENT_FORMAT_XXX constants.
1142 public function getContentFormat() {
1143 if ( !$this->mContentFormat
) {
1144 $handler = $this->getContentHandler();
1145 $this->mContentFormat
= $handler->getDefaultFormat();
1147 assert( !empty( $this->mContentFormat
) );
1150 return $this->mContentFormat
;
1154 * Returns the content handler appropriate for this revision's content model.
1156 * @throws MWException
1157 * @return ContentHandler
1159 public function getContentHandler() {
1160 if ( !$this->mContentHandler
) {
1161 $model = $this->getContentModel();
1162 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1164 $format = $this->getContentFormat();
1166 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1167 throw new MWException( "Oops, the content format $format is not supported for "
1168 . "this content model, $model" );
1172 return $this->mContentHandler
;
1178 public function getTimestamp() {
1179 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1185 public function isCurrent() {
1186 return $this->mCurrent
;
1190 * Get previous revision for this title
1192 * @return Revision|null
1194 public function getPrevious() {
1195 if ( $this->getTitle() ) {
1196 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1198 return self
::newFromTitle( $this->getTitle(), $prev );
1205 * Get next revision for this title
1207 * @return Revision|null
1209 public function getNext() {
1210 if ( $this->getTitle() ) {
1211 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1213 return self
::newFromTitle( $this->getTitle(), $next );
1220 * Get previous revision Id for this page_id
1221 * This is used to populate rev_parent_id on save
1223 * @param IDatabase $db
1226 private function getPreviousRevisionId( $db ) {
1227 if ( $this->mPage
=== null ) {
1230 # Use page_latest if ID is not given
1231 if ( !$this->mId
) {
1232 $prevId = $db->selectField( 'page', 'page_latest',
1233 [ 'page_id' => $this->mPage
],
1236 $prevId = $db->selectField( 'revision', 'rev_id',
1237 [ 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
],
1239 [ 'ORDER BY' => 'rev_id DESC' ] );
1241 return intval( $prevId );
1245 * Get revision text associated with an old or archive row
1247 * Both the flags and the text field must be included. Including the old_id
1248 * field will activate cache usage as long as the $wiki parameter is not set.
1250 * @param stdClass $row The text data
1251 * @param string $prefix Table prefix (default 'old_')
1252 * @param string|bool $wiki The name of the wiki to load the revision text from
1253 * (same as the the wiki $row was loaded from) or false to indicate the local
1254 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1255 * identifier as understood by the LoadBalancer class.
1256 * @return string|false Text the text requested or false on failure
1258 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1259 $textField = $prefix . 'text';
1260 $flagsField = $prefix . 'flags';
1262 if ( isset( $row->$flagsField ) ) {
1263 $flags = explode( ',', $row->$flagsField );
1268 if ( isset( $row->$textField ) ) {
1269 $text = $row->$textField;
1274 // Use external methods for external objects, text in table is URL-only then
1275 if ( in_array( 'external', $flags ) ) {
1277 $parts = explode( '://', $url, 2 );
1278 if ( count( $parts ) == 1 ||
$parts[1] == '' ) {
1282 if ( isset( $row->old_id
) && $wiki === false ) {
1283 // Make use of the wiki-local revision text cache
1284 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1285 // The cached value should be decompressed, so handle that and return here
1286 return $cache->getWithSetCallback(
1287 $cache->makeKey( 'revisiontext', 'textid', $row->old_id
),
1288 self
::getCacheTTL( $cache ),
1289 function () use ( $url, $wiki, $flags ) {
1290 // No negative caching per Revision::loadText()
1291 $text = ExternalStore
::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1293 return self
::decompressRevisionText( $text, $flags );
1295 [ 'pcGroup' => self
::TEXT_CACHE_GROUP
, 'pcTTL' => $cache::TTL_PROC_LONG
]
1298 $text = ExternalStore
::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1302 return self
::decompressRevisionText( $text, $flags );
1306 * If $wgCompressRevisions is enabled, we will compress data.
1307 * The input string is modified in place.
1308 * Return value is the flags field: contains 'gzip' if the
1309 * data is compressed, and 'utf-8' if we're saving in UTF-8
1312 * @param mixed $text Reference to a text
1315 public static function compressRevisionText( &$text ) {
1316 global $wgCompressRevisions;
1319 # Revisions not marked this way will be converted
1320 # on load if $wgLegacyCharset is set in the future.
1323 if ( $wgCompressRevisions ) {
1324 if ( function_exists( 'gzdeflate' ) ) {
1325 $deflated = gzdeflate( $text );
1327 if ( $deflated === false ) {
1328 wfLogWarning( __METHOD__
. ': gzdeflate() failed' );
1334 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1337 return implode( ',', $flags );
1341 * Re-converts revision text according to it's flags.
1343 * @param mixed $text Reference to a text
1344 * @param array $flags Compression flags
1345 * @return string|bool Decompressed text, or false on failure
1347 public static function decompressRevisionText( $text, $flags ) {
1348 global $wgLegacyEncoding, $wgContLang;
1350 if ( $text === false ) {
1351 // Text failed to be fetched; nothing to do
1355 if ( in_array( 'gzip', $flags ) ) {
1356 # Deal with optional compression of archived pages.
1357 # This can be done periodically via maintenance/compressOld.php, and
1358 # as pages are saved if $wgCompressRevisions is set.
1359 $text = gzinflate( $text );
1361 if ( $text === false ) {
1362 wfLogWarning( __METHOD__
. ': gzinflate() failed' );
1367 if ( in_array( 'object', $flags ) ) {
1368 # Generic compressed storage
1369 $obj = unserialize( $text );
1370 if ( !is_object( $obj ) ) {
1374 $text = $obj->getText();
1377 if ( $text !== false && $wgLegacyEncoding
1378 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags )
1380 # Old revisions kept around in a legacy encoding?
1381 # Upconvert on demand.
1382 # ("utf8" checked for compatibility with some broken
1383 # conversion scripts 2008-12-30)
1384 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1391 * Insert a new revision into the database, returning the new revision ID
1392 * number on success and dies horribly on failure.
1394 * @param IDatabase $dbw (master connection)
1395 * @throws MWException
1398 public function insertOn( $dbw ) {
1399 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1401 // We're inserting a new revision, so we have to use master anyway.
1402 // If it's a null revision, it may have references to rows that
1403 // are not in the replica yet (the text row).
1404 $this->mQueryFlags |
= self
::READ_LATEST
;
1406 // Not allowed to have rev_page equal to 0, false, etc.
1407 if ( !$this->mPage
) {
1408 $title = $this->getTitle();
1409 if ( $title instanceof Title
) {
1410 $titleText = ' for page ' . $title->getPrefixedText();
1414 throw new MWException( "Cannot insert revision$titleText: page ID must be nonzero" );
1417 $this->checkContentModel();
1419 $data = $this->mText
;
1420 $flags = self
::compressRevisionText( $data );
1422 # Write to external storage if required
1423 if ( $wgDefaultExternalStore ) {
1424 // Store and get the URL
1425 $data = ExternalStore
::insertToDefault( $data );
1427 throw new MWException( "Unable to store text to external storage" );
1432 $flags .= 'external';
1435 # Record the text (or external storage URL) to the text table
1436 if ( $this->mTextId
=== null ) {
1437 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1438 $dbw->insert( 'text',
1440 'old_id' => $old_id,
1441 'old_text' => $data,
1442 'old_flags' => $flags,
1445 $this->mTextId
= $dbw->insertId();
1448 if ( $this->mComment
=== null ) {
1449 $this->mComment
= "";
1452 # Record the edit in revisions
1453 $rev_id = $this->mId
!== null
1455 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1457 'rev_id' => $rev_id,
1458 'rev_page' => $this->mPage
,
1459 'rev_text_id' => $this->mTextId
,
1460 'rev_comment' => $this->mComment
,
1461 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1462 'rev_user' => $this->mUser
,
1463 'rev_user_text' => $this->mUserText
,
1464 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1465 'rev_deleted' => $this->mDeleted
,
1466 'rev_len' => $this->mSize
,
1467 'rev_parent_id' => $this->mParentId
=== null
1468 ?
$this->getPreviousRevisionId( $dbw )
1470 'rev_sha1' => $this->mSha1
=== null
1471 ? Revision
::base36Sha1( $this->mText
)
1475 if ( $wgContentHandlerUseDB ) {
1476 // NOTE: Store null for the default model and format, to save space.
1477 // XXX: Makes the DB sensitive to changed defaults.
1478 // Make this behavior optional? Only in miser mode?
1480 $model = $this->getContentModel();
1481 $format = $this->getContentFormat();
1483 $title = $this->getTitle();
1485 if ( $title === null ) {
1486 throw new MWException( "Insufficient information to determine the title of the "
1487 . "revision's page!" );
1490 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1491 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1493 $row['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
1494 $row['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
1497 $dbw->insert( 'revision', $row, __METHOD__
);
1499 $this->mId
= $rev_id !== null ?
$rev_id : $dbw->insertId();
1501 // Assertion to try to catch T92046
1502 if ( (int)$this->mId
=== 0 ) {
1503 throw new UnexpectedValueException(
1504 'After insert, Revision mId is ' . var_export( $this->mId
, 1 ) . ': ' .
1505 var_export( $row, 1 )
1509 // Avoid PHP 7.1 warning of passing $this by reference
1511 Hooks
::run( 'RevisionInsertComplete', [ &$revision, $data, $flags ] );
1516 protected function checkContentModel() {
1517 global $wgContentHandlerUseDB;
1519 // Note: may return null for revisions that have not yet been inserted
1520 $title = $this->getTitle();
1522 $model = $this->getContentModel();
1523 $format = $this->getContentFormat();
1524 $handler = $this->getContentHandler();
1526 if ( !$handler->isSupportedFormat( $format ) ) {
1527 $t = $title->getPrefixedDBkey();
1529 throw new MWException( "Can't use format $format with content model $model on $t" );
1532 if ( !$wgContentHandlerUseDB && $title ) {
1533 // if $wgContentHandlerUseDB is not set,
1534 // all revisions must use the default content model and format.
1536 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1537 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1538 $defaultFormat = $defaultHandler->getDefaultFormat();
1540 if ( $this->getContentModel() != $defaultModel ) {
1541 $t = $title->getPrefixedDBkey();
1543 throw new MWException( "Can't save non-default content model with "
1544 . "\$wgContentHandlerUseDB disabled: model is $model, "
1545 . "default for $t is $defaultModel" );
1548 if ( $this->getContentFormat() != $defaultFormat ) {
1549 $t = $title->getPrefixedDBkey();
1551 throw new MWException( "Can't use non-default content format with "
1552 . "\$wgContentHandlerUseDB disabled: format is $format, "
1553 . "default for $t is $defaultFormat" );
1557 $content = $this->getContent( Revision
::RAW
);
1558 $prefixedDBkey = $title->getPrefixedDBkey();
1559 $revId = $this->mId
;
1562 throw new MWException(
1563 "Content of revision $revId ($prefixedDBkey) could not be loaded for validation!"
1566 if ( !$content->isValid() ) {
1567 throw new MWException(
1568 "Content of revision $revId ($prefixedDBkey) is not valid! Content model is $model"
1574 * Get the base 36 SHA-1 value for a string of text
1575 * @param string $text
1578 public static function base36Sha1( $text ) {
1579 return Wikimedia\base_convert
( sha1( $text ), 16, 36, 31 );
1583 * Get the text cache TTL
1585 * @param WANObjectCache $cache
1588 private static function getCacheTTL( WANObjectCache
$cache ) {
1589 global $wgRevisionCacheExpiry;
1591 if ( $cache->getQoS( $cache::ATTR_EMULATION
) <= $cache::QOS_EMULATION_SQL
) {
1592 // Do not cache RDBMs blobs in...the RDBMs store
1593 $ttl = $cache::TTL_UNCACHEABLE
;
1595 $ttl = $wgRevisionCacheExpiry ?
: $cache::TTL_UNCACHEABLE
;
1602 * Lazy-load the revision's text.
1603 * Currently hardcoded to the 'text' table storage engine.
1605 * @return string|bool The revision's text, or false on failure
1607 private function loadText() {
1608 $cache = ObjectCache
::getMainWANInstance();
1610 // No negative caching; negative hits on text rows may be due to corrupted replica DBs
1611 return $cache->getWithSetCallback(
1612 $cache->makeKey( 'revisiontext', 'textid', $this->getTextId() ),
1613 self
::getCacheTTL( $cache ),
1615 return $this->fetchText();
1617 [ 'pcGroup' => self
::TEXT_CACHE_GROUP
, 'pcTTL' => $cache::TTL_PROC_LONG
]
1621 private function fetchText() {
1622 $textId = $this->getTextId();
1624 // If we kept data for lazy extraction, use it now...
1625 if ( $this->mTextRow
!== null ) {
1626 $row = $this->mTextRow
;
1627 $this->mTextRow
= null;
1632 // Callers doing updates will pass in READ_LATEST as usual. Since the text/blob tables
1633 // do not normally get rows changed around, set READ_LATEST_IMMUTABLE in those cases.
1634 $flags = $this->mQueryFlags
;
1635 $flags |
= DBAccessObjectUtils
::hasFlags( $flags, self
::READ_LATEST
)
1636 ? self
::READ_LATEST_IMMUTABLE
1639 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
1640 DBAccessObjectUtils
::getDBOptions( $flags );
1643 // Text data is immutable; check replica DBs first.
1644 $row = wfGetDB( $index )->selectRow(
1646 [ 'old_text', 'old_flags' ],
1647 [ 'old_id' => $textId ],
1653 // Fallback to DB_MASTER in some cases if the row was not found
1654 if ( !$row && $fallbackIndex !== null ) {
1655 // Use FOR UPDATE if it was used to fetch this revision. This avoids missing the row
1656 // due to REPEATABLE-READ. Also fallback to the master if READ_LATEST is provided.
1657 $row = wfGetDB( $fallbackIndex )->selectRow(
1659 [ 'old_text', 'old_flags' ],
1660 [ 'old_id' => $textId ],
1667 wfDebugLog( 'Revision', "No text row with ID '$textId' (revision {$this->getId()})." );
1670 $text = self
::getRevisionText( $row );
1671 if ( $row && $text === false ) {
1672 wfDebugLog( 'Revision', "No blob for text row '$textId' (revision {$this->getId()})." );
1675 return is_string( $text ) ?
$text : false;
1679 * Create a new null-revision for insertion into a page's
1680 * history. This will not re-save the text, but simply refer
1681 * to the text from the previous version.
1683 * Such revisions can for instance identify page rename
1684 * operations and other such meta-modifications.
1686 * @param IDatabase $dbw
1687 * @param int $pageId ID number of the page to read from
1688 * @param string $summary Revision's summary
1689 * @param bool $minor Whether the revision should be considered as minor
1690 * @param User|null $user User object to use or null for $wgUser
1691 * @return Revision|null Revision or null on error
1693 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1694 global $wgContentHandlerUseDB, $wgContLang;
1696 $fields = [ 'page_latest', 'page_namespace', 'page_title',
1697 'rev_text_id', 'rev_len', 'rev_sha1' ];
1699 if ( $wgContentHandlerUseDB ) {
1700 $fields[] = 'rev_content_model';
1701 $fields[] = 'rev_content_format';
1704 $current = $dbw->selectRow(
1705 [ 'page', 'revision' ],
1708 'page_id' => $pageId,
1709 'page_latest=rev_id',
1712 [ 'FOR UPDATE' ] // T51581
1721 // Truncate for whole multibyte characters
1722 $summary = $wgContLang->truncate( $summary, 255 );
1726 'user_text' => $user->getName(),
1727 'user' => $user->getId(),
1728 'comment' => $summary,
1729 'minor_edit' => $minor,
1730 'text_id' => $current->rev_text_id
,
1731 'parent_id' => $current->page_latest
,
1732 'len' => $current->rev_len
,
1733 'sha1' => $current->rev_sha1
1736 if ( $wgContentHandlerUseDB ) {
1737 $row['content_model'] = $current->rev_content_model
;
1738 $row['content_format'] = $current->rev_content_format
;
1741 $row['title'] = Title
::makeTitle( $current->page_namespace
, $current->page_title
);
1743 $revision = new Revision( $row );
1752 * Determine if the current user is allowed to view a particular
1753 * field of this revision, if it's marked as deleted.
1755 * @param int $field One of self::DELETED_TEXT,
1756 * self::DELETED_COMMENT,
1757 * self::DELETED_USER
1758 * @param User|null $user User object to check, or null to use $wgUser
1761 public function userCan( $field, User
$user = null ) {
1762 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1766 * Determine if the current user is allowed to view a particular
1767 * field of this revision, if it's marked as deleted. This is used
1768 * by various classes to avoid duplication.
1770 * @param int $bitfield Current field
1771 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1772 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1773 * self::DELETED_USER = File::DELETED_USER
1774 * @param User|null $user User object to check, or null to use $wgUser
1775 * @param Title|null $title A Title object to check for per-page restrictions on,
1776 * instead of just plain userrights
1779 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1782 if ( $bitfield & $field ) { // aspect is deleted
1783 if ( $user === null ) {
1787 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1788 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
1789 } elseif ( $field & self
::DELETED_TEXT
) {
1790 $permissions = [ 'deletedtext' ];
1792 $permissions = [ 'deletedhistory' ];
1794 $permissionlist = implode( ', ', $permissions );
1795 if ( $title === null ) {
1796 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
1797 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
1799 $text = $title->getPrefixedText();
1800 wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" );
1801 foreach ( $permissions as $perm ) {
1802 if ( $title->userCan( $perm, $user ) ) {
1814 * Get rev_timestamp from rev_id, without loading the rest of the row
1816 * @param Title $title
1819 * @return string|bool False if not found
1821 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1822 $db = ( $flags & self
::READ_LATEST
)
1823 ?
wfGetDB( DB_MASTER
)
1824 : wfGetDB( DB_REPLICA
);
1825 // Casting fix for databases that can't take '' for rev_id
1829 $conds = [ 'rev_id' => $id ];
1830 $conds['rev_page'] = $title->getArticleID();
1831 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1833 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
1837 * Get count of revisions per page...not very efficient
1839 * @param IDatabase $db
1840 * @param int $id Page id
1843 static function countByPageId( $db, $id ) {
1844 $row = $db->selectRow( 'revision', [ 'revCount' => 'COUNT(*)' ],
1845 [ 'rev_page' => $id ], __METHOD__
);
1847 return $row->revCount
;
1853 * Get count of revisions per page...not very efficient
1855 * @param IDatabase $db
1856 * @param Title $title
1859 static function countByTitle( $db, $title ) {
1860 $id = $title->getArticleID();
1862 return self
::countByPageId( $db, $id );
1868 * Check if no edits were made by other users since
1869 * the time a user started editing the page. Limit to
1870 * 50 revisions for the sake of performance.
1873 * @deprecated since 1.24
1875 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1876 * Database object or a database identifier usable with wfGetDB.
1877 * @param int $pageId The ID of the page in question
1878 * @param int $userId The ID of the user in question
1879 * @param string $since Look at edits since this time
1881 * @return bool True if the given user was the only one to edit since the given timestamp
1883 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1888 if ( is_int( $db ) ) {
1889 $db = wfGetDB( $db );
1892 $res = $db->select( 'revision',
1895 'rev_page' => $pageId,
1896 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1899 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ] );
1900 foreach ( $res as $row ) {
1901 if ( $row->rev_user
!= $userId ) {
1909 * Load a revision based on a known page ID and current revision ID from the DB
1911 * This method allows for the use of caching, though accessing anything that normally
1912 * requires permission checks (aside from the text) will trigger a small DB lookup.
1913 * The title will also be lazy loaded, though setTitle() can be used to preload it.
1915 * @param IDatabase $db
1916 * @param int $pageId Page ID
1917 * @param int $revId Known current revision of this page
1918 * @return Revision|bool Returns false if missing
1921 public static function newKnownCurrent( IDatabase
$db, $pageId, $revId ) {
1922 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1923 return $cache->getWithSetCallback(
1924 // Page/rev IDs passed in from DB to reflect history merges
1925 $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ),
1927 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1928 $setOpts +
= Database
::getCacheSetOptions( $db );
1930 $rev = Revision
::loadFromPageId( $db, $pageId, $revId );
1931 // Reflect revision deletion and user renames
1933 $rev->mTitle
= null; // mutable; lazy-load
1934 $rev->mRefreshMutableFields
= true;
1937 return $rev ?
: false; // don't cache negatives
1943 * For cached revisions, make sure the user name and rev_deleted is up-to-date
1945 private function loadMutableFields() {
1946 if ( !$this->mRefreshMutableFields
) {
1947 return; // not needed
1950 $this->mRefreshMutableFields
= false;
1951 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
1952 $row = $dbr->selectRow(
1953 [ 'revision', 'user' ],
1954 [ 'rev_deleted', 'user_name' ],
1955 [ 'rev_id' => $this->mId
, 'user_id = rev_user' ],
1958 if ( $row ) { // update values
1959 $this->mDeleted
= (int)$row->rev_deleted
;
1960 $this->mUserText
= $row->user_name
;