* HistoryBlobStub: the last-used HistoryBlob is kept open to speed up
[lhc/web/wiklou.git] / includes / SpecialExport.php
1 <?php
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19 /**
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 /** */
26 require_once( 'Revision.php' );
27
28 /**
29 *
30 */
31 function wfSpecialExport( $page = '' ) {
32 global $wgOut, $wgLang, $wgRequest;
33
34 if( $wgRequest->getVal( 'action' ) == 'submit') {
35 $page = $wgRequest->getText( 'pages' );
36 $curonly = $wgRequest->getCheck( 'curonly' );
37 } else {
38 # Pre-check the 'current version only' box in the UI
39 $curonly = true;
40 }
41
42 if( $page != '' ) {
43 $wgOut->disable();
44 header( "Content-type: application/xml; charset=utf-8" );
45 $pages = explode( "\n", $page );
46
47 $db =& wfGetDB( DB_SLAVE );
48 $history = $curonly ? MW_EXPORT_CURRENT : MW_EXPORT_FULL;
49 $exporter = new WikiExporter( $db, $history );
50 $exporter->openStream();
51 $exporter->pagesByName( $pages );
52 $exporter->closeStream();
53 return;
54 }
55
56 $wgOut->addWikiText( wfMsg( "exporttext" ) );
57 $titleObj = Title::makeTitle( NS_SPECIAL, "Export" );
58 $action = $titleObj->escapeLocalURL( 'action=submit' );
59 $wgOut->addHTML( "
60 <form method='post' action=\"$action\">
61 <input type='hidden' name='action' value='submit' />
62 <textarea name='pages' cols='40' rows='10'></textarea><br />
63 <label><input type='checkbox' name='curonly' value='true' checked='checked' />
64 " . wfMsg( "exportcuronly" ) . "</label><br />
65 <input type='submit' />
66 </form>
67 " );
68 }
69
70 define( 'MW_EXPORT_FULL', 0 );
71 define( 'MW_EXPORT_CURRENT', 1 );
72
73 define( 'MW_EXPORT_BUFFER', 0 );
74 define( 'MW_EXPORT_STREAM', 1 );
75
76 class WikiExporter {
77 var $pageCallback = null;
78 var $revCallback = null;
79
80 /**
81 * If using MW_EXPORT_STREAM to stream a large amount of data,
82 * provide a database connection which is not managed by
83 * LoadBalancer to read from: some history blob types will
84 * make additional queries to pull source data while the
85 * main query is still running.
86 *
87 * @param Database $db
88 * @param int $history one of MW_EXPORT_FULL or MW_EXPORT_CURRENT
89 * @param int $buffer one of MW_EXPORT_BUFFER or MW_EXPORT_STREAM
90 */
91 function WikiExporter( &$db, $history = MW_EXPORT_CURRENT,
92 $buffer = MW_EXPORT_BUFFER ) {
93 $this->db =& $db;
94 $this->history = $history;
95 $this->buffer = $buffer;
96 }
97
98 /**
99 * Set a callback to be called after each page in the output
100 * stream is closed. The callback will be passed a database row
101 * object with the last revision output.
102 *
103 * A set callback can be removed by passing null here.
104 *
105 * @param mixed $callback
106 */
107 function setPageCallback( $callback ) {
108 $this->pageCallback = $callback;
109 }
110
111 /**
112 * Set a callback to be called after each revision in the output
113 * stream is closed. The callback will be passed a database row
114 * object with the revision data.
115 *
116 * A set callback can be removed by passing null here.
117 *
118 * @param mixed $callback
119 */
120 function setRevCallback( $callback ) {
121 $this->revCallback = $callback;
122 }
123
124 /**
125 * Opens the XML output stream's root <mediawiki> element.
126 * This does not include an xml directive, so is safe to include
127 * as a subelement in a larger XML stream. Namespace and XML Schema
128 * references are included.
129 *
130 * To capture the stream to a string, use PHP's output buffering
131 * functions. Output will be encoded in UTF-8.
132 */
133 function openStream() {
134 global $wgContLanguageCode;
135 print wfElement( 'mediawiki', array(
136 'xmlns' => 'http://www.mediawiki.org/xml/export-0.1/',
137 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
138 'xsi:schemaLocation' => 'http://www.mediawiki.org/xml/export-0.1/ ' .
139 'http://www.mediawiki.org/xml/export-0.1.xsd',
140 'version' => '0.1',
141 'xml:lang' => $wgContLanguageCode ),
142 null ) . "\n";
143 }
144
145 /**
146 * Closes the output stream with the closing root element.
147 * Call when finished dumping things.
148 */
149 function closeStream() {
150 print "</mediawiki>\n";
151 }
152
153 /**
154 * Dumps a series of page and revision records for all pages
155 * in the database, either including complete history or only
156 * the most recent version.
157 *
158 *
159 * @param Database $db
160 */
161 function allPages() {
162 return $this->dumpFrom( '' );
163 }
164
165 /**
166 * @param Title $title
167 */
168 function pageByTitle( $title ) {
169 return $this->dumpFrom(
170 'page_namespace=' . $title->getNamespace() .
171 ' AND page_title=' . $this->db->addQuotes( $title->getDbKey() ) );
172 }
173
174 function pageByName( $name ) {
175 $title = Title::newFromText( $name );
176 if( is_null( $title ) ) {
177 return WikiError( "Can't export invalid title" );
178 } else {
179 return $this->pageByTitle( $title );
180 }
181 }
182
183 function pagesByName( $names ) {
184 foreach( $names as $name ) {
185 $this->pageByName( $name );
186 }
187 }
188
189
190 // -------------------- private implementation below --------------------
191
192 function dumpFrom( $cond = '' ) {
193 $fname = 'WikiExporter::dumpFrom';
194 wfProfileIn( $fname );
195
196 $page = $this->db->tableName( 'page' );
197 $revision = $this->db->tableName( 'revision' );
198 $text = $this->db->tableName( 'text' );
199
200 if( $this->history == MW_EXPORT_FULL ) {
201 $join = 'page_id=rev_page';
202 } elseif( $this->history == MW_EXPORT_CURRENT ) {
203 $join = 'page_id=rev_page AND page_latest=rev_id';
204 } else {
205 wfProfileOut( $fname );
206 return new WikiError( "$fname given invalid history dump type." );
207 }
208 $where = ( $cond == '' ) ? '' : "$cond AND";
209
210 if( $this->buffer == MW_EXPORT_STREAM ) {
211 $prev = $this->db->bufferResults( false );
212 }
213 $result = $this->db->query(
214 "SELECT * FROM
215 $page FORCE INDEX (PRIMARY),
216 $revision FORCE INDEX(page_timestamp),
217 $text
218 WHERE $where $join AND rev_text_id=old_id
219 ORDER BY page_id", $fname );
220 $wrapper = $this->db->resultObject( $result );
221 $this->outputStream( $wrapper );
222
223 if( $this->buffer == MW_EXPORT_STREAM ) {
224 $this->db->bufferResults( $prev );
225 }
226
227 wfProfileOut( $fname );
228 }
229
230 /**
231 * Runs through a query result set dumping page and revision records.
232 * The result set should be sorted/grouped by page to avoid duplicate
233 * page records in the output.
234 *
235 * The result set will be freed once complete. Should be safe for
236 * streaming (non-buffered) queries, as long as it was made on a
237 * separate database connection not managed by LoadBalancer; some
238 * blob storage types will make queries to pull source data.
239 *
240 * @param ResultWrapper $resultset
241 * @access private
242 */
243 function outputStream( $resultset ) {
244 $last = null;
245 while( $row = $resultset->fetchObject() ) {
246 if( is_null( $last ) ||
247 $last->page_namespace != $row->page_namespace ||
248 $last->page_title != $row->page_title ) {
249 if( isset( $last ) ) {
250 $this->closePage( $last );
251 }
252 $this->openPage( $row );
253 $last = $row;
254 }
255 $this->dumpRev( $row );
256 }
257 if( isset( $last ) ) {
258 $this->closePage( $last );
259 }
260 $resultset->free();
261 }
262
263 /**
264 * Opens a <page> section on the output stream, with data
265 * from the given database row.
266 *
267 * @param object $row
268 * @access private
269 */
270 function openPage( $row ) {
271 print "<page>\n";
272 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
273 print ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
274 print ' ' . wfElement( 'id', array(), $row->page_id ) . "\n";
275 if( '' != $row->page_restrictions ) {
276 print ' ' . wfElement( 'restrictions', array(),
277 $row->page_restrictions ) . "\n";
278 }
279 }
280
281 /**
282 * Closes a <page> section on the output stream.
283 * If a per-page callback has been set, it will be called
284 * and passed the last database row used for this page.
285 *
286 * @param object $row
287 * @access private
288 */
289 function closePage( $row ) {
290 print "</page>\n";
291 if( isset( $this->pageCallback ) ) {
292 call_user_func( $this->pageCallback, $row );
293 }
294 }
295
296 /**
297 * Dumps a <revision> section on the output stream, with
298 * data filled in from the given database row.
299 *
300 * @param object $row
301 * @access private
302 */
303 function dumpRev( $row ) {
304 $fname = 'WikiExporter::dumpRev';
305 wfProfileIn( $fname );
306
307 print " <revision>\n";
308 print " " . wfElement( 'id', null, $row->rev_id ) . "\n";
309
310 $ts = wfTimestamp2ISO8601( $row->rev_timestamp );
311 print " " . wfElement( 'timestamp', null, $ts ) . "\n";
312
313 print " <contributor>";
314 if( $row->rev_user ) {
315 print wfElementClean( 'username', null, $row->rev_user_text );
316 print wfElement( 'id', null, $row->rev_user );
317 } else {
318 print wfElementClean( 'ip', null, $row->rev_user_text );
319 }
320 print "</contributor>\n";
321
322 if( $row->rev_minor_edit ) {
323 print " <minor/>\n";
324 }
325 if( $row->rev_comment != '' ) {
326 print " " . wfElementClean( 'comment', null, $row->rev_comment ) . "\n";
327 }
328
329 $text = Revision::getRevisionText( $row );
330 print " " . wfElementClean( 'text', array(), $text ) . "\n";
331 print " </revision>\n";
332
333 wfProfileOut( $fname );
334
335 if( isset( $this->revCallback ) ) {
336 call_user_func( $this->revCallback, $row );
337 }
338 }
339
340 }
341
342 function wfTimestamp2ISO8601( $ts ) {
343 #2003-08-05T18:30:02Z
344 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
345 }
346
347 function xmlsafe( $string ) {
348 $fname = 'xmlsafe';
349 wfProfileIn( $fname );
350
351 /**
352 * The page may contain old data which has not been properly normalized.
353 * Invalid UTF-8 sequences or forbidden control characters will make our
354 * XML output invalid, so be sure to strip them out.
355 */
356 $string = UtfNormal::cleanUp( $string );
357
358 $string = htmlspecialchars( $string );
359 wfProfileOut( $fname );
360 return $string;
361 }
362
363 ?>