Merge "Expand release notes for Iab02cbd4"
[mediawiki.git] / includes / Revision.php
blob444617339c2fd35c67e7d769d80e2295a4e3f2e5
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 /**
65 * @var int
67 protected $mQueryFlags = 0;
69 // Revision deletion constants
70 const DELETED_TEXT = 1;
71 const DELETED_COMMENT = 2;
72 const DELETED_USER = 4;
73 const DELETED_RESTRICTED = 8;
74 const SUPPRESSED_USER = 12; // convenience
76 // Audience options for accessors
77 const FOR_PUBLIC = 1;
78 const FOR_THIS_USER = 2;
79 const RAW = 3;
81 /**
82 * Load a page revision from a given revision ID number.
83 * Returns null if no such revision can be found.
85 * $flags include:
86 * Revision::READ_LATEST : Select the data from the master
87 * Revision::READ_LOCKING : Select & lock the data from the master
89 * @param $id Integer
90 * @param $flags Integer (optional)
91 * @return Revision or null
93 public static function newFromId( $id, $flags = 0 ) {
94 return self::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
97 /**
98 * Load either the current, or a specified, revision
99 * that's attached to a given title. If not attached
100 * to that title, will return null.
102 * $flags include:
103 * Revision::READ_LATEST : Select the data from the master
104 * Revision::READ_LOCKING : Select & lock the data from the master
106 * @param $title Title
107 * @param $id Integer (optional)
108 * @param $flags Integer Bitfield (optional)
109 * @return Revision or null
111 public static function newFromTitle( $title, $id = 0, $flags = 0 ) {
112 $conds = array(
113 'page_namespace' => $title->getNamespace(),
114 'page_title' => $title->getDBkey()
116 if ( $id ) {
117 // Use the specified ID
118 $conds['rev_id'] = $id;
119 return self::newFromConds( $conds, (int)$flags );
120 } else {
121 // Use a join to get the latest revision
122 $conds[] = 'rev_id=page_latest';
123 $db = wfGetDB( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_SLAVE );
124 return self::loadFromConds( $db, $conds, $flags );
129 * Load either the current, or a specified, revision
130 * that's attached to a given page ID.
131 * Returns null if no such revision can be found.
133 * $flags include:
134 * Revision::READ_LATEST : Select the data from the master (since 1.20)
135 * Revision::READ_LOCKING : Select & lock the data from the master
137 * @param $revId Integer
138 * @param $pageId Integer (optional)
139 * @param $flags Integer Bitfield (optional)
140 * @return Revision or null
142 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
143 $conds = array( 'page_id' => $pageId );
144 if ( $revId ) {
145 $conds['rev_id'] = $revId;
146 } else {
147 // Use a join to get the latest revision
148 $conds[] = 'rev_id = page_latest';
150 return self::newFromConds( $conds, (int)$flags );
154 * Make a fake revision object from an archive table row. This is queried
155 * for permissions or even inserted (as in Special:Undelete)
156 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
158 * @param $row
159 * @param $overrides array
161 * @throws MWException
162 * @return Revision
164 public static function newFromArchiveRow( $row, $overrides = array() ) {
165 global $wgContentHandlerUseDB;
167 $attribs = $overrides + array(
168 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
169 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
170 'comment' => $row->ar_comment,
171 'user' => $row->ar_user,
172 'user_text' => $row->ar_user_text,
173 'timestamp' => $row->ar_timestamp,
174 'minor_edit' => $row->ar_minor_edit,
175 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
176 'deleted' => $row->ar_deleted,
177 'len' => $row->ar_len,
178 'sha1' => isset( $row->ar_sha1 ) ? $row->ar_sha1 : null,
179 'content_model' => isset( $row->ar_content_model ) ? $row->ar_content_model : null,
180 'content_format' => isset( $row->ar_content_format ) ? $row->ar_content_format : null,
183 if ( !$wgContentHandlerUseDB ) {
184 unset( $attribs['content_model'] );
185 unset( $attribs['content_format'] );
188 if ( !isset( $attribs['title'] )
189 && isset( $row->ar_namespace )
190 && isset( $row->ar_title ) ) {
192 $attribs['title'] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
195 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
196 // Pre-1.5 ar_text row
197 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
198 if ( $attribs['text'] === false ) {
199 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
202 return new self( $attribs );
206 * @since 1.19
208 * @param $row
209 * @return Revision
211 public static function newFromRow( $row ) {
212 return new self( $row );
216 * Load a page revision from a given revision ID number.
217 * Returns null if no such revision can be found.
219 * @param $db DatabaseBase
220 * @param $id Integer
221 * @return Revision or null
223 public static function loadFromId( $db, $id ) {
224 return self::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
228 * Load either the current, or a specified, revision
229 * that's attached to a given page. If not attached
230 * to that page, will return null.
232 * @param $db DatabaseBase
233 * @param $pageid Integer
234 * @param $id Integer
235 * @return Revision or null
237 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
238 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
239 if ( $id ) {
240 $conds['rev_id'] = intval( $id );
241 } else {
242 $conds[] = 'rev_id=page_latest';
244 return self::loadFromConds( $db, $conds );
248 * Load either the current, or a specified, revision
249 * that's attached to a given page. If not attached
250 * to that page, will return null.
252 * @param $db DatabaseBase
253 * @param $title Title
254 * @param $id Integer
255 * @return Revision or null
257 public static function loadFromTitle( $db, $title, $id = 0 ) {
258 if ( $id ) {
259 $matchId = intval( $id );
260 } else {
261 $matchId = 'page_latest';
263 return self::loadFromConds( $db,
264 array(
265 "rev_id=$matchId",
266 'page_namespace' => $title->getNamespace(),
267 'page_title' => $title->getDBkey()
273 * Load the revision for the given title with the given timestamp.
274 * WARNING: Timestamps may in some circumstances not be unique,
275 * so this isn't the best key to use.
277 * @param $db DatabaseBase
278 * @param $title Title
279 * @param $timestamp String
280 * @return Revision or null
282 public static function loadFromTimestamp( $db, $title, $timestamp ) {
283 return self::loadFromConds( $db,
284 array(
285 'rev_timestamp' => $db->timestamp( $timestamp ),
286 'page_namespace' => $title->getNamespace(),
287 'page_title' => $title->getDBkey()
293 * Given a set of conditions, fetch a revision.
295 * @param $conditions Array
296 * @param $flags integer (optional)
297 * @return Revision or null
299 private static function newFromConds( $conditions, $flags = 0 ) {
300 $db = wfGetDB( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_SLAVE );
301 $rev = self::loadFromConds( $db, $conditions, $flags );
302 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
303 if ( !( $flags & self::READ_LATEST ) ) {
304 $dbw = wfGetDB( DB_MASTER );
305 $rev = self::loadFromConds( $dbw, $conditions, $flags );
308 if ( $rev ) {
309 $rev->mQueryFlags = $flags;
311 return $rev;
315 * Given a set of conditions, fetch a revision from
316 * the given database connection.
318 * @param $db DatabaseBase
319 * @param $conditions Array
320 * @param $flags integer (optional)
321 * @return Revision or null
323 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
324 $res = self::fetchFromConds( $db, $conditions, $flags );
325 if ( $res ) {
326 $row = $res->fetchObject();
327 if ( $row ) {
328 $ret = new Revision( $row );
329 return $ret;
332 $ret = null;
333 return $ret;
337 * Return a wrapper for a series of database rows to
338 * fetch all of a given page's revisions in turn.
339 * Each row can be fed to the constructor to get objects.
341 * @param $title Title
342 * @return ResultWrapper
344 public static function fetchRevision( $title ) {
345 return self::fetchFromConds(
346 wfGetDB( DB_SLAVE ),
347 array(
348 'rev_id=page_latest',
349 'page_namespace' => $title->getNamespace(),
350 'page_title' => $title->getDBkey()
356 * Given a set of conditions, return a ResultWrapper
357 * which will return matching database rows with the
358 * fields necessary to build Revision objects.
360 * @param $db DatabaseBase
361 * @param $conditions Array
362 * @param $flags integer (optional)
363 * @return ResultWrapper
365 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
366 $fields = array_merge(
367 self::selectFields(),
368 self::selectPageFields(),
369 self::selectUserFields()
371 $options = array( 'LIMIT' => 1 );
372 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
373 $options[] = 'FOR UPDATE';
375 return $db->select(
376 array( 'revision', 'page', 'user' ),
377 $fields,
378 $conditions,
379 __METHOD__,
380 $options,
381 array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
386 * Return the value of a select() JOIN conds array for the user table.
387 * This will get user table rows for logged-in users.
388 * @since 1.19
389 * @return Array
391 public static function userJoinCond() {
392 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
396 * Return the value of a select() page conds array for the page table.
397 * This will assure that the revision(s) are not orphaned from live pages.
398 * @since 1.19
399 * @return Array
401 public static function pageJoinCond() {
402 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
406 * Return the list of revision fields that should be selected to create
407 * a new revision.
408 * @return array
410 public static function selectFields() {
411 global $wgContentHandlerUseDB;
413 $fields = array(
414 'rev_id',
415 'rev_page',
416 'rev_text_id',
417 'rev_timestamp',
418 'rev_comment',
419 'rev_user_text',
420 'rev_user',
421 'rev_minor_edit',
422 'rev_deleted',
423 'rev_len',
424 'rev_parent_id',
425 'rev_sha1',
428 if ( $wgContentHandlerUseDB ) {
429 $fields[] = 'rev_content_format';
430 $fields[] = 'rev_content_model';
433 return $fields;
437 * Return the list of revision fields that should be selected to create
438 * a new revision from an archive row.
439 * @return array
441 public static function selectArchiveFields() {
442 global $wgContentHandlerUseDB;
443 $fields = array(
444 'ar_id',
445 'ar_page_id',
446 'ar_rev_id',
447 'ar_text_id',
448 'ar_timestamp',
449 'ar_comment',
450 'ar_user_text',
451 'ar_user',
452 'ar_minor_edit',
453 'ar_deleted',
454 'ar_len',
455 'ar_parent_id',
456 'ar_sha1',
459 if ( $wgContentHandlerUseDB ) {
460 $fields[] = 'ar_content_format';
461 $fields[] = 'ar_content_model';
463 return $fields;
467 * Return the list of text fields that should be selected to read the
468 * revision text
469 * @return array
471 public static function selectTextFields() {
472 return array(
473 'old_text',
474 'old_flags'
479 * Return the list of page fields that should be selected from page table
480 * @return array
482 public static function selectPageFields() {
483 return array(
484 'page_namespace',
485 'page_title',
486 'page_id',
487 'page_latest',
488 'page_is_redirect',
489 'page_len',
494 * Return the list of user fields that should be selected from user table
495 * @return array
497 public static function selectUserFields() {
498 return array( 'user_name' );
502 * Do a batched query to get the parent revision lengths
503 * @param $db DatabaseBase
504 * @param $revIds Array
505 * @return array
507 public static function getParentLengths( $db, array $revIds ) {
508 $revLens = array();
509 if ( !$revIds ) {
510 return $revLens; // empty
512 wfProfileIn( __METHOD__ );
513 $res = $db->select( 'revision',
514 array( 'rev_id', 'rev_len' ),
515 array( 'rev_id' => $revIds ),
516 __METHOD__ );
517 foreach ( $res as $row ) {
518 $revLens[$row->rev_id] = $row->rev_len;
520 wfProfileOut( __METHOD__ );
521 return $revLens;
525 * Constructor
527 * @param $row Mixed: either a database row or an array
528 * @throws MWException
529 * @access private
531 function __construct( $row ) {
532 if ( is_object( $row ) ) {
533 $this->mId = intval( $row->rev_id );
534 $this->mPage = intval( $row->rev_page );
535 $this->mTextId = intval( $row->rev_text_id );
536 $this->mComment = $row->rev_comment;
537 $this->mUser = intval( $row->rev_user );
538 $this->mMinorEdit = intval( $row->rev_minor_edit );
539 $this->mTimestamp = $row->rev_timestamp;
540 $this->mDeleted = intval( $row->rev_deleted );
542 if ( !isset( $row->rev_parent_id ) ) {
543 $this->mParentId = null;
544 } else {
545 $this->mParentId = intval( $row->rev_parent_id );
548 if ( !isset( $row->rev_len ) ) {
549 $this->mSize = null;
550 } else {
551 $this->mSize = intval( $row->rev_len );
554 if ( !isset( $row->rev_sha1 ) ) {
555 $this->mSha1 = null;
556 } else {
557 $this->mSha1 = $row->rev_sha1;
560 if ( isset( $row->page_latest ) ) {
561 $this->mCurrent = ( $row->rev_id == $row->page_latest );
562 $this->mTitle = Title::newFromRow( $row );
563 } else {
564 $this->mCurrent = false;
565 $this->mTitle = null;
568 if ( !isset( $row->rev_content_model ) || is_null( $row->rev_content_model ) ) {
569 $this->mContentModel = null; # determine on demand if needed
570 } else {
571 $this->mContentModel = strval( $row->rev_content_model );
574 if ( !isset( $row->rev_content_format ) || is_null( $row->rev_content_format ) ) {
575 $this->mContentFormat = null; # determine on demand if needed
576 } else {
577 $this->mContentFormat = strval( $row->rev_content_format );
580 // Lazy extraction...
581 $this->mText = null;
582 if ( isset( $row->old_text ) ) {
583 $this->mTextRow = $row;
584 } else {
585 // 'text' table row entry will be lazy-loaded
586 $this->mTextRow = null;
589 // Use user_name for users and rev_user_text for IPs...
590 $this->mUserText = null; // lazy load if left null
591 if ( $this->mUser == 0 ) {
592 $this->mUserText = $row->rev_user_text; // IP user
593 } elseif ( isset( $row->user_name ) ) {
594 $this->mUserText = $row->user_name; // logged-in user
596 $this->mOrigUserText = $row->rev_user_text;
597 } elseif ( is_array( $row ) ) {
598 // Build a new revision to be saved...
599 global $wgUser; // ugh
601 # if we have a content object, use it to set the model and type
602 if ( !empty( $row['content'] ) ) {
603 // @todo when is that set? test with external store setup! check out insertOn() [dk]
604 if ( !empty( $row['text_id'] ) ) {
605 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
606 "can't serialize content object" );
609 $row['content_model'] = $row['content']->getModel();
610 # note: mContentFormat is initializes later accordingly
611 # note: content is serialized later in this method!
612 # also set text to null?
615 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
616 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
617 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
618 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
619 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
620 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
621 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestampNow();
622 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
623 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
624 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
625 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
627 $this->mContentModel = isset( $row['content_model'] ) ? strval( $row['content_model'] ) : null;
628 $this->mContentFormat = isset( $row['content_format'] ) ? strval( $row['content_format'] ) : null;
630 // Enforce spacing trimming on supplied text
631 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
632 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
633 $this->mTextRow = null;
635 $this->mTitle = isset( $row['title'] ) ? $row['title'] : null;
637 // if we have a Content object, override mText and mContentModel
638 if ( !empty( $row['content'] ) ) {
639 if ( !( $row['content'] instanceof Content ) ) {
640 throw new MWException( '`content` field must contain a Content object.' );
643 $handler = $this->getContentHandler();
644 $this->mContent = $row['content'];
646 $this->mContentModel = $this->mContent->getModel();
647 $this->mContentHandler = null;
649 $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
650 } elseif ( !is_null( $this->mText ) ) {
651 $handler = $this->getContentHandler();
652 $this->mContent = $handler->unserializeContent( $this->mText );
655 // If we have a Title object, make sure it is consistent with mPage.
656 if ( $this->mTitle && $this->mTitle->exists() ) {
657 if ( $this->mPage === null ) {
658 // if the page ID wasn't known, set it now
659 $this->mPage = $this->mTitle->getArticleID();
660 } elseif ( $this->mTitle->getArticleID() !== $this->mPage ) {
661 // Got different page IDs. This may be legit (e.g. during undeletion),
662 // but it seems worth mentioning it in the log.
663 wfDebug( "Page ID " . $this->mPage . " mismatches the ID " .
664 $this->mTitle->getArticleID() . " provided by the Title object." );
668 $this->mCurrent = false;
670 // If we still have no length, see it we have the text to figure it out
671 if ( !$this->mSize ) {
672 if ( !is_null( $this->mContent ) ) {
673 $this->mSize = $this->mContent->getSize();
674 } else {
675 #NOTE: this should never happen if we have either text or content object!
676 $this->mSize = null;
680 // Same for sha1
681 if ( $this->mSha1 === null ) {
682 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
685 // force lazy init
686 $this->getContentModel();
687 $this->getContentFormat();
688 } else {
689 throw new MWException( 'Revision constructor passed invalid row format.' );
691 $this->mUnpatrolled = null;
695 * Get revision ID
697 * @return Integer|null
699 public function getId() {
700 return $this->mId;
704 * Set the revision ID
706 * @since 1.19
707 * @param $id Integer
709 public function setId( $id ) {
710 $this->mId = $id;
714 * Get text row ID
716 * @return Integer|null
718 public function getTextId() {
719 return $this->mTextId;
723 * Get parent revision ID (the original previous page revision)
725 * @return Integer|null
727 public function getParentId() {
728 return $this->mParentId;
732 * Returns the length of the text in this revision, or null if unknown.
734 * @return Integer|null
736 public function getSize() {
737 return $this->mSize;
741 * Returns the base36 sha1 of the text in this revision, or null if unknown.
743 * @return String|null
745 public function getSha1() {
746 return $this->mSha1;
750 * Returns the title of the page associated with this entry or null.
752 * Will do a query, when title is not set and id is given.
754 * @return Title|null
756 public function getTitle() {
757 if ( isset( $this->mTitle ) ) {
758 return $this->mTitle;
760 if ( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
761 $dbr = wfGetDB( DB_SLAVE );
762 $row = $dbr->selectRow(
763 array( 'page', 'revision' ),
764 self::selectPageFields(),
765 array( 'page_id=rev_page',
766 'rev_id' => $this->mId ),
767 __METHOD__ );
768 if ( $row ) {
769 $this->mTitle = Title::newFromRow( $row );
773 if ( !$this->mTitle && !is_null( $this->mPage ) && $this->mPage > 0 ) {
774 $this->mTitle = Title::newFromID( $this->mPage );
777 return $this->mTitle;
781 * Set the title of the revision
783 * @param $title Title
785 public function setTitle( $title ) {
786 $this->mTitle = $title;
790 * Get the page ID
792 * @return Integer|null
794 public function getPage() {
795 return $this->mPage;
799 * Fetch revision's user id if it's available to the specified audience.
800 * If the specified audience does not have access to it, zero will be
801 * returned.
803 * @param $audience Integer: one of:
804 * Revision::FOR_PUBLIC to be displayed to all users
805 * Revision::FOR_THIS_USER to be displayed to the given user
806 * Revision::RAW get the ID regardless of permissions
807 * @param $user User object to check for, only if FOR_THIS_USER is passed
808 * to the $audience parameter
809 * @return Integer
811 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
812 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
813 return 0;
814 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
815 return 0;
816 } else {
817 return $this->mUser;
822 * Fetch revision's user id without regard for the current user's permissions
824 * @return String
826 public function getRawUser() {
827 return $this->mUser;
831 * Fetch revision's username if it's available to the specified audience.
832 * If the specified audience does not have access to the username, 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 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
844 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
845 return '';
846 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
847 return '';
848 } else {
849 return $this->getRawUserText();
854 * Fetch revision's username without regard for view restrictions
856 * @return String
858 public function getRawUserText() {
859 if ( $this->mUserText === null ) {
860 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
861 if ( $this->mUserText === false ) {
862 # This shouldn't happen, but it can if the wiki was recovered
863 # via importing revs and there is no user table entry yet.
864 $this->mUserText = $this->mOrigUserText;
867 return $this->mUserText;
871 * Fetch revision comment if it's available to the specified audience.
872 * If the specified audience does not have access to the comment, an
873 * empty string will be returned.
875 * @param $audience Integer: one of:
876 * Revision::FOR_PUBLIC to be displayed to all users
877 * Revision::FOR_THIS_USER to be displayed to the given user
878 * Revision::RAW get the text regardless of permissions
879 * @param $user User object to check for, only if FOR_THIS_USER is passed
880 * to the $audience parameter
881 * @return String
883 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
884 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
885 return '';
886 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
887 return '';
888 } else {
889 return $this->mComment;
894 * Fetch revision comment without regard for the current user's permissions
896 * @return String
898 public function getRawComment() {
899 return $this->mComment;
903 * @return Boolean
905 public function isMinor() {
906 return (bool)$this->mMinorEdit;
910 * @return integer rcid of the unpatrolled row, zero if there isn't one
912 public function isUnpatrolled() {
913 if ( $this->mUnpatrolled !== null ) {
914 return $this->mUnpatrolled;
916 $rc = $this->getRecentChange();
917 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
918 $this->mUnpatrolled = $rc->getAttribute( 'rc_id' );
919 } else {
920 $this->mUnpatrolled = 0;
922 return $this->mUnpatrolled;
926 * Get the RC object belonging to the current revision, if there's one
928 * @since 1.22
929 * @return RecentChange|null
931 public function getRecentChange() {
932 $dbr = wfGetDB( DB_SLAVE );
933 return RecentChange::newFromConds(
934 array(
935 'rc_user_text' => $this->getRawUserText(),
936 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
937 'rc_this_oldid' => $this->getId()
939 __METHOD__
944 * @param int $field one of DELETED_* bitfield constants
946 * @return Boolean
948 public function isDeleted( $field ) {
949 return ( $this->mDeleted & $field ) == $field;
953 * Get the deletion bitfield of the revision
955 * @return int
957 public function getVisibility() {
958 return (int)$this->mDeleted;
962 * Fetch revision text if it's available to the specified audience.
963 * If the specified audience does not have the ability to view this
964 * revision, an empty string will be returned.
966 * @param $audience Integer: one of:
967 * Revision::FOR_PUBLIC to be displayed to all users
968 * Revision::FOR_THIS_USER to be displayed to the given user
969 * Revision::RAW get the text regardless of permissions
970 * @param $user User object to check for, only if FOR_THIS_USER is passed
971 * to the $audience parameter
973 * @deprecated in 1.21, use getContent() instead
974 * @todo Replace usage in core
975 * @return String
977 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
978 ContentHandler::deprecated( __METHOD__, '1.21' );
980 $content = $this->getContent( $audience, $user );
981 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
985 * Fetch revision content if it's available to the specified audience.
986 * If the specified audience does not have the ability to view this
987 * revision, null will be returned.
989 * @param $audience Integer: one of:
990 * Revision::FOR_PUBLIC to be displayed to all users
991 * Revision::FOR_THIS_USER to be displayed to $wgUser
992 * Revision::RAW get the text regardless of permissions
993 * @param $user User object to check for, only if FOR_THIS_USER is passed
994 * to the $audience parameter
995 * @since 1.21
996 * @return Content|null
998 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
999 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
1000 return null;
1001 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
1002 return null;
1003 } else {
1004 return $this->getContentInternal();
1009 * Alias for getText(Revision::FOR_THIS_USER)
1011 * @deprecated since 1.17
1012 * @return String
1014 public function revText() {
1015 wfDeprecated( __METHOD__, '1.17' );
1016 return $this->getText( self::FOR_THIS_USER );
1020 * Fetch revision text without regard for view restrictions
1022 * @return String
1024 * @deprecated since 1.21. Instead, use Revision::getContent( Revision::RAW )
1025 * or Revision::getSerializedData() as appropriate.
1027 public function getRawText() {
1028 ContentHandler::deprecated( __METHOD__, "1.21" );
1029 return $this->getText( self::RAW );
1033 * Fetch original serialized data without regard for view restrictions
1035 * @since 1.21
1036 * @return String
1038 public function getSerializedData() {
1039 if ( is_null( $this->mText ) ) {
1040 $this->mText = $this->loadText();
1043 return $this->mText;
1047 * Gets the content object for the revision (or null on failure).
1049 * Note that for mutable Content objects, each call to this method will return a
1050 * fresh clone.
1052 * @since 1.21
1053 * @return Content|null the Revision's content, or null on failure.
1055 protected function getContentInternal() {
1056 if ( is_null( $this->mContent ) ) {
1057 // Revision is immutable. Load on demand:
1058 if ( is_null( $this->mText ) ) {
1059 $this->mText = $this->loadText();
1062 if ( $this->mText !== null && $this->mText !== false ) {
1063 // Unserialize content
1064 $handler = $this->getContentHandler();
1065 $format = $this->getContentFormat();
1067 $this->mContent = $handler->unserializeContent( $this->mText, $format );
1068 } else {
1069 $this->mContent = false; // negative caching!
1073 // NOTE: copy() will return $this for immutable content objects
1074 return $this->mContent ? $this->mContent->copy() : null;
1078 * Returns the content model for this revision.
1080 * If no content model was stored in the database, $this->getTitle()->getContentModel() is
1081 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1082 * is used as a last resort.
1084 * @return String the content model id associated with this revision, see the CONTENT_MODEL_XXX constants.
1086 public function getContentModel() {
1087 if ( !$this->mContentModel ) {
1088 $title = $this->getTitle();
1089 $this->mContentModel = ( $title ? $title->getContentModel() : CONTENT_MODEL_WIKITEXT );
1091 assert( !empty( $this->mContentModel ) );
1094 return $this->mContentModel;
1098 * Returns the content format for this revision.
1100 * If no content format was stored in the database, the default format for this
1101 * revision's content model is returned.
1103 * @return String the content format id associated with this revision, see the CONTENT_FORMAT_XXX constants.
1105 public function getContentFormat() {
1106 if ( !$this->mContentFormat ) {
1107 $handler = $this->getContentHandler();
1108 $this->mContentFormat = $handler->getDefaultFormat();
1110 assert( !empty( $this->mContentFormat ) );
1113 return $this->mContentFormat;
1117 * Returns the content handler appropriate for this revision's content model.
1119 * @throws MWException
1120 * @return ContentHandler
1122 public function getContentHandler() {
1123 if ( !$this->mContentHandler ) {
1124 $model = $this->getContentModel();
1125 $this->mContentHandler = ContentHandler::getForModelID( $model );
1127 $format = $this->getContentFormat();
1129 if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
1130 throw new MWException( "Oops, the content format $format is not supported for this content model, $model" );
1134 return $this->mContentHandler;
1138 * @return String
1140 public function getTimestamp() {
1141 return wfTimestamp( TS_MW, $this->mTimestamp );
1145 * @return Boolean
1147 public function isCurrent() {
1148 return $this->mCurrent;
1152 * Get previous revision for this title
1154 * @return Revision|null
1156 public function getPrevious() {
1157 if ( $this->getTitle() ) {
1158 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1159 if ( $prev ) {
1160 return self::newFromTitle( $this->getTitle(), $prev );
1163 return null;
1167 * Get next revision for this title
1169 * @return Revision or null
1171 public function getNext() {
1172 if ( $this->getTitle() ) {
1173 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1174 if ( $next ) {
1175 return self::newFromTitle( $this->getTitle(), $next );
1178 return null;
1182 * Get previous revision Id for this page_id
1183 * This is used to populate rev_parent_id on save
1185 * @param $db DatabaseBase
1186 * @return Integer
1188 private function getPreviousRevisionId( $db ) {
1189 if ( is_null( $this->mPage ) ) {
1190 return 0;
1192 # Use page_latest if ID is not given
1193 if ( !$this->mId ) {
1194 $prevId = $db->selectField( 'page', 'page_latest',
1195 array( 'page_id' => $this->mPage ),
1196 __METHOD__ );
1197 } else {
1198 $prevId = $db->selectField( 'revision', 'rev_id',
1199 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
1200 __METHOD__,
1201 array( 'ORDER BY' => 'rev_id DESC' ) );
1203 return intval( $prevId );
1207 * Get revision text associated with an old or archive row
1208 * $row is usually an object from wfFetchRow(), both the flags and the text
1209 * field must be included
1211 * @param $row Object: the text data
1212 * @param string $prefix table prefix (default 'old_')
1213 * @param string|false $wiki the name of the wiki to load the revision text from
1214 * (same as the the wiki $row was loaded from) or false to indicate the local
1215 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1216 * identifier as understood by the LoadBalancer class.
1217 * @return String: text the text requested or false on failure
1219 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1220 wfProfileIn( __METHOD__ );
1222 # Get data
1223 $textField = $prefix . 'text';
1224 $flagsField = $prefix . 'flags';
1226 if ( isset( $row->$flagsField ) ) {
1227 $flags = explode( ',', $row->$flagsField );
1228 } else {
1229 $flags = array();
1232 if ( isset( $row->$textField ) ) {
1233 $text = $row->$textField;
1234 } else {
1235 wfProfileOut( __METHOD__ );
1236 return false;
1239 # Use external methods for external objects, text in table is URL-only then
1240 if ( in_array( 'external', $flags ) ) {
1241 $url = $text;
1242 $parts = explode( '://', $url, 2 );
1243 if ( count( $parts ) == 1 || $parts[1] == '' ) {
1244 wfProfileOut( __METHOD__ );
1245 return false;
1247 $text = ExternalStore::fetchFromURL( $url, array( 'wiki' => $wiki ) );
1250 // If the text was fetched without an error, convert it
1251 if ( $text !== false ) {
1252 $text = self::decompressRevisionText( $text, $flags );
1254 wfProfileOut( __METHOD__ );
1255 return $text;
1259 * If $wgCompressRevisions is enabled, we will compress data.
1260 * The input string is modified in place.
1261 * Return value is the flags field: contains 'gzip' if the
1262 * data is compressed, and 'utf-8' if we're saving in UTF-8
1263 * mode.
1265 * @param $text Mixed: reference to a text
1266 * @return String
1268 public static function compressRevisionText( &$text ) {
1269 global $wgCompressRevisions;
1270 $flags = array();
1272 # Revisions not marked this way will be converted
1273 # on load if $wgLegacyCharset is set in the future.
1274 $flags[] = 'utf-8';
1276 if ( $wgCompressRevisions ) {
1277 if ( function_exists( 'gzdeflate' ) ) {
1278 $text = gzdeflate( $text );
1279 $flags[] = 'gzip';
1280 } else {
1281 wfDebug( __METHOD__ . " -- no zlib support, not compressing\n" );
1284 return implode( ',', $flags );
1288 * Re-converts revision text according to it's flags.
1290 * @param $text Mixed: reference to a text
1291 * @param $flags array: compression flags
1292 * @return String|bool decompressed text, or false on failure
1294 public static function decompressRevisionText( $text, $flags ) {
1295 if ( in_array( 'gzip', $flags ) ) {
1296 # Deal with optional compression of archived pages.
1297 # This can be done periodically via maintenance/compressOld.php, and
1298 # as pages are saved if $wgCompressRevisions is set.
1299 $text = gzinflate( $text );
1302 if ( in_array( 'object', $flags ) ) {
1303 # Generic compressed storage
1304 $obj = unserialize( $text );
1305 if ( !is_object( $obj ) ) {
1306 // Invalid object
1307 return false;
1309 $text = $obj->getText();
1312 global $wgLegacyEncoding;
1313 if ( $text !== false && $wgLegacyEncoding
1314 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1316 # Old revisions kept around in a legacy encoding?
1317 # Upconvert on demand.
1318 # ("utf8" checked for compatibility with some broken
1319 # conversion scripts 2008-12-30)
1320 global $wgContLang;
1321 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1324 return $text;
1328 * Insert a new revision into the database, returning the new revision ID
1329 * number on success and dies horribly on failure.
1331 * @param $dbw DatabaseBase: (master connection)
1332 * @throws MWException
1333 * @return Integer
1335 public function insertOn( $dbw ) {
1336 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1338 wfProfileIn( __METHOD__ );
1340 $this->checkContentModel();
1342 $data = $this->mText;
1343 $flags = self::compressRevisionText( $data );
1345 # Write to external storage if required
1346 if ( $wgDefaultExternalStore ) {
1347 // Store and get the URL
1348 $data = ExternalStore::insertToDefault( $data );
1349 if ( !$data ) {
1350 wfProfileOut( __METHOD__ );
1351 throw new MWException( "Unable to store text to external storage" );
1353 if ( $flags ) {
1354 $flags .= ',';
1356 $flags .= 'external';
1359 # Record the text (or external storage URL) to the text table
1360 if ( !isset( $this->mTextId ) ) {
1361 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1362 $dbw->insert( 'text',
1363 array(
1364 'old_id' => $old_id,
1365 'old_text' => $data,
1366 'old_flags' => $flags,
1367 ), __METHOD__
1369 $this->mTextId = $dbw->insertId();
1372 if ( $this->mComment === null ) {
1373 $this->mComment = "";
1376 # Record the edit in revisions
1377 $rev_id = isset( $this->mId )
1378 ? $this->mId
1379 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1380 $row = array(
1381 'rev_id' => $rev_id,
1382 'rev_page' => $this->mPage,
1383 'rev_text_id' => $this->mTextId,
1384 'rev_comment' => $this->mComment,
1385 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1386 'rev_user' => $this->mUser,
1387 'rev_user_text' => $this->mUserText,
1388 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1389 'rev_deleted' => $this->mDeleted,
1390 'rev_len' => $this->mSize,
1391 'rev_parent_id' => is_null( $this->mParentId )
1392 ? $this->getPreviousRevisionId( $dbw )
1393 : $this->mParentId,
1394 'rev_sha1' => is_null( $this->mSha1 )
1395 ? Revision::base36Sha1( $this->mText )
1396 : $this->mSha1,
1399 if ( $wgContentHandlerUseDB ) {
1400 //NOTE: Store null for the default model and format, to save space.
1401 //XXX: Makes the DB sensitive to changed defaults. Make this behavior optional? Only in miser mode?
1403 $model = $this->getContentModel();
1404 $format = $this->getContentFormat();
1406 $title = $this->getTitle();
1408 if ( $title === null ) {
1409 wfProfileOut( __METHOD__ );
1410 throw new MWException( "Insufficient information to determine the title of the revision's page!" );
1413 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1414 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
1416 $row['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
1417 $row['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
1420 $dbw->insert( 'revision', $row, __METHOD__ );
1422 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
1424 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1426 wfProfileOut( __METHOD__ );
1427 return $this->mId;
1430 protected function checkContentModel() {
1431 global $wgContentHandlerUseDB;
1433 $title = $this->getTitle(); //note: may return null for revisions that have not yet been inserted.
1435 $model = $this->getContentModel();
1436 $format = $this->getContentFormat();
1437 $handler = $this->getContentHandler();
1439 if ( !$handler->isSupportedFormat( $format ) ) {
1440 $t = $title->getPrefixedDBkey();
1442 throw new MWException( "Can't use format $format with content model $model on $t" );
1445 if ( !$wgContentHandlerUseDB && $title ) {
1446 // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
1448 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1449 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1450 $defaultFormat = $defaultHandler->getDefaultFormat();
1452 if ( $this->getContentModel() != $defaultModel ) {
1453 $t = $title->getPrefixedDBkey();
1455 throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: "
1456 . "model is $model , default for $t is $defaultModel" );
1459 if ( $this->getContentFormat() != $defaultFormat ) {
1460 $t = $title->getPrefixedDBkey();
1462 throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: "
1463 . "format is $format, default for $t is $defaultFormat" );
1467 $content = $this->getContent( Revision::RAW );
1469 if ( !$content || !$content->isValid() ) {
1470 $t = $title->getPrefixedDBkey();
1472 throw new MWException( "Content of $t is not valid! Content model is $model" );
1477 * Get the base 36 SHA-1 value for a string of text
1478 * @param $text String
1479 * @return String
1481 public static function base36Sha1( $text ) {
1482 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1486 * Lazy-load the revision's text.
1487 * Currently hardcoded to the 'text' table storage engine.
1489 * @return String|bool the revision's text, or false on failure
1491 protected function loadText() {
1492 wfProfileIn( __METHOD__ );
1494 // Caching may be beneficial for massive use of external storage
1495 global $wgRevisionCacheExpiry, $wgMemc;
1496 $textId = $this->getTextId();
1497 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1498 if ( $wgRevisionCacheExpiry ) {
1499 $text = $wgMemc->get( $key );
1500 if ( is_string( $text ) ) {
1501 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1502 wfProfileOut( __METHOD__ );
1503 return $text;
1507 // If we kept data for lazy extraction, use it now...
1508 if ( isset( $this->mTextRow ) ) {
1509 $row = $this->mTextRow;
1510 $this->mTextRow = null;
1511 } else {
1512 $row = null;
1515 if ( !$row ) {
1516 // Text data is immutable; check slaves first.
1517 $dbr = wfGetDB( DB_SLAVE );
1518 $row = $dbr->selectRow( 'text',
1519 array( 'old_text', 'old_flags' ),
1520 array( 'old_id' => $textId ),
1521 __METHOD__ );
1524 // Fallback to the master in case of slave lag. Also use FOR UPDATE if it was
1525 // used to fetch this revision to avoid missing the row due to REPEATABLE-READ.
1526 $forUpdate = ( $this->mQueryFlags & self::READ_LOCKING == self::READ_LOCKING );
1527 if ( !$row && ( $forUpdate || wfGetLB()->getServerCount() > 1 ) ) {
1528 $dbw = wfGetDB( DB_MASTER );
1529 $row = $dbw->selectRow( 'text',
1530 array( 'old_text', 'old_flags' ),
1531 array( 'old_id' => $textId ),
1532 __METHOD__,
1533 $forUpdate ? array( 'FOR UPDATE' ) : array() );
1536 if ( !$row ) {
1537 wfDebugLog( 'Revision', "No text row with ID '$textId' (revision {$this->getId()})." );
1540 $text = self::getRevisionText( $row );
1541 if ( $row && $text === false ) {
1542 wfDebugLog( 'Revision', "No blob for text row '$textId' (revision {$this->getId()})." );
1545 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1546 if ( $wgRevisionCacheExpiry && $text !== false ) {
1547 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1550 wfProfileOut( __METHOD__ );
1552 return $text;
1556 * Create a new null-revision for insertion into a page's
1557 * history. This will not re-save the text, but simply refer
1558 * to the text from the previous version.
1560 * Such revisions can for instance identify page rename
1561 * operations and other such meta-modifications.
1563 * @param $dbw DatabaseBase
1564 * @param $pageId Integer: ID number of the page to read from
1565 * @param string $summary revision's summary
1566 * @param $minor Boolean: whether the revision should be considered as minor
1567 * @return Revision|null on error
1569 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1570 global $wgContentHandlerUseDB;
1572 wfProfileIn( __METHOD__ );
1574 $fields = array( 'page_latest', 'page_namespace', 'page_title',
1575 'rev_text_id', 'rev_len', 'rev_sha1' );
1577 if ( $wgContentHandlerUseDB ) {
1578 $fields[] = 'rev_content_model';
1579 $fields[] = 'rev_content_format';
1582 $current = $dbw->selectRow(
1583 array( 'page', 'revision' ),
1584 $fields,
1585 array(
1586 'page_id' => $pageId,
1587 'page_latest=rev_id',
1589 __METHOD__ );
1591 if ( $current ) {
1592 $row = array(
1593 'page' => $pageId,
1594 'comment' => $summary,
1595 'minor_edit' => $minor,
1596 'text_id' => $current->rev_text_id,
1597 'parent_id' => $current->page_latest,
1598 'len' => $current->rev_len,
1599 'sha1' => $current->rev_sha1
1602 if ( $wgContentHandlerUseDB ) {
1603 $row['content_model'] = $current->rev_content_model;
1604 $row['content_format'] = $current->rev_content_format;
1607 $revision = new Revision( $row );
1608 $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
1609 } else {
1610 $revision = null;
1613 wfProfileOut( __METHOD__ );
1614 return $revision;
1618 * Determine if the current user is allowed to view a particular
1619 * field of this revision, if it's marked as deleted.
1621 * @param $field Integer:one of self::DELETED_TEXT,
1622 * self::DELETED_COMMENT,
1623 * self::DELETED_USER
1624 * @param $user User object to check, or null to use $wgUser
1625 * @return Boolean
1627 public function userCan( $field, User $user = null ) {
1628 return self::userCanBitfield( $this->mDeleted, $field, $user );
1632 * Determine if the current user is allowed to view a particular
1633 * field of this revision, if it's marked as deleted. This is used
1634 * by various classes to avoid duplication.
1636 * @param $bitfield Integer: current field
1637 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1638 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1639 * self::DELETED_USER = File::DELETED_USER
1640 * @param $user User object to check, or null to use $wgUser
1641 * @return Boolean
1643 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1644 if ( $bitfield & $field ) { // aspect is deleted
1645 if ( $bitfield & self::DELETED_RESTRICTED ) {
1646 $permission = 'suppressrevision';
1647 } elseif ( $field & self::DELETED_TEXT ) {
1648 $permission = 'deletedtext';
1649 } else {
1650 $permission = 'deletedhistory';
1652 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1653 if ( $user === null ) {
1654 global $wgUser;
1655 $user = $wgUser;
1657 return $user->isAllowed( $permission );
1658 } else {
1659 return true;
1664 * Get rev_timestamp from rev_id, without loading the rest of the row
1666 * @param $title Title
1667 * @param $id Integer
1668 * @return String
1670 static function getTimestampFromId( $title, $id ) {
1671 $dbr = wfGetDB( DB_SLAVE );
1672 // Casting fix for databases that can't take '' for rev_id
1673 if ( $id == '' ) {
1674 $id = 0;
1676 $conds = array( 'rev_id' => $id );
1677 $conds['rev_page'] = $title->getArticleID();
1678 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1679 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1680 # Not in slave, try master
1681 $dbw = wfGetDB( DB_MASTER );
1682 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1684 return wfTimestamp( TS_MW, $timestamp );
1688 * Get count of revisions per page...not very efficient
1690 * @param $db DatabaseBase
1691 * @param $id Integer: page id
1692 * @return Integer
1694 static function countByPageId( $db, $id ) {
1695 $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
1696 array( 'rev_page' => $id ), __METHOD__ );
1697 if ( $row ) {
1698 return $row->revCount;
1700 return 0;
1704 * Get count of revisions per page...not very efficient
1706 * @param $db DatabaseBase
1707 * @param $title Title
1708 * @return Integer
1710 static function countByTitle( $db, $title ) {
1711 $id = $title->getArticleID();
1712 if ( $id ) {
1713 return self::countByPageId( $db, $id );
1715 return 0;
1719 * Check if no edits were made by other users since
1720 * the time a user started editing the page. Limit to
1721 * 50 revisions for the sake of performance.
1723 * @since 1.20
1725 * @param DatabaseBase|int $db the Database to perform the check on. May be given as a Database object or
1726 * a database identifier usable with wfGetDB.
1727 * @param int $pageId the ID of the page in question
1728 * @param int $userId the ID of the user in question
1729 * @param string $since look at edits since this time
1731 * @return bool True if the given user was the only one to edit since the given timestamp
1733 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1734 if ( !$userId ) {
1735 return false;
1738 if ( is_int( $db ) ) {
1739 $db = wfGetDB( $db );
1742 $res = $db->select( 'revision',
1743 'rev_user',
1744 array(
1745 'rev_page' => $pageId,
1746 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1748 __METHOD__,
1749 array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
1750 foreach ( $res as $row ) {
1751 if ( $row->rev_user != $userId ) {
1752 return false;
1755 return true;