Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / Revision.php
blob2bdc7c0016c303d4f5d17d2f84945c282956c104
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 $rc = $this->getRecentChange();
877 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
878 $this->mUnpatrolled = $rc->getAttribute( 'rc_id' );
879 } else {
880 $this->mUnpatrolled = 0;
882 return $this->mUnpatrolled;
886 * Get the RC object belonging to the current revision, if there's one
888 * @since 1.22
889 * @return RecentChange|null
891 public function getRecentChange() {
892 $dbr = wfGetDB( DB_SLAVE );
893 return RecentChange::newFromConds(
894 array(
895 'rc_user_text' => $this->getRawUserText(),
896 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
897 'rc_this_oldid' => $this->getId()
899 __METHOD__
904 * @param int $field one of DELETED_* bitfield constants
906 * @return Boolean
908 public function isDeleted( $field ) {
909 return ( $this->mDeleted & $field ) == $field;
913 * Get the deletion bitfield of the revision
915 * @return int
917 public function getVisibility() {
918 return (int)$this->mDeleted;
922 * Fetch revision text if it's available to the specified audience.
923 * If the specified audience does not have the ability to view this
924 * revision, an empty string will be returned.
926 * @param $audience Integer: one of:
927 * Revision::FOR_PUBLIC to be displayed to all users
928 * Revision::FOR_THIS_USER to be displayed to the given user
929 * Revision::RAW get the text regardless of permissions
930 * @param $user User object to check for, only if FOR_THIS_USER is passed
931 * to the $audience parameter
933 * @deprecated in 1.21, use getContent() instead
934 * @todo Replace usage in core
935 * @return String
937 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
938 ContentHandler::deprecated( __METHOD__, '1.21' );
940 $content = $this->getContent( $audience, $user );
941 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
945 * Fetch revision content if it's available to the specified audience.
946 * If the specified audience does not have the ability to view this
947 * revision, null will be returned.
949 * @param $audience Integer: one of:
950 * Revision::FOR_PUBLIC to be displayed to all users
951 * Revision::FOR_THIS_USER to be displayed to $wgUser
952 * Revision::RAW get the text regardless of permissions
953 * @param $user User object to check for, only if FOR_THIS_USER is passed
954 * to the $audience parameter
955 * @since 1.21
956 * @return Content|null
958 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
959 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
960 return null;
961 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
962 return null;
963 } else {
964 return $this->getContentInternal();
969 * Alias for getText(Revision::FOR_THIS_USER)
971 * @deprecated since 1.17
972 * @return String
974 public function revText() {
975 wfDeprecated( __METHOD__, '1.17' );
976 return $this->getText( self::FOR_THIS_USER );
980 * Fetch revision text without regard for view restrictions
982 * @return String
984 * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW )
985 * or Revision::getSerializedData() as appropriate.
987 public function getRawText() {
988 ContentHandler::deprecated( __METHOD__, "1.21" );
989 return $this->getText( self::RAW );
993 * Fetch original serialized data without regard for view restrictions
995 * @since 1.21
996 * @return String
998 public function getSerializedData() {
999 if ( is_null( $this->mText ) ) {
1000 $this->mText = $this->loadText();
1003 return $this->mText;
1007 * Gets the content object for the revision (or null on failure).
1009 * Note that for mutable Content objects, each call to this method will return a
1010 * fresh clone.
1012 * @since 1.21
1013 * @return Content|null the Revision's content, or null on failure.
1015 protected function getContentInternal() {
1016 if ( is_null( $this->mContent ) ) {
1017 // Revision is immutable. Load on demand:
1018 if ( is_null( $this->mText ) ) {
1019 $this->mText = $this->loadText();
1022 if ( $this->mText !== null && $this->mText !== false ) {
1023 // Unserialize content
1024 $handler = $this->getContentHandler();
1025 $format = $this->getContentFormat();
1027 $this->mContent = $handler->unserializeContent( $this->mText, $format );
1028 } else {
1029 $this->mContent = false; // negative caching!
1033 // NOTE: copy() will return $this for immutable content objects
1034 return $this->mContent ? $this->mContent->copy() : null;
1038 * Returns the content model for this revision.
1040 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
1041 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1042 * is used as a last resort.
1044 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
1046 public function getContentModel() {
1047 if ( !$this->mContentModel ) {
1048 $title = $this->getTitle();
1049 $this->mContentModel = ( $title ? $title->getContentModel() : CONTENT_MODEL_WIKITEXT );
1051 assert( !empty( $this->mContentModel ) );
1054 return $this->mContentModel;
1058 * Returns the content format for this revision.
1060 * If no content format was stored in the database, the default format for this
1061 * revision's content model is returned.
1063 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1065 public function getContentFormat() {
1066 if ( !$this->mContentFormat ) {
1067 $handler = $this->getContentHandler();
1068 $this->mContentFormat = $handler->getDefaultFormat();
1070 assert( !empty( $this->mContentFormat ) );
1073 return $this->mContentFormat;
1077 * Returns the content handler appropriate for this revision's content model.
1079 * @throws MWException
1080 * @return ContentHandler
1082 public function getContentHandler() {
1083 if ( !$this->mContentHandler ) {
1084 $model = $this->getContentModel();
1085 $this->mContentHandler = ContentHandler::getForModelID( $model );
1087 $format = $this->getContentFormat();
1089 if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
1090 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1094 return $this->mContentHandler;
1098 * @return String
1100 public function getTimestamp() {
1101 return wfTimestamp( TS_MW, $this->mTimestamp );
1105 * @return Boolean
1107 public function isCurrent() {
1108 return $this->mCurrent;
1112 * Get previous revision for this title
1114 * @return Revision|null
1116 public function getPrevious() {
1117 if ( $this->getTitle() ) {
1118 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1119 if ( $prev ) {
1120 return self::newFromTitle( $this->getTitle(), $prev );
1123 return null;
1127 * Get next revision for this title
1129 * @return Revision or null
1131 public function getNext() {
1132 if ( $this->getTitle() ) {
1133 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1134 if ( $next ) {
1135 return self::newFromTitle( $this->getTitle(), $next );
1138 return null;
1142 * Get previous revision Id for this page_id
1143 * This is used to populate rev_parent_id on save
1145 * @param $db DatabaseBase
1146 * @return Integer
1148 private function getPreviousRevisionId( $db ) {
1149 if ( is_null( $this->mPage ) ) {
1150 return 0;
1152 # Use page_latest if ID is not given
1153 if ( !$this->mId ) {
1154 $prevId = $db->selectField( 'page', 'page_latest',
1155 array( 'page_id' => $this->mPage ),
1156 __METHOD__ );
1157 } else {
1158 $prevId = $db->selectField( 'revision', 'rev_id',
1159 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
1160 __METHOD__,
1161 array( 'ORDER BY' => 'rev_id DESC' ) );
1163 return intval( $prevId );
1167 * Get revision text associated with an old or archive row
1168 * $row is usually an object from wfFetchRow(), both the flags and the text
1169 * field must be included
1171 * @param $row Object: the text data
1172 * @param string $prefix table prefix (default 'old_')
1173 * @param string|false $wiki the name of the wiki to load the revision text from
1174 * (same as the the wiki $row was loaded from) or false to indicate the local
1175 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1176 * identifier as understood by the LoadBalancer class.
1177 * @return String: text the text requested or false on failure
1179 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1180 wfProfileIn( __METHOD__ );
1182 # Get data
1183 $textField = $prefix . 'text';
1184 $flagsField = $prefix . 'flags';
1186 if ( isset( $row->$flagsField ) ) {
1187 $flags = explode( ',', $row->$flagsField );
1188 } else {
1189 $flags = array();
1192 if ( isset( $row->$textField ) ) {
1193 $text = $row->$textField;
1194 } else {
1195 wfProfileOut( __METHOD__ );
1196 return false;
1199 # Use external methods for external objects, text in table is URL-only then
1200 if ( in_array( 'external', $flags ) ) {
1201 $url = $text;
1202 $parts = explode( '://', $url, 2 );
1203 if ( count( $parts ) == 1 || $parts[1] == '' ) {
1204 wfProfileOut( __METHOD__ );
1205 return false;
1207 $text = ExternalStore::fetchFromURL( $url, array( 'wiki' => $wiki ) );
1210 // If the text was fetched without an error, convert it
1211 if ( $text !== false ) {
1212 if ( in_array( 'gzip', $flags ) ) {
1213 # Deal with optional compression of archived pages.
1214 # This can be done periodically via maintenance/compressOld.php, and
1215 # as pages are saved if $wgCompressRevisions is set.
1216 $text = gzinflate( $text );
1219 if ( in_array( 'object', $flags ) ) {
1220 # Generic compressed storage
1221 $obj = unserialize( $text );
1222 if ( !is_object( $obj ) ) {
1223 // Invalid object
1224 wfProfileOut( __METHOD__ );
1225 return false;
1227 $text = $obj->getText();
1230 global $wgLegacyEncoding;
1231 if ( $text !== false && $wgLegacyEncoding
1232 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1234 # Old revisions kept around in a legacy encoding?
1235 # Upconvert on demand.
1236 # ("utf8" checked for compatibility with some broken
1237 # conversion scripts 2008-12-30)
1238 global $wgContLang;
1239 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1242 wfProfileOut( __METHOD__ );
1243 return $text;
1247 * If $wgCompressRevisions is enabled, we will compress data.
1248 * The input string is modified in place.
1249 * Return value is the flags field: contains 'gzip' if the
1250 * data is compressed, and 'utf-8' if we're saving in UTF-8
1251 * mode.
1253 * @param $text Mixed: reference to a text
1254 * @return String
1256 public static function compressRevisionText( &$text ) {
1257 global $wgCompressRevisions;
1258 $flags = array();
1260 # Revisions not marked this way will be converted
1261 # on load if $wgLegacyCharset is set in the future.
1262 $flags[] = 'utf-8';
1264 if ( $wgCompressRevisions ) {
1265 if ( function_exists( 'gzdeflate' ) ) {
1266 $text = gzdeflate( $text );
1267 $flags[] = 'gzip';
1268 } else {
1269 wfDebug( __METHOD__ . " -- no zlib support, not compressing\n" );
1272 return implode( ',', $flags );
1276 * Insert a new revision into the database, returning the new revision ID
1277 * number on success and dies horribly on failure.
1279 * @param $dbw DatabaseBase: (master connection)
1280 * @throws MWException
1281 * @return Integer
1283 public function insertOn( $dbw ) {
1284 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1286 wfProfileIn( __METHOD__ );
1288 $this->checkContentModel();
1290 $data = $this->mText;
1291 $flags = self::compressRevisionText( $data );
1293 # Write to external storage if required
1294 if ( $wgDefaultExternalStore ) {
1295 // Store and get the URL
1296 $data = ExternalStore::insertToDefault( $data );
1297 if ( !$data ) {
1298 wfProfileOut( __METHOD__ );
1299 throw new MWException( "Unable to store text to external storage" );
1301 if ( $flags ) {
1302 $flags .= ',';
1304 $flags .= 'external';
1307 # Record the text (or external storage URL) to the text table
1308 if ( !isset( $this->mTextId ) ) {
1309 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1310 $dbw->insert( 'text',
1311 array(
1312 'old_id' => $old_id,
1313 'old_text' => $data,
1314 'old_flags' => $flags,
1315 ), __METHOD__
1317 $this->mTextId = $dbw->insertId();
1320 if ( $this->mComment === null ) {
1321 $this->mComment = "";
1324 # Record the edit in revisions
1325 $rev_id = isset( $this->mId )
1326 ? $this->mId
1327 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1328 $row = array(
1329 'rev_id' => $rev_id,
1330 'rev_page' => $this->mPage,
1331 'rev_text_id' => $this->mTextId,
1332 'rev_comment' => $this->mComment,
1333 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1334 'rev_user' => $this->mUser,
1335 'rev_user_text' => $this->mUserText,
1336 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1337 'rev_deleted' => $this->mDeleted,
1338 'rev_len' => $this->mSize,
1339 'rev_parent_id' => is_null( $this->mParentId )
1340 ? $this->getPreviousRevisionId( $dbw )
1341 : $this->mParentId,
1342 'rev_sha1' => is_null( $this->mSha1 )
1343 ? Revision::base36Sha1( $this->mText )
1344 : $this->mSha1,
1347 if ( $wgContentHandlerUseDB ) {
1348 //NOTE: Store null for the default model and format, to save space.
1349 //XXX: Makes the DB sensitive to changed defaults. Make this behavior optional? Only in miser mode?
1351 $model = $this->getContentModel();
1352 $format = $this->getContentFormat();
1354 $title = $this->getTitle();
1356 if ( $title === null ) {
1357 wfProfileOut( __METHOD__ );
1358 throw new MWException( "Insufficient information to determine the title of the revision's page!" );
1361 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1362 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
1364 $row['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
1365 $row['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
1368 $dbw->insert( 'revision', $row, __METHOD__ );
1370 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1372 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1374 wfProfileOut( __METHOD__ );
1375 return $this->mId;
1378 protected function checkContentModel() {
1379 global $wgContentHandlerUseDB;
1381 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1383 $model = $this->getContentModel();
1384 $format = $this->getContentFormat();
1385 $handler = $this->getContentHandler();
1387 if ( !$handler->isSupportedFormat( $format ) ) {
1388 $t = $title->getPrefixedDBkey();
1390 throw new MWException( "Can't use format $format with content model $model on $t" );
1393 if ( !$wgContentHandlerUseDB && $title ) {
1394 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1396 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1397 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1398 $defaultFormat = $defaultHandler->getDefaultFormat();
1400 if ( $this->getContentModel() != $defaultModel ) {
1401 $t = $title->getPrefixedDBkey();
1403 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: "
1404 . "model is $model , default for $t is $defaultModel" );
1407 if ( $this->getContentFormat() != $defaultFormat ) {
1408 $t = $title->getPrefixedDBkey();
1410 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: "
1411 . "format is $format, default for $t is $defaultFormat" );
1415 $content = $this->getContent( Revision::RAW );
1417 if ( !$content || !$content->isValid() ) {
1418 $t = $title->getPrefixedDBkey();
1420 throw new MWException( "Content of $t is not valid! Content model is $model" );
1425 * Get the base 36 SHA-1 value for a string of text
1426 * @param $text String
1427 * @return String
1429 public static function base36Sha1( $text ) {
1430 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1434 * Lazy-load the revision's text.
1435 * Currently hardcoded to the 'text' table storage engine.
1437 * @return String|bool the revision's text, or false on failure
1439 protected function loadText() {
1440 wfProfileIn( __METHOD__ );
1442 // Caching may be beneficial for massive use of external storage
1443 global $wgRevisionCacheExpiry, $wgMemc;
1444 $textId = $this->getTextId();
1445 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1446 if ( $wgRevisionCacheExpiry ) {
1447 $text = $wgMemc->get( $key );
1448 if ( is_string( $text ) ) {
1449 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1450 wfProfileOut( __METHOD__ );
1451 return $text;
1455 // If we kept data for lazy extraction, use it now...
1456 if ( isset( $this->mTextRow ) ) {
1457 $row = $this->mTextRow;
1458 $this->mTextRow = null;
1459 } else {
1460 $row = null;
1463 if ( !$row ) {
1464 // Text data is immutable; check slaves first.
1465 $dbr = wfGetDB( DB_SLAVE );
1466 $row = $dbr->selectRow( 'text',
1467 array( 'old_text', 'old_flags' ),
1468 array( 'old_id' => $this->getTextId() ),
1469 __METHOD__ );
1472 if ( !$row && wfGetLB()->getServerCount() > 1 ) {
1473 // Possible slave lag!
1474 $dbw = wfGetDB( DB_MASTER );
1475 $row = $dbw->selectRow( 'text',
1476 array( 'old_text', 'old_flags' ),
1477 array( 'old_id' => $this->getTextId() ),
1478 __METHOD__ );
1481 $text = self::getRevisionText( $row );
1483 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1484 if ( $wgRevisionCacheExpiry && $text !== false ) {
1485 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1488 wfProfileOut( __METHOD__ );
1490 return $text;
1494 * Create a new null-revision for insertion into a page's
1495 * history. This will not re-save the text, but simply refer
1496 * to the text from the previous version.
1498 * Such revisions can for instance identify page rename
1499 * operations and other such meta-modifications.
1501 * @param $dbw DatabaseBase
1502 * @param $pageId Integer: ID number of the page to read from
1503 * @param string $summary revision's summary
1504 * @param $minor Boolean: whether the revision should be considered as minor
1505 * @return Revision|null on error
1507 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1508 global $wgContentHandlerUseDB;
1510 wfProfileIn( __METHOD__ );
1512 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1513 'rev_text_id', 'rev_len', 'rev_sha1' );
1515 if ( $wgContentHandlerUseDB ) {
1516 $fields[] = 'rev_content_model';
1517 $fields[] = 'rev_content_format';
1520 $current = $dbw->selectRow(
1521 array( 'page', 'revision' ),
1522 $fields,
1523 array(
1524 'page_id' => $pageId,
1525 'page_latest=rev_id',
1527 __METHOD__ );
1529 if ( $current ) {
1530 $row = array(
1531 'page' => $pageId,
1532 'comment' => $summary,
1533 'minor_edit' => $minor,
1534 'text_id' => $current->rev_text_id,
1535 'parent_id' => $current->page_latest,
1536 'len' => $current->rev_len,
1537 'sha1' => $current->rev_sha1
1540 if ( $wgContentHandlerUseDB ) {
1541 $row['content_model'] = $current->rev_content_model;
1542 $row['content_format'] = $current->rev_content_format;
1545 $revision = new Revision( $row );
1546 $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
1547 } else {
1548 $revision = null;
1551 wfProfileOut( __METHOD__ );
1552 return $revision;
1556 * Determine if the current user is allowed to view a particular
1557 * field of this revision, if it's marked as deleted.
1559 * @param $field Integer:one of self::DELETED_TEXT,
1560 * self::DELETED_COMMENT,
1561 * self::DELETED_USER
1562 * @param $user User object to check, or null to use $wgUser
1563 * @return Boolean
1565 public function userCan( $field, User $user = null ) {
1566 return self::userCanBitfield( $this->mDeleted, $field, $user );
1570 * Determine if the current user is allowed to view a particular
1571 * field of this revision, if it's marked as deleted. This is used
1572 * by various classes to avoid duplication.
1574 * @param $bitfield Integer: current field
1575 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1576 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1577 * self::DELETED_USER = File::DELETED_USER
1578 * @param $user User object to check, or null to use $wgUser
1579 * @return Boolean
1581 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1582 if ( $bitfield & $field ) { // aspect is deleted
1583 if ( $bitfield & self::DELETED_RESTRICTED ) {
1584 $permission = 'suppressrevision';
1585 } elseif ( $field & self::DELETED_TEXT ) {
1586 $permission = 'deletedtext';
1587 } else {
1588 $permission = 'deletedhistory';
1590 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1591 if ( $user === null ) {
1592 global $wgUser;
1593 $user = $wgUser;
1595 return $user->isAllowed( $permission );
1596 } else {
1597 return true;
1602 * Get rev_timestamp from rev_id, without loading the rest of the row
1604 * @param $title Title
1605 * @param $id Integer
1606 * @return String
1608 static function getTimestampFromId( $title, $id ) {
1609 $dbr = wfGetDB( DB_SLAVE );
1610 // Casting fix for databases that can't take '' for rev_id
1611 if ( $id == '' ) {
1612 $id = 0;
1614 $conds = array( 'rev_id' => $id );
1615 $conds['rev_page'] = $title->getArticleID();
1616 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1617 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1618 # Not in slave, try master
1619 $dbw = wfGetDB( DB_MASTER );
1620 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1622 return wfTimestamp( TS_MW, $timestamp );
1626 * Get count of revisions per page...not very efficient
1628 * @param $db DatabaseBase
1629 * @param $id Integer: page id
1630 * @return Integer
1632 static function countByPageId( $db, $id ) {
1633 $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
1634 array( 'rev_page' => $id ), __METHOD__ );
1635 if ( $row ) {
1636 return $row->revCount;
1638 return 0;
1642 * Get count of revisions per page...not very efficient
1644 * @param $db DatabaseBase
1645 * @param $title Title
1646 * @return Integer
1648 static function countByTitle( $db, $title ) {
1649 $id = $title->getArticleID();
1650 if ( $id ) {
1651 return self::countByPageId( $db, $id );
1653 return 0;
1657 * Check if no edits were made by other users since
1658 * the time a user started editing the page. Limit to
1659 * 50 revisions for the sake of performance.
1661 * @since 1.20
1663 * @param DatabaseBase|int $db the Database to perform the check on. May be given as a Database object or
1664 * a database identifier usable with wfGetDB.
1665 * @param int $pageId the ID of the page in question
1666 * @param int $userId the ID of the user in question
1667 * @param string $since look at edits since this time
1669 * @return bool True if the given user was the only one to edit since the given timestamp
1671 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1672 if ( !$userId ) {
1673 return false;
1676 if ( is_int( $db ) ) {
1677 $db = wfGetDB( $db );
1680 $res = $db->select( 'revision',
1681 'rev_user',
1682 array(
1683 'rev_page' => $pageId,
1684 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1686 __METHOD__,
1687 array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
1688 foreach ( $res as $row ) {
1689 if ( $row->rev_user != $userId ) {
1690 return false;
1693 return true;