Improve efficiency of autoreviewing of edits rollbacks and merge into main function
[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 * Return the list of text fields that should be selected to read the
264 * revision text
265 */
266 static function selectTextFields() {
267 return array(
268 'old_text',
269 'old_flags'
270 );
271 }
272 /**
273 * Return the list of page fields that should be selected from page table
274 */
275 static function selectPageFields() {
276 return array(
277 'page_namespace',
278 'page_title',
279 'page_latest'
280 );
281 }
282
283 /**
284 * @param object $row
285 * @access private
286 */
287 function Revision( $row ) {
288 if( is_object( $row ) ) {
289 $this->mId = intval( $row->rev_id );
290 $this->mPage = intval( $row->rev_page );
291 $this->mTextId = intval( $row->rev_text_id );
292 $this->mComment = $row->rev_comment;
293 $this->mUserText = $row->rev_user_text;
294 $this->mUser = intval( $row->rev_user );
295 $this->mMinorEdit = intval( $row->rev_minor_edit );
296 $this->mTimestamp = $row->rev_timestamp;
297 $this->mDeleted = intval( $row->rev_deleted );
298
299 if( !isset( $row->rev_parent_id ) )
300 $this->mParentId = is_null($row->rev_parent_id) ? null : 0;
301 else
302 $this->mParentId = intval( $row->rev_parent_id );
303
304 if( !isset( $row->rev_len ) || is_null( $row->rev_len ) )
305 $this->mSize = null;
306 else
307 $this->mSize = intval( $row->rev_len );
308
309 if( isset( $row->page_latest ) ) {
310 $this->mCurrent = ( $row->rev_id == $row->page_latest );
311 $this->mTitle = Title::makeTitle( $row->page_namespace,
312 $row->page_title );
313 } else {
314 $this->mCurrent = false;
315 $this->mTitle = null;
316 }
317
318 // Lazy extraction...
319 $this->mText = null;
320 if( isset( $row->old_text ) ) {
321 $this->mTextRow = $row;
322 } else {
323 // 'text' table row entry will be lazy-loaded
324 $this->mTextRow = null;
325 }
326 } elseif( is_array( $row ) ) {
327 // Build a new revision to be saved...
328 global $wgUser;
329
330 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
331 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
332 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
333 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
334 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
335 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
336 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
337 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
338 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
339 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
340
341 // Enforce spacing trimming on supplied text
342 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
343 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
344 $this->mTextRow = null;
345
346 $this->mTitle = null; # Load on demand if needed
347 $this->mCurrent = false;
348 # If we still have no len_size, see it we have the text to figure it out
349 if ( !$this->mSize )
350 $this->mSize = is_null($this->mText) ? null : strlen($this->mText);
351 } else {
352 throw new MWException( 'Revision constructor passed invalid row format.' );
353 }
354 }
355
356 /**#@+
357 * @access public
358 */
359
360 /**
361 * Get revision ID
362 * @return int
363 */
364 public function getId() {
365 return $this->mId;
366 }
367
368 /**
369 * Get text row ID
370 * @return int
371 */
372 public function getTextId() {
373 return $this->mTextId;
374 }
375
376 /**
377 * Get parent revision ID (the original previous page revision)
378 * @return int
379 */
380 public function getParentId() {
381 return $this->mParentId;
382 }
383
384 /**
385 * Returns the length of the text in this revision, or null if unknown.
386 * @return int
387 */
388 public function getSize() {
389 return $this->mSize;
390 }
391
392 /**
393 * Returns the title of the page associated with this entry.
394 * @return Title
395 */
396 public function getTitle() {
397 if( isset( $this->mTitle ) ) {
398 return $this->mTitle;
399 }
400 $dbr = wfGetDB( DB_SLAVE );
401 $row = $dbr->selectRow(
402 array( 'page', 'revision' ),
403 array( 'page_namespace', 'page_title' ),
404 array( 'page_id=rev_page',
405 'rev_id' => $this->mId ),
406 'Revision::getTitle' );
407 if( $row ) {
408 $this->mTitle = Title::makeTitle( $row->page_namespace,
409 $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()->getPreviousRevisionID( $this->mId );
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()->getNextRevisionID( $this->mId );
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 * @param bool $edit, was this a new edit? (optional)
711 * @param integer $baseID, what revision was this based on? (optional)
712 * @return int
713 */
714 public function insertOn( &$dbw, $edit = false, $baseID = false ) {
715 global $wgDefaultExternalStore;
716
717 wfProfileIn( __METHOD__ );
718
719 $data = $this->mText;
720 $flags = Revision::compressRevisionText( $data );
721
722 # Write to external storage if required
723 if ( $wgDefaultExternalStore ) {
724 if ( is_array( $wgDefaultExternalStore ) ) {
725 // Distribute storage across multiple clusters
726 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
727 } else {
728 $store = $wgDefaultExternalStore;
729 }
730 // Store and get the URL
731 $data = ExternalStore::insert( $store, $data );
732 if ( !$data ) {
733 # This should only happen in the case of a configuration error, where the external store is not valid
734 throw new MWException( "Unable to store text to external storage $store" );
735 }
736 if ( $flags ) {
737 $flags .= ',';
738 }
739 $flags .= 'external';
740 }
741
742 # Record the text (or external storage URL) to the text table
743 if( !isset( $this->mTextId ) ) {
744 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
745 $dbw->insert( 'text',
746 array(
747 'old_id' => $old_id,
748 'old_text' => $data,
749 'old_flags' => $flags,
750 ), __METHOD__
751 );
752 $this->mTextId = $dbw->insertId();
753 }
754
755 # Record the edit in revisions
756 $rev_id = isset( $this->mId )
757 ? $this->mId
758 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
759 $dbw->insert( 'revision',
760 array(
761 'rev_id' => $rev_id,
762 'rev_page' => $this->mPage,
763 'rev_text_id' => $this->mTextId,
764 'rev_comment' => $this->mComment,
765 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
766 'rev_user' => $this->mUser,
767 'rev_user_text' => $this->mUserText,
768 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
769 'rev_deleted' => $this->mDeleted,
770 'rev_len' => $this->mSize,
771 'rev_parent_id' => $this->mParentId ? $this->mParentId : $this->getPreviousRevisionId( $dbw )
772 ), __METHOD__
773 );
774
775 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
776
777 wfRunHooks( 'RevisionInsertComplete', array( &$this, $edit, $baseID ) );
778
779 wfProfileOut( __METHOD__ );
780 return $this->mId;
781 }
782
783 /**
784 * Lazy-load the revision's text.
785 * Currently hardcoded to the 'text' table storage engine.
786 *
787 * @return string
788 */
789 private function loadText() {
790 wfProfileIn( __METHOD__ );
791
792 // Caching may be beneficial for massive use of external storage
793 global $wgRevisionCacheExpiry, $wgMemc;
794 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
795 if( $wgRevisionCacheExpiry ) {
796 $text = $wgMemc->get( $key );
797 if( is_string( $text ) ) {
798 wfProfileOut( __METHOD__ );
799 return $text;
800 }
801 }
802
803 // If we kept data for lazy extraction, use it now...
804 if ( isset( $this->mTextRow ) ) {
805 $row = $this->mTextRow;
806 $this->mTextRow = null;
807 } else {
808 $row = null;
809 }
810
811 if( !$row ) {
812 // Text data is immutable; check slaves first.
813 $dbr = wfGetDB( DB_SLAVE );
814 $row = $dbr->selectRow( 'text',
815 array( 'old_text', 'old_flags' ),
816 array( 'old_id' => $this->getTextId() ),
817 __METHOD__ );
818 }
819
820 if( !$row ) {
821 // Possible slave lag!
822 $dbw = wfGetDB( DB_MASTER );
823 $row = $dbw->selectRow( 'text',
824 array( 'old_text', 'old_flags' ),
825 array( 'old_id' => $this->getTextId() ),
826 __METHOD__ );
827 }
828
829 $text = self::getRevisionText( $row );
830
831 if( $wgRevisionCacheExpiry ) {
832 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
833 }
834
835 wfProfileOut( __METHOD__ );
836
837 return $text;
838 }
839
840 /**
841 * Create a new null-revision for insertion into a page's
842 * history. This will not re-save the text, but simply refer
843 * to the text from the previous version.
844 *
845 * Such revisions can for instance identify page rename
846 * operations and other such meta-modifications.
847 *
848 * @param Database $dbw
849 * @param int $pageId ID number of the page to read from
850 * @param string $summary
851 * @param bool $minor
852 * @return Revision
853 */
854 public static function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
855 wfProfileIn( __METHOD__ );
856
857 $current = $dbw->selectRow(
858 array( 'page', 'revision' ),
859 array( 'page_latest', 'rev_text_id' ),
860 array(
861 'page_id' => $pageId,
862 'page_latest=rev_id',
863 ),
864 __METHOD__ );
865
866 if( $current ) {
867 $revision = new Revision( array(
868 'page' => $pageId,
869 'comment' => $summary,
870 'minor_edit' => $minor,
871 'text_id' => $current->rev_text_id,
872 'parent_id' => $current->page_latest
873 ) );
874 } else {
875 $revision = null;
876 }
877
878 wfProfileOut( __METHOD__ );
879 return $revision;
880 }
881
882 /**
883 * Determine if the current user is allowed to view a particular
884 * field of this revision, if it's marked as deleted.
885 * @param int $field one of self::DELETED_TEXT,
886 * self::DELETED_COMMENT,
887 * self::DELETED_USER
888 * @return bool
889 */
890 public function userCan( $field ) {
891 if( ( $this->mDeleted & $field ) == $field ) {
892 global $wgUser;
893 $permission = ( $this->mDeleted & self::DELETED_RESTRICTED ) == self::DELETED_RESTRICTED
894 ? 'hiderevision'
895 : 'deleterevision';
896 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
897 return $wgUser->isAllowed( $permission );
898 } else {
899 return true;
900 }
901 }
902
903
904 /**
905 * Get rev_timestamp from rev_id, without loading the rest of the row
906 * @param integer $id
907 */
908 static function getTimestampFromID( $id ) {
909 $dbr = wfGetDB( DB_SLAVE );
910 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
911 array( 'rev_id' => $id ), __METHOD__ );
912 if ( $timestamp === false ) {
913 # Not in slave, try master
914 $dbw = wfGetDB( DB_MASTER );
915 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp',
916 array( 'rev_id' => $id ), __METHOD__ );
917 }
918 return $timestamp;
919 }
920
921 /**
922 * Get count of revisions per page...not very efficient
923 * @param Database $db
924 * @param int $id, page id
925 */
926 static function countByPageId( $db, $id ) {
927 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
928 array( 'rev_page' => $id ), __METHOD__ );
929 if( $row ) {
930 return $row->revCount;
931 }
932 return 0;
933 }
934
935 /**
936 * Get count of revisions per page...not very efficient
937 * @param Database $db
938 * @param Title $title
939 */
940 static function countByTitle( $db, $title ) {
941 $id = $title->getArticleId();
942 if( $id ) {
943 return Revision::countByPageId( $db, $id );
944 }
945 return 0;
946 }
947 }
948
949 /**
950 * Aliases for backwards compatibility with 1.6
951 */
952 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
953 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
954 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
955 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );