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 MediaWikiServices
::getInstance()
331 ->getPermissionManager()
332 ->userHasRight( $this->getUser(), 'override-export-depth' );
336 * Do the actual page exporting
338 * @param string $page User input on what page(s) to export
339 * @param int $history One of the WikiExporter history export constants
340 * @param bool $list_authors Whether to add distinct author list (when
341 * not returning full history)
342 * @param bool $exportall Whether to export everything
344 private function doExport( $page, $history, $list_authors, $exportall ) {
345 // If we are grabbing everything, enable full history and ignore the rest
347 $history = WikiExporter
::FULL
;
349 $pageSet = []; // Inverted index of all pages to look up
351 // Split up and normalize input
352 foreach ( explode( "\n", $page ) as $pageName ) {
353 $pageName = trim( $pageName );
354 $title = Title
::newFromText( $pageName );
355 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
356 // Only record each page once!
357 $pageSet[$title->getPrefixedText()] = true;
361 // Set of original pages to pass on to further manipulation...
362 $inputPages = array_keys( $pageSet );
364 // Look up any linked pages if asked...
365 if ( $this->templates
) {
366 $pageSet = $this->getTemplates( $inputPages, $pageSet );
368 $linkDepth = $this->pageLinkDepth
;
370 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
373 $pages = array_keys( $pageSet );
375 // Normalize titles to the same format and remove dupes, see T19374
376 foreach ( $pages as $k => $v ) {
377 $pages[$k] = str_replace( " ", "_", $v );
380 $pages = array_unique( $pages );
383 /* Ok, let's get to it... */
384 $db = wfGetDB( DB_REPLICA
);
386 $exporter = new WikiExporter( $db, $history );
387 $exporter->list_authors
= $list_authors;
388 $exporter->openStream();
391 $exporter->allPages();
393 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
395 foreach ( $pages as $page ) {
396 # T10824: Only export pages the user can read
397 $title = Title
::newFromText( $page );
398 if ( is_null( $title ) ) {
399 // @todo Perhaps output an <error> tag or something.
403 if ( !$permissionManager->userCan( 'read', $this->getUser(), $title ) ) {
404 // @todo Perhaps output an <error> tag or something.
408 $exporter->pageByTitle( $title );
412 $exporter->closeStream();
416 * @param Title $title
419 private function getPagesFromCategory( $title ) {
420 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
422 $name = $title->getDBkey();
424 $dbr = wfGetDB( DB_REPLICA
);
426 [ 'page', 'categorylinks' ],
427 [ 'page_namespace', 'page_title' ],
428 [ 'cl_from=page_id', 'cl_to' => $name ],
430 [ 'LIMIT' => $maxPages ]
435 foreach ( $res as $row ) {
436 $pages[] = Title
::makeName( $row->page_namespace
, $row->page_title
);
443 * @param int $nsindex
446 private function getPagesFromNamespace( $nsindex ) {
447 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
449 $dbr = wfGetDB( DB_REPLICA
);
452 [ 'page_namespace', 'page_title' ],
453 [ 'page_namespace' => $nsindex ],
455 [ 'LIMIT' => $maxPages ]
460 foreach ( $res as $row ) {
461 $pages[] = Title
::makeName( $row->page_namespace
, $row->page_title
);
468 * Expand a list of pages to include templates used in those pages.
469 * @param array $inputPages List of titles to look up
470 * @param array $pageSet Associative array indexed by titles for output
471 * @return array Associative array index by titles
473 private function getTemplates( $inputPages, $pageSet ) {
474 return $this->getLinks( $inputPages, $pageSet,
476 [ 'namespace' => 'tl_namespace', 'title' => 'tl_title' ],
477 [ 'page_id=tl_from' ]
482 * Validate link depth setting, if available.
486 private function validateLinkDepth( $depth ) {
491 if ( !$this->userCanOverrideExportDepth() ) {
492 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
493 if ( $depth > $maxLinkDepth ) {
494 return $maxLinkDepth;
499 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
500 * crazy-big export from being done by someone setting the depth
501 * number too high. In other words, last resort safety net.
504 return intval( min( $depth, 5 ) );
508 * Expand a list of pages to include pages linked to from that page.
509 * @param array $inputPages
510 * @param array $pageSet
514 private function getPageLinks( $inputPages, $pageSet, $depth ) {
515 for ( ; $depth > 0; --$depth ) {
516 $pageSet = $this->getLinks(
517 $inputPages, $pageSet, 'pagelinks',
518 [ 'namespace' => 'pl_namespace', 'title' => 'pl_title' ],
519 [ 'page_id=pl_from' ]
521 $inputPages = array_keys( $pageSet );
528 * Expand a list of pages to include items used in those pages.
529 * @param array $inputPages Array of page titles
530 * @param array $pageSet
531 * @param string $table
532 * @param array $fields Array of field names
536 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
537 $dbr = wfGetDB( DB_REPLICA
);
539 foreach ( $inputPages as $page ) {
540 $title = Title
::newFromText( $page );
543 $pageSet[$title->getPrefixedText()] = true;
544 /// @todo FIXME: May or may not be more efficient to batch these
545 /// by namespace when given multiple input pages.
546 $result = $dbr->select(
552 'page_namespace' => $title->getNamespace(),
553 'page_title' => $title->getDBkey()
559 foreach ( $result as $row ) {
560 $template = Title
::makeTitle( $row->namespace, $row->title
);
561 $pageSet[$template->getPrefixedText()] = true;
569 protected function getGroupName() {