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
;
29 * A special page that allows users to export pages in a XML file
31 * @ingroup SpecialPage
33 class SpecialExport
extends SpecialPage
{
34 private $curonly, $doExport, $pageLinkDepth, $templates;
36 public function __construct() {
37 parent
::__construct( 'Export' );
40 public function execute( $par ) {
42 $this->outputHeader();
43 $config = $this->getConfig();
46 $this->curonly
= true;
47 $this->doExport
= false;
48 $request = $this->getRequest();
49 $this->templates
= $request->getCheck( 'templates' );
50 $this->pageLinkDepth
= $this->validateLinkDepth(
51 $request->getIntOrNull( 'pagelink-depth' )
56 if ( $request->getCheck( 'addcat' ) ) {
57 $page = $request->getText( 'pages' );
58 $catname = $request->getText( 'catname' );
60 if ( $catname !== '' && $catname !== null && $catname !== false ) {
61 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
64 * @todo FIXME: This can lead to hitting memory limit for very large
65 * categories. Ideally we would do the lookup synchronously
66 * during the export in a single query.
68 $catpages = $this->getPagesFromCategory( $t );
73 $page .= implode( "\n", $catpages );
77 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
78 $page = $request->getText( 'pages' );
79 $nsindex = $request->getText( 'nsindex', '' );
81 if ( strval( $nsindex ) !== '' ) {
83 * Same implementation as above, so same @todo
85 $nspages = $this->getPagesFromNamespace( $nsindex );
87 $page .= "\n" . implode( "\n", $nspages );
90 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
91 $this->doExport
= true;
94 /* Although $page and $history are not used later on, we
95 nevertheless set them to avoid that PHP notices about using
96 undefined variables foul up our XML output (see call to
97 doExport(...) further down) */
100 } elseif ( $request->wasPosted() && $par == '' ) {
101 // Log to see if certain parameters are actually used.
102 // If not, we could deprecate them and do some cleanup, here and in WikiExporter.
103 LoggerFactory
::getInstance( 'export' )->debug(
104 'Special:Export POST, dir: [{dir}], offset: [{offset}], limit: [{limit}]', [
105 'dir' => $request->getRawVal( 'dir' ),
106 'offset' => $request->getRawVal( 'offset' ),
107 'limit' => $request->getRawVal( 'limit' ),
110 $page = $request->getText( 'pages' );
111 $this->curonly
= $request->getCheck( 'curonly' );
112 $rawOffset = $request->getVal( 'offset' );
115 $offset = wfTimestamp( TS_MW
, $rawOffset );
120 $maxHistory = $config->get( 'ExportMaxHistory' );
121 $limit = $request->getInt( 'limit' );
122 $dir = $request->getVal( 'dir' );
126 'limit' => $maxHistory,
128 $historyCheck = $request->getCheck( 'history' );
130 if ( $this->curonly
) {
131 $history = WikiExporter
::CURRENT
;
132 } elseif ( !$historyCheck ) {
133 if ( $limit > 0 && ( $maxHistory == 0 ||
$limit < $maxHistory ) ) {
134 $history['limit'] = $limit;
137 if ( !is_null( $offset ) ) {
138 $history['offset'] = $offset;
141 if ( strtolower( $dir ) == 'desc' ) {
142 $history['dir'] = 'desc';
147 $this->doExport
= true;
150 // Default to current-only for GET requests.
151 $page = $request->getText( 'pages', $par );
152 $historyCheck = $request->getCheck( 'history' );
154 if ( $historyCheck ) {
155 $history = WikiExporter
::FULL
;
157 $history = WikiExporter
::CURRENT
;
161 $this->doExport
= true;
165 if ( !$config->get( 'ExportAllowHistory' ) ) {
167 $history = WikiExporter
::CURRENT
;
170 $list_authors = $request->getCheck( 'listauthors' );
171 if ( !$this->curonly ||
!$config->get( 'ExportAllowListContributors' ) ) {
172 $list_authors = false;
175 if ( $this->doExport
) {
176 $this->getOutput()->disable();
178 // Cancel output buffering and gzipping if set
179 // This should provide safer streaming for pages with history
180 wfResetOutputBuffers();
181 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
182 $request->response()->header( "X-Robots-Tag: noindex,nofollow" );
184 if ( $request->getCheck( 'wpDownload' ) ) {
185 // Provide a sane filename suggestion
186 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
187 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
190 $this->doExport( $page, $history, $list_authors, $exportall );
195 $out = $this->getOutput();
196 $out->addWikiMsg( 'exporttext' );
199 $categoryName = $request->getText( 'catname' );
206 'type' => 'textwithbutton',
208 'horizontal-label' => true,
209 'label-message' => 'export-addcattext',
210 'default' => $categoryName,
212 'buttontype' => 'submit',
213 'buttonname' => 'addcat',
214 'buttondefault' => $this->msg( 'export-addcat' )->text(),
215 'hide-if' => [ '===', 'exportall', '1' ],
218 if ( $config->get( 'ExportFromNamespaces' ) ) {
221 'type' => 'namespaceselectwithbutton',
222 'default' => $nsindex,
223 'label-message' => 'export-addnstext',
224 'horizontal-label' => true,
227 'cssclass' => 'namespaceselector',
228 'buttontype' => 'submit',
229 'buttonname' => 'addns',
230 'buttondefault' => $this->msg( 'export-addns' )->text(),
231 'hide-if' => [ '===', 'exportall', '1' ],
236 if ( $config->get( 'ExportAllowAll' ) ) {
240 'label-message' => 'exportall',
241 'name' => 'exportall',
243 'default' => $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false,
250 'class' => HTMLTextAreaField
::class,
252 'label-message' => 'export-manual',
256 'hide-if' => [ '===', 'exportall', '1' ],
260 if ( $config->get( 'ExportAllowHistory' ) ) {
264 'label-message' => 'exportcuronly',
267 'default' => $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true,
271 $out->addWikiMsg( 'exportnohistory' );
277 'label-message' => 'export-templates',
278 'name' => 'templates',
279 'id' => 'wpExportTemplates',
280 'default' => $request->wasPosted() ?
$request->getCheck( 'templates' ) : false,
284 if ( $config->get( 'ExportMaxLinkDepth' ) ||
$this->userCanOverrideExportDepth() ) {
286 'pagelink-depth' => [
288 'name' => 'pagelink-depth',
289 'id' => 'pagelink-depth',
290 'label-message' => 'export-pagelinks',
300 'name' => 'wpDownload',
301 'id' => 'wpDownload',
302 'default' => $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true,
303 'label-message' => 'export-download',
307 if ( $config->get( 'ExportAllowListContributors' ) ) {
311 'label-message' => 'exportlistauthors',
312 'default' => $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false,
313 'name' => 'listauthors',
314 'id' => 'listauthors',
319 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
320 $htmlForm->setSubmitTextMsg( 'export-submit' );
321 $htmlForm->prepareForm()->displayForm( false );
322 $this->addHelpLink( 'Help:Export' );
328 private function userCanOverrideExportDepth() {
329 return $this->getUser()->isAllowed( 'override-export-depth' );
333 * Do the actual page exporting
335 * @param string $page User input on what page(s) to export
336 * @param int $history One of the WikiExporter history export constants
337 * @param bool $list_authors Whether to add distinct author list (when
338 * not returning full history)
339 * @param bool $exportall Whether to export everything
341 private function doExport( $page, $history, $list_authors, $exportall ) {
342 // If we are grabbing everything, enable full history and ignore the rest
344 $history = WikiExporter
::FULL
;
346 $pageSet = []; // Inverted index of all pages to look up
348 // Split up and normalize input
349 foreach ( explode( "\n", $page ) as $pageName ) {
350 $pageName = trim( $pageName );
351 $title = Title
::newFromText( $pageName );
352 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
353 // Only record each page once!
354 $pageSet[$title->getPrefixedText()] = true;
358 // Set of original pages to pass on to further manipulation...
359 $inputPages = array_keys( $pageSet );
361 // Look up any linked pages if asked...
362 if ( $this->templates
) {
363 $pageSet = $this->getTemplates( $inputPages, $pageSet );
365 $linkDepth = $this->pageLinkDepth
;
367 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
370 $pages = array_keys( $pageSet );
372 // Normalize titles to the same format and remove dupes, see T19374
373 foreach ( $pages as $k => $v ) {
374 $pages[$k] = str_replace( " ", "_", $v );
377 $pages = array_unique( $pages );
380 /* 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 foreach ( $pages as $page ) {
392 # T10824: Only export pages the user can read
393 $title = Title
::newFromText( $page );
394 if ( is_null( $title ) ) {
395 // @todo Perhaps output an <error> tag or something.
399 if ( !$title->userCan( 'read', $this->getUser() ) ) {
400 // @todo Perhaps output an <error> tag or something.
404 $exporter->pageByTitle( $title );
408 $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() {