3 * Implements Special:Export
5 * Copyright © 2003-2008 Brion Vibber <brion@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
27 * A special page that allows users to export pages in a XML file
29 * @ingroup SpecialPage
31 class SpecialExport
extends SpecialPage
{
33 private $curonly, $doExport, $pageLinkDepth, $templates;
36 public function __construct() {
37 parent
::__construct( 'Export' );
40 public function execute( $par ) {
41 global $wgSitename, $wgExportAllowListContributors, $wgExportFromNamespaces;
42 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
43 global $wgExportAllowAll;
46 $this->outputHeader();
49 $this->curonly
= true;
50 $this->doExport
= false;
51 $request = $this->getRequest();
52 $this->templates
= $request->getCheck( 'templates' );
53 $this->images
= $request->getCheck( 'images' ); // Doesn't do anything yet
54 $this->pageLinkDepth
= $this->validateLinkDepth(
55 $request->getIntOrNull( 'pagelink-depth' )
60 if ( $request->getCheck( 'addcat' ) ) {
61 $page = $request->getText( 'pages' );
62 $catname = $request->getText( 'catname' );
64 if ( $catname !== '' && $catname !== null && $catname !== false ) {
65 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
68 * @todo FIXME: This can lead to hitting memory limit for very large
69 * categories. Ideally we would do the lookup synchronously
70 * during the export in a single query.
72 $catpages = $this->getPagesFromCategory( $t );
74 $page .= "\n" . implode( "\n", $catpages );
79 elseif( $request->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
80 $page = $request->getText( 'pages' );
81 $nsindex = $request->getText( 'nsindex', '' );
83 if ( strval( $nsindex ) !== '' ) {
85 * Same implementation as above, so same @todo
87 $nspages = $this->getPagesFromNamespace( $nsindex );
89 $page .= "\n" . implode( "\n", $nspages );
93 elseif( $request->getCheck( 'exportall' ) && $wgExportAllowAll ) {
94 $this->doExport
= true;
97 elseif( $request->wasPosted() && $par == '' ) {
98 $page = $request->getText( 'pages' );
99 $this->curonly
= $request->getCheck( 'curonly' );
100 $rawOffset = $request->getVal( 'offset' );
103 $offset = wfTimestamp( TS_MW
, $rawOffset );
108 $limit = $request->getInt( 'limit' );
109 $dir = $request->getVal( 'dir' );
113 'limit' => $wgExportMaxHistory,
115 $historyCheck = $request->getCheck( 'history' );
117 if ( $this->curonly
) {
118 $history = WikiExporter
::CURRENT
;
119 } elseif ( !$historyCheck ) {
120 if ( $limit > 0 && ($wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
121 $history['limit'] = $limit;
123 if ( !is_null( $offset ) ) {
124 $history['offset'] = $offset;
126 if ( strtolower( $dir ) == 'desc' ) {
127 $history['dir'] = 'desc';
132 $this->doExport
= true;
135 // Default to current-only for GET requests.
136 $page = $request->getText( 'pages', $par );
137 $historyCheck = $request->getCheck( 'history' );
139 if( $historyCheck ) {
140 $history = WikiExporter
::FULL
;
142 $history = WikiExporter
::CURRENT
;
146 $this->doExport
= true;
150 if( !$wgExportAllowHistory ) {
152 $history = WikiExporter
::CURRENT
;
155 $list_authors = $request->getCheck( 'listauthors' );
156 if ( !$this->curonly ||
!$wgExportAllowListContributors ) {
157 $list_authors = false ;
160 if ( $this->doExport
) {
161 $this->getOutput()->disable();
163 // Cancel output buffering and gzipping if set
164 // This should provide safer streaming for pages with history
165 wfResetOutputBuffers();
166 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
168 if( $request->getCheck( 'wpDownload' ) ) {
169 // Provide a sane filename suggestion
170 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
171 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
174 $this->doExport( $page, $history, $list_authors, $exportall );
179 $out = $this->getOutput();
180 $out->addWikiMsg( 'exporttext' );
182 $form = Xml
::openElement( 'form', array( 'method' => 'post',
183 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
184 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
185 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
187 if ( $wgExportFromNamespaces ) {
188 $form .= Xml
::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
189 $form .= Xml
::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
192 if ( $wgExportAllowAll ) {
193 $form .= Xml
::checkLabel(
194 wfMsg( 'exportall' ),
197 $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false
201 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
204 if( $wgExportAllowHistory ) {
205 $form .= Xml
::checkLabel(
206 wfMsg( 'exportcuronly' ),
209 $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true
212 $out->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
215 $form .= Xml
::checkLabel(
216 wfMsg( 'export-templates' ),
219 $request->wasPosted() ?
$request->getCheck( 'templates' ) : false
222 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
223 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
225 // Enable this when we can do something useful exporting/importing image information. :)
226 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
227 $form .= Xml
::checkLabel(
228 wfMsg( 'export-download' ),
231 $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true
234 if ( $wgExportAllowListContributors ) {
235 $form .= Xml
::checkLabel(
236 wfMsg( 'exportlistauthors' ),
239 $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false
243 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), Linker
::tooltipAndAccesskeyAttribs( 'export' ) );
244 $form .= Xml
::closeElement( 'form' );
246 $out->addHTML( $form );
252 private function userCanOverrideExportDepth() {
253 return $this->getUser()->isAllowed( 'override-export-depth' );
257 * Do the actual page exporting
259 * @param $page String: user input on what page(s) to export
260 * @param $history Mixed: one of the WikiExporter history export constants
261 * @param $list_authors Boolean: Whether to add distinct author list (when
262 * not returning full history)
263 * @param $exportall Boolean: Whether to export everything
265 private function doExport( $page, $history, $list_authors, $exportall ) {
267 // If we are grabbing everything, enable full history and ignore the rest
269 $history = WikiExporter
::FULL
;
272 $pageSet = array(); // Inverted index of all pages to look up
274 // Split up and normalize input
275 foreach( explode( "\n", $page ) as $pageName ) {
276 $pageName = trim( $pageName );
277 $title = Title
::newFromText( $pageName );
278 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
279 // Only record each page once!
280 $pageSet[$title->getPrefixedText()] = true;
284 // Set of original pages to pass on to further manipulation...
285 $inputPages = array_keys( $pageSet );
287 // Look up any linked pages if asked...
288 if( $this->templates
) {
289 $pageSet = $this->getTemplates( $inputPages, $pageSet );
291 $linkDepth = $this->pageLinkDepth
;
293 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
297 // Enable this when we can do something useful exporting/importing image information. :)
298 if( $this->images ) ) {
299 $pageSet = $this->getImages( $inputPages, $pageSet );
303 $pages = array_keys( $pageSet );
305 // Normalize titles to the same format and remove dupes, see bug 17374
306 foreach( $pages as $k => $v ) {
307 $pages[$k] = str_replace( " ", "_", $v );
310 $pages = array_unique( $pages );
313 /* Ok, let's get to it... */
314 if( $history == WikiExporter
::CURRENT
) {
316 $db = wfGetDB( DB_SLAVE
);
317 $buffer = WikiExporter
::BUFFER
;
319 // Use an unbuffered query; histories may be very long!
320 $lb = wfGetLBFactory()->newMainLB();
321 $db = $lb->getConnection( DB_SLAVE
);
322 $buffer = WikiExporter
::STREAM
;
324 // This might take a while... :D
325 wfSuppressWarnings();
330 $exporter = new WikiExporter( $db, $history, $buffer );
331 $exporter->list_authors
= $list_authors;
332 $exporter->openStream();
335 $exporter->allPages();
337 foreach( $pages as $page ) {
339 if( $wgExportMaxHistory && !$this->curonly ) {
340 $title = Title::newFromText( $page );
342 $count = Revision::countByTitle( $db, $title );
343 if( $count > $wgExportMaxHistory ) {
344 wfDebug( __FUNCTION__ .
345 ": Skipped $page, $count revisions too big\n" );
350 #Bug 8824: Only export pages the user can read
351 $title = Title
::newFromText( $page );
352 if( is_null( $title ) ) {
353 continue; #TODO: perhaps output an <error> tag or something.
355 if( !$title->userCan( 'read', $this->getUser() ) ) {
356 continue; #TODO: perhaps output an <error> tag or something.
359 $exporter->pageByTitle( $title );
363 $exporter->closeStream();
371 * @param $title Title
374 private function getPagesFromCategory( $title ) {
377 $name = $title->getDBkey();
379 $dbr = wfGetDB( DB_SLAVE
);
381 array( 'page', 'categorylinks' ),
382 array( 'page_namespace', 'page_title' ),
383 array( 'cl_from=page_id', 'cl_to' => $name ),
385 array( 'LIMIT' => '5000' )
390 foreach ( $res as $row ) {
391 $n = $row->page_title
;
392 if ($row->page_namespace
) {
393 $ns = $wgContLang->getNsText( $row->page_namespace
);
403 * @param $nsindex int
406 private function getPagesFromNamespace( $nsindex ) {
409 $dbr = wfGetDB( DB_SLAVE
);
412 array( 'page_namespace', 'page_title' ),
413 array( 'page_namespace' => $nsindex ),
415 array( 'LIMIT' => '5000' )
420 foreach ( $res as $row ) {
421 $n = $row->page_title
;
423 if ( $row->page_namespace
) {
424 $ns = $wgContLang->getNsText( $row->page_namespace
);
434 * Expand a list of pages to include templates used in those pages.
435 * @param $inputPages array, list of titles to look up
436 * @param $pageSet array, associative array indexed by titles for output
437 * @return array associative array index by titles
439 private function getTemplates( $inputPages, $pageSet ) {
440 return $this->getLinks( $inputPages, $pageSet,
442 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
443 array( 'page_id=tl_from' )
448 * Validate link depth setting, if available.
452 private function validateLinkDepth( $depth ) {
453 global $wgExportMaxLinkDepth;
459 if ( !$this->userCanOverrideExportDepth() ) {
460 if( $depth > $wgExportMaxLinkDepth ) {
461 return $wgExportMaxLinkDepth;
466 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
467 * crazy-big export from being done by someone setting the depth
468 * number too high. In other words, last resort safety net.
470 return intval( min( $depth, 5 ) );
474 * Expand a list of pages to include pages linked to from that page.
475 * @param $inputPages array
476 * @param $pageSet array
480 private function getPageLinks( $inputPages, $pageSet, $depth ) {
481 for( ; $depth > 0; --$depth ) {
482 $pageSet = $this->getLinks(
483 $inputPages, $pageSet, 'pagelinks',
484 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
485 array( 'page_id=pl_from' )
487 $inputPages = array_keys( $pageSet );
494 * Expand a list of pages to include images used in those pages.
496 * @param $inputPages array, list of titles to look up
497 * @param $pageSet array, associative array indexed by titles for output
499 * @return array associative array index by titles
501 private function getImages( $inputPages, $pageSet ) {
502 return $this->getLinks(
506 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
507 array( 'page_id=il_from' )
512 * Expand a list of pages to include items used in those pages.
514 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
515 $dbr = wfGetDB( DB_SLAVE
);
517 foreach( $inputPages as $page ) {
518 $title = Title
::newFromText( $page );
521 $pageSet[$title->getPrefixedText()] = true;
522 /// @todo FIXME: May or may not be more efficient to batch these
523 /// by namespace when given multiple input pages.
524 $result = $dbr->select(
525 array( 'page', $table ),
530 'page_namespace' => $title->getNamespace(),
531 'page_title' => $title->getDBkey()
537 foreach( $result as $row ) {
538 $template = Title
::makeTitle( $row->namespace, $row->title
);
539 $pageSet[$template->getPrefixedText()] = true;