3 require_once( 'Database.php' );
4 require_once( 'Article.php' );
8 * Load a page revision from a given revision ID number.
9 * Returns null if no such revision can be found.
15 function &newFromId( $id ) {
16 return Revision
::newFromConds(
17 array( 'page_id=rev_page',
18 'rev_id' => IntVal( $id ),
23 * Load either the current, or a specified, revision
24 * that's attached to a given title. If not attached
25 * to that title, will return null.
32 function &newFromTitle( &$title, $id = 0 ) {
34 $matchId = IntVal( $id );
36 $matchId = 'page_latest';
38 return Revision
::newFromConds(
39 array( "rev_id=$matchId",
41 'page_namespace' => $title->getNamespace(),
42 'page_title' => $title->getDbkey(),
47 * Given a set of conditions, fetch a revision.
49 * @param array $conditions
54 function &newFromConds( $conditions ) {
55 $res =& Revision
::fetchFromConds( $conditions );
57 $row = $res->fetchObject();
60 return new Revision( $row );
67 * Return a wrapper for a series of database rows to
68 * fetch all of a given page's revisions in turn.
69 * Each row can be fed to the constructor to get objects.
72 * @return ResultWrapper
76 function &fetchAllRevisions( &$title ) {
77 return Revision
::fetchFromConds(
78 array( 'page_namespace' => $title->getNamespace(),
79 'page_title' => $title->getDbkey(),
85 * Return a wrapper for a series of database rows to
86 * fetch all of a given page's revisions in turn.
87 * Each row can be fed to the constructor to get objects.
90 * @return ResultWrapper
94 function &fetchRevision( &$title ) {
95 return Revision
::fetchFromConds(
96 array( 'rev_id=page_latest',
97 'page_namespace' => $title->getNamespace(),
98 'page_title' => $title->getDbkey(),
103 * Given a set of conditions, return a ResultWrapper
104 * which will return matching database rows with the
105 * fields necessary to build Revision objects.
107 * @param array $conditions
108 * @return ResultWrapper
112 function &fetchFromConds( $conditions ) {
113 $dbr =& wfGetDB( DB_SLAVE
);
115 array( 'page', 'revision', 'text' ),
116 array( 'page_namespace',
129 'Revision::fetchRow' );
130 return $dbr->resultObject( $res );
137 function Revision( $row ) {
138 $this->mId
= IntVal( $row->rev_id
);
139 $this->mPage
= IntVal( $row->rev_page
);
140 $this->mComment
= $row->rev_comment
;
141 $this->mUserText
= $row->rev_user_text
;
142 $this->mUser
= IntVal( $row->rev_user
);
143 $this->mMinorEdit
= IntVal( $row->rev_minor_edit
);
144 $this->mTimestamp
= $row->rev_timestamp
;
146 $this->mCurrent
= ( $row->rev_id
== $row->page_latest
);
147 $this->mTitle
= Title
::makeTitle( $row->page_namespace
,
149 $this->mText
= $this->getRevisionText( $row );
164 * Returns the title of the page associated with this entry.
167 function &getTitle() {
168 if( isset( $this->mTitle
) ) {
169 return $this->mTitle
;
171 $dbr =& wfGetDB( DB_SLAVE
);
172 $row = $dbr->selectRow(
173 array( 'page', 'revision' ),
174 array( 'page_namespace', 'page_title' ),
175 array( 'page_id=rev_page',
176 'rev_id' => $this->mId
),
177 'Revision::getTItle' );
179 $this->mTitle
=& Title
::makeTitle( $row->page_namespace
,
182 return $this->mTitle
;
195 function getUserText() {
196 return $this->mUserText
;
202 function getComment() {
203 return $this->mComment
;
210 return (bool)$this->mMinorEdit
;
223 function getTimestamp() {
224 return $this->mTimestamp
;
230 function isCurrent() {
231 return $this->mCurrent
;
237 function &getPrevious() {
238 $prev = $this->mTitle
->getPreviousRevisionID( $this->mId
);
239 return Revision
::newFromTitle( $this->mTitle
, $prev );
245 function &getNext() {
246 $next = $this->mTitle
->getNextRevisionID( $this->mId
);
247 return Revision
::newFromTitle( $this->mTitle
, $next );
252 * Get revision text associated with an old or archive row
253 * $row is usually an object from wfFetchRow(), both the flags and the text
254 * field must be included
256 * @param integer $row Id of a row
257 * @param string $prefix table prefix (default 'old_')
258 * @return string $text|false the text requested
260 function getRevisionText( $row, $prefix = 'old_' ) {
261 $fname = 'Revision::getRevisionText';
262 wfProfileIn( $fname );
265 $textField = $prefix . 'text';
266 $flagsField = $prefix . 'flags';
268 if( isset( $row->$flagsField ) ) {
269 $flags = explode( ',', $row->$flagsField );
274 if( isset( $row->$textField ) ) {
275 $text = $row->$textField;
277 wfProfileOut( $fname );
281 if( in_array( 'gzip', $flags ) ) {
282 # Deal with optional compression of archived pages.
283 # This can be done periodically via maintenance/compressOld.php, and
284 # as pages are saved if $wgCompressRevisions is set.
285 $text = gzinflate( $text );
288 if( in_array( 'object', $flags ) ) {
289 # Generic compressed storage
290 $obj = unserialize( $text );
292 # Bugger, corrupted my test database by double-serializing
293 if ( !is_object( $obj ) ) {
294 $obj = unserialize( $obj );
297 $text = $obj->getText();
300 global $wgLegacyEncoding;
301 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
302 # Old revisions kept around in a legacy encoding?
303 # Upconvert on demand.
304 global $wgInputEncoding, $wgContLang;
305 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
307 wfProfileOut( $fname );
312 * If $wgCompressRevisions is enabled, we will compress data.
313 * The input string is modified in place.
314 * Return value is the flags field: contains 'gzip' if the
315 * data is compressed, and 'utf-8' if we're saving in UTF-8
319 * @param mixed $text reference to a text
322 function compressRevisionText( &$text ) {
323 global $wgCompressRevisions, $wgUseLatin1;
325 if( !$wgUseLatin1 ) {
326 # Revisions not marked this way will be converted
327 # on load if $wgLegacyCharset is set in the future.
330 if( $wgCompressRevisions ) {
331 if( function_exists( 'gzdeflate' ) ) {
332 $text = gzdeflate( $text );
335 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
338 return implode( ',', $flags );