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(
27 array( 'page_id=rev_page',
28 'rev_id' => intval( $id ) ) );
32 * Load either the current, or a specified, revision
33 * that's attached to a given title. If not attached
34 * to that title, will return null.
37 * @param $id Integer (optional)
38 * @return Revision or null
40 public static function newFromTitle( $title, $id = 0 ) {
42 'page_namespace' => $title->getNamespace(),
43 'page_title' => $title->getDBkey()
46 // Use the specified ID
47 $conds['rev_id'] = $id;
48 } elseif ( wfGetLB()->getServerCount() > 1 ) {
49 // Get the latest revision ID from the master
50 $dbw = wfGetDB( DB_MASTER
);
51 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
52 if ( $latest === false ) {
53 return null; // page does not exist
55 $conds['rev_id'] = $latest;
57 // Use a join to get the latest revision
58 $conds[] = 'rev_id=page_latest';
60 $conds[] = 'page_id=rev_page';
61 return Revision
::newFromConds( $conds );
65 * Load either the current, or a specified, revision
66 * that's attached to a given page ID.
67 * Returns null if no such revision can be found.
69 * @param $revId Integer
70 * @param $pageId Integer (optional)
71 * @return Revision or null
73 public static function newFromPageId( $pageId, $revId = 0 ) {
74 $conds = array( 'page_id' => $pageId );
76 $conds['rev_id'] = $revId;
77 } elseif ( wfGetLB()->getServerCount() > 1 ) {
78 // Get the latest revision ID from the master
79 $dbw = wfGetDB( DB_MASTER
);
80 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
81 if ( $latest === false ) {
82 return null; // page does not exist
84 $conds['rev_id'] = $latest;
86 $conds[] = 'rev_id = page_latest';
88 $conds[] = 'page_id=rev_page';
89 return Revision
::newFromConds( $conds );
93 * Make a fake revision object from an archive table row. This is queried
94 * for permissions or even inserted (as in Special:Undelete)
95 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
98 * @param $overrides array
102 public static function newFromArchiveRow( $row, $overrides = array() ) {
103 $attribs = $overrides +
array(
104 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
105 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
106 'comment' => $row->ar_comment
,
107 'user' => $row->ar_user
,
108 'user_text' => $row->ar_user_text
,
109 'timestamp' => $row->ar_timestamp
,
110 'minor_edit' => $row->ar_minor_edit
,
111 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
112 'deleted' => $row->ar_deleted
,
113 'len' => $row->ar_len
,
114 'sha1' => $row->ar_sha1
116 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
117 // Pre-1.5 ar_text row
118 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
119 if ( $attribs['text'] === false ) {
120 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
123 return new self( $attribs );
132 public static function newFromRow( $row ) {
133 return new self( $row );
137 * Load a page revision from a given revision ID number.
138 * Returns null if no such revision can be found.
140 * @param $db DatabaseBase
142 * @return Revision or null
144 public static function loadFromId( $db, $id ) {
145 return Revision
::loadFromConds( $db,
146 array( 'page_id=rev_page',
147 'rev_id' => intval( $id ) ) );
151 * Load either the current, or a specified, revision
152 * that's attached to a given page. If not attached
153 * to that page, will return null.
155 * @param $db DatabaseBase
156 * @param $pageid Integer
158 * @return Revision or null
160 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
161 $conds = array( 'page_id=rev_page','rev_page' => intval( $pageid ), 'page_id'=>intval( $pageid ) );
163 $conds['rev_id'] = intval( $id );
165 $conds[] = 'rev_id=page_latest';
167 return Revision
::loadFromConds( $db, $conds );
171 * Load either the current, or a specified, revision
172 * that's attached to a given page. If not attached
173 * to that page, will return null.
175 * @param $db DatabaseBase
176 * @param $title Title
178 * @return Revision or null
180 public static function loadFromTitle( $db, $title, $id = 0 ) {
182 $matchId = intval( $id );
184 $matchId = 'page_latest';
186 return Revision
::loadFromConds(
188 array( "rev_id=$matchId",
190 'page_namespace' => $title->getNamespace(),
191 'page_title' => $title->getDBkey() ) );
195 * Load the revision for the given title with the given timestamp.
196 * WARNING: Timestamps may in some circumstances not be unique,
197 * so this isn't the best key to use.
199 * @param $db DatabaseBase
200 * @param $title Title
201 * @param $timestamp String
202 * @return Revision or null
204 public static function loadFromTimestamp( $db, $title, $timestamp ) {
205 return Revision
::loadFromConds(
207 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
209 'page_namespace' => $title->getNamespace(),
210 'page_title' => $title->getDBkey() ) );
214 * Given a set of conditions, fetch a revision.
216 * @param $conditions Array
217 * @return Revision or null
219 public static function newFromConds( $conditions ) {
220 $db = wfGetDB( DB_SLAVE
);
221 $row = Revision
::loadFromConds( $db, $conditions );
222 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
223 $dbw = wfGetDB( DB_MASTER
);
224 $row = Revision
::loadFromConds( $dbw, $conditions );
230 * Given a set of conditions, fetch a revision from
231 * the given database connection.
233 * @param $db DatabaseBase
234 * @param $conditions Array
235 * @return Revision or null
237 private static function loadFromConds( $db, $conditions ) {
238 $res = Revision
::fetchFromConds( $db, $conditions );
240 $row = $res->fetchObject();
243 $ret = new Revision( $row );
252 * Return a wrapper for a series of database rows to
253 * fetch all of a given page's revisions in turn.
254 * Each row can be fed to the constructor to get objects.
256 * @param $title Title
257 * @return ResultWrapper
259 public static function fetchRevision( $title ) {
260 return Revision
::fetchFromConds(
262 array( 'rev_id=page_latest',
263 'page_namespace' => $title->getNamespace(),
264 'page_title' => $title->getDBkey(),
265 'page_id=rev_page' ) );
269 * Given a set of conditions, return a ResultWrapper
270 * which will return matching database rows with the
271 * fields necessary to build Revision objects.
273 * @param $db DatabaseBase
274 * @param $conditions Array
275 * @return ResultWrapper
277 private static function fetchFromConds( $db, $conditions ) {
278 $fields = self
::selectFields();
279 $fields[] = 'page_namespace';
280 $fields[] = 'page_title';
281 $fields[] = 'page_latest';
283 array( 'page', 'revision' ),
287 array( 'LIMIT' => 1 ) );
291 * Return the list of revision fields that should be selected to create
294 public static function selectFields() {
312 * Return the list of text fields that should be selected to read the
315 static function selectTextFields() {
323 * Return the list of page fields that should be selected from page table
325 static function selectPageFields() {
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->mUserText
= $row->rev_user_text
;
346 $this->mUser
= intval( $row->rev_user
);
347 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
348 $this->mTimestamp
= $row->rev_timestamp
;
349 $this->mDeleted
= intval( $row->rev_deleted
);
351 if( !isset( $row->rev_parent_id
) ) {
352 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
354 $this->mParentId
= intval( $row->rev_parent_id
);
357 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
360 $this->mSize
= intval( $row->rev_len
);
363 if ( !isset( $row->rev_sha1
) ) {
366 $this->mSha1
= $row->rev_sha1
;
369 if( isset( $row->page_latest
) ) {
370 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
371 $this->mTitle
= Title
::newFromRow( $row );
373 $this->mCurrent
= false;
374 $this->mTitle
= null;
377 // Lazy extraction...
379 if( isset( $row->old_text
) ) {
380 $this->mTextRow
= $row;
382 // 'text' table row entry will be lazy-loaded
383 $this->mTextRow
= null;
385 } elseif( is_array( $row ) ) {
386 // Build a new revision to be saved...
387 global $wgUser; // ugh
389 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
390 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
391 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
392 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
393 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
394 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
395 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
396 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
397 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
398 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
399 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : 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 length, 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 if ( $this->mSha1
=== null ) {
414 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
417 throw new MWException( 'Revision constructor passed invalid row format.' );
419 $this->mUnpatrolled
= null;
427 public function getId() {
436 public function getTextId() {
437 return $this->mTextId
;
441 * Get parent revision ID (the original previous page revision)
445 public function getParentId() {
446 return $this->mParentId
;
450 * Returns the length of the text in this revision, or null if unknown.
454 public function getSize() {
459 * Returns the base36 sha1 of the text in this revision, or null if unknown.
463 public function getSha1() {
468 * Returns the title of the page associated with this entry.
472 public function getTitle() {
473 if( isset( $this->mTitle
) ) {
474 return $this->mTitle
;
476 $dbr = wfGetDB( DB_SLAVE
);
477 $row = $dbr->selectRow(
478 array( 'page', 'revision' ),
479 array( 'page_namespace', 'page_title' ),
480 array( 'page_id=rev_page',
481 'rev_id' => $this->mId
),
482 'Revision::getTitle' );
484 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
486 return $this->mTitle
;
490 * Set the title of the revision
492 * @param $title Title
494 public function setTitle( $title ) {
495 $this->mTitle
= $title;
503 public function getPage() {
508 * Fetch revision's user id if it's available to the specified audience.
509 * If the specified audience does not have access to it, zero will be
512 * @param $audience Integer: one of:
513 * Revision::FOR_PUBLIC to be displayed to all users
514 * Revision::FOR_THIS_USER to be displayed to $wgUser
515 * Revision::RAW get the ID regardless of permissions
520 public function getUser( $audience = self
::FOR_PUBLIC
) {
521 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
523 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
531 * Fetch revision's user id without regard for the current user's permissions
535 public function getRawUser() {
540 * Fetch revision's username if it's available to the specified audience.
541 * If the specified audience does not have access to the username, an
542 * empty string will be returned.
544 * @param $audience Integer: one of:
545 * Revision::FOR_PUBLIC to be displayed to all users
546 * Revision::FOR_THIS_USER to be displayed to $wgUser
547 * Revision::RAW get the text regardless of permissions
551 public function getUserText( $audience = self
::FOR_PUBLIC
) {
552 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
554 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
557 return $this->mUserText
;
562 * Fetch revision's username without regard for view restrictions
566 public function getRawUserText() {
567 return $this->mUserText
;
571 * Fetch revision comment if it's available to the specified audience.
572 * If the specified audience does not have access to the comment, an
573 * empty string will be returned.
575 * @param $audience Integer: one of:
576 * Revision::FOR_PUBLIC to be displayed to all users
577 * Revision::FOR_THIS_USER to be displayed to $wgUser
578 * Revision::RAW get the text regardless of permissions
582 function getComment( $audience = self
::FOR_PUBLIC
) {
583 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
585 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
) ) {
588 return $this->mComment
;
593 * Fetch revision comment without regard for the current user's permissions
597 public function getRawComment() {
598 return $this->mComment
;
604 public function isMinor() {
605 return (bool)$this->mMinorEdit
;
609 * @return Integer rcid of the unpatrolled row, zero if there isn't one
611 public function isUnpatrolled() {
612 if( $this->mUnpatrolled
!== null ) {
613 return $this->mUnpatrolled
;
615 $dbr = wfGetDB( DB_SLAVE
);
616 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
618 array( // Add redundant user,timestamp condition so we can use the existing index
619 'rc_user_text' => $this->getRawUserText(),
620 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
621 'rc_this_oldid' => $this->getId(),
626 return (int)$this->mUnpatrolled
;
630 * @param $field int one of DELETED_* bitfield constants
634 public function isDeleted( $field ) {
635 return ( $this->mDeleted
& $field ) == $field;
639 * Get the deletion bitfield of the revision
643 public function getVisibility() {
644 return (int)$this->mDeleted
;
648 * Fetch revision text if it's available to the specified audience.
649 * If the specified audience does not have the ability to view this
650 * revision, an empty string will be returned.
652 * @param $audience Integer: one of:
653 * Revision::FOR_PUBLIC to be displayed to all users
654 * Revision::FOR_THIS_USER to be displayed to $wgUser
655 * Revision::RAW get the text regardless of permissions
659 public function getText( $audience = self
::FOR_PUBLIC
) {
660 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
662 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
) ) {
665 return $this->getRawText();
670 * Alias for getText(Revision::FOR_THIS_USER)
672 * @deprecated since 1.17
675 public function revText() {
676 wfDeprecated( __METHOD__
);
677 return $this->getText( self
::FOR_THIS_USER
);
681 * Fetch revision text without regard for view restrictions
685 public function getRawText() {
686 if( is_null( $this->mText
) ) {
687 // Revision text is immutable. Load on demand:
688 $this->mText
= $this->loadText();
696 public function getTimestamp() {
697 return wfTimestamp( TS_MW
, $this->mTimestamp
);
703 public function isCurrent() {
704 return $this->mCurrent
;
708 * Get previous revision for this title
710 * @return Revision or null
712 public function getPrevious() {
713 if( $this->getTitle() ) {
714 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
716 return Revision
::newFromTitle( $this->getTitle(), $prev );
723 * Get next revision for this title
725 * @return Revision or null
727 public function getNext() {
728 if( $this->getTitle() ) {
729 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
731 return Revision
::newFromTitle( $this->getTitle(), $next );
738 * Get previous revision Id for this page_id
739 * This is used to populate rev_parent_id on save
741 * @param $db DatabaseBase
744 private function getPreviousRevisionId( $db ) {
745 if( is_null( $this->mPage
) ) {
748 # Use page_latest if ID is not given
750 $prevId = $db->selectField( 'page', 'page_latest',
751 array( 'page_id' => $this->mPage
),
754 $prevId = $db->selectField( 'revision', 'rev_id',
755 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
757 array( 'ORDER BY' => 'rev_id DESC' ) );
759 return intval( $prevId );
763 * Get revision text associated with an old or archive row
764 * $row is usually an object from wfFetchRow(), both the flags and the text
765 * field must be included
767 * @param $row Object: the text data
768 * @param $prefix String: table prefix (default 'old_')
769 * @return String: text the text requested or false on failure
771 public static function getRevisionText( $row, $prefix = 'old_' ) {
772 wfProfileIn( __METHOD__
);
775 $textField = $prefix . 'text';
776 $flagsField = $prefix . 'flags';
778 if( isset( $row->$flagsField ) ) {
779 $flags = explode( ',', $row->$flagsField );
784 if( isset( $row->$textField ) ) {
785 $text = $row->$textField;
787 wfProfileOut( __METHOD__
);
791 # Use external methods for external objects, text in table is URL-only then
792 if ( in_array( 'external', $flags ) ) {
794 $parts = explode( '://', $url, 2 );
795 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
796 wfProfileOut( __METHOD__
);
799 $text = ExternalStore
::fetchFromURL( $url );
802 // If the text was fetched without an error, convert it
803 if ( $text !== false ) {
804 if( in_array( 'gzip', $flags ) ) {
805 # Deal with optional compression of archived pages.
806 # This can be done periodically via maintenance/compressOld.php, and
807 # as pages are saved if $wgCompressRevisions is set.
808 $text = gzinflate( $text );
811 if( in_array( 'object', $flags ) ) {
812 # Generic compressed storage
813 $obj = unserialize( $text );
814 if ( !is_object( $obj ) ) {
816 wfProfileOut( __METHOD__
);
819 $text = $obj->getText();
822 global $wgLegacyEncoding;
823 if( $text !== false && $wgLegacyEncoding
824 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
826 # Old revisions kept around in a legacy encoding?
827 # Upconvert on demand.
828 # ("utf8" checked for compatibility with some broken
829 # conversion scripts 2008-12-30)
831 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
834 wfProfileOut( __METHOD__
);
839 * If $wgCompressRevisions is enabled, we will compress data.
840 * The input string is modified in place.
841 * Return value is the flags field: contains 'gzip' if the
842 * data is compressed, and 'utf-8' if we're saving in UTF-8
845 * @param $text Mixed: reference to a text
848 public static function compressRevisionText( &$text ) {
849 global $wgCompressRevisions;
852 # Revisions not marked this way will be converted
853 # on load if $wgLegacyCharset is set in the future.
856 if( $wgCompressRevisions ) {
857 if( function_exists( 'gzdeflate' ) ) {
858 $text = gzdeflate( $text );
861 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
864 return implode( ',', $flags );
868 * Insert a new revision into the database, returning the new revision ID
869 * number on success and dies horribly on failure.
871 * @param $dbw DatabaseBase: (master connection)
874 public function insertOn( $dbw ) {
875 global $wgDefaultExternalStore;
877 wfProfileIn( __METHOD__
);
879 $data = $this->mText
;
880 $flags = Revision
::compressRevisionText( $data );
882 # Write to external storage if required
883 if( $wgDefaultExternalStore ) {
884 // Store and get the URL
885 $data = ExternalStore
::insertToDefault( $data );
887 throw new MWException( "Unable to store text to external storage" );
892 $flags .= 'external';
895 # Record the text (or external storage URL) to the text table
896 if( !isset( $this->mTextId
) ) {
897 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
898 $dbw->insert( 'text',
902 'old_flags' => $flags,
905 $this->mTextId
= $dbw->insertId();
908 if ( $this->mComment
=== null ) $this->mComment
= "";
910 # Record the edit in revisions
911 $rev_id = isset( $this->mId
)
913 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
914 $dbw->insert( 'revision',
917 'rev_page' => $this->mPage
,
918 'rev_text_id' => $this->mTextId
,
919 'rev_comment' => $this->mComment
,
920 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
921 'rev_user' => $this->mUser
,
922 'rev_user_text' => $this->mUserText
,
923 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
924 'rev_deleted' => $this->mDeleted
,
925 'rev_len' => $this->mSize
,
926 'rev_parent_id' => is_null( $this->mParentId
)
927 ?
$this->getPreviousRevisionId( $dbw )
929 'rev_sha1' => is_null( $this->mSha1
)
930 ? Revision
::base36Sha1( $this->mText
)
935 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
937 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
939 wfProfileOut( __METHOD__
);
944 * Get the base 36 SHA-1 value for a string of text
945 * @param $text String
948 public static function base36Sha1( $text ) {
949 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
953 * Lazy-load the revision's text.
954 * Currently hardcoded to the 'text' table storage engine.
958 protected function loadText() {
959 wfProfileIn( __METHOD__
);
961 // Caching may be beneficial for massive use of external storage
962 global $wgRevisionCacheExpiry, $wgMemc;
963 $textId = $this->getTextId();
964 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
965 if( $wgRevisionCacheExpiry ) {
966 $text = $wgMemc->get( $key );
967 if( is_string( $text ) ) {
968 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
969 wfProfileOut( __METHOD__
);
974 // If we kept data for lazy extraction, use it now...
975 if ( isset( $this->mTextRow
) ) {
976 $row = $this->mTextRow
;
977 $this->mTextRow
= null;
983 // Text data is immutable; check slaves first.
984 $dbr = wfGetDB( DB_SLAVE
);
985 $row = $dbr->selectRow( 'text',
986 array( 'old_text', 'old_flags' ),
987 array( 'old_id' => $this->getTextId() ),
991 if( !$row && wfGetLB()->getServerCount() > 1 ) {
992 // Possible slave lag!
993 $dbw = wfGetDB( DB_MASTER
);
994 $row = $dbw->selectRow( 'text',
995 array( 'old_text', 'old_flags' ),
996 array( 'old_id' => $this->getTextId() ),
1000 $text = self
::getRevisionText( $row );
1002 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1003 if( $wgRevisionCacheExpiry && $text !== false ) {
1004 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1007 wfProfileOut( __METHOD__
);
1013 * Create a new null-revision for insertion into a page's
1014 * history. This will not re-save the text, but simply refer
1015 * to the text from the previous version.
1017 * Such revisions can for instance identify page rename
1018 * operations and other such meta-modifications.
1020 * @param $dbw DatabaseBase
1021 * @param $pageId Integer: ID number of the page to read from
1022 * @param $summary String: revision's summary
1023 * @param $minor Boolean: whether the revision should be considered as minor
1024 * @return Revision|null on error
1026 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1027 wfProfileIn( __METHOD__
);
1029 $current = $dbw->selectRow(
1030 array( 'page', 'revision' ),
1031 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1033 'page_id' => $pageId,
1034 'page_latest=rev_id',
1039 $revision = new Revision( array(
1041 'comment' => $summary,
1042 'minor_edit' => $minor,
1043 'text_id' => $current->rev_text_id
,
1044 'parent_id' => $current->page_latest
,
1045 'len' => $current->rev_len
1051 wfProfileOut( __METHOD__
);
1056 * Determine if the current user is allowed to view a particular
1057 * field of this revision, if it's marked as deleted.
1059 * @param $field Integer:one of self::DELETED_TEXT,
1060 * self::DELETED_COMMENT,
1061 * self::DELETED_USER
1064 public function userCan( $field ) {
1065 return self
::userCanBitfield( $this->mDeleted
, $field );
1069 * Determine if the current user is allowed to view a particular
1070 * field of this revision, if it's marked as deleted. This is used
1071 * by various classes to avoid duplication.
1073 * @param $bitfield Integer: current field
1074 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1075 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1076 * self::DELETED_USER = File::DELETED_USER
1079 public static function userCanBitfield( $bitfield, $field ) {
1080 if( $bitfield & $field ) { // aspect is deleted
1082 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1083 $permission = 'suppressrevision';
1084 } elseif ( $field & self
::DELETED_TEXT
) {
1085 $permission = 'deletedtext';
1087 $permission = 'deletedhistory';
1089 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1090 return $wgUser->isAllowed( $permission );
1097 * Get rev_timestamp from rev_id, without loading the rest of the row
1099 * @param $title Title
1100 * @param $id Integer
1103 static function getTimestampFromId( $title, $id ) {
1104 $dbr = wfGetDB( DB_SLAVE
);
1105 // Casting fix for DB2
1109 $conds = array( 'rev_id' => $id );
1110 $conds['rev_page'] = $title->getArticleId();
1111 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1112 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1113 # Not in slave, try master
1114 $dbw = wfGetDB( DB_MASTER
);
1115 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1117 return wfTimestamp( TS_MW
, $timestamp );
1121 * Get count of revisions per page...not very efficient
1123 * @param $db DatabaseBase
1124 * @param $id Integer: page id
1127 static function countByPageId( $db, $id ) {
1128 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1129 array( 'rev_page' => $id ), __METHOD__
);
1131 return $row->revCount
;
1137 * Get count of revisions per page...not very efficient
1139 * @param $db DatabaseBase
1140 * @param $title Title
1143 static function countByTitle( $db, $title ) {
1144 $id = $title->getArticleId();
1146 return Revision
::countByPageId( $db, $id );