11 const DELETED_TEXT
= 1;
12 const DELETED_COMMENT
= 2;
13 const DELETED_USER
= 4;
14 const DELETED_RESTRICTED
= 8;
16 // Audience options for Revision::getText()
18 const FOR_THIS_USER
= 2;
22 * Load a page revision from a given revision ID number.
23 * Returns null if no such revision can be found.
29 public static function newFromId( $id ) {
30 return Revision
::newFromConds(
31 array( 'page_id=rev_page',
32 'rev_id' => intval( $id ) ) );
36 * Load either the current, or a specified, revision
37 * that's attached to a given title. If not attached
38 * to that title, will return null.
44 public static function newFromTitle( $title, $id = 0 ) {
46 $matchId = intval( $id );
48 $matchId = 'page_latest';
50 return Revision
::newFromConds(
51 array( "rev_id=$matchId",
53 'page_namespace' => $title->getNamespace(),
54 'page_title' => $title->getDBkey() ) );
58 * Load a page revision from a given revision ID number.
59 * Returns null if no such revision can be found.
66 public static function loadFromId( $db, $id ) {
67 return Revision
::loadFromConds( $db,
68 array( 'page_id=rev_page',
69 'rev_id' => intval( $id ) ) );
73 * Load either the current, or a specified, revision
74 * that's attached to a given page. If not attached
75 * to that page, will return null.
84 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
85 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
87 $conds['rev_id']=intval($id);
89 $conds[]='rev_id=page_latest';
91 return Revision
::loadFromConds( $db, $conds );
95 * Load either the current, or a specified, revision
96 * that's attached to a given page. If not attached
97 * to that page, will return null.
100 * @param Title $title
106 public static function loadFromTitle( $db, $title, $id = 0 ) {
108 $matchId = intval( $id );
110 $matchId = 'page_latest';
112 return Revision
::loadFromConds(
114 array( "rev_id=$matchId",
116 'page_namespace' => $title->getNamespace(),
117 'page_title' => $title->getDBkey() ) );
121 * Load the revision for the given title with the given timestamp.
122 * WARNING: Timestamps may in some circumstances not be unique,
123 * so this isn't the best key to use.
125 * @param Database $db
126 * @param Title $title
127 * @param string $timestamp
132 public static function loadFromTimestamp( $db, $title, $timestamp ) {
133 return Revision
::loadFromConds(
135 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
137 'page_namespace' => $title->getNamespace(),
138 'page_title' => $title->getDBkey() ) );
142 * Given a set of conditions, fetch a revision.
144 * @param array $conditions
149 private static function newFromConds( $conditions ) {
150 $db = wfGetDB( DB_SLAVE
);
151 $row = Revision
::loadFromConds( $db, $conditions );
152 if( is_null( $row ) ) {
153 $dbw = wfGetDB( DB_MASTER
);
154 $row = Revision
::loadFromConds( $dbw, $conditions );
160 * Given a set of conditions, fetch a revision from
161 * the given database connection.
163 * @param Database $db
164 * @param array $conditions
169 private static function loadFromConds( $db, $conditions ) {
170 $res = Revision
::fetchFromConds( $db, $conditions );
172 $row = $res->fetchObject();
175 $ret = new Revision( $row );
184 * Return a wrapper for a series of database rows to
185 * fetch all of a given page's revisions in turn.
186 * Each row can be fed to the constructor to get objects.
188 * @param Title $title
189 * @return ResultWrapper
193 public static function fetchAllRevisions( $title ) {
194 return Revision
::fetchFromConds(
196 array( 'page_namespace' => $title->getNamespace(),
197 'page_title' => $title->getDBkey(),
198 'page_id=rev_page' ) );
202 * Return a wrapper for a series of database rows to
203 * fetch all of a given page's revisions in turn.
204 * Each row can be fed to the constructor to get objects.
206 * @param Title $title
207 * @return ResultWrapper
211 public static function fetchRevision( $title ) {
212 return Revision
::fetchFromConds(
214 array( 'rev_id=page_latest',
215 'page_namespace' => $title->getNamespace(),
216 'page_title' => $title->getDBkey(),
217 'page_id=rev_page' ) );
221 * Given a set of conditions, return a ResultWrapper
222 * which will return matching database rows with the
223 * fields necessary to build Revision objects.
225 * @param Database $db
226 * @param array $conditions
227 * @return ResultWrapper
231 private static function fetchFromConds( $db, $conditions ) {
232 $fields = self
::selectFields();
233 $fields[] = 'page_namespace';
234 $fields[] = 'page_title';
235 $fields[] = 'page_latest';
237 array( 'page', 'revision' ),
240 'Revision::fetchRow',
241 array( 'LIMIT' => 1 ) );
242 $ret = $db->resultObject( $res );
247 * Return the list of revision fields that should be selected to create
250 static function selectFields() {
267 * Return the list of text fields that should be selected to read the
270 static function selectTextFields() {
277 * Return the list of page fields that should be selected from page table
279 static function selectPageFields() {
291 function Revision( $row ) {
292 if( is_object( $row ) ) {
293 $this->mId
= intval( $row->rev_id
);
294 $this->mPage
= intval( $row->rev_page
);
295 $this->mTextId
= intval( $row->rev_text_id
);
296 $this->mComment
= $row->rev_comment
;
297 $this->mUserText
= $row->rev_user_text
;
298 $this->mUser
= intval( $row->rev_user
);
299 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
300 $this->mTimestamp
= $row->rev_timestamp
;
301 $this->mDeleted
= intval( $row->rev_deleted
);
303 if( !isset( $row->rev_parent_id
) )
304 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
306 $this->mParentId
= intval( $row->rev_parent_id
);
308 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
311 $this->mSize
= intval( $row->rev_len
);
313 if( isset( $row->page_latest
) ) {
314 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
315 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
318 $this->mCurrent
= false;
319 $this->mTitle
= null;
322 // Lazy extraction...
324 if( isset( $row->old_text
) ) {
325 $this->mTextRow
= $row;
327 // 'text' table row entry will be lazy-loaded
328 $this->mTextRow
= null;
330 } elseif( is_array( $row ) ) {
331 // Build a new revision to be saved...
334 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
335 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
336 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
337 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
338 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
339 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
340 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
341 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
342 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
343 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
345 // Enforce spacing trimming on supplied text
346 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
347 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
348 $this->mTextRow
= null;
350 $this->mTitle
= null; # Load on demand if needed
351 $this->mCurrent
= false;
352 # If we still have no len_size, see it we have the text to figure it out
354 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
356 throw new MWException( 'Revision constructor passed invalid row format.' );
368 public function getId() {
376 public function getTextId() {
377 return $this->mTextId
;
381 * Get parent revision ID (the original previous page revision)
384 public function getParentId() {
385 return $this->mParentId
;
389 * Returns the length of the text in this revision, or null if unknown.
392 public function getSize() {
397 * Returns the title of the page associated with this entry.
400 public function getTitle() {
401 if( isset( $this->mTitle
) ) {
402 return $this->mTitle
;
404 $dbr = wfGetDB( DB_SLAVE
);
405 $row = $dbr->selectRow(
406 array( 'page', 'revision' ),
407 array( 'page_namespace', 'page_title' ),
408 array( 'page_id=rev_page',
409 'rev_id' => $this->mId
),
410 'Revision::getTitle' );
412 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
415 return $this->mTitle
;
419 * Set the title of the revision
420 * @param Title $title
422 public function setTitle( $title ) {
423 $this->mTitle
= $title;
430 public function getPage() {
435 * Fetch revision's user id if it's available to the specified audience.
436 * If the specified audience does not have access to it, zero will be
439 * @param integer $audience One of:
440 * Revision::FOR_PUBLIC to be displayed to all users
441 * Revision::FOR_THIS_USER to be displayed to $wgUser
442 * Revision::RAW get the ID regardless of permissions
447 public function getUser( $audience = self
::FOR_PUBLIC
) {
448 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
450 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
458 * Fetch revision's user id without regard for the current user's permissions
461 public function getRawUser() {
466 * Fetch revision's username if it's available to the specified audience.
467 * If the specified audience does not have access to the username, an
468 * empty string will be returned.
470 * @param integer $audience One of:
471 * Revision::FOR_PUBLIC to be displayed to all users
472 * Revision::FOR_THIS_USER to be displayed to $wgUser
473 * Revision::RAW get the text regardless of permissions
477 public function getUserText( $audience = self
::FOR_PUBLIC
) {
478 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
480 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
483 return $this->mUserText
;
488 * Fetch revision's username without regard for view restrictions
491 public function getRawUserText() {
492 return $this->mUserText
;
496 * Fetch revision comment if it's available to the specified audience.
497 * If the specified audience does not have access to the comment, an
498 * empty string will be returned.
500 * @param integer $audience One of:
501 * Revision::FOR_PUBLIC to be displayed to all users
502 * Revision::FOR_THIS_USER to be displayed to $wgUser
503 * Revision::RAW get the text regardless of permissions
507 function getComment( $audience = self
::FOR_PUBLIC
) {
508 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
510 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
) ) {
513 return $this->mComment
;
518 * Fetch revision comment without regard for the current user's permissions
521 public function getRawComment() {
522 return $this->mComment
;
528 public function isMinor() {
529 return (bool)$this->mMinorEdit
;
533 * int $field one of DELETED_* bitfield constants
536 public function isDeleted( $field ) {
537 return ($this->mDeleted
& $field) == $field;
541 * Fetch revision text if it's available to the specified audience.
542 * If the specified audience does not have the ability to view this
543 * revision, an empty string will be returned.
545 * @param integer $audience One of:
546 * Revision::FOR_PUBLIC to be displayed to all users
547 * Revision::FOR_THIS_USER to be displayed to $wgUser
548 * Revision::RAW get the text regardless of permissions
553 public function getText( $audience = self
::FOR_PUBLIC
) {
554 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
556 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
) ) {
559 return $this->getRawText();
564 * Alias for getText(Revision::FOR_THIS_USER)
566 public function revText() {
567 return $this->getText( self
::FOR_THIS_USER
);
571 * Fetch revision text without regard for view restrictions
574 public function getRawText() {
575 if( is_null( $this->mText
) ) {
576 // Revision text is immutable. Load on demand:
577 $this->mText
= $this->loadText();
585 public function getTimestamp() {
586 return wfTimestamp(TS_MW
, $this->mTimestamp
);
592 public function isCurrent() {
593 return $this->mCurrent
;
597 * Get previous revision for this title
600 public function getPrevious() {
601 if( $this->getTitle() ) {
602 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
604 return Revision
::newFromTitle( $this->getTitle(), $prev );
613 public function getNext() {
614 if( $this->getTitle() ) {
615 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
617 return Revision
::newFromTitle( $this->getTitle(), $next );
624 * Get previous revision Id for this page_id
625 * This is used to populate rev_parent_id on save
626 * @param Database $db
629 private function getPreviousRevisionId( $db ) {
630 if( is_null($this->mPage
) ) {
633 # Use page_latest if ID is not given
635 $prevId = $db->selectField( 'page', 'page_latest',
636 array( 'page_id' => $this->mPage
),
639 $prevId = $db->selectField( 'revision', 'rev_id',
640 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
642 array( 'ORDER BY' => 'rev_id DESC' ) );
644 return intval($prevId);
648 * Get revision text associated with an old or archive row
649 * $row is usually an object from wfFetchRow(), both the flags and the text
650 * field must be included
652 * @param integer $row Id of a row
653 * @param string $prefix table prefix (default 'old_')
654 * @return string $text|false the text requested
656 public static function getRevisionText( $row, $prefix = 'old_' ) {
657 wfProfileIn( __METHOD__
);
660 $textField = $prefix . 'text';
661 $flagsField = $prefix . 'flags';
663 if( isset( $row->$flagsField ) ) {
664 $flags = explode( ',', $row->$flagsField );
669 if( isset( $row->$textField ) ) {
670 $text = $row->$textField;
672 wfProfileOut( __METHOD__
);
676 # Use external methods for external objects, text in table is URL-only then
677 if ( in_array( 'external', $flags ) ) {
679 @list
(/* $proto */,$path)=explode('://',$url,2);
681 wfProfileOut( __METHOD__
);
684 $text=ExternalStore
::fetchFromURL($url);
687 // If the text was fetched without an error, convert it
688 if ( $text !== false ) {
689 if( in_array( 'gzip', $flags ) ) {
690 # Deal with optional compression of archived pages.
691 # This can be done periodically via maintenance/compressOld.php, and
692 # as pages are saved if $wgCompressRevisions is set.
693 $text = gzinflate( $text );
696 if( in_array( 'object', $flags ) ) {
697 # Generic compressed storage
698 $obj = unserialize( $text );
699 if ( !is_object( $obj ) ) {
701 wfProfileOut( __METHOD__
);
704 $text = $obj->getText();
707 global $wgLegacyEncoding;
708 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
709 # Old revisions kept around in a legacy encoding?
710 # Upconvert on demand.
711 global $wgInputEncoding, $wgContLang;
712 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
715 wfProfileOut( __METHOD__
);
720 * If $wgCompressRevisions is enabled, we will compress data.
721 * The input string is modified in place.
722 * Return value is the flags field: contains 'gzip' if the
723 * data is compressed, and 'utf-8' if we're saving in UTF-8
726 * @param mixed $text reference to a text
729 public static function compressRevisionText( &$text ) {
730 global $wgCompressRevisions;
733 # Revisions not marked this way will be converted
734 # on load if $wgLegacyCharset is set in the future.
737 if( $wgCompressRevisions ) {
738 if( function_exists( 'gzdeflate' ) ) {
739 $text = gzdeflate( $text );
742 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
745 return implode( ',', $flags );
749 * Insert a new revision into the database, returning the new revision ID
750 * number on success and dies horribly on failure.
752 * @param Database $dbw
755 public function insertOn( $dbw ) {
756 global $wgDefaultExternalStore;
758 wfProfileIn( __METHOD__
);
760 $data = $this->mText
;
761 $flags = Revision
::compressRevisionText( $data );
763 # Write to external storage if required
764 if ( $wgDefaultExternalStore ) {
765 // Store and get the URL
766 $data = ExternalStore
::insertToDefault( $data );
770 $flags .= 'external';
773 # Record the text (or external storage URL) to the text table
774 if( !isset( $this->mTextId
) ) {
775 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
776 $dbw->insert( 'text',
780 'old_flags' => $flags,
783 $this->mTextId
= $dbw->insertId();
786 # Record the edit in revisions
787 $rev_id = isset( $this->mId
)
789 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
790 $dbw->insert( 'revision',
793 'rev_page' => $this->mPage
,
794 'rev_text_id' => $this->mTextId
,
795 'rev_comment' => $this->mComment
,
796 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
797 'rev_user' => $this->mUser
,
798 'rev_user_text' => $this->mUserText
,
799 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
800 'rev_deleted' => $this->mDeleted
,
801 'rev_len' => $this->mSize
,
802 'rev_parent_id' => $this->mParentId ?
$this->mParentId
: $this->getPreviousRevisionId( $dbw )
806 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
808 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
810 wfProfileOut( __METHOD__
);
815 * Lazy-load the revision's text.
816 * Currently hardcoded to the 'text' table storage engine.
820 private function loadText() {
821 wfProfileIn( __METHOD__
);
823 // Caching may be beneficial for massive use of external storage
824 global $wgRevisionCacheExpiry, $wgMemc;
825 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
826 if( $wgRevisionCacheExpiry ) {
827 $text = $wgMemc->get( $key );
828 if( is_string( $text ) ) {
829 wfProfileOut( __METHOD__
);
834 // If we kept data for lazy extraction, use it now...
835 if ( isset( $this->mTextRow
) ) {
836 $row = $this->mTextRow
;
837 $this->mTextRow
= null;
843 // Text data is immutable; check slaves first.
844 $dbr = wfGetDB( DB_SLAVE
);
845 $row = $dbr->selectRow( 'text',
846 array( 'old_text', 'old_flags' ),
847 array( 'old_id' => $this->getTextId() ),
852 // Possible slave lag!
853 $dbw = wfGetDB( DB_MASTER
);
854 $row = $dbw->selectRow( 'text',
855 array( 'old_text', 'old_flags' ),
856 array( 'old_id' => $this->getTextId() ),
860 $text = self
::getRevisionText( $row );
862 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
863 if( $wgRevisionCacheExpiry && $text !== false ) {
864 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
867 wfProfileOut( __METHOD__
);
873 * Create a new null-revision for insertion into a page's
874 * history. This will not re-save the text, but simply refer
875 * to the text from the previous version.
877 * Such revisions can for instance identify page rename
878 * operations and other such meta-modifications.
880 * @param Database $dbw
881 * @param int $pageId ID number of the page to read from
882 * @param string $summary
886 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
887 wfProfileIn( __METHOD__
);
889 $current = $dbw->selectRow(
890 array( 'page', 'revision' ),
891 array( 'page_latest', 'rev_text_id' ),
893 'page_id' => $pageId,
894 'page_latest=rev_id',
899 $revision = new Revision( array(
901 'comment' => $summary,
902 'minor_edit' => $minor,
903 'text_id' => $current->rev_text_id
,
904 'parent_id' => $current->page_latest
910 wfProfileOut( __METHOD__
);
915 * Determine if the current user is allowed to view a particular
916 * field of this revision, if it's marked as deleted.
917 * @param int $field one of self::DELETED_TEXT,
918 * self::DELETED_COMMENT,
922 public function userCan( $field ) {
923 if( ( $this->mDeleted
& $field ) == $field ) {
925 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
928 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
929 return $wgUser->isAllowed( $permission );
937 * Get rev_timestamp from rev_id, without loading the rest of the row
939 * @param integer $pageid, optional
941 static function getTimestampFromId( $id, $pageId = 0 ) {
942 $dbr = wfGetDB( DB_SLAVE
);
943 $conds = array( 'rev_id' => $id );
945 $conds['rev_page'] = $pageId;
947 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
948 if ( $timestamp === false ) {
949 # Not in slave, try master
950 $dbw = wfGetDB( DB_MASTER
);
951 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
953 return wfTimestamp( TS_MW
, $timestamp );
957 * Get count of revisions per page...not very efficient
958 * @param Database $db
959 * @param int $id, page id
961 static function countByPageId( $db, $id ) {
962 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
963 array( 'rev_page' => $id ), __METHOD__
);
965 return $row->revCount
;
971 * Get count of revisions per page...not very efficient
972 * @param Database $db
973 * @param Title $title
975 static function countByTitle( $db, $title ) {
976 $id = $title->getArticleId();
978 return Revision
::countByPageId( $db, $id );
985 * Aliases for backwards compatibility with 1.6
987 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
988 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
989 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
990 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);