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
26 use MediaWiki\Logger\LoggerFactory
;
27 use MediaWiki\MediaWikiServices
;
30 * A special page that allows users to export pages in a XML file
32 * @ingroup SpecialPage
34 class SpecialExport
extends SpecialPage
{
35 private $curonly, $doExport, $pageLinkDepth, $templates;
37 public function __construct() {
38 parent
::__construct( 'Export' );
41 public function execute( $par ) {
43 $this->outputHeader();
44 $config = $this->getConfig();
47 $this->curonly
= true;
48 $this->doExport
= false;
49 $request = $this->getRequest();
50 $this->templates
= $request->getCheck( 'templates' );
51 $this->pageLinkDepth
= $this->validateLinkDepth(
52 $request->getIntOrNull( 'pagelink-depth' )
57 if ( $request->getCheck( 'addcat' ) ) {
58 $page = $request->getText( 'pages' );
59 $catname = $request->getText( 'catname' );
61 if ( $catname !== '' && $catname !== null && $catname !== false ) {
62 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
65 * @todo FIXME: This can lead to hitting memory limit for very large
66 * categories. Ideally we would do the lookup synchronously
67 * during the export in a single query.
69 $catpages = $this->getPagesFromCategory( $t );
74 $page .= implode( "\n", $catpages );
78 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
79 $page = $request->getText( 'pages' );
80 $nsindex = $request->getText( 'nsindex', '' );
82 if ( strval( $nsindex ) !== '' ) {
84 * Same implementation as above, so same @todo
86 $nspages = $this->getPagesFromNamespace( $nsindex );
88 $page .= "\n" . implode( "\n", $nspages );
91 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
92 $this->doExport
= true;
95 /* Although $page and $history are not used later on, we
96 nevertheless set them to avoid that PHP notices about using
97 undefined variables foul up our XML output (see call to
98 doExport(...) further down) */
101 } elseif ( $request->wasPosted() && $par == '' ) {
102 // Log to see if certain parameters are actually used.
103 // If not, we could deprecate them and do some cleanup, here and in WikiExporter.
104 LoggerFactory
::getInstance( 'export' )->debug(
105 'Special:Export POST, dir: [{dir}], offset: [{offset}], limit: [{limit}]', [
106 'dir' => $request->getRawVal( 'dir' ),
107 'offset' => $request->getRawVal( 'offset' ),
108 'limit' => $request->getRawVal( 'limit' ),
111 $page = $request->getText( 'pages' );
112 $this->curonly
= $request->getCheck( 'curonly' );
113 $rawOffset = $request->getVal( 'offset' );
116 $offset = wfTimestamp( TS_MW
, $rawOffset );
121 $maxHistory = $config->get( 'ExportMaxHistory' );
122 $limit = $request->getInt( 'limit' );
123 $dir = $request->getVal( 'dir' );
127 'limit' => $maxHistory,
129 $historyCheck = $request->getCheck( 'history' );
131 if ( $this->curonly
) {
132 $history = WikiExporter
::CURRENT
;
133 } elseif ( !$historyCheck ) {
134 if ( $limit > 0 && ( $maxHistory == 0 ||
$limit < $maxHistory ) ) {
135 $history['limit'] = $limit;
138 if ( !is_null( $offset ) ) {
139 $history['offset'] = $offset;
142 if ( strtolower( $dir ) == 'desc' ) {
143 $history['dir'] = 'desc';
148 $this->doExport
= true;
151 // Default to current-only for GET requests.
152 $page = $request->getText( 'pages', $par );
153 $historyCheck = $request->getCheck( 'history' );
155 if ( $historyCheck ) {
156 $history = WikiExporter
::FULL
;
158 $history = WikiExporter
::CURRENT
;
162 $this->doExport
= true;
166 if ( !$config->get( 'ExportAllowHistory' ) ) {
168 $history = WikiExporter
::CURRENT
;
171 $list_authors = $request->getCheck( 'listauthors' );
172 if ( !$this->curonly ||
!$config->get( 'ExportAllowListContributors' ) ) {
173 $list_authors = false;
176 if ( $this->doExport
) {
177 $this->getOutput()->disable();
179 // Cancel output buffering and gzipping if set
180 // This should provide safer streaming for pages with history
181 wfResetOutputBuffers();
182 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
183 $request->response()->header( "X-Robots-Tag: noindex,nofollow" );
185 if ( $request->getCheck( 'wpDownload' ) ) {
186 // Provide a sane filename suggestion
187 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
188 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
191 $this->doExport( $page, $history, $list_authors, $exportall );
196 $out = $this->getOutput();
197 $out->addWikiMsg( 'exporttext' );
200 $categoryName = $request->getText( 'catname' );
207 'type' => 'textwithbutton',
209 'horizontal-label' => true,
210 'label-message' => 'export-addcattext',
211 'default' => $categoryName,
213 'buttontype' => 'submit',
214 'buttonname' => 'addcat',
215 'buttondefault' => $this->msg( 'export-addcat' )->text(),
216 'hide-if' => [ '===', 'exportall', '1' ],
219 if ( $config->get( 'ExportFromNamespaces' ) ) {
222 'type' => 'namespaceselectwithbutton',
223 'default' => $nsindex,
224 'label-message' => 'export-addnstext',
225 'horizontal-label' => true,
228 'cssclass' => 'namespaceselector',
229 'buttontype' => 'submit',
230 'buttonname' => 'addns',
231 'buttondefault' => $this->msg( 'export-addns' )->text(),
232 'hide-if' => [ '===', 'exportall', '1' ],
237 if ( $config->get( 'ExportAllowAll' ) ) {
241 'label-message' => 'exportall',
242 'name' => 'exportall',
244 'default' => $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false,
251 'class' => HTMLTextAreaField
::class,
253 'label-message' => 'export-manual',
257 'hide-if' => [ '===', 'exportall', '1' ],
261 if ( $config->get( 'ExportAllowHistory' ) ) {
265 'label-message' => 'exportcuronly',
268 'default' => $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true,
272 $out->addWikiMsg( 'exportnohistory' );
278 'label-message' => 'export-templates',
279 'name' => 'templates',
280 'id' => 'wpExportTemplates',
281 'default' => $request->wasPosted() ?
$request->getCheck( 'templates' ) : false,
285 if ( $config->get( 'ExportMaxLinkDepth' ) ||
$this->userCanOverrideExportDepth() ) {
287 'pagelink-depth' => [
289 'name' => 'pagelink-depth',
290 'id' => 'pagelink-depth',
291 'label-message' => 'export-pagelinks',
301 'name' => 'wpDownload',
302 'id' => 'wpDownload',
303 'default' => $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true,
304 'label-message' => 'export-download',
308 if ( $config->get( 'ExportAllowListContributors' ) ) {
312 'label-message' => 'exportlistauthors',
313 'default' => $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false,
314 'name' => 'listauthors',
315 'id' => 'listauthors',
320 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
321 $htmlForm->setSubmitTextMsg( 'export-submit' );
322 $htmlForm->prepareForm()->displayForm( false );
323 $this->addHelpLink( 'Help:Export' );
329 private function userCanOverrideExportDepth() {
330 return $this->getUser()->isAllowed( 'override-export-depth' );
334 * Do the actual page exporting
336 * @param string $page User input on what page(s) to export
337 * @param int $history One of the WikiExporter history export constants
338 * @param bool $list_authors Whether to add distinct author list (when
339 * not returning full history)
340 * @param bool $exportall Whether to export everything
342 private function doExport( $page, $history, $list_authors, $exportall ) {
343 // If we are grabbing everything, enable full history and ignore the rest
345 $history = WikiExporter
::FULL
;
347 $pageSet = []; // Inverted index of all pages to look up
349 // Split up and normalize input
350 foreach ( explode( "\n", $page ) as $pageName ) {
351 $pageName = trim( $pageName );
352 $title = Title
::newFromText( $pageName );
353 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
354 // Only record each page once!
355 $pageSet[$title->getPrefixedText()] = true;
359 // Set of original pages to pass on to further manipulation...
360 $inputPages = array_keys( $pageSet );
362 // Look up any linked pages if asked...
363 if ( $this->templates
) {
364 $pageSet = $this->getTemplates( $inputPages, $pageSet );
366 $linkDepth = $this->pageLinkDepth
;
368 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
371 $pages = array_keys( $pageSet );
373 // Normalize titles to the same format and remove dupes, see T19374
374 foreach ( $pages as $k => $v ) {
375 $pages[$k] = str_replace( " ", "_", $v );
378 $pages = array_unique( $pages );
381 /* Ok, let's get to it... */
382 $db = wfGetDB( DB_REPLICA
);
384 $exporter = new WikiExporter( $db, $history );
385 $exporter->list_authors
= $list_authors;
386 $exporter->openStream();
389 $exporter->allPages();
391 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
393 foreach ( $pages as $page ) {
394 # T10824: Only export pages the user can read
395 $title = Title
::newFromText( $page );
396 if ( is_null( $title ) ) {
397 // @todo Perhaps output an <error> tag or something.
401 if ( !$permissionManager->userCan( 'read', $this->getUser(), $title ) ) {
402 // @todo Perhaps output an <error> tag or something.
406 $exporter->pageByTitle( $title );
410 $exporter->closeStream();
414 * @param Title $title
417 private function getPagesFromCategory( $title ) {
418 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
420 $name = $title->getDBkey();
422 $dbr = wfGetDB( DB_REPLICA
);
424 [ 'page', 'categorylinks' ],
425 [ 'page_namespace', 'page_title' ],
426 [ 'cl_from=page_id', 'cl_to' => $name ],
428 [ 'LIMIT' => $maxPages ]
433 foreach ( $res as $row ) {
434 $pages[] = Title
::makeName( $row->page_namespace
, $row->page_title
);
441 * @param int $nsindex
444 private function getPagesFromNamespace( $nsindex ) {
445 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
447 $dbr = wfGetDB( DB_REPLICA
);
450 [ 'page_namespace', 'page_title' ],
451 [ 'page_namespace' => $nsindex ],
453 [ 'LIMIT' => $maxPages ]
458 foreach ( $res as $row ) {
459 $pages[] = Title
::makeName( $row->page_namespace
, $row->page_title
);
466 * Expand a list of pages to include templates used in those pages.
467 * @param array $inputPages List of titles to look up
468 * @param array $pageSet Associative array indexed by titles for output
469 * @return array Associative array index by titles
471 private function getTemplates( $inputPages, $pageSet ) {
472 return $this->getLinks( $inputPages, $pageSet,
474 [ 'namespace' => 'tl_namespace', 'title' => 'tl_title' ],
475 [ 'page_id=tl_from' ]
480 * Validate link depth setting, if available.
484 private function validateLinkDepth( $depth ) {
489 if ( !$this->userCanOverrideExportDepth() ) {
490 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
491 if ( $depth > $maxLinkDepth ) {
492 return $maxLinkDepth;
497 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
498 * crazy-big export from being done by someone setting the depth
499 * number too high. In other words, last resort safety net.
502 return intval( min( $depth, 5 ) );
506 * Expand a list of pages to include pages linked to from that page.
507 * @param array $inputPages
508 * @param array $pageSet
512 private function getPageLinks( $inputPages, $pageSet, $depth ) {
513 for ( ; $depth > 0; --$depth ) {
514 $pageSet = $this->getLinks(
515 $inputPages, $pageSet, 'pagelinks',
516 [ 'namespace' => 'pl_namespace', 'title' => 'pl_title' ],
517 [ 'page_id=pl_from' ]
519 $inputPages = array_keys( $pageSet );
526 * Expand a list of pages to include items used in those pages.
527 * @param array $inputPages Array of page titles
528 * @param array $pageSet
529 * @param string $table
530 * @param array $fields Array of field names
534 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
535 $dbr = wfGetDB( DB_REPLICA
);
537 foreach ( $inputPages as $page ) {
538 $title = Title
::newFromText( $page );
541 $pageSet[$title->getPrefixedText()] = true;
542 /// @todo FIXME: May or may not be more efficient to batch these
543 /// by namespace when given multiple input pages.
544 $result = $dbr->select(
550 'page_namespace' => $title->getNamespace(),
551 'page_title' => $title->getDBkey()
557 foreach ( $result as $row ) {
558 $template = Title
::makeTitle( $row->namespace, $row->title
);
559 $pageSet[$template->getPrefixedText()] = true;
567 protected function getGroupName() {