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 len_size, see it we have the text to figure it out
410 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
412 throw new MWException( 'Revision constructor passed invalid row format.' );
414 $this->mUnpatrolled
= null;
422 public function getId() {
431 public function getTextId() {
432 return $this->mTextId
;
436 * Get parent revision ID (the original previous page revision)
440 public function getParentId() {
441 return $this->mParentId
;
445 * Returns the length of the text in this revision, or null if unknown.
449 public function getSize() {
454 * Returns the title of the page associated with this entry.
458 public function getTitle() {
459 if( isset( $this->mTitle
) ) {
460 return $this->mTitle
;
462 $dbr = wfGetDB( DB_SLAVE
);
463 $row = $dbr->selectRow(
464 array( 'page', 'revision' ),
465 array( 'page_namespace', 'page_title' ),
466 array( 'page_id=rev_page',
467 'rev_id' => $this->mId
),
468 'Revision::getTitle' );
470 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
472 return $this->mTitle
;
476 * Set the title of the revision
478 * @param $title Title
480 public function setTitle( $title ) {
481 $this->mTitle
= $title;
489 public function getPage() {
494 * Fetch revision's user id if it's available to the specified audience.
495 * If the specified audience does not have access to it, zero will be
498 * @param $audience Integer: one of:
499 * Revision::FOR_PUBLIC to be displayed to all users
500 * Revision::FOR_THIS_USER to be displayed to $wgUser
501 * Revision::RAW get the ID regardless of permissions
506 public function getUser( $audience = self
::FOR_PUBLIC
) {
507 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
509 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
517 * Fetch revision's user id without regard for the current user's permissions
521 public function getRawUser() {
526 * Fetch revision's username if it's available to the specified audience.
527 * If the specified audience does not have access to the username, an
528 * empty string will be returned.
530 * @param $audience Integer: one of:
531 * Revision::FOR_PUBLIC to be displayed to all users
532 * Revision::FOR_THIS_USER to be displayed to $wgUser
533 * Revision::RAW get the text regardless of permissions
537 public function getUserText( $audience = self
::FOR_PUBLIC
) {
538 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
540 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_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
568 function getComment( $audience = self
::FOR_PUBLIC
) {
569 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
571 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
) ) {
574 return $this->mComment
;
579 * Fetch revision comment without regard for the current user's permissions
583 public function getRawComment() {
584 return $this->mComment
;
590 public function isMinor() {
591 return (bool)$this->mMinorEdit
;
595 * @return Integer rcid of the unpatrolled row, zero if there isn't one
597 public function isUnpatrolled() {
598 if( $this->mUnpatrolled
!== null ) {
599 return $this->mUnpatrolled
;
601 $dbr = wfGetDB( DB_SLAVE
);
602 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
604 array( // Add redundant user,timestamp condition so we can use the existing index
605 'rc_user_text' => $this->getRawUserText(),
606 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
607 'rc_this_oldid' => $this->getId(),
612 return (int)$this->mUnpatrolled
;
616 * @param $field int one of DELETED_* bitfield constants
620 public function isDeleted( $field ) {
621 return ( $this->mDeleted
& $field ) == $field;
625 * Get the deletion bitfield of the revision
629 public function getVisibility() {
630 return (int)$this->mDeleted
;
634 * Fetch revision text if it's available to the specified audience.
635 * If the specified audience does not have the ability to view this
636 * revision, an empty string will be returned.
638 * @param $audience Integer: one of:
639 * Revision::FOR_PUBLIC to be displayed to all users
640 * Revision::FOR_THIS_USER to be displayed to $wgUser
641 * Revision::RAW get the text regardless of permissions
645 public function getText( $audience = self
::FOR_PUBLIC
) {
646 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
648 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
) ) {
651 return $this->getRawText();
656 * Alias for getText(Revision::FOR_THIS_USER)
658 * @deprecated since 1.17
661 public function revText() {
662 wfDeprecated( __METHOD__
);
663 return $this->getText( self
::FOR_THIS_USER
);
667 * Fetch revision text without regard for view restrictions
671 public function getRawText() {
672 if( is_null( $this->mText
) ) {
673 // Revision text is immutable. Load on demand:
674 $this->mText
= $this->loadText();
682 public function getTimestamp() {
683 return wfTimestamp( TS_MW
, $this->mTimestamp
);
689 public function isCurrent() {
690 return $this->mCurrent
;
694 * Get previous revision for this title
696 * @return Revision or null
698 public function getPrevious() {
699 if( $this->getTitle() ) {
700 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
702 return Revision
::newFromTitle( $this->getTitle(), $prev );
709 * Get next revision for this title
711 * @return Revision or null
713 public function getNext() {
714 if( $this->getTitle() ) {
715 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
717 return Revision
::newFromTitle( $this->getTitle(), $next );
724 * Get previous revision Id for this page_id
725 * This is used to populate rev_parent_id on save
727 * @param $db DatabaseBase
730 private function getPreviousRevisionId( $db ) {
731 if( is_null( $this->mPage
) ) {
734 # Use page_latest if ID is not given
736 $prevId = $db->selectField( 'page', 'page_latest',
737 array( 'page_id' => $this->mPage
),
740 $prevId = $db->selectField( 'revision', 'rev_id',
741 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
743 array( 'ORDER BY' => 'rev_id DESC' ) );
745 return intval( $prevId );
749 * Get revision text associated with an old or archive row
750 * $row is usually an object from wfFetchRow(), both the flags and the text
751 * field must be included
753 * @param $row Object: the text data
754 * @param $prefix String: table prefix (default 'old_')
755 * @return String: text the text requested or false on failure
757 public static function getRevisionText( $row, $prefix = 'old_' ) {
758 wfProfileIn( __METHOD__
);
761 $textField = $prefix . 'text';
762 $flagsField = $prefix . 'flags';
764 if( isset( $row->$flagsField ) ) {
765 $flags = explode( ',', $row->$flagsField );
770 if( isset( $row->$textField ) ) {
771 $text = $row->$textField;
773 wfProfileOut( __METHOD__
);
777 # Use external methods for external objects, text in table is URL-only then
778 if ( in_array( 'external', $flags ) ) {
780 $parts = explode( '://', $url, 2 );
781 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
782 wfProfileOut( __METHOD__
);
785 $text = ExternalStore
::fetchFromURL( $url );
788 // If the text was fetched without an error, convert it
789 if ( $text !== false ) {
790 if( in_array( 'gzip', $flags ) ) {
791 # Deal with optional compression of archived pages.
792 # This can be done periodically via maintenance/compressOld.php, and
793 # as pages are saved if $wgCompressRevisions is set.
794 $text = gzinflate( $text );
797 if( in_array( 'object', $flags ) ) {
798 # Generic compressed storage
799 $obj = unserialize( $text );
800 if ( !is_object( $obj ) ) {
802 wfProfileOut( __METHOD__
);
805 $text = $obj->getText();
808 global $wgLegacyEncoding;
809 if( $text !== false && $wgLegacyEncoding
810 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
812 # Old revisions kept around in a legacy encoding?
813 # Upconvert on demand.
814 # ("utf8" checked for compatibility with some broken
815 # conversion scripts 2008-12-30)
817 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
820 wfProfileOut( __METHOD__
);
825 * If $wgCompressRevisions is enabled, we will compress data.
826 * The input string is modified in place.
827 * Return value is the flags field: contains 'gzip' if the
828 * data is compressed, and 'utf-8' if we're saving in UTF-8
831 * @param $text Mixed: reference to a text
834 public static function compressRevisionText( &$text ) {
835 global $wgCompressRevisions;
838 # Revisions not marked this way will be converted
839 # on load if $wgLegacyCharset is set in the future.
842 if( $wgCompressRevisions ) {
843 if( function_exists( 'gzdeflate' ) ) {
844 $text = gzdeflate( $text );
847 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
850 return implode( ',', $flags );
854 * Insert a new revision into the database, returning the new revision ID
855 * number on success and dies horribly on failure.
857 * @param $dbw DatabaseBase: (master connection)
860 public function insertOn( $dbw ) {
861 global $wgDefaultExternalStore;
863 wfProfileIn( __METHOD__
);
865 $data = $this->mText
;
866 $flags = Revision
::compressRevisionText( $data );
868 # Write to external storage if required
869 if( $wgDefaultExternalStore ) {
870 // Store and get the URL
871 $data = ExternalStore
::insertToDefault( $data );
873 throw new MWException( "Unable to store text to external storage" );
878 $flags .= 'external';
881 # Record the text (or external storage URL) to the text table
882 if( !isset( $this->mTextId
) ) {
883 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
884 $dbw->insert( 'text',
888 'old_flags' => $flags,
891 $this->mTextId
= $dbw->insertId();
894 if ( $this->mComment
=== null ) $this->mComment
= "";
896 # Record the edit in revisions
897 $rev_id = isset( $this->mId
)
899 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
900 $dbw->insert( 'revision',
903 'rev_page' => $this->mPage
,
904 'rev_text_id' => $this->mTextId
,
905 'rev_comment' => $this->mComment
,
906 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
907 'rev_user' => $this->mUser
,
908 'rev_user_text' => $this->mUserText
,
909 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
910 'rev_deleted' => $this->mDeleted
,
911 'rev_len' => $this->mSize
,
912 'rev_parent_id' => is_null( $this->mParentId
)
913 ?
$this->getPreviousRevisionId( $dbw )
915 'rev_sha1' => is_null( $this->mSha1
)
916 ? Revision
::base36Sha1( $this->mText
)
921 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
923 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
925 wfProfileOut( __METHOD__
);
930 * Get the base 36 SHA-1 value for a string of text
931 * @param $text String
934 public static function base36Sha1( $text ) {
935 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
939 * Lazy-load the revision's text.
940 * Currently hardcoded to the 'text' table storage engine.
944 protected function loadText() {
945 wfProfileIn( __METHOD__
);
947 // Caching may be beneficial for massive use of external storage
948 global $wgRevisionCacheExpiry, $wgMemc;
949 $textId = $this->getTextId();
950 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
951 if( $wgRevisionCacheExpiry ) {
952 $text = $wgMemc->get( $key );
953 if( is_string( $text ) ) {
954 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
955 wfProfileOut( __METHOD__
);
960 // If we kept data for lazy extraction, use it now...
961 if ( isset( $this->mTextRow
) ) {
962 $row = $this->mTextRow
;
963 $this->mTextRow
= null;
969 // Text data is immutable; check slaves first.
970 $dbr = wfGetDB( DB_SLAVE
);
971 $row = $dbr->selectRow( 'text',
972 array( 'old_text', 'old_flags' ),
973 array( 'old_id' => $this->getTextId() ),
977 if( !$row && wfGetLB()->getServerCount() > 1 ) {
978 // Possible slave lag!
979 $dbw = wfGetDB( DB_MASTER
);
980 $row = $dbw->selectRow( 'text',
981 array( 'old_text', 'old_flags' ),
982 array( 'old_id' => $this->getTextId() ),
986 $text = self
::getRevisionText( $row );
988 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
989 if( $wgRevisionCacheExpiry && $text !== false ) {
990 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
993 wfProfileOut( __METHOD__
);
999 * Create a new null-revision for insertion into a page's
1000 * history. This will not re-save the text, but simply refer
1001 * to the text from the previous version.
1003 * Such revisions can for instance identify page rename
1004 * operations and other such meta-modifications.
1006 * @param $dbw DatabaseBase
1007 * @param $pageId Integer: ID number of the page to read from
1008 * @param $summary String: revision's summary
1009 * @param $minor Boolean: whether the revision should be considered as minor
1010 * @return Revision|null on error
1012 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1013 wfProfileIn( __METHOD__
);
1015 $current = $dbw->selectRow(
1016 array( 'page', 'revision' ),
1017 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1019 'page_id' => $pageId,
1020 'page_latest=rev_id',
1025 $revision = new Revision( array(
1027 'comment' => $summary,
1028 'minor_edit' => $minor,
1029 'text_id' => $current->rev_text_id
,
1030 'parent_id' => $current->page_latest
,
1031 'len' => $current->rev_len
1037 wfProfileOut( __METHOD__
);
1042 * Determine if the current user is allowed to view a particular
1043 * field of this revision, if it's marked as deleted.
1045 * @param $field Integer:one of self::DELETED_TEXT,
1046 * self::DELETED_COMMENT,
1047 * self::DELETED_USER
1050 public function userCan( $field ) {
1051 return self
::userCanBitfield( $this->mDeleted
, $field );
1055 * Determine if the current user is allowed to view a particular
1056 * field of this revision, if it's marked as deleted. This is used
1057 * by various classes to avoid duplication.
1059 * @param $bitfield Integer: current field
1060 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1061 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1062 * self::DELETED_USER = File::DELETED_USER
1065 public static function userCanBitfield( $bitfield, $field ) {
1066 if( $bitfield & $field ) { // aspect is deleted
1068 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1069 $permission = 'suppressrevision';
1070 } elseif ( $field & self
::DELETED_TEXT
) {
1071 $permission = 'deletedtext';
1073 $permission = 'deletedhistory';
1075 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1076 return $wgUser->isAllowed( $permission );
1083 * Get rev_timestamp from rev_id, without loading the rest of the row
1085 * @param $title Title
1086 * @param $id Integer
1089 static function getTimestampFromId( $title, $id ) {
1090 $dbr = wfGetDB( DB_SLAVE
);
1091 // Casting fix for DB2
1095 $conds = array( 'rev_id' => $id );
1096 $conds['rev_page'] = $title->getArticleId();
1097 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1098 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1099 # Not in slave, try master
1100 $dbw = wfGetDB( DB_MASTER
);
1101 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1103 return wfTimestamp( TS_MW
, $timestamp );
1107 * Get count of revisions per page...not very efficient
1109 * @param $db DatabaseBase
1110 * @param $id Integer: page id
1113 static function countByPageId( $db, $id ) {
1114 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1115 array( 'rev_page' => $id ), __METHOD__
);
1117 return $row->revCount
;
1123 * Get count of revisions per page...not very efficient
1125 * @param $db DatabaseBase
1126 * @param $title Title
1129 static function countByTitle( $db, $title ) {
1130 $id = $title->getArticleId();
1132 return Revision
::countByPageId( $db, $id );