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
22 use MediaWiki\Linker\LinkTarget
;
23 use MediaWiki\MediaWikiServices
;
28 class Revision
implements IDBAccessObject
{
36 protected $mOrigUserText;
40 protected $mMinorEdit;
42 protected $mTimestamp;
58 protected $mUnpatrolled;
60 /** @var stdClass|null */
63 /** @var null|Title */
68 protected $mContentModel;
70 protected $mContentFormat;
72 /** @var Content|null|bool */
74 /** @var null|ContentHandler */
75 protected $mContentHandler;
78 protected $mQueryFlags = 0;
79 /** @var bool Used for cached values to reload user text and rev_deleted */
80 protected $mRefreshMutableFields = false;
81 /** @var string Wiki ID; false means the current wiki */
82 protected $mWiki = false;
84 // Revision deletion constants
85 const DELETED_TEXT
= 1;
86 const DELETED_COMMENT
= 2;
87 const DELETED_USER
= 4;
88 const DELETED_RESTRICTED
= 8;
89 const SUPPRESSED_USER
= 12; // convenience
90 const SUPPRESSED_ALL
= 15; // convenience
92 // Audience options for accessors
94 const FOR_THIS_USER
= 2;
97 const TEXT_CACHE_GROUP
= 'revisiontext:10'; // process cache name and max key count
100 * Load a page revision from a given revision ID number.
101 * Returns null if no such revision can be found.
104 * Revision::READ_LATEST : Select the data from the master
105 * Revision::READ_LOCKING : Select & lock the data from the master
108 * @param int $flags (optional)
109 * @return Revision|null
111 public static function newFromId( $id, $flags = 0 ) {
112 return self
::newFromConds( [ 'rev_id' => intval( $id ) ], $flags );
116 * Load either the current, or a specified, revision
117 * that's attached to a given link target. If not attached
118 * to that link target, will return null.
121 * Revision::READ_LATEST : Select the data from the master
122 * Revision::READ_LOCKING : Select & lock the data from the master
124 * @param LinkTarget $linkTarget
125 * @param int $id (optional)
126 * @param int $flags Bitfield (optional)
127 * @return Revision|null
129 public static function newFromTitle( LinkTarget
$linkTarget, $id = 0, $flags = 0 ) {
131 'page_namespace' => $linkTarget->getNamespace(),
132 'page_title' => $linkTarget->getDBkey()
135 // Use the specified ID
136 $conds['rev_id'] = $id;
137 return self
::newFromConds( $conds, $flags );
139 // Use a join to get the latest revision
140 $conds[] = 'rev_id=page_latest';
141 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
142 return self
::loadFromConds( $db, $conds, $flags );
147 * Load either the current, or a specified, revision
148 * that's attached to a given page ID.
149 * Returns null if no such revision can be found.
152 * Revision::READ_LATEST : Select the data from the master (since 1.20)
153 * Revision::READ_LOCKING : Select & lock the data from the master
156 * @param int $revId (optional)
157 * @param int $flags Bitfield (optional)
158 * @return Revision|null
160 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
161 $conds = [ 'page_id' => $pageId ];
163 $conds['rev_id'] = $revId;
164 return self
::newFromConds( $conds, $flags );
166 // Use a join to get the latest revision
167 $conds[] = 'rev_id = page_latest';
168 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
169 return self
::loadFromConds( $db, $conds, $flags );
174 * Make a fake revision object from an archive table row. This is queried
175 * for permissions or even inserted (as in Special:Undelete)
176 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
179 * @param array $overrides
181 * @throws MWException
184 public static function newFromArchiveRow( $row, $overrides = [] ) {
185 global $wgContentHandlerUseDB;
187 $attribs = $overrides +
[
188 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
189 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
190 'comment' => $row->ar_comment
,
191 'user' => $row->ar_user
,
192 'user_text' => $row->ar_user_text
,
193 'timestamp' => $row->ar_timestamp
,
194 'minor_edit' => $row->ar_minor_edit
,
195 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
196 'deleted' => $row->ar_deleted
,
197 'len' => $row->ar_len
,
198 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
199 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
200 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
203 if ( !$wgContentHandlerUseDB ) {
204 unset( $attribs['content_model'] );
205 unset( $attribs['content_format'] );
208 if ( !isset( $attribs['title'] )
209 && isset( $row->ar_namespace
)
210 && isset( $row->ar_title
)
212 $attribs['title'] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
215 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
216 // Pre-1.5 ar_text row
217 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
218 if ( $attribs['text'] === false ) {
219 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
222 return new self( $attribs );
231 public static function newFromRow( $row ) {
232 return new self( $row );
236 * Load a page revision from a given revision ID number.
237 * Returns null if no such revision can be found.
239 * @param IDatabase $db
241 * @return Revision|null
243 public static function loadFromId( $db, $id ) {
244 return self
::loadFromConds( $db, [ 'rev_id' => intval( $id ) ] );
248 * Load either the current, or a specified, revision
249 * that's attached to a given page. If not attached
250 * to that page, will return null.
252 * @param IDatabase $db
255 * @return Revision|null
257 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
258 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
260 $conds['rev_id'] = intval( $id );
262 $conds[] = 'rev_id=page_latest';
264 return self
::loadFromConds( $db, $conds );
268 * Load either the current, or a specified, revision
269 * that's attached to a given page. If not attached
270 * to that page, will return null.
272 * @param IDatabase $db
273 * @param Title $title
275 * @return Revision|null
277 public static function loadFromTitle( $db, $title, $id = 0 ) {
279 $matchId = intval( $id );
281 $matchId = 'page_latest';
283 return self
::loadFromConds( $db,
286 'page_namespace' => $title->getNamespace(),
287 'page_title' => $title->getDBkey()
293 * Load the revision for the given title with the given timestamp.
294 * WARNING: Timestamps may in some circumstances not be unique,
295 * so this isn't the best key to use.
297 * @param IDatabase $db
298 * @param Title $title
299 * @param string $timestamp
300 * @return Revision|null
302 public static function loadFromTimestamp( $db, $title, $timestamp ) {
303 return self
::loadFromConds( $db,
305 'rev_timestamp' => $db->timestamp( $timestamp ),
306 'page_namespace' => $title->getNamespace(),
307 'page_title' => $title->getDBkey()
313 * Given a set of conditions, fetch a revision
315 * This method is used then a revision ID is qualified and
316 * will incorporate some basic replica DB/master fallback logic
318 * @param array $conditions
319 * @param int $flags (optional)
320 * @return Revision|null
322 private static function newFromConds( $conditions, $flags = 0 ) {
323 $db = wfGetDB( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
325 $rev = self
::loadFromConds( $db, $conditions, $flags );
326 // Make sure new pending/committed revision are visibile later on
327 // within web requests to certain avoid bugs like T93866 and T94407.
329 && !( $flags & self
::READ_LATEST
)
330 && wfGetLB()->getServerCount() > 1
331 && wfGetLB()->hasOrMadeRecentMasterChanges()
333 $flags = self
::READ_LATEST
;
334 $db = wfGetDB( DB_MASTER
);
335 $rev = self
::loadFromConds( $db, $conditions, $flags );
339 $rev->mQueryFlags
= $flags;
346 * Given a set of conditions, fetch a revision from
347 * the given database connection.
349 * @param IDatabase $db
350 * @param array $conditions
351 * @param int $flags (optional)
352 * @return Revision|null
354 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
355 $row = self
::fetchFromConds( $db, $conditions, $flags );
357 $rev = new Revision( $row );
358 $rev->mWiki
= $db->getWikiID();
367 * Return a wrapper for a series of database rows to
368 * fetch all of a given page's revisions in turn.
369 * Each row can be fed to the constructor to get objects.
371 * @param LinkTarget $title
372 * @return ResultWrapper
373 * @deprecated Since 1.28
375 public static function fetchRevision( LinkTarget
$title ) {
376 $row = self
::fetchFromConds(
377 wfGetDB( DB_REPLICA
),
379 'rev_id=page_latest',
380 'page_namespace' => $title->getNamespace(),
381 'page_title' => $title->getDBkey()
385 return new FakeResultWrapper( $row ?
[ $row ] : [] );
389 * Given a set of conditions, return a ResultWrapper
390 * which will return matching database rows with the
391 * fields necessary to build Revision objects.
393 * @param IDatabase $db
394 * @param array $conditions
395 * @param int $flags (optional)
398 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
399 $fields = array_merge(
400 self
::selectFields(),
401 self
::selectPageFields(),
402 self
::selectUserFields()
405 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
406 $options[] = 'FOR UPDATE';
408 return $db->selectRow(
409 [ 'revision', 'page', 'user' ],
414 [ 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() ]
419 * Return the value of a select() JOIN conds array for the user table.
420 * This will get user table rows for logged-in users.
424 public static function userJoinCond() {
425 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
429 * Return the value of a select() page conds array for the page table.
430 * This will assure that the revision(s) are not orphaned from live pages.
434 public static function pageJoinCond() {
435 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
439 * Return the list of revision fields that should be selected to create
443 public static function selectFields() {
444 global $wgContentHandlerUseDB;
461 if ( $wgContentHandlerUseDB ) {
462 $fields[] = 'rev_content_format';
463 $fields[] = 'rev_content_model';
470 * Return the list of revision fields that should be selected to create
471 * a new revision from an archive row.
474 public static function selectArchiveFields() {
475 global $wgContentHandlerUseDB;
493 if ( $wgContentHandlerUseDB ) {
494 $fields[] = 'ar_content_format';
495 $fields[] = 'ar_content_model';
501 * Return the list of text fields that should be selected to read the
505 public static function selectTextFields() {
513 * Return the list of page fields that should be selected from page table
516 public static function selectPageFields() {
528 * Return the list of user fields that should be selected from user table
531 public static function selectUserFields() {
532 return [ 'user_name' ];
536 * Do a batched query to get the parent revision lengths
537 * @param IDatabase $db
538 * @param array $revIds
541 public static function getParentLengths( $db, array $revIds ) {
544 return $revLens; // empty
546 $res = $db->select( 'revision',
547 [ 'rev_id', 'rev_len' ],
548 [ 'rev_id' => $revIds ],
550 foreach ( $res as $row ) {
551 $revLens[$row->rev_id
] = $row->rev_len
;
559 * @param object|array $row Either a database row or an array
560 * @throws MWException
563 function __construct( $row ) {
564 if ( is_object( $row ) ) {
565 $this->mId
= intval( $row->rev_id
);
566 $this->mPage
= intval( $row->rev_page
);
567 $this->mTextId
= intval( $row->rev_text_id
);
568 $this->mComment
= $row->rev_comment
;
569 $this->mUser
= intval( $row->rev_user
);
570 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
571 $this->mTimestamp
= $row->rev_timestamp
;
572 $this->mDeleted
= intval( $row->rev_deleted
);
574 if ( !isset( $row->rev_parent_id
) ) {
575 $this->mParentId
= null;
577 $this->mParentId
= intval( $row->rev_parent_id
);
580 if ( !isset( $row->rev_len
) ) {
583 $this->mSize
= intval( $row->rev_len
);
586 if ( !isset( $row->rev_sha1
) ) {
589 $this->mSha1
= $row->rev_sha1
;
592 if ( isset( $row->page_latest
) ) {
593 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
594 $this->mTitle
= Title
::newFromRow( $row );
596 $this->mCurrent
= false;
597 $this->mTitle
= null;
600 if ( !isset( $row->rev_content_model
) ) {
601 $this->mContentModel
= null; # determine on demand if needed
603 $this->mContentModel
= strval( $row->rev_content_model
);
606 if ( !isset( $row->rev_content_format
) ) {
607 $this->mContentFormat
= null; # determine on demand if needed
609 $this->mContentFormat
= strval( $row->rev_content_format
);
612 // Lazy extraction...
614 if ( isset( $row->old_text
) ) {
615 $this->mTextRow
= $row;
617 // 'text' table row entry will be lazy-loaded
618 $this->mTextRow
= null;
621 // Use user_name for users and rev_user_text for IPs...
622 $this->mUserText
= null; // lazy load if left null
623 if ( $this->mUser
== 0 ) {
624 $this->mUserText
= $row->rev_user_text
; // IP user
625 } elseif ( isset( $row->user_name
) ) {
626 $this->mUserText
= $row->user_name
; // logged-in user
628 $this->mOrigUserText
= $row->rev_user_text
;
629 } elseif ( is_array( $row ) ) {
630 // Build a new revision to be saved...
631 global $wgUser; // ugh
633 # if we have a content object, use it to set the model and type
634 if ( !empty( $row['content'] ) ) {
635 // @todo when is that set? test with external store setup! check out insertOn() [dk]
636 if ( !empty( $row['text_id'] ) ) {
637 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
638 "can't serialize content object" );
641 $row['content_model'] = $row['content']->getModel();
642 # note: mContentFormat is initializes later accordingly
643 # note: content is serialized later in this method!
644 # also set text to null?
647 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
648 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
649 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
650 $this->mUserText
= isset( $row['user_text'] )
651 ?
strval( $row['user_text'] ) : $wgUser->getName();
652 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
653 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
654 $this->mTimestamp
= isset( $row['timestamp'] )
655 ?
strval( $row['timestamp'] ) : wfTimestampNow();
656 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
657 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
658 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
659 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
661 $this->mContentModel
= isset( $row['content_model'] )
662 ?
strval( $row['content_model'] ) : null;
663 $this->mContentFormat
= isset( $row['content_format'] )
664 ?
strval( $row['content_format'] ) : null;
666 // Enforce spacing trimming on supplied text
667 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
668 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
669 $this->mTextRow
= null;
671 $this->mTitle
= isset( $row['title'] ) ?
$row['title'] : null;
673 // if we have a Content object, override mText and mContentModel
674 if ( !empty( $row['content'] ) ) {
675 if ( !( $row['content'] instanceof Content
) ) {
676 throw new MWException( '`content` field must contain a Content object.' );
679 $handler = $this->getContentHandler();
680 $this->mContent
= $row['content'];
682 $this->mContentModel
= $this->mContent
->getModel();
683 $this->mContentHandler
= null;
685 $this->mText
= $handler->serializeContent( $row['content'], $this->getContentFormat() );
686 } elseif ( $this->mText
!== null ) {
687 $handler = $this->getContentHandler();
688 $this->mContent
= $handler->unserializeContent( $this->mText
);
691 // If we have a Title object, make sure it is consistent with mPage.
692 if ( $this->mTitle
&& $this->mTitle
->exists() ) {
693 if ( $this->mPage
=== null ) {
694 // if the page ID wasn't known, set it now
695 $this->mPage
= $this->mTitle
->getArticleID();
696 } elseif ( $this->mTitle
->getArticleID() !== $this->mPage
) {
697 // Got different page IDs. This may be legit (e.g. during undeletion),
698 // but it seems worth mentioning it in the log.
699 wfDebug( "Page ID " . $this->mPage
. " mismatches the ID " .
700 $this->mTitle
->getArticleID() . " provided by the Title object." );
704 $this->mCurrent
= false;
706 // If we still have no length, see it we have the text to figure it out
707 if ( !$this->mSize
&& $this->mContent
!== null ) {
708 $this->mSize
= $this->mContent
->getSize();
712 if ( $this->mSha1
=== null ) {
713 $this->mSha1
= $this->mText
=== null ?
null : self
::base36Sha1( $this->mText
);
717 $this->getContentModel();
718 $this->getContentFormat();
720 throw new MWException( 'Revision constructor passed invalid row format.' );
722 $this->mUnpatrolled
= null;
730 public function getId() {
735 * Set the revision ID
737 * This should only be used for proposed revisions that turn out to be null edits
742 public function setId( $id ) {
743 $this->mId
= (int)$id;
747 * Set the user ID/name
749 * This should only be used for proposed revisions that turn out to be null edits
752 * @param integer $id User ID
753 * @param string $name User name
755 public function setUserIdAndName( $id, $name ) {
756 $this->mUser
= (int)$id;
757 $this->mUserText
= $name;
758 $this->mOrigUserText
= $name;
766 public function getTextId() {
767 return $this->mTextId
;
771 * Get parent revision ID (the original previous page revision)
775 public function getParentId() {
776 return $this->mParentId
;
780 * Returns the length of the text in this revision, or null if unknown.
784 public function getSize() {
789 * Returns the base36 sha1 of the text in this revision, or null if unknown.
791 * @return string|null
793 public function getSha1() {
798 * Returns the title of the page associated with this entry or null.
800 * Will do a query, when title is not set and id is given.
804 public function getTitle() {
805 if ( $this->mTitle
!== null ) {
806 return $this->mTitle
;
808 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
809 if ( $this->mId
!== null ) {
810 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
811 $row = $dbr->selectRow(
812 [ 'page', 'revision' ],
813 self
::selectPageFields(),
814 [ 'page_id=rev_page', 'rev_id' => $this->mId
],
818 // @TODO: better foreign title handling
819 $this->mTitle
= Title
::newFromRow( $row );
823 if ( $this->mWiki
=== false ||
$this->mWiki
=== wfWikiID() ) {
824 // Loading by ID is best, though not possible for foreign titles
825 if ( !$this->mTitle
&& $this->mPage
!== null && $this->mPage
> 0 ) {
826 $this->mTitle
= Title
::newFromID( $this->mPage
);
830 return $this->mTitle
;
834 * Set the title of the revision
836 * @param Title $title
838 public function setTitle( $title ) {
839 $this->mTitle
= $title;
847 public function getPage() {
852 * Fetch revision's user id if it's available to the specified audience.
853 * If the specified audience does not have access to it, zero will be
856 * @param int $audience One of:
857 * Revision::FOR_PUBLIC to be displayed to all users
858 * Revision::FOR_THIS_USER to be displayed to the given user
859 * Revision::RAW get the ID regardless of permissions
860 * @param User $user User object to check for, only if FOR_THIS_USER is passed
861 * to the $audience parameter
864 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
865 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
867 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
875 * Fetch revision's user id without regard for the current user's permissions
878 * @deprecated since 1.25, use getUser( Revision::RAW )
880 public function getRawUser() {
881 wfDeprecated( __METHOD__
, '1.25' );
882 return $this->getUser( self
::RAW
);
886 * Fetch revision's username if it's available to the specified audience.
887 * If the specified audience does not have access to the username, an
888 * empty string will be returned.
890 * @param int $audience One of:
891 * Revision::FOR_PUBLIC to be displayed to all users
892 * Revision::FOR_THIS_USER to be displayed to the given user
893 * Revision::RAW get the text regardless of permissions
894 * @param User $user User object to check for, only if FOR_THIS_USER is passed
895 * to the $audience parameter
898 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
899 $this->loadMutableFields();
901 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
903 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
906 if ( $this->mUserText
=== null ) {
907 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
908 if ( $this->mUserText
=== false ) {
909 # This shouldn't happen, but it can if the wiki was recovered
910 # via importing revs and there is no user table entry yet.
911 $this->mUserText
= $this->mOrigUserText
;
914 return $this->mUserText
;
919 * Fetch revision's username without regard for view restrictions
922 * @deprecated since 1.25, use getUserText( Revision::RAW )
924 public function getRawUserText() {
925 wfDeprecated( __METHOD__
, '1.25' );
926 return $this->getUserText( self
::RAW
);
930 * Fetch revision comment if it's available to the specified audience.
931 * If the specified audience does not have access to the comment, an
932 * empty string will be returned.
934 * @param int $audience One of:
935 * Revision::FOR_PUBLIC to be displayed to all users
936 * Revision::FOR_THIS_USER to be displayed to the given user
937 * Revision::RAW get the text regardless of permissions
938 * @param User $user User object to check for, only if FOR_THIS_USER is passed
939 * to the $audience parameter
942 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
943 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
945 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
948 return $this->mComment
;
953 * Fetch revision comment without regard for the current user's permissions
956 * @deprecated since 1.25, use getComment( Revision::RAW )
958 public function getRawComment() {
959 wfDeprecated( __METHOD__
, '1.25' );
960 return $this->getComment( self
::RAW
);
966 public function isMinor() {
967 return (bool)$this->mMinorEdit
;
971 * @return int Rcid of the unpatrolled row, zero if there isn't one
973 public function isUnpatrolled() {
974 if ( $this->mUnpatrolled
!== null ) {
975 return $this->mUnpatrolled
;
977 $rc = $this->getRecentChange();
978 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
979 $this->mUnpatrolled
= $rc->getAttribute( 'rc_id' );
981 $this->mUnpatrolled
= 0;
983 return $this->mUnpatrolled
;
987 * Get the RC object belonging to the current revision, if there's one
989 * @param int $flags (optional) $flags include:
990 * Revision::READ_LATEST : Select the data from the master
993 * @return RecentChange|null
995 public function getRecentChange( $flags = 0 ) {
996 $dbr = wfGetDB( DB_REPLICA
);
998 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
1000 return RecentChange
::newFromConds(
1002 'rc_user_text' => $this->getUserText( Revision
::RAW
),
1003 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
1004 'rc_this_oldid' => $this->getId()
1012 * @param int $field One of DELETED_* bitfield constants
1016 public function isDeleted( $field ) {
1017 if ( $this->isCurrent() && $field === self
::DELETED_TEXT
) {
1018 // Current revisions of pages cannot have the content hidden. Skipping this
1019 // check is very useful for Parser as it fetches templates using newKnownCurrent().
1020 // Calling getVisibility() in that case triggers a verification database query.
1021 return false; // no need to check
1024 return ( $this->getVisibility() & $field ) == $field;
1028 * Get the deletion bitfield of the revision
1032 public function getVisibility() {
1033 $this->loadMutableFields();
1035 return (int)$this->mDeleted
;
1039 * Fetch revision text if it's available to the specified audience.
1040 * If the specified audience does not have the ability to view this
1041 * revision, an empty string will be returned.
1043 * @param int $audience One of:
1044 * Revision::FOR_PUBLIC to be displayed to all users
1045 * Revision::FOR_THIS_USER to be displayed to the given user
1046 * Revision::RAW get the text regardless of permissions
1047 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1048 * to the $audience parameter
1050 * @deprecated since 1.21, use getContent() instead
1053 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1054 wfDeprecated( __METHOD__
, '1.21' );
1056 $content = $this->getContent( $audience, $user );
1057 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
1061 * Fetch revision content if it's available to the specified audience.
1062 * If the specified audience does not have the ability to view this
1063 * revision, null will be returned.
1065 * @param int $audience One of:
1066 * Revision::FOR_PUBLIC to be displayed to all users
1067 * Revision::FOR_THIS_USER to be displayed to $wgUser
1068 * Revision::RAW get the text regardless of permissions
1069 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1070 * to the $audience parameter
1072 * @return Content|null
1074 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1075 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
1077 } elseif ( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
1080 return $this->getContentInternal();
1085 * Get original serialized data (without checking view restrictions)
1090 public function getSerializedData() {
1091 if ( $this->mText
=== null ) {
1092 // Revision is immutable. Load on demand.
1093 $this->mText
= $this->loadText();
1096 return $this->mText
;
1100 * Gets the content object for the revision (or null on failure).
1102 * Note that for mutable Content objects, each call to this method will return a
1106 * @return Content|null The Revision's content, or null on failure.
1108 protected function getContentInternal() {
1109 if ( $this->mContent
=== null ) {
1110 $text = $this->getSerializedData();
1112 if ( $text !== null && $text !== false ) {
1113 // Unserialize content
1114 $handler = $this->getContentHandler();
1115 $format = $this->getContentFormat();
1117 $this->mContent
= $handler->unserializeContent( $text, $format );
1121 // NOTE: copy() will return $this for immutable content objects
1122 return $this->mContent ?
$this->mContent
->copy() : null;
1126 * Returns the content model for this revision.
1128 * If no content model was stored in the database, the default content model for the title is
1129 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1130 * is used as a last resort.
1132 * @return string The content model id associated with this revision,
1133 * see the CONTENT_MODEL_XXX constants.
1135 public function getContentModel() {
1136 if ( !$this->mContentModel
) {
1137 $title = $this->getTitle();
1139 $this->mContentModel
= ContentHandler
::getDefaultModelFor( $title );
1141 $this->mContentModel
= CONTENT_MODEL_WIKITEXT
;
1144 assert( !empty( $this->mContentModel
) );
1147 return $this->mContentModel
;
1151 * Returns the content format for this revision.
1153 * If no content format was stored in the database, the default format for this
1154 * revision's content model is returned.
1156 * @return string The content format id associated with this revision,
1157 * see the CONTENT_FORMAT_XXX constants.
1159 public function getContentFormat() {
1160 if ( !$this->mContentFormat
) {
1161 $handler = $this->getContentHandler();
1162 $this->mContentFormat
= $handler->getDefaultFormat();
1164 assert( !empty( $this->mContentFormat
) );
1167 return $this->mContentFormat
;
1171 * Returns the content handler appropriate for this revision's content model.
1173 * @throws MWException
1174 * @return ContentHandler
1176 public function getContentHandler() {
1177 if ( !$this->mContentHandler
) {
1178 $model = $this->getContentModel();
1179 $this->mContentHandler
= ContentHandler
::getForModelID( $model );
1181 $format = $this->getContentFormat();
1183 if ( !$this->mContentHandler
->isSupportedFormat( $format ) ) {
1184 throw new MWException( "Oops, the content format $format is not supported for "
1185 . "this content model, $model" );
1189 return $this->mContentHandler
;
1195 public function getTimestamp() {
1196 return wfTimestamp( TS_MW
, $this->mTimestamp
);
1202 public function isCurrent() {
1203 return $this->mCurrent
;
1207 * Get previous revision for this title
1209 * @return Revision|null
1211 public function getPrevious() {
1212 if ( $this->getTitle() ) {
1213 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1215 return self
::newFromTitle( $this->getTitle(), $prev );
1222 * Get next revision for this title
1224 * @return Revision|null
1226 public function getNext() {
1227 if ( $this->getTitle() ) {
1228 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1230 return self
::newFromTitle( $this->getTitle(), $next );
1237 * Get previous revision Id for this page_id
1238 * This is used to populate rev_parent_id on save
1240 * @param IDatabase $db
1243 private function getPreviousRevisionId( $db ) {
1244 if ( $this->mPage
=== null ) {
1247 # Use page_latest if ID is not given
1248 if ( !$this->mId
) {
1249 $prevId = $db->selectField( 'page', 'page_latest',
1250 [ 'page_id' => $this->mPage
],
1253 $prevId = $db->selectField( 'revision', 'rev_id',
1254 [ 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
],
1256 [ 'ORDER BY' => 'rev_id DESC' ] );
1258 return intval( $prevId );
1262 * Get revision text associated with an old or archive row
1263 * $row is usually an object from wfFetchRow(), both the flags and the text
1264 * field must be included.
1266 * @param stdClass $row The text data
1267 * @param string $prefix Table prefix (default 'old_')
1268 * @param string|bool $wiki The name of the wiki to load the revision text from
1269 * (same as the the wiki $row was loaded from) or false to indicate the local
1270 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1271 * identifier as understood by the LoadBalancer class.
1272 * @return string Text the text requested or false on failure
1274 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1277 $textField = $prefix . 'text';
1278 $flagsField = $prefix . 'flags';
1280 if ( isset( $row->$flagsField ) ) {
1281 $flags = explode( ',', $row->$flagsField );
1286 if ( isset( $row->$textField ) ) {
1287 $text = $row->$textField;
1292 # Use external methods for external objects, text in table is URL-only then
1293 if ( in_array( 'external', $flags ) ) {
1295 $parts = explode( '://', $url, 2 );
1296 if ( count( $parts ) == 1 ||
$parts[1] == '' ) {
1299 $text = ExternalStore
::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1302 // If the text was fetched without an error, convert it
1303 if ( $text !== false ) {
1304 $text = self
::decompressRevisionText( $text, $flags );
1310 * If $wgCompressRevisions is enabled, we will compress data.
1311 * The input string is modified in place.
1312 * Return value is the flags field: contains 'gzip' if the
1313 * data is compressed, and 'utf-8' if we're saving in UTF-8
1316 * @param mixed $text Reference to a text
1319 public static function compressRevisionText( &$text ) {
1320 global $wgCompressRevisions;
1323 # Revisions not marked this way will be converted
1324 # on load if $wgLegacyCharset is set in the future.
1327 if ( $wgCompressRevisions ) {
1328 if ( function_exists( 'gzdeflate' ) ) {
1329 $deflated = gzdeflate( $text );
1331 if ( $deflated === false ) {
1332 wfLogWarning( __METHOD__
. ': gzdeflate() failed' );
1338 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1341 return implode( ',', $flags );
1345 * Re-converts revision text according to it's flags.
1347 * @param mixed $text Reference to a text
1348 * @param array $flags Compression flags
1349 * @return string|bool Decompressed text, or false on failure
1351 public static function decompressRevisionText( $text, $flags ) {
1352 if ( in_array( 'gzip', $flags ) ) {
1353 # Deal with optional compression of archived pages.
1354 # This can be done periodically via maintenance/compressOld.php, and
1355 # as pages are saved if $wgCompressRevisions is set.
1356 $text = gzinflate( $text );
1358 if ( $text === false ) {
1359 wfLogWarning( __METHOD__
. ': gzinflate() failed' );
1364 if ( in_array( 'object', $flags ) ) {
1365 # Generic compressed storage
1366 $obj = unserialize( $text );
1367 if ( !is_object( $obj ) ) {
1371 $text = $obj->getText();
1374 global $wgLegacyEncoding;
1375 if ( $text !== false && $wgLegacyEncoding
1376 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags )
1378 # Old revisions kept around in a legacy encoding?
1379 # Upconvert on demand.
1380 # ("utf8" checked for compatibility with some broken
1381 # conversion scripts 2008-12-30)
1383 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1390 * Insert a new revision into the database, returning the new revision ID
1391 * number on success and dies horribly on failure.
1393 * @param IDatabase $dbw (master connection)
1394 * @throws MWException
1397 public function insertOn( $dbw ) {
1398 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1400 // We're inserting a new revision, so we have to use master anyway.
1401 // If it's a null revision, it may have references to rows that
1402 // are not in the replica yet (the text row).
1403 $this->mQueryFlags |
= self
::READ_LATEST
;
1405 // Not allowed to have rev_page equal to 0, false, etc.
1406 if ( !$this->mPage
) {
1407 $title = $this->getTitle();
1408 if ( $title instanceof Title
) {
1409 $titleText = ' for page ' . $title->getPrefixedText();
1413 throw new MWException( "Cannot insert revision$titleText: page ID must be nonzero" );
1416 $this->checkContentModel();
1418 $data = $this->mText
;
1419 $flags = self
::compressRevisionText( $data );
1421 # Write to external storage if required
1422 if ( $wgDefaultExternalStore ) {
1423 // Store and get the URL
1424 $data = ExternalStore
::insertToDefault( $data );
1426 throw new MWException( "Unable to store text to external storage" );
1431 $flags .= 'external';
1434 # Record the text (or external storage URL) to the text table
1435 if ( $this->mTextId
=== null ) {
1436 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1437 $dbw->insert( 'text',
1439 'old_id' => $old_id,
1440 'old_text' => $data,
1441 'old_flags' => $flags,
1444 $this->mTextId
= $dbw->insertId();
1447 if ( $this->mComment
=== null ) {
1448 $this->mComment
= "";
1451 # Record the edit in revisions
1452 $rev_id = $this->mId
!== null
1454 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1456 'rev_id' => $rev_id,
1457 'rev_page' => $this->mPage
,
1458 'rev_text_id' => $this->mTextId
,
1459 'rev_comment' => $this->mComment
,
1460 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1461 'rev_user' => $this->mUser
,
1462 'rev_user_text' => $this->mUserText
,
1463 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1464 'rev_deleted' => $this->mDeleted
,
1465 'rev_len' => $this->mSize
,
1466 'rev_parent_id' => $this->mParentId
=== null
1467 ?
$this->getPreviousRevisionId( $dbw )
1469 'rev_sha1' => $this->mSha1
=== null
1470 ? Revision
::base36Sha1( $this->mText
)
1474 if ( $wgContentHandlerUseDB ) {
1475 // NOTE: Store null for the default model and format, to save space.
1476 // XXX: Makes the DB sensitive to changed defaults.
1477 // Make this behavior optional? Only in miser mode?
1479 $model = $this->getContentModel();
1480 $format = $this->getContentFormat();
1482 $title = $this->getTitle();
1484 if ( $title === null ) {
1485 throw new MWException( "Insufficient information to determine the title of the "
1486 . "revision's page!" );
1489 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1490 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
1492 $row['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
1493 $row['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
1496 $dbw->insert( 'revision', $row, __METHOD__
);
1498 $this->mId
= $rev_id !== null ?
$rev_id : $dbw->insertId();
1500 // Assertion to try to catch T92046
1501 if ( (int)$this->mId
=== 0 ) {
1502 throw new UnexpectedValueException(
1503 'After insert, Revision mId is ' . var_export( $this->mId
, 1 ) . ': ' .
1504 var_export( $row, 1 )
1508 Hooks
::run( 'RevisionInsertComplete', [ &$this, $data, $flags ] );
1513 protected function checkContentModel() {
1514 global $wgContentHandlerUseDB;
1516 // Note: may return null for revisions that have not yet been inserted
1517 $title = $this->getTitle();
1519 $model = $this->getContentModel();
1520 $format = $this->getContentFormat();
1521 $handler = $this->getContentHandler();
1523 if ( !$handler->isSupportedFormat( $format ) ) {
1524 $t = $title->getPrefixedDBkey();
1526 throw new MWException( "Can't use format $format with content model $model on $t" );
1529 if ( !$wgContentHandlerUseDB && $title ) {
1530 // if $wgContentHandlerUseDB is not set,
1531 // all revisions must use the default content model and format.
1533 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
1534 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1535 $defaultFormat = $defaultHandler->getDefaultFormat();
1537 if ( $this->getContentModel() != $defaultModel ) {
1538 $t = $title->getPrefixedDBkey();
1540 throw new MWException( "Can't save non-default content model with "
1541 . "\$wgContentHandlerUseDB disabled: model is $model, "
1542 . "default for $t is $defaultModel" );
1545 if ( $this->getContentFormat() != $defaultFormat ) {
1546 $t = $title->getPrefixedDBkey();
1548 throw new MWException( "Can't use non-default content format with "
1549 . "\$wgContentHandlerUseDB disabled: format is $format, "
1550 . "default for $t is $defaultFormat" );
1554 $content = $this->getContent( Revision
::RAW
);
1555 $prefixedDBkey = $title->getPrefixedDBkey();
1556 $revId = $this->mId
;
1559 throw new MWException(
1560 "Content of revision $revId ($prefixedDBkey) could not be loaded for validation!"
1563 if ( !$content->isValid() ) {
1564 throw new MWException(
1565 "Content of revision $revId ($prefixedDBkey) is not valid! Content model is $model"
1571 * Get the base 36 SHA-1 value for a string of text
1572 * @param string $text
1575 public static function base36Sha1( $text ) {
1576 return Wikimedia\base_convert
( sha1( $text ), 16, 36, 31 );
1580 * Lazy-load the revision's text.
1581 * Currently hardcoded to the 'text' table storage engine.
1583 * @return string|bool The revision's text, or false on failure
1585 private function loadText() {
1586 global $wgRevisionCacheExpiry;
1588 $cache = ObjectCache
::getMainWANInstance();
1589 if ( $cache->getQoS( $cache::ATTR_EMULATION
) <= $cache::QOS_EMULATION_SQL
) {
1590 // Do not cache RDBMs blobs in...the RDBMs store
1591 $ttl = $cache::TTL_UNCACHEABLE
;
1593 $ttl = $wgRevisionCacheExpiry ?
: $cache::TTL_UNCACHEABLE
;
1596 // No negative caching; negative hits on text rows may be due to corrupted replica DBs
1597 return $cache->getWithSetCallback(
1598 $cache->makeKey( 'revisiontext', 'textid', $this->getTextId() ),
1601 return $this->fetchText();
1603 [ 'pcGroup' => self
::TEXT_CACHE_GROUP
, 'pcTTL' => $cache::TTL_PROC_LONG
]
1607 private function fetchText() {
1608 $textId = $this->getTextId();
1610 // If we kept data for lazy extraction, use it now...
1611 if ( $this->mTextRow
!== null ) {
1612 $row = $this->mTextRow
;
1613 $this->mTextRow
= null;
1618 // Callers doing updates will pass in READ_LATEST as usual. Since the text/blob tables
1619 // do not normally get rows changed around, set READ_LATEST_IMMUTABLE in those cases.
1620 $flags = $this->mQueryFlags
;
1621 $flags |
= DBAccessObjectUtils
::hasFlags( $flags, self
::READ_LATEST
)
1622 ? self
::READ_LATEST_IMMUTABLE
1625 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
1626 DBAccessObjectUtils
::getDBOptions( $flags );
1629 // Text data is immutable; check replica DBs first.
1630 $row = wfGetDB( $index )->selectRow(
1632 [ 'old_text', 'old_flags' ],
1633 [ 'old_id' => $textId ],
1639 // Fallback to DB_MASTER in some cases if the row was not found
1640 if ( !$row && $fallbackIndex !== null ) {
1641 // Use FOR UPDATE if it was used to fetch this revision. This avoids missing the row
1642 // due to REPEATABLE-READ. Also fallback to the master if READ_LATEST is provided.
1643 $row = wfGetDB( $fallbackIndex )->selectRow(
1645 [ 'old_text', 'old_flags' ],
1646 [ 'old_id' => $textId ],
1653 wfDebugLog( 'Revision', "No text row with ID '$textId' (revision {$this->getId()})." );
1656 $text = self
::getRevisionText( $row );
1657 if ( $row && $text === false ) {
1658 wfDebugLog( 'Revision', "No blob for text row '$textId' (revision {$this->getId()})." );
1661 return is_string( $text ) ?
$text : false;
1665 * Create a new null-revision for insertion into a page's
1666 * history. This will not re-save the text, but simply refer
1667 * to the text from the previous version.
1669 * Such revisions can for instance identify page rename
1670 * operations and other such meta-modifications.
1672 * @param IDatabase $dbw
1673 * @param int $pageId ID number of the page to read from
1674 * @param string $summary Revision's summary
1675 * @param bool $minor Whether the revision should be considered as minor
1676 * @param User|null $user User object to use or null for $wgUser
1677 * @return Revision|null Revision or null on error
1679 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1680 global $wgContentHandlerUseDB, $wgContLang;
1682 $fields = [ 'page_latest', 'page_namespace', 'page_title',
1683 'rev_text_id', 'rev_len', 'rev_sha1' ];
1685 if ( $wgContentHandlerUseDB ) {
1686 $fields[] = 'rev_content_model';
1687 $fields[] = 'rev_content_format';
1690 $current = $dbw->selectRow(
1691 [ 'page', 'revision' ],
1694 'page_id' => $pageId,
1695 'page_latest=rev_id',
1698 [ 'FOR UPDATE' ] // T51581
1707 // Truncate for whole multibyte characters
1708 $summary = $wgContLang->truncate( $summary, 255 );
1712 'user_text' => $user->getName(),
1713 'user' => $user->getId(),
1714 'comment' => $summary,
1715 'minor_edit' => $minor,
1716 'text_id' => $current->rev_text_id
,
1717 'parent_id' => $current->page_latest
,
1718 'len' => $current->rev_len
,
1719 'sha1' => $current->rev_sha1
1722 if ( $wgContentHandlerUseDB ) {
1723 $row['content_model'] = $current->rev_content_model
;
1724 $row['content_format'] = $current->rev_content_format
;
1727 $row['title'] = Title
::makeTitle( $current->page_namespace
, $current->page_title
);
1729 $revision = new Revision( $row );
1738 * Determine if the current user is allowed to view a particular
1739 * field of this revision, if it's marked as deleted.
1741 * @param int $field One of self::DELETED_TEXT,
1742 * self::DELETED_COMMENT,
1743 * self::DELETED_USER
1744 * @param User|null $user User object to check, or null to use $wgUser
1747 public function userCan( $field, User
$user = null ) {
1748 return self
::userCanBitfield( $this->getVisibility(), $field, $user );
1752 * Determine if the current user is allowed to view a particular
1753 * field of this revision, if it's marked as deleted. This is used
1754 * by various classes to avoid duplication.
1756 * @param int $bitfield Current field
1757 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1758 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1759 * self::DELETED_USER = File::DELETED_USER
1760 * @param User|null $user User object to check, or null to use $wgUser
1761 * @param Title|null $title A Title object to check for per-page restrictions on,
1762 * instead of just plain userrights
1765 public static function userCanBitfield( $bitfield, $field, User
$user = null,
1768 if ( $bitfield & $field ) { // aspect is deleted
1769 if ( $user === null ) {
1773 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1774 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
1775 } elseif ( $field & self
::DELETED_TEXT
) {
1776 $permissions = [ 'deletedtext' ];
1778 $permissions = [ 'deletedhistory' ];
1780 $permissionlist = implode( ', ', $permissions );
1781 if ( $title === null ) {
1782 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
1783 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
1785 $text = $title->getPrefixedText();
1786 wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" );
1787 foreach ( $permissions as $perm ) {
1788 if ( $title->userCan( $perm, $user ) ) {
1800 * Get rev_timestamp from rev_id, without loading the rest of the row
1802 * @param Title $title
1804 * @return string|bool False if not found
1806 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1807 $db = ( $flags & self
::READ_LATEST
)
1808 ?
wfGetDB( DB_MASTER
)
1809 : wfGetDB( DB_REPLICA
);
1810 // Casting fix for databases that can't take '' for rev_id
1814 $conds = [ 'rev_id' => $id ];
1815 $conds['rev_page'] = $title->getArticleID();
1816 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1818 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
1822 * Get count of revisions per page...not very efficient
1824 * @param IDatabase $db
1825 * @param int $id Page id
1828 static function countByPageId( $db, $id ) {
1829 $row = $db->selectRow( 'revision', [ 'revCount' => 'COUNT(*)' ],
1830 [ 'rev_page' => $id ], __METHOD__
);
1832 return $row->revCount
;
1838 * Get count of revisions per page...not very efficient
1840 * @param IDatabase $db
1841 * @param Title $title
1844 static function countByTitle( $db, $title ) {
1845 $id = $title->getArticleID();
1847 return self
::countByPageId( $db, $id );
1853 * Check if no edits were made by other users since
1854 * the time a user started editing the page. Limit to
1855 * 50 revisions for the sake of performance.
1858 * @deprecated since 1.24
1860 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1861 * Database object or a database identifier usable with wfGetDB.
1862 * @param int $pageId The ID of the page in question
1863 * @param int $userId The ID of the user in question
1864 * @param string $since Look at edits since this time
1866 * @return bool True if the given user was the only one to edit since the given timestamp
1868 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1873 if ( is_int( $db ) ) {
1874 $db = wfGetDB( $db );
1877 $res = $db->select( 'revision',
1880 'rev_page' => $pageId,
1881 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1884 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ] );
1885 foreach ( $res as $row ) {
1886 if ( $row->rev_user
!= $userId ) {
1894 * Load a revision based on a known page ID and current revision ID from the DB
1896 * This method allows for the use of caching, though accessing anything that normally
1897 * requires permission checks (aside from the text) will trigger a small DB lookup.
1898 * The title will also be lazy loaded, though setTitle() can be used to preload it.
1900 * @param IDatabase $db
1901 * @param int $pageId Page ID
1902 * @param int $revId Known current revision of this page
1903 * @return Revision|bool Returns false if missing
1906 public static function newKnownCurrent( IDatabase
$db, $pageId, $revId ) {
1907 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1908 return $cache->getWithSetCallback(
1909 // Page/rev IDs passed in from DB to reflect history merges
1910 $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ),
1912 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1913 $setOpts +
= Database
::getCacheSetOptions( $db );
1915 $rev = Revision
::loadFromPageId( $db, $pageId, $revId );
1916 // Reflect revision deletion and user renames
1918 $rev->mTitle
= null; // mutable; lazy-load
1919 $rev->mRefreshMutableFields
= true;
1922 return $rev ?
: false; // don't cache negatives
1928 * For cached revisions, make sure the user name and rev_deleted is up-to-date
1930 private function loadMutableFields() {
1931 if ( !$this->mRefreshMutableFields
) {
1932 return; // not needed
1935 $this->mRefreshMutableFields
= false;
1936 $dbr = wfGetLB( $this->mWiki
)->getConnectionRef( DB_REPLICA
, [], $this->mWiki
);
1937 $row = $dbr->selectRow(
1938 [ 'revision', 'user' ],
1939 [ 'rev_deleted', 'user_name' ],
1940 [ 'rev_id' => $this->mId
, 'user_id = rev_user' ],
1943 if ( $row ) { // update values
1944 $this->mDeleted
= (int)$row->rev_deleted
;
1945 $this->mUserText
= $row->user_name
;