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
326 public static function selectFields() {
344 * Return the list of text fields that should be selected to read the
348 public static function selectTextFields() {
356 * Return the list of page fields that should be selected from page table
359 public static function selectPageFields() {
369 * Return the list of user fields that should be selected from user table
372 public static function selectUserFields() {
373 return array( 'user_name' );
379 * @param $row Mixed: either a database row or an array
382 function __construct( $row ) {
383 if( is_object( $row ) ) {
384 $this->mId
= intval( $row->rev_id
);
385 $this->mPage
= intval( $row->rev_page
);
386 $this->mTextId
= intval( $row->rev_text_id
);
387 $this->mComment
= $row->rev_comment
;
388 $this->mUser
= intval( $row->rev_user
);
389 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
390 $this->mTimestamp
= $row->rev_timestamp
;
391 $this->mDeleted
= intval( $row->rev_deleted
);
393 if( !isset( $row->rev_parent_id
) ) {
394 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
396 $this->mParentId
= intval( $row->rev_parent_id
);
399 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
402 $this->mSize
= intval( $row->rev_len
);
405 if ( !isset( $row->rev_sha1
) ) {
408 $this->mSha1
= $row->rev_sha1
;
411 if( isset( $row->page_latest
) ) {
412 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
413 $this->mTitle
= Title
::newFromRow( $row );
415 $this->mCurrent
= false;
416 $this->mTitle
= null;
419 // Lazy extraction...
421 if( isset( $row->old_text
) ) {
422 $this->mTextRow
= $row;
424 // 'text' table row entry will be lazy-loaded
425 $this->mTextRow
= null;
428 // Use user_name for users and rev_user_text for IPs...
429 $this->mUserText
= null; // lazy load if left null
430 if ( $this->mUser
== 0 ) {
431 $this->mUserText
= $row->rev_user_text
; // IP user
432 } elseif ( isset( $row->user_name
) ) {
433 $this->mUserText
= $row->user_name
; // logged-in user
435 $this->mOrigUserText
= $row->rev_user_text
;
436 } elseif( is_array( $row ) ) {
437 // Build a new revision to be saved...
438 global $wgUser; // ugh
440 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
441 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
442 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
443 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
444 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
445 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
446 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
447 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
448 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
449 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
450 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
452 // Enforce spacing trimming on supplied text
453 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
454 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
455 $this->mTextRow
= null;
457 $this->mTitle
= null; # Load on demand if needed
458 $this->mCurrent
= false;
459 # If we still have no length, see it we have the text to figure it out
460 if ( !$this->mSize
) {
461 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
464 if ( $this->mSha1
=== null ) {
465 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
468 throw new MWException( 'Revision constructor passed invalid row format.' );
470 $this->mUnpatrolled
= null;
478 public function getId() {
483 * Set the revision ID
488 public function setId( $id ) {
497 public function getTextId() {
498 return $this->mTextId
;
502 * Get parent revision ID (the original previous page revision)
504 * @return Integer|null
506 public function getParentId() {
507 return $this->mParentId
;
511 * Returns the length of the text in this revision, or null if unknown.
515 public function getSize() {
520 * Returns the base36 sha1 of the text in this revision, or null if unknown.
524 public function getSha1() {
529 * Returns the title of the page associated with this entry.
533 public function getTitle() {
534 if( isset( $this->mTitle
) ) {
535 return $this->mTitle
;
537 $dbr = wfGetDB( DB_SLAVE
);
538 $row = $dbr->selectRow(
539 array( 'page', 'revision' ),
540 self
::selectPageFields(),
541 array( 'page_id=rev_page',
542 'rev_id' => $this->mId
),
545 $this->mTitle
= Title
::newFromRow( $row );
547 return $this->mTitle
;
551 * Set the title of the revision
553 * @param $title Title
555 public function setTitle( $title ) {
556 $this->mTitle
= $title;
564 public function getPage() {
569 * Fetch revision's user id if it's available to the specified audience.
570 * If the specified audience does not have access to it, zero will be
573 * @param $audience Integer: one of:
574 * Revision::FOR_PUBLIC to be displayed to all users
575 * Revision::FOR_THIS_USER to be displayed to $wgUser
576 * Revision::RAW get the ID regardless of permissions
577 * @param $user User object to check for, only if FOR_THIS_USER is passed
578 * to the $audience parameter
581 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
582 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
584 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
592 * Fetch revision's user id without regard for the current user's permissions
596 public function getRawUser() {
601 * Fetch revision's username if it's available to the specified audience.
602 * If the specified audience does not have access to the username, an
603 * empty string will be returned.
605 * @param $audience Integer: one of:
606 * Revision::FOR_PUBLIC to be displayed to all users
607 * Revision::FOR_THIS_USER to be displayed to $wgUser
608 * Revision::RAW get the text regardless of permissions
609 * @param $user User object to check for, only if FOR_THIS_USER is passed
610 * to the $audience parameter
613 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
614 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
616 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
619 return $this->getRawUserText();
624 * Fetch revision's username without regard for view restrictions
628 public function getRawUserText() {
629 if ( $this->mUserText
=== null ) {
630 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
631 if ( $this->mUserText
=== false ) {
632 # This shouldn't happen, but it can if the wiki was recovered
633 # via importing revs and there is no user table entry yet.
634 $this->mUserText
= $this->mOrigUserText
;
637 return $this->mUserText
;
641 * Fetch revision comment if it's available to the specified audience.
642 * If the specified audience does not have access to the comment, an
643 * empty string will be returned.
645 * @param $audience Integer: one of:
646 * Revision::FOR_PUBLIC to be displayed to all users
647 * Revision::FOR_THIS_USER to be displayed to $wgUser
648 * Revision::RAW get the text regardless of permissions
649 * @param $user User object to check for, only if FOR_THIS_USER is passed
650 * to the $audience parameter
653 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
654 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
656 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
659 return $this->mComment
;
664 * Fetch revision comment without regard for the current user's permissions
668 public function getRawComment() {
669 return $this->mComment
;
675 public function isMinor() {
676 return (bool)$this->mMinorEdit
;
680 * @return Integer rcid of the unpatrolled row, zero if there isn't one
682 public function isUnpatrolled() {
683 if( $this->mUnpatrolled
!== null ) {
684 return $this->mUnpatrolled
;
686 $dbr = wfGetDB( DB_SLAVE
);
687 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
689 array( // Add redundant user,timestamp condition so we can use the existing index
690 'rc_user_text' => $this->getRawUserText(),
691 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
692 'rc_this_oldid' => $this->getId(),
697 return (int)$this->mUnpatrolled
;
701 * @param $field int one of DELETED_* bitfield constants
705 public function isDeleted( $field ) {
706 return ( $this->mDeleted
& $field ) == $field;
710 * Get the deletion bitfield of the revision
714 public function getVisibility() {
715 return (int)$this->mDeleted
;
719 * Fetch revision text if it's available to the specified audience.
720 * If the specified audience does not have the ability to view this
721 * revision, an empty string will be returned.
723 * @param $audience Integer: one of:
724 * Revision::FOR_PUBLIC to be displayed to all users
725 * Revision::FOR_THIS_USER to be displayed to $wgUser
726 * Revision::RAW get the text regardless of permissions
727 * @param $user User object to check for, only if FOR_THIS_USER is passed
728 * to the $audience parameter
731 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
732 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
734 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
737 return $this->getRawText();
742 * Alias for getText(Revision::FOR_THIS_USER)
744 * @deprecated since 1.17
747 public function revText() {
748 wfDeprecated( __METHOD__
, '1.17' );
749 return $this->getText( self
::FOR_THIS_USER
);
753 * Fetch revision text without regard for view restrictions
757 public function getRawText() {
758 if( is_null( $this->mText
) ) {
759 // Revision text is immutable. Load on demand:
760 $this->mText
= $this->loadText();
768 public function getTimestamp() {
769 return wfTimestamp( TS_MW
, $this->mTimestamp
);
775 public function isCurrent() {
776 return $this->mCurrent
;
780 * Get previous revision for this title
782 * @return Revision or null
784 public function getPrevious() {
785 if( $this->getTitle() ) {
786 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
788 return Revision
::newFromTitle( $this->getTitle(), $prev );
795 * Get next revision for this title
797 * @return Revision or null
799 public function getNext() {
800 if( $this->getTitle() ) {
801 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
803 return Revision
::newFromTitle( $this->getTitle(), $next );
810 * Get previous revision Id for this page_id
811 * This is used to populate rev_parent_id on save
813 * @param $db DatabaseBase
816 private function getPreviousRevisionId( $db ) {
817 if( is_null( $this->mPage
) ) {
820 # Use page_latest if ID is not given
822 $prevId = $db->selectField( 'page', 'page_latest',
823 array( 'page_id' => $this->mPage
),
826 $prevId = $db->selectField( 'revision', 'rev_id',
827 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
829 array( 'ORDER BY' => 'rev_id DESC' ) );
831 return intval( $prevId );
835 * Get revision text associated with an old or archive row
836 * $row is usually an object from wfFetchRow(), both the flags and the text
837 * field must be included
839 * @param $row Object: the text data
840 * @param $prefix String: table prefix (default 'old_')
841 * @return String: text the text requested or false on failure
843 public static function getRevisionText( $row, $prefix = 'old_' ) {
844 wfProfileIn( __METHOD__
);
847 $textField = $prefix . 'text';
848 $flagsField = $prefix . 'flags';
850 if( isset( $row->$flagsField ) ) {
851 $flags = explode( ',', $row->$flagsField );
856 if( isset( $row->$textField ) ) {
857 $text = $row->$textField;
859 wfProfileOut( __METHOD__
);
863 # Use external methods for external objects, text in table is URL-only then
864 if ( in_array( 'external', $flags ) ) {
866 $parts = explode( '://', $url, 2 );
867 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
868 wfProfileOut( __METHOD__
);
871 $text = ExternalStore
::fetchFromURL( $url );
874 // If the text was fetched without an error, convert it
875 if ( $text !== false ) {
876 if( in_array( 'gzip', $flags ) ) {
877 # Deal with optional compression of archived pages.
878 # This can be done periodically via maintenance/compressOld.php, and
879 # as pages are saved if $wgCompressRevisions is set.
880 $text = gzinflate( $text );
883 if( in_array( 'object', $flags ) ) {
884 # Generic compressed storage
885 $obj = unserialize( $text );
886 if ( !is_object( $obj ) ) {
888 wfProfileOut( __METHOD__
);
891 $text = $obj->getText();
894 global $wgLegacyEncoding;
895 if( $text !== false && $wgLegacyEncoding
896 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
898 # Old revisions kept around in a legacy encoding?
899 # Upconvert on demand.
900 # ("utf8" checked for compatibility with some broken
901 # conversion scripts 2008-12-30)
903 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
906 wfProfileOut( __METHOD__
);
911 * If $wgCompressRevisions is enabled, we will compress data.
912 * The input string is modified in place.
913 * Return value is the flags field: contains 'gzip' if the
914 * data is compressed, and 'utf-8' if we're saving in UTF-8
917 * @param $text Mixed: reference to a text
920 public static function compressRevisionText( &$text ) {
921 global $wgCompressRevisions;
924 # Revisions not marked this way will be converted
925 # on load if $wgLegacyCharset is set in the future.
928 if( $wgCompressRevisions ) {
929 if( function_exists( 'gzdeflate' ) ) {
930 $text = gzdeflate( $text );
933 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
936 return implode( ',', $flags );
940 * Insert a new revision into the database, returning the new revision ID
941 * number on success and dies horribly on failure.
943 * @param $dbw DatabaseBase: (master connection)
946 public function insertOn( $dbw ) {
947 global $wgDefaultExternalStore;
949 wfProfileIn( __METHOD__
);
951 $data = $this->mText
;
952 $flags = Revision
::compressRevisionText( $data );
954 # Write to external storage if required
955 if( $wgDefaultExternalStore ) {
956 // Store and get the URL
957 $data = ExternalStore
::insertToDefault( $data );
959 throw new MWException( "Unable to store text to external storage" );
964 $flags .= 'external';
967 # Record the text (or external storage URL) to the text table
968 if( !isset( $this->mTextId
) ) {
969 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
970 $dbw->insert( 'text',
974 'old_flags' => $flags,
977 $this->mTextId
= $dbw->insertId();
980 if ( $this->mComment
=== null ) $this->mComment
= "";
982 # Record the edit in revisions
983 $rev_id = isset( $this->mId
)
985 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
986 $dbw->insert( 'revision',
989 'rev_page' => $this->mPage
,
990 'rev_text_id' => $this->mTextId
,
991 'rev_comment' => $this->mComment
,
992 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
993 'rev_user' => $this->mUser
,
994 'rev_user_text' => $this->mUserText
,
995 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
996 'rev_deleted' => $this->mDeleted
,
997 'rev_len' => $this->mSize
,
998 'rev_parent_id' => is_null( $this->mParentId
)
999 ?
$this->getPreviousRevisionId( $dbw )
1001 'rev_sha1' => is_null( $this->mSha1
)
1002 ? Revision
::base36Sha1( $this->mText
)
1007 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1009 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1011 wfProfileOut( __METHOD__
);
1016 * Get the base 36 SHA-1 value for a string of text
1017 * @param $text String
1020 public static function base36Sha1( $text ) {
1021 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1025 * Lazy-load the revision's text.
1026 * Currently hardcoded to the 'text' table storage engine.
1030 protected function loadText() {
1031 wfProfileIn( __METHOD__
);
1033 // Caching may be beneficial for massive use of external storage
1034 global $wgRevisionCacheExpiry, $wgMemc;
1035 $textId = $this->getTextId();
1036 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1037 if( $wgRevisionCacheExpiry ) {
1038 $text = $wgMemc->get( $key );
1039 if( is_string( $text ) ) {
1040 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1041 wfProfileOut( __METHOD__
);
1046 // If we kept data for lazy extraction, use it now...
1047 if ( isset( $this->mTextRow
) ) {
1048 $row = $this->mTextRow
;
1049 $this->mTextRow
= null;
1055 // Text data is immutable; check slaves first.
1056 $dbr = wfGetDB( DB_SLAVE
);
1057 $row = $dbr->selectRow( 'text',
1058 array( 'old_text', 'old_flags' ),
1059 array( 'old_id' => $this->getTextId() ),
1063 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1064 // Possible slave lag!
1065 $dbw = wfGetDB( DB_MASTER
);
1066 $row = $dbw->selectRow( 'text',
1067 array( 'old_text', 'old_flags' ),
1068 array( 'old_id' => $this->getTextId() ),
1072 $text = self
::getRevisionText( $row );
1074 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1075 if( $wgRevisionCacheExpiry && $text !== false ) {
1076 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1079 wfProfileOut( __METHOD__
);
1085 * Create a new null-revision for insertion into a page's
1086 * history. This will not re-save the text, but simply refer
1087 * to the text from the previous version.
1089 * Such revisions can for instance identify page rename
1090 * operations and other such meta-modifications.
1092 * @param $dbw DatabaseBase
1093 * @param $pageId Integer: ID number of the page to read from
1094 * @param $summary String: revision's summary
1095 * @param $minor Boolean: whether the revision should be considered as minor
1096 * @return Revision|null on error
1098 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1099 wfProfileIn( __METHOD__
);
1101 $current = $dbw->selectRow(
1102 array( 'page', 'revision' ),
1103 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' ),
1105 'page_id' => $pageId,
1106 'page_latest=rev_id',
1111 $revision = new Revision( array(
1113 'comment' => $summary,
1114 'minor_edit' => $minor,
1115 'text_id' => $current->rev_text_id
,
1116 'parent_id' => $current->page_latest
,
1117 'len' => $current->rev_len
,
1118 'sha1' => $current->rev_sha1
1124 wfProfileOut( __METHOD__
);
1129 * Determine if the current user is allowed to view a particular
1130 * field of this revision, if it's marked as deleted.
1132 * @param $field Integer:one of self::DELETED_TEXT,
1133 * self::DELETED_COMMENT,
1134 * self::DELETED_USER
1135 * @param $user User object to check, or null to use $wgUser
1138 public function userCan( $field, User
$user = null ) {
1139 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1143 * Determine if the current user is allowed to view a particular
1144 * field of this revision, if it's marked as deleted. This is used
1145 * by various classes to avoid duplication.
1147 * @param $bitfield Integer: current field
1148 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1149 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1150 * self::DELETED_USER = File::DELETED_USER
1151 * @param $user User object to check, or null to use $wgUser
1154 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1155 if( $bitfield & $field ) { // aspect is deleted
1156 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1157 $permission = 'suppressrevision';
1158 } elseif ( $field & self
::DELETED_TEXT
) {
1159 $permission = 'deletedtext';
1161 $permission = 'deletedhistory';
1163 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1164 if ( $user === null ) {
1168 return $user->isAllowed( $permission );
1175 * Get rev_timestamp from rev_id, without loading the rest of the row
1177 * @param $title Title
1178 * @param $id Integer
1181 static function getTimestampFromId( $title, $id ) {
1182 $dbr = wfGetDB( DB_SLAVE
);
1183 // Casting fix for DB2
1187 $conds = array( 'rev_id' => $id );
1188 $conds['rev_page'] = $title->getArticleID();
1189 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1190 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1191 # Not in slave, try master
1192 $dbw = wfGetDB( DB_MASTER
);
1193 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1195 return wfTimestamp( TS_MW
, $timestamp );
1199 * Get count of revisions per page...not very efficient
1201 * @param $db DatabaseBase
1202 * @param $id Integer: page id
1205 static function countByPageId( $db, $id ) {
1206 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1207 array( 'rev_page' => $id ), __METHOD__
);
1209 return $row->revCount
;
1215 * Get count of revisions per page...not very efficient
1217 * @param $db DatabaseBase
1218 * @param $title Title
1221 static function countByTitle( $db, $title ) {
1222 $id = $title->getArticleID();
1224 return Revision
::countByPageId( $db, $id );