Merge "Added a --profiler option to all maintenance scripts."
[mediawiki.git] / includes / Revision.php
blob47626a22b3fd1ed143884bef17948d5cfd5b4744
1 <?php
2 /**
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
20 * @file
23 /**
24 * @todo document
26 class Revision implements IDBAccessObject {
27 protected $mId;
29 /**
30 * @var int|null
32 protected $mPage;
33 protected $mUserText;
34 protected $mOrigUserText;
35 protected $mUser;
36 protected $mMinorEdit;
37 protected $mTimestamp;
38 protected $mDeleted;
39 protected $mSize;
40 protected $mSha1;
41 protected $mParentId;
42 protected $mComment;
43 protected $mText;
44 protected $mTextRow;
46 /**
47 * @var null|Title
49 protected $mTitle;
50 protected $mCurrent;
51 protected $mContentModel;
52 protected $mContentFormat;
54 /**
55 * @var Content|null|bool
57 protected $mContent;
59 /**
60 * @var null|ContentHandler
62 protected $mContentHandler;
64 // Revision deletion constants
65 const DELETED_TEXT = 1;
66 const DELETED_COMMENT = 2;
67 const DELETED_USER = 4;
68 const DELETED_RESTRICTED = 8;
69 const SUPPRESSED_USER = 12; // convenience
71 // Audience options for accessors
72 const FOR_PUBLIC = 1;
73 const FOR_THIS_USER = 2;
74 const RAW = 3;
76 /**
77 * Load a page revision from a given revision ID number.
78 * Returns null if no such revision can be found.
80 * $flags include:
81 * Revision::READ_LATEST : Select the data from the master
82 * Revision::READ_LOCKING : Select & lock the data from the master
84 * @param $id Integer
85 * @param $flags Integer (optional)
86 * @return Revision or null
88 public static function newFromId( $id, $flags = 0 ) {
89 return self::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
92 /**
93 * Load either the current, or a specified, revision
94 * that's attached to a given title. If not attached
95 * to that title, will return null.
97 * $flags include:
98 * Revision::READ_LATEST : Select the data from the master
99 * Revision::READ_LOCKING : Select & lock the data from the master
101 * @param $title Title
102 * @param $id Integer (optional)
103 * @param $flags Integer Bitfield (optional)
104 * @return Revision or null
106 public static function newFromTitle( $title, $id = 0, $flags = 0 ) {
107 $conds = array(
108 'page_namespace' => $title->getNamespace(),
109 'page_title' => $title->getDBkey()
111 if ( $id ) {
112 // Use the specified ID
113 $conds['rev_id'] = $id;
114 } else {
115 // Use a join to get the latest revision
116 $conds[] = 'rev_id=page_latest';
118 return self::newFromConds( $conds, (int)$flags );
122 * Load either the current, or a specified, revision
123 * that's attached to a given page ID.
124 * Returns null if no such revision can be found.
126 * $flags include:
127 * Revision::READ_LATEST : Select the data from the master (since 1.20)
128 * Revision::READ_LOCKING : Select & lock the data from the master
130 * @param $revId Integer
131 * @param $pageId Integer (optional)
132 * @param $flags Integer Bitfield (optional)
133 * @return Revision or null
135 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
136 $conds = array( 'page_id' => $pageId );
137 if ( $revId ) {
138 $conds['rev_id'] = $revId;
139 } else {
140 // Use a join to get the latest revision
141 $conds[] = 'rev_id = page_latest';
143 return self::newFromConds( $conds, (int)$flags );
147 * Make a fake revision object from an archive table row. This is queried
148 * for permissions or even inserted (as in Special:Undelete)
149 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
151 * @param $row
152 * @param $overrides array
154 * @throws MWException
155 * @return Revision
157 public static function newFromArchiveRow( $row, $overrides = array() ) {
158 global $wgContentHandlerUseDB;
160 $attribs = $overrides + array(
161 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
162 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
163 'comment' => $row->ar_comment,
164 'user' => $row->ar_user,
165 'user_text' => $row->ar_user_text,
166 'timestamp' => $row->ar_timestamp,
167 'minor_edit' => $row->ar_minor_edit,
168 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
169 'deleted' => $row->ar_deleted,
170 'len' => $row->ar_len,
171 'sha1' => isset( $row->ar_sha1 ) ? $row->ar_sha1 : null,
172 'content_model' => isset( $row->ar_content_model ) ? $row->ar_content_model : null,
173 'content_format' => isset( $row->ar_content_format ) ? $row->ar_content_format : null,
176 if ( !$wgContentHandlerUseDB ) {
177 unset( $attribs['content_model'] );
178 unset( $attribs['content_format'] );
181 if ( !isset( $attribs['title'] )
182 && isset( $row->ar_namespace )
183 && isset( $row->ar_title ) ) {
185 $attribs['title'] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
188 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
189 // Pre-1.5 ar_text row
190 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
191 if ( $attribs['text'] === false ) {
192 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
195 return new self( $attribs );
199 * @since 1.19
201 * @param $row
202 * @return Revision
204 public static function newFromRow( $row ) {
205 return new self( $row );
209 * Load a page revision from a given revision ID number.
210 * Returns null if no such revision can be found.
212 * @param $db DatabaseBase
213 * @param $id Integer
214 * @return Revision or null
216 public static function loadFromId( $db, $id ) {
217 return self::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
221 * Load either the current, or a specified, revision
222 * that's attached to a given page. If not attached
223 * to that page, will return null.
225 * @param $db DatabaseBase
226 * @param $pageid Integer
227 * @param $id Integer
228 * @return Revision or null
230 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
231 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
232 if ( $id ) {
233 $conds['rev_id'] = intval( $id );
234 } else {
235 $conds[] = 'rev_id=page_latest';
237 return self::loadFromConds( $db, $conds );
241 * Load either the current, or a specified, revision
242 * that's attached to a given page. If not attached
243 * to that page, will return null.
245 * @param $db DatabaseBase
246 * @param $title Title
247 * @param $id Integer
248 * @return Revision or null
250 public static function loadFromTitle( $db, $title, $id = 0 ) {
251 if ( $id ) {
252 $matchId = intval( $id );
253 } else {
254 $matchId = 'page_latest';
256 return self::loadFromConds( $db,
257 array(
258 "rev_id=$matchId",
259 'page_namespace' => $title->getNamespace(),
260 'page_title' => $title->getDBkey()
266 * Load the revision for the given title with the given timestamp.
267 * WARNING: Timestamps may in some circumstances not be unique,
268 * so this isn't the best key to use.
270 * @param $db DatabaseBase
271 * @param $title Title
272 * @param $timestamp String
273 * @return Revision or null
275 public static function loadFromTimestamp( $db, $title, $timestamp ) {
276 return self::loadFromConds( $db,
277 array(
278 'rev_timestamp' => $db->timestamp( $timestamp ),
279 'page_namespace' => $title->getNamespace(),
280 'page_title' => $title->getDBkey()
286 * Given a set of conditions, fetch a revision.
288 * @param $conditions Array
289 * @param $flags integer (optional)
290 * @return Revision or null
292 private static function newFromConds( $conditions, $flags = 0 ) {
293 $db = wfGetDB( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_SLAVE );
294 $rev = self::loadFromConds( $db, $conditions, $flags );
295 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
296 if ( !( $flags & self::READ_LATEST ) ) {
297 $dbw = wfGetDB( DB_MASTER );
298 $rev = self::loadFromConds( $dbw, $conditions, $flags );
301 return $rev;
305 * Given a set of conditions, fetch a revision from
306 * the given database connection.
308 * @param $db DatabaseBase
309 * @param $conditions Array
310 * @param $flags integer (optional)
311 * @return Revision or null
313 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
314 $res = self::fetchFromConds( $db, $conditions, $flags );
315 if ( $res ) {
316 $row = $res->fetchObject();
317 if ( $row ) {
318 $ret = new Revision( $row );
319 return $ret;
322 $ret = null;
323 return $ret;
327 * Return a wrapper for a series of database rows to
328 * fetch all of a given page's revisions in turn.
329 * Each row can be fed to the constructor to get objects.
331 * @param $title Title
332 * @return ResultWrapper
334 public static function fetchRevision( $title ) {
335 return self::fetchFromConds(
336 wfGetDB( DB_SLAVE ),
337 array(
338 'rev_id=page_latest',
339 'page_namespace' => $title->getNamespace(),
340 'page_title' => $title->getDBkey()
346 * Given a set of conditions, return a ResultWrapper
347 * which will return matching database rows with the
348 * fields necessary to build Revision objects.
350 * @param $db DatabaseBase
351 * @param $conditions Array
352 * @param $flags integer (optional)
353 * @return ResultWrapper
355 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
356 $fields = array_merge(
357 self::selectFields(),
358 self::selectPageFields(),
359 self::selectUserFields()
361 $options = array( 'LIMIT' => 1 );
362 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
363 $options[] = 'FOR UPDATE';
365 return $db->select(
366 array( 'revision', 'page', 'user' ),
367 $fields,
368 $conditions,
369 __METHOD__,
370 $options,
371 array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
376 * Return the value of a select() JOIN conds array for the user table.
377 * This will get user table rows for logged-in users.
378 * @since 1.19
379 * @return Array
381 public static function userJoinCond() {
382 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
386 * Return the value of a select() page conds array for the page table.
387 * This will assure that the revision(s) are not orphaned from live pages.
388 * @since 1.19
389 * @return Array
391 public static function pageJoinCond() {
392 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
396 * Return the list of revision fields that should be selected to create
397 * a new revision.
398 * @return array
400 public static function selectFields() {
401 global $wgContentHandlerUseDB;
403 $fields = array(
404 'rev_id',
405 'rev_page',
406 'rev_text_id',
407 'rev_timestamp',
408 'rev_comment',
409 'rev_user_text',
410 'rev_user',
411 'rev_minor_edit',
412 'rev_deleted',
413 'rev_len',
414 'rev_parent_id',
415 'rev_sha1',
418 if ( $wgContentHandlerUseDB ) {
419 $fields[] = 'rev_content_format';
420 $fields[] = 'rev_content_model';
423 return $fields;
427 * Return the list of text fields that should be selected to read the
428 * revision text
429 * @return array
431 public static function selectTextFields() {
432 return array(
433 'old_text',
434 'old_flags'
439 * Return the list of page fields that should be selected from page table
440 * @return array
442 public static function selectPageFields() {
443 return array(
444 'page_namespace',
445 'page_title',
446 'page_id',
447 'page_latest',
448 'page_is_redirect',
449 'page_len',
454 * Return the list of user fields that should be selected from user table
455 * @return array
457 public static function selectUserFields() {
458 return array( 'user_name' );
462 * Do a batched query to get the parent revision lengths
463 * @param $db DatabaseBase
464 * @param $revIds Array
465 * @return array
467 public static function getParentLengths( $db, array $revIds ) {
468 $revLens = array();
469 if ( !$revIds ) {
470 return $revLens; // empty
472 wfProfileIn( __METHOD__ );
473 $res = $db->select( 'revision',
474 array( 'rev_id', 'rev_len' ),
475 array( 'rev_id' => $revIds ),
476 __METHOD__ );
477 foreach ( $res as $row ) {
478 $revLens[$row->rev_id] = $row->rev_len;
480 wfProfileOut( __METHOD__ );
481 return $revLens;
485 * Constructor
487 * @param $row Mixed: either a database row or an array
488 * @throws MWException
489 * @access private
491 function __construct( $row ) {
492 if ( is_object( $row ) ) {
493 $this->mId = intval( $row->rev_id );
494 $this->mPage = intval( $row->rev_page );
495 $this->mTextId = intval( $row->rev_text_id );
496 $this->mComment = $row->rev_comment;
497 $this->mUser = intval( $row->rev_user );
498 $this->mMinorEdit = intval( $row->rev_minor_edit );
499 $this->mTimestamp = $row->rev_timestamp;
500 $this->mDeleted = intval( $row->rev_deleted );
502 if ( !isset( $row->rev_parent_id ) ) {
503 $this->mParentId = null;
504 } else {
505 $this->mParentId = intval( $row->rev_parent_id );
508 if ( !isset( $row->rev_len ) ) {
509 $this->mSize = null;
510 } else {
511 $this->mSize = intval( $row->rev_len );
514 if ( !isset( $row->rev_sha1 ) ) {
515 $this->mSha1 = null;
516 } else {
517 $this->mSha1 = $row->rev_sha1;
520 if ( isset( $row->page_latest ) ) {
521 $this->mCurrent = ( $row->rev_id == $row->page_latest );
522 $this->mTitle = Title::newFromRow( $row );
523 } else {
524 $this->mCurrent = false;
525 $this->mTitle = null;
528 if ( !isset( $row->rev_content_model ) || is_null( $row->rev_content_model ) ) {
529 $this->mContentModel = null; # determine on demand if needed
530 } else {
531 $this->mContentModel = strval( $row->rev_content_model );
534 if ( !isset( $row->rev_content_format ) || is_null( $row->rev_content_format ) ) {
535 $this->mContentFormat = null; # determine on demand if needed
536 } else {
537 $this->mContentFormat = strval( $row->rev_content_format );
540 // Lazy extraction...
541 $this->mText = null;
542 if ( isset( $row->old_text ) ) {
543 $this->mTextRow = $row;
544 } else {
545 // 'text' table row entry will be lazy-loaded
546 $this->mTextRow = null;
549 // Use user_name for users and rev_user_text for IPs...
550 $this->mUserText = null; // lazy load if left null
551 if ( $this->mUser == 0 ) {
552 $this->mUserText = $row->rev_user_text; // IP user
553 } elseif ( isset( $row->user_name ) ) {
554 $this->mUserText = $row->user_name; // logged-in user
556 $this->mOrigUserText = $row->rev_user_text;
557 } elseif ( is_array( $row ) ) {
558 // Build a new revision to be saved...
559 global $wgUser; // ugh
561 # if we have a content object, use it to set the model and type
562 if ( !empty( $row['content'] ) ) {
563 // @todo when is that set? test with external store setup! check out insertOn() [dk]
564 if ( !empty( $row['text_id'] ) ) {
565 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
566 "can't serialize content object" );
569 $row['content_model'] = $row['content']->getModel();
570 # note: mContentFormat is initializes later accordingly
571 # note: content is serialized later in this method!
572 # also set text to null?
575 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
576 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
577 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
578 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
579 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
580 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
581 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestampNow();
582 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
583 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
584 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
585 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
587 $this->mContentModel = isset( $row['content_model'] ) ? strval( $row['content_model'] ) : null;
588 $this->mContentFormat = isset( $row['content_format'] ) ? strval( $row['content_format'] ) : null;
590 // Enforce spacing trimming on supplied text
591 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
592 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
593 $this->mTextRow = null;
595 $this->mTitle = isset( $row['title'] ) ? $row['title'] : null;
597 // if we have a Content object, override mText and mContentModel
598 if ( !empty( $row['content'] ) ) {
599 if ( !( $row['content'] instanceof Content ) ) {
600 throw new MWException( '`content` field must contain a Content object.' );
603 $handler = $this->getContentHandler();
604 $this->mContent = $row['content'];
606 $this->mContentModel = $this->mContent->getModel();
607 $this->mContentHandler = null;
609 $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
610 } elseif ( !is_null( $this->mText ) ) {
611 $handler = $this->getContentHandler();
612 $this->mContent = $handler->unserializeContent( $this->mText );
615 // If we have a Title object, make sure it is consistent with mPage.
616 if ( $this->mTitle && $this->mTitle->exists() ) {
617 if ( $this->mPage === null ) {
618 // if the page ID wasn't known, set it now
619 $this->mPage = $this->mTitle->getArticleID();
620 } elseif ( $this->mTitle->getArticleID() !== $this->mPage ) {
621 // Got different page IDs. This may be legit (e.g. during undeletion),
622 // but it seems worth mentioning it in the log.
623 wfDebug( "Page ID " . $this->mPage . " mismatches the ID " .
624 $this->mTitle->getArticleID() . " provided by the Title object." );
628 $this->mCurrent = false;
630 // If we still have no length, see it we have the text to figure it out
631 if ( !$this->mSize ) {
632 if ( !is_null( $this->mContent ) ) {
633 $this->mSize = $this->mContent->getSize();
634 } else {
635 #NOTE: this should never happen if we have either text or content object!
636 $this->mSize = null;
640 // Same for sha1
641 if ( $this->mSha1 === null ) {
642 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
645 // force lazy init
646 $this->getContentModel();
647 $this->getContentFormat();
648 } else {
649 throw new MWException( 'Revision constructor passed invalid row format.' );
651 $this->mUnpatrolled = null;
655 * Get revision ID
657 * @return Integer|null
659 public function getId() {
660 return $this->mId;
664 * Set the revision ID
666 * @since 1.19
667 * @param $id Integer
669 public function setId( $id ) {
670 $this->mId = $id;
674 * Get text row ID
676 * @return Integer|null
678 public function getTextId() {
679 return $this->mTextId;
683 * Get parent revision ID (the original previous page revision)
685 * @return Integer|null
687 public function getParentId() {
688 return $this->mParentId;
692 * Returns the length of the text in this revision, or null if unknown.
694 * @return Integer|null
696 public function getSize() {
697 return $this->mSize;
701 * Returns the base36 sha1 of the text in this revision, or null if unknown.
703 * @return String|null
705 public function getSha1() {
706 return $this->mSha1;
710 * Returns the title of the page associated with this entry or null.
712 * Will do a query, when title is not set and id is given.
714 * @return Title|null
716 public function getTitle() {
717 if ( isset( $this->mTitle ) ) {
718 return $this->mTitle;
720 if ( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
721 $dbr = wfGetDB( DB_SLAVE );
722 $row = $dbr->selectRow(
723 array( 'page', 'revision' ),
724 self::selectPageFields(),
725 array( 'page_id=rev_page',
726 'rev_id' => $this->mId ),
727 __METHOD__ );
728 if ( $row ) {
729 $this->mTitle = Title::newFromRow( $row );
733 if ( !$this->mTitle && !is_null( $this->mPage ) && $this->mPage > 0 ) {
734 $this->mTitle = Title::newFromID( $this->mPage );
737 return $this->mTitle;
741 * Set the title of the revision
743 * @param $title Title
745 public function setTitle( $title ) {
746 $this->mTitle = $title;
750 * Get the page ID
752 * @return Integer|null
754 public function getPage() {
755 return $this->mPage;
759 * Fetch revision's user id if it's available to the specified audience.
760 * If the specified audience does not have access to it, zero will be
761 * returned.
763 * @param $audience Integer: one of:
764 * Revision::FOR_PUBLIC to be displayed to all users
765 * Revision::FOR_THIS_USER to be displayed to the given user
766 * Revision::RAW get the ID regardless of permissions
767 * @param $user User object to check for, only if FOR_THIS_USER is passed
768 * to the $audience parameter
769 * @return Integer
771 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
772 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
773 return 0;
774 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
775 return 0;
776 } else {
777 return $this->mUser;
782 * Fetch revision's user id without regard for the current user's permissions
784 * @return String
786 public function getRawUser() {
787 return $this->mUser;
791 * Fetch revision's username if it's available to the specified audience.
792 * If the specified audience does not have access to the username, an
793 * empty string will be returned.
795 * @param $audience Integer: one of:
796 * Revision::FOR_PUBLIC to be displayed to all users
797 * Revision::FOR_THIS_USER to be displayed to the given user
798 * Revision::RAW get the text regardless of permissions
799 * @param $user User object to check for, only if FOR_THIS_USER is passed
800 * to the $audience parameter
801 * @return string
803 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
804 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
805 return '';
806 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
807 return '';
808 } else {
809 return $this->getRawUserText();
814 * Fetch revision's username without regard for view restrictions
816 * @return String
818 public function getRawUserText() {
819 if ( $this->mUserText === null ) {
820 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
821 if ( $this->mUserText === false ) {
822 # This shouldn't happen, but it can if the wiki was recovered
823 # via importing revs and there is no user table entry yet.
824 $this->mUserText = $this->mOrigUserText;
827 return $this->mUserText;
831 * Fetch revision comment if it's available to the specified audience.
832 * If the specified audience does not have access to the comment, an
833 * empty string will be returned.
835 * @param $audience Integer: one of:
836 * Revision::FOR_PUBLIC to be displayed to all users
837 * Revision::FOR_THIS_USER to be displayed to the given user
838 * Revision::RAW get the text regardless of permissions
839 * @param $user User object to check for, only if FOR_THIS_USER is passed
840 * to the $audience parameter
841 * @return String
843 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
844 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
845 return '';
846 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
847 return '';
848 } else {
849 return $this->mComment;
854 * Fetch revision comment without regard for the current user's permissions
856 * @return String
858 public function getRawComment() {
859 return $this->mComment;
863 * @return Boolean
865 public function isMinor() {
866 return (bool)$this->mMinorEdit;
870 * @return Integer rcid of the unpatrolled row, zero if there isn't one
872 public function isUnpatrolled() {
873 if ( $this->mUnpatrolled !== null ) {
874 return $this->mUnpatrolled;
876 $dbr = wfGetDB( DB_SLAVE );
877 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
878 'rc_id',
879 array( // Add redundant user,timestamp condition so we can use the existing index
880 'rc_user_text' => $this->getRawUserText(),
881 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
882 'rc_this_oldid' => $this->getId(),
883 'rc_patrolled' => 0
885 __METHOD__
887 return (int)$this->mUnpatrolled;
891 * @param int $field one of DELETED_* bitfield constants
893 * @return Boolean
895 public function isDeleted( $field ) {
896 return ( $this->mDeleted & $field ) == $field;
900 * Get the deletion bitfield of the revision
902 * @return int
904 public function getVisibility() {
905 return (int)$this->mDeleted;
909 * Fetch revision text if it's available to the specified audience.
910 * If the specified audience does not have the ability to view this
911 * revision, an empty string will be returned.
913 * @param $audience Integer: one of:
914 * Revision::FOR_PUBLIC to be displayed to all users
915 * Revision::FOR_THIS_USER to be displayed to the given user
916 * Revision::RAW get the text regardless of permissions
917 * @param $user User object to check for, only if FOR_THIS_USER is passed
918 * to the $audience parameter
920 * @deprecated in 1.21, use getContent() instead
921 * @todo Replace usage in core
922 * @return String
924 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
925 ContentHandler::deprecated( __METHOD__, '1.21' );
927 $content = $this->getContent( $audience, $user );
928 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
932 * Fetch revision content if it's available to the specified audience.
933 * If the specified audience does not have the ability to view this
934 * revision, null will be returned.
936 * @param $audience Integer: one of:
937 * Revision::FOR_PUBLIC to be displayed to all users
938 * Revision::FOR_THIS_USER to be displayed to $wgUser
939 * Revision::RAW get the text regardless of permissions
940 * @param $user User object to check for, only if FOR_THIS_USER is passed
941 * to the $audience parameter
942 * @since 1.21
943 * @return Content|null
945 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
946 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
947 return null;
948 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
949 return null;
950 } else {
951 return $this->getContentInternal();
956 * Alias for getText(Revision::FOR_THIS_USER)
958 * @deprecated since 1.17
959 * @return String
961 public function revText() {
962 wfDeprecated( __METHOD__, '1.17' );
963 return $this->getText( self::FOR_THIS_USER );
967 * Fetch revision text without regard for view restrictions
969 * @return String
971 * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW )
972 * or Revision::getSerializedData() as appropriate.
974 public function getRawText() {
975 ContentHandler::deprecated( __METHOD__, "1.21" );
976 return $this->getText( self::RAW );
980 * Fetch original serialized data without regard for view restrictions
982 * @since 1.21
983 * @return String
985 public function getSerializedData() {
986 return $this->mText;
990 * Gets the content object for the revision (or null on failure).
992 * Note that for mutable Content objects, each call to this method will return a
993 * fresh clone.
995 * @since 1.21
996 * @return Content|null the Revision's content, or null on failure.
998 protected function getContentInternal() {
999 if ( is_null( $this->mContent ) ) {
1000 // Revision is immutable. Load on demand:
1001 if ( is_null( $this->mText ) ) {
1002 $this->mText = $this->loadText();
1005 if ( $this->mText !== null && $this->mText !== false ) {
1006 // Unserialize content
1007 $handler = $this->getContentHandler();
1008 $format = $this->getContentFormat();
1010 $this->mContent = $handler->unserializeContent( $this->mText, $format );
1011 } else {
1012 $this->mContent = false; // negative caching!
1016 // NOTE: copy() will return $this for immutable content objects
1017 return $this->mContent ? $this->mContent->copy() : null;
1021 * Returns the content model for this revision.
1023 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
1024 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1025 * is used as a last resort.
1027 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
1029 public function getContentModel() {
1030 if ( !$this->mContentModel ) {
1031 $title = $this->getTitle();
1032 $this->mContentModel = ( $title ? $title->getContentModel() : CONTENT_MODEL_WIKITEXT );
1034 assert( !empty( $this->mContentModel ) );
1037 return $this->mContentModel;
1041 * Returns the content format for this revision.
1043 * If no content format was stored in the database, the default format for this
1044 * revision's content model is returned.
1046 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1048 public function getContentFormat() {
1049 if ( !$this->mContentFormat ) {
1050 $handler = $this->getContentHandler();
1051 $this->mContentFormat = $handler->getDefaultFormat();
1053 assert( !empty( $this->mContentFormat ) );
1056 return $this->mContentFormat;
1060 * Returns the content handler appropriate for this revision's content model.
1062 * @throws MWException
1063 * @return ContentHandler
1065 public function getContentHandler() {
1066 if ( !$this->mContentHandler ) {
1067 $model = $this->getContentModel();
1068 $this->mContentHandler = ContentHandler::getForModelID( $model );
1070 $format = $this->getContentFormat();
1072 if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
1073 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1077 return $this->mContentHandler;
1081 * @return String
1083 public function getTimestamp() {
1084 return wfTimestamp( TS_MW, $this->mTimestamp );
1088 * @return Boolean
1090 public function isCurrent() {
1091 return $this->mCurrent;
1095 * Get previous revision for this title
1097 * @return Revision|null
1099 public function getPrevious() {
1100 if ( $this->getTitle() ) {
1101 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1102 if ( $prev ) {
1103 return self::newFromTitle( $this->getTitle(), $prev );
1106 return null;
1110 * Get next revision for this title
1112 * @return Revision or null
1114 public function getNext() {
1115 if ( $this->getTitle() ) {
1116 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1117 if ( $next ) {
1118 return self::newFromTitle( $this->getTitle(), $next );
1121 return null;
1125 * Get previous revision Id for this page_id
1126 * This is used to populate rev_parent_id on save
1128 * @param $db DatabaseBase
1129 * @return Integer
1131 private function getPreviousRevisionId( $db ) {
1132 if ( is_null( $this->mPage ) ) {
1133 return 0;
1135 # Use page_latest if ID is not given
1136 if ( !$this->mId ) {
1137 $prevId = $db->selectField( 'page', 'page_latest',
1138 array( 'page_id' => $this->mPage ),
1139 __METHOD__ );
1140 } else {
1141 $prevId = $db->selectField( 'revision', 'rev_id',
1142 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
1143 __METHOD__,
1144 array( 'ORDER BY' => 'rev_id DESC' ) );
1146 return intval( $prevId );
1150 * Get revision text associated with an old or archive row
1151 * $row is usually an object from wfFetchRow(), both the flags and the text
1152 * field must be included
1154 * @param $row Object: the text data
1155 * @param string $prefix table prefix (default 'old_')
1156 * @param string|false $wiki the name of the wiki to load the revision text from
1157 * (same as the the wiki $row was loaded from) or false to indicate the local
1158 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1159 * identifier as understood by the LoadBalancer class.
1160 * @return String: text the text requested or false on failure
1162 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1163 wfProfileIn( __METHOD__ );
1165 # Get data
1166 $textField = $prefix . 'text';
1167 $flagsField = $prefix . 'flags';
1169 if ( isset( $row->$flagsField ) ) {
1170 $flags = explode( ',', $row->$flagsField );
1171 } else {
1172 $flags = array();
1175 if ( isset( $row->$textField ) ) {
1176 $text = $row->$textField;
1177 } else {
1178 wfProfileOut( __METHOD__ );
1179 return false;
1182 # Use external methods for external objects, text in table is URL-only then
1183 if ( in_array( 'external', $flags ) ) {
1184 $url = $text;
1185 $parts = explode( '://', $url, 2 );
1186 if ( count( $parts ) == 1 || $parts[1] == '' ) {
1187 wfProfileOut( __METHOD__ );
1188 return false;
1190 $text = ExternalStore::fetchFromURL( $url, array( 'wiki' => $wiki ) );
1193 // If the text was fetched without an error, convert it
1194 if ( $text !== false ) {
1195 if ( in_array( 'gzip', $flags ) ) {
1196 # Deal with optional compression of archived pages.
1197 # This can be done periodically via maintenance/compressOld.php, and
1198 # as pages are saved if $wgCompressRevisions is set.
1199 $text = gzinflate( $text );
1202 if ( in_array( 'object', $flags ) ) {
1203 # Generic compressed storage
1204 $obj = unserialize( $text );
1205 if ( !is_object( $obj ) ) {
1206 // Invalid object
1207 wfProfileOut( __METHOD__ );
1208 return false;
1210 $text = $obj->getText();
1213 global $wgLegacyEncoding;
1214 if ( $text !== false && $wgLegacyEncoding
1215 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1217 # Old revisions kept around in a legacy encoding?
1218 # Upconvert on demand.
1219 # ("utf8" checked for compatibility with some broken
1220 # conversion scripts 2008-12-30)
1221 global $wgContLang;
1222 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1225 wfProfileOut( __METHOD__ );
1226 return $text;
1230 * If $wgCompressRevisions is enabled, we will compress data.
1231 * The input string is modified in place.
1232 * Return value is the flags field: contains 'gzip' if the
1233 * data is compressed, and 'utf-8' if we're saving in UTF-8
1234 * mode.
1236 * @param $text Mixed: reference to a text
1237 * @return String
1239 public static function compressRevisionText( &$text ) {
1240 global $wgCompressRevisions;
1241 $flags = array();
1243 # Revisions not marked this way will be converted
1244 # on load if $wgLegacyCharset is set in the future.
1245 $flags[] = 'utf-8';
1247 if ( $wgCompressRevisions ) {
1248 if ( function_exists( 'gzdeflate' ) ) {
1249 $text = gzdeflate( $text );
1250 $flags[] = 'gzip';
1251 } else {
1252 wfDebug( __METHOD__ . " -- no zlib support, not compressing\n" );
1255 return implode( ',', $flags );
1259 * Insert a new revision into the database, returning the new revision ID
1260 * number on success and dies horribly on failure.
1262 * @param $dbw DatabaseBase: (master connection)
1263 * @throws MWException
1264 * @return Integer
1266 public function insertOn( $dbw ) {
1267 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1269 wfProfileIn( __METHOD__ );
1271 $this->checkContentModel();
1273 $data = $this->mText;
1274 $flags = self::compressRevisionText( $data );
1276 # Write to external storage if required
1277 if ( $wgDefaultExternalStore ) {
1278 // Store and get the URL
1279 $data = ExternalStore::insertToDefault( $data );
1280 if ( !$data ) {
1281 wfProfileOut( __METHOD__ );
1282 throw new MWException( "Unable to store text to external storage" );
1284 if ( $flags ) {
1285 $flags .= ',';
1287 $flags .= 'external';
1290 # Record the text (or external storage URL) to the text table
1291 if ( !isset( $this->mTextId ) ) {
1292 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1293 $dbw->insert( 'text',
1294 array(
1295 'old_id' => $old_id,
1296 'old_text' => $data,
1297 'old_flags' => $flags,
1298 ), __METHOD__
1300 $this->mTextId = $dbw->insertId();
1303 if ( $this->mComment === null ) {
1304 $this->mComment = "";
1307 # Record the edit in revisions
1308 $rev_id = isset( $this->mId )
1309 ? $this->mId
1310 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1311 $row = array(
1312 'rev_id' => $rev_id,
1313 'rev_page' => $this->mPage,
1314 'rev_text_id' => $this->mTextId,
1315 'rev_comment' => $this->mComment,
1316 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1317 'rev_user' => $this->mUser,
1318 'rev_user_text' => $this->mUserText,
1319 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1320 'rev_deleted' => $this->mDeleted,
1321 'rev_len' => $this->mSize,
1322 'rev_parent_id' => is_null( $this->mParentId )
1323 ? $this->getPreviousRevisionId( $dbw )
1324 : $this->mParentId,
1325 'rev_sha1' => is_null( $this->mSha1 )
1326 ? Revision::base36Sha1( $this->mText )
1327 : $this->mSha1,
1330 if ( $wgContentHandlerUseDB ) {
1331 //NOTE: Store null for the default model and format, to save space.
1332 //XXX: Makes the DB sensitive to changed defaults. Make this behavior optional? Only in miser mode?
1334 $model = $this->getContentModel();
1335 $format = $this->getContentFormat();
1337 $title = $this->getTitle();
1339 if ( $title === null ) {
1340 wfProfileOut( __METHOD__ );
1341 throw new MWException( "Insufficient information to determine the title of the revision's page!" );
1344 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1345 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
1347 $row['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
1348 $row['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
1351 $dbw->insert( 'revision', $row, __METHOD__ );
1353 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1355 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1357 wfProfileOut( __METHOD__ );
1358 return $this->mId;
1361 protected function checkContentModel() {
1362 global $wgContentHandlerUseDB;
1364 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1366 $model = $this->getContentModel();
1367 $format = $this->getContentFormat();
1368 $handler = $this->getContentHandler();
1370 if ( !$handler->isSupportedFormat( $format ) ) {
1371 $t = $title->getPrefixedDBkey();
1373 throw new MWException( "Can't use format $format with content model $model on $t" );
1376 if ( !$wgContentHandlerUseDB && $title ) {
1377 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1379 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1380 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1381 $defaultFormat = $defaultHandler->getDefaultFormat();
1383 if ( $this->getContentModel() != $defaultModel ) {
1384 $t = $title->getPrefixedDBkey();
1386 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: "
1387 . "model is $model , default for $t is $defaultModel" );
1390 if ( $this->getContentFormat() != $defaultFormat ) {
1391 $t = $title->getPrefixedDBkey();
1393 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: "
1394 . "format is $format, default for $t is $defaultFormat" );
1398 $content = $this->getContent( Revision::RAW );
1400 if ( !$content || !$content->isValid() ) {
1401 $t = $title->getPrefixedDBkey();
1403 throw new MWException( "Content of $t is not valid! Content model is $model" );
1408 * Get the base 36 SHA-1 value for a string of text
1409 * @param $text String
1410 * @return String
1412 public static function base36Sha1( $text ) {
1413 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1417 * Lazy-load the revision's text.
1418 * Currently hardcoded to the 'text' table storage engine.
1420 * @return String|bool the revision's text, or false on failure
1422 protected function loadText() {
1423 wfProfileIn( __METHOD__ );
1425 // Caching may be beneficial for massive use of external storage
1426 global $wgRevisionCacheExpiry, $wgMemc;
1427 $textId = $this->getTextId();
1428 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1429 if ( $wgRevisionCacheExpiry ) {
1430 $text = $wgMemc->get( $key );
1431 if ( is_string( $text ) ) {
1432 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1433 wfProfileOut( __METHOD__ );
1434 return $text;
1438 // If we kept data for lazy extraction, use it now...
1439 if ( isset( $this->mTextRow ) ) {
1440 $row = $this->mTextRow;
1441 $this->mTextRow = null;
1442 } else {
1443 $row = null;
1446 if ( !$row ) {
1447 // Text data is immutable; check slaves first.
1448 $dbr = wfGetDB( DB_SLAVE );
1449 $row = $dbr->selectRow( 'text',
1450 array( 'old_text', 'old_flags' ),
1451 array( 'old_id' => $this->getTextId() ),
1452 __METHOD__ );
1455 if ( !$row && wfGetLB()->getServerCount() > 1 ) {
1456 // Possible slave lag!
1457 $dbw = wfGetDB( DB_MASTER );
1458 $row = $dbw->selectRow( 'text',
1459 array( 'old_text', 'old_flags' ),
1460 array( 'old_id' => $this->getTextId() ),
1461 __METHOD__ );
1464 $text = self::getRevisionText( $row );
1466 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1467 if ( $wgRevisionCacheExpiry && $text !== false ) {
1468 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1471 wfProfileOut( __METHOD__ );
1473 return $text;
1477 * Create a new null-revision for insertion into a page's
1478 * history. This will not re-save the text, but simply refer
1479 * to the text from the previous version.
1481 * Such revisions can for instance identify page rename
1482 * operations and other such meta-modifications.
1484 * @param $dbw DatabaseBase
1485 * @param $pageId Integer: ID number of the page to read from
1486 * @param string $summary revision's summary
1487 * @param $minor Boolean: whether the revision should be considered as minor
1488 * @return Revision|null on error
1490 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1491 global $wgContentHandlerUseDB;
1493 wfProfileIn( __METHOD__ );
1495 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1496 'rev_text_id', 'rev_len', 'rev_sha1' );
1498 if ( $wgContentHandlerUseDB ) {
1499 $fields[] = 'rev_content_model';
1500 $fields[] = 'rev_content_format';
1503 $current = $dbw->selectRow(
1504 array( 'page', 'revision' ),
1505 $fields,
1506 array(
1507 'page_id' => $pageId,
1508 'page_latest=rev_id',
1510 __METHOD__ );
1512 if ( $current ) {
1513 $row = array(
1514 'page' => $pageId,
1515 'comment' => $summary,
1516 'minor_edit' => $minor,
1517 'text_id' => $current->rev_text_id,
1518 'parent_id' => $current->page_latest,
1519 'len' => $current->rev_len,
1520 'sha1' => $current->rev_sha1
1523 if ( $wgContentHandlerUseDB ) {
1524 $row['content_model'] = $current->rev_content_model;
1525 $row['content_format'] = $current->rev_content_format;
1528 $revision = new Revision( $row );
1529 $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
1530 } else {
1531 $revision = null;
1534 wfProfileOut( __METHOD__ );
1535 return $revision;
1539 * Determine if the current user is allowed to view a particular
1540 * field of this revision, if it's marked as deleted.
1542 * @param $field Integer:one of self::DELETED_TEXT,
1543 * self::DELETED_COMMENT,
1544 * self::DELETED_USER
1545 * @param $user User object to check, or null to use $wgUser
1546 * @return Boolean
1548 public function userCan( $field, User $user = null ) {
1549 return self::userCanBitfield( $this->mDeleted, $field, $user );
1553 * Determine if the current user is allowed to view a particular
1554 * field of this revision, if it's marked as deleted. This is used
1555 * by various classes to avoid duplication.
1557 * @param $bitfield Integer: current field
1558 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1559 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1560 * self::DELETED_USER = File::DELETED_USER
1561 * @param $user User object to check, or null to use $wgUser
1562 * @return Boolean
1564 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1565 if ( $bitfield & $field ) { // aspect is deleted
1566 if ( $bitfield & self::DELETED_RESTRICTED ) {
1567 $permission = 'suppressrevision';
1568 } elseif ( $field & self::DELETED_TEXT ) {
1569 $permission = 'deletedtext';
1570 } else {
1571 $permission = 'deletedhistory';
1573 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1574 if ( $user === null ) {
1575 global $wgUser;
1576 $user = $wgUser;
1578 return $user->isAllowed( $permission );
1579 } else {
1580 return true;
1585 * Get rev_timestamp from rev_id, without loading the rest of the row
1587 * @param $title Title
1588 * @param $id Integer
1589 * @return String
1591 static function getTimestampFromId( $title, $id ) {
1592 $dbr = wfGetDB( DB_SLAVE );
1593 // Casting fix for databases that can't take '' for rev_id
1594 if ( $id == '' ) {
1595 $id = 0;
1597 $conds = array( 'rev_id' => $id );
1598 $conds['rev_page'] = $title->getArticleID();
1599 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1600 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1601 # Not in slave, try master
1602 $dbw = wfGetDB( DB_MASTER );
1603 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1605 return wfTimestamp( TS_MW, $timestamp );
1609 * Get count of revisions per page...not very efficient
1611 * @param $db DatabaseBase
1612 * @param $id Integer: page id
1613 * @return Integer
1615 static function countByPageId( $db, $id ) {
1616 $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
1617 array( 'rev_page' => $id ), __METHOD__ );
1618 if ( $row ) {
1619 return $row->revCount;
1621 return 0;
1625 * Get count of revisions per page...not very efficient
1627 * @param $db DatabaseBase
1628 * @param $title Title
1629 * @return Integer
1631 static function countByTitle( $db, $title ) {
1632 $id = $title->getArticleID();
1633 if ( $id ) {
1634 return self::countByPageId( $db, $id );
1636 return 0;
1640 * Check if no edits were made by other users since
1641 * the time a user started editing the page. Limit to
1642 * 50 revisions for the sake of performance.
1644 * @since 1.20
1646 * @param DatabaseBase|int $db the Database to perform the check on. May be given as a Database object or
1647 * a database identifier usable with wfGetDB.
1648 * @param int $pageId the ID of the page in question
1649 * @param int $userId the ID of the user in question
1650 * @param string $since look at edits since this time
1652 * @return bool True if the given user was the only one to edit since the given timestamp
1654 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1655 if ( !$userId ) {
1656 return false;
1659 if ( is_int( $db ) ) {
1660 $db = wfGetDB( $db );
1663 $res = $db->select( 'revision',
1664 'rev_user',
1665 array(
1666 'rev_page' => $pageId,
1667 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1669 __METHOD__,
1670 array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
1671 foreach ( $res as $row ) {
1672 if ( $row->rev_user != $userId ) {
1673 return false;
1676 return true;