Introduced Maintenance::getDB() and corresponding setDB() to control externally what...
[mediawiki.git] / includes / Revision.php
blobe72c7c1dd909fa522d9a3e4b13b24543ee280f12
1 <?php
3 /**
4 * @todo document
5 */
6 class Revision {
7 const DELETED_TEXT = 1;
8 const DELETED_COMMENT = 2;
9 const DELETED_USER = 4;
10 const DELETED_RESTRICTED = 8;
11 // Convenience field
12 const SUPPRESSED_USER = 12;
13 // Audience options for Revision::getText()
14 const FOR_PUBLIC = 1;
15 const FOR_THIS_USER = 2;
16 const RAW = 3;
18 /**
19 * Load a page revision from a given revision ID number.
20 * Returns null if no such revision can be found.
22 * @param $id Integer
23 * @return Revision or null
25 public static function newFromId( $id ) {
26 return Revision::newFromConds(
27 array( 'page_id=rev_page',
28 'rev_id' => intval( $id ) ) );
31 /**
32 * Load either the current, or a specified, revision
33 * that's attached to a given title. If not attached
34 * to that title, will return null.
36 * @param $title Title
37 * @param $id Integer
38 * @return Revision or null
40 public static function newFromTitle( $title, $id = 0 ) {
41 $conds = array(
42 'page_namespace' => $title->getNamespace(),
43 'page_title' => $title->getDBkey()
45 if ( $id ) {
46 // Use the specified ID
47 $conds['rev_id'] = $id;
48 } elseif ( wfGetLB()->getServerCount() > 1 ) {
49 // Get the latest revision ID from the master
50 $dbw = wfGetDB( DB_MASTER );
51 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
52 if ( $latest === false ) {
53 // Page does not exist
54 return null;
56 $conds['rev_id'] = $latest;
57 } else {
58 // Use a join to get the latest revision
59 $conds[] = 'rev_id=page_latest';
61 $conds[] = 'page_id=rev_page';
62 return Revision::newFromConds( $conds );
65 /**
66 * Make a fake revision object from an archive table row. This is queried
67 * for permissions or even inserted (as in Special:Undelete)
68 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
70 public static function newFromArchiveRow( $row, $overrides = array() ) {
71 $attribs = $overrides + array(
72 'page' => isset( $row->page_id ) ? $row->page_id : null,
73 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
74 'comment' => $row->ar_comment,
75 'user' => $row->ar_user,
76 'user_text' => $row->ar_user_text,
77 'timestamp' => $row->ar_timestamp,
78 'minor_edit' => $row->ar_minor_edit,
79 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
80 'deleted' => $row->ar_deleted,
81 'len' => $row->ar_len);
82 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
83 // Pre-1.5 ar_text row
84 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
85 if ( $attribs['text'] === false ) {
86 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
89 return new self( $attribs );
92 /**
93 * Load a page revision from a given revision ID number.
94 * Returns null if no such revision can be found.
96 * @param $db DatabaseBase
97 * @param $id Integer
98 * @return Revision or null
100 public static function loadFromId( $db, $id ) {
101 return Revision::loadFromConds( $db,
102 array( 'page_id=rev_page',
103 'rev_id' => intval( $id ) ) );
107 * Load either the current, or a specified, revision
108 * that's attached to a given page. If not attached
109 * to that page, will return null.
111 * @param $db DatabaseBase
112 * @param $pageid Integer
113 * @param $id Integer
114 * @return Revision or null
116 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
117 $conds = array( 'page_id=rev_page','rev_page' => intval( $pageid ), 'page_id'=>intval( $pageid ) );
118 if( $id ) {
119 $conds['rev_id'] = intval( $id );
120 } else {
121 $conds[] = 'rev_id=page_latest';
123 return Revision::loadFromConds( $db, $conds );
127 * Load either the current, or a specified, revision
128 * that's attached to a given page. If not attached
129 * to that page, will return null.
131 * @param $db DatabaseBase
132 * @param $title Title
133 * @param $id Integer
134 * @return Revision or null
136 public static function loadFromTitle( $db, $title, $id = 0 ) {
137 if( $id ) {
138 $matchId = intval( $id );
139 } else {
140 $matchId = 'page_latest';
142 return Revision::loadFromConds(
143 $db,
144 array( "rev_id=$matchId",
145 'page_id=rev_page',
146 'page_namespace' => $title->getNamespace(),
147 'page_title' => $title->getDBkey() ) );
151 * Load the revision for the given title with the given timestamp.
152 * WARNING: Timestamps may in some circumstances not be unique,
153 * so this isn't the best key to use.
155 * @param $db DatabaseBase
156 * @param $title Title
157 * @param $timestamp String
158 * @return Revision or null
160 public static function loadFromTimestamp( $db, $title, $timestamp ) {
161 return Revision::loadFromConds(
162 $db,
163 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
164 'page_id=rev_page',
165 'page_namespace' => $title->getNamespace(),
166 'page_title' => $title->getDBkey() ) );
170 * Given a set of conditions, fetch a revision.
172 * @param $conditions Array
173 * @return Revision or null
175 public static function newFromConds( $conditions ) {
176 $db = wfGetDB( DB_SLAVE );
177 $row = Revision::loadFromConds( $db, $conditions );
178 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
179 $dbw = wfGetDB( DB_MASTER );
180 $row = Revision::loadFromConds( $dbw, $conditions );
182 return $row;
186 * Given a set of conditions, fetch a revision from
187 * the given database connection.
189 * @param $db DatabaseBase
190 * @param $conditions Array
191 * @return Revision or null
193 private static function loadFromConds( $db, $conditions ) {
194 $res = Revision::fetchFromConds( $db, $conditions );
195 if( $res ) {
196 $row = $res->fetchObject();
197 $res->free();
198 if( $row ) {
199 $ret = new Revision( $row );
200 return $ret;
203 $ret = null;
204 return $ret;
208 * Return a wrapper for a series of database rows to
209 * fetch all of a given page's revisions in turn.
210 * Each row can be fed to the constructor to get objects.
212 * @param $title Title
213 * @return ResultWrapper
215 public static function fetchRevision( $title ) {
216 return Revision::fetchFromConds(
217 wfGetDB( DB_SLAVE ),
218 array( 'rev_id=page_latest',
219 'page_namespace' => $title->getNamespace(),
220 'page_title' => $title->getDBkey(),
221 'page_id=rev_page' ) );
225 * Given a set of conditions, return a ResultWrapper
226 * which will return matching database rows with the
227 * fields necessary to build Revision objects.
229 * @param $db DatabaseBase
230 * @param $conditions Array
231 * @return ResultWrapper
233 private static function fetchFromConds( $db, $conditions ) {
234 $fields = self::selectFields();
235 $fields[] = 'page_namespace';
236 $fields[] = 'page_title';
237 $fields[] = 'page_latest';
238 $res = $db->select(
239 array( 'page', 'revision' ),
240 $fields,
241 $conditions,
242 __METHOD__,
243 array( 'LIMIT' => 1 ) );
244 $ret = $db->resultObject( $res );
245 return $ret;
249 * Return the list of revision fields that should be selected to create
250 * a new revision.
252 public static function selectFields() {
253 return array(
254 'rev_id',
255 'rev_page',
256 'rev_text_id',
257 'rev_timestamp',
258 'rev_comment',
259 'rev_user_text,'.
260 'rev_user',
261 'rev_minor_edit',
262 'rev_deleted',
263 'rev_len',
264 'rev_parent_id'
269 * Return the list of text fields that should be selected to read the
270 * revision text
272 static function selectTextFields() {
273 return array(
274 'old_text',
275 'old_flags'
280 * Return the list of page fields that should be selected from page table
282 static function selectPageFields() {
283 return array(
284 'page_namespace',
285 'page_title',
286 'page_latest'
291 * Constructor
293 * @param $row Mixed: either a database row or an array
294 * @access private
296 function __construct( $row ) {
297 if( is_object( $row ) ) {
298 $this->mId = intval( $row->rev_id );
299 $this->mPage = intval( $row->rev_page );
300 $this->mTextId = intval( $row->rev_text_id );
301 $this->mComment = $row->rev_comment;
302 $this->mUserText = $row->rev_user_text;
303 $this->mUser = intval( $row->rev_user );
304 $this->mMinorEdit = intval( $row->rev_minor_edit );
305 $this->mTimestamp = $row->rev_timestamp;
306 $this->mDeleted = intval( $row->rev_deleted );
308 if( !isset( $row->rev_parent_id ) )
309 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
310 else
311 $this->mParentId = intval( $row->rev_parent_id );
313 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) )
314 $this->mSize = null;
315 else
316 $this->mSize = intval( $row->rev_len );
318 if( isset( $row->page_latest ) ) {
319 $this->mCurrent = ( $row->rev_id == $row->page_latest );
320 $this->mTitle = Title::newFromRow( $row );
321 } else {
322 $this->mCurrent = false;
323 $this->mTitle = null;
326 // Lazy extraction...
327 $this->mText = null;
328 if( isset( $row->old_text ) ) {
329 $this->mTextRow = $row;
330 } else {
331 // 'text' table row entry will be lazy-loaded
332 $this->mTextRow = null;
334 } elseif( is_array( $row ) ) {
335 // Build a new revision to be saved...
336 global $wgUser;
338 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
339 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
340 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
341 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
342 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
343 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
344 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
345 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
346 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
347 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
349 // Enforce spacing trimming on supplied text
350 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
351 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
352 $this->mTextRow = null;
354 $this->mTitle = null; # Load on demand if needed
355 $this->mCurrent = false;
356 # If we still have no len_size, see it we have the text to figure it out
357 if ( !$this->mSize )
358 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
359 } else {
360 throw new MWException( 'Revision constructor passed invalid row format.' );
362 $this->mUnpatrolled = null;
366 * Get revision ID
368 * @return Integer
370 public function getId() {
371 return $this->mId;
375 * Get text row ID
377 * @return Integer
379 public function getTextId() {
380 return $this->mTextId;
384 * Get parent revision ID (the original previous page revision)
386 * @return Integer
388 public function getParentId() {
389 return $this->mParentId;
393 * Returns the length of the text in this revision, or null if unknown.
395 * @return Integer
397 public function getSize() {
398 return $this->mSize;
402 * Returns the title of the page associated with this entry.
404 * @return Title
406 public function getTitle() {
407 if( isset( $this->mTitle ) ) {
408 return $this->mTitle;
410 $dbr = wfGetDB( DB_SLAVE );
411 $row = $dbr->selectRow(
412 array( 'page', 'revision' ),
413 array( 'page_namespace', 'page_title' ),
414 array( 'page_id=rev_page',
415 'rev_id' => $this->mId ),
416 'Revision::getTitle' );
417 if( $row ) {
418 $this->mTitle = Title::makeTitle( $row->page_namespace,
419 $row->page_title );
421 return $this->mTitle;
425 * Set the title of the revision
427 * @param $title Title
429 public function setTitle( $title ) {
430 $this->mTitle = $title;
434 * Get the page ID
436 * @return Integer
438 public function getPage() {
439 return $this->mPage;
443 * Fetch revision's user id if it's available to the specified audience.
444 * If the specified audience does not have access to it, zero will be
445 * returned.
447 * @param $audience Integer: one of:
448 * Revision::FOR_PUBLIC to be displayed to all users
449 * Revision::FOR_THIS_USER to be displayed to $wgUser
450 * Revision::RAW get the ID regardless of permissions
453 * @return Integer
455 public function getUser( $audience = self::FOR_PUBLIC ) {
456 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
457 return 0;
458 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
459 return 0;
460 } else {
461 return $this->mUser;
466 * Fetch revision's user id without regard for the current user's permissions
468 * @return String
470 public function getRawUser() {
471 return $this->mUser;
475 * Fetch revision's username if it's available to the specified audience.
476 * If the specified audience does not have access to the username, an
477 * empty string will be returned.
479 * @param $audience Integer: one of:
480 * Revision::FOR_PUBLIC to be displayed to all users
481 * Revision::FOR_THIS_USER to be displayed to $wgUser
482 * Revision::RAW get the text regardless of permissions
484 * @return string
486 public function getUserText( $audience = self::FOR_PUBLIC ) {
487 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
488 return '';
489 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
490 return '';
491 } else {
492 return $this->mUserText;
497 * Fetch revision's username without regard for view restrictions
499 * @return String
501 public function getRawUserText() {
502 return $this->mUserText;
506 * Fetch revision comment if it's available to the specified audience.
507 * If the specified audience does not have access to the comment, an
508 * empty string will be returned.
510 * @param $audience Integer: one of:
511 * Revision::FOR_PUBLIC to be displayed to all users
512 * Revision::FOR_THIS_USER to be displayed to $wgUser
513 * Revision::RAW get the text regardless of permissions
515 * @return String
517 function getComment( $audience = self::FOR_PUBLIC ) {
518 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
519 return '';
520 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT ) ) {
521 return '';
522 } else {
523 return $this->mComment;
528 * Fetch revision comment without regard for the current user's permissions
530 * @return String
532 public function getRawComment() {
533 return $this->mComment;
537 * @return Boolean
539 public function isMinor() {
540 return (bool)$this->mMinorEdit;
544 * @return Integer rcid of the unpatrolled row, zero if there isn't one
546 public function isUnpatrolled() {
547 if( $this->mUnpatrolled !== null ) {
548 return $this->mUnpatrolled;
550 $dbr = wfGetDB( DB_SLAVE );
551 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
552 'rc_id',
553 array( // Add redundant user,timestamp condition so we can use the existing index
554 'rc_user_text' => $this->getRawUserText(),
555 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
556 'rc_this_oldid' => $this->getId(),
557 'rc_patrolled' => 0
559 __METHOD__
561 return (int)$this->mUnpatrolled;
565 * int $field one of DELETED_* bitfield constants
567 * @return Boolean
569 public function isDeleted( $field ) {
570 return ( $this->mDeleted & $field ) == $field;
574 * Get the deletion bitfield of the revision
576 public function getVisibility() {
577 return (int)$this->mDeleted;
581 * Fetch revision text if it's available to the specified audience.
582 * If the specified audience does not have the ability to view this
583 * revision, an empty string will be returned.
585 * @param $audience Integer: one of:
586 * Revision::FOR_PUBLIC to be displayed to all users
587 * Revision::FOR_THIS_USER to be displayed to $wgUser
588 * Revision::RAW get the text regardless of permissions
590 * @return String
592 public function getText( $audience = self::FOR_PUBLIC ) {
593 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
594 return '';
595 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT ) ) {
596 return '';
597 } else {
598 return $this->getRawText();
603 * Alias for getText(Revision::FOR_THIS_USER)
605 * @deprecated since 1.17
606 * @return String
608 public function revText() {
609 wfDeprecated( __METHOD__ );
610 return $this->getText( self::FOR_THIS_USER );
614 * Fetch revision text without regard for view restrictions
616 * @return String
618 public function getRawText() {
619 if( is_null( $this->mText ) ) {
620 // Revision text is immutable. Load on demand:
621 $this->mText = $this->loadText();
623 return $this->mText;
627 * @return String
629 public function getTimestamp() {
630 return wfTimestamp( TS_MW, $this->mTimestamp );
634 * @return Boolean
636 public function isCurrent() {
637 return $this->mCurrent;
641 * Get previous revision for this title
643 * @return Revision or null
645 public function getPrevious() {
646 if( $this->getTitle() ) {
647 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
648 if( $prev ) {
649 return Revision::newFromTitle( $this->getTitle(), $prev );
652 return null;
656 * Get next revision for this title
658 * @return Revision or null
660 public function getNext() {
661 if( $this->getTitle() ) {
662 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
663 if ( $next ) {
664 return Revision::newFromTitle( $this->getTitle(), $next );
667 return null;
671 * Get previous revision Id for this page_id
672 * This is used to populate rev_parent_id on save
674 * @param $db DatabaseBase
675 * @return Integer
677 private function getPreviousRevisionId( $db ) {
678 if( is_null( $this->mPage ) ) {
679 return 0;
681 # Use page_latest if ID is not given
682 if( !$this->mId ) {
683 $prevId = $db->selectField( 'page', 'page_latest',
684 array( 'page_id' => $this->mPage ),
685 __METHOD__ );
686 } else {
687 $prevId = $db->selectField( 'revision', 'rev_id',
688 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
689 __METHOD__,
690 array( 'ORDER BY' => 'rev_id DESC' ) );
692 return intval( $prevId );
696 * Get revision text associated with an old or archive row
697 * $row is usually an object from wfFetchRow(), both the flags and the text
698 * field must be included
700 * @param $row Object: the text data
701 * @param $prefix String: table prefix (default 'old_')
702 * @return String: text the text requested or false on failure
704 public static function getRevisionText( $row, $prefix = 'old_' ) {
705 wfProfileIn( __METHOD__ );
707 # Get data
708 $textField = $prefix . 'text';
709 $flagsField = $prefix . 'flags';
711 if( isset( $row->$flagsField ) ) {
712 $flags = explode( ',', $row->$flagsField );
713 } else {
714 $flags = array();
717 if( isset( $row->$textField ) ) {
718 $text = $row->$textField;
719 } else {
720 wfProfileOut( __METHOD__ );
721 return false;
724 # Use external methods for external objects, text in table is URL-only then
725 if ( in_array( 'external', $flags ) ) {
726 $url = $text;
727 @list(/* $proto */, $path ) = explode( '://', $url, 2 );
728 if( $path == '' ) {
729 wfProfileOut( __METHOD__ );
730 return false;
732 $text = ExternalStore::fetchFromURL( $url );
735 // If the text was fetched without an error, convert it
736 if ( $text !== false ) {
737 if( in_array( 'gzip', $flags ) ) {
738 # Deal with optional compression of archived pages.
739 # This can be done periodically via maintenance/compressOld.php, and
740 # as pages are saved if $wgCompressRevisions is set.
741 $text = gzinflate( $text );
744 if( in_array( 'object', $flags ) ) {
745 # Generic compressed storage
746 $obj = unserialize( $text );
747 if ( !is_object( $obj ) ) {
748 // Invalid object
749 wfProfileOut( __METHOD__ );
750 return false;
752 $text = $obj->getText();
755 global $wgLegacyEncoding;
756 if( $text !== false && $wgLegacyEncoding
757 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
759 # Old revisions kept around in a legacy encoding?
760 # Upconvert on demand.
761 # ("utf8" checked for compatibility with some broken
762 # conversion scripts 2008-12-30)
763 global $wgContLang;
764 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
767 wfProfileOut( __METHOD__ );
768 return $text;
772 * If $wgCompressRevisions is enabled, we will compress data.
773 * The input string is modified in place.
774 * Return value is the flags field: contains 'gzip' if the
775 * data is compressed, and 'utf-8' if we're saving in UTF-8
776 * mode.
778 * @param $text Mixed: reference to a text
779 * @return String
781 public static function compressRevisionText( &$text ) {
782 global $wgCompressRevisions;
783 $flags = array();
785 # Revisions not marked this way will be converted
786 # on load if $wgLegacyCharset is set in the future.
787 $flags[] = 'utf-8';
789 if( $wgCompressRevisions ) {
790 if( function_exists( 'gzdeflate' ) ) {
791 $text = gzdeflate( $text );
792 $flags[] = 'gzip';
793 } else {
794 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
797 return implode( ',', $flags );
801 * Insert a new revision into the database, returning the new revision ID
802 * number on success and dies horribly on failure.
804 * @param $dbw DatabaseBase: (master connection)
805 * @return Integer
807 public function insertOn( $dbw ) {
808 global $wgDefaultExternalStore;
810 wfProfileIn( __METHOD__ );
812 $data = $this->mText;
813 $flags = Revision::compressRevisionText( $data );
815 # Write to external storage if required
816 if( $wgDefaultExternalStore ) {
817 // Store and get the URL
818 $data = ExternalStore::insertToDefault( $data );
819 if( !$data ) {
820 throw new MWException( "Unable to store text to external storage" );
822 if( $flags ) {
823 $flags .= ',';
825 $flags .= 'external';
828 # Record the text (or external storage URL) to the text table
829 if( !isset( $this->mTextId ) ) {
830 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
831 $dbw->insert( 'text',
832 array(
833 'old_id' => $old_id,
834 'old_text' => $data,
835 'old_flags' => $flags,
836 ), __METHOD__
838 $this->mTextId = $dbw->insertId();
841 if ( $this->mComment === null ) $this->mComment = "";
843 # Record the edit in revisions
844 $rev_id = isset( $this->mId )
845 ? $this->mId
846 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
847 $dbw->insert( 'revision',
848 array(
849 'rev_id' => $rev_id,
850 'rev_page' => $this->mPage,
851 'rev_text_id' => $this->mTextId,
852 'rev_comment' => $this->mComment,
853 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
854 'rev_user' => $this->mUser,
855 'rev_user_text' => $this->mUserText,
856 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
857 'rev_deleted' => $this->mDeleted,
858 'rev_len' => $this->mSize,
859 'rev_parent_id' => is_null($this->mParentId) ?
860 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
861 ), __METHOD__
864 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
866 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
868 wfProfileOut( __METHOD__ );
869 return $this->mId;
873 * Lazy-load the revision's text.
874 * Currently hardcoded to the 'text' table storage engine.
876 * @return String
878 protected function loadText() {
879 wfProfileIn( __METHOD__ );
881 // Caching may be beneficial for massive use of external storage
882 global $wgRevisionCacheExpiry, $wgMemc;
883 $textId = $this->getTextId();
884 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
885 if( $wgRevisionCacheExpiry ) {
886 $text = $wgMemc->get( $key );
887 if( is_string( $text ) ) {
888 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
889 wfProfileOut( __METHOD__ );
890 return $text;
894 // If we kept data for lazy extraction, use it now...
895 if ( isset( $this->mTextRow ) ) {
896 $row = $this->mTextRow;
897 $this->mTextRow = null;
898 } else {
899 $row = null;
902 if( !$row ) {
903 // Text data is immutable; check slaves first.
904 $dbr = wfGetDB( DB_SLAVE );
905 $row = $dbr->selectRow( 'text',
906 array( 'old_text', 'old_flags' ),
907 array( 'old_id' => $this->getTextId() ),
908 __METHOD__ );
911 if( !$row && wfGetLB()->getServerCount() > 1 ) {
912 // Possible slave lag!
913 $dbw = wfGetDB( DB_MASTER );
914 $row = $dbw->selectRow( 'text',
915 array( 'old_text', 'old_flags' ),
916 array( 'old_id' => $this->getTextId() ),
917 __METHOD__ );
920 $text = self::getRevisionText( $row );
922 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
923 if( $wgRevisionCacheExpiry && $text !== false ) {
924 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
927 wfProfileOut( __METHOD__ );
929 return $text;
933 * Create a new null-revision for insertion into a page's
934 * history. This will not re-save the text, but simply refer
935 * to the text from the previous version.
937 * Such revisions can for instance identify page rename
938 * operations and other such meta-modifications.
940 * @param $dbw DatabaseBase
941 * @param $pageId Integer: ID number of the page to read from
942 * @param $summary String: revision's summary
943 * @param $minor Boolean: whether the revision should be considered as minor
944 * @return Revision|null on error
946 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
947 wfProfileIn( __METHOD__ );
949 $current = $dbw->selectRow(
950 array( 'page', 'revision' ),
951 array( 'page_latest', 'rev_text_id', 'rev_len' ),
952 array(
953 'page_id' => $pageId,
954 'page_latest=rev_id',
956 __METHOD__ );
958 if( $current ) {
959 $revision = new Revision( array(
960 'page' => $pageId,
961 'comment' => $summary,
962 'minor_edit' => $minor,
963 'text_id' => $current->rev_text_id,
964 'parent_id' => $current->page_latest,
965 'len' => $current->rev_len
966 ) );
967 } else {
968 $revision = null;
971 wfProfileOut( __METHOD__ );
972 return $revision;
976 * Determine if the current user is allowed to view a particular
977 * field of this revision, if it's marked as deleted.
979 * @param $field Integer:one of self::DELETED_TEXT,
980 * self::DELETED_COMMENT,
981 * self::DELETED_USER
982 * @return Boolean
984 public function userCan( $field ) {
985 return self::userCanBitfield( $this->mDeleted, $field );
989 * Determine if the current user is allowed to view a particular
990 * field of this revision, if it's marked as deleted. This is used
991 * by various classes to avoid duplication.
993 * @param $bitfield Integer: current field
994 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
995 * self::DELETED_COMMENT = File::DELETED_COMMENT,
996 * self::DELETED_USER = File::DELETED_USER
997 * @return Boolean
999 public static function userCanBitfield( $bitfield, $field ) {
1000 if( $bitfield & $field ) { // aspect is deleted
1001 global $wgUser;
1002 if ( $bitfield & self::DELETED_RESTRICTED ) {
1003 $permission = 'suppressrevision';
1004 } elseif ( $field & self::DELETED_TEXT ) {
1005 $permission = 'deletedtext';
1006 } else {
1007 $permission = 'deletedhistory';
1009 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1010 return $wgUser->isAllowed( $permission );
1011 } else {
1012 return true;
1017 * Get rev_timestamp from rev_id, without loading the rest of the row
1019 * @param $title Title
1020 * @param $id Integer
1021 * @return String
1023 static function getTimestampFromId( $title, $id ) {
1024 $dbr = wfGetDB( DB_SLAVE );
1025 // Casting fix for DB2
1026 if ( $id == '' ) {
1027 $id = 0;
1029 $conds = array( 'rev_id' => $id );
1030 $conds['rev_page'] = $title->getArticleId();
1031 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1032 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1033 # Not in slave, try master
1034 $dbw = wfGetDB( DB_MASTER );
1035 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1037 return wfTimestamp( TS_MW, $timestamp );
1041 * Get count of revisions per page...not very efficient
1043 * @param $db DatabaseBase
1044 * @param $id Integer: page id
1045 * @return Integer
1047 static function countByPageId( $db, $id ) {
1048 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1049 array( 'rev_page' => $id ), __METHOD__ );
1050 if( $row ) {
1051 return $row->revCount;
1053 return 0;
1057 * Get count of revisions per page...not very efficient
1059 * @param $db DatabaseBase
1060 * @param $title Title
1061 * @return Integer
1063 static function countByTitle( $db, $title ) {
1064 $id = $title->getArticleId();
1065 if( $id ) {
1066 return Revision::countByPageId( $db, $id );
1068 return 0;
1073 * Aliases for backwards compatibility with 1.6
1075 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
1076 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
1077 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
1078 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );