10 protected $mOrigUserText;
12 protected $mMinorEdit;
13 protected $mTimestamp;
23 protected $mContentModelName;
24 protected $mContentFormat;
26 protected $mContentHandler;
28 const DELETED_TEXT
= 1;
29 const DELETED_COMMENT
= 2;
30 const DELETED_USER
= 4;
31 const DELETED_RESTRICTED
= 8;
33 const SUPPRESSED_USER
= 12;
34 // Audience options for Revision::getText()
36 const FOR_THIS_USER
= 2;
40 * Load a page revision from a given revision ID number.
41 * Returns null if no such revision can be found.
44 * @return Revision or null
46 public static function newFromId( $id ) {
47 return Revision
::newFromConds( array( 'rev_id' => intval( $id ) ) );
51 * Load either the current, or a specified, revision
52 * that's attached to a given title. If not attached
53 * to that title, will return null.
56 * @param $id Integer (optional)
57 * @return Revision or null
59 public static function newFromTitle( $title, $id = 0 ) {
61 'page_namespace' => $title->getNamespace(),
62 'page_title' => $title->getDBkey()
65 // Use the specified ID
66 $conds['rev_id'] = $id;
67 } elseif ( wfGetLB()->getServerCount() > 1 ) {
68 // Get the latest revision ID from the master
69 $dbw = wfGetDB( DB_MASTER
);
70 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
71 if ( $latest === false ) {
72 return null; // page does not exist
74 $conds['rev_id'] = $latest;
76 // Use a join to get the latest revision
77 $conds[] = 'rev_id=page_latest';
79 return Revision
::newFromConds( $conds );
83 * Load either the current, or a specified, revision
84 * that's attached to a given page ID.
85 * Returns null if no such revision can be found.
87 * @param $revId Integer
88 * @param $pageId Integer (optional)
89 * @return Revision or null
91 public static function newFromPageId( $pageId, $revId = 0 ) {
92 $conds = array( 'page_id' => $pageId );
94 $conds['rev_id'] = $revId;
95 } elseif ( wfGetLB()->getServerCount() > 1 ) {
96 // Get the latest revision ID from the master
97 $dbw = wfGetDB( DB_MASTER
);
98 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
99 if ( $latest === false ) {
100 return null; // page does not exist
102 $conds['rev_id'] = $latest;
104 $conds[] = 'rev_id = page_latest';
106 return Revision
::newFromConds( $conds );
110 * Make a fake revision object from an archive table row. This is queried
111 * for permissions or even inserted (as in Special:Undelete)
112 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
115 * @param $overrides array
119 public static function newFromArchiveRow( $row, $overrides = array() ) {
120 $attribs = $overrides +
array(
121 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
122 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
123 'comment' => $row->ar_comment
,
124 'user' => $row->ar_user
,
125 'user_text' => $row->ar_user_text
,
126 'timestamp' => $row->ar_timestamp
,
127 'minor_edit' => $row->ar_minor_edit
,
128 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
129 'deleted' => $row->ar_deleted
,
130 'len' => $row->ar_len
,
131 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
132 'content_model' => isset( $row->ar_content_model
) ?
$row->ar_content_model
: null,
133 'content_format' => isset( $row->ar_content_format
) ?
$row->ar_content_format
: null,
135 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
136 // Pre-1.5 ar_text row
137 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
138 if ( $attribs['text'] === false ) {
139 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
142 return new self( $attribs );
151 public static function newFromRow( $row ) {
152 return new self( $row );
156 * Load a page revision from a given revision ID number.
157 * Returns null if no such revision can be found.
159 * @param $db DatabaseBase
161 * @return Revision or null
163 public static function loadFromId( $db, $id ) {
164 return Revision
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
168 * Load either the current, or a specified, revision
169 * that's attached to a given page. If not attached
170 * to that page, will return null.
172 * @param $db DatabaseBase
173 * @param $pageid Integer
175 * @return Revision or null
177 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
178 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
180 $conds['rev_id'] = intval( $id );
182 $conds[] = 'rev_id=page_latest';
184 return Revision
::loadFromConds( $db, $conds );
188 * Load either the current, or a specified, revision
189 * that's attached to a given page. If not attached
190 * to that page, will return null.
192 * @param $db DatabaseBase
193 * @param $title Title
195 * @return Revision or null
197 public static function loadFromTitle( $db, $title, $id = 0 ) {
199 $matchId = intval( $id );
201 $matchId = 'page_latest';
203 return Revision
::loadFromConds( $db,
204 array( "rev_id=$matchId",
205 'page_namespace' => $title->getNamespace(),
206 'page_title' => $title->getDBkey() )
211 * Load the revision for the given title with the given timestamp.
212 * WARNING: Timestamps may in some circumstances not be unique,
213 * so this isn't the best key to use.
215 * @param $db DatabaseBase
216 * @param $title Title
217 * @param $timestamp String
218 * @return Revision or null
220 public static function loadFromTimestamp( $db, $title, $timestamp ) {
221 return Revision
::loadFromConds( $db,
222 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
223 'page_namespace' => $title->getNamespace(),
224 'page_title' => $title->getDBkey() )
229 * Given a set of conditions, fetch a revision.
231 * @param $conditions Array
232 * @return Revision or null
234 public static function newFromConds( $conditions ) {
235 $db = wfGetDB( DB_SLAVE
);
236 $rev = Revision
::loadFromConds( $db, $conditions );
237 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
238 $dbw = wfGetDB( DB_MASTER
);
239 $rev = Revision
::loadFromConds( $dbw, $conditions );
245 * Given a set of conditions, fetch a revision from
246 * the given database connection.
248 * @param $db DatabaseBase
249 * @param $conditions Array
250 * @return Revision or null
252 private static function loadFromConds( $db, $conditions ) {
253 $res = Revision
::fetchFromConds( $db, $conditions );
255 $row = $res->fetchObject();
257 $ret = new Revision( $row );
266 * Return a wrapper for a series of database rows to
267 * fetch all of a given page's revisions in turn.
268 * Each row can be fed to the constructor to get objects.
270 * @param $title Title
271 * @return ResultWrapper
273 public static function fetchRevision( $title ) {
274 return Revision
::fetchFromConds(
276 array( 'rev_id=page_latest',
277 'page_namespace' => $title->getNamespace(),
278 'page_title' => $title->getDBkey() )
283 * Given a set of conditions, return a ResultWrapper
284 * which will return matching database rows with the
285 * fields necessary to build Revision objects.
287 * @param $db DatabaseBase
288 * @param $conditions Array
289 * @return ResultWrapper
291 private static function fetchFromConds( $db, $conditions ) {
292 $fields = array_merge(
293 self
::selectFields(),
294 self
::selectPageFields(),
295 self
::selectUserFields()
298 array( 'revision', 'page', 'user' ),
302 array( 'LIMIT' => 1 ),
303 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
308 * Return the value of a select() JOIN conds array for the user table.
309 * This will get user table rows for logged-in users.
313 public static function userJoinCond() {
314 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
318 * Return the value of a select() page conds array for the paeg table.
319 * This will assure that the revision(s) are not orphaned from live pages.
323 public static function pageJoinCond() {
324 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
328 * Return the list of revision fields that should be selected to create
332 public static function selectFields() {
346 'rev_content_format',
352 * Return the list of text fields that should be selected to read the
356 public static function selectTextFields() {
364 * Return the list of page fields that should be selected from page table
367 public static function selectPageFields() {
377 * Return the list of user fields that should be selected from user table
380 public static function selectUserFields() {
381 return array( 'user_name' );
387 * @param $row Mixed: either a database row or an array
390 function __construct( $row ) {
391 if( is_object( $row ) ) {
392 $this->mId
= intval( $row->rev_id
);
393 $this->mPage
= intval( $row->rev_page
);
394 $this->mTextId
= intval( $row->rev_text_id
);
395 $this->mComment
= $row->rev_comment
;
396 $this->mUser
= intval( $row->rev_user
);
397 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
398 $this->mTimestamp
= $row->rev_timestamp
;
399 $this->mDeleted
= intval( $row->rev_deleted
);
401 if( !isset( $row->rev_parent_id
) ) {
402 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
404 $this->mParentId
= intval( $row->rev_parent_id
);
407 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
410 $this->mSize
= intval( $row->rev_len
);
413 if ( !isset( $row->rev_sha1
) ) {
416 $this->mSha1
= $row->rev_sha1
;
419 if( isset( $row->page_latest
) ) {
420 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
421 $this->mTitle
= Title
::newFromRow( $row );
423 $this->mCurrent
= false;
424 $this->mTitle
= null;
427 if( !isset( $row->rev_content_model
) ||
is_null( $row->rev_content_model
) ) {
428 $this->mContentModelName
= null; # determine on demand if needed
430 $this->mContentModelName
= strval( $row->rev_content_model
);
433 if( !isset( $row->rev_content_format
) ||
is_null( $row->rev_content_format
) ) {
434 $this->mContentFormat
= null; # determine on demand if needed
436 $this->mContentFormat
= strval( $row->rev_content_format
);
439 // Lazy extraction...
441 if( isset( $row->old_text
) ) {
442 $this->mTextRow
= $row;
444 // 'text' table row entry will be lazy-loaded
445 $this->mTextRow
= null;
448 // Use user_name for users and rev_user_text for IPs...
449 $this->mUserText
= null; // lazy load if left null
450 if ( $this->mUser
== 0 ) {
451 $this->mUserText
= $row->rev_user_text
; // IP user
452 } elseif ( isset( $row->user_name
) ) {
453 $this->mUserText
= $row->user_name
; // logged-in user
455 $this->mOrigUserText
= $row->rev_user_text
;
456 } elseif( is_array( $row ) ) {
457 // Build a new revision to be saved...
458 global $wgUser; // ugh
461 # if we have a content object, use it to set the model and type
462 if ( !empty( $row['content'] ) ) {
463 if ( !empty( $row['text_id'] ) ) { #FIXME: when is that set? test with external store setup! check out insertOn()
464 throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
467 $row['content_model'] = $row['content']->getModelName();
468 # note: mContentFormat is initializes later accordingly
469 # note: content is serialized later in this method!
470 # also set text to null?
473 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
474 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
475 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
476 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
477 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
478 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
479 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
480 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
481 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
482 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
483 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
485 $this->mContentModelName
= isset( $row['content_model'] ) ?
strval( $row['content_model'] ) : null;
486 $this->mContentFormat
= isset( $row['content_format'] ) ?
strval( $row['content_format'] ) : null;
488 // Enforce spacing trimming on supplied text
489 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
490 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
491 $this->mTextRow
= null;
493 $this->mTitle
= null; # Load on demand if needed
494 $this->mCurrent
= false;
495 # If we still have no length, see it we have the text to figure it out
496 if ( !$this->mSize
) {
497 #XXX: my be inconsistent with the notion of "size" use for the present content model
498 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
501 if ( $this->mSha1
=== null ) {
502 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
505 $this->getContentModelName(); # force lazy init
506 $this->getContentFormat(); # force lazy init
508 # if we have a content object, serialize it, overriding mText
509 if ( !empty( $row['content'] ) ) {
510 $handler = $this->getContentHandler();
511 $this->mText
= $handler->serialize( $row['content'], $this->getContentFormat() );
514 throw new MWException( 'Revision constructor passed invalid row format.' );
516 $this->mUnpatrolled
= null;
518 #FIXME: add patch for ar_content_format, ar_content_model, rev_content_format, rev_content_model to installer
519 #FIXME: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
527 public function getId() {
532 * Set the revision ID
537 public function setId( $id ) {
546 public function getTextId() {
547 return $this->mTextId
;
551 * Get parent revision ID (the original previous page revision)
553 * @return Integer|null
555 public function getParentId() {
556 return $this->mParentId
;
560 * Returns the length of the text in this revision, or null if unknown.
564 public function getSize() {
569 * Returns the base36 sha1 of the text in this revision, or null if unknown.
573 public function getSha1() {
578 * Returns the title of the page associated with this entry.
582 public function getTitle() {
583 if( isset( $this->mTitle
) ) {
584 return $this->mTitle
;
586 $dbr = wfGetDB( DB_SLAVE
);
587 $row = $dbr->selectRow(
588 array( 'page', 'revision' ),
589 self
::selectPageFields(),
590 array( 'page_id=rev_page',
591 'rev_id' => $this->mId
),
594 $this->mTitle
= Title
::newFromRow( $row );
596 return $this->mTitle
;
600 * Set the title of the revision
602 * @param $title Title
604 public function setTitle( $title ) {
605 $this->mTitle
= $title;
613 public function getPage() {
618 * Fetch revision's user id if it's available to the specified audience.
619 * If the specified audience does not have access to it, zero will be
622 * @param $audience Integer: one of:
623 * Revision::FOR_PUBLIC to be displayed to all users
624 * Revision::FOR_THIS_USER to be displayed to $wgUser
625 * Revision::RAW get the ID regardless of permissions
626 * @param $user User object to check for, only if FOR_THIS_USER is passed
627 * to the $audience parameter
630 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
631 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
633 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
641 * Fetch revision's user id without regard for the current user's permissions
645 public function getRawUser() {
650 * Fetch revision's username if it's available to the specified audience.
651 * If the specified audience does not have access to the username, an
652 * empty string will be returned.
654 * @param $audience Integer: one of:
655 * Revision::FOR_PUBLIC to be displayed to all users
656 * Revision::FOR_THIS_USER to be displayed to $wgUser
657 * Revision::RAW get the text regardless of permissions
658 * @param $user User object to check for, only if FOR_THIS_USER is passed
659 * to the $audience parameter
662 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
663 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
665 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
668 return $this->getRawUserText();
673 * Fetch revision's username without regard for view restrictions
677 public function getRawUserText() {
678 if ( $this->mUserText
=== null ) {
679 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
680 if ( $this->mUserText
=== false ) {
681 # This shouldn't happen, but it can if the wiki was recovered
682 # via importing revs and there is no user table entry yet.
683 $this->mUserText
= $this->mOrigUserText
;
686 return $this->mUserText
;
690 * Fetch revision comment if it's available to the specified audience.
691 * If the specified audience does not have access to the comment, an
692 * empty string will be returned.
694 * @param $audience Integer: one of:
695 * Revision::FOR_PUBLIC to be displayed to all users
696 * Revision::FOR_THIS_USER to be displayed to $wgUser
697 * Revision::RAW get the text regardless of permissions
698 * @param $user User object to check for, only if FOR_THIS_USER is passed
699 * to the $audience parameter
702 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
703 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
705 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
708 return $this->mComment
;
713 * Fetch revision comment without regard for the current user's permissions
717 public function getRawComment() {
718 return $this->mComment
;
724 public function isMinor() {
725 return (bool)$this->mMinorEdit
;
729 * @return Integer rcid of the unpatrolled row, zero if there isn't one
731 public function isUnpatrolled() {
732 if( $this->mUnpatrolled
!== null ) {
733 return $this->mUnpatrolled
;
735 $dbr = wfGetDB( DB_SLAVE
);
736 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
738 array( // Add redundant user,timestamp condition so we can use the existing index
739 'rc_user_text' => $this->getRawUserText(),
740 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
741 'rc_this_oldid' => $this->getId(),
746 return (int)$this->mUnpatrolled
;
750 * @param $field int one of DELETED_* bitfield constants
754 public function isDeleted( $field ) {
755 return ( $this->mDeleted
& $field ) == $field;
759 * Get the deletion bitfield of the revision
763 public function getVisibility() {
764 return (int)$this->mDeleted
;
768 * Fetch revision text if it's available to the specified audience.
769 * If the specified audience does not have the ability to view this
770 * revision, an empty string will be returned.
772 * @param $audience Integer: one of:
773 * Revision::FOR_PUBLIC to be displayed to all users
774 * Revision::FOR_THIS_USER to be displayed to $wgUser
775 * Revision::RAW get the text regardless of permissions
776 * @param $user User object to check for, only if FOR_THIS_USER is passed
777 * to the $audience parameter
779 * @deprectaed in 1.20, use getContent() instead
781 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) { #FIXME: deprecated, replace usage! #FIXME: used a LOT!
782 wfDeprecated( __METHOD__
, '1.20' );
784 $content = $this->getContent();
785 return ContentHandler
::getContentText( $content ); # returns the raw content text, if applicable
789 * Fetch revision content if it's available to the specified audience.
790 * If the specified audience does not have the ability to view this
791 * revision, null will be returned.
793 * @param $audience Integer: one of:
794 * Revision::FOR_PUBLIC to be displayed to all users
795 * Revision::FOR_THIS_USER to be displayed to $wgUser
796 * Revision::RAW get the text regardless of permissions
797 * @param $user User object to check for, only if FOR_THIS_USER is passed
798 * to the $audience parameter
801 public function getContent( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
802 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
804 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
807 return $this->getContentInternal();
812 * Alias for getText(Revision::FOR_THIS_USER)
814 * @deprecated since 1.17
817 public function revText() {
818 wfDeprecated( __METHOD__
, '1.17' );
819 return $this->getText( self
::FOR_THIS_USER
);
823 * Fetch revision text without regard for view restrictions
827 public function getRawText() { #FIXME: deprecated, replace usage!
828 return $this->getText( self
::RAW
);
831 protected function getContentInternal() {
832 if( is_null( $this->mContent
) ) {
833 // Revision is immutable. Load on demand:
835 $handler = $this->getContentHandler();
836 $format = $this->getContentFormat();
837 $title = $this->getTitle();
839 if( is_null( $this->mText
) ) {
840 // Load text on demand:
841 $this->mText
= $this->loadText();
844 $this->mContent
= is_null( $this->mText
) ?
null : $handler->unserialize( $this->mText
, $format );
847 return $this->mContent
;
850 public function getContentModelName() {
851 if ( !$this->mContentModelName
) {
852 $title = $this->getTitle();
853 $this->mContentModelName
= ( $title ?
$title->getContentModelName() : CONTENT_MODEL_WIKITEXT
);
856 return $this->mContentModelName
;
859 public function getContentFormat() {
860 if ( !$this->mContentFormat
) {
861 $handler = $this->getContentHandler();
862 $this->mContentFormat
= $handler->getDefaultFormat();
865 return $this->mContentFormat
;
868 public function getContentHandler() {
869 if ( !$this->mContentHandler
) {
870 $title = $this->getTitle();
872 if ( $title ) $model = $title->getContentModelName();
873 else $model = CONTENT_MODEL_WIKITEXT
;
875 $this->mContentHandler
= ContentHandler
::getForModelName( $model );
877 #XXX: do we need to verify that mContentHandler supports mContentFormat?
878 # otherwise, a fixed content format may cause problems on insert.
881 return $this->mContentHandler
;
887 public function getTimestamp() {
888 return wfTimestamp( TS_MW
, $this->mTimestamp
);
894 public function isCurrent() {
895 return $this->mCurrent
;
899 * Get previous revision for this title
901 * @return Revision or null
903 public function getPrevious() {
904 if( $this->getTitle() ) {
905 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
907 return Revision
::newFromTitle( $this->getTitle(), $prev );
914 * Get next revision for this title
916 * @return Revision or null
918 public function getNext() {
919 if( $this->getTitle() ) {
920 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
922 return Revision
::newFromTitle( $this->getTitle(), $next );
929 * Get previous revision Id for this page_id
930 * This is used to populate rev_parent_id on save
932 * @param $db DatabaseBase
935 private function getPreviousRevisionId( $db ) {
936 if( is_null( $this->mPage
) ) {
939 # Use page_latest if ID is not given
941 $prevId = $db->selectField( 'page', 'page_latest',
942 array( 'page_id' => $this->mPage
),
945 $prevId = $db->selectField( 'revision', 'rev_id',
946 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
948 array( 'ORDER BY' => 'rev_id DESC' ) );
950 return intval( $prevId );
954 * Get revision text associated with an old or archive row
955 * $row is usually an object from wfFetchRow(), both the flags and the text
956 * field must be included
958 * @param $row Object: the text data
959 * @param $prefix String: table prefix (default 'old_')
960 * @return String: text the text requested or false on failure
962 public static function getRevisionText( $row, $prefix = 'old_' ) {
963 wfProfileIn( __METHOD__
);
966 $textField = $prefix . 'text';
967 $flagsField = $prefix . 'flags';
969 if( isset( $row->$flagsField ) ) {
970 $flags = explode( ',', $row->$flagsField );
975 if( isset( $row->$textField ) ) {
976 $text = $row->$textField;
978 wfProfileOut( __METHOD__
);
982 # Use external methods for external objects, text in table is URL-only then
983 if ( in_array( 'external', $flags ) ) {
985 $parts = explode( '://', $url, 2 );
986 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
987 wfProfileOut( __METHOD__
);
990 $text = ExternalStore
::fetchFromURL( $url );
993 // If the text was fetched without an error, convert it
994 if ( $text !== false ) {
995 if( in_array( 'gzip', $flags ) ) {
996 # Deal with optional compression of archived pages.
997 # This can be done periodically via maintenance/compressOld.php, and
998 # as pages are saved if $wgCompressRevisions is set.
999 $text = gzinflate( $text );
1002 if( in_array( 'object', $flags ) ) {
1003 # Generic compressed storage
1004 $obj = unserialize( $text );
1005 if ( !is_object( $obj ) ) {
1007 wfProfileOut( __METHOD__
);
1010 $text = $obj->getText();
1013 global $wgLegacyEncoding;
1014 if( $text !== false && $wgLegacyEncoding
1015 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
1017 # Old revisions kept around in a legacy encoding?
1018 # Upconvert on demand.
1019 # ("utf8" checked for compatibility with some broken
1020 # conversion scripts 2008-12-30)
1022 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1025 wfProfileOut( __METHOD__
);
1030 * If $wgCompressRevisions is enabled, we will compress data.
1031 * The input string is modified in place.
1032 * Return value is the flags field: contains 'gzip' if the
1033 * data is compressed, and 'utf-8' if we're saving in UTF-8
1036 * @param $text Mixed: reference to a text
1039 public static function compressRevisionText( &$text ) {
1040 global $wgCompressRevisions;
1043 # Revisions not marked this way will be converted
1044 # on load if $wgLegacyCharset is set in the future.
1047 if( $wgCompressRevisions ) {
1048 if( function_exists( 'gzdeflate' ) ) {
1049 $text = gzdeflate( $text );
1052 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
1055 return implode( ',', $flags );
1059 * Insert a new revision into the database, returning the new revision ID
1060 * number on success and dies horribly on failure.
1062 * @param $dbw DatabaseBase: (master connection)
1065 public function insertOn( $dbw ) {
1066 global $wgDefaultExternalStore;
1068 wfProfileIn( __METHOD__
);
1070 $data = $this->mText
;
1071 $flags = Revision
::compressRevisionText( $data );
1073 # Write to external storage if required
1074 if( $wgDefaultExternalStore ) {
1075 // Store and get the URL
1076 $data = ExternalStore
::insertToDefault( $data );
1078 throw new MWException( "Unable to store text to external storage" );
1083 $flags .= 'external';
1086 # Record the text (or external storage URL) to the text table
1087 if( !isset( $this->mTextId
) ) {
1088 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1089 $dbw->insert( 'text',
1091 'old_id' => $old_id,
1092 'old_text' => $data,
1093 'old_flags' => $flags,
1096 $this->mTextId
= $dbw->insertId();
1099 if ( $this->mComment
=== null ) $this->mComment
= "";
1101 # Record the edit in revisions
1102 $rev_id = isset( $this->mId
)
1104 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1107 'rev_id' => $rev_id,
1108 'rev_page' => $this->mPage
,
1109 'rev_text_id' => $this->mTextId
,
1110 'rev_comment' => $this->mComment
,
1111 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1112 'rev_user' => $this->mUser
,
1113 'rev_user_text' => $this->mUserText
,
1114 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1115 'rev_deleted' => $this->mDeleted
,
1116 'rev_len' => $this->mSize
,
1117 'rev_parent_id' => is_null( $this->mParentId
)
1118 ?
$this->getPreviousRevisionId( $dbw )
1120 'rev_sha1' => is_null( $this->mSha1
)
1121 ? Revision
::base36Sha1( $this->mText
)
1123 'rev_content_model' => $this->getContentModelName(),
1124 'rev_content_format' => $this->getContentFormat(),
1127 $dbw->insert( 'revision', $row, __METHOD__
);
1129 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1131 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1133 wfProfileOut( __METHOD__
);
1138 * Get the base 36 SHA-1 value for a string of text
1139 * @param $text String
1142 public static function base36Sha1( $text ) {
1143 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1147 * Lazy-load the revision's text.
1148 * Currently hardcoded to the 'text' table storage engine.
1152 protected function loadText() {
1153 wfProfileIn( __METHOD__
);
1155 // Caching may be beneficial for massive use of external storage
1156 global $wgRevisionCacheExpiry, $wgMemc;
1157 $textId = $this->getTextId();
1158 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1159 if( $wgRevisionCacheExpiry ) {
1160 $text = $wgMemc->get( $key );
1161 if( is_string( $text ) ) {
1162 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1163 wfProfileOut( __METHOD__
);
1168 // If we kept data for lazy extraction, use it now...
1169 if ( isset( $this->mTextRow
) ) {
1170 $row = $this->mTextRow
;
1171 $this->mTextRow
= null;
1177 // Text data is immutable; check slaves first.
1178 $dbr = wfGetDB( DB_SLAVE
);
1179 $row = $dbr->selectRow( 'text',
1180 array( 'old_text', 'old_flags' ),
1181 array( 'old_id' => $this->getTextId() ),
1185 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1186 // Possible slave lag!
1187 $dbw = wfGetDB( DB_MASTER
);
1188 $row = $dbw->selectRow( 'text',
1189 array( 'old_text', 'old_flags' ),
1190 array( 'old_id' => $this->getTextId() ),
1194 $text = self
::getRevisionText( $row );
1196 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1197 if( $wgRevisionCacheExpiry && $text !== false ) {
1198 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1201 wfProfileOut( __METHOD__
);
1207 * Create a new null-revision for insertion into a page's
1208 * history. This will not re-save the text, but simply refer
1209 * to the text from the previous version.
1211 * Such revisions can for instance identify page rename
1212 * operations and other such meta-modifications.
1214 * @param $dbw DatabaseBase
1215 * @param $pageId Integer: ID number of the page to read from
1216 * @param $summary String: revision's summary
1217 * @param $minor Boolean: whether the revision should be considered as minor
1218 * @return Revision|null on error
1220 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1221 wfProfileIn( __METHOD__
);
1223 $current = $dbw->selectRow(
1224 array( 'page', 'revision' ),
1225 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1',
1226 'rev_content_model', 'rev_content_format' ),
1228 'page_id' => $pageId,
1229 'page_latest=rev_id',
1234 $revision = new Revision( array(
1236 'comment' => $summary,
1237 'minor_edit' => $minor,
1238 'text_id' => $current->rev_text_id
,
1239 'parent_id' => $current->page_latest
,
1240 'len' => $current->rev_len
,
1241 'sha1' => $current->rev_sha1
,
1242 'content_model' => $current->rev_content_model
,
1243 'content_format' => $current->rev_content_format
1249 wfProfileOut( __METHOD__
);
1254 * Determine if the current user is allowed to view a particular
1255 * field of this revision, if it's marked as deleted.
1257 * @param $field Integer:one of self::DELETED_TEXT,
1258 * self::DELETED_COMMENT,
1259 * self::DELETED_USER
1260 * @param $user User object to check, or null to use $wgUser
1263 public function userCan( $field, User
$user = null ) {
1264 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1268 * Determine if the current user is allowed to view a particular
1269 * field of this revision, if it's marked as deleted. This is used
1270 * by various classes to avoid duplication.
1272 * @param $bitfield Integer: current field
1273 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1274 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1275 * self::DELETED_USER = File::DELETED_USER
1276 * @param $user User object to check, or null to use $wgUser
1279 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1280 if( $bitfield & $field ) { // aspect is deleted
1281 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1282 $permission = 'suppressrevision';
1283 } elseif ( $field & self
::DELETED_TEXT
) {
1284 $permission = 'deletedtext';
1286 $permission = 'deletedhistory';
1288 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1289 if ( $user === null ) {
1293 return $user->isAllowed( $permission );
1300 * Get rev_timestamp from rev_id, without loading the rest of the row
1302 * @param $title Title
1303 * @param $id Integer
1306 static function getTimestampFromId( $title, $id ) {
1307 $dbr = wfGetDB( DB_SLAVE
);
1308 // Casting fix for DB2
1312 $conds = array( 'rev_id' => $id );
1313 $conds['rev_page'] = $title->getArticleID();
1314 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1315 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1316 # Not in slave, try master
1317 $dbw = wfGetDB( DB_MASTER
);
1318 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1320 return wfTimestamp( TS_MW
, $timestamp );
1324 * Get count of revisions per page...not very efficient
1326 * @param $db DatabaseBase
1327 * @param $id Integer: page id
1330 static function countByPageId( $db, $id ) {
1331 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1332 array( 'rev_page' => $id ), __METHOD__
);
1334 return $row->revCount
;
1340 * Get count of revisions per page...not very efficient
1342 * @param $db DatabaseBase
1343 * @param $title Title
1346 static function countByTitle( $db, $title ) {
1347 $id = $title->getArticleID();
1349 return Revision
::countByPageId( $db, $id );