Extend RevisionInsertComplete hook (bug 14780)
[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() {
434 if( $this->isDeleted( self::DELETED_USER ) ) {
435 return 0;
436 } else {
437 return $this->mUser;
438 }
439 }
440
441 /**
442 * Fetch revision's user id without regard for the current user's permissions
443 * @return string
444 */
445 public function getRawUser() {
446 return $this->mUser;
447 }
448
449 /**
450 * Fetch revision's username if it's available to all users
451 * @return string
452 */
453 public function getUserText() {
454 if( $this->isDeleted( self::DELETED_USER ) ) {
455 return "";
456 } else {
457 return $this->mUserText;
458 }
459 }
460
461 /**
462 * Fetch revision's username without regard for view restrictions
463 * @return string
464 */
465 public function getRawUserText() {
466 return $this->mUserText;
467 }
468
469 /**
470 * Fetch revision comment if it's available to all users
471 * @return string
472 */
473 function getComment() {
474 if( $this->isDeleted( self::DELETED_COMMENT ) ) {
475 return "";
476 } else {
477 return $this->mComment;
478 }
479 }
480
481 /**
482 * Fetch revision comment without regard for the current user's permissions
483 * @return string
484 */
485 public function getRawComment() {
486 return $this->mComment;
487 }
488
489 /**
490 * @return bool
491 */
492 public function isMinor() {
493 return (bool)$this->mMinorEdit;
494 }
495
496 /**
497 * int $field one of DELETED_* bitfield constants
498 * @return bool
499 */
500 public function isDeleted( $field ) {
501 return ($this->mDeleted & $field) == $field;
502 }
503
504 /**
505 * Fetch revision text if it's available to all users
506 * @return string
507 */
508 public function getText() {
509 if( $this->isDeleted( self::DELETED_TEXT ) ) {
510 return "";
511 } else {
512 return $this->getRawText();
513 }
514 }
515
516 /**
517 * Fetch revision text without regard for view restrictions
518 * @return string
519 */
520 public function getRawText() {
521 if( is_null( $this->mText ) ) {
522 // Revision text is immutable. Load on demand:
523 $this->mText = $this->loadText();
524 }
525 return $this->mText;
526 }
527
528 /**
529 * Fetch revision text if it's available to THIS user
530 * @return string
531 */
532 public function revText() {
533 if( !$this->userCan( self::DELETED_TEXT ) ) {
534 return "";
535 } else {
536 return $this->getRawText();
537 }
538 }
539
540 /**
541 * @return string
542 */
543 public function getTimestamp() {
544 return wfTimestamp(TS_MW, $this->mTimestamp);
545 }
546
547 /**
548 * @return bool
549 */
550 public function isCurrent() {
551 return $this->mCurrent;
552 }
553
554 /**
555 * Get previous revision for this title
556 * @return Revision
557 */
558 public function getPrevious() {
559 if( $this->getTitle() ) {
560 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
561 if( $prev ) {
562 return Revision::newFromTitle( $this->getTitle(), $prev );
563 }
564 }
565 return null;
566 }
567
568 /**
569 * @return Revision
570 */
571 public function getNext() {
572 if( $this->getTitle() ) {
573 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
574 if ( $next ) {
575 return Revision::newFromTitle( $this->getTitle(), $next );
576 }
577 }
578 return null;
579 }
580
581 /**
582 * Get previous revision Id for this page_id
583 * This is used to populate rev_parent_id on save
584 * @param Database $db
585 * @return int
586 */
587 private function getPreviousRevisionId( $db ) {
588 if( is_null($this->mPage) ) {
589 return 0;
590 }
591 # Use page_latest if ID is not given
592 if( !$this->mId ) {
593 $prevId = $db->selectField( 'page', 'page_latest',
594 array( 'page_id' => $this->mPage ),
595 __METHOD__ );
596 } else {
597 $prevId = $db->selectField( 'revision', 'rev_id',
598 array( 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ),
599 __METHOD__,
600 array( 'ORDER BY' => 'rev_id DESC' ) );
601 }
602 return intval($prevId);
603 }
604
605 /**
606 * Get revision text associated with an old or archive row
607 * $row is usually an object from wfFetchRow(), both the flags and the text
608 * field must be included
609 *
610 * @param integer $row Id of a row
611 * @param string $prefix table prefix (default 'old_')
612 * @return string $text|false the text requested
613 */
614 public static function getRevisionText( $row, $prefix = 'old_' ) {
615 wfProfileIn( __METHOD__ );
616
617 # Get data
618 $textField = $prefix . 'text';
619 $flagsField = $prefix . 'flags';
620
621 if( isset( $row->$flagsField ) ) {
622 $flags = explode( ',', $row->$flagsField );
623 } else {
624 $flags = array();
625 }
626
627 if( isset( $row->$textField ) ) {
628 $text = $row->$textField;
629 } else {
630 wfProfileOut( __METHOD__ );
631 return false;
632 }
633
634 # Use external methods for external objects, text in table is URL-only then
635 if ( in_array( 'external', $flags ) ) {
636 $url=$text;
637 @list(/* $proto */,$path)=explode('://',$url,2);
638 if ($path=="") {
639 wfProfileOut( __METHOD__ );
640 return false;
641 }
642 $text=ExternalStore::fetchFromURL($url);
643 }
644
645 // If the text was fetched without an error, convert it
646 if ( $text !== false ) {
647 if( in_array( 'gzip', $flags ) ) {
648 # Deal with optional compression of archived pages.
649 # This can be done periodically via maintenance/compressOld.php, and
650 # as pages are saved if $wgCompressRevisions is set.
651 $text = gzinflate( $text );
652 }
653
654 if( in_array( 'object', $flags ) ) {
655 # Generic compressed storage
656 $obj = unserialize( $text );
657 if ( !is_object( $obj ) ) {
658 // Invalid object
659 wfProfileOut( __METHOD__ );
660 return false;
661 }
662 $text = $obj->getText();
663 }
664
665 global $wgLegacyEncoding;
666 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
667 # Old revisions kept around in a legacy encoding?
668 # Upconvert on demand.
669 global $wgInputEncoding, $wgContLang;
670 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
671 }
672 }
673 wfProfileOut( __METHOD__ );
674 return $text;
675 }
676
677 /**
678 * If $wgCompressRevisions is enabled, we will compress data.
679 * The input string is modified in place.
680 * Return value is the flags field: contains 'gzip' if the
681 * data is compressed, and 'utf-8' if we're saving in UTF-8
682 * mode.
683 *
684 * @param mixed $text reference to a text
685 * @return string
686 */
687 public static function compressRevisionText( &$text ) {
688 global $wgCompressRevisions;
689 $flags = array();
690
691 # Revisions not marked this way will be converted
692 # on load if $wgLegacyCharset is set in the future.
693 $flags[] = 'utf-8';
694
695 if( $wgCompressRevisions ) {
696 if( function_exists( 'gzdeflate' ) ) {
697 $text = gzdeflate( $text );
698 $flags[] = 'gzip';
699 } else {
700 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
701 }
702 }
703 return implode( ',', $flags );
704 }
705
706 /**
707 * Insert a new revision into the database, returning the new revision ID
708 * number on success and dies horribly on failure.
709 *
710 * @param Database $dbw
711 * @return int
712 */
713 public function insertOn( $dbw ) {
714 global $wgDefaultExternalStore;
715
716 wfProfileIn( __METHOD__ );
717
718 $data = $this->mText;
719 $flags = Revision::compressRevisionText( $data );
720
721 # Write to external storage if required
722 if ( $wgDefaultExternalStore ) {
723 if ( is_array( $wgDefaultExternalStore ) ) {
724 // Distribute storage across multiple clusters
725 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
726 } else {
727 $store = $wgDefaultExternalStore;
728 }
729 // Store and get the URL
730 $data = ExternalStore::insert( $store, $data );
731 if ( !$data ) {
732 # This should only happen in the case of a configuration error, where the external store is not valid
733 throw new MWException( "Unable to store text to external storage $store" );
734 }
735 if ( $flags ) {
736 $flags .= ',';
737 }
738 $flags .= 'external';
739 }
740
741 # Record the text (or external storage URL) to the text table
742 if( !isset( $this->mTextId ) ) {
743 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
744 $dbw->insert( 'text',
745 array(
746 'old_id' => $old_id,
747 'old_text' => $data,
748 'old_flags' => $flags,
749 ), __METHOD__
750 );
751 $this->mTextId = $dbw->insertId();
752 }
753
754 # Record the edit in revisions
755 $rev_id = isset( $this->mId )
756 ? $this->mId
757 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
758 $dbw->insert( 'revision',
759 array(
760 'rev_id' => $rev_id,
761 'rev_page' => $this->mPage,
762 'rev_text_id' => $this->mTextId,
763 'rev_comment' => $this->mComment,
764 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
765 'rev_user' => $this->mUser,
766 'rev_user_text' => $this->mUserText,
767 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
768 'rev_deleted' => $this->mDeleted,
769 'rev_len' => $this->mSize,
770 'rev_parent_id' => $this->mParentId ? $this->mParentId : $this->getPreviousRevisionId( $dbw )
771 ), __METHOD__
772 );
773
774 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
775
776 wfRunHooks( 'RevisionInsertComplete', array( &$this, &$data, &$flags ) );
777
778 wfProfileOut( __METHOD__ );
779 return $this->mId;
780 }
781
782 /**
783 * Lazy-load the revision's text.
784 * Currently hardcoded to the 'text' table storage engine.
785 *
786 * @return string
787 */
788 private function loadText() {
789 wfProfileIn( __METHOD__ );
790
791 // Caching may be beneficial for massive use of external storage
792 global $wgRevisionCacheExpiry, $wgMemc;
793 $key = wfMemcKey( 'revisiontext', 'textid', $this->getTextId() );
794 if( $wgRevisionCacheExpiry ) {
795 $text = $wgMemc->get( $key );
796 if( is_string( $text ) ) {
797 wfProfileOut( __METHOD__ );
798 return $text;
799 }
800 }
801
802 // If we kept data for lazy extraction, use it now...
803 if ( isset( $this->mTextRow ) ) {
804 $row = $this->mTextRow;
805 $this->mTextRow = null;
806 } else {
807 $row = null;
808 }
809
810 if( !$row ) {
811 // Text data is immutable; check slaves first.
812 $dbr = wfGetDB( DB_SLAVE );
813 $row = $dbr->selectRow( 'text',
814 array( 'old_text', 'old_flags' ),
815 array( 'old_id' => $this->getTextId() ),
816 __METHOD__ );
817 }
818
819 if( !$row ) {
820 // Possible slave lag!
821 $dbw = wfGetDB( DB_MASTER );
822 $row = $dbw->selectRow( 'text',
823 array( 'old_text', 'old_flags' ),
824 array( 'old_id' => $this->getTextId() ),
825 __METHOD__ );
826 }
827
828 $text = self::getRevisionText( $row );
829
830 if( $wgRevisionCacheExpiry ) {
831 $wgMemc->set( $key, $text, $wgRevisionCacheExpiry );
832 }
833
834 wfProfileOut( __METHOD__ );
835
836 return $text;
837 }
838
839 /**
840 * Create a new null-revision for insertion into a page's
841 * history. This will not re-save the text, but simply refer
842 * to the text from the previous version.
843 *
844 * Such revisions can for instance identify page rename
845 * operations and other such meta-modifications.
846 *
847 * @param Database $dbw
848 * @param int $pageId ID number of the page to read from
849 * @param string $summary
850 * @param bool $minor
851 * @return Revision
852 */
853 public static function newNullRevision( $dbw, $pageId, $summary, $minor ) {
854 wfProfileIn( __METHOD__ );
855
856 $current = $dbw->selectRow(
857 array( 'page', 'revision' ),
858 array( 'page_latest', 'rev_text_id' ),
859 array(
860 'page_id' => $pageId,
861 'page_latest=rev_id',
862 ),
863 __METHOD__ );
864
865 if( $current ) {
866 $revision = new Revision( array(
867 'page' => $pageId,
868 'comment' => $summary,
869 'minor_edit' => $minor,
870 'text_id' => $current->rev_text_id,
871 'parent_id' => $current->page_latest
872 ) );
873 } else {
874 $revision = null;
875 }
876
877 wfProfileOut( __METHOD__ );
878 return $revision;
879 }
880
881 /**
882 * Determine if the current user is allowed to view a particular
883 * field of this revision, if it's marked as deleted.
884 * @param int $field one of self::DELETED_TEXT,
885 * self::DELETED_COMMENT,
886 * self::DELETED_USER
887 * @return bool
888 */
889 public function userCan( $field ) {
890 if( ( $this->mDeleted & $field ) == $field ) {
891 global $wgUser;
892 $permission = ( $this->mDeleted & self::DELETED_RESTRICTED ) == self::DELETED_RESTRICTED
893 ? 'suppressrevision'
894 : 'deleterevision';
895 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
896 return $wgUser->isAllowed( $permission );
897 } else {
898 return true;
899 }
900 }
901
902
903 /**
904 * Get rev_timestamp from rev_id, without loading the rest of the row
905 * @param integer $id
906 * @param integer $pageid, optional
907 */
908 static function getTimestampFromId( $id, $pageId = 0 ) {
909 $dbr = wfGetDB( DB_SLAVE );
910 $conds = array( 'rev_id' => $id );
911 if( $pageId ) {
912 $conds['rev_page'] = $pageId;
913 }
914 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
915 if ( $timestamp === false ) {
916 # Not in slave, try master
917 $dbw = wfGetDB( DB_MASTER );
918 $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
919 }
920 return wfTimestamp( TS_MW, $timestamp );
921 }
922
923 /**
924 * Get count of revisions per page...not very efficient
925 * @param Database $db
926 * @param int $id, page id
927 */
928 static function countByPageId( $db, $id ) {
929 $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
930 array( 'rev_page' => $id ), __METHOD__ );
931 if( $row ) {
932 return $row->revCount;
933 }
934 return 0;
935 }
936
937 /**
938 * Get count of revisions per page...not very efficient
939 * @param Database $db
940 * @param Title $title
941 */
942 static function countByTitle( $db, $title ) {
943 $id = $title->getArticleId();
944 if( $id ) {
945 return Revision::countByPageId( $db, $id );
946 }
947 return 0;
948 }
949 }
950
951 /**
952 * Aliases for backwards compatibility with 1.6
953 */
954 define( 'MW_REV_DELETED_TEXT', Revision::DELETED_TEXT );
955 define( 'MW_REV_DELETED_COMMENT', Revision::DELETED_COMMENT );
956 define( 'MW_REV_DELETED_USER', Revision::DELETED_USER );
957 define( 'MW_REV_DELETED_RESTRICTED', Revision::DELETED_RESTRICTED );