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