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