7 const DELETED_TEXT
= 1;
8 const DELETED_COMMENT
= 2;
9 const DELETED_USER
= 4;
10 const DELETED_RESTRICTED
= 8;
12 const SUPPRESSED_USER
= 12;
13 // Audience options for Revision::getText()
15 const FOR_THIS_USER
= 2;
19 * Load a page revision from a given revision ID number.
20 * Returns null if no such revision can be found.
23 * @return Revision or null
25 public static function newFromId( $id ) {
26 return Revision
::newFromConds( array( 'rev_id' => intval( $id ) ) );
30 * Load either the current, or a specified, revision
31 * that's attached to a given title. If not attached
32 * to that title, will return null.
35 * @param $id Integer (optional)
36 * @return Revision or null
38 public static function newFromTitle( $title, $id = 0 ) {
40 'page_namespace' => $title->getNamespace(),
41 'page_title' => $title->getDBkey()
44 // Use the specified ID
45 $conds['rev_id'] = $id;
46 } elseif ( wfGetLB()->getServerCount() > 1 ) {
47 // Get the latest revision ID from the master
48 $dbw = wfGetDB( DB_MASTER
);
49 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
50 if ( $latest === false ) {
51 return null; // page does not exist
53 $conds['rev_id'] = $latest;
55 // Use a join to get the latest revision
56 $conds[] = 'rev_id=page_latest';
58 return Revision
::newFromConds( $conds );
62 * Load either the current, or a specified, revision
63 * that's attached to a given page ID.
64 * Returns null if no such revision can be found.
66 * @param $revId Integer
67 * @param $pageId Integer (optional)
68 * @return Revision or null
70 public static function newFromPageId( $pageId, $revId = 0 ) {
71 $conds = array( 'page_id' => $pageId );
73 $conds['rev_id'] = $revId;
74 } elseif ( wfGetLB()->getServerCount() > 1 ) {
75 // Get the latest revision ID from the master
76 $dbw = wfGetDB( DB_MASTER
);
77 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
78 if ( $latest === false ) {
79 return null; // page does not exist
81 $conds['rev_id'] = $latest;
83 $conds[] = 'rev_id = page_latest';
85 return Revision
::newFromConds( $conds );
89 * Make a fake revision object from an archive table row. This is queried
90 * for permissions or even inserted (as in Special:Undelete)
91 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
94 * @param $overrides array
98 public static function newFromArchiveRow( $row, $overrides = array() ) {
99 $attribs = $overrides +
array(
100 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
101 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
102 'comment' => $row->ar_comment
,
103 'user' => $row->ar_user
,
104 'user_text' => $row->ar_user_text
,
105 'timestamp' => $row->ar_timestamp
,
106 'minor_edit' => $row->ar_minor_edit
,
107 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
108 'deleted' => $row->ar_deleted
,
109 'len' => $row->ar_len
111 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
112 // Pre-1.5 ar_text row
113 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
114 if ( $attribs['text'] === false ) {
115 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
118 return new self( $attribs );
127 public static function newFromRow( $row ) {
128 return new self( $row );
132 * Load a page revision from a given revision ID number.
133 * Returns null if no such revision can be found.
135 * @param $db DatabaseBase
137 * @return Revision or null
139 public static function loadFromId( $db, $id ) {
140 return Revision
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
144 * Load either the current, or a specified, revision
145 * that's attached to a given page. If not attached
146 * to that page, will return null.
148 * @param $db DatabaseBase
149 * @param $pageid Integer
151 * @return Revision or null
153 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
154 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
156 $conds['rev_id'] = intval( $id );
158 $conds[] = 'rev_id=page_latest';
160 return Revision
::loadFromConds( $db, $conds );
164 * Load either the current, or a specified, revision
165 * that's attached to a given page. If not attached
166 * to that page, will return null.
168 * @param $db DatabaseBase
169 * @param $title Title
171 * @return Revision or null
173 public static function loadFromTitle( $db, $title, $id = 0 ) {
175 $matchId = intval( $id );
177 $matchId = 'page_latest';
179 return Revision
::loadFromConds( $db,
180 array( "rev_id=$matchId",
181 'page_namespace' => $title->getNamespace(),
182 'page_title' => $title->getDBkey() )
187 * Load the revision for the given title with the given timestamp.
188 * WARNING: Timestamps may in some circumstances not be unique,
189 * so this isn't the best key to use.
191 * @param $db DatabaseBase
192 * @param $title Title
193 * @param $timestamp String
194 * @return Revision or null
196 public static function loadFromTimestamp( $db, $title, $timestamp ) {
197 return Revision
::loadFromConds( $db,
198 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDBkey() )
205 * Given a set of conditions, fetch a revision.
207 * @param $conditions Array
208 * @return Revision or null
210 public static function newFromConds( $conditions ) {
211 $db = wfGetDB( DB_SLAVE
);
212 $rev = Revision
::loadFromConds( $db, $conditions );
213 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
214 $dbw = wfGetDB( DB_MASTER
);
215 $rev = Revision
::loadFromConds( $dbw, $conditions );
221 * Given a set of conditions, fetch a revision from
222 * the given database connection.
224 * @param $db DatabaseBase
225 * @param $conditions Array
226 * @return Revision or null
228 private static function loadFromConds( $db, $conditions ) {
229 $res = Revision
::fetchFromConds( $db, $conditions );
231 $row = $res->fetchObject();
233 $ret = new Revision( $row );
242 * Return a wrapper for a series of database rows to
243 * fetch all of a given page's revisions in turn.
244 * Each row can be fed to the constructor to get objects.
246 * @param $title Title
247 * @return ResultWrapper
249 public static function fetchRevision( $title ) {
250 return Revision
::fetchFromConds(
252 array( 'rev_id=page_latest',
253 'page_namespace' => $title->getNamespace(),
254 'page_title' => $title->getDBkey() )
259 * Given a set of conditions, return a ResultWrapper
260 * which will return matching database rows with the
261 * fields necessary to build Revision objects.
263 * @param $db DatabaseBase
264 * @param $conditions Array
265 * @return ResultWrapper
267 private static function fetchFromConds( $db, $conditions ) {
268 $fields = array_merge(
269 self
::selectFields(),
270 self
::selectPageFields(),
271 self
::selectUserFields()
274 array( 'revision', 'page', 'user' ),
278 array( 'LIMIT' => 1 ),
279 array( 'page' => array( 'INNER JOIN', 'page_id = rev_page' ),
280 'user' => array( 'LEFT JOIN', 'user_id = rev_user' ) )
285 * Return the list of revision fields that should be selected to create
288 public static function selectFields() {
305 * Return the list of text fields that should be selected to read the
308 static function selectTextFields() {
316 * Return the list of page fields that should be selected from page table
318 static function selectPageFields() {
327 * Return the list of user fields that should be selected from user table
329 static function selectUserFields() {
330 return array( 'COALESCE(user_name,rev_user_text) AS rev_user_name' );
336 * @param $row Mixed: either a database row or an array
339 function __construct( $row ) {
340 if( is_object( $row ) ) {
341 $this->mId
= intval( $row->rev_id
);
342 $this->mPage
= intval( $row->rev_page
);
343 $this->mTextId
= intval( $row->rev_text_id
);
344 $this->mComment
= $row->rev_comment
;
345 $this->mUser
= intval( $row->rev_user
);
346 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
347 $this->mTimestamp
= $row->rev_timestamp
;
348 $this->mDeleted
= intval( $row->rev_deleted
);
350 if( !isset( $row->rev_parent_id
) ) {
351 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
353 $this->mParentId
= intval( $row->rev_parent_id
);
356 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
359 $this->mSize
= intval( $row->rev_len
);
362 if( isset( $row->page_latest
) ) {
363 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
364 $this->mTitle
= Title
::newFromRow( $row );
366 $this->mCurrent
= false;
367 $this->mTitle
= null;
370 // Lazy extraction...
372 if( isset( $row->old_text
) ) {
373 $this->mTextRow
= $row;
375 // 'text' table row entry will be lazy-loaded
376 $this->mTextRow
= null;
379 if ( isset( $row->rev_user_name
) ) {
380 $this->mUserText
= $row->rev_user_name
; // current user name
381 } else { // fallback to rev_user_text
382 $this->mUserText
= $row->rev_user_text
; // de-normalized
384 } elseif( is_array( $row ) ) {
385 // Build a new revision to be saved...
388 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
389 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
390 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
391 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
392 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
393 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
394 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
395 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
396 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
397 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
399 // Enforce spacing trimming on supplied text
400 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
401 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
402 $this->mTextRow
= null;
404 $this->mTitle
= null; # Load on demand if needed
405 $this->mCurrent
= false;
406 # If we still have no len_size, see it we have the text to figure it out
407 if ( !$this->mSize
) {
408 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
411 throw new MWException( 'Revision constructor passed invalid row format.' );
413 $this->mUnpatrolled
= null;
421 public function getId() {
430 public function getTextId() {
431 return $this->mTextId
;
435 * Get parent revision ID (the original previous page revision)
439 public function getParentId() {
440 return $this->mParentId
;
444 * Returns the length of the text in this revision, or null if unknown.
448 public function getSize() {
453 * Returns the title of the page associated with this entry.
457 public function getTitle() {
458 if( isset( $this->mTitle
) ) {
459 return $this->mTitle
;
461 $dbr = wfGetDB( DB_SLAVE
);
462 $row = $dbr->selectRow(
463 array( 'page', 'revision' ),
464 array( 'page_namespace', 'page_title' ),
465 array( 'page_id=rev_page',
466 'rev_id' => $this->mId
),
467 'Revision::getTitle' );
469 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
471 return $this->mTitle
;
475 * Set the title of the revision
477 * @param $title Title
479 public function setTitle( $title ) {
480 $this->mTitle
= $title;
488 public function getPage() {
493 * Fetch revision's user id if it's available to the specified audience.
494 * If the specified audience does not have access to it, zero will be
497 * @param $audience Integer: one of:
498 * Revision::FOR_PUBLIC to be displayed to all users
499 * Revision::FOR_THIS_USER to be displayed to $wgUser
500 * Revision::RAW get the ID regardless of permissions
501 * @param $user User object to check for, only if FOR_THIS_USER is passed
502 * to the $audience parameter
505 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
506 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
508 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
516 * Fetch revision's user id without regard for the current user's permissions
520 public function getRawUser() {
525 * Fetch revision's username if it's available to the specified audience.
526 * If the specified audience does not have access to the username, an
527 * empty string will be returned.
529 * @param $audience Integer: one of:
530 * Revision::FOR_PUBLIC to be displayed to all users
531 * Revision::FOR_THIS_USER to be displayed to $wgUser
532 * Revision::RAW get the text regardless of permissions
533 * @param $user User object to check for, only if FOR_THIS_USER is passed
534 * to the $audience parameter
537 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
538 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
540 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
543 return $this->mUserText
;
548 * Fetch revision's username without regard for view restrictions
552 public function getRawUserText() {
553 return $this->mUserText
;
557 * Fetch revision comment if it's available to the specified audience.
558 * If the specified audience does not have access to the comment, an
559 * empty string will be returned.
561 * @param $audience Integer: one of:
562 * Revision::FOR_PUBLIC to be displayed to all users
563 * Revision::FOR_THIS_USER to be displayed to $wgUser
564 * Revision::RAW get the text regardless of permissions
565 * @param $user User object to check for, only if FOR_THIS_USER is passed
566 * to the $audience parameter
569 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
570 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
572 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
575 return $this->mComment
;
580 * Fetch revision comment without regard for the current user's permissions
584 public function getRawComment() {
585 return $this->mComment
;
591 public function isMinor() {
592 return (bool)$this->mMinorEdit
;
596 * @return Integer rcid of the unpatrolled row, zero if there isn't one
598 public function isUnpatrolled() {
599 if( $this->mUnpatrolled
!== null ) {
600 return $this->mUnpatrolled
;
602 $dbr = wfGetDB( DB_SLAVE
);
603 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
605 array( // Add redundant user,timestamp condition so we can use the existing index
606 'rc_user_text' => $this->getRawUserText(),
607 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
608 'rc_this_oldid' => $this->getId(),
613 return (int)$this->mUnpatrolled
;
617 * @param $field int one of DELETED_* bitfield constants
621 public function isDeleted( $field ) {
622 return ( $this->mDeleted
& $field ) == $field;
626 * Get the deletion bitfield of the revision
630 public function getVisibility() {
631 return (int)$this->mDeleted
;
635 * Fetch revision text if it's available to the specified audience.
636 * If the specified audience does not have the ability to view this
637 * revision, an empty string will be returned.
639 * @param $audience Integer: one of:
640 * Revision::FOR_PUBLIC to be displayed to all users
641 * Revision::FOR_THIS_USER to be displayed to $wgUser
642 * Revision::RAW get the text regardless of permissions
643 * @param $user User object to check for, only if FOR_THIS_USER is passed
644 * to the $audience parameter
647 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
648 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
650 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
653 return $this->getRawText();
658 * Alias for getText(Revision::FOR_THIS_USER)
660 * @deprecated since 1.17
663 public function revText() {
664 wfDeprecated( __METHOD__
);
665 return $this->getText( self
::FOR_THIS_USER
);
669 * Fetch revision text without regard for view restrictions
673 public function getRawText() {
674 if( is_null( $this->mText
) ) {
675 // Revision text is immutable. Load on demand:
676 $this->mText
= $this->loadText();
684 public function getTimestamp() {
685 return wfTimestamp( TS_MW
, $this->mTimestamp
);
691 public function isCurrent() {
692 return $this->mCurrent
;
696 * Get previous revision for this title
698 * @return Revision or null
700 public function getPrevious() {
701 if( $this->getTitle() ) {
702 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
704 return Revision
::newFromTitle( $this->getTitle(), $prev );
711 * Get next revision for this title
713 * @return Revision or null
715 public function getNext() {
716 if( $this->getTitle() ) {
717 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
719 return Revision
::newFromTitle( $this->getTitle(), $next );
726 * Get previous revision Id for this page_id
727 * This is used to populate rev_parent_id on save
729 * @param $db DatabaseBase
732 private function getPreviousRevisionId( $db ) {
733 if( is_null( $this->mPage
) ) {
736 # Use page_latest if ID is not given
738 $prevId = $db->selectField( 'page', 'page_latest',
739 array( 'page_id' => $this->mPage
),
742 $prevId = $db->selectField( 'revision', 'rev_id',
743 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
745 array( 'ORDER BY' => 'rev_id DESC' ) );
747 return intval( $prevId );
751 * Get revision text associated with an old or archive row
752 * $row is usually an object from wfFetchRow(), both the flags and the text
753 * field must be included
755 * @param $row Object: the text data
756 * @param $prefix String: table prefix (default 'old_')
757 * @return String: text the text requested or false on failure
759 public static function getRevisionText( $row, $prefix = 'old_' ) {
760 wfProfileIn( __METHOD__
);
763 $textField = $prefix . 'text';
764 $flagsField = $prefix . 'flags';
766 if( isset( $row->$flagsField ) ) {
767 $flags = explode( ',', $row->$flagsField );
772 if( isset( $row->$textField ) ) {
773 $text = $row->$textField;
775 wfProfileOut( __METHOD__
);
779 # Use external methods for external objects, text in table is URL-only then
780 if ( in_array( 'external', $flags ) ) {
782 $parts = explode( '://', $url, 2 );
783 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
784 wfProfileOut( __METHOD__
);
787 $text = ExternalStore
::fetchFromURL( $url );
790 // If the text was fetched without an error, convert it
791 if ( $text !== false ) {
792 if( in_array( 'gzip', $flags ) ) {
793 # Deal with optional compression of archived pages.
794 # This can be done periodically via maintenance/compressOld.php, and
795 # as pages are saved if $wgCompressRevisions is set.
796 $text = gzinflate( $text );
799 if( in_array( 'object', $flags ) ) {
800 # Generic compressed storage
801 $obj = unserialize( $text );
802 if ( !is_object( $obj ) ) {
804 wfProfileOut( __METHOD__
);
807 $text = $obj->getText();
810 global $wgLegacyEncoding;
811 if( $text !== false && $wgLegacyEncoding
812 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
814 # Old revisions kept around in a legacy encoding?
815 # Upconvert on demand.
816 # ("utf8" checked for compatibility with some broken
817 # conversion scripts 2008-12-30)
819 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
822 wfProfileOut( __METHOD__
);
827 * If $wgCompressRevisions is enabled, we will compress data.
828 * The input string is modified in place.
829 * Return value is the flags field: contains 'gzip' if the
830 * data is compressed, and 'utf-8' if we're saving in UTF-8
833 * @param $text Mixed: reference to a text
836 public static function compressRevisionText( &$text ) {
837 global $wgCompressRevisions;
840 # Revisions not marked this way will be converted
841 # on load if $wgLegacyCharset is set in the future.
844 if( $wgCompressRevisions ) {
845 if( function_exists( 'gzdeflate' ) ) {
846 $text = gzdeflate( $text );
849 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
852 return implode( ',', $flags );
856 * Insert a new revision into the database, returning the new revision ID
857 * number on success and dies horribly on failure.
859 * @param $dbw DatabaseBase: (master connection)
862 public function insertOn( $dbw ) {
863 global $wgDefaultExternalStore;
865 wfProfileIn( __METHOD__
);
867 $data = $this->mText
;
868 $flags = Revision
::compressRevisionText( $data );
870 # Write to external storage if required
871 if( $wgDefaultExternalStore ) {
872 // Store and get the URL
873 $data = ExternalStore
::insertToDefault( $data );
875 throw new MWException( "Unable to store text to external storage" );
880 $flags .= 'external';
883 # Record the text (or external storage URL) to the text table
884 if( !isset( $this->mTextId
) ) {
885 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
886 $dbw->insert( 'text',
890 'old_flags' => $flags,
893 $this->mTextId
= $dbw->insertId();
896 if ( $this->mComment
=== null ) $this->mComment
= "";
898 # Record the edit in revisions
899 $rev_id = isset( $this->mId
)
901 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
902 $dbw->insert( 'revision',
905 'rev_page' => $this->mPage
,
906 'rev_text_id' => $this->mTextId
,
907 'rev_comment' => $this->mComment
,
908 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
909 'rev_user' => $this->mUser
,
910 'rev_user_text' => $this->mUserText
,
911 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
912 'rev_deleted' => $this->mDeleted
,
913 'rev_len' => $this->mSize
,
914 'rev_parent_id' => is_null($this->mParentId
) ?
915 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
919 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
921 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
923 wfProfileOut( __METHOD__
);
928 * Lazy-load the revision's text.
929 * Currently hardcoded to the 'text' table storage engine.
933 protected function loadText() {
934 wfProfileIn( __METHOD__
);
936 // Caching may be beneficial for massive use of external storage
937 global $wgRevisionCacheExpiry, $wgMemc;
938 $textId = $this->getTextId();
939 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
940 if( $wgRevisionCacheExpiry ) {
941 $text = $wgMemc->get( $key );
942 if( is_string( $text ) ) {
943 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
944 wfProfileOut( __METHOD__
);
949 // If we kept data for lazy extraction, use it now...
950 if ( isset( $this->mTextRow
) ) {
951 $row = $this->mTextRow
;
952 $this->mTextRow
= null;
958 // Text data is immutable; check slaves first.
959 $dbr = wfGetDB( DB_SLAVE
);
960 $row = $dbr->selectRow( 'text',
961 array( 'old_text', 'old_flags' ),
962 array( 'old_id' => $this->getTextId() ),
966 if( !$row && wfGetLB()->getServerCount() > 1 ) {
967 // Possible slave lag!
968 $dbw = wfGetDB( DB_MASTER
);
969 $row = $dbw->selectRow( 'text',
970 array( 'old_text', 'old_flags' ),
971 array( 'old_id' => $this->getTextId() ),
975 $text = self
::getRevisionText( $row );
977 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
978 if( $wgRevisionCacheExpiry && $text !== false ) {
979 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
982 wfProfileOut( __METHOD__
);
988 * Create a new null-revision for insertion into a page's
989 * history. This will not re-save the text, but simply refer
990 * to the text from the previous version.
992 * Such revisions can for instance identify page rename
993 * operations and other such meta-modifications.
995 * @param $dbw DatabaseBase
996 * @param $pageId Integer: ID number of the page to read from
997 * @param $summary String: revision's summary
998 * @param $minor Boolean: whether the revision should be considered as minor
999 * @return Revision|null on error
1001 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1002 wfProfileIn( __METHOD__
);
1004 $current = $dbw->selectRow(
1005 array( 'page', 'revision' ),
1006 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1008 'page_id' => $pageId,
1009 'page_latest=rev_id',
1014 $revision = new Revision( array(
1016 'comment' => $summary,
1017 'minor_edit' => $minor,
1018 'text_id' => $current->rev_text_id
,
1019 'parent_id' => $current->page_latest
,
1020 'len' => $current->rev_len
1026 wfProfileOut( __METHOD__
);
1031 * Determine if the current user is allowed to view a particular
1032 * field of this revision, if it's marked as deleted.
1034 * @param $field Integer:one of self::DELETED_TEXT,
1035 * self::DELETED_COMMENT,
1036 * self::DELETED_USER
1037 * @param $user User object to check, or null to use $wgUser
1040 public function userCan( $field, User
$user = null ) {
1041 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1045 * Determine if the current user is allowed to view a particular
1046 * field of this revision, if it's marked as deleted. This is used
1047 * by various classes to avoid duplication.
1049 * @param $bitfield Integer: current field
1050 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1051 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1052 * self::DELETED_USER = File::DELETED_USER
1053 * @param $user User object to check, or null to use $wgUser
1056 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1057 if( $bitfield & $field ) { // aspect is deleted
1058 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1059 $permission = 'suppressrevision';
1060 } elseif ( $field & self
::DELETED_TEXT
) {
1061 $permission = 'deletedtext';
1063 $permission = 'deletedhistory';
1065 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1066 if ( $user === null ) {
1070 return $user->isAllowed( $permission );
1077 * Get rev_timestamp from rev_id, without loading the rest of the row
1079 * @param $title Title
1080 * @param $id Integer
1083 static function getTimestampFromId( $title, $id ) {
1084 $dbr = wfGetDB( DB_SLAVE
);
1085 // Casting fix for DB2
1089 $conds = array( 'rev_id' => $id );
1090 $conds['rev_page'] = $title->getArticleId();
1091 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1092 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1093 # Not in slave, try master
1094 $dbw = wfGetDB( DB_MASTER
);
1095 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1097 return wfTimestamp( TS_MW
, $timestamp );
1101 * Get count of revisions per page...not very efficient
1103 * @param $db DatabaseBase
1104 * @param $id Integer: page id
1107 static function countByPageId( $db, $id ) {
1108 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1109 array( 'rev_page' => $id ), __METHOD__
);
1111 return $row->revCount
;
1117 * Get count of revisions per page...not very efficient
1119 * @param $db DatabaseBase
1120 * @param $title Title
1123 static function countByTitle( $db, $title ) {
1124 $id = $title->getArticleId();
1126 return Revision
::countByPageId( $db, $id );