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', 'rev_user != 0 AND 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 public static function selectTextFields() {
316 * Return the list of page fields that should be selected from page table
318 public static function selectPageFields() {
327 * Return the list of user fields that should be selected from user table
329 public static function selectUserFields() {
330 return array( '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 // Use user_name for users and rev_user_text for IPs.
380 // Also fallback to rev_user_text if user_name not given.
381 if ( isset( $row->user_name
) ) {
382 $this->mUserText
= $row->user_name
; // logged-in user (ideally)
384 $this->mUserText
= $row->rev_user_text
; // IP user (ideally)
386 } elseif( is_array( $row ) ) {
387 // Build a new revision to be saved...
390 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
391 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
392 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
393 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
394 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
395 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
396 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
397 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
398 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
399 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
401 // Enforce spacing trimming on supplied text
402 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
403 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
404 $this->mTextRow
= null;
406 $this->mTitle
= null; # Load on demand if needed
407 $this->mCurrent
= false;
408 # If we still have no len_size, see it we have the text to figure it out
409 if ( !$this->mSize
) {
410 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
413 throw new MWException( 'Revision constructor passed invalid row format.' );
415 $this->mUnpatrolled
= null;
423 public function getId() {
432 public function getTextId() {
433 return $this->mTextId
;
437 * Get parent revision ID (the original previous page revision)
441 public function getParentId() {
442 return $this->mParentId
;
446 * Returns the length of the text in this revision, or null if unknown.
450 public function getSize() {
455 * Returns the title of the page associated with this entry.
459 public function getTitle() {
460 if( isset( $this->mTitle
) ) {
461 return $this->mTitle
;
463 $dbr = wfGetDB( DB_SLAVE
);
464 $row = $dbr->selectRow(
465 array( 'page', 'revision' ),
466 array( 'page_namespace', 'page_title' ),
467 array( 'page_id=rev_page',
468 'rev_id' => $this->mId
),
469 'Revision::getTitle' );
471 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
473 return $this->mTitle
;
477 * Set the title of the revision
479 * @param $title Title
481 public function setTitle( $title ) {
482 $this->mTitle
= $title;
490 public function getPage() {
495 * Fetch revision's user id if it's available to the specified audience.
496 * If the specified audience does not have access to it, zero will be
499 * @param $audience Integer: one of:
500 * Revision::FOR_PUBLIC to be displayed to all users
501 * Revision::FOR_THIS_USER to be displayed to $wgUser
502 * Revision::RAW get the ID regardless of permissions
503 * @param $user User object to check for, only if FOR_THIS_USER is passed
504 * to the $audience parameter
507 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
508 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
510 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
518 * Fetch revision's user id without regard for the current user's permissions
522 public function getRawUser() {
527 * Fetch revision's username if it's available to the specified audience.
528 * If the specified audience does not have access to the username, an
529 * empty string will be returned.
531 * @param $audience Integer: one of:
532 * Revision::FOR_PUBLIC to be displayed to all users
533 * Revision::FOR_THIS_USER to be displayed to $wgUser
534 * Revision::RAW get the text regardless of permissions
535 * @param $user User object to check for, only if FOR_THIS_USER is passed
536 * to the $audience parameter
539 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
540 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
542 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
545 return $this->mUserText
;
550 * Fetch revision's username without regard for view restrictions
554 public function getRawUserText() {
555 return $this->mUserText
;
559 * Fetch revision comment if it's available to the specified audience.
560 * If the specified audience does not have access to the comment, an
561 * empty string will be returned.
563 * @param $audience Integer: one of:
564 * Revision::FOR_PUBLIC to be displayed to all users
565 * Revision::FOR_THIS_USER to be displayed to $wgUser
566 * Revision::RAW get the text regardless of permissions
567 * @param $user User object to check for, only if FOR_THIS_USER is passed
568 * to the $audience parameter
571 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
572 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
574 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
577 return $this->mComment
;
582 * Fetch revision comment without regard for the current user's permissions
586 public function getRawComment() {
587 return $this->mComment
;
593 public function isMinor() {
594 return (bool)$this->mMinorEdit
;
598 * @return Integer rcid of the unpatrolled row, zero if there isn't one
600 public function isUnpatrolled() {
601 if( $this->mUnpatrolled
!== null ) {
602 return $this->mUnpatrolled
;
604 $dbr = wfGetDB( DB_SLAVE
);
605 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
607 array( // Add redundant user,timestamp condition so we can use the existing index
608 'rc_user_text' => $this->getRawUserText(),
609 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
610 'rc_this_oldid' => $this->getId(),
615 return (int)$this->mUnpatrolled
;
619 * @param $field int one of DELETED_* bitfield constants
623 public function isDeleted( $field ) {
624 return ( $this->mDeleted
& $field ) == $field;
628 * Get the deletion bitfield of the revision
632 public function getVisibility() {
633 return (int)$this->mDeleted
;
637 * Fetch revision text if it's available to the specified audience.
638 * If the specified audience does not have the ability to view this
639 * revision, an 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 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
650 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
652 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
655 return $this->getRawText();
660 * Alias for getText(Revision::FOR_THIS_USER)
662 * @deprecated since 1.17
665 public function revText() {
666 wfDeprecated( __METHOD__
);
667 return $this->getText( self
::FOR_THIS_USER
);
671 * Fetch revision text without regard for view restrictions
675 public function getRawText() {
676 if( is_null( $this->mText
) ) {
677 // Revision text is immutable. Load on demand:
678 $this->mText
= $this->loadText();
686 public function getTimestamp() {
687 return wfTimestamp( TS_MW
, $this->mTimestamp
);
693 public function isCurrent() {
694 return $this->mCurrent
;
698 * Get previous revision for this title
700 * @return Revision or null
702 public function getPrevious() {
703 if( $this->getTitle() ) {
704 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
706 return Revision
::newFromTitle( $this->getTitle(), $prev );
713 * Get next revision for this title
715 * @return Revision or null
717 public function getNext() {
718 if( $this->getTitle() ) {
719 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
721 return Revision
::newFromTitle( $this->getTitle(), $next );
728 * Get previous revision Id for this page_id
729 * This is used to populate rev_parent_id on save
731 * @param $db DatabaseBase
734 private function getPreviousRevisionId( $db ) {
735 if( is_null( $this->mPage
) ) {
738 # Use page_latest if ID is not given
740 $prevId = $db->selectField( 'page', 'page_latest',
741 array( 'page_id' => $this->mPage
),
744 $prevId = $db->selectField( 'revision', 'rev_id',
745 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
747 array( 'ORDER BY' => 'rev_id DESC' ) );
749 return intval( $prevId );
753 * Get revision text associated with an old or archive row
754 * $row is usually an object from wfFetchRow(), both the flags and the text
755 * field must be included
757 * @param $row Object: the text data
758 * @param $prefix String: table prefix (default 'old_')
759 * @return String: text the text requested or false on failure
761 public static function getRevisionText( $row, $prefix = 'old_' ) {
762 wfProfileIn( __METHOD__
);
765 $textField = $prefix . 'text';
766 $flagsField = $prefix . 'flags';
768 if( isset( $row->$flagsField ) ) {
769 $flags = explode( ',', $row->$flagsField );
774 if( isset( $row->$textField ) ) {
775 $text = $row->$textField;
777 wfProfileOut( __METHOD__
);
781 # Use external methods for external objects, text in table is URL-only then
782 if ( in_array( 'external', $flags ) ) {
784 $parts = explode( '://', $url, 2 );
785 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
786 wfProfileOut( __METHOD__
);
789 $text = ExternalStore
::fetchFromURL( $url );
792 // If the text was fetched without an error, convert it
793 if ( $text !== false ) {
794 if( in_array( 'gzip', $flags ) ) {
795 # Deal with optional compression of archived pages.
796 # This can be done periodically via maintenance/compressOld.php, and
797 # as pages are saved if $wgCompressRevisions is set.
798 $text = gzinflate( $text );
801 if( in_array( 'object', $flags ) ) {
802 # Generic compressed storage
803 $obj = unserialize( $text );
804 if ( !is_object( $obj ) ) {
806 wfProfileOut( __METHOD__
);
809 $text = $obj->getText();
812 global $wgLegacyEncoding;
813 if( $text !== false && $wgLegacyEncoding
814 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
816 # Old revisions kept around in a legacy encoding?
817 # Upconvert on demand.
818 # ("utf8" checked for compatibility with some broken
819 # conversion scripts 2008-12-30)
821 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
824 wfProfileOut( __METHOD__
);
829 * If $wgCompressRevisions is enabled, we will compress data.
830 * The input string is modified in place.
831 * Return value is the flags field: contains 'gzip' if the
832 * data is compressed, and 'utf-8' if we're saving in UTF-8
835 * @param $text Mixed: reference to a text
838 public static function compressRevisionText( &$text ) {
839 global $wgCompressRevisions;
842 # Revisions not marked this way will be converted
843 # on load if $wgLegacyCharset is set in the future.
846 if( $wgCompressRevisions ) {
847 if( function_exists( 'gzdeflate' ) ) {
848 $text = gzdeflate( $text );
851 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
854 return implode( ',', $flags );
858 * Insert a new revision into the database, returning the new revision ID
859 * number on success and dies horribly on failure.
861 * @param $dbw DatabaseBase: (master connection)
864 public function insertOn( $dbw ) {
865 global $wgDefaultExternalStore;
867 wfProfileIn( __METHOD__
);
869 $data = $this->mText
;
870 $flags = Revision
::compressRevisionText( $data );
872 # Write to external storage if required
873 if( $wgDefaultExternalStore ) {
874 // Store and get the URL
875 $data = ExternalStore
::insertToDefault( $data );
877 throw new MWException( "Unable to store text to external storage" );
882 $flags .= 'external';
885 # Record the text (or external storage URL) to the text table
886 if( !isset( $this->mTextId
) ) {
887 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
888 $dbw->insert( 'text',
892 'old_flags' => $flags,
895 $this->mTextId
= $dbw->insertId();
898 if ( $this->mComment
=== null ) $this->mComment
= "";
900 # Record the edit in revisions
901 $rev_id = isset( $this->mId
)
903 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
904 $dbw->insert( 'revision',
907 'rev_page' => $this->mPage
,
908 'rev_text_id' => $this->mTextId
,
909 'rev_comment' => $this->mComment
,
910 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
911 'rev_user' => $this->mUser
,
912 'rev_user_text' => $this->mUserText
,
913 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
914 'rev_deleted' => $this->mDeleted
,
915 'rev_len' => $this->mSize
,
916 'rev_parent_id' => is_null($this->mParentId
) ?
917 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
921 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
923 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
925 wfProfileOut( __METHOD__
);
930 * Lazy-load the revision's text.
931 * Currently hardcoded to the 'text' table storage engine.
935 protected function loadText() {
936 wfProfileIn( __METHOD__
);
938 // Caching may be beneficial for massive use of external storage
939 global $wgRevisionCacheExpiry, $wgMemc;
940 $textId = $this->getTextId();
941 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
942 if( $wgRevisionCacheExpiry ) {
943 $text = $wgMemc->get( $key );
944 if( is_string( $text ) ) {
945 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
946 wfProfileOut( __METHOD__
);
951 // If we kept data for lazy extraction, use it now...
952 if ( isset( $this->mTextRow
) ) {
953 $row = $this->mTextRow
;
954 $this->mTextRow
= null;
960 // Text data is immutable; check slaves first.
961 $dbr = wfGetDB( DB_SLAVE
);
962 $row = $dbr->selectRow( 'text',
963 array( 'old_text', 'old_flags' ),
964 array( 'old_id' => $this->getTextId() ),
968 if( !$row && wfGetLB()->getServerCount() > 1 ) {
969 // Possible slave lag!
970 $dbw = wfGetDB( DB_MASTER
);
971 $row = $dbw->selectRow( 'text',
972 array( 'old_text', 'old_flags' ),
973 array( 'old_id' => $this->getTextId() ),
977 $text = self
::getRevisionText( $row );
979 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
980 if( $wgRevisionCacheExpiry && $text !== false ) {
981 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
984 wfProfileOut( __METHOD__
);
990 * Create a new null-revision for insertion into a page's
991 * history. This will not re-save the text, but simply refer
992 * to the text from the previous version.
994 * Such revisions can for instance identify page rename
995 * operations and other such meta-modifications.
997 * @param $dbw DatabaseBase
998 * @param $pageId Integer: ID number of the page to read from
999 * @param $summary String: revision's summary
1000 * @param $minor Boolean: whether the revision should be considered as minor
1001 * @return Revision|null on error
1003 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1004 wfProfileIn( __METHOD__
);
1006 $current = $dbw->selectRow(
1007 array( 'page', 'revision' ),
1008 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1010 'page_id' => $pageId,
1011 'page_latest=rev_id',
1016 $revision = new Revision( array(
1018 'comment' => $summary,
1019 'minor_edit' => $minor,
1020 'text_id' => $current->rev_text_id
,
1021 'parent_id' => $current->page_latest
,
1022 'len' => $current->rev_len
1028 wfProfileOut( __METHOD__
);
1033 * Determine if the current user is allowed to view a particular
1034 * field of this revision, if it's marked as deleted.
1036 * @param $field Integer:one of self::DELETED_TEXT,
1037 * self::DELETED_COMMENT,
1038 * self::DELETED_USER
1039 * @param $user User object to check, or null to use $wgUser
1042 public function userCan( $field, User
$user = null ) {
1043 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1047 * Determine if the current user is allowed to view a particular
1048 * field of this revision, if it's marked as deleted. This is used
1049 * by various classes to avoid duplication.
1051 * @param $bitfield Integer: current field
1052 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1053 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1054 * self::DELETED_USER = File::DELETED_USER
1055 * @param $user User object to check, or null to use $wgUser
1058 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1059 if( $bitfield & $field ) { // aspect is deleted
1060 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1061 $permission = 'suppressrevision';
1062 } elseif ( $field & self
::DELETED_TEXT
) {
1063 $permission = 'deletedtext';
1065 $permission = 'deletedhistory';
1067 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1068 if ( $user === null ) {
1072 return $user->isAllowed( $permission );
1079 * Get rev_timestamp from rev_id, without loading the rest of the row
1081 * @param $title Title
1082 * @param $id Integer
1085 static function getTimestampFromId( $title, $id ) {
1086 $dbr = wfGetDB( DB_SLAVE
);
1087 // Casting fix for DB2
1091 $conds = array( 'rev_id' => $id );
1092 $conds['rev_page'] = $title->getArticleId();
1093 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1094 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1095 # Not in slave, try master
1096 $dbw = wfGetDB( DB_MASTER
);
1097 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1099 return wfTimestamp( TS_MW
, $timestamp );
1103 * Get count of revisions per page...not very efficient
1105 * @param $db DatabaseBase
1106 * @param $id Integer: page id
1109 static function countByPageId( $db, $id ) {
1110 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1111 array( 'rev_page' => $id ), __METHOD__
);
1113 return $row->revCount
;
1119 * Get count of revisions per page...not very efficient
1121 * @param $db DatabaseBase
1122 * @param $title Title
1125 static function countByTitle( $db, $title ) {
1126 $id = $title->getArticleId();
1128 return Revision
::countByPageId( $db, $id );