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