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 );
68 $page .= "\n" . implode( "\n", $catpages );
72 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
73 $page = $request->getText( 'pages' );
74 $nsindex = $request->getText( 'nsindex', '' );
76 if ( strval( $nsindex ) !== '' ) {
78 * Same implementation as above, so same @todo
80 $nspages = $this->getPagesFromNamespace( $nsindex );
82 $page .= "\n" . implode( "\n", $nspages );
85 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
86 $this->doExport
= true;
89 /* Although $page and $history are not used later on, we
90 nevertheless set them to avoid that PHP notices about using
91 undefined variables foul up our XML output (see call to
92 doExport(...) further down) */
95 } elseif ( $request->wasPosted() && $par == '' ) {
96 $page = $request->getText( 'pages' );
97 $this->curonly
= $request->getCheck( 'curonly' );
98 $rawOffset = $request->getVal( 'offset' );
101 $offset = wfTimestamp( TS_MW
, $rawOffset );
106 $maxHistory = $config->get( 'ExportMaxHistory' );
107 $limit = $request->getInt( 'limit' );
108 $dir = $request->getVal( 'dir' );
112 'limit' => $maxHistory,
114 $historyCheck = $request->getCheck( 'history' );
116 if ( $this->curonly
) {
117 $history = WikiExporter
::CURRENT
;
118 } elseif ( !$historyCheck ) {
119 if ( $limit > 0 && ( $maxHistory == 0 ||
$limit < $maxHistory ) ) {
120 $history['limit'] = $limit;
123 if ( !is_null( $offset ) ) {
124 $history['offset'] = $offset;
127 if ( strtolower( $dir ) == 'desc' ) {
128 $history['dir'] = 'desc';
133 $this->doExport
= true;
136 // Default to current-only for GET requests.
137 $page = $request->getText( 'pages', $par );
138 $historyCheck = $request->getCheck( 'history' );
140 if ( $historyCheck ) {
141 $history = WikiExporter
::FULL
;
143 $history = WikiExporter
::CURRENT
;
147 $this->doExport
= true;
151 if ( !$config->get( 'ExportAllowHistory' ) ) {
153 $history = WikiExporter
::CURRENT
;
156 $list_authors = $request->getCheck( 'listauthors' );
157 if ( !$this->curonly ||
!$config->get( 'ExportAllowListContributors' ) ) {
158 $list_authors = false;
161 if ( $this->doExport
) {
162 $this->getOutput()->disable();
164 // Cancel output buffering and gzipping if set
165 // This should provide safer streaming for pages with history
166 wfResetOutputBuffers();
167 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
169 if ( $request->getCheck( 'wpDownload' ) ) {
170 // Provide a sane filename suggestion
171 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
172 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
175 $this->doExport( $page, $history, $list_authors, $exportall );
180 $out = $this->getOutput();
181 $out->addWikiMsg( 'exporttext' );
184 $categoryName = $request->getText( 'catname' );
189 $formDescriptor = array(
191 'type' => 'textwithbutton',
193 'horizontal-label' => true,
194 'label-message' => 'export-addcattext',
195 'default' => $categoryName,
197 'buttontype' => 'submit',
198 'buttonname' => 'addcat',
199 'buttondefault' => $this->msg( 'export-addcat' )->text(),
202 if ( $config->get( 'ExportFromNamespaces' ) ) {
203 $formDescriptor +
= array(
205 'type' => 'namespaceselectwithbutton',
206 'default' => $nsindex,
207 'label-message' => 'export-addnstext',
208 'horizontal-label' => true,
211 'cssclass' => 'namespaceselector',
212 'buttontype' => 'submit',
213 'buttonname' => 'addns',
214 'buttondefault' => $this->msg( 'export-addns' )->text(),
219 if ( $config->get( 'ExportAllowAll' ) ) {
220 $formDescriptor +
= array(
221 'exportall' => array(
223 'label-message' => 'exportall',
224 'name' => 'exportall',
226 'default' => $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false,
231 $formDescriptor +
= array(
233 'class' => 'HTMLTextAreaField',
242 if ( $config->get( 'ExportAllowHistory' ) ) {
243 $formDescriptor +
= array(
246 'label-message' => 'exportcuronly',
249 'default' => $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true,
253 $out->addWikiMsg( 'exportnohistory' );
256 $formDescriptor +
= array(
257 'templates' => array(
259 'label-message' => 'export-templates',
260 'name' => 'templates',
261 'id' => 'wpExportTemplates',
262 'default' => $request->wasPosted() ?
$request->getCheck( 'templates' ) : false,
266 if ( $config->get( 'ExportMaxLinkDepth' ) ||
$this->userCanOverrideExportDepth() ) {
267 $formDescriptor +
= array(
268 'pagelink-depth' => array(
270 'name' => 'pagelink-depth',
271 'id' => 'pagelink-depth',
272 'label-message' => 'export-pagelinks',
279 $formDescriptor +
= array(
280 'wpDownload' => array(
282 'name' =>'wpDownload',
283 'id' => 'wpDownload',
284 'default' => $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true,
285 'label-message' => 'export-download',
289 if ( $config->get( 'ExportAllowListContributors' ) ) {
290 $formDescriptor +
= array(
291 'listauthors' => array(
293 'label-message' => 'exportlistauthors',
294 'default' => $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false,
295 'name' => 'listauthors',
296 'id' => 'listauthors',
301 $htmlForm = HTMLForm
::factory( 'div', $formDescriptor, $this->getContext() );
302 $htmlForm->setSubmitTextMsg( 'export-submit' );
303 $htmlForm->prepareForm()->displayForm( false );
304 $this->addHelpLink( 'Help:Export' );
310 private function userCanOverrideExportDepth() {
311 return $this->getUser()->isAllowed( 'override-export-depth' );
315 * Do the actual page exporting
317 * @param string $page User input on what page(s) to export
318 * @param int $history One of the WikiExporter history export constants
319 * @param bool $list_authors Whether to add distinct author list (when
320 * not returning full history)
321 * @param bool $exportall Whether to export everything
323 private function doExport( $page, $history, $list_authors, $exportall ) {
325 // If we are grabbing everything, enable full history and ignore the rest
327 $history = WikiExporter
::FULL
;
329 $pageSet = array(); // Inverted index of all pages to look up
331 // Split up and normalize input
332 foreach ( explode( "\n", $page ) as $pageName ) {
333 $pageName = trim( $pageName );
334 $title = Title
::newFromText( $pageName );
335 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
336 // Only record each page once!
337 $pageSet[$title->getPrefixedText()] = true;
341 // Set of original pages to pass on to further manipulation...
342 $inputPages = array_keys( $pageSet );
344 // Look up any linked pages if asked...
345 if ( $this->templates
) {
346 $pageSet = $this->getTemplates( $inputPages, $pageSet );
348 $linkDepth = $this->pageLinkDepth
;
350 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
353 $pages = array_keys( $pageSet );
355 // Normalize titles to the same format and remove dupes, see bug 17374
356 foreach ( $pages as $k => $v ) {
357 $pages[$k] = str_replace( " ", "_", $v );
360 $pages = array_unique( $pages );
363 /* Ok, let's get to it... */
364 if ( $history == WikiExporter
::CURRENT
) {
366 $db = wfGetDB( DB_SLAVE
);
367 $buffer = WikiExporter
::BUFFER
;
369 // Use an unbuffered query; histories may be very long!
370 $lb = wfGetLBFactory()->newMainLB();
371 $db = $lb->getConnection( DB_SLAVE
);
372 $buffer = WikiExporter
::STREAM
;
374 // This might take a while... :D
375 MediaWiki\
suppressWarnings();
377 MediaWiki\restoreWarnings
();
380 $exporter = new WikiExporter( $db, $history, $buffer );
381 $exporter->list_authors
= $list_authors;
382 $exporter->openStream();
385 $exporter->allPages();
387 foreach ( $pages as $page ) {
388 #Bug 8824: Only export pages the user can read
389 $title = Title
::newFromText( $page );
390 if ( is_null( $title ) ) {
391 // @todo Perhaps output an <error> tag or something.
395 if ( !$title->userCan( 'read', $this->getUser() ) ) {
396 // @todo Perhaps output an <error> tag or something.
400 $exporter->pageByTitle( $title );
404 $exporter->closeStream();
412 * @param Title $title
415 private function getPagesFromCategory( $title ) {
418 $name = $title->getDBkey();
420 $dbr = wfGetDB( DB_SLAVE
);
422 array( 'page', 'categorylinks' ),
423 array( 'page_namespace', 'page_title' ),
424 array( 'cl_from=page_id', 'cl_to' => $name ),
426 array( 'LIMIT' => '5000' )
431 foreach ( $res as $row ) {
432 $n = $row->page_title
;
433 if ( $row->page_namespace
) {
434 $ns = $wgContLang->getNsText( $row->page_namespace
);
445 * @param int $nsindex
448 private function getPagesFromNamespace( $nsindex ) {
451 $dbr = wfGetDB( DB_SLAVE
);
454 array( 'page_namespace', 'page_title' ),
455 array( 'page_namespace' => $nsindex ),
457 array( 'LIMIT' => '5000' )
462 foreach ( $res as $row ) {
463 $n = $row->page_title
;
465 if ( $row->page_namespace
) {
466 $ns = $wgContLang->getNsText( $row->page_namespace
);
477 * Expand a list of pages to include templates used in those pages.
478 * @param array $inputPages List of titles to look up
479 * @param array $pageSet Associative array indexed by titles for output
480 * @return array Associative array index by titles
482 private function getTemplates( $inputPages, $pageSet ) {
483 return $this->getLinks( $inputPages, $pageSet,
485 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
486 array( 'page_id=tl_from' )
491 * Validate link depth setting, if available.
495 private function validateLinkDepth( $depth ) {
500 if ( !$this->userCanOverrideExportDepth() ) {
501 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
502 if ( $depth > $maxLinkDepth ) {
503 return $maxLinkDepth;
508 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
509 * crazy-big export from being done by someone setting the depth
510 * number too high. In other words, last resort safety net.
513 return intval( min( $depth, 5 ) );
517 * Expand a list of pages to include pages linked to from that page.
518 * @param array $inputPages
519 * @param array $pageSet
523 private function getPageLinks( $inputPages, $pageSet, $depth ) {
524 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
525 for ( ; $depth > 0; --$depth ) {
526 // @codingStandardsIgnoreEnd
527 $pageSet = $this->getLinks(
528 $inputPages, $pageSet, 'pagelinks',
529 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
530 array( 'page_id=pl_from' )
532 $inputPages = array_keys( $pageSet );
539 * Expand a list of pages to include items used in those pages.
540 * @param array $inputPages Array of page titles
541 * @param array $pageSet
542 * @param string $table
543 * @param array $fields Array of field names
547 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
548 $dbr = wfGetDB( DB_SLAVE
);
550 foreach ( $inputPages as $page ) {
551 $title = Title
::newFromText( $page );
554 $pageSet[$title->getPrefixedText()] = true;
555 /// @todo FIXME: May or may not be more efficient to batch these
556 /// by namespace when given multiple input pages.
557 $result = $dbr->select(
558 array( 'page', $table ),
563 'page_namespace' => $title->getNamespace(),
564 'page_title' => $title->getDBkey()
570 foreach ( $result as $row ) {
571 $template = Title
::makeTitle( $row->namespace, $row->title
);
572 $pageSet[$template->getPrefixedText()] = true;
580 protected function getGroupName() {