722c1191e6906341d4ebac878c39eb2fcf09e8f9
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
22 * @subpackage SpecialPage
26 require_once( 'Revision.php' );
31 function wfSpecialExport( $page = '' ) {
32 global $wgOut, $wgLang, $wgRequest;
34 if( $wgRequest->getVal( 'action' ) == 'submit') {
35 $page = $wgRequest->getText( 'pages' );
36 $curonly = $wgRequest->getCheck( 'curonly' );
38 # Pre-check the 'current version only' box in the UI
44 header( "Content-type: application/xml; charset=utf-8" );
45 $pages = explode( "\n", $page );
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();
56 $wgOut->addWikiText( wfMsg( "exporttext" ) );
57 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Export" );
58 $action = $titleObj->escapeLocalURL( 'action=submit' );
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' />
70 define( 'MW_EXPORT_FULL', 0 );
71 define( 'MW_EXPORT_CURRENT', 1 );
73 define( 'MW_EXPORT_BUFFER', 0 );
74 define( 'MW_EXPORT_STREAM', 1 );
77 var $pageCallback = null;
78 var $revCallback = null;
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.
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
91 function WikiExporter( &$db, $history = MW_EXPORT_CURRENT
,
92 $buffer = MW_EXPORT_BUFFER
) {
94 $this->history
= $history;
95 $this->buffer
= $buffer;
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.
103 * A set callback can be removed by passing null here.
105 * @param mixed $callback
107 function setPageCallback( $callback ) {
108 $this->pageCallback
= $callback;
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.
116 * A set callback can be removed by passing null here.
118 * @param mixed $callback
120 function setRevCallback( $callback ) {
121 $this->revCallback
= $callback;
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.
130 * To capture the stream to a string, use PHP's output buffering
131 * functions. Output will be encoded in UTF-8.
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',
141 'xml:lang' => $wgContLanguageCode ),
146 * Closes the output stream with the closing root element.
147 * Call when finished dumping things.
149 function closeStream() {
150 print "</mediawiki>\n";
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.
159 * @param Database $db
161 function allPages() {
162 return $this->dumpFrom( '' );
166 * @param Title $title
168 function pageByTitle( $title ) {
169 return $this->dumpFrom(
170 'page_namespace=' . $title->getNamespace() .
171 ' AND page_title=' . $this->db
->addQuotes( $title->getDbKey() ) );
174 function pageByName( $name ) {
175 $title = Title
::newFromText( $name );
176 if( is_null( $title ) ) {
177 return WikiError( "Can't export invalid title" );
179 return $this->pageByTitle( $title );
183 function pagesByName( $names ) {
184 foreach( $names as $name ) {
185 $this->pageByName( $name );
190 // -------------------- private implementation below --------------------
192 function dumpFrom( $cond = '' ) {
193 $fname = 'WikiExporter::dumpFrom';
194 wfProfileIn( $fname );
196 $page = $this->db
->tableName( 'page' );
197 $revision = $this->db
->tableName( 'revision' );
198 $text = $this->db
->tableName( 'text' );
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';
205 wfProfileOut( $fname );
206 return new WikiError( "$fname given invalid history dump type." );
208 $where = ( $cond == '' ) ?
'' : "$cond AND";
210 if( $this->buffer
== MW_EXPORT_STREAM
) {
211 $prev = $this->db
->bufferResults( false );
213 $result = $this->db
->query(
215 $page FORCE INDEX (PRIMARY),
216 $revision FORCE INDEX(page_timestamp),
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 );
223 if( $this->buffer
== MW_EXPORT_STREAM
) {
224 $this->db
->bufferResults( $prev );
227 wfProfileOut( $fname );
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.
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.
240 * @param ResultWrapper $resultset
243 function outputStream( $resultset ) {
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 );
252 $this->openPage( $row );
255 $this->dumpRev( $row );
257 if( isset( $last ) ) {
258 $this->closePage( $last );
264 * Opens a <page> section on the output stream, with data
265 * from the given database row.
270 function openPage( $row ) {
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";
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.
289 function closePage( $row ) {
291 if( isset( $this->pageCallback
) ) {
292 call_user_func( $this->pageCallback
, $row );
297 * Dumps a <revision> section on the output stream, with
298 * data filled in from the given database row.
303 function dumpRev( $row ) {
304 $fname = 'WikiExporter::dumpRev';
305 wfProfileIn( $fname );
307 print " <revision>\n";
308 print " " . wfElement( 'id', null, $row->rev_id
) . "\n";
310 $ts = wfTimestamp2ISO8601( $row->rev_timestamp
);
311 print " " . wfElement( 'timestamp', null, $ts ) . "\n";
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
);
318 print wfElementClean( 'ip', null, $row->rev_user_text
);
320 print "</contributor>\n";
322 if( $row->rev_minor_edit
) {
325 if( $row->rev_comment
!= '' ) {
326 print " " . wfElementClean( 'comment', null, $row->rev_comment
) . "\n";
329 $text = Revision
::getRevisionText( $row );
330 print " " . wfElementClean( 'text', array(), $text ) . "\n";
331 print " </revision>\n";
333 wfProfileOut( $fname );
335 if( isset( $this->revCallback
) ) {
336 call_user_func( $this->revCallback
, $row );
342 function wfTimestamp2ISO8601( $ts ) {
343 #2003-08-05T18:30:02Z
344 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
347 function xmlsafe( $string ) {
349 wfProfileIn( $fname );
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.
356 $string = UtfNormal
::cleanUp( $string );
358 $string = htmlspecialchars( $string );
359 wfProfileOut( $fname );