don't try to prefill edit summary when section=new (relevant only for preload=)
[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 /**
77 * @package MediaWiki
78 * @subpackage SpecialPage
79 */
80 class WikiExporter {
81 var $pageCallback = null;
82 var $revCallback = null;
83
84 /**
85 * If using MW_EXPORT_STREAM to stream a large amount of data,
86 * provide a database connection which is not managed by
87 * LoadBalancer to read from: some history blob types will
88 * make additional queries to pull source data while the
89 * main query is still running.
90 *
91 * @param Database $db
92 * @param int $history one of MW_EXPORT_FULL or MW_EXPORT_CURRENT
93 * @param int $buffer one of MW_EXPORT_BUFFER or MW_EXPORT_STREAM
94 */
95 function WikiExporter( &$db, $history = MW_EXPORT_CURRENT,
96 $buffer = MW_EXPORT_BUFFER ) {
97 $this->db =& $db;
98 $this->history = $history;
99 $this->buffer = $buffer;
100 }
101
102 /**
103 * Set a callback to be called after each page in the output
104 * stream is closed. The callback will be passed a database row
105 * object with the last revision output.
106 *
107 * A set callback can be removed by passing null here.
108 *
109 * @param mixed $callback
110 */
111 function setPageCallback( $callback ) {
112 $this->pageCallback = $callback;
113 }
114
115 /**
116 * Set a callback to be called after each revision in the output
117 * stream is closed. The callback will be passed a database row
118 * object with the revision data.
119 *
120 * A set callback can be removed by passing null here.
121 *
122 * @param mixed $callback
123 */
124 function setRevisionCallback( $callback ) {
125 $this->revCallback = $callback;
126 }
127
128 /**
129 * Returns the export schema version.
130 * @return string
131 */
132 function schemaVersion() {
133 return "0.3";
134 }
135
136 /**
137 * Opens the XML output stream's root <mediawiki> element.
138 * This does not include an xml directive, so is safe to include
139 * as a subelement in a larger XML stream. Namespace and XML Schema
140 * references are included.
141 *
142 * To capture the stream to a string, use PHP's output buffering
143 * functions. Output will be encoded in UTF-8.
144 */
145 function openStream() {
146 global $wgContLanguageCode;
147 $ver = $this->schemaVersion();
148 print wfElement( 'mediawiki', array(
149 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
150 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
151 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
152 "http://www.mediawiki.org/xml/export-$ver.xsd",
153 'version' => $ver,
154 'xml:lang' => $wgContLanguageCode ),
155 null ) . "\n";
156 $this->siteInfo();
157 }
158
159 function siteInfo() {
160 $info = array(
161 $this->sitename(),
162 $this->homelink(),
163 $this->generator(),
164 $this->caseSetting(),
165 $this->namespaces() );
166 print "<siteinfo>\n";
167 foreach( $info as $item ) {
168 print " $item\n";
169 }
170 print "</siteinfo>\n";
171 }
172
173 function sitename() {
174 global $wgSitename;
175 return wfElement( 'sitename', array(), $wgSitename );
176 }
177
178 function generator() {
179 global $wgVersion;
180 return wfElement( 'generator', array(), "MediaWiki $wgVersion" );
181 }
182
183 function homelink() {
184 $page = Title::newFromText( wfMsgForContent( 'mainpage' ) );
185 return wfElement( 'base', array(), $page->getFullUrl() );
186 }
187
188 function caseSetting() {
189 global $wgCapitalLinks;
190 // "case-insensitive" option is reserved for future
191 $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
192 return wfElement( 'case', array(), $sensitivity );
193 }
194
195 function namespaces() {
196 global $wgContLang;
197 $spaces = "<namespaces>\n";
198 foreach( $wgContLang->getNamespaces() as $ns => $title ) {
199 $spaces .= ' ' . wfElement( 'namespace',
200 array( 'key' => $ns ),
201 str_replace( '_', ' ', $title ) ) . "\n";
202 }
203 $spaces .= " </namespaces>";
204 return $spaces;
205 }
206
207 /**
208 * Closes the output stream with the closing root element.
209 * Call when finished dumping things.
210 */
211 function closeStream() {
212 print "</mediawiki>\n";
213 }
214
215 /**
216 * Dumps a series of page and revision records for all pages
217 * in the database, either including complete history or only
218 * the most recent version.
219 *
220 *
221 * @param Database $db
222 */
223 function allPages() {
224 return $this->dumpFrom( '' );
225 }
226
227 /**
228 * @param Title $title
229 */
230 function pageByTitle( $title ) {
231 return $this->dumpFrom(
232 'page_namespace=' . $title->getNamespace() .
233 ' AND page_title=' . $this->db->addQuotes( $title->getDbKey() ) );
234 }
235
236 function pageByName( $name ) {
237 $title = Title::newFromText( $name );
238 if( is_null( $title ) ) {
239 return WikiError( "Can't export invalid title" );
240 } else {
241 return $this->pageByTitle( $title );
242 }
243 }
244
245 function pagesByName( $names ) {
246 foreach( $names as $name ) {
247 $this->pageByName( $name );
248 }
249 }
250
251
252 // -------------------- private implementation below --------------------
253
254 function dumpFrom( $cond = '' ) {
255 $fname = 'WikiExporter::dumpFrom';
256 wfProfileIn( $fname );
257
258 $page = $this->db->tableName( 'page' );
259 $revision = $this->db->tableName( 'revision' );
260 $text = $this->db->tableName( 'text' );
261
262 if( $this->history == MW_EXPORT_FULL ) {
263 $join = 'page_id=rev_page';
264 } elseif( $this->history == MW_EXPORT_CURRENT ) {
265 $join = 'page_id=rev_page AND page_latest=rev_id';
266 } else {
267 wfProfileOut( $fname );
268 return new WikiError( "$fname given invalid history dump type." );
269 }
270 $where = ( $cond == '' ) ? '' : "$cond AND";
271
272 if( $this->buffer == MW_EXPORT_STREAM ) {
273 $prev = $this->db->bufferResults( false );
274 }
275 if( $cond == '' ) {
276 // Optimization hack for full-database dump
277 $pageindex = 'FORCE INDEX (PRIMARY)';
278 $revindex = 'FORCE INDEX(page_timestamp)';
279 } else {
280 $pageindex = '';
281 $revindex = '';
282 }
283 $result = $this->db->query(
284 "SELECT * FROM
285 $page $pageindex,
286 $revision $revindex,
287 $text
288 WHERE $where $join AND rev_text_id=old_id
289 ORDER BY page_id", $fname );
290 $wrapper = $this->db->resultObject( $result );
291 $this->outputStream( $wrapper );
292
293 if( $this->buffer == MW_EXPORT_STREAM ) {
294 $this->db->bufferResults( $prev );
295 }
296
297 wfProfileOut( $fname );
298 }
299
300 /**
301 * Runs through a query result set dumping page and revision records.
302 * The result set should be sorted/grouped by page to avoid duplicate
303 * page records in the output.
304 *
305 * The result set will be freed once complete. Should be safe for
306 * streaming (non-buffered) queries, as long as it was made on a
307 * separate database connection not managed by LoadBalancer; some
308 * blob storage types will make queries to pull source data.
309 *
310 * @param ResultWrapper $resultset
311 * @access private
312 */
313 function outputStream( $resultset ) {
314 $last = null;
315 while( $row = $resultset->fetchObject() ) {
316 if( is_null( $last ) ||
317 $last->page_namespace != $row->page_namespace ||
318 $last->page_title != $row->page_title ) {
319 if( isset( $last ) ) {
320 $this->closePage( $last );
321 }
322 $this->openPage( $row );
323 $last = $row;
324 }
325 $this->dumpRev( $row );
326 }
327 if( isset( $last ) ) {
328 $this->closePage( $last );
329 }
330 $resultset->free();
331 }
332
333 /**
334 * Opens a <page> section on the output stream, with data
335 * from the given database row.
336 *
337 * @param object $row
338 * @access private
339 */
340 function openPage( $row ) {
341 print "<page>\n";
342 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
343 print ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
344 print ' ' . wfElement( 'id', array(), $row->page_id ) . "\n";
345 if( '' != $row->page_restrictions ) {
346 print ' ' . wfElement( 'restrictions', array(),
347 $row->page_restrictions ) . "\n";
348 }
349 }
350
351 /**
352 * Closes a <page> section on the output stream.
353 * If a per-page callback has been set, it will be called
354 * and passed the last database row used for this page.
355 *
356 * @param object $row
357 * @access private
358 */
359 function closePage( $row ) {
360 print "</page>\n";
361 if( isset( $this->pageCallback ) ) {
362 call_user_func( $this->pageCallback, $row );
363 }
364 }
365
366 /**
367 * Dumps a <revision> section on the output stream, with
368 * data filled in from the given database row.
369 *
370 * @param object $row
371 * @access private
372 */
373 function dumpRev( $row ) {
374 $fname = 'WikiExporter::dumpRev';
375 wfProfileIn( $fname );
376
377 print " <revision>\n";
378 print " " . wfElement( 'id', null, $row->rev_id ) . "\n";
379
380 $ts = wfTimestamp2ISO8601( $row->rev_timestamp );
381 print " " . wfElement( 'timestamp', null, $ts ) . "\n";
382
383 print " <contributor>";
384 if( $row->rev_user ) {
385 print wfElementClean( 'username', null, $row->rev_user_text );
386 print wfElement( 'id', null, $row->rev_user );
387 } else {
388 print wfElementClean( 'ip', null, $row->rev_user_text );
389 }
390 print "</contributor>\n";
391
392 if( $row->rev_minor_edit ) {
393 print " <minor/>\n";
394 }
395 if( $row->rev_comment != '' ) {
396 print " " . wfElementClean( 'comment', null, $row->rev_comment ) . "\n";
397 }
398
399 $text = Revision::getRevisionText( $row );
400 print " " . wfElementClean( 'text',
401 array( 'xml:space' => 'preserve' ), $text ) . "\n";
402
403 print " </revision>\n";
404
405 wfProfileOut( $fname );
406
407 if( isset( $this->revCallback ) ) {
408 call_user_func( $this->revCallback, $row );
409 }
410 }
411
412 }
413
414 function wfTimestamp2ISO8601( $ts ) {
415 #2003-08-05T18:30:02Z
416 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
417 }
418
419 function xmlsafe( $string ) {
420 $fname = 'xmlsafe';
421 wfProfileIn( $fname );
422
423 /**
424 * The page may contain old data which has not been properly normalized.
425 * Invalid UTF-8 sequences or forbidden control characters will make our
426 * XML output invalid, so be sure to strip them out.
427 */
428 $string = UtfNormal::cleanUp( $string );
429
430 $string = htmlspecialchars( $string );
431 wfProfileOut( $fname );
432 return $string;
433 }
434
435 ?>