Use Revision for individual message loads; not using it for bulk load just now; need...
[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_comment',
169 'rev_user_text',
170 'rev_user',
171 'rev_minor_edit',
172 'rev_timestamp' ),
173 $conditions,
174 'Revision::fetchRow' );
175 return $db->resultObject( $res );
176 }
177
178 /**
179 * @param object $row
180 * @access private
181 */
182 function Revision( $row ) {
183 if( is_object( $row ) ) {
184 $this->mId = IntVal( $row->rev_id );
185 $this->mPage = IntVal( $row->rev_page );
186 $this->mComment = $row->rev_comment;
187 $this->mUserText = $row->rev_user_text;
188 $this->mUser = IntVal( $row->rev_user );
189 $this->mMinorEdit = IntVal( $row->rev_minor_edit );
190 $this->mTimestamp = $row->rev_timestamp;
191
192 $this->mCurrent = ( $row->rev_id == $row->page_latest );
193 $this->mTitle = Title::makeTitle( $row->page_namespace,
194 $row->page_title );
195
196 if( isset( $row->old_text ) ) {
197 $this->mText = $this->getRevisionText( $row );
198 } else {
199 $this->mText = null;
200 }
201 } elseif( is_array( $row ) ) {
202 // Build a new revision to be saved...
203 global $wgUser;
204
205 $this->mId = isset( $row['id'] ) ? IntVal( $row['id'] ) : null;
206 $this->mPage = isset( $row['page'] ) ? IntVal( $row['page'] ) : null;
207 $this->mComment = isset( $row['comment'] ) ? StrVal( $row['comment'] ) : null;
208 $this->mUserText = isset( $row['user_text'] ) ? StrVal( $row['user_text'] ) : $wgUser->getName();
209 $this->mUser = isset( $row['user'] ) ? IntVal( $row['user'] ) : $wgUser->getId();
210 $this->mMinorEdit = isset( $row['minor_edit'] ) ? IntVal( $row['minor_edit'] ) : 0;
211 $this->mTimestamp = isset( $row['timestamp'] ) ? StrVal( $row['timestamp'] ) : wfTimestamp( TS_MW );
212 $this->mText = isset( $row['text'] ) ? StrVal( $row['text'] ) : null;
213
214 $this->mTitle = null; # Load on demand if needed
215 $this->mCurrent = false;
216 } else {
217 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
218 }
219 }
220
221 /**#@+
222 * @access public
223 */
224
225 /**
226 * @return int
227 */
228 function getId() {
229 return $this->mId;
230 }
231
232 /**
233 * Returns the title of the page associated with this entry.
234 * @return Title
235 */
236 function &getTitle() {
237 if( isset( $this->mTitle ) ) {
238 return $this->mTitle;
239 }
240 $dbr =& wfGetDB( DB_SLAVE );
241 $row = $dbr->selectRow(
242 array( 'page', 'revision' ),
243 array( 'page_namespace', 'page_title' ),
244 array( 'page_id=rev_page',
245 'rev_id' => $this->mId ),
246 'Revision::getTItle' );
247 if( $row ) {
248 $this->mTitle =& Title::makeTitle( $row->page_namespace,
249 $row->page_title );
250 }
251 return $this->mTitle;
252 }
253
254 /**
255 * @return int
256 */
257 function getPage() {
258 return $this->mPage;
259 }
260
261 /**
262 * @return int
263 */
264 function getUser() {
265 return $this->mUser;
266 }
267
268 /**
269 * @return string
270 */
271 function getUserText() {
272 return $this->mUserText;
273 }
274
275 /**
276 * @return string
277 */
278 function getComment() {
279 return $this->mComment;
280 }
281
282 /**
283 * @return bool
284 */
285 function isMinor() {
286 return (bool)$this->mMinorEdit;
287 }
288
289 /**
290 * @return string
291 */
292 function getText() {
293 if( is_null( $this->mText ) ) {
294 // Revision text is immutable. Load on demand:
295 $this->mText = $this->loadText();
296 }
297 return $this->mText;
298 }
299
300 /**
301 * @return string
302 */
303 function getTimestamp() {
304 return $this->mTimestamp;
305 }
306
307 /**
308 * @return bool
309 */
310 function isCurrent() {
311 return $this->mCurrent;
312 }
313
314 /**
315 * @return Revision
316 */
317 function &getPrevious() {
318 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
319 return Revision::newFromTitle( $this->mTitle, $prev );
320 }
321
322 /**
323 * @return Revision
324 */
325 function &getNext() {
326 $next = $this->mTitle->getNextRevisionID( $this->mId );
327 return Revision::newFromTitle( $this->mTitle, $next );
328 }
329 /**#@-*/
330
331 /**
332 * Get revision text associated with an old or archive row
333 * $row is usually an object from wfFetchRow(), both the flags and the text
334 * field must be included
335 * @static
336 * @param integer $row Id of a row
337 * @param string $prefix table prefix (default 'old_')
338 * @return string $text|false the text requested
339 */
340 function getRevisionText( $row, $prefix = 'old_' ) {
341 $fname = 'Revision::getRevisionText';
342 wfProfileIn( $fname );
343
344 # Get data
345 $textField = $prefix . 'text';
346 $flagsField = $prefix . 'flags';
347
348 if( isset( $row->$flagsField ) ) {
349 $flags = explode( ',', $row->$flagsField );
350 } else {
351 $flags = array();
352 }
353
354 if( isset( $row->$textField ) ) {
355 $text = $row->$textField;
356 } else {
357 wfProfileOut( $fname );
358 return false;
359 }
360
361 if( in_array( 'gzip', $flags ) ) {
362 # Deal with optional compression of archived pages.
363 # This can be done periodically via maintenance/compressOld.php, and
364 # as pages are saved if $wgCompressRevisions is set.
365 $text = gzinflate( $text );
366 }
367
368 if( in_array( 'object', $flags ) ) {
369 # Generic compressed storage
370 $obj = unserialize( $text );
371
372 # Bugger, corrupted my test database by double-serializing
373 if ( !is_object( $obj ) ) {
374 $obj = unserialize( $obj );
375 }
376
377 $text = $obj->getText();
378 }
379
380 global $wgLegacyEncoding;
381 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
382 # Old revisions kept around in a legacy encoding?
383 # Upconvert on demand.
384 global $wgInputEncoding, $wgContLang;
385 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
386 }
387 wfProfileOut( $fname );
388 return $text;
389 }
390
391 /**
392 * If $wgCompressRevisions is enabled, we will compress data.
393 * The input string is modified in place.
394 * Return value is the flags field: contains 'gzip' if the
395 * data is compressed, and 'utf-8' if we're saving in UTF-8
396 * mode.
397 *
398 * @static
399 * @param mixed $text reference to a text
400 * @return string
401 */
402 function compressRevisionText( &$text ) {
403 global $wgCompressRevisions, $wgUseLatin1;
404 $flags = array();
405 if( !$wgUseLatin1 ) {
406 # Revisions not marked this way will be converted
407 # on load if $wgLegacyCharset is set in the future.
408 $flags[] = 'utf-8';
409 }
410 if( $wgCompressRevisions ) {
411 if( function_exists( 'gzdeflate' ) ) {
412 $text = gzdeflate( $text );
413 $flags[] = 'gzip';
414 } else {
415 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
416 }
417 }
418 return implode( ',', $flags );
419 }
420
421 /**
422 * Insert a new revision into the database, returning the new revision ID
423 * number on success and dies horribly on failure.
424 *
425 * @param Database $dbw
426 * @return int
427 */
428 function insertOn( &$dbw ) {
429 $fname = 'Revision::insertOn';
430 wfProfileIn( $fname );
431
432 $mungedText = $this->mText;
433 $flags = Revision::compressRevisionText( $mungedText );
434
435 # Record the text to the text table
436 $old_id = isset( $this->mId )
437 ? $this->mId
438 : $dbw->nextSequenceValue( 'text_old_id_val' );
439 $dbw->insert( 'text',
440 array(
441 'old_id' => $old_id,
442 'old_text' => $mungedText,
443 'old_flags' => $flags,
444 ), $fname
445 );
446 $revisionId = $dbw->insertId();
447
448 # Record the edit in revisions
449 $dbw->insert( 'revision',
450 array(
451 'rev_id' => $revisionId,
452 'rev_page' => $this->mPage,
453 'rev_comment' => $this->mComment,
454 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
455 'rev_user' => $this->mUser,
456 'rev_user_text' => $this->mUserText,
457 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
458 ), $fname
459 );
460
461 $this->mId = $revisionId;
462
463 wfProfileOut( $fname );
464 return $revisionId;
465 }
466
467 /**
468 * Lazy-load the revision's text.
469 * Currently hardcoded to the 'text' table storage engine.
470 *
471 * @return string
472 * @access private
473 */
474 function loadText() {
475 $fname = 'Revision::loadText';
476 wfProfileIn( $fname );
477
478 $dbr =& wfGetDB( DB_SLAVE );
479 $row = $dbr->selectRow( 'text',
480 array( 'old_text', 'old_flags' ),
481 array( 'old_id' => $this->getId() ),
482 $fname);
483
484 $text = Revision::getRevisionText( $row );
485 wfProfileOut( $fname );
486
487 return $text;
488 }
489 }
490 ?>