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