Fix #2571: Duplicate anchors and missing form id tag in search form on Cologne Blue...
[mediawiki.git] / includes / Revision.php
blob1a367f0b57169dfe73a3b46fb644768c8232a695
1 <?php
2 /**
3 * @package MediaWiki
4 * @todo document
5 */
7 /** */
8 require_once( 'Database.php' );
9 require_once( 'Article.php' );
11 /**
12 * @package MediaWiki
13 * @todo document
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.
20 * @param int $id
21 * @static
22 * @access public
24 function newFromId( $id ) {
25 return Revision::newFromConds(
26 array( 'page_id=rev_page',
27 'rev_id' => intval( $id ) ) );
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.
35 * @param Title $title
36 * @param int $id
37 * @return Revision
38 * @access public
39 * @static
41 function newFromTitle( &$title, $id = 0 ) {
42 if( $id ) {
43 $matchId = intval( $id );
44 } else {
45 $matchId = 'page_latest';
47 return Revision::newFromConds(
48 array( "rev_id=$matchId",
49 'page_id=rev_page',
50 'page_namespace' => $title->getNamespace(),
51 'page_title' => $title->getDbkey() ) );
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.
59 * @param Database $db
60 * @param int $pageid
61 * @param int $id
62 * @return Revision
63 * @access public
65 function loadFromPageId( &$db, $pageid, $id = 0 ) {
66 if( $id ) {
67 $matchId = intval( $id );
68 } else {
69 $matchId = 'page_latest';
71 $ret = Revision::loadFromConds(
72 $db,
73 array( "rev_id=$matchId",
74 'rev_page' => intval( $pageid ),
75 'page_id=rev_page' ) );
76 return $ret;
79 /**
80 * Load either the current, or a specified, revision
81 * that's attached to a given page. If not attached
82 * to that page, will return null.
84 * @param Database $db
85 * @param Title $title
86 * @param int $id
87 * @return Revision
88 * @access public
90 function loadFromTitle( &$db, $title, $id = 0 ) {
91 if( $id ) {
92 $matchId = intval( $id );
93 } else {
94 $matchId = 'page_latest';
96 return Revision::loadFromConds(
97 $db,
98 array( "rev_id=$matchId",
99 'page_id=rev_page',
100 'page_namespace' => $title->getNamespace(),
101 'page_title' => $title->getDbkey() ) );
105 * Load the revision for the given title with the given timestamp.
106 * WARNING: Timestamps may in some circumstances not be unique,
107 * so this isn't the best key to use.
109 * @param Database $db
110 * @param Title $title
111 * @param string $timestamp
112 * @return Revision
113 * @access public
114 * @static
116 function loadFromTimestamp( &$db, &$title, $timestamp ) {
117 return Revision::loadFromConds(
118 $db,
119 array( 'rev_timestamp' => $db->timestamp( $timestamp ),
120 'page_id=rev_page',
121 'page_namespace' => $title->getNamespace(),
122 'page_title' => $title->getDbkey() ) );
126 * Given a set of conditions, fetch a revision.
128 * @param array $conditions
129 * @return Revision
130 * @static
131 * @access private
133 function newFromConds( $conditions ) {
134 $db =& wfGetDB( DB_SLAVE );
135 $row = Revision::loadFromConds( $db, $conditions );
136 if( is_null( $row ) ) {
137 $dbw =& wfGetDB( DB_MASTER );
138 $row = Revision::loadFromConds( $dbw, $conditions );
140 return $row;
144 * Given a set of conditions, fetch a revision from
145 * the given database connection.
147 * @param Database $db
148 * @param array $conditions
149 * @return Revision
150 * @static
151 * @access private
153 function loadFromConds( &$db, $conditions ) {
154 $res = Revision::fetchFromConds( $db, $conditions );
155 if( $res ) {
156 $row = $res->fetchObject();
157 $res->free();
158 if( $row ) {
159 $ret = new Revision( $row );
160 return $ret;
163 $ret = null;
164 return $ret;
168 * Return a wrapper for a series of database rows to
169 * fetch all of a given page's revisions in turn.
170 * Each row can be fed to the constructor to get objects.
172 * @param Title $title
173 * @return ResultWrapper
174 * @static
175 * @access public
177 function fetchAllRevisions( &$title ) {
178 return Revision::fetchFromConds(
179 wfGetDB( DB_SLAVE ),
180 array( 'page_namespace' => $title->getNamespace(),
181 'page_title' => $title->getDbkey(),
182 'page_id=rev_page' ) );
186 * Return a wrapper for a series of database rows to
187 * fetch all of a given page's revisions in turn.
188 * Each row can be fed to the constructor to get objects.
190 * @param Title $title
191 * @return ResultWrapper
192 * @static
193 * @access public
195 function fetchRevision( &$title ) {
196 return Revision::fetchFromConds(
197 wfGetDB( DB_SLAVE ),
198 array( 'rev_id=page_latest',
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDbkey(),
201 'page_id=rev_page' ) );
205 * Given a set of conditions, return a ResultWrapper
206 * which will return matching database rows with the
207 * fields necessary to build Revision objects.
209 * @param Database $db
210 * @param array $conditions
211 * @return ResultWrapper
212 * @static
213 * @access private
215 function fetchFromConds( &$db, $conditions ) {
216 $res = $db->select(
217 array( 'page', 'revision' ),
218 array( 'page_namespace',
219 'page_title',
220 'page_latest',
221 'rev_id',
222 'rev_page',
223 'rev_text_id',
224 'rev_comment',
225 'rev_user_text',
226 'rev_user',
227 'rev_minor_edit',
228 'rev_timestamp',
229 'rev_deleted' ),
230 $conditions,
231 'Revision::fetchRow',
232 array( 'LIMIT' => 1 ) );
233 $ret = $db->resultObject( $res );
234 return $ret;
238 * @param object $row
239 * @access private
241 function Revision( $row ) {
242 if( is_object( $row ) ) {
243 $this->mId = intval( $row->rev_id );
244 $this->mPage = intval( $row->rev_page );
245 $this->mTextId = intval( $row->rev_text_id );
246 $this->mComment = $row->rev_comment;
247 $this->mUserText = $row->rev_user_text;
248 $this->mUser = intval( $row->rev_user );
249 $this->mMinorEdit = intval( $row->rev_minor_edit );
250 $this->mTimestamp = $row->rev_timestamp;
251 $this->mDeleted = intval( $row->rev_deleted );
253 $this->mCurrent = ( $row->rev_id == $row->page_latest );
254 $this->mTitle = Title::makeTitle( $row->page_namespace,
255 $row->page_title );
257 if( isset( $row->old_text ) ) {
258 $this->mText = $this->getRevisionText( $row );
259 } else {
260 $this->mText = null;
262 } elseif( is_array( $row ) ) {
263 // Build a new revision to be saved...
264 global $wgUser;
266 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
267 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
268 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
269 $this->mUserText = isset( $row['user_text'] ) ? strval( $row['user_text'] ) : $wgUser->getName();
270 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
271 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
272 $this->mTimestamp = isset( $row['timestamp'] ) ? strval( $row['timestamp'] ) : wfTimestamp( TS_MW );
273 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
275 // Enforce spacing trimming on supplied text
276 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
277 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
279 $this->mTitle = null; # Load on demand if needed
280 $this->mCurrent = false;
281 } else {
282 wfDebugDieBacktrace( 'Revision constructor passed invalid row format.' );
286 /**#@+
287 * @access public
291 * @return int
293 function getId() {
294 return $this->mId;
298 * @return int
300 function getTextId() {
301 return $this->mTextId;
305 * Returns the title of the page associated with this entry.
306 * @return Title
308 function getTitle() {
309 if( isset( $this->mTitle ) ) {
310 return $this->mTitle;
312 $dbr =& wfGetDB( DB_SLAVE );
313 $row = $dbr->selectRow(
314 array( 'page', 'revision' ),
315 array( 'page_namespace', 'page_title' ),
316 array( 'page_id=rev_page',
317 'rev_id' => $this->mId ),
318 'Revision::getTItle' );
319 if( $row ) {
320 $this->mTitle = Title::makeTitle( $row->page_namespace,
321 $row->page_title );
323 return $this->mTitle;
327 * @return int
329 function getPage() {
330 return $this->mPage;
334 * @return int
336 function getUser() {
337 return $this->mUser;
341 * @return string
343 function getUserText() {
344 return $this->mUserText;
348 * @return string
350 function getComment() {
351 return $this->mComment;
355 * @return bool
357 function isMinor() {
358 return (bool)$this->mMinorEdit;
362 * @return bool
364 function isDeleted() {
365 return (bool)$this->mDeleted;
369 * @return string
371 function getText() {
372 if( is_null( $this->mText ) ) {
373 // Revision text is immutable. Load on demand:
374 $this->mText = $this->loadText();
376 return $this->mText;
380 * @return string
382 function getTimestamp() {
383 return wfTimestamp(TS_MW, $this->mTimestamp);
387 * @return bool
389 function isCurrent() {
390 return $this->mCurrent;
394 * @return Revision
396 function getPrevious() {
397 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
398 return Revision::newFromTitle( $this->mTitle, $prev );
402 * @return Revision
404 function getNext() {
405 $next = $this->mTitle->getNextRevisionID( $this->mId );
406 return Revision::newFromTitle( $this->mTitle, $next );
408 /**#@-*/
411 * Get revision text associated with an old or archive row
412 * $row is usually an object from wfFetchRow(), both the flags and the text
413 * field must be included
414 * @static
415 * @param integer $row Id of a row
416 * @param string $prefix table prefix (default 'old_')
417 * @return string $text|false the text requested
419 function getRevisionText( $row, $prefix = 'old_' ) {
420 $fname = 'Revision::getRevisionText';
421 wfProfileIn( $fname );
423 # Get data
424 $textField = $prefix . 'text';
425 $flagsField = $prefix . 'flags';
427 if( isset( $row->$flagsField ) ) {
428 $flags = explode( ',', $row->$flagsField );
429 } else {
430 $flags = array();
433 if( isset( $row->$textField ) ) {
434 $text = $row->$textField;
435 } else {
436 wfProfileOut( $fname );
437 return false;
440 # Use external methods for external objects, text in table is URL-only then
441 if ( in_array( 'external', $flags ) ) {
442 $url=$text;
443 @list($proto,$path)=explode('://',$url,2);
444 if ($path=="") {
445 wfProfileOut( $fname );
446 return false;
448 require_once('ExternalStore.php');
449 $text=ExternalStore::fetchFromURL($url);
452 if( in_array( 'gzip', $flags ) ) {
453 # Deal with optional compression of archived pages.
454 # This can be done periodically via maintenance/compressOld.php, and
455 # as pages are saved if $wgCompressRevisions is set.
456 $text = gzinflate( $text );
459 if( in_array( 'object', $flags ) ) {
460 # Generic compressed storage
461 $obj = unserialize( $text );
463 # Bugger, corrupted my test database by double-serializing
464 if ( !is_object( $obj ) ) {
465 $obj = unserialize( $obj );
468 $text = $obj->getText();
471 global $wgLegacyEncoding;
472 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
473 # Old revisions kept around in a legacy encoding?
474 # Upconvert on demand.
475 global $wgInputEncoding, $wgContLang;
476 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding . '//IGNORE', $text );
478 wfProfileOut( $fname );
479 return $text;
483 * If $wgCompressRevisions is enabled, we will compress data.
484 * The input string is modified in place.
485 * Return value is the flags field: contains 'gzip' if the
486 * data is compressed, and 'utf-8' if we're saving in UTF-8
487 * mode.
489 * @static
490 * @param mixed $text reference to a text
491 * @return string
493 function compressRevisionText( &$text ) {
494 global $wgCompressRevisions;
495 $flags = array();
497 # Revisions not marked this way will be converted
498 # on load if $wgLegacyCharset is set in the future.
499 $flags[] = 'utf-8';
501 if( $wgCompressRevisions ) {
502 if( function_exists( 'gzdeflate' ) ) {
503 $text = gzdeflate( $text );
504 $flags[] = 'gzip';
505 } else {
506 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
509 return implode( ',', $flags );
513 * Insert a new revision into the database, returning the new revision ID
514 * number on success and dies horribly on failure.
516 * @param Database $dbw
517 * @return int
519 function insertOn( &$dbw ) {
520 $fname = 'Revision::insertOn';
521 wfProfileIn( $fname );
523 $mungedText = $this->mText;
524 $flags = Revision::compressRevisionText( $mungedText );
526 # Record the text to the text table
527 if( !isset( $this->mTextId ) ) {
528 $old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
529 $dbw->insert( 'text',
530 array(
531 'old_id' => $old_id,
532 'old_text' => $mungedText,
533 'old_flags' => $flags,
534 ), $fname
536 $this->mTextId = $dbw->insertId();
539 # Record the edit in revisions
540 $rev_id = isset( $this->mId )
541 ? $this->mId
542 : $dbw->nextSequenceValue( 'rev_rev_id_val' );
543 $dbw->insert( 'revision',
544 array(
545 'rev_id' => $rev_id,
546 'rev_page' => $this->mPage,
547 'rev_text_id' => $this->mTextId,
548 'rev_comment' => $this->mComment,
549 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
550 'rev_user' => $this->mUser,
551 'rev_user_text' => $this->mUserText,
552 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
553 'rev_deleted' => $this->mDeleted,
554 ), $fname
557 $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId();
559 wfProfileOut( $fname );
560 return $this->mId;
564 * Lazy-load the revision's text.
565 * Currently hardcoded to the 'text' table storage engine.
567 * @return string
568 * @access private
570 function loadText() {
571 $fname = 'Revision::loadText';
572 wfProfileIn( $fname );
574 $dbr =& wfGetDB( DB_SLAVE );
575 $row = $dbr->selectRow( 'text',
576 array( 'old_text', 'old_flags' ),
577 array( 'old_id' => $this->getTextId() ),
578 $fname);
580 if( !$row ) {
581 $dbw =& wfGetDB( DB_MASTER );
582 $row = $dbw->selectRow( 'text',
583 array( 'old_text', 'old_flags' ),
584 array( 'old_id' => $this->getTextId() ),
585 $fname);
588 $text = Revision::getRevisionText( $row );
589 wfProfileOut( $fname );
591 return $text;
595 * Create a new null-revision for insertion into a page's
596 * history. This will not re-save the text, but simply refer
597 * to the text from the previous version.
599 * Such revisions can for instance identify page rename
600 * operations and other such meta-modifications.
602 * @param Database $dbw
603 * @param int $pageId ID number of the page to read from
604 * @param string $summary
605 * @param bool $minor
606 * @return Revision
608 function newNullRevision( &$dbw, $pageId, $summary, $minor ) {
609 $fname = 'Revision::newNullRevision';
610 wfProfileIn( $fname );
612 $current = $dbw->selectRow(
613 array( 'page', 'revision' ),
614 array( 'page_latest', 'rev_text_id' ),
615 array(
616 'page_id' => $pageId,
617 'page_latest=rev_id',
619 $fname );
621 if( $current ) {
622 $revision = new Revision( array(
623 'page' => $pageId,
624 'comment' => $summary,
625 'minor_edit' => $minor,
626 'text_id' => $current->rev_text_id,
627 ) );
628 } else {
629 $revision = null;
632 wfProfileOut( $fname );
633 return $revision;