ac1c9ef8d230123cd0621e3b95bbea3311fc3660
10 const DELETED_TEXT
= 1;
11 const DELETED_COMMENT
= 2;
12 const DELETED_USER
= 4;
13 const DELETED_RESTRICTED
= 8;
16 * Load a page revision from a given revision ID number.
17 * Returns null if no such revision can be found.
23 public static function newFromId( $id ) {
24 return Revision
::newFromConds(
25 array( 'page_id=rev_page',
26 'rev_id' => intval( $id ) ) );
30 * Load either the current, or a specified, revision
31 * that's attached to a given title. If not attached
32 * to that title, will return null.
40 public static function newFromTitle( &$title, $id = 0 ) {
42 $matchId = intval( $id );
44 $matchId = 'page_latest';
46 return Revision
::newFromConds(
47 array( "rev_id=$matchId",
49 'page_namespace' => $title->getNamespace(),
50 'page_title' => $title->getDBkey() ) );
54 * Load a page revision from a given revision ID number.
55 * Returns null if no such revision can be found.
62 public static function loadFromId( &$db, $id ) {
63 return Revision
::loadFromConds( $db,
64 array( 'page_id=rev_page',
65 'rev_id' => intval( $id ) ) );
69 * Load either the current, or a specified, revision
70 * that's attached to a given page. If not attached
71 * to that page, will return null.
80 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
81 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
83 $conds['rev_id']=intval($id);
85 $conds[]='rev_id=page_latest';
87 return Revision
::loadFromConds( $db, $conds );
91 * Load either the current, or a specified, revision
92 * that's attached to a given page. If not attached
93 * to that page, will return null.
102 public static function loadFromTitle( &$db, $title, $id = 0 ) {
104 $matchId = intval( $id );
106 $matchId = 'page_latest';
108 return Revision
::loadFromConds(
110 array( "rev_id=$matchId",
112 'page_namespace' => $title->getNamespace(),
113 'page_title' => $title->getDBkey() ) );
117 * Load the revision for the given title with the given timestamp.
118 * WARNING: Timestamps may in some circumstances not be unique,
119 * so this isn't the best key to use.
121 * @param Database $db
122 * @param Title $title
123 * @param string $timestamp
128 public static function loadFromTimestamp( &$db, &$title, $timestamp ) {
129 return Revision
::loadFromConds(
131 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
133 'page_namespace' => $title->getNamespace(),
134 'page_title' => $title->getDBkey() ) );
138 * Given a set of conditions, fetch a revision.
140 * @param array $conditions
145 private static function newFromConds( $conditions ) {
146 $db = wfGetDB( DB_SLAVE
);
147 $row = Revision
::loadFromConds( $db, $conditions );
148 if( is_null( $row ) ) {
149 $dbw = wfGetDB( DB_MASTER
);
150 $row = Revision
::loadFromConds( $dbw, $conditions );
156 * Given a set of conditions, fetch a revision from
157 * the given database connection.
159 * @param Database $db
160 * @param array $conditions
165 private static function loadFromConds( $db, $conditions ) {
166 $res = Revision
::fetchFromConds( $db, $conditions );
168 $row = $res->fetchObject();
171 $ret = new Revision( $row );
180 * Return a wrapper for a series of database rows to
181 * fetch all of a given page's revisions in turn.
182 * Each row can be fed to the constructor to get objects.
184 * @param Title $title
185 * @return ResultWrapper
189 public static function fetchAllRevisions( &$title ) {
190 return Revision
::fetchFromConds(
192 array( 'page_namespace' => $title->getNamespace(),
193 'page_title' => $title->getDBkey(),
194 'page_id=rev_page' ) );
198 * Return a wrapper for a series of database rows to
199 * fetch all of a given page's revisions in turn.
200 * Each row can be fed to the constructor to get objects.
202 * @param Title $title
203 * @return ResultWrapper
207 public static function fetchRevision( &$title ) {
208 return Revision
::fetchFromConds(
210 array( 'rev_id=page_latest',
211 'page_namespace' => $title->getNamespace(),
212 'page_title' => $title->getDBkey(),
213 'page_id=rev_page' ) );
217 * Given a set of conditions, return a ResultWrapper
218 * which will return matching database rows with the
219 * fields necessary to build Revision objects.
221 * @param Database $db
222 * @param array $conditions
223 * @return ResultWrapper
227 private static function fetchFromConds( $db, $conditions ) {
228 $fields = self
::selectFields();
229 $fields[] = 'page_namespace';
230 $fields[] = 'page_title';
231 $fields[] = 'page_latest';
233 array( 'page', 'revision' ),
236 'Revision::fetchRow',
237 array( 'LIMIT' => 1 ) );
238 $ret = $db->resultObject( $res );
243 * Return the list of revision fields that should be selected to create
246 static function selectFields() {
266 function Revision( $row ) {
267 if( is_object( $row ) ) {
268 $this->mId
= intval( $row->rev_id
);
269 $this->mPage
= intval( $row->rev_page
);
270 $this->mTextId
= intval( $row->rev_text_id
);
271 $this->mComment
= $row->rev_comment
;
272 $this->mUserText
= $row->rev_user_text
;
273 $this->mUser
= intval( $row->rev_user
);
274 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
275 $this->mTimestamp
= $row->rev_timestamp
;
276 $this->mDeleted
= intval( $row->rev_deleted
);
277 $this->mParentId
= intval( $row->rev_parent_id
);
279 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
282 $this->mSize
= intval( $row->rev_len
);
284 if( isset( $row->page_latest
) ) {
285 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
286 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
289 $this->mCurrent
= false;
290 $this->mTitle
= null;
293 // Lazy extraction...
295 if( isset( $row->old_text
) ) {
296 $this->mTextRow
= $row;
298 // 'text' table row entry will be lazy-loaded
299 $this->mTextRow
= null;
301 } elseif( is_array( $row ) ) {
302 // Build a new revision to be saved...
305 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
306 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
307 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
308 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
309 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
310 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
311 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
312 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
313 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
314 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : 0;
316 // Enforce spacing trimming on supplied text
317 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
318 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
319 $this->mTextRow
= null;
321 $this->mTitle
= null; # Load on demand if needed
322 $this->mCurrent
= false;
323 # If we still have no len_size, see it we have the text to figure it out
325 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
327 throw new MWException( 'Revision constructor passed invalid row format.' );
339 public function getId() {
347 public function getTextId() {
348 return $this->mTextId
;
352 * Get parent revision ID (the original previous page revision)
355 public function getParentId() {
356 return $this->mParentId
;
360 * Returns the length of the text in this revision, or null if unknown.
363 public function getSize() {
368 * Returns the title of the page associated with this entry.
371 public function getTitle() {
372 if( isset( $this->mTitle
) ) {
373 return $this->mTitle
;
375 $dbr = wfGetDB( DB_SLAVE
);
376 $row = $dbr->selectRow(
377 array( 'page', 'revision' ),
378 array( 'page_namespace', 'page_title' ),
379 array( 'page_id=rev_page',
380 'rev_id' => $this->mId
),
381 'Revision::getTitle' );
383 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
386 return $this->mTitle
;
390 * Set the title of the revision
391 * @param Title $title
393 public function setTitle( $title ) {
394 $this->mTitle
= $title;
401 public function getPage() {
406 * Fetch revision's user id if it's available to all users
409 public function getUser() {
410 if( $this->isDeleted( self
::DELETED_USER
) ) {
418 * Fetch revision's user id without regard for the current user's permissions
421 public function getRawUser() {
426 * Fetch revision's username if it's available to all users
429 public function getUserText() {
430 if( $this->isDeleted( self
::DELETED_USER
) ) {
433 return $this->mUserText
;
438 * Fetch revision's username without regard for view restrictions
441 public function getRawUserText() {
442 return $this->mUserText
;
446 * Fetch revision comment if it's available to all users
449 function getComment() {
450 if( $this->isDeleted( self
::DELETED_COMMENT
) ) {
453 return $this->mComment
;
458 * Fetch revision comment without regard for the current user's permissions
461 public function getRawComment() {
462 return $this->mComment
;
468 public function isMinor() {
469 return (bool)$this->mMinorEdit
;
473 * int $field one of DELETED_* bitfield constants
476 public function isDeleted( $field ) {
477 return ($this->mDeleted
& $field) == $field;
481 * Fetch revision text if it's available to all users
484 public function getText() {
485 if( $this->isDeleted( self
::DELETED_TEXT
) ) {
488 return $this->getRawText();
493 * Fetch revision text without regard for view restrictions
496 public function getRawText() {
497 if( is_null( $this->mText
) ) {
498 // Revision text is immutable. Load on demand:
499 $this->mText
= $this->loadText();
505 * Fetch revision text if it's available to THIS user
508 public function revText() {
509 if( !$this->userCan( self
::DELETED_TEXT
) ) {
512 return $this->getRawText();
519 public function getTimestamp() {
520 return wfTimestamp(TS_MW
, $this->mTimestamp
);
526 public function isCurrent() {
527 return $this->mCurrent
;
531 * Get previous revision for this title
534 public function getPrevious() {
535 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
537 return Revision
::newFromTitle( $this->mTitle
, $prev );
546 public function getNext() {
547 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
549 return Revision
::newFromTitle( $this->mTitle
, $next );
556 * Get previous revision Id for this page_id
557 * This is used to populate rev_parent_id on save
558 * @param Database $db
561 private function getPreviousRevisionId( $db ) {
562 if( is_null($this->mPage
) ) {
565 # Use page_latest if ID is not given
567 $revID = $db->selectField( 'page', 'page_latest',
568 array( 'page_id' => $this->mPage
),
576 $prevId = $db->selectField( 'revision', 'rev_id',
577 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $revID ),
579 array( 'ORDER BY' => 'rev_id DESC' ) );
580 # Always return an integer
581 return intval($prevId);
585 * Get revision text associated with an old or archive row
586 * $row is usually an object from wfFetchRow(), both the flags and the text
587 * field must be included
589 * @param integer $row Id of a row
590 * @param string $prefix table prefix (default 'old_')
591 * @return string $text|false the text requested
593 public static function getRevisionText( $row, $prefix = 'old_' ) {
594 wfProfileIn( __METHOD__
);
597 $textField = $prefix . 'text';
598 $flagsField = $prefix . 'flags';
600 if( isset( $row->$flagsField ) ) {
601 $flags = explode( ',', $row->$flagsField );
606 if( isset( $row->$textField ) ) {
607 $text = $row->$textField;
609 wfProfileOut( __METHOD__
);
613 # Use external methods for external objects, text in table is URL-only then
614 if ( in_array( 'external', $flags ) ) {
616 @list
(/* $proto */,$path)=explode('://',$url,2);
618 wfProfileOut( __METHOD__
);
621 $text=ExternalStore
::fetchFromURL($url);
624 // If the text was fetched without an error, convert it
625 if ( $text !== false ) {
626 if( in_array( 'gzip', $flags ) ) {
627 # Deal with optional compression of archived pages.
628 # This can be done periodically via maintenance/compressOld.php, and
629 # as pages are saved if $wgCompressRevisions is set.
630 $text = gzinflate( $text );
633 if( in_array( 'object', $flags ) ) {
634 # Generic compressed storage
635 $obj = unserialize( $text );
636 if ( !is_object( $obj ) ) {
638 wfProfileOut( __METHOD__
);
641 $text = $obj->getText();
644 global $wgLegacyEncoding;
645 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
646 # Old revisions kept around in a legacy encoding?
647 # Upconvert on demand.
648 global $wgInputEncoding, $wgContLang;
649 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
652 wfProfileOut( __METHOD__
);
657 * If $wgCompressRevisions is enabled, we will compress data.
658 * The input string is modified in place.
659 * Return value is the flags field: contains 'gzip' if the
660 * data is compressed, and 'utf-8' if we're saving in UTF-8
663 * @param mixed $text reference to a text
666 public static function compressRevisionText( &$text ) {
667 global $wgCompressRevisions;
670 # Revisions not marked this way will be converted
671 # on load if $wgLegacyCharset is set in the future.
674 if( $wgCompressRevisions ) {
675 if( function_exists( 'gzdeflate' ) ) {
676 $text = gzdeflate( $text );
679 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
682 return implode( ',', $flags );
686 * Insert a new revision into the database, returning the new revision ID
687 * number on success and dies horribly on failure.
689 * @param Database $dbw
692 public function insertOn( &$dbw ) {
693 global $wgDefaultExternalStore;
695 wfProfileIn( __METHOD__
);
697 $data = $this->mText
;
698 $flags = Revision
::compressRevisionText( $data );
700 # Write to external storage if required
701 if ( $wgDefaultExternalStore ) {
702 if ( is_array( $wgDefaultExternalStore ) ) {
703 // Distribute storage across multiple clusters
704 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
706 $store = $wgDefaultExternalStore;
708 // Store and get the URL
709 $data = ExternalStore
::insert( $store, $data );
711 # This should only happen in the case of a configuration error, where the external store is not valid
712 throw new MWException( "Unable to store text to external storage $store" );
717 $flags .= 'external';
720 # Record the text (or external storage URL) to the text table
721 if( !isset( $this->mTextId
) ) {
722 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
723 $dbw->insert( 'text',
727 'old_flags' => $flags,
730 $this->mTextId
= $dbw->insertId();
733 # Record the edit in revisions
734 $rev_id = isset( $this->mId
)
736 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
737 $dbw->insert( 'revision',
740 'rev_page' => $this->mPage
,
741 'rev_text_id' => $this->mTextId
,
742 'rev_comment' => $this->mComment
,
743 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
744 'rev_user' => $this->mUser
,
745 'rev_user_text' => $this->mUserText
,
746 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
747 'rev_deleted' => $this->mDeleted
,
748 'rev_len' => $this->mSize
,
749 'rev_parent_id' => $this->mParentId ?
$this->mParentId
: $this->getPreviousRevisionId( $dbw )
753 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
754 wfProfileOut( __METHOD__
);
759 * Lazy-load the revision's text.
760 * Currently hardcoded to the 'text' table storage engine.
764 private function loadText() {
765 wfProfileIn( __METHOD__
);
767 // Caching may be beneficial for massive use of external storage
768 global $wgRevisionCacheExpiry, $wgMemc;
769 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
770 if( $wgRevisionCacheExpiry ) {
771 $text = $wgMemc->get( $key );
772 if( is_string( $text ) ) {
773 wfProfileOut( __METHOD__
);
778 // If we kept data for lazy extraction, use it now...
779 if ( isset( $this->mTextRow
) ) {
780 $row = $this->mTextRow
;
781 $this->mTextRow
= null;
787 // Text data is immutable; check slaves first.
788 $dbr = wfGetDB( DB_SLAVE
);
789 $row = $dbr->selectRow( 'text',
790 array( 'old_text', 'old_flags' ),
791 array( 'old_id' => $this->getTextId() ),
796 // Possible slave lag!
797 $dbw = wfGetDB( DB_MASTER
);
798 $row = $dbw->selectRow( 'text',
799 array( 'old_text', 'old_flags' ),
800 array( 'old_id' => $this->getTextId() ),
804 $text = self
::getRevisionText( $row );
806 if( $wgRevisionCacheExpiry ) {
807 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
810 wfProfileOut( __METHOD__
);
816 * Create a new null-revision for insertion into a page's
817 * history. This will not re-save the text, but simply refer
818 * to the text from the previous version.
820 * Such revisions can for instance identify page rename
821 * operations and other such meta-modifications.
823 * @param Database $dbw
824 * @param int $pageId ID number of the page to read from
825 * @param string $summary
829 public static function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
830 wfProfileIn( __METHOD__
);
832 $current = $dbw->selectRow(
833 array( 'page', 'revision' ),
834 array( 'page_latest', 'rev_text_id' ),
836 'page_id' => $pageId,
837 'page_latest=rev_id',
842 $revision = new Revision( array(
844 'comment' => $summary,
845 'minor_edit' => $minor,
846 'text_id' => $current->rev_text_id
,
847 'parent_id' => $current->page_latest
853 wfProfileOut( __METHOD__
);
858 * Determine if the current user is allowed to view a particular
859 * field of this revision, if it's marked as deleted.
860 * @param int $field one of self::DELETED_TEXT,
861 * self::DELETED_COMMENT,
865 public function userCan( $field ) {
866 if( ( $this->mDeleted
& $field ) == $field ) {
868 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
871 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
872 return $wgUser->isAllowed( $permission );
880 * Get rev_timestamp from rev_id, without loading the rest of the row
883 static function getTimestampFromID( $id ) {
884 $dbr = wfGetDB( DB_SLAVE
);
885 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
886 array( 'rev_id' => $id ), __METHOD__
);
887 if ( $timestamp === false ) {
888 # Not in slave, try master
889 $dbw = wfGetDB( DB_MASTER
);
890 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
891 array( 'rev_id' => $id ), __METHOD__
);
897 * Get count of revisions per page...not very efficient
898 * @param Database $db
899 * @param int $id, page id
901 static function countByPageId( $db, $id ) {
902 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
903 array( 'rev_page' => $id ), __METHOD__
);
905 return $row->revCount
;
911 * Get count of revisions per page...not very efficient
912 * @param Database $db
913 * @param Title $title
915 static function countByTitle( $db, $title ) {
916 $id = $title->getArticleId();
918 return Revision
::countByPageId( $db, $id );
925 * Aliases for backwards compatibility with 1.6
927 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
928 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
929 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
930 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);