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