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