Revision::newFromConds -- grab from master if no result on slave
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @todo document
5 */
6
7 /** */
8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
10
11 /**
12 * @package MediaWiki
13 * @todo document
14 */
15 class Revision {
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 * @static
22 * @access public
23 */
24 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 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 either the current, or a specified, revision
56 * that's attached to a given page. If not attached
57 * to that page, will return null.
58 *
59 * @param Database $db
60 * @param int $pageid
61 * @param int $id
62 * @return Revision
63 * @access public
64 */
65 function &loadFromPageId( &$db, $pageid, $id = 0 ) {
66 if( $id ) {
67 $matchId = IntVal( $id );
68 } else {
69 $matchId = 'page_latest';
70 }
71 return Revision::loadFromConds(
72 $db,
73 array( "rev_id=$matchId",
74 'rev_page' => IntVal( $pageid ),
75 'page_id=rev_page' ) );
76 }
77
78 /**
79 * Load the revision for the given title with the given timestamp.
80 * WARNING: Timestamps may in some circumstances not be unique,
81 * so this isn't the best key to use.
82 *
83 * @param Database $db
84 * @param Title $title
85 * @param string $timestamp
86 * @return Revision
87 * @access public
88 * @static
89 */
90 function &loadFromTimestamp( &$db, &$title, $timestamp ) {
91 return Revision::loadFromConds(
92 $db,
93 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
94 'page_id=rev_page',
95 'page_namespace' => $title->getNamespace(),
96 'page_title' => $title->getDbkey() ) );
97 }
98
99 /**
100 * Given a set of conditions, fetch a revision.
101 *
102 * @param array $conditions
103 * @return Revision
104 * @static
105 * @access private
106 */
107 function &newFromConds( $conditions ) {
108 $db =& wfGetDB( DB_SLAVE );
109 $row = Revision::loadFromConds( $db, $conditions );
110 if( is_null( $row ) ) {
111 $dbw =& wfGetDB( DB_MASTER );
112 $row = Revision::loadFromConds( $dbw, $conditions );
113 }
114 return $row;
115 }
116
117 /**
118 * Given a set of conditions, fetch a revision from
119 * the given database connection.
120 *
121 * @param Database $db
122 * @param array $conditions
123 * @return Revision
124 * @static
125 * @access private
126 */
127 function &loadFromConds( &$db, $conditions ) {
128 $res =& Revision::fetchFromConds( $db, $conditions );
129 if( $res ) {
130 $row = $res->fetchObject();
131 $res->free();
132 if( $row ) {
133 return new Revision( $row );
134 }
135 }
136 return null;
137 }
138
139 /**
140 * Return a wrapper for a series of database rows to
141 * fetch all of a given page's revisions in turn.
142 * Each row can be fed to the constructor to get objects.
143 *
144 * @param Title $title
145 * @return ResultWrapper
146 * @static
147 * @access public
148 */
149 function &fetchAllRevisions( &$title ) {
150 return Revision::fetchFromConds(
151 wfGetDB( DB_SLAVE ),
152 array( 'page_namespace' => $title->getNamespace(),
153 'page_title' => $title->getDbkey(),
154 'page_id=rev_page' ) );
155 }
156
157 /**
158 * Return a wrapper for a series of database rows to
159 * fetch all of a given page's revisions in turn.
160 * Each row can be fed to the constructor to get objects.
161 *
162 * @param Title $title
163 * @return ResultWrapper
164 * @static
165 * @access public
166 */
167 function &fetchRevision( &$title ) {
168 return Revision::fetchFromConds(
169 wfGetDB( DB_SLAVE ),
170 array( 'rev_id=page_latest',
171 'page_namespace' => $title->getNamespace(),
172 'page_title' => $title->getDbkey(),
173 'page_id=rev_page' ) );
174 }
175
176 /**
177 * Given a set of conditions, return a ResultWrapper
178 * which will return matching database rows with the
179 * fields necessary to build Revision objects.
180 *
181 * @param Database $db
182 * @param array $conditions
183 * @return ResultWrapper
184 * @static
185 * @access private
186 */
187 function &fetchFromConds( &$db, $conditions ) {
188 $res = $db->select(
189 array( 'page', 'revision' ),
190 array( 'page_namespace',
191 'page_title',
192 'page_latest',
193 'rev_id',
194 'rev_page',
195 'rev_text_id',
196 'rev_comment',
197 'rev_user_text',
198 'rev_user',
199 'rev_minor_edit',
200 'rev_timestamp',
201 'rev_deleted' ),
202 $conditions,
203 'Revision::fetchRow',
204 array( 'LIMIT' => 1 ) );
205 return $db->resultObject( $res );
206 }
207
208 /**
209 * @param object $row
210 * @access private
211 */
212 function Revision( $row ) {
213 if( is_object( $row ) ) {
214 $this->mId = IntVal( $row->rev_id );
215 $this->mPage = IntVal( $row->rev_page );
216 $this->mTextId = IntVal( $row->rev_text_id );
217 $this->mComment = $row->rev_comment;
218 $this->mUserText = $row->rev_user_text;
219 $this->mUser = IntVal( $row->rev_user );
220 $this->mMinorEdit = IntVal( $row->rev_minor_edit );
221 $this->mTimestamp = $row->rev_timestamp;
222 $this->mDeleted = IntVal( $row->rev_deleted );
223
224 $this->mCurrent = ( $row->rev_id == $row->page_latest );
225 $this->mTitle = Title::makeTitle( $row->page_namespace,
226 $row->page_title );
227
228 if( isset( $row->old_text ) ) {
229 $this->mText = $this->getRevisionText( $row );
230 } else {
231 $this->mText = null;
232 }
233 } elseif( is_array( $row ) ) {
234 // Build a new revision to be saved...
235 global $wgUser;
236
237 $this->mId = isset( $row['id'] ) ? IntVal( $row['id'] ) : null;
238 $this->mPage = isset( $row['page'] ) ? IntVal( $row['page'] ) : null;
239 $this->mTextId = isset( $row['text_id'] ) ? IntVal( $row['text_id'] ) : null;
240 $this->mUserText = isset( $row['user_text'] ) ? StrVal( $row['user_text'] ) : $wgUser->getName();
241 $this->mUser = isset( $row['user'] ) ? IntVal( $row['user'] ) : $wgUser->getId();
242 $this->mMinorEdit = isset( $row['minor_edit'] ) ? IntVal( $row['minor_edit'] ) : 0;
243 $this->mTimestamp = isset( $row['timestamp'] ) ? StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW );
244 $this->mDeleted = isset( $row['deleted'] ) ? IntVal( $row['deleted'] ) : 0;
245
246 // Enforce spacing trimming on supplied text
247 $this->mComment = isset( $row['comment'] ) ? trim( StrVal( $row['comment'] ) ) : null;
248 $this->mText = isset( $row['text'] ) ? rtrim( StrVal( $row['text'] ) ) : null;
249
250 $this->mTitle = null; # Load on demand if needed
251 $this->mCurrent = false;
252 } else {
253 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
254 }
255 }
256
257 /**#@+
258 * @access public
259 */
260
261 /**
262 * @return int
263 */
264 function getId() {
265 return $this->mId;
266 }
267
268 /**
269 * @return int
270 */
271 function getTextId() {
272 return $this->mTextId;
273 }
274
275 /**
276 * Returns the title of the page associated with this entry.
277 * @return Title
278 */
279 function &getTitle() {
280 if( isset( $this->mTitle ) ) {
281 return $this->mTitle;
282 }
283 $dbr =& wfGetDB( DB_SLAVE );
284 $row = $dbr->selectRow(
285 array( 'page', 'revision' ),
286 array( 'page_namespace', 'page_title' ),
287 array( 'page_id=rev_page',
288 'rev_id' => $this->mId ),
289 'Revision::getTItle' );
290 if( $row ) {
291 $this->mTitle =& Title::makeTitle( $row->page_namespace,
292 $row->page_title );
293 }
294 return $this->mTitle;
295 }
296
297 /**
298 * @return int
299 */
300 function getPage() {
301 return $this->mPage;
302 }
303
304 /**
305 * @return int
306 */
307 function getUser() {
308 return $this->mUser;
309 }
310
311 /**
312 * @return string
313 */
314 function getUserText() {
315 return $this->mUserText;
316 }
317
318 /**
319 * @return string
320 */
321 function getComment() {
322 return $this->mComment;
323 }
324
325 /**
326 * @return bool
327 */
328 function isMinor() {
329 return (bool)$this->mMinorEdit;
330 }
331
332 /**
333 * @return bool
334 */
335 function isDeleted() {
336 return (bool)$this->mDeleted;
337 }
338
339 /**
340 * @return string
341 */
342 function getText() {
343 if( is_null( $this->mText ) ) {
344 // Revision text is immutable. Load on demand:
345 $this->mText = $this->loadText();
346 }
347 return $this->mText;
348 }
349
350 /**
351 * @return string
352 */
353 function getTimestamp() {
354 return $this->mTimestamp;
355 }
356
357 /**
358 * @return bool
359 */
360 function isCurrent() {
361 return $this->mCurrent;
362 }
363
364 /**
365 * @return Revision
366 */
367 function &getPrevious() {
368 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
369 return Revision::newFromTitle( $this->mTitle, $prev );
370 }
371
372 /**
373 * @return Revision
374 */
375 function &getNext() {
376 $next = $this->mTitle->getNextRevisionID( $this->mId );
377 return Revision::newFromTitle( $this->mTitle, $next );
378 }
379 /**#@-*/
380
381 /**
382 * Get revision text associated with an old or archive row
383 * $row is usually an object from wfFetchRow(), both the flags and the text
384 * field must be included
385 * @static
386 * @param integer $row Id of a row
387 * @param string $prefix table prefix (default 'old_')
388 * @return string $text|false the text requested
389 */
390 function getRevisionText( $row, $prefix = 'old_' ) {
391 $fname = 'Revision::getRevisionText';
392 wfProfileIn( $fname );
393
394 # Get data
395 $textField = $prefix . 'text';
396 $flagsField = $prefix . 'flags';
397
398 if( isset( $row->$flagsField ) ) {
399 $flags = explode( ',', $row->$flagsField );
400 } else {
401 $flags = array();
402 }
403
404 if( isset( $row->$textField ) ) {
405 $text = $row->$textField;
406 } else {
407 wfProfileOut( $fname );
408 return false;
409 }
410
411 # Use external methods for external objects, text in table is URL-only then
412 if ( in_array( 'external', $flags ) ) {
413 $url=$text;
414 @list($proto,$path)=explode('://',$url,2);
415 if ($path=="") {
416 wfProfileOut( $fname );
417 return false;
418 }
419 require_once('ExternalStore.php');
420 $text=ExternalStore::fetchFromURL($url);
421 }
422
423 if( in_array( 'gzip', $flags ) ) {
424 # Deal with optional compression of archived pages.
425 # This can be done periodically via maintenance/compressOld.php, and
426 # as pages are saved if $wgCompressRevisions is set.
427 $text = gzinflate( $text );
428 }
429
430 if( in_array( 'object', $flags ) ) {
431 # Generic compressed storage
432 $obj = unserialize( $text );
433
434 # Bugger, corrupted my test database by double-serializing
435 if ( !is_object( $obj ) ) {
436 $obj = unserialize( $obj );
437 }
438
439 $text = $obj->getText();
440 }
441
442 global $wgLegacyEncoding;
443 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
444 # Old revisions kept around in a legacy encoding?
445 # Upconvert on demand.
446 global $wgInputEncoding, $wgContLang;
447 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
448 }
449 wfProfileOut( $fname );
450 return $text;
451 }
452
453 /**
454 * If $wgCompressRevisions is enabled, we will compress data.
455 * The input string is modified in place.
456 * Return value is the flags field: contains 'gzip' if the
457 * data is compressed, and 'utf-8' if we're saving in UTF-8
458 * mode.
459 *
460 * @static
461 * @param mixed $text reference to a text
462 * @return string
463 */
464 function compressRevisionText( &$text ) {
465 global $wgCompressRevisions;
466 $flags = array();
467
468 # Revisions not marked this way will be converted
469 # on load if $wgLegacyCharset is set in the future.
470 $flags[] = 'utf-8';
471
472 if( $wgCompressRevisions ) {
473 if( function_exists( 'gzdeflate' ) ) {
474 $text = gzdeflate( $text );
475 $flags[] = 'gzip';
476 } else {
477 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
478 }
479 }
480 return implode( ',', $flags );
481 }
482
483 /**
484 * Insert a new revision into the database, returning the new revision ID
485 * number on success and dies horribly on failure.
486 *
487 * @param Database $dbw
488 * @return int
489 */
490 function insertOn( &$dbw ) {
491 $fname = 'Revision::insertOn';
492 wfProfileIn( $fname );
493
494 $mungedText = $this->mText;
495 $flags = Revision::compressRevisionText( $mungedText );
496
497 # Record the text to the text table
498 if( !isset( $this->mTextId ) ) {
499 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
500 $dbw->insert( 'text',
501 array(
502 'old_id' => $old_id,
503 'old_text' => $mungedText,
504 'old_flags' => $flags,
505 ), $fname
506 );
507 $this->mTextId = $dbw->insertId();
508 }
509
510 # Record the edit in revisions
511 $rev_id = isset( $this->mId )
512 ? $this->mId
513 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
514 $dbw->insert( 'revision',
515 array(
516 'rev_id' => $rev_id,
517 'rev_page' => $this->mPage,
518 'rev_text_id' => $this->mTextId,
519 'rev_comment' => $this->mComment,
520 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
521 'rev_user' => $this->mUser,
522 'rev_user_text' => $this->mUserText,
523 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
524 'rev_deleted' => $this->mDeleted,
525 ), $fname
526 );
527
528 $this->mId = $dbw->insertId();
529
530 wfProfileOut( $fname );
531 return $this->mId;
532 }
533
534 /**
535 * Lazy-load the revision's text.
536 * Currently hardcoded to the 'text' table storage engine.
537 *
538 * @return string
539 * @access private
540 */
541 function loadText() {
542 $fname = 'Revision::loadText';
543 wfProfileIn( $fname );
544
545 $dbr =& wfGetDB( DB_SLAVE );
546 $row = $dbr->selectRow( 'text',
547 array( 'old_text', 'old_flags' ),
548 array( 'old_id' => $this->getTextId() ),
549 $fname);
550
551 $text = Revision::getRevisionText( $row );
552 wfProfileOut( $fname );
553
554 return $text;
555 }
556
557 /**
558 * Create a new null-revision for insertion into a page's
559 * history. This will not re-save the text, but simply refer
560 * to the text from the previous version.
561 *
562 * Such revisions can for instance identify page rename
563 * operations and other such meta-modifications.
564 *
565 * @param Database $dbw
566 * @param int $pageId ID number of the page to read from
567 * @param string $summary
568 * @param bool $minor
569 * @return Revision
570 */
571 function &newNullRevision( &$dbw, $pageId, $summary, $minor ) {
572 $fname = 'Revision::newNullRevision';
573 wfProfileIn( $fname );
574
575 $current = $dbw->selectRow(
576 array( 'page', 'revision' ),
577 array( 'page_latest', 'rev_text_id' ),
578 array(
579 'page_id' => $pageId,
580 'page_latest=rev_id',
581 ),
582 $fname );
583
584 if( $current ) {
585 $revision = new Revision( array(
586 'page' => $pageId,
587 'comment' => $summary,
588 'minor_edit' => $minor,
589 'text_id' => $current->rev_text_id,
590 ) );
591 } else {
592 $revision = null;
593 }
594
595 wfProfileOut( $fname );
596 return $revision;
597 }
598
599 }
600 ?>