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