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