Remove debugging code
[mediawiki.git] / includes / Revision.php
blob857c50f6e995f39c14a598bddf6b2b2bc747bd85
1 <?php
2 /**
3 * @todo document
4 * @file
5 */
7 /**
8 * @todo document
9 */
10 class Revision {
11 const DELETED_TEXT = 1;
12 const DELETED_COMMENT = 2;
13 const DELETED_USER = 4;
14 const DELETED_RESTRICTED = 8;
16 // Audience options for Revision::getText()
17 const FOR_PUBLIC = 1;
18 const FOR_THIS_USER = 2;
19 const RAW = 3;
21 /**
22 * Load a page revision from a given revision ID number.
23 * Returns null if no such revision can be found.
25 * @param int $id
26 * @access public
27 * @static
29 public static function newFromId( $id ) {
30 return Revision::newFromConds(
31 array( 'page_id=rev_page',
32 'rev_id' => intval( $id ) ) );
35 /**
36 * Load either the current, or a specified, revision
37 * that's attached to a given title. If not attached
38 * to that title, will return null.
40 * @param Title $title
41 * @param int $id
42 * @return Revision
44 public static function newFromTitle( $title, $id = 0 ) {
45 $conds = array(
46 'page_namespace' => $title->getNamespace(),
47 'page_title' => $title->getDBkey()
49 if ( $id ) {
50 // Use the specified ID
51 $conds['rev_id'] = $id;
52 } elseif ( wfGetLB()->getServerCount() > 1 ) {
53 // Get the latest revision ID from the master
54 $dbw = wfGetDB( DB_MASTER );
55 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
56 $conds['rev_id'] = $latest;
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 * Load a page revision from a given revision ID number.
67 * Returns null if no such revision can be found.
69 * @param Database $db
70 * @param int $id
71 * @access public
72 * @static
74 public static function loadFromId( $db, $id ) {
75 return Revision::loadFromConds( $db,
76 array( 'page_id=rev_page',
77 'rev_id' => intval( $id ) ) );
80 /**
81 * Load either the current, or a specified, revision
82 * that's attached to a given page. If not attached
83 * to that page, will return null.
85 * @param Database $db
86 * @param int $pageid
87 * @param int $id
88 * @return Revision
89 * @access public
90 * @static
92 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
93 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
94 if( $id ) {
95 $conds['rev_id']=intval($id);
96 } else {
97 $conds[]='rev_id=page_latest';
99 return Revision::loadFromConds( $db, $conds );
103 * Load either the current, or a specified, revision
104 * that's attached to a given page. If not attached
105 * to that page, will return null.
107 * @param Database $db
108 * @param Title $title
109 * @param int $id
110 * @return Revision
111 * @access public
112 * @static
114 public static function loadFromTitle( $db, $title, $id = 0 ) {
115 if( $id ) {
116 $matchId = intval( $id );
117 } else {
118 $matchId = 'page_latest';
120 return Revision::loadFromConds(
121 $db,
122 array( "rev_id=$matchId",
123 'page_id=rev_page',
124 'page_namespace' => $title->getNamespace(),
125 'page_title' => $title->getDBkey() ) );
129 * Load the revision for the given title with the given timestamp.
130 * WARNING: Timestamps may in some circumstances not be unique,
131 * so this isn't the best key to use.
133 * @param Database $db
134 * @param Title $title
135 * @param string $timestamp
136 * @return Revision
137 * @access public
138 * @static
140 public static function loadFromTimestamp( $db, $title, $timestamp ) {
141 return Revision::loadFromConds(
142 $db,
143 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
144 'page_id=rev_page',
145 'page_namespace' => $title->getNamespace(),
146 'page_title' => $title->getDBkey() ) );
150 * Given a set of conditions, fetch a revision.
152 * @param array $conditions
153 * @return Revision
154 * @access private
155 * @static
157 private static function newFromConds( $conditions ) {
158 $db = wfGetDB( DB_SLAVE );
159 $row = Revision::loadFromConds( $db, $conditions );
160 if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
161 $dbw = wfGetDB( DB_MASTER );
162 $row = Revision::loadFromConds( $dbw, $conditions );
164 return $row;
168 * Given a set of conditions, fetch a revision from
169 * the given database connection.
171 * @param Database $db
172 * @param array $conditions
173 * @return Revision
174 * @access private
175 * @static
177 private static function loadFromConds( $db, $conditions ) {
178 $res = Revision::fetchFromConds( $db, $conditions );
179 if( $res ) {
180 $row = $res->fetchObject();
181 $res->free();
182 if( $row ) {
183 $ret = new Revision( $row );
184 return $ret;
187 $ret = null;
188 return $ret;
192 * Return a wrapper for a series of database rows to
193 * fetch all of a given page's revisions in turn.
194 * Each row can be fed to the constructor to get objects.
196 * @param Title $title
197 * @return ResultWrapper
198 * @access public
199 * @static
201 public static function fetchAllRevisions( $title ) {
202 return Revision::fetchFromConds(
203 wfGetDB( DB_SLAVE ),
204 array( 'page_namespace' => $title->getNamespace(),
205 'page_title' => $title->getDBkey(),
206 'page_id=rev_page' ) );
210 * Return a wrapper for a series of database rows to
211 * fetch all of a given page's revisions in turn.
212 * Each row can be fed to the constructor to get objects.
214 * @param Title $title
215 * @return ResultWrapper
216 * @access public
217 * @static
219 public static function fetchRevision( $title ) {
220 return Revision::fetchFromConds(
221 wfGetDB( DB_SLAVE ),
222 array( 'rev_id=page_latest',
223 'page_namespace' => $title->getNamespace(),
224 'page_title' => $title->getDBkey(),
225 'page_id=rev_page' ) );
229 * Given a set of conditions, return a ResultWrapper
230 * which will return matching database rows with the
231 * fields necessary to build Revision objects.
233 * @param Database $db
234 * @param array $conditions
235 * @return ResultWrapper
236 * @access private
237 * @static
239 private static function fetchFromConds( $db, $conditions ) {
240 $fields = self::selectFields();
241 $fields[] = 'page_namespace';
242 $fields[] = 'page_title';
243 $fields[] = 'page_latest';
244 $res = $db->select(
245 array( 'page', 'revision' ),
246 $fields,
247 $conditions,
248 __METHOD__,
249 array( 'LIMIT' => 1 ) );
250 $ret = $db->resultObject( $res );
251 return $ret;
255 * Return the list of revision fields that should be selected to create
256 * a new revision.
258 static function selectFields() {
259 return array(
260 'rev_id',
261 'rev_page',
262 'rev_text_id',
263 'rev_timestamp',
264 'rev_comment',
265 'rev_user_text,'.
266 'rev_user',
267 'rev_minor_edit',
268 'rev_deleted',
269 'rev_len',
270 'rev_parent_id'
275 * Return the list of text fields that should be selected to read the
276 * revision text
278 static function selectTextFields() {
279 return array(
280 'old_text',
281 'old_flags'
285 * Return the list of page fields that should be selected from page table
287 static function selectPageFields() {
288 return array(
289 'page_namespace',
290 'page_title',
291 'page_latest'
296 * @param object $row
297 * @access private
299 function Revision( $row ) {
300 if( is_object( $row ) ) {
301 $this->mId = intval( $row->rev_id );
302 $this->mPage = intval( $row->rev_page );
303 $this->mTextId = intval( $row->rev_text_id );
304 $this->mComment = $row->rev_comment;
305 $this->mUserText = $row->rev_user_text;
306 $this->mUser = intval( $row->rev_user );
307 $this->mMinorEdit = intval( $row->rev_minor_edit );
308 $this->mTimestamp = $row->rev_timestamp;
309 $this->mDeleted = intval( $row->rev_deleted );
311 if( !isset( $row->rev_parent_id ) )
312 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
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::makeTitle( $row->page_namespace,
324 $row->page_title );
325 } else {
326 $this->mCurrent = false;
327 $this->mTitle = null;
330 // Lazy extraction...
331 $this->mText = null;
332 if( isset( $row->old_text ) ) {
333 $this->mTextRow = $row;
334 } else {
335 // 'text' table row entry will be lazy-loaded
336 $this->mTextRow = null;
338 } elseif( is_array( $row ) ) {
339 // Build a new revision to be saved...
340 global $wgUser;
342 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
343 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
344 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
345 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
346 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
347 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
348 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
349 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
350 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
351 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
353 // Enforce spacing trimming on supplied text
354 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
355 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
356 $this->mTextRow = null;
358 $this->mTitle = null; # Load on demand if needed
359 $this->mCurrent = false;
360 # If we still have no len_size, see it we have the text to figure it out
361 if ( !$this->mSize )
362 $this->mSize = is_null($this->mText) ? null : strlen($this->mText);
363 } else {
364 throw new MWException( 'Revision constructor passed invalid row format.' );
368 /**#@+
369 * @access public
373 * Get revision ID
374 * @return int
376 public function getId() {
377 return $this->mId;
381 * Get text row ID
382 * @return int
384 public function getTextId() {
385 return $this->mTextId;
389 * Get parent revision ID (the original previous page revision)
390 * @return int
392 public function getParentId() {
393 return $this->mParentId;
397 * Returns the length of the text in this revision, or null if unknown.
398 * @return int
400 public function getSize() {
401 return $this->mSize;
405 * Returns the title of the page associated with this entry.
406 * @return Title
408 public function getTitle() {
409 if( isset( $this->mTitle ) ) {
410 return $this->mTitle;
412 $dbr = wfGetDB( DB_SLAVE );
413 $row = $dbr->selectRow(
414 array( 'page', 'revision' ),
415 array( 'page_namespace', 'page_title' ),
416 array( 'page_id=rev_page',
417 'rev_id' => $this->mId ),
418 'Revision::getTitle' );
419 if( $row ) {
420 $this->mTitle = Title::makeTitle( $row->page_namespace,
421 $row->page_title );
423 return $this->mTitle;
427 * Set the title of the revision
428 * @param Title $title
430 public function setTitle( $title ) {
431 $this->mTitle = $title;
435 * Get the page ID
436 * @return int
438 public function getPage() {
439 return $this->mPage;
443 * Fetch revision's user id if it's available to the specified audience.
444 * If the specified audience does not have access to it, zero will be
445 * returned.
447 * @param integer $audience One of:
448 * Revision::FOR_PUBLIC to be displayed to all users
449 * Revision::FOR_THIS_USER to be displayed to $wgUser
450 * Revision::RAW get the ID regardless of permissions
453 * @return int
455 public function getUser( $audience = self::FOR_PUBLIC ) {
456 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
457 return 0;
458 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
459 return 0;
460 } else {
461 return $this->mUser;
466 * Fetch revision's user id without regard for the current user's permissions
467 * @return string
469 public function getRawUser() {
470 return $this->mUser;
474 * Fetch revision's username if it's available to the specified audience.
475 * If the specified audience does not have access to the username, an
476 * empty string will be returned.
478 * @param integer $audience One of:
479 * Revision::FOR_PUBLIC to be displayed to all users
480 * Revision::FOR_THIS_USER to be displayed to $wgUser
481 * Revision::RAW get the text regardless of permissions
483 * @return string
485 public function getUserText( $audience = self::FOR_PUBLIC ) {
486 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
487 return "";
488 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
489 return "";
490 } else {
491 return $this->mUserText;
496 * Fetch revision's username without regard for view restrictions
497 * @return string
499 public function getRawUserText() {
500 return $this->mUserText;
504 * Fetch revision comment if it's available to the specified audience.
505 * If the specified audience does not have access to the comment, an
506 * empty string will be returned.
508 * @param integer $audience One of:
509 * Revision::FOR_PUBLIC to be displayed to all users
510 * Revision::FOR_THIS_USER to be displayed to $wgUser
511 * Revision::RAW get the text regardless of permissions
513 * @return string
515 function getComment( $audience = self::FOR_PUBLIC ) {
516 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
517 return "";
518 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT ) ) {
519 return "";
520 } else {
521 return $this->mComment;
526 * Fetch revision comment without regard for the current user's permissions
527 * @return string
529 public function getRawComment() {
530 return $this->mComment;
534 * @return bool
536 public function isMinor() {
537 return (bool)$this->mMinorEdit;
541 * int $field one of DELETED_* bitfield constants
542 * @return bool
544 public function isDeleted( $field ) {
545 return ($this->mDeleted & $field) == $field;
549 * Fetch revision text if it's available to the specified audience.
550 * If the specified audience does not have the ability to view this
551 * revision, an empty string will be returned.
553 * @param integer $audience One of:
554 * Revision::FOR_PUBLIC to be displayed to all users
555 * Revision::FOR_THIS_USER to be displayed to $wgUser
556 * Revision::RAW get the text regardless of permissions
559 * @return string
561 public function getText( $audience = self::FOR_PUBLIC ) {
562 if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
563 return "";
564 } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT ) ) {
565 return "";
566 } else {
567 return $this->getRawText();
572 * Alias for getText(Revision::FOR_THIS_USER)
574 public function revText() {
575 return $this->getText( self::FOR_THIS_USER );
579 * Fetch revision text without regard for view restrictions
580 * @return string
582 public function getRawText() {
583 if( is_null( $this->mText ) ) {
584 // Revision text is immutable. Load on demand:
585 $this->mText = $this->loadText();
587 return $this->mText;
591 * @return string
593 public function getTimestamp() {
594 return wfTimestamp(TS_MW, $this->mTimestamp);
598 * @return bool
600 public function isCurrent() {
601 return $this->mCurrent;
605 * Get previous revision for this title
606 * @return Revision
608 public function getPrevious() {
609 if( $this->getTitle() ) {
610 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
611 if( $prev ) {
612 return Revision::newFromTitle( $this->getTitle(), $prev );
615 return null;
619 * @return Revision
621 public function getNext() {
622 if( $this->getTitle() ) {
623 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
624 if ( $next ) {
625 return Revision::newFromTitle( $this->getTitle(), $next );
628 return null;
632 * Get previous revision Id for this page_id
633 * This is used to populate rev_parent_id on save
634 * @param Database $db
635 * @return int
637 private function getPreviousRevisionId( $db ) {
638 if( is_null($this->mPage) ) {
639 return 0;
641 # Use page_latest if ID is not given
642 if( !$this->mId ) {
643 $prevId = $db->selectField( 'page', 'page_latest',
644 array( 'page_id' => $this->mPage ),
645 __METHOD__ );
646 } else {
647 $prevId = $db->selectField( 'revision', 'rev_id',
648 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
649 __METHOD__,
650 array( 'ORDER BY' => 'rev_id DESC' ) );
652 return intval($prevId);
656 * Get revision text associated with an old or archive row
657 * $row is usually an object from wfFetchRow(), both the flags and the text
658 * field must be included
660 * @param integer $row Id of a row
661 * @param string $prefix table prefix (default 'old_')
662 * @return string $text|false the text requested
664 public static function getRevisionText( $row, $prefix = 'old_' ) {
665 wfProfileIn( __METHOD__ );
667 # Get data
668 $textField = $prefix . 'text';
669 $flagsField = $prefix . 'flags';
671 if( isset( $row->$flagsField ) ) {
672 $flags = explode( ',', $row->$flagsField );
673 } else {
674 $flags = array();
677 if( isset( $row->$textField ) ) {
678 $text = $row->$textField;
679 } else {
680 wfProfileOut( __METHOD__ );
681 return false;
684 # Use external methods for external objects, text in table is URL-only then
685 if ( in_array( 'external', $flags ) ) {
686 $url=$text;
687 @list(/* $proto */,$path)=explode('://',$url,2);
688 if ($path=="") {
689 wfProfileOut( __METHOD__ );
690 return false;
692 $text=ExternalStore::fetchFromURL($url);
695 // If the text was fetched without an error, convert it
696 if ( $text !== false ) {
697 if( in_array( 'gzip', $flags ) ) {
698 # Deal with optional compression of archived pages.
699 # This can be done periodically via maintenance/compressOld.php, and
700 # as pages are saved if $wgCompressRevisions is set.
701 $text = gzinflate( $text );
704 if( in_array( 'object', $flags ) ) {
705 # Generic compressed storage
706 $obj = unserialize( $text );
707 if ( !is_object( $obj ) ) {
708 // Invalid object
709 wfProfileOut( __METHOD__ );
710 return false;
712 $text = $obj->getText();
715 global $wgLegacyEncoding;
716 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
717 # Old revisions kept around in a legacy encoding?
718 # Upconvert on demand.
719 global $wgInputEncoding, $wgContLang;
720 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
723 wfProfileOut( __METHOD__ );
724 return $text;
728 * If $wgCompressRevisions is enabled, we will compress data.
729 * The input string is modified in place.
730 * Return value is the flags field: contains 'gzip' if the
731 * data is compressed, and 'utf-8' if we're saving in UTF-8
732 * mode.
734 * @param mixed $text reference to a text
735 * @return string
737 public static function compressRevisionText( &$text ) {
738 global $wgCompressRevisions;
739 $flags = array();
741 # Revisions not marked this way will be converted
742 # on load if $wgLegacyCharset is set in the future.
743 $flags[] = 'utf-8';
745 if( $wgCompressRevisions ) {
746 if( function_exists( 'gzdeflate' ) ) {
747 $text = gzdeflate( $text );
748 $flags[] = 'gzip';
749 } else {
750 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
753 return implode( ',', $flags );
757 * Insert a new revision into the database, returning the new revision ID
758 * number on success and dies horribly on failure.
760 * @param Database $dbw
761 * @return int
763 public function insertOn( $dbw ) {
764 global $wgDefaultExternalStore;
766 wfProfileIn( __METHOD__ );
768 $data = $this->mText;
769 $flags = Revision::compressRevisionText( $data );
771 # Write to external storage if required
772 if( $wgDefaultExternalStore ) {
773 // Store and get the URL
774 $data = ExternalStore::insertToDefault( $data );
775 if( !$data ) {
776 throw new MWException( "Unable to store text to external storage" );
778 if( $flags ) {
779 $flags .= ',';
781 $flags .= 'external';
784 # Record the text (or external storage URL) to the text table
785 if( !isset( $this->mTextId ) ) {
786 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
787 $dbw->insert( 'text',
788 array(
789 'old_id' => $old_id,
790 'old_text' => $data,
791 'old_flags' => $flags,
792 ), __METHOD__
794 $this->mTextId = $dbw->insertId();
797 # Record the edit in revisions
798 $rev_id = isset( $this->mId )
799 ? $this->mId
800 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
801 $dbw->insert( 'revision',
802 array(
803 'rev_id' => $rev_id,
804 'rev_page' => $this->mPage,
805 'rev_text_id' => $this->mTextId,
806 'rev_comment' => $this->mComment,
807 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
808 'rev_user' => $this->mUser,
809 'rev_user_text' => $this->mUserText,
810 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
811 'rev_deleted' => $this->mDeleted,
812 'rev_len' => $this->mSize,
813 'rev_parent_id' => $this->mParentId ? $this->mParentId : $this->getPreviousRevisionId( $dbw )
814 ), __METHOD__
817 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
819 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
821 wfProfileOut( __METHOD__ );
822 return $this->mId;
826 * Lazy-load the revision's text.
827 * Currently hardcoded to the 'text' table storage engine.
829 * @return string
831 private function loadText() {
832 wfProfileIn( __METHOD__ );
834 // Caching may be beneficial for massive use of external storage
835 global $wgRevisionCacheExpiry, $wgMemc;
836 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
837 if( $wgRevisionCacheExpiry ) {
838 $text = $wgMemc->get( $key );
839 if( is_string( $text ) ) {
840 wfProfileOut( __METHOD__ );
841 return $text;
845 // If we kept data for lazy extraction, use it now...
846 if ( isset( $this->mTextRow ) ) {
847 $row = $this->mTextRow;
848 $this->mTextRow = null;
849 } else {
850 $row = null;
853 if( !$row ) {
854 // Text data is immutable; check slaves first.
855 $dbr = wfGetDB( DB_SLAVE );
856 $row = $dbr->selectRow( 'text',
857 array( 'old_text', 'old_flags' ),
858 array( 'old_id' => $this->getTextId() ),
859 __METHOD__ );
862 if( !$row && wfGetLB()->getServerCount() > 1 ) {
863 // Possible slave lag!
864 $dbw = wfGetDB( DB_MASTER );
865 $row = $dbw->selectRow( 'text',
866 array( 'old_text', 'old_flags' ),
867 array( 'old_id' => $this->getTextId() ),
868 __METHOD__ );
871 $text = self::getRevisionText( $row );
873 # No negative caching -- negative hits on text rows may be due to corrupted slave servers
874 if( $wgRevisionCacheExpiry && $text !== false ) {
875 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
878 wfProfileOut( __METHOD__ );
880 return $text;
884 * Create a new null-revision for insertion into a page's
885 * history. This will not re-save the text, but simply refer
886 * to the text from the previous version.
888 * Such revisions can for instance identify page rename
889 * operations and other such meta-modifications.
891 * @param Database $dbw
892 * @param int $pageId ID number of the page to read from
893 * @param string $summary
894 * @param bool $minor
895 * @return Revision
897 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
898 wfProfileIn( __METHOD__ );
900 $current = $dbw->selectRow(
901 array( 'page', 'revision' ),
902 array( 'page_latest', 'rev_text_id' ),
903 array(
904 'page_id' => $pageId,
905 'page_latest=rev_id',
907 __METHOD__ );
909 if( $current ) {
910 $revision = new Revision( array(
911 'page' => $pageId,
912 'comment' => $summary,
913 'minor_edit' => $minor,
914 'text_id' => $current->rev_text_id,
915 'parent_id' => $current->page_latest
916 ) );
917 } else {
918 $revision = null;
921 wfProfileOut( __METHOD__ );
922 return $revision;
926 * Determine if the current user is allowed to view a particular
927 * field of this revision, if it's marked as deleted.
928 * @param int $field one of self::DELETED_TEXT,
929 * self::DELETED_COMMENT,
930 * self::DELETED_USER
931 * @return bool
933 public function userCan( $field ) {
934 if( ( $this->mDeleted & $field ) == $field ) {
935 global $wgUser;
936 $permission = ( $this->mDeleted & self::DELETED_RESTRICTED ) == self::DELETED_RESTRICTED
937 ? 'suppressrevision'
938 : 'deleterevision';
939 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
940 return $wgUser->isAllowed( $permission );
941 } else {
942 return true;
948 * Get rev_timestamp from rev_id, without loading the rest of the row
949 * @param integer $id
950 * @param integer $pageid, optional
952 static function getTimestampFromId( $id, $pageId = 0 ) {
953 $dbr = wfGetDB( DB_SLAVE );
954 $conds = array( 'rev_id' => $id );
955 if( $pageId ) {
956 $conds['rev_page'] = $pageId;
958 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
959 if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
960 # Not in slave, try master
961 $dbw = wfGetDB( DB_MASTER );
962 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
964 return wfTimestamp( TS_MW, $timestamp );
968 * Get count of revisions per page...not very efficient
969 * @param Database $db
970 * @param int $id, page id
972 static function countByPageId( $db, $id ) {
973 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
974 array( 'rev_page' => $id ), __METHOD__ );
975 if( $row ) {
976 return $row->revCount;
978 return 0;
982 * Get count of revisions per page...not very efficient
983 * @param Database $db
984 * @param Title $title
986 static function countByTitle( $db, $title ) {
987 $id = $title->getArticleId();
988 if( $id ) {
989 return Revision::countByPageId( $db, $id );
991 return 0;
996 * Aliases for backwards compatibility with 1.6
998 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
999 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
1000 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
1001 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );