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
{
32 private $curonly, $doExport, $pageLinkDepth, $templates;
34 public function __construct() {
35 parent
::__construct( 'Export' );
38 public function execute( $par ) {
40 $this->outputHeader();
41 $config = $this->getConfig();
44 $this->curonly
= true;
45 $this->doExport
= false;
46 $request = $this->getRequest();
47 $this->templates
= $request->getCheck( 'templates' );
48 $this->pageLinkDepth
= $this->validateLinkDepth(
49 $request->getIntOrNull( 'pagelink-depth' )
54 if ( $request->getCheck( 'addcat' ) ) {
55 $page = $request->getText( 'pages' );
56 $catname = $request->getText( 'catname' );
58 if ( $catname !== '' && $catname !== null && $catname !== false ) {
59 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
62 * @todo FIXME: This can lead to hitting memory limit for very large
63 * categories. Ideally we would do the lookup synchronously
64 * during the export in a single query.
66 $catpages = $this->getPagesFromCategory( $t );
71 $page .= implode( "\n", $catpages );
75 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
76 $page = $request->getText( 'pages' );
77 $nsindex = $request->getText( 'nsindex', '' );
79 if ( strval( $nsindex ) !== '' ) {
81 * Same implementation as above, so same @todo
83 $nspages = $this->getPagesFromNamespace( $nsindex );
85 $page .= "\n" . implode( "\n", $nspages );
88 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
89 $this->doExport
= true;
92 /* Although $page and $history are not used later on, we
93 nevertheless set them to avoid that PHP notices about using
94 undefined variables foul up our XML output (see call to
95 doExport(...) further down) */
98 } elseif ( $request->wasPosted() && $par == '' ) {
99 $page = $request->getText( 'pages' );
100 $this->curonly
= $request->getCheck( 'curonly' );
101 $rawOffset = $request->getVal( 'offset' );
104 $offset = wfTimestamp( TS_MW
, $rawOffset );
109 $maxHistory = $config->get( 'ExportMaxHistory' );
110 $limit = $request->getInt( 'limit' );
111 $dir = $request->getVal( 'dir' );
115 'limit' => $maxHistory,
117 $historyCheck = $request->getCheck( 'history' );
119 if ( $this->curonly
) {
120 $history = WikiExporter
::CURRENT
;
121 } elseif ( !$historyCheck ) {
122 if ( $limit > 0 && ( $maxHistory == 0 ||
$limit < $maxHistory ) ) {
123 $history['limit'] = $limit;
126 if ( !is_null( $offset ) ) {
127 $history['offset'] = $offset;
130 if ( strtolower( $dir ) == 'desc' ) {
131 $history['dir'] = 'desc';
136 $this->doExport
= true;
139 // Default to current-only for GET requests.
140 $page = $request->getText( 'pages', $par );
141 $historyCheck = $request->getCheck( 'history' );
143 if ( $historyCheck ) {
144 $history = WikiExporter
::FULL
;
146 $history = WikiExporter
::CURRENT
;
150 $this->doExport
= true;
154 if ( !$config->get( 'ExportAllowHistory' ) ) {
156 $history = WikiExporter
::CURRENT
;
159 $list_authors = $request->getCheck( 'listauthors' );
160 if ( !$this->curonly ||
!$config->get( 'ExportAllowListContributors' ) ) {
161 $list_authors = false;
164 if ( $this->doExport
) {
165 $this->getOutput()->disable();
167 // Cancel output buffering and gzipping if set
168 // This should provide safer streaming for pages with history
169 wfResetOutputBuffers();
170 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
171 $request->response()->header( "X-Robots-Tag: noindex,nofollow" );
173 if ( $request->getCheck( 'wpDownload' ) ) {
174 // Provide a sane filename suggestion
175 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
176 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
179 $this->doExport( $page, $history, $list_authors, $exportall );
184 $out = $this->getOutput();
185 $out->addWikiMsg( 'exporttext' );
188 $categoryName = $request->getText( 'catname' );
195 'type' => 'textwithbutton',
197 'horizontal-label' => true,
198 'label-message' => 'export-addcattext',
199 'default' => $categoryName,
201 'buttontype' => 'submit',
202 'buttonname' => 'addcat',
203 'buttondefault' => $this->msg( 'export-addcat' )->text(),
206 if ( $config->get( 'ExportFromNamespaces' ) ) {
209 'type' => 'namespaceselectwithbutton',
210 'default' => $nsindex,
211 'label-message' => 'export-addnstext',
212 'horizontal-label' => true,
215 'cssclass' => 'namespaceselector',
216 'buttontype' => 'submit',
217 'buttonname' => 'addns',
218 'buttondefault' => $this->msg( 'export-addns' )->text(),
223 if ( $config->get( 'ExportAllowAll' ) ) {
227 'label-message' => 'exportall',
228 'name' => 'exportall',
230 'default' => $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false,
237 'class' => 'HTMLTextAreaField',
239 'label-message' => 'export-manual',
246 if ( $config->get( 'ExportAllowHistory' ) ) {
250 'label-message' => 'exportcuronly',
253 'default' => $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true,
257 $out->addWikiMsg( 'exportnohistory' );
263 'label-message' => 'export-templates',
264 'name' => 'templates',
265 'id' => 'wpExportTemplates',
266 'default' => $request->wasPosted() ?
$request->getCheck( 'templates' ) : false,
270 if ( $config->get( 'ExportMaxLinkDepth' ) ||
$this->userCanOverrideExportDepth() ) {
272 'pagelink-depth' => [
274 'name' => 'pagelink-depth',
275 'id' => 'pagelink-depth',
276 'label-message' => 'export-pagelinks',
286 'name' =>'wpDownload',
287 'id' => 'wpDownload',
288 'default' => $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true,
289 'label-message' => 'export-download',
293 if ( $config->get( 'ExportAllowListContributors' ) ) {
297 'label-message' => 'exportlistauthors',
298 'default' => $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false,
299 'name' => 'listauthors',
300 'id' => 'listauthors',
305 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
306 $htmlForm->setSubmitTextMsg( 'export-submit' );
307 $htmlForm->prepareForm()->displayForm( false );
308 $this->addHelpLink( 'Help:Export' );
314 private function userCanOverrideExportDepth() {
315 return $this->getUser()->isAllowed( 'override-export-depth' );
319 * Do the actual page exporting
321 * @param string $page User input on what page(s) to export
322 * @param int $history One of the WikiExporter history export constants
323 * @param bool $list_authors Whether to add distinct author list (when
324 * not returning full history)
325 * @param bool $exportall Whether to export everything
327 private function doExport( $page, $history, $list_authors, $exportall ) {
329 // If we are grabbing everything, enable full history and ignore the rest
331 $history = WikiExporter
::FULL
;
333 $pageSet = []; // Inverted index of all pages to look up
335 // Split up and normalize input
336 foreach ( explode( "\n", $page ) as $pageName ) {
337 $pageName = trim( $pageName );
338 $title = Title
::newFromText( $pageName );
339 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
340 // Only record each page once!
341 $pageSet[$title->getPrefixedText()] = true;
345 // Set of original pages to pass on to further manipulation...
346 $inputPages = array_keys( $pageSet );
348 // Look up any linked pages if asked...
349 if ( $this->templates
) {
350 $pageSet = $this->getTemplates( $inputPages, $pageSet );
352 $linkDepth = $this->pageLinkDepth
;
354 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
357 $pages = array_keys( $pageSet );
359 // Normalize titles to the same format and remove dupes, see bug 17374
360 foreach ( $pages as $k => $v ) {
361 $pages[$k] = str_replace( " ", "_", $v );
364 $pages = array_unique( $pages );
367 /* Ok, let's get to it... */
368 if ( $history == WikiExporter
::CURRENT
) {
370 $db = wfGetDB( DB_SLAVE
);
371 $buffer = WikiExporter
::BUFFER
;
373 // Use an unbuffered query; histories may be very long!
374 $lb = wfGetLBFactory()->newMainLB();
375 $db = $lb->getConnection( DB_SLAVE
);
376 $buffer = WikiExporter
::STREAM
;
378 // This might take a while... :D
379 MediaWiki\
suppressWarnings();
381 MediaWiki\restoreWarnings
();
384 $exporter = new WikiExporter( $db, $history, $buffer );
385 $exporter->list_authors
= $list_authors;
386 $exporter->openStream();
389 $exporter->allPages();
391 foreach ( $pages as $page ) {
392 # Bug 8824: 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 ) {
422 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
424 $name = $title->getDBkey();
426 $dbr = wfGetDB( DB_SLAVE
);
428 [ 'page', 'categorylinks' ],
429 [ 'page_namespace', 'page_title' ],
430 [ 'cl_from=page_id', 'cl_to' => $name ],
432 [ 'LIMIT' => $maxPages ]
437 foreach ( $res as $row ) {
438 $n = $row->page_title
;
439 if ( $row->page_namespace
) {
440 $ns = $wgContLang->getNsText( $row->page_namespace
);
451 * @param int $nsindex
454 private function getPagesFromNamespace( $nsindex ) {
457 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
459 $dbr = wfGetDB( DB_SLAVE
);
462 [ 'page_namespace', 'page_title' ],
463 [ 'page_namespace' => $nsindex ],
465 [ 'LIMIT' => $maxPages ]
470 foreach ( $res as $row ) {
471 $n = $row->page_title
;
473 if ( $row->page_namespace
) {
474 $ns = $wgContLang->getNsText( $row->page_namespace
);
485 * Expand a list of pages to include templates used in those pages.
486 * @param array $inputPages List of titles to look up
487 * @param array $pageSet Associative array indexed by titles for output
488 * @return array Associative array index by titles
490 private function getTemplates( $inputPages, $pageSet ) {
491 return $this->getLinks( $inputPages, $pageSet,
493 [ 'namespace' => 'tl_namespace', 'title' => 'tl_title' ],
494 [ 'page_id=tl_from' ]
499 * Validate link depth setting, if available.
503 private function validateLinkDepth( $depth ) {
508 if ( !$this->userCanOverrideExportDepth() ) {
509 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
510 if ( $depth > $maxLinkDepth ) {
511 return $maxLinkDepth;
516 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
517 * crazy-big export from being done by someone setting the depth
518 * number too high. In other words, last resort safety net.
521 return intval( min( $depth, 5 ) );
525 * Expand a list of pages to include pages linked to from that page.
526 * @param array $inputPages
527 * @param array $pageSet
531 private function getPageLinks( $inputPages, $pageSet, $depth ) {
532 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
533 for ( ; $depth > 0; --$depth ) {
534 // @codingStandardsIgnoreEnd
535 $pageSet = $this->getLinks(
536 $inputPages, $pageSet, 'pagelinks',
537 [ 'namespace' => 'pl_namespace', 'title' => 'pl_title' ],
538 [ 'page_id=pl_from' ]
540 $inputPages = array_keys( $pageSet );
547 * Expand a list of pages to include items used in those pages.
548 * @param array $inputPages Array of page titles
549 * @param array $pageSet
550 * @param string $table
551 * @param array $fields Array of field names
555 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
556 $dbr = wfGetDB( DB_SLAVE
);
558 foreach ( $inputPages as $page ) {
559 $title = Title
::newFromText( $page );
562 $pageSet[$title->getPrefixedText()] = true;
563 /// @todo FIXME: May or may not be more efficient to batch these
564 /// by namespace when given multiple input pages.
565 $result = $dbr->select(
571 'page_namespace' => $title->getNamespace(),
572 'page_title' => $title->getDBkey()
578 foreach ( $result as $row ) {
579 $template = Title
::makeTitle( $row->namespace, $row->title
);
580 $pageSet[$template->getPrefixedText()] = true;
588 protected function getGroupName() {