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 'page_namespace' => $title->getNamespace(),
47 'page_title' => $title->getDBkey()
50 // Use the specified ID
51 $conds['rev_id'] = $id;
52 } elseif ( wfGetLB()->getServerCount() > 1 ) {
53 // Get the latest revision ID from the master
54 $dbw = wfGetDB( DB_MASTER
);
55 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
56 $conds['rev_id'] = $latest;
58 // Use a join to get the latest revision
59 $conds[] = 'rev_id=page_latest';
61 $conds[] = 'page_id=rev_page';
62 return Revision
::newFromConds( $conds );
66 * Load a page revision from a given revision ID number.
67 * Returns null if no such revision can be found.
74 public static function loadFromId( $db, $id ) {
75 return Revision
::loadFromConds( $db,
76 array( 'page_id=rev_page',
77 'rev_id' => intval( $id ) ) );
81 * Load either the current, or a specified, revision
82 * that's attached to a given page. If not attached
83 * to that page, will return null.
92 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
93 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
95 $conds['rev_id']=intval($id);
97 $conds[]='rev_id=page_latest';
99 return Revision
::loadFromConds( $db, $conds );
103 * Load either the current, or a specified, revision
104 * that's attached to a given page. If not attached
105 * to that page, will return null.
107 * @param Database $db
108 * @param Title $title
114 public static function loadFromTitle( $db, $title, $id = 0 ) {
116 $matchId = intval( $id );
118 $matchId = 'page_latest';
120 return Revision
::loadFromConds(
122 array( "rev_id=$matchId",
124 'page_namespace' => $title->getNamespace(),
125 'page_title' => $title->getDBkey() ) );
129 * Load the revision for the given title with the given timestamp.
130 * WARNING: Timestamps may in some circumstances not be unique,
131 * so this isn't the best key to use.
133 * @param Database $db
134 * @param Title $title
135 * @param string $timestamp
140 public static function loadFromTimestamp( $db, $title, $timestamp ) {
141 return Revision
::loadFromConds(
143 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
145 'page_namespace' => $title->getNamespace(),
146 'page_title' => $title->getDBkey() ) );
150 * Given a set of conditions, fetch a revision.
152 * @param array $conditions
157 private static function newFromConds( $conditions ) {
158 $db = wfGetDB( DB_SLAVE
);
159 $row = Revision
::loadFromConds( $db, $conditions );
160 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
161 $dbw = wfGetDB( DB_MASTER
);
162 $row = Revision
::loadFromConds( $dbw, $conditions );
168 * Given a set of conditions, fetch a revision from
169 * the given database connection.
171 * @param Database $db
172 * @param array $conditions
177 private static function loadFromConds( $db, $conditions ) {
178 $res = Revision
::fetchFromConds( $db, $conditions );
180 $row = $res->fetchObject();
183 $ret = new Revision( $row );
192 * Return a wrapper for a series of database rows to
193 * fetch all of a given page's revisions in turn.
194 * Each row can be fed to the constructor to get objects.
196 * @param Title $title
197 * @return ResultWrapper
201 public static function fetchAllRevisions( $title ) {
202 return Revision
::fetchFromConds(
204 array( 'page_namespace' => $title->getNamespace(),
205 'page_title' => $title->getDBkey(),
206 'page_id=rev_page' ) );
210 * Return a wrapper for a series of database rows to
211 * fetch all of a given page's revisions in turn.
212 * Each row can be fed to the constructor to get objects.
214 * @param Title $title
215 * @return ResultWrapper
219 public static function fetchRevision( $title ) {
220 return Revision
::fetchFromConds(
222 array( 'rev_id=page_latest',
223 'page_namespace' => $title->getNamespace(),
224 'page_title' => $title->getDBkey(),
225 'page_id=rev_page' ) );
229 * Given a set of conditions, return a ResultWrapper
230 * which will return matching database rows with the
231 * fields necessary to build Revision objects.
233 * @param Database $db
234 * @param array $conditions
235 * @return ResultWrapper
239 private static function fetchFromConds( $db, $conditions ) {
240 $fields = self
::selectFields();
241 $fields[] = 'page_namespace';
242 $fields[] = 'page_title';
243 $fields[] = 'page_latest';
245 array( 'page', 'revision' ),
249 array( 'LIMIT' => 1 ) );
250 $ret = $db->resultObject( $res );
255 * Return the list of revision fields that should be selected to create
258 static function selectFields() {
275 * Return the list of text fields that should be selected to read the
278 static function selectTextFields() {
285 * Return the list of page fields that should be selected from page table
287 static function selectPageFields() {
299 function Revision( $row ) {
300 if( is_object( $row ) ) {
301 $this->mId
= intval( $row->rev_id
);
302 $this->mPage
= intval( $row->rev_page
);
303 $this->mTextId
= intval( $row->rev_text_id
);
304 $this->mComment
= $row->rev_comment
;
305 $this->mUserText
= $row->rev_user_text
;
306 $this->mUser
= intval( $row->rev_user
);
307 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
308 $this->mTimestamp
= $row->rev_timestamp
;
309 $this->mDeleted
= intval( $row->rev_deleted
);
311 if( !isset( $row->rev_parent_id
) )
312 $this->mParentId
= is_null($row->rev_parent_id
) ?
null : 0;
314 $this->mParentId
= intval( $row->rev_parent_id
);
316 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) )
319 $this->mSize
= intval( $row->rev_len
);
321 if( isset( $row->page_latest
) ) {
322 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
323 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
326 $this->mCurrent
= false;
327 $this->mTitle
= null;
330 // Lazy extraction...
332 if( isset( $row->old_text
) ) {
333 $this->mTextRow
= $row;
335 // 'text' table row entry will be lazy-loaded
336 $this->mTextRow
= null;
338 } elseif( is_array( $row ) ) {
339 // Build a new revision to be saved...
342 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
343 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
344 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
345 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
346 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
347 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
348 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestamp( TS_MW
);
349 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
350 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
351 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
353 // Enforce spacing trimming on supplied text
354 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
355 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
356 $this->mTextRow
= null;
358 $this->mTitle
= null; # Load on demand if needed
359 $this->mCurrent
= false;
360 # If we still have no len_size, see it we have the text to figure it out
362 $this->mSize
= is_null($this->mText
) ?
null : strlen($this->mText
);
364 throw new MWException( 'Revision constructor passed invalid row format.' );
376 public function getId() {
384 public function getTextId() {
385 return $this->mTextId
;
389 * Get parent revision ID (the original previous page revision)
392 public function getParentId() {
393 return $this->mParentId
;
397 * Returns the length of the text in this revision, or null if unknown.
400 public function getSize() {
405 * Returns the title of the page associated with this entry.
408 public function getTitle() {
409 if( isset( $this->mTitle
) ) {
410 return $this->mTitle
;
412 $dbr = wfGetDB( DB_SLAVE
);
413 $row = $dbr->selectRow(
414 array( 'page', 'revision' ),
415 array( 'page_namespace', 'page_title' ),
416 array( 'page_id=rev_page',
417 'rev_id' => $this->mId
),
418 'Revision::getTitle' );
420 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
423 return $this->mTitle
;
427 * Set the title of the revision
428 * @param Title $title
430 public function setTitle( $title ) {
431 $this->mTitle
= $title;
438 public function getPage() {
443 * Fetch revision's user id if it's available to the specified audience.
444 * If the specified audience does not have access to it, zero will be
447 * @param integer $audience One of:
448 * Revision::FOR_PUBLIC to be displayed to all users
449 * Revision::FOR_THIS_USER to be displayed to $wgUser
450 * Revision::RAW get the ID regardless of permissions
455 public function getUser( $audience = self
::FOR_PUBLIC
) {
456 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
458 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
466 * Fetch revision's user id without regard for the current user's permissions
469 public function getRawUser() {
474 * Fetch revision's username if it's available to the specified audience.
475 * If the specified audience does not have access to the username, an
476 * empty string will be returned.
478 * @param integer $audience One of:
479 * Revision::FOR_PUBLIC to be displayed to all users
480 * Revision::FOR_THIS_USER to be displayed to $wgUser
481 * Revision::RAW get the text regardless of permissions
485 public function getUserText( $audience = self
::FOR_PUBLIC
) {
486 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
488 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
) ) {
491 return $this->mUserText
;
496 * Fetch revision's username without regard for view restrictions
499 public function getRawUserText() {
500 return $this->mUserText
;
504 * Fetch revision comment if it's available to the specified audience.
505 * If the specified audience does not have access to the comment, an
506 * empty string will be returned.
508 * @param integer $audience One of:
509 * Revision::FOR_PUBLIC to be displayed to all users
510 * Revision::FOR_THIS_USER to be displayed to $wgUser
511 * Revision::RAW get the text regardless of permissions
515 function getComment( $audience = self
::FOR_PUBLIC
) {
516 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
518 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
) ) {
521 return $this->mComment
;
526 * Fetch revision comment without regard for the current user's permissions
529 public function getRawComment() {
530 return $this->mComment
;
536 public function isMinor() {
537 return (bool)$this->mMinorEdit
;
541 * int $field one of DELETED_* bitfield constants
544 public function isDeleted( $field ) {
545 return ($this->mDeleted
& $field) == $field;
549 * Get the deletion bitfield of the revision
551 public function getVisibility() {
552 return (int)$this->mDeleted
;
556 * Fetch revision text if it's available to the specified audience.
557 * If the specified audience does not have the ability to view this
558 * revision, an empty string will be returned.
560 * @param integer $audience One of:
561 * Revision::FOR_PUBLIC to be displayed to all users
562 * Revision::FOR_THIS_USER to be displayed to $wgUser
563 * Revision::RAW get the text regardless of permissions
568 public function getText( $audience = self
::FOR_PUBLIC
) {
569 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
571 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
) ) {
574 return $this->getRawText();
579 * Alias for getText(Revision::FOR_THIS_USER)
581 public function revText() {
582 return $this->getText( self
::FOR_THIS_USER
);
586 * Fetch revision text without regard for view restrictions
589 public function getRawText() {
590 if( is_null( $this->mText
) ) {
591 // Revision text is immutable. Load on demand:
592 $this->mText
= $this->loadText();
600 public function getTimestamp() {
601 return wfTimestamp(TS_MW
, $this->mTimestamp
);
607 public function isCurrent() {
608 return $this->mCurrent
;
612 * Get previous revision for this title
615 public function getPrevious() {
616 if( $this->getTitle() ) {
617 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
619 return Revision
::newFromTitle( $this->getTitle(), $prev );
628 public function getNext() {
629 if( $this->getTitle() ) {
630 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
632 return Revision
::newFromTitle( $this->getTitle(), $next );
639 * Get previous revision Id for this page_id
640 * This is used to populate rev_parent_id on save
641 * @param Database $db
644 private function getPreviousRevisionId( $db ) {
645 if( is_null($this->mPage
) ) {
648 # Use page_latest if ID is not given
650 $prevId = $db->selectField( 'page', 'page_latest',
651 array( 'page_id' => $this->mPage
),
654 $prevId = $db->selectField( 'revision', 'rev_id',
655 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
657 array( 'ORDER BY' => 'rev_id DESC' ) );
659 return intval($prevId);
663 * Get revision text associated with an old or archive row
664 * $row is usually an object from wfFetchRow(), both the flags and the text
665 * field must be included
667 * @param object $row The text data
668 * @param string $prefix table prefix (default 'old_')
669 * @return string $text|false the text requested
671 public static function getRevisionText( $row, $prefix = 'old_' ) {
672 wfProfileIn( __METHOD__
);
675 $textField = $prefix . 'text';
676 $flagsField = $prefix . 'flags';
678 if( isset( $row->$flagsField ) ) {
679 $flags = explode( ',', $row->$flagsField );
684 if( isset( $row->$textField ) ) {
685 $text = $row->$textField;
687 wfProfileOut( __METHOD__
);
691 # Use external methods for external objects, text in table is URL-only then
692 if ( in_array( 'external', $flags ) ) {
694 @list
(/* $proto */,$path)=explode('://',$url,2);
696 wfProfileOut( __METHOD__
);
699 $text=ExternalStore
::fetchFromURL($url);
702 // If the text was fetched without an error, convert it
703 if ( $text !== false ) {
704 if( in_array( 'gzip', $flags ) ) {
705 # Deal with optional compression of archived pages.
706 # This can be done periodically via maintenance/compressOld.php, and
707 # as pages are saved if $wgCompressRevisions is set.
708 $text = gzinflate( $text );
711 if( in_array( 'object', $flags ) ) {
712 # Generic compressed storage
713 $obj = unserialize( $text );
714 if ( !is_object( $obj ) ) {
716 wfProfileOut( __METHOD__
);
719 $text = $obj->getText();
722 global $wgLegacyEncoding;
723 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
724 # Old revisions kept around in a legacy encoding?
725 # Upconvert on demand.
726 global $wgInputEncoding, $wgContLang;
727 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
730 wfProfileOut( __METHOD__
);
735 * If $wgCompressRevisions is enabled, we will compress data.
736 * The input string is modified in place.
737 * Return value is the flags field: contains 'gzip' if the
738 * data is compressed, and 'utf-8' if we're saving in UTF-8
741 * @param mixed $text reference to a text
744 public static function compressRevisionText( &$text ) {
745 global $wgCompressRevisions;
748 # Revisions not marked this way will be converted
749 # on load if $wgLegacyCharset is set in the future.
752 if( $wgCompressRevisions ) {
753 if( function_exists( 'gzdeflate' ) ) {
754 $text = gzdeflate( $text );
757 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
760 return implode( ',', $flags );
764 * Insert a new revision into the database, returning the new revision ID
765 * number on success and dies horribly on failure.
767 * @param Database $dbw
770 public function insertOn( $dbw ) {
771 global $wgDefaultExternalStore;
773 wfProfileIn( __METHOD__
);
775 $data = $this->mText
;
776 $flags = Revision
::compressRevisionText( $data );
778 # Write to external storage if required
779 if( $wgDefaultExternalStore ) {
780 // Store and get the URL
781 $data = ExternalStore
::insertToDefault( $data );
783 throw new MWException( "Unable to store text to external storage" );
788 $flags .= 'external';
791 # Record the text (or external storage URL) to the text table
792 if( !isset( $this->mTextId
) ) {
793 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
794 $dbw->insert( 'text',
798 'old_flags' => $flags,
801 $this->mTextId
= $dbw->insertId();
804 # Record the edit in revisions
805 $rev_id = isset( $this->mId
)
807 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
808 $dbw->insert( 'revision',
811 'rev_page' => $this->mPage
,
812 'rev_text_id' => $this->mTextId
,
813 'rev_comment' => $this->mComment
,
814 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
815 'rev_user' => $this->mUser
,
816 'rev_user_text' => $this->mUserText
,
817 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
818 'rev_deleted' => $this->mDeleted
,
819 'rev_len' => $this->mSize
,
820 'rev_parent_id' => $this->mParentId ?
$this->mParentId
: $this->getPreviousRevisionId( $dbw )
824 $this->mId
= !is_null($rev_id) ?
$rev_id : $dbw->insertId();
826 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
828 wfProfileOut( __METHOD__
);
833 * Lazy-load the revision's text.
834 * Currently hardcoded to the 'text' table storage engine.
838 private function loadText() {
839 wfProfileIn( __METHOD__
);
841 // Caching may be beneficial for massive use of external storage
842 global $wgRevisionCacheExpiry, $wgMemc;
843 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
844 if( $wgRevisionCacheExpiry ) {
845 $text = $wgMemc->get( $key );
846 if( is_string( $text ) ) {
847 wfProfileOut( __METHOD__
);
852 // If we kept data for lazy extraction, use it now...
853 if ( isset( $this->mTextRow
) ) {
854 $row = $this->mTextRow
;
855 $this->mTextRow
= null;
861 // Text data is immutable; check slaves first.
862 $dbr = wfGetDB( DB_SLAVE
);
863 $row = $dbr->selectRow( 'text',
864 array( 'old_text', 'old_flags' ),
865 array( 'old_id' => $this->getTextId() ),
869 if( !$row && wfGetLB()->getServerCount() > 1 ) {
870 // Possible slave lag!
871 $dbw = wfGetDB( DB_MASTER
);
872 $row = $dbw->selectRow( 'text',
873 array( 'old_text', 'old_flags' ),
874 array( 'old_id' => $this->getTextId() ),
878 $text = self
::getRevisionText( $row );
880 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
881 if( $wgRevisionCacheExpiry && $text !== false ) {
882 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
885 wfProfileOut( __METHOD__
);
891 * Create a new null-revision for insertion into a page's
892 * history. This will not re-save the text, but simply refer
893 * to the text from the previous version.
895 * Such revisions can for instance identify page rename
896 * operations and other such meta-modifications.
898 * @param Database $dbw
899 * @param int $pageId ID number of the page to read from
900 * @param string $summary
904 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
905 wfProfileIn( __METHOD__
);
907 $current = $dbw->selectRow(
908 array( 'page', 'revision' ),
909 array( 'page_latest', 'rev_text_id' ),
911 'page_id' => $pageId,
912 'page_latest=rev_id',
917 $revision = new Revision( array(
919 'comment' => $summary,
920 'minor_edit' => $minor,
921 'text_id' => $current->rev_text_id
,
922 'parent_id' => $current->page_latest
928 wfProfileOut( __METHOD__
);
933 * Determine if the current user is allowed to view a particular
934 * field of this revision, if it's marked as deleted.
935 * @param int $field one of self::DELETED_TEXT,
936 * self::DELETED_COMMENT,
940 public function userCan( $field ) {
941 if( ( $this->mDeleted
& $field ) == $field ) {
943 $permission = ( $this->mDeleted
& self
::DELETED_RESTRICTED
) == self
::DELETED_RESTRICTED
946 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
947 return $wgUser->isAllowed( $permission );
955 * Get rev_timestamp from rev_id, without loading the rest of the row
956 * @param Title $title
959 static function getTimestampFromId( $title, $id ) {
960 $dbr = wfGetDB( DB_SLAVE
);
961 $conds = array( 'rev_id' => $id );
962 $conds['rev_page'] = $title->getArticleId();
963 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
964 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
965 # Not in slave, try master
966 $dbw = wfGetDB( DB_MASTER
);
967 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
969 return wfTimestamp( TS_MW
, $timestamp );
973 * Get count of revisions per page...not very efficient
974 * @param Database $db
975 * @param int $id, page id
977 static function countByPageId( $db, $id ) {
978 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
979 array( 'rev_page' => $id ), __METHOD__
);
981 return $row->revCount
;
987 * Get count of revisions per page...not very efficient
988 * @param Database $db
989 * @param Title $title
991 static function countByTitle( $db, $title ) {
992 $id = $title->getArticleId();
994 return Revision
::countByPageId( $db, $id );
1001 * Aliases for backwards compatibility with 1.6
1003 define( 'MW_REV_DELETED_TEXT', Revision
::DELETED_TEXT
);
1004 define( 'MW_REV_DELETED_COMMENT', Revision
::DELETED_COMMENT
);
1005 define( 'MW_REV_DELETED_USER', Revision
::DELETED_USER
);
1006 define( 'MW_REV_DELETED_RESTRICTED', Revision
::DELETED_RESTRICTED
);