10 protected $mOrigUserText;
12 protected $mMinorEdit;
13 protected $mTimestamp;
24 const DELETED_TEXT
= 1;
25 const DELETED_COMMENT
= 2;
26 const DELETED_USER
= 4;
27 const DELETED_RESTRICTED
= 8;
29 const SUPPRESSED_USER
= 12;
30 // Audience options for Revision::getText()
32 const FOR_THIS_USER
= 2;
36 * Load a page revision from a given revision ID number.
37 * Returns null if no such revision can be found.
40 * @return Revision or null
42 public static function newFromId( $id ) {
43 return Revision
::newFromConds( array( 'rev_id' => intval( $id ) ) );
47 * Load either the current, or a specified, revision
48 * that's attached to a given title. If not attached
49 * to that title, will return null.
52 * @param $id Integer (optional)
53 * @return Revision or null
55 public static function newFromTitle( $title, $id = 0 ) {
57 'page_namespace' => $title->getNamespace(),
58 'page_title' => $title->getDBkey()
61 // Use the specified ID
62 $conds['rev_id'] = $id;
63 } elseif ( wfGetLB()->getServerCount() > 1 ) {
64 // Get the latest revision ID from the master
65 $dbw = wfGetDB( DB_MASTER
);
66 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
67 if ( $latest === false ) {
68 return null; // page does not exist
70 $conds['rev_id'] = $latest;
72 // Use a join to get the latest revision
73 $conds[] = 'rev_id=page_latest';
75 return Revision
::newFromConds( $conds );
79 * Load either the current, or a specified, revision
80 * that's attached to a given page ID.
81 * Returns null if no such revision can be found.
83 * @param $revId Integer
84 * @param $pageId Integer (optional)
85 * @return Revision or null
87 public static function newFromPageId( $pageId, $revId = 0 ) {
88 $conds = array( 'page_id' => $pageId );
90 $conds['rev_id'] = $revId;
91 } elseif ( wfGetLB()->getServerCount() > 1 ) {
92 // Get the latest revision ID from the master
93 $dbw = wfGetDB( DB_MASTER
);
94 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
95 if ( $latest === false ) {
96 return null; // page does not exist
98 $conds['rev_id'] = $latest;
100 $conds[] = 'rev_id = page_latest';
102 return Revision
::newFromConds( $conds );
106 * Make a fake revision object from an archive table row. This is queried
107 * for permissions or even inserted (as in Special:Undelete)
108 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
111 * @param $overrides array
115 public static function newFromArchiveRow( $row, $overrides = array() ) {
116 $attribs = $overrides +
array(
117 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
118 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
119 'comment' => $row->ar_comment
,
120 'user' => $row->ar_user
,
121 'user_text' => $row->ar_user_text
,
122 'timestamp' => $row->ar_timestamp
,
123 'minor_edit' => $row->ar_minor_edit
,
124 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
125 'deleted' => $row->ar_deleted
,
126 'len' => $row->ar_len
,
127 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
129 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
130 // Pre-1.5 ar_text row
131 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
132 if ( $attribs['text'] === false ) {
133 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
136 return new self( $attribs );
145 public static function newFromRow( $row ) {
146 return new self( $row );
150 * Load a page revision from a given revision ID number.
151 * Returns null if no such revision can be found.
153 * @param $db DatabaseBase
155 * @return Revision or null
157 public static function loadFromId( $db, $id ) {
158 return Revision
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
162 * Load either the current, or a specified, revision
163 * that's attached to a given page. If not attached
164 * to that page, will return null.
166 * @param $db DatabaseBase
167 * @param $pageid Integer
169 * @return Revision or null
171 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
172 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
174 $conds['rev_id'] = intval( $id );
176 $conds[] = 'rev_id=page_latest';
178 return Revision
::loadFromConds( $db, $conds );
182 * Load either the current, or a specified, revision
183 * that's attached to a given page. If not attached
184 * to that page, will return null.
186 * @param $db DatabaseBase
187 * @param $title Title
189 * @return Revision or null
191 public static function loadFromTitle( $db, $title, $id = 0 ) {
193 $matchId = intval( $id );
195 $matchId = 'page_latest';
197 return Revision
::loadFromConds( $db,
198 array( "rev_id=$matchId",
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDBkey() )
205 * Load the revision for the given title with the given timestamp.
206 * WARNING: Timestamps may in some circumstances not be unique,
207 * so this isn't the best key to use.
209 * @param $db DatabaseBase
210 * @param $title Title
211 * @param $timestamp String
212 * @return Revision or null
214 public static function loadFromTimestamp( $db, $title, $timestamp ) {
215 return Revision
::loadFromConds( $db,
216 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
217 'page_namespace' => $title->getNamespace(),
218 'page_title' => $title->getDBkey() )
223 * Given a set of conditions, fetch a revision.
225 * @param $conditions Array
226 * @return Revision or null
228 public static function newFromConds( $conditions ) {
229 $db = wfGetDB( DB_SLAVE
);
230 $rev = Revision
::loadFromConds( $db, $conditions );
231 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
232 $dbw = wfGetDB( DB_MASTER
);
233 $rev = Revision
::loadFromConds( $dbw, $conditions );
239 * Given a set of conditions, fetch a revision from
240 * the given database connection.
242 * @param $db DatabaseBase
243 * @param $conditions Array
244 * @return Revision or null
246 private static function loadFromConds( $db, $conditions ) {
247 $res = Revision
::fetchFromConds( $db, $conditions );
249 $row = $res->fetchObject();
251 $ret = new Revision( $row );
260 * Return a wrapper for a series of database rows to
261 * fetch all of a given page's revisions in turn.
262 * Each row can be fed to the constructor to get objects.
264 * @param $title Title
265 * @return ResultWrapper
267 public static function fetchRevision( $title ) {
268 return Revision
::fetchFromConds(
270 array( 'rev_id=page_latest',
271 'page_namespace' => $title->getNamespace(),
272 'page_title' => $title->getDBkey() )
277 * Given a set of conditions, return a ResultWrapper
278 * which will return matching database rows with the
279 * fields necessary to build Revision objects.
281 * @param $db DatabaseBase
282 * @param $conditions Array
283 * @return ResultWrapper
285 private static function fetchFromConds( $db, $conditions ) {
286 $fields = array_merge(
287 self
::selectFields(),
288 self
::selectPageFields(),
289 self
::selectUserFields()
292 array( 'revision', 'page', 'user' ),
296 array( 'LIMIT' => 1 ),
297 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
302 * Return the value of a select() JOIN conds array for the user table.
303 * This will get user table rows for logged-in users.
307 public static function userJoinCond() {
308 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
312 * Return the value of a select() page conds array for the paeg table.
313 * This will assure that the revision(s) are not orphaned from live pages.
317 public static function pageJoinCond() {
318 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
322 * Return the list of revision fields that should be selected to create
325 public static function selectFields() {
343 * Return the list of text fields that should be selected to read the
346 public static function selectTextFields() {
354 * Return the list of page fields that should be selected from page table
356 public static function selectPageFields() {
365 * Return the list of user fields that should be selected from user table
367 public static function selectUserFields() {
368 return array( 'user_name' );
374 * @param $row Mixed: either a database row or an array
377 function __construct( $row ) {
378 if( is_object( $row ) ) {
379 $this->mId
= intval( $row->rev_id
);
380 $this->mPage
= intval( $row->rev_page
);
381 $this->mTextId
= intval( $row->rev_text_id
);
382 $this->mComment
= $row->rev_comment
;
383 $this->mUser
= intval( $row->rev_user
);
384 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
385 $this->mTimestamp
= $row->rev_timestamp
;
386 $this->mDeleted
= intval( $row->rev_deleted
);
388 if( !isset( $row->rev_parent_id
) ) {
389 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
391 $this->mParentId
= intval( $row->rev_parent_id
);
394 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
397 $this->mSize
= intval( $row->rev_len
);
400 if ( !isset( $row->rev_sha1
) ) {
403 $this->mSha1
= $row->rev_sha1
;
406 if( isset( $row->page_latest
) ) {
407 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
408 $this->mTitle
= Title
::newFromRow( $row );
410 $this->mCurrent
= false;
411 $this->mTitle
= null;
414 // Lazy extraction...
416 if( isset( $row->old_text
) ) {
417 $this->mTextRow
= $row;
419 // 'text' table row entry will be lazy-loaded
420 $this->mTextRow
= null;
423 // Use user_name for users and rev_user_text for IPs...
424 $this->mUserText
= null; // lazy load if left null
425 if ( $this->mUser
== 0 ) {
426 $this->mUserText
= $row->rev_user_text
; // IP user
427 } elseif ( isset( $row->user_name
) ) {
428 $this->mUserText
= $row->user_name
; // logged-in user
430 $this->mOrigUserText
= $row->rev_user_text
;
431 } elseif( is_array( $row ) ) {
432 // Build a new revision to be saved...
433 global $wgUser; // ugh
435 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
436 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
437 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
438 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
439 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
440 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
441 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
442 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
443 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
444 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
445 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
447 // Enforce spacing trimming on supplied text
448 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
449 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
450 $this->mTextRow
= null;
452 $this->mTitle
= null; # Load on demand if needed
453 $this->mCurrent
= false;
454 # If we still have no length, see it we have the text to figure it out
455 if ( !$this->mSize
) {
456 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
459 if ( $this->mSha1
=== null ) {
460 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
463 throw new MWException( 'Revision constructor passed invalid row format.' );
465 $this->mUnpatrolled
= null;
473 public function getId() {
478 * Set the revision ID
483 public function setId( $id ) {
492 public function getTextId() {
493 return $this->mTextId
;
497 * Get parent revision ID (the original previous page revision)
501 public function getParentId() {
502 return $this->mParentId
;
506 * Returns the length of the text in this revision, or null if unknown.
510 public function getSize() {
515 * Returns the base36 sha1 of the text in this revision, or null if unknown.
519 public function getSha1() {
524 * Returns the title of the page associated with this entry.
528 public function getTitle() {
529 if( isset( $this->mTitle
) ) {
530 return $this->mTitle
;
532 $dbr = wfGetDB( DB_SLAVE
);
533 $row = $dbr->selectRow(
534 array( 'page', 'revision' ),
535 array( 'page_namespace', 'page_title' ),
536 array( 'page_id=rev_page',
537 'rev_id' => $this->mId
),
538 'Revision::getTitle' );
540 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
542 return $this->mTitle
;
546 * Set the title of the revision
548 * @param $title Title
550 public function setTitle( $title ) {
551 $this->mTitle
= $title;
559 public function getPage() {
564 * Fetch revision's user id if it's available to the specified audience.
565 * If the specified audience does not have access to it, zero will be
568 * @param $audience Integer: one of:
569 * Revision::FOR_PUBLIC to be displayed to all users
570 * Revision::FOR_THIS_USER to be displayed to $wgUser
571 * Revision::RAW get the ID regardless of permissions
572 * @param $user User object to check for, only if FOR_THIS_USER is passed
573 * to the $audience parameter
576 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
577 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
579 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
587 * Fetch revision's user id without regard for the current user's permissions
591 public function getRawUser() {
596 * Fetch revision's username if it's available to the specified audience.
597 * If the specified audience does not have access to the username, an
598 * empty string will be returned.
600 * @param $audience Integer: one of:
601 * Revision::FOR_PUBLIC to be displayed to all users
602 * Revision::FOR_THIS_USER to be displayed to $wgUser
603 * Revision::RAW get the text regardless of permissions
604 * @param $user User object to check for, only if FOR_THIS_USER is passed
605 * to the $audience parameter
608 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
609 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
611 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
614 return $this->getRawUserText();
619 * Fetch revision's username without regard for view restrictions
623 public function getRawUserText() {
624 if ( $this->mUserText
=== null ) {
625 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
626 if ( $this->mUserText
=== false ) {
627 # This shouldn't happen, but it can if the wiki was recovered
628 # via importing revs and there is no user table entry yet.
629 $this->mUserText
= $this->mOrigUserText
;
632 return $this->mUserText
;
636 * Fetch revision comment if it's available to the specified audience.
637 * If the specified audience does not have access to the comment, an
638 * empty string will be returned.
640 * @param $audience Integer: one of:
641 * Revision::FOR_PUBLIC to be displayed to all users
642 * Revision::FOR_THIS_USER to be displayed to $wgUser
643 * Revision::RAW get the text regardless of permissions
644 * @param $user User object to check for, only if FOR_THIS_USER is passed
645 * to the $audience parameter
648 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
649 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
651 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
654 return $this->mComment
;
659 * Fetch revision comment without regard for the current user's permissions
663 public function getRawComment() {
664 return $this->mComment
;
670 public function isMinor() {
671 return (bool)$this->mMinorEdit
;
675 * @return Integer rcid of the unpatrolled row, zero if there isn't one
677 public function isUnpatrolled() {
678 if( $this->mUnpatrolled
!== null ) {
679 return $this->mUnpatrolled
;
681 $dbr = wfGetDB( DB_SLAVE
);
682 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
684 array( // Add redundant user,timestamp condition so we can use the existing index
685 'rc_user_text' => $this->getRawUserText(),
686 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
687 'rc_this_oldid' => $this->getId(),
692 return (int)$this->mUnpatrolled
;
696 * @param $field int one of DELETED_* bitfield constants
700 public function isDeleted( $field ) {
701 return ( $this->mDeleted
& $field ) == $field;
705 * Get the deletion bitfield of the revision
709 public function getVisibility() {
710 return (int)$this->mDeleted
;
714 * Fetch revision text if it's available to the specified audience.
715 * If the specified audience does not have the ability to view this
716 * revision, an empty string will be returned.
718 * @param $audience Integer: one of:
719 * Revision::FOR_PUBLIC to be displayed to all users
720 * Revision::FOR_THIS_USER to be displayed to $wgUser
721 * Revision::RAW get the text regardless of permissions
722 * @param $user User object to check for, only if FOR_THIS_USER is passed
723 * to the $audience parameter
726 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
727 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
729 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
732 return $this->getRawText();
737 * Alias for getText(Revision::FOR_THIS_USER)
739 * @deprecated since 1.17
742 public function revText() {
743 wfDeprecated( __METHOD__
, '1.17' );
744 return $this->getText( self
::FOR_THIS_USER
);
748 * Fetch revision text without regard for view restrictions
752 public function getRawText() {
753 if( is_null( $this->mText
) ) {
754 // Revision text is immutable. Load on demand:
755 $this->mText
= $this->loadText();
763 public function getTimestamp() {
764 return wfTimestamp( TS_MW
, $this->mTimestamp
);
770 public function isCurrent() {
771 return $this->mCurrent
;
775 * Get previous revision for this title
777 * @return Revision or null
779 public function getPrevious() {
780 if( $this->getTitle() ) {
781 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
783 return Revision
::newFromTitle( $this->getTitle(), $prev );
790 * Get next revision for this title
792 * @return Revision or null
794 public function getNext() {
795 if( $this->getTitle() ) {
796 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
798 return Revision
::newFromTitle( $this->getTitle(), $next );
805 * Get previous revision Id for this page_id
806 * This is used to populate rev_parent_id on save
808 * @param $db DatabaseBase
811 private function getPreviousRevisionId( $db ) {
812 if( is_null( $this->mPage
) ) {
815 # Use page_latest if ID is not given
817 $prevId = $db->selectField( 'page', 'page_latest',
818 array( 'page_id' => $this->mPage
),
821 $prevId = $db->selectField( 'revision', 'rev_id',
822 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
824 array( 'ORDER BY' => 'rev_id DESC' ) );
826 return intval( $prevId );
830 * Get revision text associated with an old or archive row
831 * $row is usually an object from wfFetchRow(), both the flags and the text
832 * field must be included
834 * @param $row Object: the text data
835 * @param $prefix String: table prefix (default 'old_')
836 * @return String: text the text requested or false on failure
838 public static function getRevisionText( $row, $prefix = 'old_' ) {
839 wfProfileIn( __METHOD__
);
842 $textField = $prefix . 'text';
843 $flagsField = $prefix . 'flags';
845 if( isset( $row->$flagsField ) ) {
846 $flags = explode( ',', $row->$flagsField );
851 if( isset( $row->$textField ) ) {
852 $text = $row->$textField;
854 wfProfileOut( __METHOD__
);
858 # Use external methods for external objects, text in table is URL-only then
859 if ( in_array( 'external', $flags ) ) {
861 $parts = explode( '://', $url, 2 );
862 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
863 wfProfileOut( __METHOD__
);
866 $text = ExternalStore
::fetchFromURL( $url );
869 // If the text was fetched without an error, convert it
870 if ( $text !== false ) {
871 if( in_array( 'gzip', $flags ) ) {
872 # Deal with optional compression of archived pages.
873 # This can be done periodically via maintenance/compressOld.php, and
874 # as pages are saved if $wgCompressRevisions is set.
875 $text = gzinflate( $text );
878 if( in_array( 'object', $flags ) ) {
879 # Generic compressed storage
880 $obj = unserialize( $text );
881 if ( !is_object( $obj ) ) {
883 wfProfileOut( __METHOD__
);
886 $text = $obj->getText();
889 global $wgLegacyEncoding;
890 if( $text !== false && $wgLegacyEncoding
891 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
893 # Old revisions kept around in a legacy encoding?
894 # Upconvert on demand.
895 # ("utf8" checked for compatibility with some broken
896 # conversion scripts 2008-12-30)
898 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
901 wfProfileOut( __METHOD__
);
906 * If $wgCompressRevisions is enabled, we will compress data.
907 * The input string is modified in place.
908 * Return value is the flags field: contains 'gzip' if the
909 * data is compressed, and 'utf-8' if we're saving in UTF-8
912 * @param $text Mixed: reference to a text
915 public static function compressRevisionText( &$text ) {
916 global $wgCompressRevisions;
919 # Revisions not marked this way will be converted
920 # on load if $wgLegacyCharset is set in the future.
923 if( $wgCompressRevisions ) {
924 if( function_exists( 'gzdeflate' ) ) {
925 $text = gzdeflate( $text );
928 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
931 return implode( ',', $flags );
935 * Insert a new revision into the database, returning the new revision ID
936 * number on success and dies horribly on failure.
938 * @param $dbw DatabaseBase: (master connection)
941 public function insertOn( $dbw ) {
942 global $wgDefaultExternalStore;
944 wfProfileIn( __METHOD__
);
946 $data = $this->mText
;
947 $flags = Revision
::compressRevisionText( $data );
949 # Write to external storage if required
950 if( $wgDefaultExternalStore ) {
951 // Store and get the URL
952 $data = ExternalStore
::insertToDefault( $data );
954 throw new MWException( "Unable to store text to external storage" );
959 $flags .= 'external';
962 # Record the text (or external storage URL) to the text table
963 if( !isset( $this->mTextId
) ) {
964 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
965 $dbw->insert( 'text',
969 'old_flags' => $flags,
972 $this->mTextId
= $dbw->insertId();
975 if ( $this->mComment
=== null ) $this->mComment
= "";
977 # Record the edit in revisions
978 $rev_id = isset( $this->mId
)
980 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
981 $dbw->insert( 'revision',
984 'rev_page' => $this->mPage
,
985 'rev_text_id' => $this->mTextId
,
986 'rev_comment' => $this->mComment
,
987 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
988 'rev_user' => $this->mUser
,
989 'rev_user_text' => $this->mUserText
,
990 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
991 'rev_deleted' => $this->mDeleted
,
992 'rev_len' => $this->mSize
,
993 'rev_parent_id' => is_null( $this->mParentId
)
994 ?
$this->getPreviousRevisionId( $dbw )
996 'rev_sha1' => is_null( $this->mSha1
)
997 ? Revision
::base36Sha1( $this->mText
)
1002 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1004 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1006 wfProfileOut( __METHOD__
);
1011 * Get the base 36 SHA-1 value for a string of text
1012 * @param $text String
1015 public static function base36Sha1( $text ) {
1016 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1020 * Lazy-load the revision's text.
1021 * Currently hardcoded to the 'text' table storage engine.
1025 protected function loadText() {
1026 wfProfileIn( __METHOD__
);
1028 // Caching may be beneficial for massive use of external storage
1029 global $wgRevisionCacheExpiry, $wgMemc;
1030 $textId = $this->getTextId();
1031 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1032 if( $wgRevisionCacheExpiry ) {
1033 $text = $wgMemc->get( $key );
1034 if( is_string( $text ) ) {
1035 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1036 wfProfileOut( __METHOD__
);
1041 // If we kept data for lazy extraction, use it now...
1042 if ( isset( $this->mTextRow
) ) {
1043 $row = $this->mTextRow
;
1044 $this->mTextRow
= null;
1050 // Text data is immutable; check slaves first.
1051 $dbr = wfGetDB( DB_SLAVE
);
1052 $row = $dbr->selectRow( 'text',
1053 array( 'old_text', 'old_flags' ),
1054 array( 'old_id' => $this->getTextId() ),
1058 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1059 // Possible slave lag!
1060 $dbw = wfGetDB( DB_MASTER
);
1061 $row = $dbw->selectRow( 'text',
1062 array( 'old_text', 'old_flags' ),
1063 array( 'old_id' => $this->getTextId() ),
1067 $text = self
::getRevisionText( $row );
1069 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1070 if( $wgRevisionCacheExpiry && $text !== false ) {
1071 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1074 wfProfileOut( __METHOD__
);
1080 * Create a new null-revision for insertion into a page's
1081 * history. This will not re-save the text, but simply refer
1082 * to the text from the previous version.
1084 * Such revisions can for instance identify page rename
1085 * operations and other such meta-modifications.
1087 * @param $dbw DatabaseBase
1088 * @param $pageId Integer: ID number of the page to read from
1089 * @param $summary String: revision's summary
1090 * @param $minor Boolean: whether the revision should be considered as minor
1091 * @return Revision|null on error
1093 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1094 wfProfileIn( __METHOD__
);
1096 $current = $dbw->selectRow(
1097 array( 'page', 'revision' ),
1098 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' ),
1100 'page_id' => $pageId,
1101 'page_latest=rev_id',
1106 $revision = new Revision( array(
1108 'comment' => $summary,
1109 'minor_edit' => $minor,
1110 'text_id' => $current->rev_text_id
,
1111 'parent_id' => $current->page_latest
,
1112 'len' => $current->rev_len
,
1113 'sha1' => $current->rev_sha1
1119 wfProfileOut( __METHOD__
);
1124 * Determine if the current user is allowed to view a particular
1125 * field of this revision, if it's marked as deleted.
1127 * @param $field Integer:one of self::DELETED_TEXT,
1128 * self::DELETED_COMMENT,
1129 * self::DELETED_USER
1130 * @param $user User object to check, or null to use $wgUser
1133 public function userCan( $field, User
$user = null ) {
1134 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1138 * Determine if the current user is allowed to view a particular
1139 * field of this revision, if it's marked as deleted. This is used
1140 * by various classes to avoid duplication.
1142 * @param $bitfield Integer: current field
1143 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1144 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1145 * self::DELETED_USER = File::DELETED_USER
1146 * @param $user User object to check, or null to use $wgUser
1149 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1150 if( $bitfield & $field ) { // aspect is deleted
1151 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1152 $permission = 'suppressrevision';
1153 } elseif ( $field & self
::DELETED_TEXT
) {
1154 $permission = 'deletedtext';
1156 $permission = 'deletedhistory';
1158 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1159 if ( $user === null ) {
1163 return $user->isAllowed( $permission );
1170 * Get rev_timestamp from rev_id, without loading the rest of the row
1172 * @param $title Title
1173 * @param $id Integer
1176 static function getTimestampFromId( $title, $id ) {
1177 $dbr = wfGetDB( DB_SLAVE
);
1178 // Casting fix for DB2
1182 $conds = array( 'rev_id' => $id );
1183 $conds['rev_page'] = $title->getArticleId();
1184 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1185 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1186 # Not in slave, try master
1187 $dbw = wfGetDB( DB_MASTER
);
1188 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1190 return wfTimestamp( TS_MW
, $timestamp );
1194 * Get count of revisions per page...not very efficient
1196 * @param $db DatabaseBase
1197 * @param $id Integer: page id
1200 static function countByPageId( $db, $id ) {
1201 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1202 array( 'rev_page' => $id ), __METHOD__
);
1204 return $row->revCount
;
1210 * Get count of revisions per page...not very efficient
1212 * @param $db DatabaseBase
1213 * @param $title Title
1216 static function countByTitle( $db, $title ) {
1217 $id = $title->getArticleId();
1219 return Revision
::countByPageId( $db, $id );