3 * Representation of a page version.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class Revision
implements IDBAccessObject
{
30 protected $mOrigUserText;
32 protected $mMinorEdit;
33 protected $mTimestamp;
44 const DELETED_TEXT
= 1;
45 const DELETED_COMMENT
= 2;
46 const DELETED_USER
= 4;
47 const DELETED_RESTRICTED
= 8;
49 const SUPPRESSED_USER
= 12;
50 // Audience options for accessors
52 const FOR_THIS_USER
= 2;
56 * Load a page revision from a given revision ID number.
57 * Returns null if no such revision can be found.
60 * IDBAccessObject::LATEST_READ : Select the data from the master
61 * IDBAccessObject::LOCKING_READ : Select & lock the data from the master
62 * IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
65 * @param $flags Integer (optional)
66 * @return Revision or null
68 public static function newFromId( $id, $flags = 0 ) {
69 return self
::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
73 * Load either the current, or a specified, revision
74 * that's attached to a given title. If not attached
75 * to that title, will return null.
78 * IDBAccessObject::LATEST_READ : Select the data from the master
79 * IDBAccessObject::LOCKING_READ : Select & lock the data from the master
80 * IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
83 * @param $id Integer (optional)
84 * @param $flags Integer Bitfield (optional)
85 * @return Revision or null
87 public static function newFromTitle( $title, $id = 0, $flags = 0 ) {
89 'page_namespace' => $title->getNamespace(),
90 'page_title' => $title->getDBkey()
93 // Use the specified ID
94 $conds['rev_id'] = $id;
95 } elseif ( !( $flags & self
::AVOID_MASTER
) && 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 // Use a join to get the latest revision
105 $conds[] = 'rev_id=page_latest';
107 return self
::newFromConds( $conds, $flags );
111 * Load either the current, or a specified, revision
112 * that's attached to a given page ID.
113 * Returns null if no such revision can be found.
116 * IDBAccessObject::LATEST_READ : Select the data from the master
117 * IDBAccessObject::LOCKING_READ : Select & lock the data from the master
118 * IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
120 * @param $revId Integer
121 * @param $pageId Integer (optional)
122 * @param $flags Integer Bitfield (optional)
123 * @return Revision or null
125 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
126 $conds = array( 'page_id' => $pageId );
128 $conds['rev_id'] = $revId;
129 } elseif ( !( $flags & self
::AVOID_MASTER
) && wfGetLB()->getServerCount() > 1 ) {
130 // Get the latest revision ID from the master
131 $dbw = wfGetDB( DB_MASTER
);
132 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
133 if ( $latest === false ) {
134 return null; // page does not exist
136 $conds['rev_id'] = $latest;
138 $conds[] = 'rev_id = page_latest';
140 return self
::newFromConds( $conds, $flags );
144 * Make a fake revision object from an archive table row. This is queried
145 * for permissions or even inserted (as in Special:Undelete)
146 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
149 * @param $overrides array
153 public static function newFromArchiveRow( $row, $overrides = array() ) {
154 $attribs = $overrides +
array(
155 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
156 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
157 'comment' => $row->ar_comment
,
158 'user' => $row->ar_user
,
159 'user_text' => $row->ar_user_text
,
160 'timestamp' => $row->ar_timestamp
,
161 'minor_edit' => $row->ar_minor_edit
,
162 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
163 'deleted' => $row->ar_deleted
,
164 'len' => $row->ar_len
,
165 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
167 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
168 // Pre-1.5 ar_text row
169 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
170 if ( $attribs['text'] === false ) {
171 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
174 return new self( $attribs );
183 public static function newFromRow( $row ) {
184 return new self( $row );
188 * Load a page revision from a given revision ID number.
189 * Returns null if no such revision can be found.
191 * @param $db DatabaseBase
193 * @return Revision or null
195 public static function loadFromId( $db, $id ) {
196 return self
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
200 * Load either the current, or a specified, revision
201 * that's attached to a given page. If not attached
202 * to that page, will return null.
204 * @param $db DatabaseBase
205 * @param $pageid Integer
207 * @return Revision or null
209 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
210 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
212 $conds['rev_id'] = intval( $id );
214 $conds[] = 'rev_id=page_latest';
216 return self
::loadFromConds( $db, $conds );
220 * Load either the current, or a specified, revision
221 * that's attached to a given page. If not attached
222 * to that page, will return null.
224 * @param $db DatabaseBase
225 * @param $title Title
227 * @return Revision or null
229 public static function loadFromTitle( $db, $title, $id = 0 ) {
231 $matchId = intval( $id );
233 $matchId = 'page_latest';
235 return self
::loadFromConds( $db,
236 array( "rev_id=$matchId",
237 'page_namespace' => $title->getNamespace(),
238 'page_title' => $title->getDBkey() )
243 * Load the revision for the given title with the given timestamp.
244 * WARNING: Timestamps may in some circumstances not be unique,
245 * so this isn't the best key to use.
247 * @param $db DatabaseBase
248 * @param $title Title
249 * @param $timestamp String
250 * @return Revision or null
252 public static function loadFromTimestamp( $db, $title, $timestamp ) {
253 return self
::loadFromConds( $db,
254 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
255 'page_namespace' => $title->getNamespace(),
256 'page_title' => $title->getDBkey() )
261 * Given a set of conditions, fetch a revision.
263 * @param $conditions Array
264 * @param $flags integer (optional)
265 * @return Revision or null
267 private static function newFromConds( $conditions, $flags = 0 ) {
268 $db = wfGetDB( ( $flags & self
::LATEST_READ
) ? DB_MASTER
: DB_SLAVE
);
269 $rev = self
::loadFromConds( $db, $conditions, $flags );
270 if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
271 if ( !( $flags & self
::LATEST_READ
) && !( $flags & self
::AVOID_MASTER
) ) {
272 $dbw = wfGetDB( DB_MASTER
);
273 $rev = self
::loadFromConds( $dbw, $conditions, $flags );
280 * Given a set of conditions, fetch a revision from
281 * the given database connection.
283 * @param $db DatabaseBase
284 * @param $conditions Array
285 * @param $flags integer (optional)
286 * @return Revision or null
288 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
289 $res = self
::fetchFromConds( $db, $conditions, $flags );
291 $row = $res->fetchObject();
293 $ret = new Revision( $row );
302 * Return a wrapper for a series of database rows to
303 * fetch all of a given page's revisions in turn.
304 * Each row can be fed to the constructor to get objects.
306 * @param $title Title
307 * @return ResultWrapper
309 public static function fetchRevision( $title ) {
310 return self
::fetchFromConds(
312 array( 'rev_id=page_latest',
313 'page_namespace' => $title->getNamespace(),
314 'page_title' => $title->getDBkey() )
319 * Given a set of conditions, return a ResultWrapper
320 * which will return matching database rows with the
321 * fields necessary to build Revision objects.
323 * @param $db DatabaseBase
324 * @param $conditions Array
325 * @param $flags integer (optional)
326 * @return ResultWrapper
328 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
329 $fields = array_merge(
330 self
::selectFields(),
331 self
::selectPageFields(),
332 self
::selectUserFields()
334 $options = array( 'LIMIT' => 1 );
335 if ( $flags & self
::FOR_UPDATE
) {
336 $options[] = 'FOR UPDATE';
339 array( 'revision', 'page', 'user' ),
344 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
349 * Return the value of a select() JOIN conds array for the user table.
350 * This will get user table rows for logged-in users.
354 public static function userJoinCond() {
355 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
359 * Return the value of a select() page conds array for the paeg table.
360 * This will assure that the revision(s) are not orphaned from live pages.
364 public static function pageJoinCond() {
365 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
369 * Return the list of revision fields that should be selected to create
373 public static function selectFields() {
391 * Return the list of text fields that should be selected to read the
395 public static function selectTextFields() {
403 * Return the list of page fields that should be selected from page table
406 public static function selectPageFields() {
418 * Return the list of user fields that should be selected from user table
421 public static function selectUserFields() {
422 return array( 'user_name' );
426 * Do a batched query to get the parent revision lengths
427 * @param $db DatabaseBase
428 * @param $revIds Array
431 public static function getParentLengths( $db, array $revIds ) {
434 return $revLens; // empty
436 wfProfileIn( __METHOD__
);
437 $res = $db->select( 'revision',
438 array( 'rev_id', 'rev_len' ),
439 array( 'rev_id' => $revIds ),
441 foreach ( $res as $row ) {
442 $revLens[$row->rev_id
] = $row->rev_len
;
444 wfProfileOut( __METHOD__
);
451 * @param $row Mixed: either a database row or an array
454 function __construct( $row ) {
455 if( is_object( $row ) ) {
456 $this->mId
= intval( $row->rev_id
);
457 $this->mPage
= intval( $row->rev_page
);
458 $this->mTextId
= intval( $row->rev_text_id
);
459 $this->mComment
= $row->rev_comment
;
460 $this->mUser
= intval( $row->rev_user
);
461 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
462 $this->mTimestamp
= $row->rev_timestamp
;
463 $this->mDeleted
= intval( $row->rev_deleted
);
465 if( !isset( $row->rev_parent_id
) ) {
466 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
468 $this->mParentId
= intval( $row->rev_parent_id
);
471 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
474 $this->mSize
= intval( $row->rev_len
);
477 if ( !isset( $row->rev_sha1
) ) {
480 $this->mSha1
= $row->rev_sha1
;
483 if( isset( $row->page_latest
) ) {
484 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
485 $this->mTitle
= Title
::newFromRow( $row );
487 $this->mCurrent
= false;
488 $this->mTitle
= null;
491 // Lazy extraction...
493 if( isset( $row->old_text
) ) {
494 $this->mTextRow
= $row;
496 // 'text' table row entry will be lazy-loaded
497 $this->mTextRow
= null;
500 // Use user_name for users and rev_user_text for IPs...
501 $this->mUserText
= null; // lazy load if left null
502 if ( $this->mUser
== 0 ) {
503 $this->mUserText
= $row->rev_user_text
; // IP user
504 } elseif ( isset( $row->user_name
) ) {
505 $this->mUserText
= $row->user_name
; // logged-in user
507 $this->mOrigUserText
= $row->rev_user_text
;
508 } elseif( is_array( $row ) ) {
509 // Build a new revision to be saved...
510 global $wgUser; // ugh
512 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
513 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
514 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
515 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
516 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
517 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
518 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
519 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
520 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
521 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
522 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
524 // Enforce spacing trimming on supplied text
525 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
526 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
527 $this->mTextRow
= null;
529 $this->mTitle
= null; # Load on demand if needed
530 $this->mCurrent
= false;
531 # If we still have no length, see it we have the text to figure it out
532 if ( !$this->mSize
) {
533 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
536 if ( $this->mSha1
=== null ) {
537 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
540 throw new MWException( 'Revision constructor passed invalid row format.' );
542 $this->mUnpatrolled
= null;
548 * @return Integer|null
550 public function getId() {
555 * Set the revision ID
560 public function setId( $id ) {
567 * @return Integer|null
569 public function getTextId() {
570 return $this->mTextId
;
574 * Get parent revision ID (the original previous page revision)
576 * @return Integer|null
578 public function getParentId() {
579 return $this->mParentId
;
583 * Returns the length of the text in this revision, or null if unknown.
585 * @return Integer|null
587 public function getSize() {
592 * Returns the base36 sha1 of the text in this revision, or null if unknown.
594 * @return String|null
596 public function getSha1() {
601 * Returns the title of the page associated with this entry or null.
603 * Will do a query, when title is not set and id is given.
607 public function getTitle() {
608 if( isset( $this->mTitle
) ) {
609 return $this->mTitle
;
611 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL
612 $dbr = wfGetDB( DB_SLAVE
);
613 $row = $dbr->selectRow(
614 array( 'page', 'revision' ),
615 self
::selectPageFields(),
616 array( 'page_id=rev_page',
617 'rev_id' => $this->mId
),
620 $this->mTitle
= Title
::newFromRow( $row );
623 return $this->mTitle
;
627 * Set the title of the revision
629 * @param $title Title
631 public function setTitle( $title ) {
632 $this->mTitle
= $title;
638 * @return Integer|null
640 public function getPage() {
645 * Fetch revision's user id if it's available to the specified audience.
646 * If the specified audience does not have access to it, zero will be
649 * @param $audience Integer: one of:
650 * Revision::FOR_PUBLIC to be displayed to all users
651 * Revision::FOR_THIS_USER to be displayed to the given user
652 * Revision::RAW get the ID regardless of permissions
653 * @param $user User object to check for, only if FOR_THIS_USER is passed
654 * to the $audience parameter
657 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
658 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
660 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
668 * Fetch revision's user id without regard for the current user's permissions
672 public function getRawUser() {
677 * Fetch revision's username if it's available to the specified audience.
678 * If the specified audience does not have access to the username, an
679 * empty string will be returned.
681 * @param $audience Integer: one of:
682 * Revision::FOR_PUBLIC to be displayed to all users
683 * Revision::FOR_THIS_USER to be displayed to the given user
684 * Revision::RAW get the text regardless of permissions
685 * @param $user User object to check for, only if FOR_THIS_USER is passed
686 * to the $audience parameter
689 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
690 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
692 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
695 return $this->getRawUserText();
700 * Fetch revision's username without regard for view restrictions
704 public function getRawUserText() {
705 if ( $this->mUserText
=== null ) {
706 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
707 if ( $this->mUserText
=== false ) {
708 # This shouldn't happen, but it can if the wiki was recovered
709 # via importing revs and there is no user table entry yet.
710 $this->mUserText
= $this->mOrigUserText
;
713 return $this->mUserText
;
717 * Fetch revision comment if it's available to the specified audience.
718 * If the specified audience does not have access to the comment, an
719 * empty string will be returned.
721 * @param $audience Integer: one of:
722 * Revision::FOR_PUBLIC to be displayed to all users
723 * Revision::FOR_THIS_USER to be displayed to the given user
724 * Revision::RAW get the text regardless of permissions
725 * @param $user User object to check for, only if FOR_THIS_USER is passed
726 * to the $audience parameter
729 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
730 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
732 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
735 return $this->mComment
;
740 * Fetch revision comment without regard for the current user's permissions
744 public function getRawComment() {
745 return $this->mComment
;
751 public function isMinor() {
752 return (bool)$this->mMinorEdit
;
756 * @return Integer rcid of the unpatrolled row, zero if there isn't one
758 public function isUnpatrolled() {
759 if( $this->mUnpatrolled
!== null ) {
760 return $this->mUnpatrolled
;
762 $dbr = wfGetDB( DB_SLAVE
);
763 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
765 array( // Add redundant user,timestamp condition so we can use the existing index
766 'rc_user_text' => $this->getRawUserText(),
767 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
768 'rc_this_oldid' => $this->getId(),
773 return (int)$this->mUnpatrolled
;
777 * @param $field int one of DELETED_* bitfield constants
781 public function isDeleted( $field ) {
782 return ( $this->mDeleted
& $field ) == $field;
786 * Get the deletion bitfield of the revision
790 public function getVisibility() {
791 return (int)$this->mDeleted
;
795 * Fetch revision text if it's available to the specified audience.
796 * If the specified audience does not have the ability to view this
797 * revision, an empty string will be returned.
799 * @param $audience Integer: one of:
800 * Revision::FOR_PUBLIC to be displayed to all users
801 * Revision::FOR_THIS_USER to be displayed to the given user
802 * Revision::RAW get the text regardless of permissions
803 * @param $user User object to check for, only if FOR_THIS_USER is passed
804 * to the $audience parameter
807 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
808 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
810 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
813 return $this->getRawText();
818 * Alias for getText(Revision::FOR_THIS_USER)
820 * @deprecated since 1.17
823 public function revText() {
824 wfDeprecated( __METHOD__
, '1.17' );
825 return $this->getText( self
::FOR_THIS_USER
);
829 * Fetch revision text without regard for view restrictions
833 public function getRawText() {
834 if( is_null( $this->mText
) ) {
835 // Revision text is immutable. Load on demand:
836 $this->mText
= $this->loadText();
844 public function getTimestamp() {
845 return wfTimestamp( TS_MW
, $this->mTimestamp
);
851 public function isCurrent() {
852 return $this->mCurrent
;
856 * Get previous revision for this title
858 * @return Revision or null
860 public function getPrevious() {
861 if( $this->getTitle() ) {
862 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
864 return self
::newFromTitle( $this->getTitle(), $prev );
871 * Get next revision for this title
873 * @return Revision or null
875 public function getNext() {
876 if( $this->getTitle() ) {
877 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
879 return self
::newFromTitle( $this->getTitle(), $next );
886 * Get previous revision Id for this page_id
887 * This is used to populate rev_parent_id on save
889 * @param $db DatabaseBase
892 private function getPreviousRevisionId( $db ) {
893 if( is_null( $this->mPage
) ) {
896 # Use page_latest if ID is not given
898 $prevId = $db->selectField( 'page', 'page_latest',
899 array( 'page_id' => $this->mPage
),
902 $prevId = $db->selectField( 'revision', 'rev_id',
903 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
905 array( 'ORDER BY' => 'rev_id DESC' ) );
907 return intval( $prevId );
911 * Get revision text associated with an old or archive row
912 * $row is usually an object from wfFetchRow(), both the flags and the text
913 * field must be included
915 * @param $row Object: the text data
916 * @param $prefix String: table prefix (default 'old_')
917 * @return String: text the text requested or false on failure
919 public static function getRevisionText( $row, $prefix = 'old_' ) {
920 wfProfileIn( __METHOD__
);
923 $textField = $prefix . 'text';
924 $flagsField = $prefix . 'flags';
926 if( isset( $row->$flagsField ) ) {
927 $flags = explode( ',', $row->$flagsField );
932 if( isset( $row->$textField ) ) {
933 $text = $row->$textField;
935 wfProfileOut( __METHOD__
);
939 # Use external methods for external objects, text in table is URL-only then
940 if ( in_array( 'external', $flags ) ) {
942 $parts = explode( '://', $url, 2 );
943 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
944 wfProfileOut( __METHOD__
);
947 $text = ExternalStore
::fetchFromURL( $url );
950 // If the text was fetched without an error, convert it
951 if ( $text !== false ) {
952 if( in_array( 'gzip', $flags ) ) {
953 # Deal with optional compression of archived pages.
954 # This can be done periodically via maintenance/compressOld.php, and
955 # as pages are saved if $wgCompressRevisions is set.
956 $text = gzinflate( $text );
959 if( in_array( 'object', $flags ) ) {
960 # Generic compressed storage
961 $obj = unserialize( $text );
962 if ( !is_object( $obj ) ) {
964 wfProfileOut( __METHOD__
);
967 $text = $obj->getText();
970 global $wgLegacyEncoding;
971 if( $text !== false && $wgLegacyEncoding
972 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
974 # Old revisions kept around in a legacy encoding?
975 # Upconvert on demand.
976 # ("utf8" checked for compatibility with some broken
977 # conversion scripts 2008-12-30)
979 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
982 wfProfileOut( __METHOD__
);
987 * If $wgCompressRevisions is enabled, we will compress data.
988 * The input string is modified in place.
989 * Return value is the flags field: contains 'gzip' if the
990 * data is compressed, and 'utf-8' if we're saving in UTF-8
993 * @param $text Mixed: reference to a text
996 public static function compressRevisionText( &$text ) {
997 global $wgCompressRevisions;
1000 # Revisions not marked this way will be converted
1001 # on load if $wgLegacyCharset is set in the future.
1004 if( $wgCompressRevisions ) {
1005 if( function_exists( 'gzdeflate' ) ) {
1006 $text = gzdeflate( $text );
1009 wfDebug( __METHOD__
. " -- no zlib support, not compressing\n" );
1012 return implode( ',', $flags );
1016 * Insert a new revision into the database, returning the new revision ID
1017 * number on success and dies horribly on failure.
1019 * @param $dbw DatabaseBase: (master connection)
1022 public function insertOn( $dbw ) {
1023 global $wgDefaultExternalStore;
1025 wfProfileIn( __METHOD__
);
1027 $data = $this->mText
;
1028 $flags = self
::compressRevisionText( $data );
1030 # Write to external storage if required
1031 if( $wgDefaultExternalStore ) {
1032 // Store and get the URL
1033 $data = ExternalStore
::insertToDefault( $data );
1035 throw new MWException( "Unable to store text to external storage" );
1040 $flags .= 'external';
1043 # Record the text (or external storage URL) to the text table
1044 if( !isset( $this->mTextId
) ) {
1045 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1046 $dbw->insert( 'text',
1048 'old_id' => $old_id,
1049 'old_text' => $data,
1050 'old_flags' => $flags,
1053 $this->mTextId
= $dbw->insertId();
1056 if ( $this->mComment
=== null ) $this->mComment
= "";
1058 # Record the edit in revisions
1059 $rev_id = isset( $this->mId
)
1061 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1062 $dbw->insert( 'revision',
1064 'rev_id' => $rev_id,
1065 'rev_page' => $this->mPage
,
1066 'rev_text_id' => $this->mTextId
,
1067 'rev_comment' => $this->mComment
,
1068 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1069 'rev_user' => $this->mUser
,
1070 'rev_user_text' => $this->mUserText
,
1071 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1072 'rev_deleted' => $this->mDeleted
,
1073 'rev_len' => $this->mSize
,
1074 'rev_parent_id' => is_null( $this->mParentId
)
1075 ?
$this->getPreviousRevisionId( $dbw )
1077 'rev_sha1' => is_null( $this->mSha1
)
1078 ? self
::base36Sha1( $this->mText
)
1083 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1085 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1087 wfProfileOut( __METHOD__
);
1092 * Get the base 36 SHA-1 value for a string of text
1093 * @param $text String
1096 public static function base36Sha1( $text ) {
1097 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1101 * Lazy-load the revision's text.
1102 * Currently hardcoded to the 'text' table storage engine.
1106 protected function loadText() {
1107 wfProfileIn( __METHOD__
);
1109 // Caching may be beneficial for massive use of external storage
1110 global $wgRevisionCacheExpiry, $wgMemc;
1111 $textId = $this->getTextId();
1112 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1113 if( $wgRevisionCacheExpiry ) {
1114 $text = $wgMemc->get( $key );
1115 if( is_string( $text ) ) {
1116 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1117 wfProfileOut( __METHOD__
);
1122 // If we kept data for lazy extraction, use it now...
1123 if ( isset( $this->mTextRow
) ) {
1124 $row = $this->mTextRow
;
1125 $this->mTextRow
= null;
1131 // Text data is immutable; check slaves first.
1132 $dbr = wfGetDB( DB_SLAVE
);
1133 $row = $dbr->selectRow( 'text',
1134 array( 'old_text', 'old_flags' ),
1135 array( 'old_id' => $this->getTextId() ),
1139 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1140 // Possible slave lag!
1141 $dbw = wfGetDB( DB_MASTER
);
1142 $row = $dbw->selectRow( 'text',
1143 array( 'old_text', 'old_flags' ),
1144 array( 'old_id' => $this->getTextId() ),
1148 $text = self
::getRevisionText( $row );
1150 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1151 if( $wgRevisionCacheExpiry && $text !== false ) {
1152 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1155 wfProfileOut( __METHOD__
);
1161 * Create a new null-revision for insertion into a page's
1162 * history. This will not re-save the text, but simply refer
1163 * to the text from the previous version.
1165 * Such revisions can for instance identify page rename
1166 * operations and other such meta-modifications.
1168 * @param $dbw DatabaseBase
1169 * @param $pageId Integer: ID number of the page to read from
1170 * @param $summary String: revision's summary
1171 * @param $minor Boolean: whether the revision should be considered as minor
1172 * @return Revision|null on error
1174 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1175 wfProfileIn( __METHOD__
);
1177 $current = $dbw->selectRow(
1178 array( 'page', 'revision' ),
1179 array( 'page_latest', 'page_namespace', 'page_title',
1180 'rev_text_id', 'rev_len', 'rev_sha1' ),
1182 'page_id' => $pageId,
1183 'page_latest=rev_id',
1188 $revision = new Revision( array(
1190 'comment' => $summary,
1191 'minor_edit' => $minor,
1192 'text_id' => $current->rev_text_id
,
1193 'parent_id' => $current->page_latest
,
1194 'len' => $current->rev_len
,
1195 'sha1' => $current->rev_sha1
1197 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1202 wfProfileOut( __METHOD__
);
1207 * Determine if the current user is allowed to view a particular
1208 * field of this revision, if it's marked as deleted.
1210 * @param $field Integer:one of self::DELETED_TEXT,
1211 * self::DELETED_COMMENT,
1212 * self::DELETED_USER
1213 * @param $user User object to check, or null to use $wgUser
1216 public function userCan( $field, User
$user = null ) {
1217 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1221 * Determine if the current user is allowed to view a particular
1222 * field of this revision, if it's marked as deleted. This is used
1223 * by various classes to avoid duplication.
1225 * @param $bitfield Integer: current field
1226 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1227 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1228 * self::DELETED_USER = File::DELETED_USER
1229 * @param $user User object to check, or null to use $wgUser
1232 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1233 if( $bitfield & $field ) { // aspect is deleted
1234 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1235 $permission = 'suppressrevision';
1236 } elseif ( $field & self
::DELETED_TEXT
) {
1237 $permission = 'deletedtext';
1239 $permission = 'deletedhistory';
1241 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1242 if ( $user === null ) {
1246 return $user->isAllowed( $permission );
1253 * Get rev_timestamp from rev_id, without loading the rest of the row
1255 * @param $title Title
1256 * @param $id Integer
1259 static function getTimestampFromId( $title, $id ) {
1260 $dbr = wfGetDB( DB_SLAVE
);
1261 // Casting fix for DB2
1265 $conds = array( 'rev_id' => $id );
1266 $conds['rev_page'] = $title->getArticleID();
1267 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1268 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1269 # Not in slave, try master
1270 $dbw = wfGetDB( DB_MASTER
);
1271 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1273 return wfTimestamp( TS_MW
, $timestamp );
1277 * Get count of revisions per page...not very efficient
1279 * @param $db DatabaseBase
1280 * @param $id Integer: page id
1283 static function countByPageId( $db, $id ) {
1284 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1285 array( 'rev_page' => $id ), __METHOD__
);
1287 return $row->revCount
;
1293 * Get count of revisions per page...not very efficient
1295 * @param $db DatabaseBase
1296 * @param $title Title
1299 static function countByTitle( $db, $title ) {
1300 $id = $title->getArticleID();
1302 return self
::countByPageId( $db, $id );