Autoreview null edits too (bug 13733)
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2 /**
3 * @todo document
4 */
5
6 /**
7 * @todo document
8 */
9 class Revision {
10 const DELETED_TEXT = 1;
11 const DELETED_COMMENT = 2;
12 const DELETED_USER = 4;
13 const DELETED_RESTRICTED = 8;
14
15 /**
16 * Load a page revision from a given revision ID number.
17 * Returns null if no such revision can be found.
18 *
19 * @param int $id
20 * @access public
21 * @static
22 */
23 public static function newFromId( $id ) {
24 return Revision::newFromConds(
25 array( 'page_id=rev_page',
26 'rev_id' => intval( $id ) ) );
27 }
28
29 /**
30 * Load either the current, or a specified, revision
31 * that's attached to a given title. If not attached
32 * to that title, will return null.
33 *
34 * @param Title $title
35 * @param int $id
36 * @return Revision
37 * @access public
38 * @static
39 */
40 public static function newFromTitle( &$title, $id = 0 ) {
41 if( $id ) {
42 $matchId = intval( $id );
43 } else {
44 $matchId = 'page_latest';
45 }
46 return Revision::newFromConds(
47 array( "rev_id=$matchId",
48 'page_id=rev_page',
49 'page_namespace' => $title->getNamespace(),
50 'page_title' => $title->getDBkey() ) );
51 }
52
53 /**
54 * Load a page revision from a given revision ID number.
55 * Returns null if no such revision can be found.
56 *
57 * @param Database $db
58 * @param int $id
59 * @access public
60 * @static
61 */
62 public static function loadFromId( &$db, $id ) {
63 return Revision::loadFromConds( $db,
64 array( 'page_id=rev_page',
65 'rev_id' => intval( $id ) ) );
66 }
67
68 /**
69 * Load either the current, or a specified, revision
70 * that's attached to a given page. If not attached
71 * to that page, will return null.
72 *
73 * @param Database $db
74 * @param int $pageid
75 * @param int $id
76 * @return Revision
77 * @access public
78 * @static
79 */
80 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
81 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
82 if( $id ) {
83 $conds['rev_id']=intval($id);
84 } else {
85 $conds[]='rev_id=page_latest';
86 }
87 return Revision::loadFromConds( $db, $conds );
88 }
89
90 /**
91 * Load either the current, or a specified, revision
92 * that's attached to a given page. If not attached
93 * to that page, will return null.
94 *
95 * @param Database $db
96 * @param Title $title
97 * @param int $id
98 * @return Revision
99 * @access public
100 * @static
101 */
102 public static function loadFromTitle( &$db, $title, $id = 0 ) {
103 if( $id ) {
104 $matchId = intval( $id );
105 } else {
106 $matchId = 'page_latest';
107 }
108 return Revision::loadFromConds(
109 $db,
110 array( "rev_id=$matchId",
111 'page_id=rev_page',
112 'page_namespace' => $title->getNamespace(),
113 'page_title' => $title->getDBkey() ) );
114 }
115
116 /**
117 * Load the revision for the given title with the given timestamp.
118 * WARNING: Timestamps may in some circumstances not be unique,
119 * so this isn't the best key to use.
120 *
121 * @param Database $db
122 * @param Title $title
123 * @param string $timestamp
124 * @return Revision
125 * @access public
126 * @static
127 */
128 public static function loadFromTimestamp( &$db, &$title, $timestamp ) {
129 return Revision::loadFromConds(
130 $db,
131 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
132 'page_id=rev_page',
133 'page_namespace' => $title->getNamespace(),
134 'page_title' => $title->getDBkey() ) );
135 }
136
137 /**
138 * Given a set of conditions, fetch a revision.
139 *
140 * @param array $conditions
141 * @return Revision
142 * @access private
143 * @static
144 */
145 private static function newFromConds( $conditions ) {
146 $db = wfGetDB( DB_SLAVE );
147 $row = Revision::loadFromConds( $db, $conditions );
148 if( is_null( $row ) ) {
149 $dbw = wfGetDB( DB_MASTER );
150 $row = Revision::loadFromConds( $dbw, $conditions );
151 }
152 return $row;
153 }
154
155 /**
156 * Given a set of conditions, fetch a revision from
157 * the given database connection.
158 *
159 * @param Database $db
160 * @param array $conditions
161 * @return Revision
162 * @access private
163 * @static
164 */
165 private static function loadFromConds( $db, $conditions ) {
166 $res = Revision::fetchFromConds( $db, $conditions );
167 if( $res ) {
168 $row = $res->fetchObject();
169 $res->free();
170 if( $row ) {
171 $ret = new Revision( $row );
172 return $ret;
173 }
174 }
175 $ret = null;
176 return $ret;
177 }
178
179 /**
180 * Return a wrapper for a series of database rows to
181 * fetch all of a given page's revisions in turn.
182 * Each row can be fed to the constructor to get objects.
183 *
184 * @param Title $title
185 * @return ResultWrapper
186 * @access public
187 * @static
188 */
189 public static function fetchAllRevisions( &$title ) {
190 return Revision::fetchFromConds(
191 wfGetDB( DB_SLAVE ),
192 array( 'page_namespace' => $title->getNamespace(),
193 'page_title' => $title->getDBkey(),
194 'page_id=rev_page' ) );
195 }
196
197 /**
198 * Return a wrapper for a series of database rows to
199 * fetch all of a given page's revisions in turn.
200 * Each row can be fed to the constructor to get objects.
201 *
202 * @param Title $title
203 * @return ResultWrapper
204 * @access public
205 * @static
206 */
207 public static function fetchRevision( &$title ) {
208 return Revision::fetchFromConds(
209 wfGetDB( DB_SLAVE ),
210 array( 'rev_id=page_latest',
211 'page_namespace' => $title->getNamespace(),
212 'page_title' => $title->getDBkey(),
213 'page_id=rev_page' ) );
214 }
215
216 /**
217 * Given a set of conditions, return a ResultWrapper
218 * which will return matching database rows with the
219 * fields necessary to build Revision objects.
220 *
221 * @param Database $db
222 * @param array $conditions
223 * @return ResultWrapper
224 * @access private
225 * @static
226 */
227 private static function fetchFromConds( $db, $conditions ) {
228 $fields = self::selectFields();
229 $fields[] = 'page_namespace';
230 $fields[] = 'page_title';
231 $fields[] = 'page_latest';
232 $res = $db->select(
233 array( 'page', 'revision' ),
234 $fields,
235 $conditions,
236 'Revision::fetchRow',
237 array( 'LIMIT' => 1 ) );
238 $ret = $db->resultObject( $res );
239 return $ret;
240 }
241
242 /**
243 * Return the list of revision fields that should be selected to create
244 * a new revision.
245 */
246 static function selectFields() {
247 return array(
248 'rev_id',
249 'rev_page',
250 'rev_text_id',
251 'rev_timestamp',
252 'rev_comment',
253 'rev_user_text,'.
254 'rev_user',
255 'rev_minor_edit',
256 'rev_deleted',
257 'rev_len',
258 'rev_parent_id'
259 );
260 }
261
262 /**
263 * @param object $row
264 * @access private
265 */
266 function Revision( $row ) {
267 if( is_object( $row ) ) {
268 $this->mId = intval( $row->rev_id );
269 $this->mPage = intval( $row->rev_page );
270 $this->mTextId = intval( $row->rev_text_id );
271 $this->mComment = $row->rev_comment;
272 $this->mUserText = $row->rev_user_text;
273 $this->mUser = intval( $row->rev_user );
274 $this->mMinorEdit = intval( $row->rev_minor_edit );
275 $this->mTimestamp = $row->rev_timestamp;
276 $this->mDeleted = intval( $row->rev_deleted );
277
278 if( !isset( $row->rev_parent_id ) )
279 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
280 else
281 $this->mParentId = intval( $row->rev_parent_id );
282
283 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) )
284 $this->mSize = null;
285 else
286 $this->mSize = intval( $row->rev_len );
287
288 if( isset( $row->page_latest ) ) {
289 $this->mCurrent = ( $row->rev_id == $row->page_latest );
290 $this->mTitle = Title::makeTitle( $row->page_namespace,
291 $row->page_title );
292 } else {
293 $this->mCurrent = false;
294 $this->mTitle = null;
295 }
296
297 // Lazy extraction...
298 $this->mText = null;
299 if( isset( $row->old_text ) ) {
300 $this->mTextRow = $row;
301 } else {
302 // 'text' table row entry will be lazy-loaded
303 $this->mTextRow = null;
304 }
305 } elseif( is_array( $row ) ) {
306 // Build a new revision to be saved...
307 global $wgUser;
308
309 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
310 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
311 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
312 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
313 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
314 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
315 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
316 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
317 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
318 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
319
320 // Enforce spacing trimming on supplied text
321 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
322 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
323 $this->mTextRow = null;
324
325 $this->mTitle = null; # Load on demand if needed
326 $this->mCurrent = false;
327 # If we still have no len_size, see it we have the text to figure it out
328 if ( !$this->mSize )
329 $this->mSize = is_null($this->mText) ? null : strlen($this->mText);
330 } else {
331 throw new MWException( 'Revision constructor passed invalid row format.' );
332 }
333 }
334
335 /**#@+
336 * @access public
337 */
338
339 /**
340 * Get revision ID
341 * @return int
342 */
343 public function getId() {
344 return $this->mId;
345 }
346
347 /**
348 * Get text row ID
349 * @return int
350 */
351 public function getTextId() {
352 return $this->mTextId;
353 }
354
355 /**
356 * Get parent revision ID (the original previous page revision)
357 * @return int
358 */
359 public function getParentId() {
360 return $this->mParentId;
361 }
362
363 /**
364 * Returns the length of the text in this revision, or null if unknown.
365 * @return int
366 */
367 public function getSize() {
368 return $this->mSize;
369 }
370
371 /**
372 * Returns the title of the page associated with this entry.
373 * @return Title
374 */
375 public function getTitle() {
376 if( isset( $this->mTitle ) ) {
377 return $this->mTitle;
378 }
379 $dbr = wfGetDB( DB_SLAVE );
380 $row = $dbr->selectRow(
381 array( 'page', 'revision' ),
382 array( 'page_namespace', 'page_title' ),
383 array( 'page_id=rev_page',
384 'rev_id' => $this->mId ),
385 'Revision::getTitle' );
386 if( $row ) {
387 $this->mTitle = Title::makeTitle( $row->page_namespace,
388 $row->page_title );
389 }
390 return $this->mTitle;
391 }
392
393 /**
394 * Set the title of the revision
395 * @param Title $title
396 */
397 public function setTitle( $title ) {
398 $this->mTitle = $title;
399 }
400
401 /**
402 * Get the page ID
403 * @return int
404 */
405 public function getPage() {
406 return $this->mPage;
407 }
408
409 /**
410 * Fetch revision's user id if it's available to all users
411 * @return int
412 */
413 public function getUser() {
414 if( $this->isDeleted( self::DELETED_USER ) ) {
415 return 0;
416 } else {
417 return $this->mUser;
418 }
419 }
420
421 /**
422 * Fetch revision's user id without regard for the current user's permissions
423 * @return string
424 */
425 public function getRawUser() {
426 return $this->mUser;
427 }
428
429 /**
430 * Fetch revision's username if it's available to all users
431 * @return string
432 */
433 public function getUserText() {
434 if( $this->isDeleted( self::DELETED_USER ) ) {
435 return "";
436 } else {
437 return $this->mUserText;
438 }
439 }
440
441 /**
442 * Fetch revision's username without regard for view restrictions
443 * @return string
444 */
445 public function getRawUserText() {
446 return $this->mUserText;
447 }
448
449 /**
450 * Fetch revision comment if it's available to all users
451 * @return string
452 */
453 function getComment() {
454 if( $this->isDeleted( self::DELETED_COMMENT ) ) {
455 return "";
456 } else {
457 return $this->mComment;
458 }
459 }
460
461 /**
462 * Fetch revision comment without regard for the current user's permissions
463 * @return string
464 */
465 public function getRawComment() {
466 return $this->mComment;
467 }
468
469 /**
470 * @return bool
471 */
472 public function isMinor() {
473 return (bool)$this->mMinorEdit;
474 }
475
476 /**
477 * int $field one of DELETED_* bitfield constants
478 * @return bool
479 */
480 public function isDeleted( $field ) {
481 return ($this->mDeleted & $field) == $field;
482 }
483
484 /**
485 * Fetch revision text if it's available to all users
486 * @return string
487 */
488 public function getText() {
489 if( $this->isDeleted( self::DELETED_TEXT ) ) {
490 return "";
491 } else {
492 return $this->getRawText();
493 }
494 }
495
496 /**
497 * Fetch revision text without regard for view restrictions
498 * @return string
499 */
500 public function getRawText() {
501 if( is_null( $this->mText ) ) {
502 // Revision text is immutable. Load on demand:
503 $this->mText = $this->loadText();
504 }
505 return $this->mText;
506 }
507
508 /**
509 * Fetch revision text if it's available to THIS user
510 * @return string
511 */
512 public function revText() {
513 if( !$this->userCan( self::DELETED_TEXT ) ) {
514 return "";
515 } else {
516 return $this->getRawText();
517 }
518 }
519
520 /**
521 * @return string
522 */
523 public function getTimestamp() {
524 return wfTimestamp(TS_MW, $this->mTimestamp);
525 }
526
527 /**
528 * @return bool
529 */
530 public function isCurrent() {
531 return $this->mCurrent;
532 }
533
534 /**
535 * Get previous revision for this title
536 * @return Revision
537 */
538 public function getPrevious() {
539 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
540 if( $prev ) {
541 return Revision::newFromTitle( $this->mTitle, $prev );
542 } else {
543 return null;
544 }
545 }
546
547 /**
548 * @return Revision
549 */
550 public function getNext() {
551 $next = $this->mTitle->getNextRevisionID( $this->mId );
552 if ( $next ) {
553 return Revision::newFromTitle( $this->mTitle, $next );
554 } else {
555 return null;
556 }
557 }
558
559 /**
560 * Get previous revision Id for this page_id
561 * This is used to populate rev_parent_id on save
562 * @param Database $db
563 * @return int
564 */
565 private function getPreviousRevisionId( $db ) {
566 if( is_null($this->mPage) ) {
567 return 0;
568 }
569 # Use page_latest if ID is not given
570 if( !$this->mId ) {
571 $prevId = $db->selectField( 'page', 'page_latest',
572 array( 'page_id' => $this->mPage ),
573 __METHOD__ );
574 } else {
575 $prevId = $db->selectField( 'revision', 'rev_id',
576 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
577 __METHOD__,
578 array( 'ORDER BY' => 'rev_id DESC' ) );
579 }
580 return intval($prevId);
581 }
582
583 /**
584 * Get revision text associated with an old or archive row
585 * $row is usually an object from wfFetchRow(), both the flags and the text
586 * field must be included
587 *
588 * @param integer $row Id of a row
589 * @param string $prefix table prefix (default 'old_')
590 * @return string $text|false the text requested
591 */
592 public static function getRevisionText( $row, $prefix = 'old_' ) {
593 wfProfileIn( __METHOD__ );
594
595 # Get data
596 $textField = $prefix . 'text';
597 $flagsField = $prefix . 'flags';
598
599 if( isset( $row->$flagsField ) ) {
600 $flags = explode( ',', $row->$flagsField );
601 } else {
602 $flags = array();
603 }
604
605 if( isset( $row->$textField ) ) {
606 $text = $row->$textField;
607 } else {
608 wfProfileOut( __METHOD__ );
609 return false;
610 }
611
612 # Use external methods for external objects, text in table is URL-only then
613 if ( in_array( 'external', $flags ) ) {
614 $url=$text;
615 @list(/* $proto */,$path)=explode('://',$url,2);
616 if ($path=="") {
617 wfProfileOut( __METHOD__ );
618 return false;
619 }
620 $text=ExternalStore::fetchFromURL($url);
621 }
622
623 // If the text was fetched without an error, convert it
624 if ( $text !== false ) {
625 if( in_array( 'gzip', $flags ) ) {
626 # Deal with optional compression of archived pages.
627 # This can be done periodically via maintenance/compressOld.php, and
628 # as pages are saved if $wgCompressRevisions is set.
629 $text = gzinflate( $text );
630 }
631
632 if( in_array( 'object', $flags ) ) {
633 # Generic compressed storage
634 $obj = unserialize( $text );
635 if ( !is_object( $obj ) ) {
636 // Invalid object
637 wfProfileOut( __METHOD__ );
638 return false;
639 }
640 $text = $obj->getText();
641 }
642
643 global $wgLegacyEncoding;
644 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
645 # Old revisions kept around in a legacy encoding?
646 # Upconvert on demand.
647 global $wgInputEncoding, $wgContLang;
648 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
649 }
650 }
651 wfProfileOut( __METHOD__ );
652 return $text;
653 }
654
655 /**
656 * If $wgCompressRevisions is enabled, we will compress data.
657 * The input string is modified in place.
658 * Return value is the flags field: contains 'gzip' if the
659 * data is compressed, and 'utf-8' if we're saving in UTF-8
660 * mode.
661 *
662 * @param mixed $text reference to a text
663 * @return string
664 */
665 public static function compressRevisionText( &$text ) {
666 global $wgCompressRevisions;
667 $flags = array();
668
669 # Revisions not marked this way will be converted
670 # on load if $wgLegacyCharset is set in the future.
671 $flags[] = 'utf-8';
672
673 if( $wgCompressRevisions ) {
674 if( function_exists( 'gzdeflate' ) ) {
675 $text = gzdeflate( $text );
676 $flags[] = 'gzip';
677 } else {
678 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
679 }
680 }
681 return implode( ',', $flags );
682 }
683
684 /**
685 * Insert a new revision into the database, returning the new revision ID
686 * number on success and dies horribly on failure.
687 *
688 * @param Database $dbw
689 * @return int
690 */
691 public function insertOn( &$dbw ) {
692 global $wgDefaultExternalStore;
693
694 wfProfileIn( __METHOD__ );
695
696 $data = $this->mText;
697 $flags = Revision::compressRevisionText( $data );
698
699 # Write to external storage if required
700 if ( $wgDefaultExternalStore ) {
701 if ( is_array( $wgDefaultExternalStore ) ) {
702 // Distribute storage across multiple clusters
703 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
704 } else {
705 $store = $wgDefaultExternalStore;
706 }
707 // Store and get the URL
708 $data = ExternalStore::insert( $store, $data );
709 if ( !$data ) {
710 # This should only happen in the case of a configuration error, where the external store is not valid
711 throw new MWException( "Unable to store text to external storage $store" );
712 }
713 if ( $flags ) {
714 $flags .= ',';
715 }
716 $flags .= 'external';
717 }
718
719 # Record the text (or external storage URL) to the text table
720 if( !isset( $this->mTextId ) ) {
721 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
722 $dbw->insert( 'text',
723 array(
724 'old_id' => $old_id,
725 'old_text' => $data,
726 'old_flags' => $flags,
727 ), __METHOD__
728 );
729 $this->mTextId = $dbw->insertId();
730 }
731
732 # Record the edit in revisions
733 $rev_id = isset( $this->mId )
734 ? $this->mId
735 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
736 $dbw->insert( 'revision',
737 array(
738 'rev_id' => $rev_id,
739 'rev_page' => $this->mPage,
740 'rev_text_id' => $this->mTextId,
741 'rev_comment' => $this->mComment,
742 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
743 'rev_user' => $this->mUser,
744 'rev_user_text' => $this->mUserText,
745 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
746 'rev_deleted' => $this->mDeleted,
747 'rev_len' => $this->mSize,
748 'rev_parent_id' => $this->mParentId ? $this->mParentId : $this->getPreviousRevisionId( $dbw )
749 ), __METHOD__
750 );
751
752 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
753
754 wfRunHooks( 'RevisionInsertComplete', array( &$this ) );
755
756 wfProfileOut( __METHOD__ );
757 return $this->mId;
758 }
759
760 /**
761 * Lazy-load the revision's text.
762 * Currently hardcoded to the 'text' table storage engine.
763 *
764 * @return string
765 */
766 private function loadText() {
767 wfProfileIn( __METHOD__ );
768
769 // Caching may be beneficial for massive use of external storage
770 global $wgRevisionCacheExpiry, $wgMemc;
771 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
772 if( $wgRevisionCacheExpiry ) {
773 $text = $wgMemc->get( $key );
774 if( is_string( $text ) ) {
775 wfProfileOut( __METHOD__ );
776 return $text;
777 }
778 }
779
780 // If we kept data for lazy extraction, use it now...
781 if ( isset( $this->mTextRow ) ) {
782 $row = $this->mTextRow;
783 $this->mTextRow = null;
784 } else {
785 $row = null;
786 }
787
788 if( !$row ) {
789 // Text data is immutable; check slaves first.
790 $dbr = wfGetDB( DB_SLAVE );
791 $row = $dbr->selectRow( 'text',
792 array( 'old_text', 'old_flags' ),
793 array( 'old_id' => $this->getTextId() ),
794 __METHOD__ );
795 }
796
797 if( !$row ) {
798 // Possible slave lag!
799 $dbw = wfGetDB( DB_MASTER );
800 $row = $dbw->selectRow( 'text',
801 array( 'old_text', 'old_flags' ),
802 array( 'old_id' => $this->getTextId() ),
803 __METHOD__ );
804 }
805
806 $text = self::getRevisionText( $row );
807
808 if( $wgRevisionCacheExpiry ) {
809 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
810 }
811
812 wfProfileOut( __METHOD__ );
813
814 return $text;
815 }
816
817 /**
818 * Create a new null-revision for insertion into a page's
819 * history. This will not re-save the text, but simply refer
820 * to the text from the previous version.
821 *
822 * Such revisions can for instance identify page rename
823 * operations and other such meta-modifications.
824 *
825 * @param Database $dbw
826 * @param int $pageId ID number of the page to read from
827 * @param string $summary
828 * @param bool $minor
829 * @return Revision
830 */
831 public static function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
832 wfProfileIn( __METHOD__ );
833
834 $current = $dbw->selectRow(
835 array( 'page', 'revision' ),
836 array( 'page_latest', 'rev_text_id' ),
837 array(
838 'page_id' => $pageId,
839 'page_latest=rev_id',
840 ),
841 __METHOD__ );
842
843 if( $current ) {
844 $revision = new Revision( array(
845 'page' => $pageId,
846 'comment' => $summary,
847 'minor_edit' => $minor,
848 'text_id' => $current->rev_text_id,
849 'parent_id' => $current->page_latest
850 ) );
851 } else {
852 $revision = null;
853 }
854
855 wfProfileOut( __METHOD__ );
856 return $revision;
857 }
858
859 /**
860 * Determine if the current user is allowed to view a particular
861 * field of this revision, if it's marked as deleted.
862 * @param int $field one of self::DELETED_TEXT,
863 * self::DELETED_COMMENT,
864 * self::DELETED_USER
865 * @return bool
866 */
867 public function userCan( $field ) {
868 if( ( $this->mDeleted & $field ) == $field ) {
869 global $wgUser;
870 $permission = ( $this->mDeleted & self::DELETED_RESTRICTED ) == self::DELETED_RESTRICTED
871 ? 'hiderevision'
872 : 'deleterevision';
873 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
874 return $wgUser->isAllowed( $permission );
875 } else {
876 return true;
877 }
878 }
879
880
881 /**
882 * Get rev_timestamp from rev_id, without loading the rest of the row
883 * @param integer $id
884 */
885 static function getTimestampFromID( $id ) {
886 $dbr = wfGetDB( DB_SLAVE );
887 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
888 array( 'rev_id' => $id ), __METHOD__ );
889 if ( $timestamp === false ) {
890 # Not in slave, try master
891 $dbw = wfGetDB( DB_MASTER );
892 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
893 array( 'rev_id' => $id ), __METHOD__ );
894 }
895 return $timestamp;
896 }
897
898 /**
899 * Get count of revisions per page...not very efficient
900 * @param Database $db
901 * @param int $id, page id
902 */
903 static function countByPageId( $db, $id ) {
904 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
905 array( 'rev_page' => $id ), __METHOD__ );
906 if( $row ) {
907 return $row->revCount;
908 }
909 return 0;
910 }
911
912 /**
913 * Get count of revisions per page...not very efficient
914 * @param Database $db
915 * @param Title $title
916 */
917 static function countByTitle( $db, $title ) {
918 $id = $title->getArticleId();
919 if( $id ) {
920 return Revision::countByPageId( $db, $id );
921 }
922 return 0;
923 }
924 }
925
926 /**
927 * Aliases for backwards compatibility with 1.6
928 */
929 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
930 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
931 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
932 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );