Rename messages from r90670
[mediawiki.git] / includes / Revision.php
blob849668397056b647848a3b0dff773d9183b5d64b
1 <?php
3 /**
4 * @todo document
5 */
6 class Revision {
7 const DELETED_TEXT = 1;
8 const DELETED_COMMENT = 2;
9 const DELETED_USER = 4;
10 const DELETED_RESTRICTED = 8;
11 // Convenience field
12 const SUPPRESSED_USER = 12;
13 // Audience options for Revision::getText()
14 const FOR_PUBLIC = 1;
15 const FOR_THIS_USER = 2;
16 const RAW = 3;
18 /**
19 * Load a page revision from a given revision ID number.
20 * Returns null if no such revision can be found.
22 * @param $id Integer
23 * @return Revision or null
25 public static function newFromId( $id ) {
26 return Revision::newFromConds(
27 array( 'page_id=rev_page',
28 'rev_id' => intval( $id ) ) );
31 /**
32 * Load either the current, or a specified, revision
33 * that's attached to a given title. If not attached
34 * to that title, will return null.
36 * @param $title Title
37 * @param $id Integer
38 * @return Revision or null
40 public static function newFromTitle( $title, $id = 0 ) {
41 $conds = array(
42 'page_namespace' => $title->getNamespace(),
43 'page_title' => $title->getDBkey()
45 if ( $id ) {
46 // Use the specified ID
47 $conds['rev_id'] = $id;
48 } elseif ( wfGetLB()->getServerCount() > 1 ) {
49 // Get the latest revision ID from the master
50 $dbw = wfGetDB( DB_MASTER );
51 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
52 if ( $latest === false ) {
53 // Page does not exist
54 return null;
56 $conds['rev_id'] = $latest;
57 } else {
58 // Use a join to get the latest revision
59 $conds[] = 'rev_id=page_latest';
61 $conds[] = 'page_id=rev_page';
62 return Revision::newFromConds( $conds );
65 /**
66 * Make a fake revision object from an archive table row. This is queried
67 * for permissions or even inserted (as in Special:Undelete)
68 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
70 * @param $row
71 * @param $overrides array
73 * @return Revision
75 public static function newFromArchiveRow( $row, $overrides = array() ) {
76 $attribs = $overrides + array(
77 'page' => isset( $row->page_id ) ? $row->page_id : null,
78 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
79 'comment' => $row->ar_comment,
80 'user' => $row->ar_user,
81 'user_text' => $row->ar_user_text,
82 'timestamp' => $row->ar_timestamp,
83 'minor_edit' => $row->ar_minor_edit,
84 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
85 'deleted' => $row->ar_deleted,
86 'len' => $row->ar_len);
87 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
88 // Pre-1.5 ar_text row
89 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
90 if ( $attribs['text'] === false ) {
91 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
94 return new self( $attribs );
97 /**
98 * Load a page revision from a given revision ID number.
99 * Returns null if no such revision can be found.
101 * @param $db DatabaseBase
102 * @param $id Integer
103 * @return Revision or null
105 public static function loadFromId( $db, $id ) {
106 return Revision::loadFromConds( $db,
107 array( 'page_id=rev_page',
108 'rev_id' => intval( $id ) ) );
112 * Load either the current, or a specified, revision
113 * that's attached to a given page. If not attached
114 * to that page, will return null.
116 * @param $db DatabaseBase
117 * @param $pageid Integer
118 * @param $id Integer
119 * @return Revision or null
121 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
122 $conds = array( 'page_id=rev_page','rev_page' => intval( $pageid ), 'page_id'=>intval( $pageid ) );
123 if( $id ) {
124 $conds['rev_id'] = intval( $id );
125 } else {
126 $conds[] = 'rev_id=page_latest';
128 return Revision::loadFromConds( $db, $conds );
132 * Load either the current, or a specified, revision
133 * that's attached to a given page. If not attached
134 * to that page, will return null.
136 * @param $db DatabaseBase
137 * @param $title Title
138 * @param $id Integer
139 * @return Revision or null
141 public static function loadFromTitle( $db, $title, $id = 0 ) {
142 if( $id ) {
143 $matchId = intval( $id );
144 } else {
145 $matchId = 'page_latest';
147 return Revision::loadFromConds(
148 $db,
149 array( "rev_id=$matchId",
150 'page_id=rev_page',
151 'page_namespace' => $title->getNamespace(),
152 'page_title' => $title->getDBkey() ) );
156 * Load the revision for the given title with the given timestamp.
157 * WARNING: Timestamps may in some circumstances not be unique,
158 * so this isn't the best key to use.
160 * @param $db DatabaseBase
161 * @param $title Title
162 * @param $timestamp String
163 * @return Revision or null
165 public static function loadFromTimestamp( $db, $title, $timestamp ) {
166 return Revision::loadFromConds(
167 $db,
168 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
169 'page_id=rev_page',
170 'page_namespace' => $title->getNamespace(),
171 'page_title' => $title->getDBkey() ) );
175 * Given a set of conditions, fetch a revision.
177 * @param $conditions Array
178 * @return Revision or null
180 public static function newFromConds( $conditions ) {
181 $db = wfGetDB( DB_SLAVE );
182 $row = Revision::loadFromConds( $db, $conditions );
183 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
184 $dbw = wfGetDB( DB_MASTER );
185 $row = Revision::loadFromConds( $dbw, $conditions );
187 return $row;
191 * Given a set of conditions, fetch a revision from
192 * the given database connection.
194 * @param $db DatabaseBase
195 * @param $conditions Array
196 * @return Revision or null
198 private static function loadFromConds( $db, $conditions ) {
199 $res = Revision::fetchFromConds( $db, $conditions );
200 if( $res ) {
201 $row = $res->fetchObject();
202 $res->free();
203 if( $row ) {
204 $ret = new Revision( $row );
205 return $ret;
208 $ret = null;
209 return $ret;
213 * Return a wrapper for a series of database rows to
214 * fetch all of a given page's revisions in turn.
215 * Each row can be fed to the constructor to get objects.
217 * @param $title Title
218 * @return ResultWrapper
220 public static function fetchRevision( $title ) {
221 return Revision::fetchFromConds(
222 wfGetDB( DB_SLAVE ),
223 array( 'rev_id=page_latest',
224 'page_namespace' => $title->getNamespace(),
225 'page_title' => $title->getDBkey(),
226 'page_id=rev_page' ) );
230 * Given a set of conditions, return a ResultWrapper
231 * which will return matching database rows with the
232 * fields necessary to build Revision objects.
234 * @param $db DatabaseBase
235 * @param $conditions Array
236 * @return ResultWrapper
238 private static function fetchFromConds( $db, $conditions ) {
239 $fields = self::selectFields();
240 $fields[] = 'page_namespace';
241 $fields[] = 'page_title';
242 $fields[] = 'page_latest';
243 return $db->select(
244 array( 'page', 'revision' ),
245 $fields,
246 $conditions,
247 __METHOD__,
248 array( 'LIMIT' => 1 ) );
252 * Return the list of revision fields that should be selected to create
253 * a new revision.
255 public static function selectFields() {
256 return array(
257 'rev_id',
258 'rev_page',
259 'rev_text_id',
260 'rev_timestamp',
261 'rev_comment',
262 'rev_user_text,'.
263 'rev_user',
264 'rev_minor_edit',
265 'rev_deleted',
266 'rev_len',
267 'rev_parent_id'
272 * Return the list of text fields that should be selected to read the
273 * revision text
275 static function selectTextFields() {
276 return array(
277 'old_text',
278 'old_flags'
283 * Return the list of page fields that should be selected from page table
285 static function selectPageFields() {
286 return array(
287 'page_namespace',
288 'page_title',
289 'page_latest'
294 * Constructor
296 * @param $row Mixed: either a database row or an array
297 * @access private
299 function __construct( $row ) {
300 if( is_object( $row ) ) {
301 $this->mId = intval( $row->rev_id );
302 $this->mPage = intval( $row->rev_page );
303 $this->mTextId = intval( $row->rev_text_id );
304 $this->mComment = $row->rev_comment;
305 $this->mUserText = $row->rev_user_text;
306 $this->mUser = intval( $row->rev_user );
307 $this->mMinorEdit = intval( $row->rev_minor_edit );
308 $this->mTimestamp = $row->rev_timestamp;
309 $this->mDeleted = intval( $row->rev_deleted );
311 if( !isset( $row->rev_parent_id ) )
312 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
313 else
314 $this->mParentId = intval( $row->rev_parent_id );
316 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) )
317 $this->mSize = null;
318 else
319 $this->mSize = intval( $row->rev_len );
321 if( isset( $row->page_latest ) ) {
322 $this->mCurrent = ( $row->rev_id == $row->page_latest );
323 $this->mTitle = Title::newFromRow( $row );
324 } else {
325 $this->mCurrent = false;
326 $this->mTitle = null;
329 // Lazy extraction...
330 $this->mText = null;
331 if( isset( $row->old_text ) ) {
332 $this->mTextRow = $row;
333 } else {
334 // 'text' table row entry will be lazy-loaded
335 $this->mTextRow = null;
337 } elseif( is_array( $row ) ) {
338 // Build a new revision to be saved...
339 global $wgUser;
341 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
342 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
343 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
344 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
345 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
346 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
347 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
348 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
349 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
350 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
352 // Enforce spacing trimming on supplied text
353 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
354 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
355 $this->mTextRow = null;
357 $this->mTitle = null; # Load on demand if needed
358 $this->mCurrent = false;
359 # If we still have no len_size, see it we have the text to figure it out
360 if ( !$this->mSize )
361 $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
362 } else {
363 throw new MWException( 'Revision constructor passed invalid row format.' );
365 $this->mUnpatrolled = null;
369 * Get revision ID
371 * @return Integer
373 public function getId() {
374 return $this->mId;
378 * Get text row ID
380 * @return Integer
382 public function getTextId() {
383 return $this->mTextId;
387 * Get parent revision ID (the original previous page revision)
389 * @return Integer
391 public function getParentId() {
392 return $this->mParentId;
396 * Returns the length of the text in this revision, or null if unknown.
398 * @return Integer
400 public function getSize() {
401 return $this->mSize;
405 * Returns the title of the page associated with this entry.
407 * @return Title
409 public function getTitle() {
410 if( isset( $this->mTitle ) ) {
411 return $this->mTitle;
413 $dbr = wfGetDB( DB_SLAVE );
414 $row = $dbr->selectRow(
415 array( 'page', 'revision' ),
416 array( 'page_namespace', 'page_title' ),
417 array( 'page_id=rev_page',
418 'rev_id' => $this->mId ),
419 'Revision::getTitle' );
420 if( $row ) {
421 $this->mTitle = Title::makeTitle( $row->page_namespace,
422 $row->page_title );
424 return $this->mTitle;
428 * Set the title of the revision
430 * @param $title Title
432 public function setTitle( $title ) {
433 $this->mTitle = $title;
437 * Get the page ID
439 * @return Integer
441 public function getPage() {
442 return $this->mPage;
446 * Fetch revision's user id if it's available to the specified audience.
447 * If the specified audience does not have access to it, zero will be
448 * returned.
450 * @param $audience Integer: one of:
451 * Revision::FOR_PUBLIC to be displayed to all users
452 * Revision::FOR_THIS_USER to be displayed to $wgUser
453 * Revision::RAW get the ID regardless of permissions
456 * @return Integer
458 public function getUser( $audience = self::FOR_PUBLIC ) {
459 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
460 return 0;
461 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
462 return 0;
463 } else {
464 return $this->mUser;
469 * Fetch revision's user id without regard for the current user's permissions
471 * @return String
473 public function getRawUser() {
474 return $this->mUser;
478 * Fetch revision's username if it's available to the specified audience.
479 * If the specified audience does not have access to the username, an
480 * empty string will be returned.
482 * @param $audience Integer: one of:
483 * Revision::FOR_PUBLIC to be displayed to all users
484 * Revision::FOR_THIS_USER to be displayed to $wgUser
485 * Revision::RAW get the text regardless of permissions
487 * @return string
489 public function getUserText( $audience = self::FOR_PUBLIC ) {
490 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
491 return '';
492 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
493 return '';
494 } else {
495 return $this->mUserText;
500 * Fetch revision's username without regard for view restrictions
502 * @return String
504 public function getRawUserText() {
505 return $this->mUserText;
509 * Fetch revision comment if it's available to the specified audience.
510 * If the specified audience does not have access to the comment, an
511 * empty string will be returned.
513 * @param $audience Integer: one of:
514 * Revision::FOR_PUBLIC to be displayed to all users
515 * Revision::FOR_THIS_USER to be displayed to $wgUser
516 * Revision::RAW get the text regardless of permissions
518 * @return String
520 function getComment( $audience = self::FOR_PUBLIC ) {
521 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
522 return '';
523 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT ) ) {
524 return '';
525 } else {
526 return $this->mComment;
531 * Fetch revision comment without regard for the current user's permissions
533 * @return String
535 public function getRawComment() {
536 return $this->mComment;
540 * @return Boolean
542 public function isMinor() {
543 return (bool)$this->mMinorEdit;
547 * @return Integer rcid of the unpatrolled row, zero if there isn't one
549 public function isUnpatrolled() {
550 if( $this->mUnpatrolled !== null ) {
551 return $this->mUnpatrolled;
553 $dbr = wfGetDB( DB_SLAVE );
554 $this->mUnpatrolled = $dbr->selectField( 'recentchanges',
555 'rc_id',
556 array( // Add redundant user,timestamp condition so we can use the existing index
557 'rc_user_text' => $this->getRawUserText(),
558 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
559 'rc_this_oldid' => $this->getId(),
560 'rc_patrolled' => 0
562 __METHOD__
564 return (int)$this->mUnpatrolled;
568 * int $field one of DELETED_* bitfield constants
570 * @return Boolean
572 public function isDeleted( $field ) {
573 return ( $this->mDeleted & $field ) == $field;
577 * Get the deletion bitfield of the revision
579 public function getVisibility() {
580 return (int)$this->mDeleted;
584 * Fetch revision text if it's available to the specified audience.
585 * If the specified audience does not have the ability to view this
586 * revision, an 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
593 * @return String
595 public function getText( $audience = self::FOR_PUBLIC ) {
596 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
597 return '';
598 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT ) ) {
599 return '';
600 } else {
601 return $this->getRawText();
606 * Alias for getText(Revision::FOR_THIS_USER)
608 * @deprecated since 1.17
609 * @return String
611 public function revText() {
612 wfDeprecated( __METHOD__ );
613 return $this->getText( self::FOR_THIS_USER );
617 * Fetch revision text without regard for view restrictions
619 * @return String
621 public function getRawText() {
622 if( is_null( $this->mText ) ) {
623 // Revision text is immutable. Load on demand:
624 $this->mText = $this->loadText();
626 return $this->mText;
630 * @return String
632 public function getTimestamp() {
633 return wfTimestamp( TS_MW, $this->mTimestamp );
637 * @return Boolean
639 public function isCurrent() {
640 return $this->mCurrent;
644 * Get previous revision for this title
646 * @return Revision or null
648 public function getPrevious() {
649 if( $this->getTitle() ) {
650 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
651 if( $prev ) {
652 return Revision::newFromTitle( $this->getTitle(), $prev );
655 return null;
659 * Get next revision for this title
661 * @return Revision or null
663 public function getNext() {
664 if( $this->getTitle() ) {
665 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
666 if ( $next ) {
667 return Revision::newFromTitle( $this->getTitle(), $next );
670 return null;
674 * Get previous revision Id for this page_id
675 * This is used to populate rev_parent_id on save
677 * @param $db DatabaseBase
678 * @return Integer
680 private function getPreviousRevisionId( $db ) {
681 if( is_null( $this->mPage ) ) {
682 return 0;
684 # Use page_latest if ID is not given
685 if( !$this->mId ) {
686 $prevId = $db->selectField( 'page', 'page_latest',
687 array( 'page_id' => $this->mPage ),
688 __METHOD__ );
689 } else {
690 $prevId = $db->selectField( 'revision', 'rev_id',
691 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
692 __METHOD__,
693 array( 'ORDER BY' => 'rev_id DESC' ) );
695 return intval( $prevId );
699 * Get revision text associated with an old or archive row
700 * $row is usually an object from wfFetchRow(), both the flags and the text
701 * field must be included
703 * @param $row Object: the text data
704 * @param $prefix String: table prefix (default 'old_')
705 * @return String: text the text requested or false on failure
707 public static function getRevisionText( $row, $prefix = 'old_' ) {
708 wfProfileIn( __METHOD__ );
710 # Get data
711 $textField = $prefix . 'text';
712 $flagsField = $prefix . 'flags';
714 if( isset( $row->$flagsField ) ) {
715 $flags = explode( ',', $row->$flagsField );
716 } else {
717 $flags = array();
720 if( isset( $row->$textField ) ) {
721 $text = $row->$textField;
722 } else {
723 wfProfileOut( __METHOD__ );
724 return false;
727 # Use external methods for external objects, text in table is URL-only then
728 if ( in_array( 'external', $flags ) ) {
729 $url = $text;
730 @list(/* $proto */, $path ) = explode( '://', $url, 2 );
731 if( $path == '' ) {
732 wfProfileOut( __METHOD__ );
733 return false;
735 $text = ExternalStore::fetchFromURL( $url );
738 // If the text was fetched without an error, convert it
739 if ( $text !== false ) {
740 if( in_array( 'gzip', $flags ) ) {
741 # Deal with optional compression of archived pages.
742 # This can be done periodically via maintenance/compressOld.php, and
743 # as pages are saved if $wgCompressRevisions is set.
744 $text = gzinflate( $text );
747 if( in_array( 'object', $flags ) ) {
748 # Generic compressed storage
749 $obj = unserialize( $text );
750 if ( !is_object( $obj ) ) {
751 // Invalid object
752 wfProfileOut( __METHOD__ );
753 return false;
755 $text = $obj->getText();
758 global $wgLegacyEncoding;
759 if( $text !== false && $wgLegacyEncoding
760 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags ) )
762 # Old revisions kept around in a legacy encoding?
763 # Upconvert on demand.
764 # ("utf8" checked for compatibility with some broken
765 # conversion scripts 2008-12-30)
766 global $wgContLang;
767 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
770 wfProfileOut( __METHOD__ );
771 return $text;
775 * If $wgCompressRevisions is enabled, we will compress data.
776 * The input string is modified in place.
777 * Return value is the flags field: contains 'gzip' if the
778 * data is compressed, and 'utf-8' if we're saving in UTF-8
779 * mode.
781 * @param $text Mixed: reference to a text
782 * @return String
784 public static function compressRevisionText( &$text ) {
785 global $wgCompressRevisions;
786 $flags = array();
788 # Revisions not marked this way will be converted
789 # on load if $wgLegacyCharset is set in the future.
790 $flags[] = 'utf-8';
792 if( $wgCompressRevisions ) {
793 if( function_exists( 'gzdeflate' ) ) {
794 $text = gzdeflate( $text );
795 $flags[] = 'gzip';
796 } else {
797 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
800 return implode( ',', $flags );
804 * Insert a new revision into the database, returning the new revision ID
805 * number on success and dies horribly on failure.
807 * @param $dbw DatabaseBase: (master connection)
808 * @return Integer
810 public function insertOn( $dbw ) {
811 global $wgDefaultExternalStore;
813 wfProfileIn( __METHOD__ );
815 $data = $this->mText;
816 $flags = Revision::compressRevisionText( $data );
818 # Write to external storage if required
819 if( $wgDefaultExternalStore ) {
820 // Store and get the URL
821 $data = ExternalStore::insertToDefault( $data );
822 if( !$data ) {
823 throw new MWException( "Unable to store text to external storage" );
825 if( $flags ) {
826 $flags .= ',';
828 $flags .= 'external';
831 # Record the text (or external storage URL) to the text table
832 if( !isset( $this->mTextId ) ) {
833 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
834 $dbw->insert( 'text',
835 array(
836 'old_id' => $old_id,
837 'old_text' => $data,
838 'old_flags' => $flags,
839 ), __METHOD__
841 $this->mTextId = $dbw->insertId();
844 if ( $this->mComment === null ) $this->mComment = "";
846 # Record the edit in revisions
847 $rev_id = isset( $this->mId )
848 ? $this->mId
849 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
850 $dbw->insert( 'revision',
851 array(
852 'rev_id' => $rev_id,
853 'rev_page' => $this->mPage,
854 'rev_text_id' => $this->mTextId,
855 'rev_comment' => $this->mComment,
856 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
857 'rev_user' => $this->mUser,
858 'rev_user_text' => $this->mUserText,
859 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
860 'rev_deleted' => $this->mDeleted,
861 'rev_len' => $this->mSize,
862 'rev_parent_id' => is_null($this->mParentId) ?
863 $this->getPreviousRevisionId( $dbw ) : $this->mParentId
864 ), __METHOD__
867 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
869 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
871 wfProfileOut( __METHOD__ );
872 return $this->mId;
876 * Lazy-load the revision's text.
877 * Currently hardcoded to the 'text' table storage engine.
879 * @return String
881 protected function loadText() {
882 wfProfileIn( __METHOD__ );
884 // Caching may be beneficial for massive use of external storage
885 global $wgRevisionCacheExpiry, $wgMemc;
886 $textId = $this->getTextId();
887 $key = wfMemcKey( 'revisiontext', 'textid', $textId );
888 if( $wgRevisionCacheExpiry ) {
889 $text = $wgMemc->get( $key );
890 if( is_string( $text ) ) {
891 wfDebug( __METHOD__ . ": got id $textId from cache\n" );
892 wfProfileOut( __METHOD__ );
893 return $text;
897 // If we kept data for lazy extraction, use it now...
898 if ( isset( $this->mTextRow ) ) {
899 $row = $this->mTextRow;
900 $this->mTextRow = null;
901 } else {
902 $row = null;
905 if( !$row ) {
906 // Text data is immutable; check slaves first.
907 $dbr = wfGetDB( DB_SLAVE );
908 $row = $dbr->selectRow( 'text',
909 array( 'old_text', 'old_flags' ),
910 array( 'old_id' => $this->getTextId() ),
911 __METHOD__ );
914 if( !$row && wfGetLB()->getServerCount() > 1 ) {
915 // Possible slave lag!
916 $dbw = wfGetDB( DB_MASTER );
917 $row = $dbw->selectRow( 'text',
918 array( 'old_text', 'old_flags' ),
919 array( 'old_id' => $this->getTextId() ),
920 __METHOD__ );
923 $text = self::getRevisionText( $row );
925 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
926 if( $wgRevisionCacheExpiry && $text !== false ) {
927 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
930 wfProfileOut( __METHOD__ );
932 return $text;
936 * Create a new null-revision for insertion into a page's
937 * history. This will not re-save the text, but simply refer
938 * to the text from the previous version.
940 * Such revisions can for instance identify page rename
941 * operations and other such meta-modifications.
943 * @param $dbw DatabaseBase
944 * @param $pageId Integer: ID number of the page to read from
945 * @param $summary String: revision's summary
946 * @param $minor Boolean: whether the revision should be considered as minor
947 * @return Revision|null on error
949 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
950 wfProfileIn( __METHOD__ );
952 $current = $dbw->selectRow(
953 array( 'page', 'revision' ),
954 array( 'page_latest', 'rev_text_id', 'rev_len' ),
955 array(
956 'page_id' => $pageId,
957 'page_latest=rev_id',
959 __METHOD__ );
961 if( $current ) {
962 $revision = new Revision( array(
963 'page' => $pageId,
964 'comment' => $summary,
965 'minor_edit' => $minor,
966 'text_id' => $current->rev_text_id,
967 'parent_id' => $current->page_latest,
968 'len' => $current->rev_len
969 ) );
970 } else {
971 $revision = null;
974 wfProfileOut( __METHOD__ );
975 return $revision;
979 * Determine if the current user is allowed to view a particular
980 * field of this revision, if it's marked as deleted.
982 * @param $field Integer:one of self::DELETED_TEXT,
983 * self::DELETED_COMMENT,
984 * self::DELETED_USER
985 * @return Boolean
987 public function userCan( $field ) {
988 return self::userCanBitfield( $this->mDeleted, $field );
992 * Determine if the current user is allowed to view a particular
993 * field of this revision, if it's marked as deleted. This is used
994 * by various classes to avoid duplication.
996 * @param $bitfield Integer: current field
997 * @param $field Integer: one of self::DELETED_TEXT = File::DELETED_FILE,
998 * self::DELETED_COMMENT = File::DELETED_COMMENT,
999 * self::DELETED_USER = File::DELETED_USER
1000 * @return Boolean
1002 public static function userCanBitfield( $bitfield, $field ) {
1003 if( $bitfield & $field ) { // aspect is deleted
1004 global $wgUser;
1005 if ( $bitfield & self::DELETED_RESTRICTED ) {
1006 $permission = 'suppressrevision';
1007 } elseif ( $field & self::DELETED_TEXT ) {
1008 $permission = 'deletedtext';
1009 } else {
1010 $permission = 'deletedhistory';
1012 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
1013 return $wgUser->isAllowed( $permission );
1014 } else {
1015 return true;
1020 * Get rev_timestamp from rev_id, without loading the rest of the row
1022 * @param $title Title
1023 * @param $id Integer
1024 * @return String
1026 static function getTimestampFromId( $title, $id ) {
1027 $dbr = wfGetDB( DB_SLAVE );
1028 // Casting fix for DB2
1029 if ( $id == '' ) {
1030 $id = 0;
1032 $conds = array( 'rev_id' => $id );
1033 $conds['rev_page'] = $title->getArticleId();
1034 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1035 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
1036 # Not in slave, try master
1037 $dbw = wfGetDB( DB_MASTER );
1038 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1040 return wfTimestamp( TS_MW, $timestamp );
1044 * Get count of revisions per page...not very efficient
1046 * @param $db DatabaseBase
1047 * @param $id Integer: page id
1048 * @return Integer
1050 static function countByPageId( $db, $id ) {
1051 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
1052 array( 'rev_page' => $id ), __METHOD__ );
1053 if( $row ) {
1054 return $row->revCount;
1056 return 0;
1060 * Get count of revisions per page...not very efficient
1062 * @param $db DatabaseBase
1063 * @param $title Title
1064 * @return Integer
1066 static function countByTitle( $db, $title ) {
1067 $id = $title->getArticleId();
1068 if( $id ) {
1069 return Revision::countByPageId( $db, $id );
1071 return 0;
1076 * Aliases for backwards compatibility with 1.6
1078 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
1079 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
1080 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
1081 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );