quotes
[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 'rev_id=old_id' ) );
29 }
30
31 /**
32 * Load either the current, or a specified, revision
33 * that's attached to a given title. If not attached
34 * to that title, will return null.
35 *
36 * @param Title $title
37 * @param int $id
38 * @return Revision
39 * @access public
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 'rev_id=old_id' ) );
53 }
54
55 /**
56 * Given a set of conditions, fetch a revision.
57 *
58 * @param array $conditions
59 * @return Revision
60 * @static
61 * @access private
62 */
63 function &newFromConds( $conditions ) {
64 $res =& Revision::fetchFromConds( $conditions );
65 if( $res ) {
66 $row = $res->fetchObject();
67 $res->free();
68 if( $row ) {
69 return new Revision( $row );
70 }
71 }
72 return null;
73 }
74
75 /**
76 * Return a wrapper for a series of database rows to
77 * fetch all of a given page's revisions in turn.
78 * Each row can be fed to the constructor to get objects.
79 *
80 * @param Title $title
81 * @return ResultWrapper
82 * @static
83 * @access public
84 */
85 function &fetchAllRevisions( &$title ) {
86 return Revision::fetchFromConds(
87 array( 'page_namespace' => $title->getNamespace(),
88 'page_title' => $title->getDbkey(),
89 'page_id=rev_page',
90 'rev_id=old_id' ) );
91 }
92
93 /**
94 * Return a wrapper for a series of database rows to
95 * fetch all of a given page's revisions in turn.
96 * Each row can be fed to the constructor to get objects.
97 *
98 * @param Title $title
99 * @return ResultWrapper
100 * @static
101 * @access public
102 */
103 function &fetchRevision( &$title ) {
104 return Revision::fetchFromConds(
105 array( 'rev_id=page_latest',
106 'page_namespace' => $title->getNamespace(),
107 'page_title' => $title->getDbkey(),
108 'page_id=rev_page',
109 'rev_id=old_id' ) );
110 }
111 /**
112 * Given a set of conditions, return a ResultWrapper
113 * which will return matching database rows with the
114 * fields necessary to build Revision objects.
115 *
116 * @param array $conditions
117 * @return ResultWrapper
118 * @static
119 * @access private
120 */
121 function &fetchFromConds( $conditions ) {
122 $dbr =& wfGetDB( DB_SLAVE );
123 $res = $dbr->select(
124 array( 'page', 'revision', 'text' ),
125 array( 'page_namespace',
126 'page_title',
127 'page_latest',
128 'rev_id',
129 'rev_page',
130 'rev_comment',
131 'rev_user_text',
132 'rev_user',
133 'rev_minor_edit',
134 'rev_timestamp',
135 'old_flags',
136 'old_text' ),
137 $conditions,
138 'Revision::fetchRow' );
139 return $dbr->resultObject( $res );
140 }
141
142 /**
143 * @param object $row
144 * @access private
145 */
146 function Revision( $row ) {
147 $this->mId = IntVal( $row->rev_id );
148 $this->mPage = IntVal( $row->rev_page );
149 $this->mComment = $row->rev_comment;
150 $this->mUserText = $row->rev_user_text;
151 $this->mUser = IntVal( $row->rev_user );
152 $this->mMinorEdit = IntVal( $row->rev_minor_edit );
153 $this->mTimestamp = $row->rev_timestamp;
154
155 $this->mCurrent = ( $row->rev_id == $row->page_latest );
156 $this->mTitle = Title::makeTitle( $row->page_namespace,
157 $row->page_title );
158 $this->mText = $this->getRevisionText( $row );
159 }
160
161 /**#@+
162 * @access public
163 */
164
165 /**
166 * @return int
167 */
168 function getId() {
169 return $this->mId;
170 }
171
172 /**
173 * Returns the title of the page associated with this entry.
174 * @return Title
175 */
176 function &getTitle() {
177 if( isset( $this->mTitle ) ) {
178 return $this->mTitle;
179 }
180 $dbr =& wfGetDB( DB_SLAVE );
181 $row = $dbr->selectRow(
182 array( 'page', 'revision' ),
183 array( 'page_namespace', 'page_title' ),
184 array( 'page_id=rev_page',
185 'rev_id' => $this->mId ),
186 'Revision::getTItle' );
187 if( $row ) {
188 $this->mTitle =& Title::makeTitle( $row->page_namespace,
189 $row->page_title );
190 }
191 return $this->mTitle;
192 }
193
194 /**
195 * @return int
196 */
197 function getUser() {
198 return $this->mUser;
199 }
200
201 /**
202 * @return string
203 */
204 function getUserText() {
205 return $this->mUserText;
206 }
207
208 /**
209 * @return string
210 */
211 function getComment() {
212 return $this->mComment;
213 }
214
215 /**
216 * @return bool
217 */
218 function isMinor() {
219 return (bool)$this->mMinorEdit;
220 }
221
222 /**
223 * @return string
224 */
225 function getText() {
226 return $this->mText;
227 }
228
229 /**
230 * @return string
231 */
232 function getTimestamp() {
233 return $this->mTimestamp;
234 }
235
236 /**
237 * @return bool
238 */
239 function isCurrent() {
240 return $this->mCurrent;
241 }
242
243 /**
244 * @return Revision
245 */
246 function &getPrevious() {
247 $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
248 return Revision::newFromTitle( $this->mTitle, $prev );
249 }
250
251 /**
252 * @return Revision
253 */
254 function &getNext() {
255 $next = $this->mTitle->getNextRevisionID( $this->mId );
256 return Revision::newFromTitle( $this->mTitle, $next );
257 }
258 /**#@-*/
259
260 /**
261 * Get revision text associated with an old or archive row
262 * $row is usually an object from wfFetchRow(), both the flags and the text
263 * field must be included
264 * @static
265 * @param integer $row Id of a row
266 * @param string $prefix table prefix (default 'old_')
267 * @return string $text|false the text requested
268 */
269 function getRevisionText( $row, $prefix = 'old_' ) {
270 $fname = 'Revision::getRevisionText';
271 wfProfileIn( $fname );
272
273 # Get data
274 $textField = $prefix . 'text';
275 $flagsField = $prefix . 'flags';
276
277 if( isset( $row->$flagsField ) ) {
278 $flags = explode( ',', $row->$flagsField );
279 } else {
280 $flags = array();
281 }
282
283 if( isset( $row->$textField ) ) {
284 $text = $row->$textField;
285 } else {
286 wfProfileOut( $fname );
287 return false;
288 }
289
290 if( in_array( 'gzip', $flags ) ) {
291 # Deal with optional compression of archived pages.
292 # This can be done periodically via maintenance/compressOld.php, and
293 # as pages are saved if $wgCompressRevisions is set.
294 $text = gzinflate( $text );
295 }
296
297 if( in_array( 'object', $flags ) ) {
298 # Generic compressed storage
299 $obj = unserialize( $text );
300
301 # Bugger, corrupted my test database by double-serializing
302 if ( !is_object( $obj ) ) {
303 $obj = unserialize( $obj );
304 }
305
306 $text = $obj->getText();
307 }
308
309 global $wgLegacyEncoding;
310 if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
311 # Old revisions kept around in a legacy encoding?
312 # Upconvert on demand.
313 global $wgInputEncoding, $wgContLang;
314 $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
315 }
316 wfProfileOut( $fname );
317 return $text;
318 }
319
320 /**
321 * If $wgCompressRevisions is enabled, we will compress data.
322 * The input string is modified in place.
323 * Return value is the flags field: contains 'gzip' if the
324 * data is compressed, and 'utf-8' if we're saving in UTF-8
325 * mode.
326 *
327 * @static
328 * @param mixed $text reference to a text
329 * @return string
330 */
331 function compressRevisionText( &$text ) {
332 global $wgCompressRevisions, $wgUseLatin1;
333 $flags = array();
334 if( !$wgUseLatin1 ) {
335 # Revisions not marked this way will be converted
336 # on load if $wgLegacyCharset is set in the future.
337 $flags[] = 'utf-8';
338 }
339 if( $wgCompressRevisions ) {
340 if( function_exists( 'gzdeflate' ) ) {
341 $text = gzdeflate( $text );
342 $flags[] = 'gzip';
343 } else {
344 wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
345 }
346 }
347 return implode( ',', $flags );
348 }
349 }
350 ?>