Revert r106545 and pass a null variable by ref (also updated the documentation) so...
[mediawiki.git] / includes / Revision.php
blob3e06cfa8cc9046e868e8a6d3cc92adeabd2a102b
1 <?php
3 /**
4 * @todo document
5 */
6 class Revision {
7 protected $mId;
8 protected $mPage;
9 protected $mUserText;
10 protected $mOrigUserText;
11 protected $mUser;
12 protected $mMinorEdit;
13 protected $mTimestamp;
14 protected $mDeleted;
15 protected $mSize;
16 protected $mSha1;
17 protected $mParentId;
18 protected $mComment;
19 protected $mText;
20 protected $mTextRow;
21 protected $mTitle;
22 protected $mCurrent;
24 const DELETED_TEXT = 1;
25 const DELETED_COMMENT = 2;
26 const DELETED_USER = 4;
27 const DELETED_RESTRICTED = 8;
28 // Convenience field
29 const SUPPRESSED_USER = 12;
30 // Audience options for Revision::getText()
31 const FOR_PUBLIC = 1;
32 const FOR_THIS_USER = 2;
33 const RAW = 3;
35 /**
36 * Load a page revision from a given revision ID number.
37 * Returns null if no such revision can be found.
39 * @param $id Integer
40 * @return Revision or null
42 public static function newFromId( $id ) {
43 return Revision::newFromConds( array( 'rev_id' => intval( $id ) ) );
46 /**
47 * Load either the current, or a specified, revision
48 * that's attached to a given title. If not attached
49 * to that title, will return null.
51 * @param $title Title
52 * @param $id Integer (optional)
53 * @return Revision or null
55 public static function newFromTitle( $title, $id = 0 ) {
56 $conds = array(
57 'page_namespace' => $title->getNamespace(),
58 'page_title' => $title->getDBkey()
60 if ( $id ) {
61 // Use the specified ID
62 $conds['rev_id'] = $id;
63 } elseif ( wfGetLB()->getServerCount() > 1 ) {
64 // Get the latest revision ID from the master
65 $dbw = wfGetDB( DB_MASTER );
66 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
67 if ( $latest === false ) {
68 return null; // page does not exist
70 $conds['rev_id'] = $latest;
71 } else {
72 // Use a join to get the latest revision
73 $conds[] = 'rev_id=page_latest';
75 return Revision::newFromConds( $conds );
78 /**
79 * Load either the current, or a specified, revision
80 * that's attached to a given page ID.
81 * Returns null if no such revision can be found.
83 * @param $revId Integer
84 * @param $pageId Integer (optional)
85 * @return Revision or null
87 public static function newFromPageId( $pageId, $revId = 0 ) {
88 $conds = array( 'page_id' => $pageId );
89 if ( $revId ) {
90 $conds['rev_id'] = $revId;
91 } elseif ( wfGetLB()->getServerCount() > 1 ) {
92 // Get the latest revision ID from the master
93 $dbw = wfGetDB( DB_MASTER );
94 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
95 if ( $latest === false ) {
96 return null; // page does not exist
98 $conds['rev_id'] = $latest;
99 } else {
100 $conds[] = 'rev_id = page_latest';
102 return Revision::newFromConds( $conds );
106 * Make a fake revision object from an archive table row. This is queried
107 * for permissions or even inserted (as in Special:Undelete)
108 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
110 * @param $row
111 * @param $overrides array
113 * @return Revision
115 public static function newFromArchiveRow( $row, $overrides = array() ) {
116 $attribs = $overrides + array(
117 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
118 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
119 'comment' => $row->ar_comment,
120 'user' => $row->ar_user,
121 'user_text' => $row->ar_user_text,
122 'timestamp' => $row->ar_timestamp,
123 'minor_edit' => $row->ar_minor_edit,
124 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
125 'deleted' => $row->ar_deleted,
126 'len' => $row->ar_len,
127 'sha1' => isset( $row->ar_sha1 ) ? $row->ar_sha1 : null,
129 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
130 // Pre-1.5 ar_text row
131 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
132 if ( $attribs['text'] === false ) {
133 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
136 return new self( $attribs );
140 * @since 1.19
142 * @param $row
143 * @return Revision
145 public static function newFromRow( $row ) {
146 return new self( $row );
150 * Load a page revision from a given revision ID number.
151 * Returns null if no such revision can be found.
153 * @param $db DatabaseBase
154 * @param $id Integer
155 * @return Revision or null
157 public static function loadFromId( $db, $id ) {
158 return Revision::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
162 * Load either the current, or a specified, revision
163 * that's attached to a given page. If not attached
164 * to that page, will return null.
166 * @param $db DatabaseBase
167 * @param $pageid Integer
168 * @param $id Integer
169 * @return Revision or null
171 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
172 $conds = array( 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) );
173 if( $id ) {
174 $conds['rev_id'] = intval( $id );
175 } else {
176 $conds[] = 'rev_id=page_latest';
178 return Revision::loadFromConds( $db, $conds );
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 $title Title
188 * @param $id Integer
189 * @return Revision or null
191 public static function loadFromTitle( $db, $title, $id = 0 ) {
192 if( $id ) {
193 $matchId = intval( $id );
194 } else {
195 $matchId = 'page_latest';
197 return Revision::loadFromConds( $db,
198 array( "rev_id=$matchId",
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDBkey() )
205 * Load the revision for the given title with the given timestamp.
206 * WARNING: Timestamps may in some circumstances not be unique,
207 * so this isn't the best key to use.
209 * @param $db DatabaseBase
210 * @param $title Title
211 * @param $timestamp String
212 * @return Revision or null
214 public static function loadFromTimestamp( $db, $title, $timestamp ) {
215 return Revision::loadFromConds( $db,
216 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
217 'page_namespace' => $title->getNamespace(),
218 'page_title' => $title->getDBkey() )
223 * Given a set of conditions, fetch a revision.
225 * @param $conditions Array
226 * @return Revision or null
228 public static function newFromConds( $conditions ) {
229 $db = wfGetDB( DB_SLAVE );
230 $rev = Revision::loadFromConds( $db, $conditions );
231 if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
232 $dbw = wfGetDB( DB_MASTER );
233 $rev = Revision::loadFromConds( $dbw, $conditions );
235 return $rev;
239 * Given a set of conditions, fetch a revision from
240 * the given database connection.
242 * @param $db DatabaseBase
243 * @param $conditions Array
244 * @return Revision or null
246 private static function loadFromConds( $db, $conditions ) {
247 $res = Revision::fetchFromConds( $db, $conditions );
248 if( $res ) {
249 $row = $res->fetchObject();
250 if( $row ) {
251 $ret = new Revision( $row );
252 return $ret;
255 $ret = null;
256 return $ret;
260 * Return a wrapper for a series of database rows to
261 * fetch all of a given page's revisions in turn.
262 * Each row can be fed to the constructor to get objects.
264 * @param $title Title
265 * @return ResultWrapper
267 public static function fetchRevision( $title ) {
268 return Revision::fetchFromConds(
269 wfGetDB( DB_SLAVE ),
270 array( 'rev_id=page_latest',
271 'page_namespace' => $title->getNamespace(),
272 'page_title' => $title->getDBkey() )
277 * Given a set of conditions, return a ResultWrapper
278 * which will return matching database rows with the
279 * fields necessary to build Revision objects.
281 * @param $db DatabaseBase
282 * @param $conditions Array
283 * @return ResultWrapper
285 private static function fetchFromConds( $db, $conditions ) {
286 $fields = array_merge(
287 self::selectFields(),
288 self::selectPageFields(),
289 self::selectUserFields()
291 return $db->select(
292 array( 'revision', 'page', 'user' ),
293 $fields,
294 $conditions,
295 __METHOD__,
296 array( 'LIMIT' => 1 ),
297 array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
302 * Return the value of a select() JOIN conds array for the user table.
303 * This will get user table rows for logged-in users.
304 * @return Array
306 public static function userJoinCond() {
307 return array( 'LEFT JOIN', array( 'rev_user != 0', 'user_id = rev_user' ) );
311 * Return the value of a select() page conds array for the paeg table.
312 * This will assure that the revision(s) are not orphaned from live pages.
313 * @return Array
315 public static function pageJoinCond() {
316 return array( 'INNER JOIN', array( 'page_id = rev_page' ) );
320 * Return the list of revision fields that should be selected to create
321 * a new revision.
323 public static function selectFields() {
324 return array(
325 'rev_id',
326 'rev_page',
327 'rev_text_id',
328 'rev_timestamp',
329 'rev_comment',
330 'rev_user_text',
331 'rev_user',
332 'rev_minor_edit',
333 'rev_deleted',
334 'rev_len',
335 'rev_parent_id',
336 'rev_sha1'
341 * Return the list of text fields that should be selected to read the
342 * revision text
344 public static function selectTextFields() {
345 return array(
346 'old_text',
347 'old_flags'
352 * Return the list of page fields that should be selected from page table
354 public static function selectPageFields() {
355 return array(
356 'page_namespace',
357 'page_title',
358 'page_latest'
363 * Return the list of user fields that should be selected from user table
365 public static function selectUserFields() {
366 return array( 'user_name' );
370 * Constructor
372 * @param $row Mixed: either a database row or an array
373 * @access private
375 function __construct( $row ) {
376 if( is_object( $row ) ) {
377 $this->mId = intval( $row->rev_id );
378 $this->mPage = intval( $row->rev_page );
379 $this->mTextId = intval( $row->rev_text_id );
380 $this->mComment = $row->rev_comment;
381 $this->mUser = intval( $row->rev_user );
382 $this->mMinorEdit = intval( $row->rev_minor_edit );
383 $this->mTimestamp = $row->rev_timestamp;
384 $this->mDeleted = intval( $row->rev_deleted );
386 if( !isset( $row->rev_parent_id ) ) {
387 $this->mParentId = is_null( $row->rev_parent_id ) ? null : 0;
388 } else {
389 $this->mParentId = intval( $row->rev_parent_id );
392 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) ) {
393 $this->mSize = null;
394 } else {
395 $this->mSize = intval( $row->rev_len );
398 if ( !isset( $row->rev_sha1 ) ) {
399 $this->mSha1 = null;
400 } else {
401 $this->mSha1 = $row->rev_sha1;
404 if( isset( $row->page_latest ) ) {
405 $this->mCurrent = ( $row->rev_id == $row->page_latest );
406 $this->mTitle = Title::newFromRow( $row );
407 } else {
408 $this->mCurrent = false;
409 $this->mTitle = null;
412 // Lazy extraction...
413 $this->mText = null;
414 if( isset( $row->old_text ) ) {
415 $this->mTextRow = $row;
416 } else {
417 // 'text' table row entry will be lazy-loaded
418 $this->mTextRow = null;
421 // Use user_name for users and rev_user_text for IPs...
422 $this->mUserText = null; // lazy load if left null
423 if ( $this->mUser == 0 ) {
424 $this->mUserText = $row->rev_user_text; // IP user
425 } elseif ( isset( $row->user_name ) ) {
426 $this->mUserText = $row->user_name; // logged-in user
428 $this->mOrigUserText = $row->rev_user_text;
429 } elseif( is_array( $row ) ) {
430 // Build a new revision to be saved...
431 global $wgUser; // ugh
433 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
434 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
435 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
436 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
437 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
438 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
439 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
440 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
441 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
442 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
443 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
445 // Enforce spacing trimming on supplied text
446 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
447 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
448 $this->mTextRow = null;
450 $this->mTitle = null; # Load on demand if needed
451 $this->mCurrent = false;
452 # If we still have no length, see it we have the text to figure it out
453 if ( !$this->mSize ) {
454 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
456 # Same for sha1
457 if ( $this->mSha1 === null ) {
458 $this->mSha1 = is_null( $this->mText ) ? null : self::base36Sha1( $this->mText );
460 } else {
461 throw new MWException( 'Revision constructor passed invalid row format.' );
463 $this->mUnpatrolled = null;
467 * Get revision ID
469 * @return Integer
471 public function getId() {
472 return $this->mId;
476 * Get text row ID
478 * @return Integer
480 public function getTextId() {
481 return $this->mTextId;
485 * Get parent revision ID (the original previous page revision)
487 * @return Integer
489 public function getParentId() {
490 return $this->mParentId;
494 * Returns the length of the text in this revision, or null if unknown.
496 * @return Integer
498 public function getSize() {
499 return $this->mSize;
503 * Returns the base36 sha1 of the text in this revision, or null if unknown.
505 * @return String
507 public function getSha1() {
508 return $this->mSha1;
512 * Returns the title of the page associated with this entry.
514 * @return Title
516 public function getTitle() {
517 if( isset( $this->mTitle ) ) {
518 return $this->mTitle;
520 $dbr = wfGetDB( DB_SLAVE );
521 $row = $dbr->selectRow(
522 array( 'page', 'revision' ),
523 array( 'page_namespace', 'page_title' ),
524 array( 'page_id=rev_page',
525 'rev_id' => $this->mId ),
526 'Revision::getTitle' );
527 if( $row ) {
528 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
530 return $this->mTitle;
534 * Set the title of the revision
536 * @param $title Title
538 public function setTitle( $title ) {
539 $this->mTitle = $title;
543 * Get the page ID
545 * @return Integer
547 public function getPage() {
548 return $this->mPage;
552 * Fetch revision's user id if it's available to the specified audience.
553 * If the specified audience does not have access to it, zero will be
554 * returned.
556 * @param $audience Integer: one of:
557 * Revision::FOR_PUBLIC to be displayed to all users
558 * Revision::FOR_THIS_USER to be displayed to $wgUser
559 * Revision::RAW get the ID regardless of permissions
560 * @param $user User object to check for, only if FOR_THIS_USER is passed
561 * to the $audience parameter
562 * @return Integer
564 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
565 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
566 return 0;
567 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
568 return 0;
569 } else {
570 return $this->mUser;
575 * Fetch revision's user id without regard for the current user's permissions
577 * @return String
579 public function getRawUser() {
580 return $this->mUser;
584 * Fetch revision's username if it's available to the specified audience.
585 * If the specified audience does not have access to the username, an
586 * empty string will be returned.
588 * @param $audience Integer: one of:
589 * Revision::FOR_PUBLIC to be displayed to all users
590 * Revision::FOR_THIS_USER to be displayed to $wgUser
591 * Revision::RAW get the text regardless of permissions
592 * @param $user User object to check for, only if FOR_THIS_USER is passed
593 * to the $audience parameter
594 * @return string
596 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
597 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
598 return '';
599 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
600 return '';
601 } else {
602 return $this->getRawUserText();
607 * Fetch revision's username without regard for view restrictions
609 * @return String
611 public function getRawUserText() {
612 if ( $this->mUserText === null ) {
613 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
614 if ( $this->mUserText === false ) {
615 # This shouldn't happen, but it can if the wiki was recovered
616 # via importing revs and there is no user table entry yet.
617 $this->mUserText = $this->mOrigUserText;
620 return $this->mUserText;
624 * Fetch revision comment if it's available to the specified audience.
625 * If the specified audience does not have access to the comment, an
626 * empty string will be returned.
628 * @param $audience Integer: one of:
629 * Revision::FOR_PUBLIC to be displayed to all users
630 * Revision::FOR_THIS_USER to be displayed to $wgUser
631 * Revision::RAW get the text regardless of permissions
632 * @param $user User object to check for, only if FOR_THIS_USER is passed
633 * to the $audience parameter
634 * @return String
636 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
637 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
638 return '';
639 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
640 return '';
641 } else {
642 return $this->mComment;
647 * Fetch revision comment without regard for the current user's permissions
649 * @return String
651 public function getRawComment() {
652 return $this->mComment;
656 * @return Boolean
658 public function isMinor() {
659 return (bool)$this->mMinorEdit;
663 * @return Integer rcid of the unpatrolled row, zero if there isn't one
665 public function isUnpatrolled() {
666 if( $this->mUnpatrolled !== null ) {
667 return $this->mUnpatrolled;
669 $dbr = wfGetDB( DB_SLAVE );
670 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
671 'rc_id',
672 array( // Add redundant user,timestamp condition so we can use the existing index
673 'rc_user_text' => $this->getRawUserText(),
674 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
675 'rc_this_oldid' => $this->getId(),
676 'rc_patrolled' => 0
678 __METHOD__
680 return (int)$this->mUnpatrolled;
684 * @param $field int one of DELETED_* bitfield constants
686 * @return Boolean
688 public function isDeleted( $field ) {
689 return ( $this->mDeleted & $field ) == $field;
693 * Get the deletion bitfield of the revision
695 * @return int
697 public function getVisibility() {
698 return (int)$this->mDeleted;
702 * Fetch revision text if it's available to the specified audience.
703 * If the specified audience does not have the ability to view this
704 * revision, an empty string will be returned.
706 * @param $audience Integer: one of:
707 * Revision::FOR_PUBLIC to be displayed to all users
708 * Revision::FOR_THIS_USER to be displayed to $wgUser
709 * Revision::RAW get the text regardless of permissions
710 * @param $user User object to check for, only if FOR_THIS_USER is passed
711 * to the $audience parameter
712 * @return String
714 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
715 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
716 return '';
717 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
718 return '';
719 } else {
720 return $this->getRawText();
725 * Alias for getText(Revision::FOR_THIS_USER)
727 * @deprecated since 1.17
728 * @return String
730 public function revText() {
731 wfDeprecated( __METHOD__, '1.17' );
732 return $this->getText( self::FOR_THIS_USER );
736 * Fetch revision text without regard for view restrictions
738 * @return String
740 public function getRawText() {
741 if( is_null( $this->mText ) ) {
742 // Revision text is immutable. Load on demand:
743 $this->mText = $this->loadText();
745 return $this->mText;
749 * @return String
751 public function getTimestamp() {
752 return wfTimestamp( TS_MW, $this->mTimestamp );
756 * @return Boolean
758 public function isCurrent() {
759 return $this->mCurrent;
763 * Get previous revision for this title
765 * @return Revision or null
767 public function getPrevious() {
768 if( $this->getTitle() ) {
769 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
770 if( $prev ) {
771 return Revision::newFromTitle( $this->getTitle(), $prev );
774 return null;
778 * Get next revision for this title
780 * @return Revision or null
782 public function getNext() {
783 if( $this->getTitle() ) {
784 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
785 if ( $next ) {
786 return Revision::newFromTitle( $this->getTitle(), $next );
789 return null;
793 * Get previous revision Id for this page_id
794 * This is used to populate rev_parent_id on save
796 * @param $db DatabaseBase
797 * @return Integer
799 private function getPreviousRevisionId( $db ) {
800 if( is_null( $this->mPage ) ) {
801 return 0;
803 # Use page_latest if ID is not given
804 if( !$this->mId ) {
805 $prevId = $db->selectField( 'page', 'page_latest',
806 array( 'page_id' => $this->mPage ),
807 __METHOD__ );
808 } else {
809 $prevId = $db->selectField( 'revision', 'rev_id',
810 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
811 __METHOD__,
812 array( 'ORDER BY' => 'rev_id DESC' ) );
814 return intval( $prevId );
818 * Get revision text associated with an old or archive row
819 * $row is usually an object from wfFetchRow(), both the flags and the text
820 * field must be included
822 * @param $row Object: the text data
823 * @param $prefix String: table prefix (default 'old_')
824 * @return String: text the text requested or false on failure
826 public static function getRevisionText( $row, $prefix = 'old_' ) {
827 wfProfileIn( __METHOD__ );
829 # Get data
830 $textField = $prefix . 'text';
831 $flagsField = $prefix . 'flags';
833 if( isset( $row->$flagsField ) ) {
834 $flags = explode( ',', $row->$flagsField );
835 } else {
836 $flags = array();
839 if( isset( $row->$textField ) ) {
840 $text = $row->$textField;
841 } else {
842 wfProfileOut( __METHOD__ );
843 return false;
846 # Use external methods for external objects, text in table is URL-only then
847 if ( in_array( 'external', $flags ) ) {
848 $url = $text;
849 $parts = explode( '://', $url, 2 );
850 if( count( $parts ) == 1 || $parts[1] == '' ) {
851 wfProfileOut( __METHOD__ );
852 return false;
854 $text = ExternalStore::fetchFromURL( $url );
857 // If the text was fetched without an error, convert it
858 if ( $text !== false ) {
859 if( in_array( 'gzip', $flags ) ) {
860 # Deal with optional compression of archived pages.
861 # This can be done periodically via maintenance/compressOld.php, and
862 # as pages are saved if $wgCompressRevisions is set.
863 $text = gzinflate( $text );
866 if( in_array( 'object', $flags ) ) {
867 # Generic compressed storage
868 $obj = unserialize( $text );
869 if ( !is_object( $obj ) ) {
870 // Invalid object
871 wfProfileOut( __METHOD__ );
872 return false;
874 $text = $obj->getText();
877 global $wgLegacyEncoding;
878 if( $text !== false && $wgLegacyEncoding
879 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
881 # Old revisions kept around in a legacy encoding?
882 # Upconvert on demand.
883 # ("utf8" checked for compatibility with some broken
884 # conversion scripts 2008-12-30)
885 global $wgContLang;
886 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
889 wfProfileOut( __METHOD__ );
890 return $text;
894 * If $wgCompressRevisions is enabled, we will compress data.
895 * The input string is modified in place.
896 * Return value is the flags field: contains 'gzip' if the
897 * data is compressed, and 'utf-8' if we're saving in UTF-8
898 * mode.
900 * @param $text Mixed: reference to a text
901 * @return String
903 public static function compressRevisionText( &$text ) {
904 global $wgCompressRevisions;
905 $flags = array();
907 # Revisions not marked this way will be converted
908 # on load if $wgLegacyCharset is set in the future.
909 $flags[] = 'utf-8';
911 if( $wgCompressRevisions ) {
912 if( function_exists( 'gzdeflate' ) ) {
913 $text = gzdeflate( $text );
914 $flags[] = 'gzip';
915 } else {
916 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
919 return implode( ',', $flags );
923 * Insert a new revision into the database, returning the new revision ID
924 * number on success and dies horribly on failure.
926 * @param $dbw DatabaseBase: (master connection)
927 * @return Integer
929 public function insertOn( $dbw ) {
930 global $wgDefaultExternalStore;
932 wfProfileIn( __METHOD__ );
934 $data = $this->mText;
935 $flags = Revision::compressRevisionText( $data );
937 # Write to external storage if required
938 if( $wgDefaultExternalStore ) {
939 // Store and get the URL
940 $data = ExternalStore::insertToDefault( $data );
941 if( !$data ) {
942 throw new MWException( "Unable to store text to external storage" );
944 if( $flags ) {
945 $flags .= ',';
947 $flags .= 'external';
950 # Record the text (or external storage URL) to the text table
951 if( !isset( $this->mTextId ) ) {
952 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
953 $dbw->insert( 'text',
954 array(
955 'old_id' => $old_id,
956 'old_text' => $data,
957 'old_flags' => $flags,
958 ), __METHOD__
960 $this->mTextId = $dbw->insertId();
963 if ( $this->mComment === null ) $this->mComment = "";
965 # Record the edit in revisions
966 $rev_id = isset( $this->mId )
967 ? $this->mId
968 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
969 $dbw->insert( 'revision',
970 array(
971 'rev_id' => $rev_id,
972 'rev_page' => $this->mPage,
973 'rev_text_id' => $this->mTextId,
974 'rev_comment' => $this->mComment,
975 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
976 'rev_user' => $this->mUser,
977 'rev_user_text' => $this->mUserText,
978 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
979 'rev_deleted' => $this->mDeleted,
980 'rev_len' => $this->mSize,
981 'rev_parent_id' => is_null( $this->mParentId )
982 ? $this->getPreviousRevisionId( $dbw )
983 : $this->mParentId,
984 'rev_sha1' => is_null( $this->mSha1 )
985 ? Revision::base36Sha1( $this->mText )
986 : $this->mSha1
987 ), __METHOD__
990 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
992 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
994 wfProfileOut( __METHOD__ );
995 return $this->mId;
999 * Get the base 36 SHA-1 value for a string of text
1000 * @param $text String
1001 * @return String
1003 public static function base36Sha1( $text ) {
1004 return wfBaseConvert( sha1( $text ), 16, 36, 31 );
1008 * Lazy-load the revision's text.
1009 * Currently hardcoded to the 'text' table storage engine.
1011 * @return String
1013 protected function loadText() {
1014 wfProfileIn( __METHOD__ );
1016 // Caching may be beneficial for massive use of external storage
1017 global $wgRevisionCacheExpiry, $wgMemc;
1018 $textId = $this->getTextId();
1019 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
1020 if( $wgRevisionCacheExpiry ) {
1021 $text = $wgMemc->get( $key );
1022 if( is_string( $text ) ) {
1023 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
1024 wfProfileOut( __METHOD__ );
1025 return $text;
1029 // If we kept data for lazy extraction, use it now...
1030 if ( isset( $this->mTextRow ) ) {
1031 $row = $this->mTextRow;
1032 $this->mTextRow = null;
1033 } else {
1034 $row = null;
1037 if( !$row ) {
1038 // Text data is immutable; check slaves first.
1039 $dbr = wfGetDB( DB_SLAVE );
1040 $row = $dbr->selectRow( 'text',
1041 array( 'old_text', 'old_flags' ),
1042 array( 'old_id' => $this->getTextId() ),
1043 __METHOD__ );
1046 if( !$row && wfGetLB()->getServerCount() > 1 ) {
1047 // Possible slave lag!
1048 $dbw = wfGetDB( DB_MASTER );
1049 $row = $dbw->selectRow( 'text',
1050 array( 'old_text', 'old_flags' ),
1051 array( 'old_id' => $this->getTextId() ),
1052 __METHOD__ );
1055 $text = self::getRevisionText( $row );
1057 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
1058 if( $wgRevisionCacheExpiry && $text !== false ) {
1059 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
1062 wfProfileOut( __METHOD__ );
1064 return $text;
1068 * Create a new null-revision for insertion into a page's
1069 * history. This will not re-save the text, but simply refer
1070 * to the text from the previous version.
1072 * Such revisions can for instance identify page rename
1073 * operations and other such meta-modifications.
1075 * @param $dbw DatabaseBase
1076 * @param $pageId Integer: ID number of the page to read from
1077 * @param $summary String: revision's summary
1078 * @param $minor Boolean: whether the revision should be considered as minor
1079 * @return Revision|null on error
1081 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
1082 wfProfileIn( __METHOD__ );
1084 $current = $dbw->selectRow(
1085 array( 'page', 'revision' ),
1086 array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' ),
1087 array(
1088 'page_id' => $pageId,
1089 'page_latest=rev_id',
1091 __METHOD__ );
1093 if( $current ) {
1094 $revision = new Revision( array(
1095 'page' => $pageId,
1096 'comment' => $summary,
1097 'minor_edit' => $minor,
1098 'text_id' => $current->rev_text_id,
1099 'parent_id' => $current->page_latest,
1100 'len' => $current->rev_len,
1101 'sha1' => $current->rev_sha1
1102 ) );
1103 } else {
1104 $revision = null;
1107 wfProfileOut( __METHOD__ );
1108 return $revision;
1112 * Determine if the current user is allowed to view a particular
1113 * field of this revision, if it's marked as deleted.
1115 * @param $field Integer:one of self::DELETED_TEXT,
1116 * self::DELETED_COMMENT,
1117 * self::DELETED_USER
1118 * @param $user User object to check, or null to use $wgUser
1119 * @return Boolean
1121 public function userCan( $field, User $user = null ) {
1122 return self::userCanBitfield( $this->mDeleted, $field, $user );
1126 * Determine if the current user is allowed to view a particular
1127 * field of this revision, if it's marked as deleted. This is used
1128 * by various classes to avoid duplication.
1130 * @param $bitfield Integer: current field
1131 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
1132 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1133 * self::DELETED_USER = File::DELETED_USER
1134 * @param $user User object to check, or null to use $wgUser
1135 * @return Boolean
1137 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
1138 if( $bitfield & $field ) { // aspect is deleted
1139 if ( $bitfield & self::DELETED_RESTRICTED ) {
1140 $permission = 'suppressrevision';
1141 } elseif ( $field & self::DELETED_TEXT ) {
1142 $permission = 'deletedtext';
1143 } else {
1144 $permission = 'deletedhistory';
1146 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1147 if ( $user === null ) {
1148 global $wgUser;
1149 $user = $wgUser;
1151 return $user->isAllowed( $permission );
1152 } else {
1153 return true;
1158 * Get rev_timestamp from rev_id, without loading the rest of the row
1160 * @param $title Title
1161 * @param $id Integer
1162 * @return String
1164 static function getTimestampFromId( $title, $id ) {
1165 $dbr = wfGetDB( DB_SLAVE );
1166 // Casting fix for DB2
1167 if ( $id == '' ) {
1168 $id = 0;
1170 $conds = array( 'rev_id' => $id );
1171 $conds['rev_page'] = $title->getArticleId();
1172 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1173 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1174 # Not in slave, try master
1175 $dbw = wfGetDB( DB_MASTER );
1176 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1178 return wfTimestamp( TS_MW, $timestamp );
1182 * Get count of revisions per page...not very efficient
1184 * @param $db DatabaseBase
1185 * @param $id Integer: page id
1186 * @return Integer
1188 static function countByPageId( $db, $id ) {
1189 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1190 array( 'rev_page' => $id ), __METHOD__ );
1191 if( $row ) {
1192 return $row->revCount;
1194 return 0;
1198 * Get count of revisions per page...not very efficient
1200 * @param $db DatabaseBase
1201 * @param $title Title
1202 * @return Integer
1204 static function countByTitle( $db, $title ) {
1205 $id = $title->getArticleId();
1206 if( $id ) {
1207 return Revision::countByPageId( $db, $id );
1209 return 0;