10 protected $mOrigUserText;
12 protected $mMinorEdit;
13 protected $mTimestamp;
23 const DELETED_TEXT
= 1;
24 const DELETED_COMMENT
= 2;
25 const DELETED_USER
= 4;
26 const DELETED_RESTRICTED
= 8;
28 const SUPPRESSED_USER
= 12;
29 // Audience options for Revision::getText()
31 const FOR_THIS_USER
= 2;
35 * Load a page revision from a given revision ID number.
36 * Returns null if no such revision can be found.
39 * @return Revision or null
41 public static function newFromId( $id ) {
42 return Revision
::newFromConds( array( 'rev_id' => intval( $id ) ) );
46 * Load either the current, or a specified, revision
47 * that's attached to a given title. If not attached
48 * to that title, will return null.
51 * @param $id Integer (optional)
52 * @return Revision or null
54 public static function newFromTitle( $title, $id = 0 ) {
56 'page_namespace' => $title->getNamespace(),
57 'page_title' => $title->getDBkey()
60 // Use the specified ID
61 $conds['rev_id'] = $id;
62 } elseif ( wfGetLB()->getServerCount() > 1 ) {
63 // Get the latest revision ID from the master
64 $dbw = wfGetDB( DB_MASTER
);
65 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
66 if ( $latest === false ) {
67 return null; // page does not exist
69 $conds['rev_id'] = $latest;
71 // Use a join to get the latest revision
72 $conds[] = 'rev_id=page_latest';
74 return Revision
::newFromConds( $conds );
78 * Load either the current, or a specified, revision
79 * that's attached to a given page ID.
80 * Returns null if no such revision can be found.
82 * @param $revId Integer
83 * @param $pageId Integer (optional)
84 * @return Revision or null
86 public static function newFromPageId( $pageId, $revId = 0 ) {
87 $conds = array( 'page_id' => $pageId );
89 $conds['rev_id'] = $revId;
90 } elseif ( wfGetLB()->getServerCount() > 1 ) {
91 // Get the latest revision ID from the master
92 $dbw = wfGetDB( DB_MASTER
);
93 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
94 if ( $latest === false ) {
95 return null; // page does not exist
97 $conds['rev_id'] = $latest;
99 $conds[] = 'rev_id = page_latest';
101 return Revision
::newFromConds( $conds );
105 * Make a fake revision object from an archive table row. This is queried
106 * for permissions or even inserted (as in Special:Undelete)
107 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
110 * @param $overrides array
114 public static function newFromArchiveRow( $row, $overrides = array() ) {
115 $attribs = $overrides +
array(
116 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
117 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
118 'comment' => $row->ar_comment
,
119 'user' => $row->ar_user
,
120 'user_text' => $row->ar_user_text
,
121 'timestamp' => $row->ar_timestamp
,
122 'minor_edit' => $row->ar_minor_edit
,
123 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
124 'deleted' => $row->ar_deleted
,
125 'len' => $row->ar_len
127 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
128 // Pre-1.5 ar_text row
129 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
130 if ( $attribs['text'] === false ) {
131 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
134 return new self( $attribs );
143 public static function newFromRow( $row ) {
144 return new self( $row );
148 * Load a page revision from a given revision ID number.
149 * Returns null if no such revision can be found.
151 * @param $db DatabaseBase
153 * @return Revision or null
155 public static function loadFromId( $db, $id ) {
156 return Revision
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
160 * Load either the current, or a specified, revision
161 * that's attached to a given page. If not attached
162 * to that page, will return null.
164 * @param $db DatabaseBase
165 * @param $pageid Integer
167 * @return Revision or null
169 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
170 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
172 $conds['rev_id'] = intval( $id );
174 $conds[] = 'rev_id=page_latest';
176 return Revision
::loadFromConds( $db, $conds );
180 * Load either the current, or a specified, revision
181 * that's attached to a given page. If not attached
182 * to that page, will return null.
184 * @param $db DatabaseBase
185 * @param $title Title
187 * @return Revision or null
189 public static function loadFromTitle( $db, $title, $id = 0 ) {
191 $matchId = intval( $id );
193 $matchId = 'page_latest';
195 return Revision
::loadFromConds( $db,
196 array( "rev_id=$matchId",
197 'page_namespace' => $title->getNamespace(),
198 'page_title' => $title->getDBkey() )
203 * Load the revision for the given title with the given timestamp.
204 * WARNING: Timestamps may in some circumstances not be unique,
205 * so this isn't the best key to use.
207 * @param $db DatabaseBase
208 * @param $title Title
209 * @param $timestamp String
210 * @return Revision or null
212 public static function loadFromTimestamp( $db, $title, $timestamp ) {
213 return Revision
::loadFromConds( $db,
214 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
215 'page_namespace' => $title->getNamespace(),
216 'page_title' => $title->getDBkey() )
221 * Given a set of conditions, fetch a revision.
223 * @param $conditions Array
224 * @return Revision or null
226 public static function newFromConds( $conditions ) {
227 $db = wfGetDB( DB_SLAVE
);
228 $rev = Revision
::loadFromConds( $db, $conditions );
229 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
230 $dbw = wfGetDB( DB_MASTER
);
231 $rev = Revision
::loadFromConds( $dbw, $conditions );
237 * Given a set of conditions, fetch a revision from
238 * the given database connection.
240 * @param $db DatabaseBase
241 * @param $conditions Array
242 * @return Revision or null
244 private static function loadFromConds( $db, $conditions ) {
245 $res = Revision
::fetchFromConds( $db, $conditions );
247 $row = $res->fetchObject();
249 $ret = new Revision( $row );
258 * Return a wrapper for a series of database rows to
259 * fetch all of a given page's revisions in turn.
260 * Each row can be fed to the constructor to get objects.
262 * @param $title Title
263 * @return ResultWrapper
265 public static function fetchRevision( $title ) {
266 return Revision
::fetchFromConds(
268 array( 'rev_id=page_latest',
269 'page_namespace' => $title->getNamespace(),
270 'page_title' => $title->getDBkey() )
275 * Given a set of conditions, return a ResultWrapper
276 * which will return matching database rows with the
277 * fields necessary to build Revision objects.
279 * @param $db DatabaseBase
280 * @param $conditions Array
281 * @return ResultWrapper
283 private static function fetchFromConds( $db, $conditions ) {
284 $fields = array_merge(
285 self
::selectFields(),
286 self
::selectPageFields(),
287 self
::selectUserFields()
290 array( 'revision', 'page', 'user' ),
294 array( 'LIMIT' => 1 ),
295 array( 'page' => array( 'INNER JOIN', 'page_id = rev_page' ),
296 'user' => array( 'LEFT JOIN', 'rev_user != 0 AND user_id = rev_user' ) )
301 * Return the list of revision fields that should be selected to create
304 public static function selectFields() {
321 * Return the list of text fields that should be selected to read the
324 public static function selectTextFields() {
332 * Return the list of page fields that should be selected from page table
334 public static function selectPageFields() {
343 * Return the list of user fields that should be selected from user table
345 public static function selectUserFields() {
346 return array( 'user_name' );
352 * @param $row Mixed: either a database row or an array
355 function __construct( $row ) {
356 if( is_object( $row ) ) {
357 $this->mId
= intval( $row->rev_id
);
358 $this->mPage
= intval( $row->rev_page
);
359 $this->mTextId
= intval( $row->rev_text_id
);
360 $this->mComment
= $row->rev_comment
;
361 $this->mUser
= intval( $row->rev_user
);
362 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
363 $this->mTimestamp
= $row->rev_timestamp
;
364 $this->mDeleted
= intval( $row->rev_deleted
);
366 if( !isset( $row->rev_parent_id
) ) {
367 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
369 $this->mParentId
= intval( $row->rev_parent_id
);
372 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
375 $this->mSize
= intval( $row->rev_len
);
378 if( isset( $row->page_latest
) ) {
379 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
380 $this->mTitle
= Title
::newFromRow( $row );
382 $this->mCurrent
= false;
383 $this->mTitle
= null;
386 // Lazy extraction...
388 if( isset( $row->old_text
) ) {
389 $this->mTextRow
= $row;
391 // 'text' table row entry will be lazy-loaded
392 $this->mTextRow
= null;
395 // Use user_name for users and rev_user_text for IPs...
396 $this->mUserText
= null; // lazy load if left null
397 if ( $this->mUser
== 0 ) {
398 $this->mUserText
= $row->rev_user_text
; // IP user
399 } elseif ( isset( $row->user_name
) ) {
400 $this->mUserText
= $row->user_name
; // logged-in user
402 $this->mOrigUserText
= $row->rev_user_text
;
403 } elseif( is_array( $row ) ) {
404 // Build a new revision to be saved...
407 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
408 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
409 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
410 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
411 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
412 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
413 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
414 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
415 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
416 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
418 // Enforce spacing trimming on supplied text
419 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
420 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
421 $this->mTextRow
= null;
423 $this->mTitle
= null; # Load on demand if needed
424 $this->mCurrent
= false;
425 # If we still have no len_size, see it we have the text to figure it out
426 if ( !$this->mSize
) {
427 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
430 throw new MWException( 'Revision constructor passed invalid row format.' );
432 $this->mUnpatrolled
= null;
440 public function getId() {
449 public function getTextId() {
450 return $this->mTextId
;
454 * Get parent revision ID (the original previous page revision)
458 public function getParentId() {
459 return $this->mParentId
;
463 * Returns the length of the text in this revision, or null if unknown.
467 public function getSize() {
472 * Returns the title of the page associated with this entry.
476 public function getTitle() {
477 if( isset( $this->mTitle
) ) {
478 return $this->mTitle
;
480 $dbr = wfGetDB( DB_SLAVE
);
481 $row = $dbr->selectRow(
482 array( 'page', 'revision' ),
483 array( 'page_namespace', 'page_title' ),
484 array( 'page_id=rev_page',
485 'rev_id' => $this->mId
),
486 'Revision::getTitle' );
488 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
490 return $this->mTitle
;
494 * Set the title of the revision
496 * @param $title Title
498 public function setTitle( $title ) {
499 $this->mTitle
= $title;
507 public function getPage() {
512 * Fetch revision's user id if it's available to the specified audience.
513 * If the specified audience does not have access to it, zero will be
516 * @param $audience Integer: one of:
517 * Revision::FOR_PUBLIC to be displayed to all users
518 * Revision::FOR_THIS_USER to be displayed to $wgUser
519 * Revision::RAW get the ID regardless of permissions
520 * @param $user User object to check for, only if FOR_THIS_USER is passed
521 * to the $audience parameter
524 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
525 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
527 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
535 * Fetch revision's user id without regard for the current user's permissions
539 public function getRawUser() {
544 * Fetch revision's username if it's available to the specified audience.
545 * If the specified audience does not have access to the username, an
546 * empty string will be returned.
548 * @param $audience Integer: one of:
549 * Revision::FOR_PUBLIC to be displayed to all users
550 * Revision::FOR_THIS_USER to be displayed to $wgUser
551 * Revision::RAW get the text regardless of permissions
552 * @param $user User object to check for, only if FOR_THIS_USER is passed
553 * to the $audience parameter
556 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
557 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
559 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
562 return $this->getRawUserText();
567 * Fetch revision's username without regard for view restrictions
571 public function getRawUserText() {
572 if ( $this->mUserText
=== null ) {
573 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
574 if ( $this->mUserText
=== false ) {
575 # This shouldn't happen, but it can if the wiki was recovered
576 # via importing revs and there is no user table entry yet.
577 $this->mUserText
= $this->mOrigUserText
;
580 return $this->mUserText
;
584 * Fetch revision comment if it's available to the specified audience.
585 * If the specified audience does not have access to the comment, an
586 * empty string will be returned.
588 * @param $audience Integer: one of:
589 * Revision::FOR_PUBLIC to be displayed to all users
590 * Revision::FOR_THIS_USER to be displayed to $wgUser
591 * Revision::RAW get the text regardless of permissions
592 * @param $user User object to check for, only if FOR_THIS_USER is passed
593 * to the $audience parameter
596 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
597 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
599 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
602 return $this->mComment
;
607 * Fetch revision comment without regard for the current user's permissions
611 public function getRawComment() {
612 return $this->mComment
;
618 public function isMinor() {
619 return (bool)$this->mMinorEdit
;
623 * @return Integer rcid of the unpatrolled row, zero if there isn't one
625 public function isUnpatrolled() {
626 if( $this->mUnpatrolled
!== null ) {
627 return $this->mUnpatrolled
;
629 $dbr = wfGetDB( DB_SLAVE
);
630 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
632 array( // Add redundant user,timestamp condition so we can use the existing index
633 'rc_user_text' => $this->getRawUserText(),
634 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
635 'rc_this_oldid' => $this->getId(),
640 return (int)$this->mUnpatrolled
;
644 * @param $field int one of DELETED_* bitfield constants
648 public function isDeleted( $field ) {
649 return ( $this->mDeleted
& $field ) == $field;
653 * Get the deletion bitfield of the revision
657 public function getVisibility() {
658 return (int)$this->mDeleted
;
662 * Fetch revision text if it's available to the specified audience.
663 * If the specified audience does not have the ability to view this
664 * revision, an empty string will be returned.
666 * @param $audience Integer: one of:
667 * Revision::FOR_PUBLIC to be displayed to all users
668 * Revision::FOR_THIS_USER to be displayed to $wgUser
669 * Revision::RAW get the text regardless of permissions
670 * @param $user User object to check for, only if FOR_THIS_USER is passed
671 * to the $audience parameter
674 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
675 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
677 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
680 return $this->getRawText();
685 * Alias for getText(Revision::FOR_THIS_USER)
687 * @deprecated since 1.17
690 public function revText() {
691 wfDeprecated( __METHOD__
);
692 return $this->getText( self
::FOR_THIS_USER
);
696 * Fetch revision text without regard for view restrictions
700 public function getRawText() {
701 if( is_null( $this->mText
) ) {
702 // Revision text is immutable. Load on demand:
703 $this->mText
= $this->loadText();
711 public function getTimestamp() {
712 return wfTimestamp( TS_MW
, $this->mTimestamp
);
718 public function isCurrent() {
719 return $this->mCurrent
;
723 * Get previous revision for this title
725 * @return Revision or null
727 public function getPrevious() {
728 if( $this->getTitle() ) {
729 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
731 return Revision
::newFromTitle( $this->getTitle(), $prev );
738 * Get next revision for this title
740 * @return Revision or null
742 public function getNext() {
743 if( $this->getTitle() ) {
744 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
746 return Revision
::newFromTitle( $this->getTitle(), $next );
753 * Get previous revision Id for this page_id
754 * This is used to populate rev_parent_id on save
756 * @param $db DatabaseBase
759 private function getPreviousRevisionId( $db ) {
760 if( is_null( $this->mPage
) ) {
763 # Use page_latest if ID is not given
765 $prevId = $db->selectField( 'page', 'page_latest',
766 array( 'page_id' => $this->mPage
),
769 $prevId = $db->selectField( 'revision', 'rev_id',
770 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
772 array( 'ORDER BY' => 'rev_id DESC' ) );
774 return intval( $prevId );
778 * Get revision text associated with an old or archive row
779 * $row is usually an object from wfFetchRow(), both the flags and the text
780 * field must be included
782 * @param $row Object: the text data
783 * @param $prefix String: table prefix (default 'old_')
784 * @return String: text the text requested or false on failure
786 public static function getRevisionText( $row, $prefix = 'old_' ) {
787 wfProfileIn( __METHOD__
);
790 $textField = $prefix . 'text';
791 $flagsField = $prefix . 'flags';
793 if( isset( $row->$flagsField ) ) {
794 $flags = explode( ',', $row->$flagsField );
799 if( isset( $row->$textField ) ) {
800 $text = $row->$textField;
802 wfProfileOut( __METHOD__
);
806 # Use external methods for external objects, text in table is URL-only then
807 if ( in_array( 'external', $flags ) ) {
809 $parts = explode( '://', $url, 2 );
810 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
811 wfProfileOut( __METHOD__
);
814 $text = ExternalStore
::fetchFromURL( $url );
817 // If the text was fetched without an error, convert it
818 if ( $text !== false ) {
819 if( in_array( 'gzip', $flags ) ) {
820 # Deal with optional compression of archived pages.
821 # This can be done periodically via maintenance/compressOld.php, and
822 # as pages are saved if $wgCompressRevisions is set.
823 $text = gzinflate( $text );
826 if( in_array( 'object', $flags ) ) {
827 # Generic compressed storage
828 $obj = unserialize( $text );
829 if ( !is_object( $obj ) ) {
831 wfProfileOut( __METHOD__
);
834 $text = $obj->getText();
837 global $wgLegacyEncoding;
838 if( $text !== false && $wgLegacyEncoding
839 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
841 # Old revisions kept around in a legacy encoding?
842 # Upconvert on demand.
843 # ("utf8" checked for compatibility with some broken
844 # conversion scripts 2008-12-30)
846 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
849 wfProfileOut( __METHOD__
);
854 * If $wgCompressRevisions is enabled, we will compress data.
855 * The input string is modified in place.
856 * Return value is the flags field: contains 'gzip' if the
857 * data is compressed, and 'utf-8' if we're saving in UTF-8
860 * @param $text Mixed: reference to a text
863 public static function compressRevisionText( &$text ) {
864 global $wgCompressRevisions;
867 # Revisions not marked this way will be converted
868 # on load if $wgLegacyCharset is set in the future.
871 if( $wgCompressRevisions ) {
872 if( function_exists( 'gzdeflate' ) ) {
873 $text = gzdeflate( $text );
876 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
879 return implode( ',', $flags );
883 * Insert a new revision into the database, returning the new revision ID
884 * number on success and dies horribly on failure.
886 * @param $dbw DatabaseBase: (master connection)
889 public function insertOn( $dbw ) {
890 global $wgDefaultExternalStore;
892 wfProfileIn( __METHOD__
);
894 $data = $this->mText
;
895 $flags = Revision
::compressRevisionText( $data );
897 # Write to external storage if required
898 if( $wgDefaultExternalStore ) {
899 // Store and get the URL
900 $data = ExternalStore
::insertToDefault( $data );
902 throw new MWException( "Unable to store text to external storage" );
907 $flags .= 'external';
910 # Record the text (or external storage URL) to the text table
911 if( !isset( $this->mTextId
) ) {
912 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
913 $dbw->insert( 'text',
917 'old_flags' => $flags,
920 $this->mTextId
= $dbw->insertId();
923 if ( $this->mComment
=== null ) $this->mComment
= "";
925 # Record the edit in revisions
926 $rev_id = isset( $this->mId
)
928 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
929 $dbw->insert( 'revision',
932 'rev_page' => $this->mPage
,
933 'rev_text_id' => $this->mTextId
,
934 'rev_comment' => $this->mComment
,
935 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
936 'rev_user' => $this->mUser
,
937 'rev_user_text' => $this->mUserText
,
938 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
939 'rev_deleted' => $this->mDeleted
,
940 'rev_len' => $this->mSize
,
941 'rev_parent_id' => is_null($this->mParentId
) ?
942 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
946 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
948 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
950 wfProfileOut( __METHOD__
);
955 * Lazy-load the revision's text.
956 * Currently hardcoded to the 'text' table storage engine.
960 protected function loadText() {
961 wfProfileIn( __METHOD__
);
963 // Caching may be beneficial for massive use of external storage
964 global $wgRevisionCacheExpiry, $wgMemc;
965 $textId = $this->getTextId();
966 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
967 if( $wgRevisionCacheExpiry ) {
968 $text = $wgMemc->get( $key );
969 if( is_string( $text ) ) {
970 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
971 wfProfileOut( __METHOD__
);
976 // If we kept data for lazy extraction, use it now...
977 if ( isset( $this->mTextRow
) ) {
978 $row = $this->mTextRow
;
979 $this->mTextRow
= null;
985 // Text data is immutable; check slaves first.
986 $dbr = wfGetDB( DB_SLAVE
);
987 $row = $dbr->selectRow( 'text',
988 array( 'old_text', 'old_flags' ),
989 array( 'old_id' => $this->getTextId() ),
993 if( !$row && wfGetLB()->getServerCount() > 1 ) {
994 // Possible slave lag!
995 $dbw = wfGetDB( DB_MASTER
);
996 $row = $dbw->selectRow( 'text',
997 array( 'old_text', 'old_flags' ),
998 array( 'old_id' => $this->getTextId() ),
1002 $text = self
::getRevisionText( $row );
1004 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1005 if( $wgRevisionCacheExpiry && $text !== false ) {
1006 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1009 wfProfileOut( __METHOD__
);
1015 * Create a new null-revision for insertion into a page's
1016 * history. This will not re-save the text, but simply refer
1017 * to the text from the previous version.
1019 * Such revisions can for instance identify page rename
1020 * operations and other such meta-modifications.
1022 * @param $dbw DatabaseBase
1023 * @param $pageId Integer: ID number of the page to read from
1024 * @param $summary String: revision's summary
1025 * @param $minor Boolean: whether the revision should be considered as minor
1026 * @return Revision|null on error
1028 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1029 wfProfileIn( __METHOD__
);
1031 $current = $dbw->selectRow(
1032 array( 'page', 'revision' ),
1033 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1035 'page_id' => $pageId,
1036 'page_latest=rev_id',
1041 $revision = new Revision( array(
1043 'comment' => $summary,
1044 'minor_edit' => $minor,
1045 'text_id' => $current->rev_text_id
,
1046 'parent_id' => $current->page_latest
,
1047 'len' => $current->rev_len
1053 wfProfileOut( __METHOD__
);
1058 * Determine if the current user is allowed to view a particular
1059 * field of this revision, if it's marked as deleted.
1061 * @param $field Integer:one of self::DELETED_TEXT,
1062 * self::DELETED_COMMENT,
1063 * self::DELETED_USER
1064 * @param $user User object to check, or null to use $wgUser
1067 public function userCan( $field, User
$user = null ) {
1068 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1072 * Determine if the current user is allowed to view a particular
1073 * field of this revision, if it's marked as deleted. This is used
1074 * by various classes to avoid duplication.
1076 * @param $bitfield Integer: current field
1077 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1078 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1079 * self::DELETED_USER = File::DELETED_USER
1080 * @param $user User object to check, or null to use $wgUser
1083 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1084 if( $bitfield & $field ) { // aspect is deleted
1085 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1086 $permission = 'suppressrevision';
1087 } elseif ( $field & self
::DELETED_TEXT
) {
1088 $permission = 'deletedtext';
1090 $permission = 'deletedhistory';
1092 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1093 if ( $user === null ) {
1097 return $user->isAllowed( $permission );
1104 * Get rev_timestamp from rev_id, without loading the rest of the row
1106 * @param $title Title
1107 * @param $id Integer
1110 static function getTimestampFromId( $title, $id ) {
1111 $dbr = wfGetDB( DB_SLAVE
);
1112 // Casting fix for DB2
1116 $conds = array( 'rev_id' => $id );
1117 $conds['rev_page'] = $title->getArticleId();
1118 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1119 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1120 # Not in slave, try master
1121 $dbw = wfGetDB( DB_MASTER
);
1122 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1124 return wfTimestamp( TS_MW
, $timestamp );
1128 * Get count of revisions per page...not very efficient
1130 * @param $db DatabaseBase
1131 * @param $id Integer: page id
1134 static function countByPageId( $db, $id ) {
1135 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1136 array( 'rev_page' => $id ), __METHOD__
);
1138 return $row->revCount
;
1144 * Get count of revisions per page...not very efficient
1146 * @param $db DatabaseBase
1147 * @param $title Title
1150 static function countByTitle( $db, $title ) {
1151 $id = $title->getArticleId();
1153 return Revision
::countByPageId( $db, $id );