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( $this->msg( 'export-addcattext' )->text(), 'catname', 'catname', 40 ) . ' ';
185 $form .= Xml
::submitButton( $this->msg( 'export-addcat' )->text(), array( 'name' => 'addcat' ) ) . '<br />';
187 if ( $wgExportFromNamespaces ) {
188 $form .= Html
::namespaceSelector(
190 'selected' => $nsindex,
191 'label' => $this->msg( 'export-addnstext' )->text()
195 'class' => 'namespaceselector',
198 $form .= Xml
::submitButton( $this->msg( 'export-addns' )->text(), array( 'name' => 'addns' ) ) . '<br />';
201 if ( $wgExportAllowAll ) {
202 $form .= Xml
::checkLabel(
203 $this->msg( 'exportall' )->text(),
206 $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false
210 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
213 if( $wgExportAllowHistory ) {
214 $form .= Xml
::checkLabel(
215 $this->msg( 'exportcuronly' )->text(),
218 $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true
221 $out->addWikiMsg( 'exportnohistory' );
224 $form .= Xml
::checkLabel(
225 $this->msg( 'export-templates' )->text(),
228 $request->wasPosted() ?
$request->getCheck( 'templates' ) : false
231 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
232 $form .= Xml
::inputLabel( $this->msg( 'export-pagelinks' )->text(), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
234 // Enable this when we can do something useful exporting/importing image information. :)
235 //$form .= Xml::checkLabel( $this->msg( 'export-images' )->text(), 'images', 'wpExportImages', false ) . '<br />';
236 $form .= Xml
::checkLabel(
237 $this->msg( 'export-download' )->text(),
240 $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true
243 if ( $wgExportAllowListContributors ) {
244 $form .= Xml
::checkLabel(
245 $this->msg( 'exportlistauthors' )->text(),
248 $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false
252 $form .= Xml
::submitButton( $this->msg( 'export-submit' )->text(), Linker
::tooltipAndAccesskeyAttribs( 'export' ) );
253 $form .= Xml
::closeElement( 'form' );
255 $out->addHTML( $form );
261 private function userCanOverrideExportDepth() {
262 return $this->getUser()->isAllowed( 'override-export-depth' );
266 * Do the actual page exporting
268 * @param $page String: user input on what page(s) to export
269 * @param $history Mixed: one of the WikiExporter history export constants
270 * @param $list_authors Boolean: Whether to add distinct author list (when
271 * not returning full history)
272 * @param $exportall Boolean: Whether to export everything
274 private function doExport( $page, $history, $list_authors, $exportall ) {
276 // If we are grabbing everything, enable full history and ignore the rest
278 $history = WikiExporter
::FULL
;
281 $pageSet = array(); // Inverted index of all pages to look up
283 // Split up and normalize input
284 foreach( explode( "\n", $page ) as $pageName ) {
285 $pageName = trim( $pageName );
286 $title = Title
::newFromText( $pageName );
287 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
288 // Only record each page once!
289 $pageSet[$title->getPrefixedText()] = true;
293 // Set of original pages to pass on to further manipulation...
294 $inputPages = array_keys( $pageSet );
296 // Look up any linked pages if asked...
297 if( $this->templates
) {
298 $pageSet = $this->getTemplates( $inputPages, $pageSet );
300 $linkDepth = $this->pageLinkDepth
;
302 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
306 // Enable this when we can do something useful exporting/importing image information. :)
307 if( $this->images ) ) {
308 $pageSet = $this->getImages( $inputPages, $pageSet );
312 $pages = array_keys( $pageSet );
314 // Normalize titles to the same format and remove dupes, see bug 17374
315 foreach( $pages as $k => $v ) {
316 $pages[$k] = str_replace( " ", "_", $v );
319 $pages = array_unique( $pages );
322 /* Ok, let's get to it... */
323 if( $history == WikiExporter
::CURRENT
) {
325 $db = wfGetDB( DB_SLAVE
);
326 $buffer = WikiExporter
::BUFFER
;
328 // Use an unbuffered query; histories may be very long!
329 $lb = wfGetLBFactory()->newMainLB();
330 $db = $lb->getConnection( DB_SLAVE
);
331 $buffer = WikiExporter
::STREAM
;
333 // This might take a while... :D
334 wfSuppressWarnings();
339 $exporter = new WikiExporter( $db, $history, $buffer );
340 $exporter->list_authors
= $list_authors;
341 $exporter->openStream();
344 $exporter->allPages();
346 foreach( $pages as $page ) {
348 if( $wgExportMaxHistory && !$this->curonly ) {
349 $title = Title::newFromText( $page );
351 $count = Revision::countByTitle( $db, $title );
352 if( $count > $wgExportMaxHistory ) {
353 wfDebug( __FUNCTION__ .
354 ": Skipped $page, $count revisions too big\n" );
359 #Bug 8824: Only export pages the user can read
360 $title = Title
::newFromText( $page );
361 if( is_null( $title ) ) {
362 continue; #TODO: perhaps output an <error> tag or something.
364 if( !$title->userCan( 'read', $this->getUser() ) ) {
365 continue; #TODO: perhaps output an <error> tag or something.
368 $exporter->pageByTitle( $title );
372 $exporter->closeStream();
380 * @param $title Title
383 private function getPagesFromCategory( $title ) {
386 $name = $title->getDBkey();
388 $dbr = wfGetDB( DB_SLAVE
);
390 array( 'page', 'categorylinks' ),
391 array( 'page_namespace', 'page_title' ),
392 array( 'cl_from=page_id', 'cl_to' => $name ),
394 array( 'LIMIT' => '5000' )
399 foreach ( $res as $row ) {
400 $n = $row->page_title
;
401 if ($row->page_namespace
) {
402 $ns = $wgContLang->getNsText( $row->page_namespace
);
412 * @param $nsindex int
415 private function getPagesFromNamespace( $nsindex ) {
418 $dbr = wfGetDB( DB_SLAVE
);
421 array( 'page_namespace', 'page_title' ),
422 array( 'page_namespace' => $nsindex ),
424 array( 'LIMIT' => '5000' )
429 foreach ( $res as $row ) {
430 $n = $row->page_title
;
432 if ( $row->page_namespace
) {
433 $ns = $wgContLang->getNsText( $row->page_namespace
);
443 * Expand a list of pages to include templates used in those pages.
444 * @param $inputPages array, list of titles to look up
445 * @param $pageSet array, associative array indexed by titles for output
446 * @return array associative array index by titles
448 private function getTemplates( $inputPages, $pageSet ) {
449 return $this->getLinks( $inputPages, $pageSet,
451 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
452 array( 'page_id=tl_from' )
457 * Validate link depth setting, if available.
461 private function validateLinkDepth( $depth ) {
462 global $wgExportMaxLinkDepth;
468 if ( !$this->userCanOverrideExportDepth() ) {
469 if( $depth > $wgExportMaxLinkDepth ) {
470 return $wgExportMaxLinkDepth;
475 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
476 * crazy-big export from being done by someone setting the depth
477 * number too high. In other words, last resort safety net.
479 return intval( min( $depth, 5 ) );
483 * Expand a list of pages to include pages linked to from that page.
484 * @param $inputPages array
485 * @param $pageSet array
489 private function getPageLinks( $inputPages, $pageSet, $depth ) {
490 for( ; $depth > 0; --$depth ) {
491 $pageSet = $this->getLinks(
492 $inputPages, $pageSet, 'pagelinks',
493 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
494 array( 'page_id=pl_from' )
496 $inputPages = array_keys( $pageSet );
503 * Expand a list of pages to include images used in those pages.
505 * @param $inputPages array, list of titles to look up
506 * @param $pageSet array, associative array indexed by titles for output
508 * @return array associative array index by titles
510 private function getImages( $inputPages, $pageSet ) {
511 return $this->getLinks(
515 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
516 array( 'page_id=il_from' )
521 * Expand a list of pages to include items used in those pages.
524 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
525 $dbr = wfGetDB( DB_SLAVE
);
527 foreach( $inputPages as $page ) {
528 $title = Title
::newFromText( $page );
531 $pageSet[$title->getPrefixedText()] = true;
532 /// @todo FIXME: May or may not be more efficient to batch these
533 /// by namespace when given multiple input pages.
534 $result = $dbr->select(
535 array( 'page', $table ),
540 'page_namespace' => $title->getNamespace(),
541 'page_title' => $title->getDBkey()
547 foreach( $result as $row ) {
548 $template = Title
::makeTitle( $row->namespace, $row->title
);
549 $pageSet[$template->getPrefixedText()] = true;