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 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
115 // Pre-1.5 ar_text row
116 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
117 if ( $attribs['text'] === false ) {
118 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
121 return new self( $attribs );
125 * Load a page revision from a given revision ID number.
126 * Returns null if no such revision can be found.
128 * @param $db DatabaseBase
130 * @return Revision or null
132 public static function loadFromId( $db, $id ) {
133 return Revision
::loadFromConds( $db,
134 array( 'page_id=rev_page',
135 'rev_id' => intval( $id ) ) );
139 * Load either the current, or a specified, revision
140 * that's attached to a given page. If not attached
141 * to that page, will return null.
143 * @param $db DatabaseBase
144 * @param $pageid Integer
146 * @return Revision or null
148 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
149 $conds = array( 'page_id=rev_page','rev_page' => intval( $pageid ), 'page_id'=>intval( $pageid ) );
151 $conds['rev_id'] = intval( $id );
153 $conds[] = 'rev_id=page_latest';
155 return Revision
::loadFromConds( $db, $conds );
159 * Stores the origin wiki of a revision in case it is a foreign wiki
161 function setWikiID( $wikiID ) {
162 $this->mWikiID
= $wikiID;
166 * Load the current revision of a given page of a foreign wiki.
167 * The WikiID is stored for further use, such as loadText() and getTimestampFromId()
169 public static function loadFromTitleForeignWiki( $wikiID, $title ) {
170 $dbr = wfGetDB( DB_SLAVE
, array(), $wikiID );
172 $revision = self
::loadFromTitle( $dbr, $title );
175 $revision->setWikiID( $wikiID );
183 * Load either the current, or a specified, revision
184 * that's attached to a given page. If not attached
185 * to that page, will return null.
187 * @param $db DatabaseBase
188 * @param $title Title
190 * @return Revision or null
192 public static function loadFromTitle( $db, $title, $id = 0 ) {
194 $matchId = intval( $id );
196 $matchId = 'page_latest';
198 return Revision
::loadFromConds(
200 array( "rev_id=$matchId",
202 'page_namespace' => $title->getNamespace(),
203 'page_title' => $title->getDBkey() ) );
207 * Load the revision for the given title with the given timestamp.
208 * WARNING: Timestamps may in some circumstances not be unique,
209 * so this isn't the best key to use.
211 * @param $db DatabaseBase
212 * @param $title Title
213 * @param $timestamp String
214 * @return Revision or null
216 public static function loadFromTimestamp( $db, $title, $timestamp ) {
217 return Revision
::loadFromConds(
219 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
221 'page_namespace' => $title->getNamespace(),
222 'page_title' => $title->getDBkey() ) );
226 * Given a set of conditions, fetch a revision.
228 * @param $conditions Array
229 * @return Revision or null
231 public static function newFromConds( $conditions ) {
232 $db = wfGetDB( DB_SLAVE
);
233 $row = Revision
::loadFromConds( $db, $conditions );
234 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
235 $dbw = wfGetDB( DB_MASTER
);
236 $row = Revision
::loadFromConds( $dbw, $conditions );
242 * Given a set of conditions, fetch a revision from
243 * the given database connection.
245 * @param $db DatabaseBase
246 * @param $conditions Array
247 * @return Revision or null
249 private static function loadFromConds( $db, $conditions ) {
250 $res = Revision
::fetchFromConds( $db, $conditions );
252 $row = $res->fetchObject();
255 $ret = new Revision( $row );
264 * Return a wrapper for a series of database rows to
265 * fetch all of a given page's revisions in turn.
266 * Each row can be fed to the constructor to get objects.
268 * @param $title Title
269 * @return ResultWrapper
271 public static function fetchRevision( $title ) {
272 return Revision
::fetchFromConds(
274 array( 'rev_id=page_latest',
275 'page_namespace' => $title->getNamespace(),
276 'page_title' => $title->getDBkey(),
277 'page_id=rev_page' ) );
281 * Given a set of conditions, return a ResultWrapper
282 * which will return matching database rows with the
283 * fields necessary to build Revision objects.
285 * @param $db DatabaseBase
286 * @param $conditions Array
287 * @return ResultWrapper
289 private static function fetchFromConds( $db, $conditions ) {
290 $fields = self
::selectFields();
291 $fields[] = 'page_namespace';
292 $fields[] = 'page_title';
293 $fields[] = 'page_latest';
295 array( 'page', 'revision' ),
299 array( 'LIMIT' => 1 ) );
303 * Return the list of revision fields that should be selected to create
306 public static function selectFields() {
323 * Return the list of text fields that should be selected to read the
326 static function selectTextFields() {
334 * Return the list of page fields that should be selected from page table
336 static function selectPageFields() {
347 * @param $row Mixed: either a database row or an array
350 function __construct( $row ) {
351 if( is_object( $row ) ) {
352 $this->mId
= intval( $row->rev_id
);
353 $this->mPage
= intval( $row->rev_page
);
354 $this->mTextId
= intval( $row->rev_text_id
);
355 $this->mComment
= $row->rev_comment
;
356 $this->mUserText
= $row->rev_user_text
;
357 $this->mUser
= intval( $row->rev_user
);
358 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
359 $this->mTimestamp
= $row->rev_timestamp
;
360 $this->mDeleted
= intval( $row->rev_deleted
);
362 if( !isset( $row->rev_parent_id
) )
363 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
365 $this->mParentId
= intval( $row->rev_parent_id
);
367 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
370 $this->mSize
= intval( $row->rev_len
);
372 if( isset( $row->page_latest
) ) {
373 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
374 $this->mTitle
= Title
::newFromRow( $row );
376 $this->mCurrent
= false;
377 $this->mTitle
= null;
380 // Lazy extraction...
382 if( isset( $row->old_text
) ) {
383 $this->mTextRow
= $row;
385 // 'text' table row entry will be lazy-loaded
386 $this->mTextRow
= null;
388 } elseif( is_array( $row ) ) {
389 // Build a new revision to be saved...
392 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
393 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
394 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
395 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
396 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
397 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
398 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
399 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
400 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
401 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
403 // Enforce spacing trimming on supplied text
404 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
405 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
406 $this->mTextRow
= null;
408 $this->mTitle
= null; # Load on demand if needed
409 $this->mCurrent
= false;
410 # If we still have no len_size, see it we have the text to figure it out
412 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
414 throw new MWException( 'Revision constructor passed invalid row format.' );
416 $this->mUnpatrolled
= null;
417 $this->mWikiID
= false;
425 public function getId() {
434 public function getTextId() {
435 return $this->mTextId
;
439 * Get parent revision ID (the original previous page revision)
443 public function getParentId() {
444 return $this->mParentId
;
448 * Returns the length of the text in this revision, or null if unknown.
452 public function getSize() {
457 * Returns the title of the page associated with this entry.
461 public function getTitle() {
462 if( isset( $this->mTitle
) ) {
463 return $this->mTitle
;
465 $dbr = wfGetDB( DB_SLAVE
, array(), $this->mWikiID
);
467 $row = $dbr->selectRow(
468 array( 'page', 'revision' ),
469 array( 'page_namespace', 'page_title' ),
470 array( 'page_id=rev_page',
471 'rev_id' => $this->mId
),
472 'Revision::getTitle' );
474 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
477 return $this->mTitle
;
481 * Set the title of the revision
483 * @param $title Title
485 public function setTitle( $title ) {
486 $this->mTitle
= $title;
494 public function getPage() {
499 * Fetch revision's user id if it's available to the specified audience.
500 * If the specified audience does not have access to it, zero will be
503 * @param $audience Integer: one of:
504 * Revision::FOR_PUBLIC to be displayed to all users
505 * Revision::FOR_THIS_USER to be displayed to $wgUser
506 * Revision::RAW get the ID regardless of permissions
511 public function getUser( $audience = self
::FOR_PUBLIC
) {
512 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
514 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
522 * Fetch revision's user id without regard for the current user's permissions
526 public function getRawUser() {
531 * Fetch revision's username if it's available to the specified audience.
532 * If the specified audience does not have access to the username, an
533 * empty string will be returned.
535 * @param $audience Integer: one of:
536 * Revision::FOR_PUBLIC to be displayed to all users
537 * Revision::FOR_THIS_USER to be displayed to $wgUser
538 * Revision::RAW get the text regardless of permissions
542 public function getUserText( $audience = self
::FOR_PUBLIC
) {
543 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
545 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
548 return $this->mUserText
;
553 * Fetch revision's username without regard for view restrictions
557 public function getRawUserText() {
558 return $this->mUserText
;
562 * Fetch revision comment if it's available to the specified audience.
563 * If the specified audience does not have access to the comment, an
564 * empty string will be returned.
566 * @param $audience Integer: one of:
567 * Revision::FOR_PUBLIC to be displayed to all users
568 * Revision::FOR_THIS_USER to be displayed to $wgUser
569 * Revision::RAW get the text regardless of permissions
573 function getComment( $audience = self
::FOR_PUBLIC
) {
574 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
576 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
) ) {
579 return $this->mComment
;
584 * Fetch revision comment without regard for the current user's permissions
588 public function getRawComment() {
589 return $this->mComment
;
595 public function isMinor() {
596 return (bool)$this->mMinorEdit
;
600 * @return Integer rcid of the unpatrolled row, zero if there isn't one
602 public function isUnpatrolled() {
603 if( $this->mUnpatrolled
!== null ) {
604 return $this->mUnpatrolled
;
606 $dbr = wfGetDB( DB_SLAVE
, array(), $this->mWikiID
);
607 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
609 array( // Add redundant user,timestamp condition so we can use the existing index
610 'rc_user_text' => $this->getRawUserText(),
611 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
612 'rc_this_oldid' => $this->getId(),
617 return (int)$this->mUnpatrolled
;
621 * int $field one of DELETED_* bitfield constants
625 public function isDeleted( $field ) {
626 return ( $this->mDeleted
& $field ) == $field;
630 * 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
648 public function getText( $audience = self
::FOR_PUBLIC
) {
649 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
651 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
) ) {
654 return $this->getRawText();
659 * Alias for getText(Revision::FOR_THIS_USER)
661 * @deprecated since 1.17
664 public function revText() {
665 wfDeprecated( __METHOD__
);
666 return $this->getText( self
::FOR_THIS_USER
);
670 * Fetch revision text without regard for view restrictions
674 public function getRawText() {
675 if( is_null( $this->mText
) ) {
676 // Revision text is immutable. Load on demand:
677 $this->mText
= $this->loadText();
685 public function getTimestamp() {
686 return wfTimestamp( TS_MW
, $this->mTimestamp
);
692 public function isCurrent() {
693 return $this->mCurrent
;
697 * Get previous revision for this title
699 * @return Revision or null
701 public function getPrevious() {
702 if( $this->getTitle() ) {
703 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
705 return Revision
::newFromTitle( $this->getTitle(), $prev );
712 * Get next revision for this title
714 * @return Revision or null
716 public function getNext() {
717 if( $this->getTitle() ) {
718 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
720 return Revision
::newFromTitle( $this->getTitle(), $next );
727 * Get previous revision Id for this page_id
728 * This is used to populate rev_parent_id on save
730 * @param $db DatabaseBase
733 private function getPreviousRevisionId( $db ) {
734 if( is_null( $this->mPage
) ) {
737 # Use page_latest if ID is not given
739 $prevId = $db->selectField( 'page', 'page_latest',
740 array( 'page_id' => $this->mPage
),
743 $prevId = $db->selectField( 'revision', 'rev_id',
744 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
746 array( 'ORDER BY' => 'rev_id DESC' ) );
748 return intval( $prevId );
752 * Get revision text associated with an old or archive row
753 * $row is usually an object from wfFetchRow(), both the flags and the text
754 * field must be included
756 * @param $row Object: the text data
757 * @param $prefix String: table prefix (default 'old_')
758 * @return String: text the text requested or false on failure
760 public static function getRevisionText( $row, $prefix = 'old_' ) {
761 wfProfileIn( __METHOD__
);
764 $textField = $prefix . 'text';
765 $flagsField = $prefix . 'flags';
767 if( isset( $row->$flagsField ) ) {
768 $flags = explode( ',', $row->$flagsField );
773 if( isset( $row->$textField ) ) {
774 $text = $row->$textField;
776 wfProfileOut( __METHOD__
);
780 # Use external methods for external objects, text in table is URL-only then
781 if ( in_array( 'external', $flags ) ) {
783 $parts = explode( '://', $url, 2 );
784 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
785 wfProfileOut( __METHOD__
);
788 $text = ExternalStore
::fetchFromURL( $url );
791 // If the text was fetched without an error, convert it
792 if ( $text !== false ) {
793 if( in_array( 'gzip', $flags ) ) {
794 # Deal with optional compression of archived pages.
795 # This can be done periodically via maintenance/compressOld.php, and
796 # as pages are saved if $wgCompressRevisions is set.
797 $text = gzinflate( $text );
800 if( in_array( 'object', $flags ) ) {
801 # Generic compressed storage
802 $obj = unserialize( $text );
803 if ( !is_object( $obj ) ) {
805 wfProfileOut( __METHOD__
);
808 $text = $obj->getText();
811 global $wgLegacyEncoding;
812 if( $text !== false && $wgLegacyEncoding
813 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
815 # Old revisions kept around in a legacy encoding?
816 # Upconvert on demand.
817 # ("utf8" checked for compatibility with some broken
818 # conversion scripts 2008-12-30)
820 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
823 wfProfileOut( __METHOD__
);
828 * If $wgCompressRevisions is enabled, we will compress data.
829 * The input string is modified in place.
830 * Return value is the flags field: contains 'gzip' if the
831 * data is compressed, and 'utf-8' if we're saving in UTF-8
834 * @param $text Mixed: reference to a text
837 public static function compressRevisionText( &$text ) {
838 global $wgCompressRevisions;
841 # Revisions not marked this way will be converted
842 # on load if $wgLegacyCharset is set in the future.
845 if( $wgCompressRevisions ) {
846 if( function_exists( 'gzdeflate' ) ) {
847 $text = gzdeflate( $text );
850 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
853 return implode( ',', $flags );
857 * Insert a new revision into the database, returning the new revision ID
858 * number on success and dies horribly on failure.
860 * @param $dbw DatabaseBase: (master connection)
863 public function insertOn( $dbw ) {
864 global $wgDefaultExternalStore;
866 wfProfileIn( __METHOD__
);
868 $data = $this->mText
;
869 $flags = Revision
::compressRevisionText( $data );
871 # Write to external storage if required
872 if( $wgDefaultExternalStore ) {
873 // Store and get the URL
874 $data = ExternalStore
::insertToDefault( $data );
876 throw new MWException( "Unable to store text to external storage" );
881 $flags .= 'external';
884 # Record the text (or external storage URL) to the text table
885 if( !isset( $this->mTextId
) ) {
886 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
887 $dbw->insert( 'text',
891 'old_flags' => $flags,
894 $this->mTextId
= $dbw->insertId();
897 if ( $this->mComment
=== null ) $this->mComment
= "";
899 # Record the edit in revisions
900 $rev_id = isset( $this->mId
)
902 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
903 $dbw->insert( 'revision',
906 'rev_page' => $this->mPage
,
907 'rev_text_id' => $this->mTextId
,
908 'rev_comment' => $this->mComment
,
909 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
910 'rev_user' => $this->mUser
,
911 'rev_user_text' => $this->mUserText
,
912 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
913 'rev_deleted' => $this->mDeleted
,
914 'rev_len' => $this->mSize
,
915 'rev_parent_id' => is_null($this->mParentId
) ?
916 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
920 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
922 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
924 wfProfileOut( __METHOD__
);
929 * Lazy-load the revision's text.
930 * Currently hardcoded to the 'text' table storage engine.
934 protected function loadText() {
935 wfProfileIn( __METHOD__
);
937 // Caching may be beneficial for massive use of external storage
938 global $wgRevisionCacheExpiry, $wgMemc;
939 $textId = $this->getTextId();
940 if( isset( $this->mWikiID
) && $this->mWikiID
!== false ) {
941 $key = wfForeignMemcKey( $this->mWikiID
, null, 'revisiontext', 'textid', $textId );
943 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
945 if( $wgRevisionCacheExpiry ) {
946 $text = $wgMemc->get( $key );
947 if( is_string( $text ) ) {
948 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
949 wfProfileOut( __METHOD__
);
954 // If we kept data for lazy extraction, use it now...
955 if ( isset( $this->mTextRow
) ) {
956 $row = $this->mTextRow
;
957 $this->mTextRow
= null;
963 // Text data is immutable; check slaves first.
964 $dbr = wfGetDB( DB_SLAVE
, array(), $this->mWikiID
);
965 $row = $dbr->selectRow( 'text',
966 array( 'old_text', 'old_flags' ),
967 array( 'old_id' => $this->getTextId() ),
971 if( !$row && wfGetLB()->getServerCount() > 1 ) {
972 // Possible slave lag!
973 $dbw = wfGetDB( DB_MASTER
, array(), $this->mWikiID
);
974 $row = $dbw->selectRow( 'text',
975 array( 'old_text', 'old_flags' ),
976 array( 'old_id' => $this->getTextId() ),
980 $text = self
::getRevisionText( $row );
982 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
983 if( $wgRevisionCacheExpiry && $text !== false ) {
984 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
987 wfProfileOut( __METHOD__
);
993 * Create a new null-revision for insertion into a page's
994 * history. This will not re-save the text, but simply refer
995 * to the text from the previous version.
997 * Such revisions can for instance identify page rename
998 * operations and other such meta-modifications.
1000 * @param $dbw DatabaseBase
1001 * @param $pageId Integer: ID number of the page to read from
1002 * @param $summary String: revision's summary
1003 * @param $minor Boolean: whether the revision should be considered as minor
1004 * @return Revision|null on error
1006 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1007 wfProfileIn( __METHOD__
);
1009 $current = $dbw->selectRow(
1010 array( 'page', 'revision' ),
1011 array( 'page_latest', 'rev_text_id', 'rev_len' ),
1013 'page_id' => $pageId,
1014 'page_latest=rev_id',
1019 $revision = new Revision( array(
1021 'comment' => $summary,
1022 'minor_edit' => $minor,
1023 'text_id' => $current->rev_text_id
,
1024 'parent_id' => $current->page_latest
,
1025 'len' => $current->rev_len
1031 wfProfileOut( __METHOD__
);
1036 * Determine if the current user is allowed to view a particular
1037 * field of this revision, if it's marked as deleted.
1039 * @param $field Integer:one of self::DELETED_TEXT,
1040 * self::DELETED_COMMENT,
1041 * self::DELETED_USER
1044 public function userCan( $field ) {
1045 return self
::userCanBitfield( $this->mDeleted
, $field );
1049 * Determine if the current user is allowed to view a particular
1050 * field of this revision, if it's marked as deleted. This is used
1051 * by various classes to avoid duplication.
1053 * @param $bitfield Integer: current field
1054 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1055 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1056 * self::DELETED_USER = File::DELETED_USER
1059 public static function userCanBitfield( $bitfield, $field ) {
1060 if( $bitfield & $field ) { // aspect is deleted
1062 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1063 $permission = 'suppressrevision';
1064 } elseif ( $field & self
::DELETED_TEXT
) {
1065 $permission = 'deletedtext';
1067 $permission = 'deletedhistory';
1069 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1070 return $wgUser->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 $wikiId = wfWikiID();
1085 $dbr = wfGetDB( DB_SLAVE
, array(), $wikiId );
1086 // Casting fix for DB2
1090 $conds = array( 'rev_id' => $id );
1091 $conds['rev_page'] = $title->getArticleId();
1092 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1093 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1094 # Not in slave, try master
1095 $dbw = wfGetDB( DB_MASTER
, array(), $wikiId );
1096 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1098 return wfTimestamp( TS_MW
, $timestamp );
1102 * Get count of revisions per page...not very efficient
1104 * @param $db DatabaseBase
1105 * @param $id Integer: page id
1108 static function countByPageId( $db, $id ) {
1109 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1110 array( 'rev_page' => $id ), __METHOD__
);
1112 return $row->revCount
;
1118 * Get count of revisions per page...not very efficient
1120 * @param $db DatabaseBase
1121 * @param $title Title
1124 static function countByTitle( $db, $title ) {
1125 $id = $title->getArticleId();
1127 return Revision
::countByPageId( $db, $id );
1134 * Aliases for backwards compatibility with 1.6
1136 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
1137 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
1138 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
1139 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);