Write load distribution across multiple external storage engines. Untested.
[mediawiki.git] / includes / Revision.php
bloba89bb8ba139f5d0e3f61fc792e1e16148990fe72
1 <?php
2 /**
3 * @package MediaWiki
4 * @todo document
5 */
7 /** */
8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
11 /** @+ */
12 define( 'MW_REV_DELETED_TEXT', 1 );
13 define( 'MW_REV_DELETED_COMMENT', 2 );
14 define( 'MW_REV_DELETED_USER', 4 );
15 define( 'MW_REV_DELETED_RESTRICTED', 8 );
16 /** @- */
18 /**
19 * @package MediaWiki
20 * @todo document
22 class Revision {
23 /**
24 * Load a page revision from a given revision ID number.
25 * Returns null if no such revision can be found.
27 * @param int $id
28 * @static
29 * @access public
31 function newFromId( $id ) {
32 return Revision::newFromConds(
33 array( 'page_id=rev_page',
34 'rev_id' => intval( $id ) ) );
37 /**
38 * Load either the current, or a specified, revision
39 * that's attached to a given title. If not attached
40 * to that title, will return null.
42 * @param Title $title
43 * @param int $id
44 * @return Revision
45 * @access public
46 * @static
48 function newFromTitle( &$title, $id = 0 ) {
49 if( $id ) {
50 $matchId = intval( $id );
51 } else {
52 $matchId = 'page_latest';
54 return Revision::newFromConds(
55 array( "rev_id=$matchId",
56 'page_id=rev_page',
57 'page_namespace' => $title->getNamespace(),
58 'page_title' => $title->getDbkey() ) );
61 /**
62 * Load either the current, or a specified, revision
63 * that's attached to a given page. If not attached
64 * to that page, will return null.
66 * @param Database $db
67 * @param int $pageid
68 * @param int $id
69 * @return Revision
70 * @access public
72 function loadFromPageId( &$db, $pageid, $id = 0 ) {
73 $conds=array('page_id=rev_page','rev_page'=>intval( $pageid ), 'page_id'=>intval( $pageid ));
74 if( $id ) {
75 $conds['rev_id']=intval($id);
76 } else {
77 $conds[]='rev_id=page_latest';
79 return Revision::loadFromConds( $db, $conds );
82 /**
83 * Load either the current, or a specified, revision
84 * that's attached to a given page. If not attached
85 * to that page, will return null.
87 * @param Database $db
88 * @param Title $title
89 * @param int $id
90 * @return Revision
91 * @access public
93 function loadFromTitle( &$db, $title, $id = 0 ) {
94 if( $id ) {
95 $matchId = intval( $id );
96 } else {
97 $matchId = 'page_latest';
99 return Revision::loadFromConds(
100 $db,
101 array( "rev_id=$matchId",
102 'page_id=rev_page',
103 'page_namespace' => $title->getNamespace(),
104 'page_title' => $title->getDbkey() ) );
108 * Load the revision for the given title with the given timestamp.
109 * WARNING: Timestamps may in some circumstances not be unique,
110 * so this isn't the best key to use.
112 * @param Database $db
113 * @param Title $title
114 * @param string $timestamp
115 * @return Revision
116 * @access public
117 * @static
119 function loadFromTimestamp( &$db, &$title, $timestamp ) {
120 return Revision::loadFromConds(
121 $db,
122 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
123 'page_id=rev_page',
124 'page_namespace' => $title->getNamespace(),
125 'page_title' => $title->getDbkey() ) );
129 * Given a set of conditions, fetch a revision.
131 * @param array $conditions
132 * @return Revision
133 * @static
134 * @access private
136 function newFromConds( $conditions ) {
137 $db =& wfGetDB( DB_SLAVE );
138 $row = Revision::loadFromConds( $db, $conditions );
139 if( is_null( $row ) ) {
140 $dbw =& wfGetDB( DB_MASTER );
141 $row = Revision::loadFromConds( $dbw, $conditions );
143 return $row;
147 * Given a set of conditions, fetch a revision from
148 * the given database connection.
150 * @param Database $db
151 * @param array $conditions
152 * @return Revision
153 * @static
154 * @access private
156 function loadFromConds( &$db, $conditions ) {
157 $res = Revision::fetchFromConds( $db, $conditions );
158 if( $res ) {
159 $row = $res->fetchObject();
160 $res->free();
161 if( $row ) {
162 $ret = new Revision( $row );
163 return $ret;
166 $ret = null;
167 return $ret;
171 * Return a wrapper for a series of database rows to
172 * fetch all of a given page's revisions in turn.
173 * Each row can be fed to the constructor to get objects.
175 * @param Title $title
176 * @return ResultWrapper
177 * @static
178 * @access public
180 function fetchAllRevisions( &$title ) {
181 return Revision::fetchFromConds(
182 wfGetDB( DB_SLAVE ),
183 array( 'page_namespace' => $title->getNamespace(),
184 'page_title' => $title->getDbkey(),
185 'page_id=rev_page' ) );
189 * Return a wrapper for a series of database rows to
190 * fetch all of a given page's revisions in turn.
191 * Each row can be fed to the constructor to get objects.
193 * @param Title $title
194 * @return ResultWrapper
195 * @static
196 * @access public
198 function fetchRevision( &$title ) {
199 return Revision::fetchFromConds(
200 wfGetDB( DB_SLAVE ),
201 array( 'rev_id=page_latest',
202 'page_namespace' => $title->getNamespace(),
203 'page_title' => $title->getDbkey(),
204 'page_id=rev_page' ) );
208 * Given a set of conditions, return a ResultWrapper
209 * which will return matching database rows with the
210 * fields necessary to build Revision objects.
212 * @param Database $db
213 * @param array $conditions
214 * @return ResultWrapper
215 * @static
216 * @access private
218 function fetchFromConds( &$db, $conditions ) {
219 $res = $db->select(
220 array( 'page', 'revision' ),
221 array( 'page_namespace',
222 'page_title',
223 'page_latest',
224 'rev_id',
225 'rev_page',
226 'rev_text_id',
227 'rev_comment',
228 'rev_user_text',
229 'rev_user',
230 'rev_minor_edit',
231 'rev_timestamp',
232 'rev_deleted' ),
233 $conditions,
234 'Revision::fetchRow',
235 array( 'LIMIT' => 1 ) );
236 $ret = $db->resultObject( $res );
237 return $ret;
241 * @param object $row
242 * @access private
244 function Revision( $row ) {
245 if( is_object( $row ) ) {
246 $this->mId = intval( $row->rev_id );
247 $this->mPage = intval( $row->rev_page );
248 $this->mTextId = intval( $row->rev_text_id );
249 $this->mComment = $row->rev_comment;
250 $this->mUserText = $row->rev_user_text;
251 $this->mUser = intval( $row->rev_user );
252 $this->mMinorEdit = intval( $row->rev_minor_edit );
253 $this->mTimestamp = $row->rev_timestamp;
254 $this->mDeleted = intval( $row->rev_deleted );
256 if( isset( $row->page_latest ) ) {
257 $this->mCurrent = ( $row->rev_id == $row->page_latest );
258 $this->mTitle = Title::makeTitle( $row->page_namespace,
259 $row->page_title );
260 } else {
261 $this->mCurrent = false;
262 $this->mTitle = null;
265 if( isset( $row->old_text ) ) {
266 $this->mText = $this->getRevisionText( $row );
267 } else {
268 $this->mText = null;
270 } elseif( is_array( $row ) ) {
271 // Build a new revision to be saved...
272 global $wgUser;
274 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
275 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
276 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
277 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
278 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
279 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
280 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
281 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
283 // Enforce spacing trimming on supplied text
284 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
285 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
287 $this->mTitle = null; # Load on demand if needed
288 $this->mCurrent = false;
289 } else {
290 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
294 /**#@+
295 * @access public
299 * @return int
301 function getId() {
302 return $this->mId;
306 * @return int
308 function getTextId() {
309 return $this->mTextId;
313 * Returns the title of the page associated with this entry.
314 * @return Title
316 function getTitle() {
317 if( isset( $this->mTitle ) ) {
318 return $this->mTitle;
320 $dbr =& wfGetDB( DB_SLAVE );
321 $row = $dbr->selectRow(
322 array( 'page', 'revision' ),
323 array( 'page_namespace', 'page_title' ),
324 array( 'page_id=rev_page',
325 'rev_id' => $this->mId ),
326 'Revision::getTItle' );
327 if( $row ) {
328 $this->mTitle = Title::makeTitle( $row->page_namespace,
329 $row->page_title );
331 return $this->mTitle;
335 * @return int
337 function getPage() {
338 return $this->mPage;
342 * Fetch revision's user id if it's available to all users
343 * @return int
345 function getUser() {
346 if( $this->isDeleted( MW_REV_DELETED_USER ) ) {
347 return 0;
348 } else {
349 return $this->mUser;
354 * Fetch revision's user id without regard for the current user's permissions
355 * @return string
357 function getRawUser() {
358 return $this->mUser;
362 * Fetch revision's username if it's available to all users
363 * @return string
365 function getUserText() {
366 if( $this->isDeleted( MW_REV_DELETED_USER ) ) {
367 return "";
368 } else {
369 return $this->mUserText;
374 * Fetch revision's username without regard for view restrictions
375 * @return string
377 function getRawUserText() {
378 return $this->mUserText;
382 * Fetch revision comment if it's available to all users
383 * @return string
385 function getComment() {
386 if( $this->isDeleted( MW_REV_DELETED_COMMENT ) ) {
387 return "";
388 } else {
389 return $this->mComment;
394 * Fetch revision comment without regard for the current user's permissions
395 * @return string
397 function getRawComment() {
398 return $this->mComment;
402 * @return bool
404 function isMinor() {
405 return (bool)$this->mMinorEdit;
409 * int $field one of MW_REV_DELETED_* bitfield constants
410 * @return bool
412 function isDeleted( $field ) {
413 return ($this->mDeleted & $field) == $field;
417 * Fetch revision text if it's available to all users
418 * @return string
420 function getText() {
421 if( $this->isDeleted( MW_REV_DELETED_TEXT ) ) {
422 return "";
423 } else {
424 return $this->getRawText();
429 * Fetch revision text without regard for view restrictions
430 * @return string
432 function getRawText() {
433 if( is_null( $this->mText ) ) {
434 // Revision text is immutable. Load on demand:
435 $this->mText = $this->loadText();
437 return $this->mText;
441 * @return string
443 function getTimestamp() {
444 return wfTimestamp(TS_MW, $this->mTimestamp);
448 * @return bool
450 function isCurrent() {
451 return $this->mCurrent;
455 * @return Revision
457 function getPrevious() {
458 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
459 if ( $prev ) {
460 return Revision::newFromTitle( $this->mTitle, $prev );
461 } else {
462 return null;
467 * @return Revision
469 function getNext() {
470 $next = $this->mTitle->getNextRevisionID( $this->mId );
471 if ( $next ) {
472 return Revision::newFromTitle( $this->mTitle, $next );
473 } else {
474 return null;
477 /**#@-*/
480 * Get revision text associated with an old or archive row
481 * $row is usually an object from wfFetchRow(), both the flags and the text
482 * field must be included
483 * @static
484 * @param integer $row Id of a row
485 * @param string $prefix table prefix (default 'old_')
486 * @return string $text|false the text requested
488 function getRevisionText( $row, $prefix = 'old_' ) {
489 $fname = 'Revision::getRevisionText';
490 wfProfileIn( $fname );
492 # Get data
493 $textField = $prefix . 'text';
494 $flagsField = $prefix . 'flags';
496 if( isset( $row->$flagsField ) ) {
497 $flags = explode( ',', $row->$flagsField );
498 } else {
499 $flags = array();
502 if( isset( $row->$textField ) ) {
503 $text = $row->$textField;
504 } else {
505 wfProfileOut( $fname );
506 return false;
509 # Use external methods for external objects, text in table is URL-only then
510 if ( in_array( 'external', $flags ) ) {
511 $url=$text;
512 @list($proto,$path)=explode('://',$url,2);
513 if ($path=="") {
514 wfProfileOut( $fname );
515 return false;
517 require_once('ExternalStore.php');
518 $text=ExternalStore::fetchFromURL($url);
521 // If the text was fetched without an error, convert it
522 if ( $text !== false ) {
523 if( in_array( 'gzip', $flags ) ) {
524 # Deal with optional compression of archived pages.
525 # This can be done periodically via maintenance/compressOld.php, and
526 # as pages are saved if $wgCompressRevisions is set.
527 $text = gzinflate( $text );
530 if( in_array( 'object', $flags ) ) {
531 # Generic compressed storage
532 $obj = unserialize( $text );
533 $text = $obj->getText();
536 global $wgLegacyEncoding;
537 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
538 # Old revisions kept around in a legacy encoding?
539 # Upconvert on demand.
540 global $wgInputEncoding, $wgContLang;
541 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
544 wfProfileOut( $fname );
545 return $text;
549 * If $wgCompressRevisions is enabled, we will compress data.
550 * The input string is modified in place.
551 * Return value is the flags field: contains 'gzip' if the
552 * data is compressed, and 'utf-8' if we're saving in UTF-8
553 * mode.
555 * @static
556 * @param mixed $text reference to a text
557 * @return string
559 function compressRevisionText( &$text ) {
560 global $wgCompressRevisions;
561 $flags = array();
563 # Revisions not marked this way will be converted
564 # on load if $wgLegacyCharset is set in the future.
565 $flags[] = 'utf-8';
567 if( $wgCompressRevisions ) {
568 if( function_exists( 'gzdeflate' ) ) {
569 $text = gzdeflate( $text );
570 $flags[] = 'gzip';
571 } else {
572 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
575 return implode( ',', $flags );
579 * Insert a new revision into the database, returning the new revision ID
580 * number on success and dies horribly on failure.
582 * @param Database $dbw
583 * @return int
585 function insertOn( &$dbw ) {
586 global $wgDefaultExternalStore;
588 $fname = 'Revision::insertOn';
589 wfProfileIn( $fname );
591 $data = $this->mText;
592 $flags = Revision::compressRevisionText( $data );
594 # Write to external storage if required
595 if ( $wgDefaultExternalStore ) {
596 if ( is_array( $wgDefaultExternalStore ) ) {
597 // Distribute storage across multiple clusters
598 $store = $wgDefaultExternalStore[mt_rand(0, count( $wgDefaultExternalStore ) - 1)];
599 } else {
600 $store = $wgDefaultExternalStore;
602 require_once('ExternalStore.php');
603 // Store and get the URL
604 $data = ExternalStore::insert( $store, $data );
605 if ( !$data ) {
606 # This should only happen in the case of a configuration error, where the external store is not valid
607 wfDebugDieBacktrace( "Unable to store text to external storage $store" );
609 if ( $flags ) {
610 $flags .= ',';
612 $flags .= 'external';
615 # Record the text (or external storage URL) to the text table
616 if( !isset( $this->mTextId ) ) {
617 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
618 $dbw->insert( 'text',
619 array(
620 'old_id' => $old_id,
621 'old_text' => $data,
622 'old_flags' => $flags,
623 ), $fname
625 $this->mTextId = $dbw->insertId();
628 # Record the edit in revisions
629 $rev_id = isset( $this->mId )
630 ? $this->mId
631 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
632 $dbw->insert( 'revision',
633 array(
634 'rev_id' => $rev_id,
635 'rev_page' => $this->mPage,
636 'rev_text_id' => $this->mTextId,
637 'rev_comment' => $this->mComment,
638 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
639 'rev_user' => $this->mUser,
640 'rev_user_text' => $this->mUserText,
641 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
642 'rev_deleted' => $this->mDeleted,
643 ), $fname
646 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
648 wfProfileOut( $fname );
649 return $this->mId;
653 * Lazy-load the revision's text.
654 * Currently hardcoded to the 'text' table storage engine.
656 * @return string
657 * @access private
659 function loadText() {
660 $fname = 'Revision::loadText';
661 wfProfileIn( $fname );
663 $dbr =& wfGetDB( DB_SLAVE );
664 $row = $dbr->selectRow( 'text',
665 array( 'old_text', 'old_flags' ),
666 array( 'old_id' => $this->getTextId() ),
667 $fname);
669 if( !$row ) {
670 $dbw =& wfGetDB( DB_MASTER );
671 $row = $dbw->selectRow( 'text',
672 array( 'old_text', 'old_flags' ),
673 array( 'old_id' => $this->getTextId() ),
674 $fname);
677 $text = Revision::getRevisionText( $row );
678 wfProfileOut( $fname );
680 return $text;
684 * Create a new null-revision for insertion into a page's
685 * history. This will not re-save the text, but simply refer
686 * to the text from the previous version.
688 * Such revisions can for instance identify page rename
689 * operations and other such meta-modifications.
691 * @param Database $dbw
692 * @param int $pageId ID number of the page to read from
693 * @param string $summary
694 * @param bool $minor
695 * @return Revision
697 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
698 $fname = 'Revision::newNullRevision';
699 wfProfileIn( $fname );
701 $current = $dbw->selectRow(
702 array( 'page', 'revision' ),
703 array( 'page_latest', 'rev_text_id' ),
704 array(
705 'page_id' => $pageId,
706 'page_latest=rev_id',
708 $fname );
710 if( $current ) {
711 $revision = new Revision( array(
712 'page' => $pageId,
713 'comment' => $summary,
714 'minor_edit' => $minor,
715 'text_id' => $current->rev_text_id,
716 ) );
717 } else {
718 $revision = null;
721 wfProfileOut( $fname );
722 return $revision;
726 * Determine if the current user is allowed to view a particular
727 * field of this revision, if it's marked as deleted.
728 * @param int $field one of MW_REV_DELETED_TEXT,
729 * MW_REV_DELETED_COMMENT,
730 * MW_REV_DELETED_USER
731 * @return bool
733 function userCan( $field ) {
734 if( ( $this->mDeleted & $field ) == $field ) {
735 global $wgUser;
736 $permission = ( $this->mDeleted & MW_REV_DELETED_RESTRICTED ) == MW_REV_DELETED_RESTRICTED
737 ? 'hiderevision'
738 : 'deleterevision';
739 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
740 return $wgUser->isAllowed( $permission );
741 } else {
742 return true;