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() {
366 * Return the list of user fields that should be selected from user table
368 public static function selectUserFields() {
369 return array( 'user_name' );
375 * @param $row Mixed: either a database row or an array
378 function __construct( $row ) {
379 if( is_object( $row ) ) {
380 $this->mId
= intval( $row->rev_id
);
381 $this->mPage
= intval( $row->rev_page
);
382 $this->mTextId
= intval( $row->rev_text_id
);
383 $this->mComment
= $row->rev_comment
;
384 $this->mUser
= intval( $row->rev_user
);
385 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
386 $this->mTimestamp
= $row->rev_timestamp
;
387 $this->mDeleted
= intval( $row->rev_deleted
);
389 if( !isset( $row->rev_parent_id
) ) {
390 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
392 $this->mParentId
= intval( $row->rev_parent_id
);
395 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
398 $this->mSize
= intval( $row->rev_len
);
401 if ( !isset( $row->rev_sha1
) ) {
404 $this->mSha1
= $row->rev_sha1
;
407 if( isset( $row->page_latest
) ) {
408 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
409 $this->mTitle
= Title
::newFromRow( $row );
411 $this->mCurrent
= false;
412 $this->mTitle
= null;
415 // Lazy extraction...
417 if( isset( $row->old_text
) ) {
418 $this->mTextRow
= $row;
420 // 'text' table row entry will be lazy-loaded
421 $this->mTextRow
= null;
424 // Use user_name for users and rev_user_text for IPs...
425 $this->mUserText
= null; // lazy load if left null
426 if ( $this->mUser
== 0 ) {
427 $this->mUserText
= $row->rev_user_text
; // IP user
428 } elseif ( isset( $row->user_name
) ) {
429 $this->mUserText
= $row->user_name
; // logged-in user
431 $this->mOrigUserText
= $row->rev_user_text
;
432 } elseif( is_array( $row ) ) {
433 // Build a new revision to be saved...
434 global $wgUser; // ugh
436 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
437 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
438 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
439 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
440 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
441 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
442 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
443 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
444 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
445 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
446 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
448 // Enforce spacing trimming on supplied text
449 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
450 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
451 $this->mTextRow
= null;
453 $this->mTitle
= null; # Load on demand if needed
454 $this->mCurrent
= false;
455 # If we still have no length, see it we have the text to figure it out
456 if ( !$this->mSize
) {
457 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
460 if ( $this->mSha1
=== null ) {
461 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
464 throw new MWException( 'Revision constructor passed invalid row format.' );
466 $this->mUnpatrolled
= null;
474 public function getId() {
479 * Set the revision ID
484 public function setId( $id ) {
493 public function getTextId() {
494 return $this->mTextId
;
498 * Get parent revision ID (the original previous page revision)
502 public function getParentId() {
503 return $this->mParentId
;
507 * Returns the length of the text in this revision, or null if unknown.
511 public function getSize() {
516 * Returns the base36 sha1 of the text in this revision, or null if unknown.
520 public function getSha1() {
525 * Returns the title of the page associated with this entry.
529 public function getTitle() {
530 if( isset( $this->mTitle
) ) {
531 return $this->mTitle
;
533 $dbr = wfGetDB( DB_SLAVE
);
534 $row = $dbr->selectRow(
535 array( 'page', 'revision' ),
536 self
::selectPageFields(),
537 array( 'page_id=rev_page',
538 'rev_id' => $this->mId
),
541 $this->mTitle
= Title
::newFromRow( $row );
543 return $this->mTitle
;
547 * Set the title of the revision
549 * @param $title Title
551 public function setTitle( $title ) {
552 $this->mTitle
= $title;
560 public function getPage() {
565 * Fetch revision's user id if it's available to the specified audience.
566 * If the specified audience does not have access to it, zero will be
569 * @param $audience Integer: one of:
570 * Revision::FOR_PUBLIC to be displayed to all users
571 * Revision::FOR_THIS_USER to be displayed to $wgUser
572 * Revision::RAW get the ID regardless of permissions
573 * @param $user User object to check for, only if FOR_THIS_USER is passed
574 * to the $audience parameter
577 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
578 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
580 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
588 * Fetch revision's user id without regard for the current user's permissions
592 public function getRawUser() {
597 * Fetch revision's username if it's available to the specified audience.
598 * If the specified audience does not have access to the username, an
599 * empty string will be returned.
601 * @param $audience Integer: one of:
602 * Revision::FOR_PUBLIC to be displayed to all users
603 * Revision::FOR_THIS_USER to be displayed to $wgUser
604 * Revision::RAW get the text regardless of permissions
605 * @param $user User object to check for, only if FOR_THIS_USER is passed
606 * to the $audience parameter
609 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
610 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
612 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
615 return $this->getRawUserText();
620 * Fetch revision's username without regard for view restrictions
624 public function getRawUserText() {
625 if ( $this->mUserText
=== null ) {
626 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
627 if ( $this->mUserText
=== false ) {
628 # This shouldn't happen, but it can if the wiki was recovered
629 # via importing revs and there is no user table entry yet.
630 $this->mUserText
= $this->mOrigUserText
;
633 return $this->mUserText
;
637 * Fetch revision comment if it's available to the specified audience.
638 * If the specified audience does not have access to the comment, an
639 * empty string will be returned.
641 * @param $audience Integer: one of:
642 * Revision::FOR_PUBLIC to be displayed to all users
643 * Revision::FOR_THIS_USER to be displayed to $wgUser
644 * Revision::RAW get the text regardless of permissions
645 * @param $user User object to check for, only if FOR_THIS_USER is passed
646 * to the $audience parameter
649 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
650 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
652 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
655 return $this->mComment
;
660 * Fetch revision comment without regard for the current user's permissions
664 public function getRawComment() {
665 return $this->mComment
;
671 public function isMinor() {
672 return (bool)$this->mMinorEdit
;
676 * @return Integer rcid of the unpatrolled row, zero if there isn't one
678 public function isUnpatrolled() {
679 if( $this->mUnpatrolled
!== null ) {
680 return $this->mUnpatrolled
;
682 $dbr = wfGetDB( DB_SLAVE
);
683 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
685 array( // Add redundant user,timestamp condition so we can use the existing index
686 'rc_user_text' => $this->getRawUserText(),
687 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
688 'rc_this_oldid' => $this->getId(),
693 return (int)$this->mUnpatrolled
;
697 * @param $field int one of DELETED_* bitfield constants
701 public function isDeleted( $field ) {
702 return ( $this->mDeleted
& $field ) == $field;
706 * Get the deletion bitfield of the revision
710 public function getVisibility() {
711 return (int)$this->mDeleted
;
715 * Fetch revision text if it's available to the specified audience.
716 * If the specified audience does not have the ability to view this
717 * revision, an empty string will be returned.
719 * @param $audience Integer: one of:
720 * Revision::FOR_PUBLIC to be displayed to all users
721 * Revision::FOR_THIS_USER to be displayed to $wgUser
722 * Revision::RAW get the text regardless of permissions
723 * @param $user User object to check for, only if FOR_THIS_USER is passed
724 * to the $audience parameter
727 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
728 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
730 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
733 return $this->getRawText();
738 * Alias for getText(Revision::FOR_THIS_USER)
740 * @deprecated since 1.17
743 public function revText() {
744 wfDeprecated( __METHOD__
, '1.17' );
745 return $this->getText( self
::FOR_THIS_USER
);
749 * Fetch revision text without regard for view restrictions
753 public function getRawText() {
754 if( is_null( $this->mText
) ) {
755 // Revision text is immutable. Load on demand:
756 $this->mText
= $this->loadText();
764 public function getTimestamp() {
765 return wfTimestamp( TS_MW
, $this->mTimestamp
);
771 public function isCurrent() {
772 return $this->mCurrent
;
776 * Get previous revision for this title
778 * @return Revision or null
780 public function getPrevious() {
781 if( $this->getTitle() ) {
782 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
784 return Revision
::newFromTitle( $this->getTitle(), $prev );
791 * Get next revision for this title
793 * @return Revision or null
795 public function getNext() {
796 if( $this->getTitle() ) {
797 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
799 return Revision
::newFromTitle( $this->getTitle(), $next );
806 * Get previous revision Id for this page_id
807 * This is used to populate rev_parent_id on save
809 * @param $db DatabaseBase
812 private function getPreviousRevisionId( $db ) {
813 if( is_null( $this->mPage
) ) {
816 # Use page_latest if ID is not given
818 $prevId = $db->selectField( 'page', 'page_latest',
819 array( 'page_id' => $this->mPage
),
822 $prevId = $db->selectField( 'revision', 'rev_id',
823 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
825 array( 'ORDER BY' => 'rev_id DESC' ) );
827 return intval( $prevId );
831 * Get revision text associated with an old or archive row
832 * $row is usually an object from wfFetchRow(), both the flags and the text
833 * field must be included
835 * @param $row Object: the text data
836 * @param $prefix String: table prefix (default 'old_')
837 * @return String: text the text requested or false on failure
839 public static function getRevisionText( $row, $prefix = 'old_' ) {
840 wfProfileIn( __METHOD__
);
843 $textField = $prefix . 'text';
844 $flagsField = $prefix . 'flags';
846 if( isset( $row->$flagsField ) ) {
847 $flags = explode( ',', $row->$flagsField );
852 if( isset( $row->$textField ) ) {
853 $text = $row->$textField;
855 wfProfileOut( __METHOD__
);
859 # Use external methods for external objects, text in table is URL-only then
860 if ( in_array( 'external', $flags ) ) {
862 $parts = explode( '://', $url, 2 );
863 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
864 wfProfileOut( __METHOD__
);
867 $text = ExternalStore
::fetchFromURL( $url );
870 // If the text was fetched without an error, convert it
871 if ( $text !== false ) {
872 if( in_array( 'gzip', $flags ) ) {
873 # Deal with optional compression of archived pages.
874 # This can be done periodically via maintenance/compressOld.php, and
875 # as pages are saved if $wgCompressRevisions is set.
876 $text = gzinflate( $text );
879 if( in_array( 'object', $flags ) ) {
880 # Generic compressed storage
881 $obj = unserialize( $text );
882 if ( !is_object( $obj ) ) {
884 wfProfileOut( __METHOD__
);
887 $text = $obj->getText();
890 global $wgLegacyEncoding;
891 if( $text !== false && $wgLegacyEncoding
892 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
894 # Old revisions kept around in a legacy encoding?
895 # Upconvert on demand.
896 # ("utf8" checked for compatibility with some broken
897 # conversion scripts 2008-12-30)
899 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
902 wfProfileOut( __METHOD__
);
907 * If $wgCompressRevisions is enabled, we will compress data.
908 * The input string is modified in place.
909 * Return value is the flags field: contains 'gzip' if the
910 * data is compressed, and 'utf-8' if we're saving in UTF-8
913 * @param $text Mixed: reference to a text
916 public static function compressRevisionText( &$text ) {
917 global $wgCompressRevisions;
920 # Revisions not marked this way will be converted
921 # on load if $wgLegacyCharset is set in the future.
924 if( $wgCompressRevisions ) {
925 if( function_exists( 'gzdeflate' ) ) {
926 $text = gzdeflate( $text );
929 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
932 return implode( ',', $flags );
936 * Insert a new revision into the database, returning the new revision ID
937 * number on success and dies horribly on failure.
939 * @param $dbw DatabaseBase: (master connection)
942 public function insertOn( $dbw ) {
943 global $wgDefaultExternalStore;
945 wfProfileIn( __METHOD__
);
947 $data = $this->mText
;
948 $flags = Revision
::compressRevisionText( $data );
950 # Write to external storage if required
951 if( $wgDefaultExternalStore ) {
952 // Store and get the URL
953 $data = ExternalStore
::insertToDefault( $data );
955 throw new MWException( "Unable to store text to external storage" );
960 $flags .= 'external';
963 # Record the text (or external storage URL) to the text table
964 if( !isset( $this->mTextId
) ) {
965 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
966 $dbw->insert( 'text',
970 'old_flags' => $flags,
973 $this->mTextId
= $dbw->insertId();
976 if ( $this->mComment
=== null ) $this->mComment
= "";
978 # Record the edit in revisions
979 $rev_id = isset( $this->mId
)
981 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
982 $dbw->insert( 'revision',
985 'rev_page' => $this->mPage
,
986 'rev_text_id' => $this->mTextId
,
987 'rev_comment' => $this->mComment
,
988 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
989 'rev_user' => $this->mUser
,
990 'rev_user_text' => $this->mUserText
,
991 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
992 'rev_deleted' => $this->mDeleted
,
993 'rev_len' => $this->mSize
,
994 'rev_parent_id' => is_null( $this->mParentId
)
995 ?
$this->getPreviousRevisionId( $dbw )
997 'rev_sha1' => is_null( $this->mSha1
)
998 ? Revision
::base36Sha1( $this->mText
)
1003 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1005 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1007 wfProfileOut( __METHOD__
);
1012 * Get the base 36 SHA-1 value for a string of text
1013 * @param $text String
1016 public static function base36Sha1( $text ) {
1017 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1021 * Lazy-load the revision's text.
1022 * Currently hardcoded to the 'text' table storage engine.
1026 protected function loadText() {
1027 wfProfileIn( __METHOD__
);
1029 // Caching may be beneficial for massive use of external storage
1030 global $wgRevisionCacheExpiry, $wgMemc;
1031 $textId = $this->getTextId();
1032 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1033 if( $wgRevisionCacheExpiry ) {
1034 $text = $wgMemc->get( $key );
1035 if( is_string( $text ) ) {
1036 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1037 wfProfileOut( __METHOD__
);
1042 // If we kept data for lazy extraction, use it now...
1043 if ( isset( $this->mTextRow
) ) {
1044 $row = $this->mTextRow
;
1045 $this->mTextRow
= null;
1051 // Text data is immutable; check slaves first.
1052 $dbr = wfGetDB( DB_SLAVE
);
1053 $row = $dbr->selectRow( 'text',
1054 array( 'old_text', 'old_flags' ),
1055 array( 'old_id' => $this->getTextId() ),
1059 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1060 // Possible slave lag!
1061 $dbw = wfGetDB( DB_MASTER
);
1062 $row = $dbw->selectRow( 'text',
1063 array( 'old_text', 'old_flags' ),
1064 array( 'old_id' => $this->getTextId() ),
1068 $text = self
::getRevisionText( $row );
1070 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1071 if( $wgRevisionCacheExpiry && $text !== false ) {
1072 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1075 wfProfileOut( __METHOD__
);
1081 * Create a new null-revision for insertion into a page's
1082 * history. This will not re-save the text, but simply refer
1083 * to the text from the previous version.
1085 * Such revisions can for instance identify page rename
1086 * operations and other such meta-modifications.
1088 * @param $dbw DatabaseBase
1089 * @param $pageId Integer: ID number of the page to read from
1090 * @param $summary String: revision's summary
1091 * @param $minor Boolean: whether the revision should be considered as minor
1092 * @return Revision|null on error
1094 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1095 wfProfileIn( __METHOD__
);
1097 $current = $dbw->selectRow(
1098 array( 'page', 'revision' ),
1099 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' ),
1101 'page_id' => $pageId,
1102 'page_latest=rev_id',
1107 $revision = new Revision( array(
1109 'comment' => $summary,
1110 'minor_edit' => $minor,
1111 'text_id' => $current->rev_text_id
,
1112 'parent_id' => $current->page_latest
,
1113 'len' => $current->rev_len
,
1114 'sha1' => $current->rev_sha1
1120 wfProfileOut( __METHOD__
);
1125 * Determine if the current user is allowed to view a particular
1126 * field of this revision, if it's marked as deleted.
1128 * @param $field Integer:one of self::DELETED_TEXT,
1129 * self::DELETED_COMMENT,
1130 * self::DELETED_USER
1131 * @param $user User object to check, or null to use $wgUser
1134 public function userCan( $field, User
$user = null ) {
1135 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1139 * Determine if the current user is allowed to view a particular
1140 * field of this revision, if it's marked as deleted. This is used
1141 * by various classes to avoid duplication.
1143 * @param $bitfield Integer: current field
1144 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1145 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1146 * self::DELETED_USER = File::DELETED_USER
1147 * @param $user User object to check, or null to use $wgUser
1150 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1151 if( $bitfield & $field ) { // aspect is deleted
1152 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1153 $permission = 'suppressrevision';
1154 } elseif ( $field & self
::DELETED_TEXT
) {
1155 $permission = 'deletedtext';
1157 $permission = 'deletedhistory';
1159 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1160 if ( $user === null ) {
1164 return $user->isAllowed( $permission );
1171 * Get rev_timestamp from rev_id, without loading the rest of the row
1173 * @param $title Title
1174 * @param $id Integer
1177 static function getTimestampFromId( $title, $id ) {
1178 $dbr = wfGetDB( DB_SLAVE
);
1179 // Casting fix for DB2
1183 $conds = array( 'rev_id' => $id );
1184 $conds['rev_page'] = $title->getArticleId();
1185 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1186 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1187 # Not in slave, try master
1188 $dbw = wfGetDB( DB_MASTER
);
1189 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1191 return wfTimestamp( TS_MW
, $timestamp );
1195 * Get count of revisions per page...not very efficient
1197 * @param $db DatabaseBase
1198 * @param $id Integer: page id
1201 static function countByPageId( $db, $id ) {
1202 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1203 array( 'rev_page' => $id ), __METHOD__
);
1205 return $row->revCount
;
1211 * Get count of revisions per page...not very efficient
1213 * @param $db DatabaseBase
1214 * @param $title Title
1217 static function countByTitle( $db, $title ) {
1218 $id = $title->getArticleId();
1220 return Revision
::countByPageId( $db, $id );