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
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 Revision::getText()
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 * @return Revision or null
62 public static function newFromId( $id ) {
63 return Revision
::newFromConds( array( 'rev_id' => intval( $id ) ) );
67 * Load either the current, or a specified, revision
68 * that's attached to a given title. If not attached
69 * to that title, will return null.
72 * @param $id Integer (optional)
73 * @return Revision or null
75 public static function newFromTitle( $title, $id = 0 ) {
77 'page_namespace' => $title->getNamespace(),
78 'page_title' => $title->getDBkey()
81 // Use the specified ID
82 $conds['rev_id'] = $id;
83 } elseif ( wfGetLB()->getServerCount() > 1 ) {
84 // Get the latest revision ID from the master
85 $dbw = wfGetDB( DB_MASTER
);
86 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
87 if ( $latest === false ) {
88 return null; // page does not exist
90 $conds['rev_id'] = $latest;
92 // Use a join to get the latest revision
93 $conds[] = 'rev_id=page_latest';
95 return Revision
::newFromConds( $conds );
99 * Load either the current, or a specified, revision
100 * that's attached to a given page ID.
101 * Returns null if no such revision can be found.
103 * @param $revId Integer
104 * @param $pageId Integer (optional)
105 * @return Revision or null
107 public static function newFromPageId( $pageId, $revId = 0 ) {
108 $conds = array( 'page_id' => $pageId );
110 $conds['rev_id'] = $revId;
111 } elseif ( wfGetLB()->getServerCount() > 1 ) {
112 // Get the latest revision ID from the master
113 $dbw = wfGetDB( DB_MASTER
);
114 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__
);
115 if ( $latest === false ) {
116 return null; // page does not exist
118 $conds['rev_id'] = $latest;
120 $conds[] = 'rev_id = page_latest';
122 return Revision
::newFromConds( $conds );
126 * Make a fake revision object from an archive table row. This is queried
127 * for permissions or even inserted (as in Special:Undelete)
128 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
131 * @param $overrides array
135 public static function newFromArchiveRow( $row, $overrides = array() ) {
136 $attribs = $overrides +
array(
137 'page' => isset( $row->ar_page_id
) ?
$row->ar_page_id
: null,
138 'id' => isset( $row->ar_rev_id
) ?
$row->ar_rev_id
: null,
139 'comment' => $row->ar_comment
,
140 'user' => $row->ar_user
,
141 'user_text' => $row->ar_user_text
,
142 'timestamp' => $row->ar_timestamp
,
143 'minor_edit' => $row->ar_minor_edit
,
144 'text_id' => isset( $row->ar_text_id
) ?
$row->ar_text_id
: null,
145 'deleted' => $row->ar_deleted
,
146 'len' => $row->ar_len
,
147 'sha1' => isset( $row->ar_sha1
) ?
$row->ar_sha1
: null,
149 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
150 // Pre-1.5 ar_text row
151 $attribs['text'] = self
::getRevisionText( $row, 'ar_' );
152 if ( $attribs['text'] === false ) {
153 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
156 return new self( $attribs );
165 public static function newFromRow( $row ) {
166 return new self( $row );
170 * Load a page revision from a given revision ID number.
171 * Returns null if no such revision can be found.
173 * @param $db DatabaseBase
175 * @return Revision or null
177 public static function loadFromId( $db, $id ) {
178 return Revision
::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
182 * Load either the current, or a specified, revision
183 * that's attached to a given page. If not attached
184 * to that page, will return null.
186 * @param $db DatabaseBase
187 * @param $pageid Integer
189 * @return Revision or null
191 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
192 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
194 $conds['rev_id'] = intval( $id );
196 $conds[] = 'rev_id=page_latest';
198 return Revision
::loadFromConds( $db, $conds );
202 * Load either the current, or a specified, revision
203 * that's attached to a given page. If not attached
204 * to that page, will return null.
206 * @param $db DatabaseBase
207 * @param $title Title
209 * @return Revision or null
211 public static function loadFromTitle( $db, $title, $id = 0 ) {
213 $matchId = intval( $id );
215 $matchId = 'page_latest';
217 return Revision
::loadFromConds( $db,
218 array( "rev_id=$matchId",
219 'page_namespace' => $title->getNamespace(),
220 'page_title' => $title->getDBkey() )
225 * Load the revision for the given title with the given timestamp.
226 * WARNING: Timestamps may in some circumstances not be unique,
227 * so this isn't the best key to use.
229 * @param $db DatabaseBase
230 * @param $title Title
231 * @param $timestamp String
232 * @return Revision or null
234 public static function loadFromTimestamp( $db, $title, $timestamp ) {
235 return Revision
::loadFromConds( $db,
236 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
237 'page_namespace' => $title->getNamespace(),
238 'page_title' => $title->getDBkey() )
243 * Given a set of conditions, fetch a revision.
245 * @param $conditions Array
246 * @return Revision or null
248 public static function newFromConds( $conditions ) {
249 $db = wfGetDB( DB_SLAVE
);
250 $rev = Revision
::loadFromConds( $db, $conditions );
251 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
252 $dbw = wfGetDB( DB_MASTER
);
253 $rev = Revision
::loadFromConds( $dbw, $conditions );
259 * Given a set of conditions, fetch a revision from
260 * the given database connection.
262 * @param $db DatabaseBase
263 * @param $conditions Array
264 * @return Revision or null
266 private static function loadFromConds( $db, $conditions ) {
267 $res = Revision
::fetchFromConds( $db, $conditions );
269 $row = $res->fetchObject();
271 $ret = new Revision( $row );
280 * Return a wrapper for a series of database rows to
281 * fetch all of a given page's revisions in turn.
282 * Each row can be fed to the constructor to get objects.
284 * @param $title Title
285 * @return ResultWrapper
287 public static function fetchRevision( $title ) {
288 return Revision
::fetchFromConds(
290 array( 'rev_id=page_latest',
291 'page_namespace' => $title->getNamespace(),
292 'page_title' => $title->getDBkey() )
297 * Given a set of conditions, return a ResultWrapper
298 * which will return matching database rows with the
299 * fields necessary to build Revision objects.
301 * @param $db DatabaseBase
302 * @param $conditions Array
303 * @return ResultWrapper
305 private static function fetchFromConds( $db, $conditions ) {
306 $fields = array_merge(
307 self
::selectFields(),
308 self
::selectPageFields(),
309 self
::selectUserFields()
312 array( 'revision', 'page', 'user' ),
316 array( 'LIMIT' => 1 ),
317 array( 'page' => self
::pageJoinCond(), 'user' => self
::userJoinCond() )
322 * Return the value of a select() JOIN conds array for the user table.
323 * This will get user table rows for logged-in users.
327 public static function userJoinCond() {
328 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
332 * Return the value of a select() page conds array for the paeg table.
333 * This will assure that the revision(s) are not orphaned from live pages.
337 public static function pageJoinCond() {
338 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
342 * Return the list of revision fields that should be selected to create
346 public static function selectFields() {
364 * Return the list of text fields that should be selected to read the
368 public static function selectTextFields() {
376 * Return the list of page fields that should be selected from page table
379 public static function selectPageFields() {
391 * Return the list of user fields that should be selected from user table
394 public static function selectUserFields() {
395 return array( 'user_name' );
401 * @param $row Mixed: either a database row or an array
404 function __construct( $row ) {
405 if( is_object( $row ) ) {
406 $this->mId
= intval( $row->rev_id
);
407 $this->mPage
= intval( $row->rev_page
);
408 $this->mTextId
= intval( $row->rev_text_id
);
409 $this->mComment
= $row->rev_comment
;
410 $this->mUser
= intval( $row->rev_user
);
411 $this->mMinorEdit
= intval( $row->rev_minor_edit
);
412 $this->mTimestamp
= $row->rev_timestamp
;
413 $this->mDeleted
= intval( $row->rev_deleted
);
415 if( !isset( $row->rev_parent_id
) ) {
416 $this->mParentId
= is_null( $row->rev_parent_id
) ?
null : 0;
418 $this->mParentId
= intval( $row->rev_parent_id
);
421 if( !isset( $row->rev_len
) ||
is_null( $row->rev_len
) ) {
424 $this->mSize
= intval( $row->rev_len
);
427 if ( !isset( $row->rev_sha1
) ) {
430 $this->mSha1
= $row->rev_sha1
;
433 if( isset( $row->page_latest
) ) {
434 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
435 $this->mTitle
= Title
::newFromRow( $row );
437 $this->mCurrent
= false;
438 $this->mTitle
= null;
441 // Lazy extraction...
443 if( isset( $row->old_text
) ) {
444 $this->mTextRow
= $row;
446 // 'text' table row entry will be lazy-loaded
447 $this->mTextRow
= null;
450 // Use user_name for users and rev_user_text for IPs...
451 $this->mUserText
= null; // lazy load if left null
452 if ( $this->mUser
== 0 ) {
453 $this->mUserText
= $row->rev_user_text
; // IP user
454 } elseif ( isset( $row->user_name
) ) {
455 $this->mUserText
= $row->user_name
; // logged-in user
457 $this->mOrigUserText
= $row->rev_user_text
;
458 } elseif( is_array( $row ) ) {
459 // Build a new revision to be saved...
460 global $wgUser; // ugh
462 $this->mId
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
463 $this->mPage
= isset( $row['page'] ) ?
intval( $row['page'] ) : null;
464 $this->mTextId
= isset( $row['text_id'] ) ?
intval( $row['text_id'] ) : null;
465 $this->mUserText
= isset( $row['user_text'] ) ?
strval( $row['user_text'] ) : $wgUser->getName();
466 $this->mUser
= isset( $row['user'] ) ?
intval( $row['user'] ) : $wgUser->getId();
467 $this->mMinorEdit
= isset( $row['minor_edit'] ) ?
intval( $row['minor_edit'] ) : 0;
468 $this->mTimestamp
= isset( $row['timestamp'] ) ?
strval( $row['timestamp'] ) : wfTimestampNow();
469 $this->mDeleted
= isset( $row['deleted'] ) ?
intval( $row['deleted'] ) : 0;
470 $this->mSize
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
471 $this->mParentId
= isset( $row['parent_id'] ) ?
intval( $row['parent_id'] ) : null;
472 $this->mSha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
474 // Enforce spacing trimming on supplied text
475 $this->mComment
= isset( $row['comment'] ) ?
trim( strval( $row['comment'] ) ) : null;
476 $this->mText
= isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
477 $this->mTextRow
= null;
479 $this->mTitle
= null; # Load on demand if needed
480 $this->mCurrent
= false;
481 # If we still have no length, see it we have the text to figure it out
482 if ( !$this->mSize
) {
483 $this->mSize
= is_null( $this->mText
) ?
null : strlen( $this->mText
);
486 if ( $this->mSha1
=== null ) {
487 $this->mSha1
= is_null( $this->mText
) ?
null : self
::base36Sha1( $this->mText
);
490 throw new MWException( 'Revision constructor passed invalid row format.' );
492 $this->mUnpatrolled
= null;
498 * @return Integer|null
500 public function getId() {
505 * Set the revision ID
510 public function setId( $id ) {
517 * @return Integer|null
519 public function getTextId() {
520 return $this->mTextId
;
524 * Get parent revision ID (the original previous page revision)
526 * @return Integer|null
528 public function getParentId() {
529 return $this->mParentId
;
533 * Returns the length of the text in this revision, or null if unknown.
535 * @return Integer|null
537 public function getSize() {
542 * Returns the base36 sha1 of the text in this revision, or null if unknown.
544 * @return String|null
546 public function getSha1() {
551 * Returns the title of the page associated with this entry or null.
553 * Will do a query, when title is not set and id is given.
557 public function getTitle() {
558 if( isset( $this->mTitle
) ) {
559 return $this->mTitle
;
561 if( !is_null( $this->mId
) ) { //rev_id is defined as NOT NULL
562 $dbr = wfGetDB( DB_SLAVE
);
563 $row = $dbr->selectRow(
564 array( 'page', 'revision' ),
565 self
::selectPageFields(),
566 array( 'page_id=rev_page',
567 'rev_id' => $this->mId
),
570 $this->mTitle
= Title
::newFromRow( $row );
573 return $this->mTitle
;
577 * Set the title of the revision
579 * @param $title Title
581 public function setTitle( $title ) {
582 $this->mTitle
= $title;
588 * @return Integer|null
590 public function getPage() {
595 * Fetch revision's user id if it's available to the specified audience.
596 * If the specified audience does not have access to it, zero will be
599 * @param $audience Integer: one of:
600 * Revision::FOR_PUBLIC to be displayed to all users
601 * Revision::FOR_THIS_USER to be displayed to the given user
602 * Revision::RAW get the ID regardless of permissions
603 * @param $user User object to check for, only if FOR_THIS_USER is passed
604 * to the $audience parameter
607 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
608 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
610 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
618 * Fetch revision's user id without regard for the current user's permissions
622 public function getRawUser() {
627 * Fetch revision's username if it's available to the specified audience.
628 * If the specified audience does not have access to the username, an
629 * empty string will be returned.
631 * @param $audience Integer: one of:
632 * Revision::FOR_PUBLIC to be displayed to all users
633 * Revision::FOR_THIS_USER to be displayed to the given user
634 * Revision::RAW get the text regardless of permissions
635 * @param $user User object to check for, only if FOR_THIS_USER is passed
636 * to the $audience parameter
639 public function getUserText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
640 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_USER
) ) {
642 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_USER
, $user ) ) {
645 return $this->getRawUserText();
650 * Fetch revision's username without regard for view restrictions
654 public function getRawUserText() {
655 if ( $this->mUserText
=== null ) {
656 $this->mUserText
= User
::whoIs( $this->mUser
); // load on demand
657 if ( $this->mUserText
=== false ) {
658 # This shouldn't happen, but it can if the wiki was recovered
659 # via importing revs and there is no user table entry yet.
660 $this->mUserText
= $this->mOrigUserText
;
663 return $this->mUserText
;
667 * Fetch revision comment if it's available to the specified audience.
668 * If the specified audience does not have access to the comment, an
669 * empty string will be returned.
671 * @param $audience Integer: one of:
672 * Revision::FOR_PUBLIC to be displayed to all users
673 * Revision::FOR_THIS_USER to be displayed to the given user
674 * Revision::RAW get the text regardless of permissions
675 * @param $user User object to check for, only if FOR_THIS_USER is passed
676 * to the $audience parameter
679 function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
680 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
682 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_COMMENT
, $user ) ) {
685 return $this->mComment
;
690 * Fetch revision comment without regard for the current user's permissions
694 public function getRawComment() {
695 return $this->mComment
;
701 public function isMinor() {
702 return (bool)$this->mMinorEdit
;
706 * @return Integer rcid of the unpatrolled row, zero if there isn't one
708 public function isUnpatrolled() {
709 if( $this->mUnpatrolled
!== null ) {
710 return $this->mUnpatrolled
;
712 $dbr = wfGetDB( DB_SLAVE
);
713 $this->mUnpatrolled
= $dbr->selectField( 'recentchanges',
715 array( // Add redundant user,timestamp condition so we can use the existing index
716 'rc_user_text' => $this->getRawUserText(),
717 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
718 'rc_this_oldid' => $this->getId(),
723 return (int)$this->mUnpatrolled
;
727 * @param $field int one of DELETED_* bitfield constants
731 public function isDeleted( $field ) {
732 return ( $this->mDeleted
& $field ) == $field;
736 * Get the deletion bitfield of the revision
740 public function getVisibility() {
741 return (int)$this->mDeleted
;
745 * Fetch revision text if it's available to the specified audience.
746 * If the specified audience does not have the ability to view this
747 * revision, an empty string will be returned.
749 * @param $audience Integer: one of:
750 * Revision::FOR_PUBLIC to be displayed to all users
751 * Revision::FOR_THIS_USER to be displayed to the given user
752 * Revision::RAW get the text regardless of permissions
753 * @param $user User object to check for, only if FOR_THIS_USER is passed
754 * to the $audience parameter
757 public function getText( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
758 if( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_TEXT
) ) {
760 } elseif( $audience == self
::FOR_THIS_USER
&& !$this->userCan( self
::DELETED_TEXT
, $user ) ) {
763 return $this->getRawText();
768 * Alias for getText(Revision::FOR_THIS_USER)
770 * @deprecated since 1.17
773 public function revText() {
774 wfDeprecated( __METHOD__
, '1.17' );
775 return $this->getText( self
::FOR_THIS_USER
);
779 * Fetch revision text without regard for view restrictions
783 public function getRawText() {
784 if( is_null( $this->mText
) ) {
785 // Revision text is immutable. Load on demand:
786 $this->mText
= $this->loadText();
794 public function getTimestamp() {
795 return wfTimestamp( TS_MW
, $this->mTimestamp
);
801 public function isCurrent() {
802 return $this->mCurrent
;
806 * Get previous revision for this title
808 * @return Revision or null
810 public function getPrevious() {
811 if( $this->getTitle() ) {
812 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
814 return Revision
::newFromTitle( $this->getTitle(), $prev );
821 * Get next revision for this title
823 * @return Revision or null
825 public function getNext() {
826 if( $this->getTitle() ) {
827 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
829 return Revision
::newFromTitle( $this->getTitle(), $next );
836 * Get previous revision Id for this page_id
837 * This is used to populate rev_parent_id on save
839 * @param $db DatabaseBase
842 private function getPreviousRevisionId( $db ) {
843 if( is_null( $this->mPage
) ) {
846 # Use page_latest if ID is not given
848 $prevId = $db->selectField( 'page', 'page_latest',
849 array( 'page_id' => $this->mPage
),
852 $prevId = $db->selectField( 'revision', 'rev_id',
853 array( 'rev_page' => $this->mPage
, 'rev_id < ' . $this->mId
),
855 array( 'ORDER BY' => 'rev_id DESC' ) );
857 return intval( $prevId );
861 * Get revision text associated with an old or archive row
862 * $row is usually an object from wfFetchRow(), both the flags and the text
863 * field must be included
865 * @param $row Object: the text data
866 * @param $prefix String: table prefix (default 'old_')
867 * @return String: text the text requested or false on failure
869 public static function getRevisionText( $row, $prefix = 'old_' ) {
870 wfProfileIn( __METHOD__
);
873 $textField = $prefix . 'text';
874 $flagsField = $prefix . 'flags';
876 if( isset( $row->$flagsField ) ) {
877 $flags = explode( ',', $row->$flagsField );
882 if( isset( $row->$textField ) ) {
883 $text = $row->$textField;
885 wfProfileOut( __METHOD__
);
889 # Use external methods for external objects, text in table is URL-only then
890 if ( in_array( 'external', $flags ) ) {
892 $parts = explode( '://', $url, 2 );
893 if( count( $parts ) == 1 ||
$parts[1] == '' ) {
894 wfProfileOut( __METHOD__
);
897 $text = ExternalStore
::fetchFromURL( $url );
900 // If the text was fetched without an error, convert it
901 if ( $text !== false ) {
902 if( in_array( 'gzip', $flags ) ) {
903 # Deal with optional compression of archived pages.
904 # This can be done periodically via maintenance/compressOld.php, and
905 # as pages are saved if $wgCompressRevisions is set.
906 $text = gzinflate( $text );
909 if( in_array( 'object', $flags ) ) {
910 # Generic compressed storage
911 $obj = unserialize( $text );
912 if ( !is_object( $obj ) ) {
914 wfProfileOut( __METHOD__
);
917 $text = $obj->getText();
920 global $wgLegacyEncoding;
921 if( $text !== false && $wgLegacyEncoding
922 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
924 # Old revisions kept around in a legacy encoding?
925 # Upconvert on demand.
926 # ("utf8" checked for compatibility with some broken
927 # conversion scripts 2008-12-30)
929 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
932 wfProfileOut( __METHOD__
);
937 * If $wgCompressRevisions is enabled, we will compress data.
938 * The input string is modified in place.
939 * Return value is the flags field: contains 'gzip' if the
940 * data is compressed, and 'utf-8' if we're saving in UTF-8
943 * @param $text Mixed: reference to a text
946 public static function compressRevisionText( &$text ) {
947 global $wgCompressRevisions;
950 # Revisions not marked this way will be converted
951 # on load if $wgLegacyCharset is set in the future.
954 if( $wgCompressRevisions ) {
955 if( function_exists( 'gzdeflate' ) ) {
956 $text = gzdeflate( $text );
959 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
962 return implode( ',', $flags );
966 * Insert a new revision into the database, returning the new revision ID
967 * number on success and dies horribly on failure.
969 * @param $dbw DatabaseBase: (master connection)
972 public function insertOn( $dbw ) {
973 global $wgDefaultExternalStore;
975 wfProfileIn( __METHOD__
);
977 $data = $this->mText
;
978 $flags = Revision
::compressRevisionText( $data );
980 # Write to external storage if required
981 if( $wgDefaultExternalStore ) {
982 // Store and get the URL
983 $data = ExternalStore
::insertToDefault( $data );
985 throw new MWException( "Unable to store text to external storage" );
990 $flags .= 'external';
993 # Record the text (or external storage URL) to the text table
994 if( !isset( $this->mTextId
) ) {
995 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
996 $dbw->insert( 'text',
1000 'old_flags' => $flags,
1003 $this->mTextId
= $dbw->insertId();
1006 if ( $this->mComment
=== null ) $this->mComment
= "";
1008 # Record the edit in revisions
1009 $rev_id = isset( $this->mId
)
1011 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1012 $dbw->insert( 'revision',
1014 'rev_id' => $rev_id,
1015 'rev_page' => $this->mPage
,
1016 'rev_text_id' => $this->mTextId
,
1017 'rev_comment' => $this->mComment
,
1018 'rev_minor_edit' => $this->mMinorEdit ?
1 : 0,
1019 'rev_user' => $this->mUser
,
1020 'rev_user_text' => $this->mUserText
,
1021 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp
),
1022 'rev_deleted' => $this->mDeleted
,
1023 'rev_len' => $this->mSize
,
1024 'rev_parent_id' => is_null( $this->mParentId
)
1025 ?
$this->getPreviousRevisionId( $dbw )
1027 'rev_sha1' => is_null( $this->mSha1
)
1028 ? Revision
::base36Sha1( $this->mText
)
1033 $this->mId
= !is_null( $rev_id ) ?
$rev_id : $dbw->insertId();
1035 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
1037 wfProfileOut( __METHOD__
);
1042 * Get the base 36 SHA-1 value for a string of text
1043 * @param $text String
1046 public static function base36Sha1( $text ) {
1047 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1051 * Lazy-load the revision's text.
1052 * Currently hardcoded to the 'text' table storage engine.
1056 protected function loadText() {
1057 wfProfileIn( __METHOD__
);
1059 // Caching may be beneficial for massive use of external storage
1060 global $wgRevisionCacheExpiry, $wgMemc;
1061 $textId = $this->getTextId();
1062 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1063 if( $wgRevisionCacheExpiry ) {
1064 $text = $wgMemc->get( $key );
1065 if( is_string( $text ) ) {
1066 wfDebug( __METHOD__
. ": got id $textId from cache\n" );
1067 wfProfileOut( __METHOD__
);
1072 // If we kept data for lazy extraction, use it now...
1073 if ( isset( $this->mTextRow
) ) {
1074 $row = $this->mTextRow
;
1075 $this->mTextRow
= null;
1081 // Text data is immutable; check slaves first.
1082 $dbr = wfGetDB( DB_SLAVE
);
1083 $row = $dbr->selectRow( 'text',
1084 array( 'old_text', 'old_flags' ),
1085 array( 'old_id' => $this->getTextId() ),
1089 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1090 // Possible slave lag!
1091 $dbw = wfGetDB( DB_MASTER
);
1092 $row = $dbw->selectRow( 'text',
1093 array( 'old_text', 'old_flags' ),
1094 array( 'old_id' => $this->getTextId() ),
1098 $text = self
::getRevisionText( $row );
1100 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1101 if( $wgRevisionCacheExpiry && $text !== false ) {
1102 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1105 wfProfileOut( __METHOD__
);
1111 * Create a new null-revision for insertion into a page's
1112 * history. This will not re-save the text, but simply refer
1113 * to the text from the previous version.
1115 * Such revisions can for instance identify page rename
1116 * operations and other such meta-modifications.
1118 * @param $dbw DatabaseBase
1119 * @param $pageId Integer: ID number of the page to read from
1120 * @param $summary String: revision's summary
1121 * @param $minor Boolean: whether the revision should be considered as minor
1122 * @return Revision|null on error
1124 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1125 wfProfileIn( __METHOD__
);
1127 $current = $dbw->selectRow(
1128 array( 'page', 'revision' ),
1129 array( 'page_latest', 'page_namespace', 'page_title',
1130 'rev_text_id', 'rev_len', 'rev_sha1' ),
1132 'page_id' => $pageId,
1133 'page_latest=rev_id',
1138 $revision = new Revision( array(
1140 'comment' => $summary,
1141 'minor_edit' => $minor,
1142 'text_id' => $current->rev_text_id
,
1143 'parent_id' => $current->page_latest
,
1144 'len' => $current->rev_len
,
1145 'sha1' => $current->rev_sha1
1147 $revision->setTitle( Title
::makeTitle( $current->page_namespace
, $current->page_title
) );
1152 wfProfileOut( __METHOD__
);
1157 * Determine if the current user is allowed to view a particular
1158 * field of this revision, if it's marked as deleted.
1160 * @param $field Integer:one of self::DELETED_TEXT,
1161 * self::DELETED_COMMENT,
1162 * self::DELETED_USER
1163 * @param $user User object to check, or null to use $wgUser
1166 public function userCan( $field, User
$user = null ) {
1167 return self
::userCanBitfield( $this->mDeleted
, $field, $user );
1171 * Determine if the current user is allowed to view a particular
1172 * field of this revision, if it's marked as deleted. This is used
1173 * by various classes to avoid duplication.
1175 * @param $bitfield Integer: current field
1176 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1177 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1178 * self::DELETED_USER = File::DELETED_USER
1179 * @param $user User object to check, or null to use $wgUser
1182 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
1183 if( $bitfield & $field ) { // aspect is deleted
1184 if ( $bitfield & self
::DELETED_RESTRICTED
) {
1185 $permission = 'suppressrevision';
1186 } elseif ( $field & self
::DELETED_TEXT
) {
1187 $permission = 'deletedtext';
1189 $permission = 'deletedhistory';
1191 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1192 if ( $user === null ) {
1196 return $user->isAllowed( $permission );
1203 * Get rev_timestamp from rev_id, without loading the rest of the row
1205 * @param $title Title
1206 * @param $id Integer
1209 static function getTimestampFromId( $title, $id ) {
1210 $dbr = wfGetDB( DB_SLAVE
);
1211 // Casting fix for DB2
1215 $conds = array( 'rev_id' => $id );
1216 $conds['rev_page'] = $title->getArticleID();
1217 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1218 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1219 # Not in slave, try master
1220 $dbw = wfGetDB( DB_MASTER
);
1221 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1223 return wfTimestamp( TS_MW
, $timestamp );
1227 * Get count of revisions per page...not very efficient
1229 * @param $db DatabaseBase
1230 * @param $id Integer: page id
1233 static function countByPageId( $db, $id ) {
1234 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1235 array( 'rev_page' => $id ), __METHOD__
);
1237 return $row->revCount
;
1243 * Get count of revisions per page...not very efficient
1245 * @param $db DatabaseBase
1246 * @param $title Title
1249 static function countByTitle( $db, $title ) {
1250 $id = $title->getArticleID();
1252 return Revision
::countByPageId( $db, $id );