Reverting this for the moment.
[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 setRevCallback( $callback ) {
125 $this->revCallback = $callback;
126 }
127
128 /**
129 * Opens the XML output stream's root <mediawiki> element.
130 * This does not include an xml directive, so is safe to include
131 * as a subelement in a larger XML stream. Namespace and XML Schema
132 * references are included.
133 *
134 * To capture the stream to a string, use PHP's output buffering
135 * functions. Output will be encoded in UTF-8.
136 */
137 function openStream() {
138 global $wgContLanguageCode;
139 print wfElement( 'mediawiki', array(
140 'xmlns' => 'http://www.mediawiki.org/xml/export-0.1/',
141 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
142 'xsi:schemaLocation' => 'http://www.mediawiki.org/xml/export-0.1/ ' .
143 'http://www.mediawiki.org/xml/export-0.1.xsd',
144 'version' => '0.1',
145 'xml:lang' => $wgContLanguageCode ),
146 null ) . "\n";
147 }
148
149 /**
150 * Closes the output stream with the closing root element.
151 * Call when finished dumping things.
152 */
153 function closeStream() {
154 print "</mediawiki>\n";
155 }
156
157 /**
158 * Dumps a series of page and revision records for all pages
159 * in the database, either including complete history or only
160 * the most recent version.
161 *
162 *
163 * @param Database $db
164 */
165 function allPages() {
166 return $this->dumpFrom( '' );
167 }
168
169 /**
170 * @param Title $title
171 */
172 function pageByTitle( $title ) {
173 return $this->dumpFrom(
174 'page_namespace=' . $title->getNamespace() .
175 ' AND page_title=' . $this->db->addQuotes( $title->getDbKey() ) );
176 }
177
178 function pageByName( $name ) {
179 $title = Title::newFromText( $name );
180 if( is_null( $title ) ) {
181 return WikiError( "Can't export invalid title" );
182 } else {
183 return $this->pageByTitle( $title );
184 }
185 }
186
187 function pagesByName( $names ) {
188 foreach( $names as $name ) {
189 $this->pageByName( $name );
190 }
191 }
192
193
194 // -------------------- private implementation below --------------------
195
196 function dumpFrom( $cond = '' ) {
197 $fname = 'WikiExporter::dumpFrom';
198 wfProfileIn( $fname );
199
200 $page = $this->db->tableName( 'page' );
201 $revision = $this->db->tableName( 'revision' );
202 $text = $this->db->tableName( 'text' );
203
204 if( $this->history == MW_EXPORT_FULL ) {
205 $join = 'page_id=rev_page';
206 } elseif( $this->history == MW_EXPORT_CURRENT ) {
207 $join = 'page_id=rev_page AND page_latest=rev_id';
208 } else {
209 wfProfileOut( $fname );
210 return new WikiError( "$fname given invalid history dump type." );
211 }
212 $where = ( $cond == '' ) ? '' : "$cond AND";
213
214 if( $this->buffer == MW_EXPORT_STREAM ) {
215 $prev = $this->db->bufferResults( false );
216 }
217 $result = $this->db->query(
218 "SELECT * FROM
219 $page FORCE INDEX (PRIMARY),
220 $revision FORCE INDEX(page_timestamp),
221 $text
222 WHERE $where $join AND rev_text_id=old_id
223 ORDER BY page_id", $fname );
224 $wrapper = $this->db->resultObject( $result );
225 $this->outputStream( $wrapper );
226
227 if( $this->buffer == MW_EXPORT_STREAM ) {
228 $this->db->bufferResults( $prev );
229 }
230
231 wfProfileOut( $fname );
232 }
233
234 /**
235 * Runs through a query result set dumping page and revision records.
236 * The result set should be sorted/grouped by page to avoid duplicate
237 * page records in the output.
238 *
239 * The result set will be freed once complete. Should be safe for
240 * streaming (non-buffered) queries, as long as it was made on a
241 * separate database connection not managed by LoadBalancer; some
242 * blob storage types will make queries to pull source data.
243 *
244 * @param ResultWrapper $resultset
245 * @access private
246 */
247 function outputStream( $resultset ) {
248 $last = null;
249 while( $row = $resultset->fetchObject() ) {
250 if( is_null( $last ) ||
251 $last->page_namespace != $row->page_namespace ||
252 $last->page_title != $row->page_title ) {
253 if( isset( $last ) ) {
254 $this->closePage( $last );
255 }
256 $this->openPage( $row );
257 $last = $row;
258 }
259 $this->dumpRev( $row );
260 }
261 if( isset( $last ) ) {
262 $this->closePage( $last );
263 }
264 $resultset->free();
265 }
266
267 /**
268 * Opens a <page> section on the output stream, with data
269 * from the given database row.
270 *
271 * @param object $row
272 * @access private
273 */
274 function openPage( $row ) {
275 print "<page>\n";
276 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
277 print ' ' . wfElementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
278 print ' ' . wfElement( 'id', array(), $row->page_id ) . "\n";
279 if( '' != $row->page_restrictions ) {
280 print ' ' . wfElement( 'restrictions', array(),
281 $row->page_restrictions ) . "\n";
282 }
283 }
284
285 /**
286 * Closes a <page> section on the output stream.
287 * If a per-page callback has been set, it will be called
288 * and passed the last database row used for this page.
289 *
290 * @param object $row
291 * @access private
292 */
293 function closePage( $row ) {
294 print "</page>\n";
295 if( isset( $this->pageCallback ) ) {
296 call_user_func( $this->pageCallback, $row );
297 }
298 }
299
300 /**
301 * Dumps a <revision> section on the output stream, with
302 * data filled in from the given database row.
303 *
304 * @param object $row
305 * @access private
306 */
307 function dumpRev( $row ) {
308 $fname = 'WikiExporter::dumpRev';
309 wfProfileIn( $fname );
310
311 print " <revision>\n";
312 print " " . wfElement( 'id', null, $row->rev_id ) . "\n";
313
314 $ts = wfTimestamp2ISO8601( $row->rev_timestamp );
315 print " " . wfElement( 'timestamp', null, $ts ) . "\n";
316
317 print " <contributor>";
318 if( $row->rev_user ) {
319 print wfElementClean( 'username', null, $row->rev_user_text );
320 print wfElement( 'id', null, $row->rev_user );
321 } else {
322 print wfElementClean( 'ip', null, $row->rev_user_text );
323 }
324 print "</contributor>\n";
325
326 if( $row->rev_minor_edit ) {
327 print " <minor/>\n";
328 }
329 if( $row->rev_comment != '' ) {
330 print " " . wfElementClean( 'comment', null, $row->rev_comment ) . "\n";
331 }
332
333 $text = Revision::getRevisionText( $row );
334 print " " . wfElementClean( 'text', array(), $text ) . "\n";
335 print " </revision>\n";
336
337 wfProfileOut( $fname );
338
339 if( isset( $this->revCallback ) ) {
340 call_user_func( $this->revCallback, $row );
341 }
342 }
343
344 }
345
346 function wfTimestamp2ISO8601( $ts ) {
347 #2003-08-05T18:30:02Z
348 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
349 }
350
351 function xmlsafe( $string ) {
352 $fname = 'xmlsafe';
353 wfProfileIn( $fname );
354
355 /**
356 * The page may contain old data which has not been properly normalized.
357 * Invalid UTF-8 sequences or forbidden control characters will make our
358 * XML output invalid, so be sure to strip them out.
359 */
360 $string = UtfNormal::cleanUp( $string );
361
362 $string = htmlspecialchars( $string );
363 wfProfileOut( $fname );
364 return $string;
365 }
366
367 ?>