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;
35 public function __construct() {
36 parent
::__construct( 'Export' );
39 public function execute( $par ) {
41 $this->outputHeader();
42 $config = $this->getConfig();
45 $this->curonly
= true;
46 $this->doExport
= false;
47 $request = $this->getRequest();
48 $this->templates
= $request->getCheck( 'templates' );
49 $this->images
= $request->getCheck( 'images' ); // Doesn't do anything yet
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 );
70 $page .= "\n" . implode( "\n", $catpages );
74 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
75 $page = $request->getText( 'pages' );
76 $nsindex = $request->getText( 'nsindex', '' );
78 if ( strval( $nsindex ) !== '' ) {
80 * Same implementation as above, so same @todo
82 $nspages = $this->getPagesFromNamespace( $nsindex );
84 $page .= "\n" . implode( "\n", $nspages );
87 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
88 $this->doExport
= true;
91 /* Although $page and $history are not used later on, we
92 nevertheless set them to avoid that PHP notices about using
93 undefined variables foul up our XML output (see call to
94 doExport(...) further down) */
97 } elseif ( $request->wasPosted() && $par == '' ) {
98 $page = $request->getText( 'pages' );
99 $this->curonly
= $request->getCheck( 'curonly' );
100 $rawOffset = $request->getVal( 'offset' );
103 $offset = wfTimestamp( TS_MW
, $rawOffset );
108 $maxHistory = $config->get( 'ExportMaxHistory' );
109 $limit = $request->getInt( 'limit' );
110 $dir = $request->getVal( 'dir' );
114 'limit' => $maxHistory,
116 $historyCheck = $request->getCheck( 'history' );
118 if ( $this->curonly
) {
119 $history = WikiExporter
::CURRENT
;
120 } elseif ( !$historyCheck ) {
121 if ( $limit > 0 && ( $maxHistory == 0 ||
$limit < $maxHistory ) ) {
122 $history['limit'] = $limit;
125 if ( !is_null( $offset ) ) {
126 $history['offset'] = $offset;
129 if ( strtolower( $dir ) == 'desc' ) {
130 $history['dir'] = 'desc';
135 $this->doExport
= true;
138 // Default to current-only for GET requests.
139 $page = $request->getText( 'pages', $par );
140 $historyCheck = $request->getCheck( 'history' );
142 if ( $historyCheck ) {
143 $history = WikiExporter
::FULL
;
145 $history = WikiExporter
::CURRENT
;
149 $this->doExport
= true;
153 if ( !$config->get( 'ExportAllowHistory' ) ) {
155 $history = WikiExporter
::CURRENT
;
158 $list_authors = $request->getCheck( 'listauthors' );
159 if ( !$this->curonly ||
!$config->get( 'ExportAllowListContributors' ) ) {
160 $list_authors = false;
163 if ( $this->doExport
) {
164 $this->getOutput()->disable();
166 // Cancel output buffering and gzipping if set
167 // This should provide safer streaming for pages with history
168 wfResetOutputBuffers();
169 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
171 if ( $request->getCheck( 'wpDownload' ) ) {
172 // Provide a sane filename suggestion
173 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
174 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
177 $this->doExport( $page, $history, $list_authors, $exportall );
182 $out = $this->getOutput();
183 $out->addWikiMsg( 'exporttext' );
186 $categoryName = $request->getText( 'catname' );
191 $form = Xml
::openElement( 'form', array( 'method' => 'post',
192 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ) ) );
193 $form .= Xml
::inputLabel(
194 $this->msg( 'export-addcattext' )->text(),
200 $form .= Xml
::submitButton(
201 $this->msg( 'export-addcat' )->text(),
202 array( 'name' => 'addcat' )
205 if ( $config->get( 'ExportFromNamespaces' ) ) {
206 $form .= Html
::namespaceSelector(
208 'selected' => $nsindex,
209 'label' => $this->msg( 'export-addnstext' )->text()
213 'class' => 'namespaceselector',
216 $form .= Xml
::submitButton(
217 $this->msg( 'export-addns' )->text(),
218 array( 'name' => 'addns' )
222 if ( $config->get( 'ExportAllowAll' ) ) {
223 $form .= Xml
::checkLabel(
224 $this->msg( 'exportall' )->text(),
227 $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false
231 $form .= Xml
::element(
233 array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ),
239 if ( $config->get( 'ExportAllowHistory' ) ) {
240 $form .= Xml
::checkLabel(
241 $this->msg( 'exportcuronly' )->text(),
244 $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true
247 $out->addWikiMsg( 'exportnohistory' );
250 $form .= Xml
::checkLabel(
251 $this->msg( 'export-templates' )->text(),
254 $request->wasPosted() ?
$request->getCheck( 'templates' ) : false
257 if ( $config->get( 'ExportMaxLinkDepth' ) ||
$this->userCanOverrideExportDepth() ) {
258 $form .= Xml
::inputLabel(
259 $this->msg( 'export-pagelinks' )->text(),
267 /* Enable this when we can do something useful exporting/importing image information.
268 $form .= Xml::checkLabel(
269 $this->msg( 'export-images' )->text(),
275 $form .= Xml
::checkLabel(
276 $this->msg( 'export-download' )->text(),
279 $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true
282 if ( $config->get( 'ExportAllowListContributors' ) ) {
283 $form .= Xml
::checkLabel(
284 $this->msg( 'exportlistauthors' )->text(),
287 $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false
291 $form .= Xml
::submitButton(
292 $this->msg( 'export-submit' )->text(),
293 Linker
::tooltipAndAccesskeyAttribs( 'export' )
295 $form .= Xml
::closeElement( 'form' );
297 $out->addHTML( $form );
303 private function userCanOverrideExportDepth() {
304 return $this->getUser()->isAllowed( 'override-export-depth' );
308 * Do the actual page exporting
310 * @param string $page User input on what page(s) to export
311 * @param int $history One of the WikiExporter history export constants
312 * @param bool $list_authors Whether to add distinct author list (when
313 * not returning full history)
314 * @param bool $exportall Whether to export everything
316 private function doExport( $page, $history, $list_authors, $exportall ) {
318 // If we are grabbing everything, enable full history and ignore the rest
320 $history = WikiExporter
::FULL
;
323 $pageSet = array(); // Inverted index of all pages to look up
325 // Split up and normalize input
326 foreach ( explode( "\n", $page ) as $pageName ) {
327 $pageName = trim( $pageName );
328 $title = Title
::newFromText( $pageName );
329 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
330 // Only record each page once!
331 $pageSet[$title->getPrefixedText()] = true;
335 // Set of original pages to pass on to further manipulation...
336 $inputPages = array_keys( $pageSet );
338 // Look up any linked pages if asked...
339 if ( $this->templates
) {
340 $pageSet = $this->getTemplates( $inputPages, $pageSet );
342 $linkDepth = $this->pageLinkDepth
;
344 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
347 // Enable this when we can do something useful exporting/importing image information.
348 // if( $this->images ) ) {
349 // $pageSet = $this->getImages( $inputPages, $pageSet );
352 $pages = array_keys( $pageSet );
354 // Normalize titles to the same format and remove dupes, see bug 17374
355 foreach ( $pages as $k => $v ) {
356 $pages[$k] = str_replace( " ", "_", $v );
359 $pages = array_unique( $pages );
362 /* Ok, let's get to it... */
363 if ( $history == WikiExporter
::CURRENT
) {
365 $db = wfGetDB( DB_SLAVE
);
366 $buffer = WikiExporter
::BUFFER
;
368 // Use an unbuffered query; histories may be very long!
369 $lb = wfGetLBFactory()->newMainLB();
370 $db = $lb->getConnection( DB_SLAVE
);
371 $buffer = WikiExporter
::STREAM
;
373 // This might take a while... :D
374 wfSuppressWarnings();
379 $exporter = new WikiExporter( $db, $history, $buffer );
380 $exporter->list_authors
= $list_authors;
381 $exporter->openStream();
384 $exporter->allPages();
386 foreach ( $pages as $page ) {
387 #Bug 8824: Only export pages the user can read
388 $title = Title
::newFromText( $page );
389 if ( is_null( $title ) ) {
390 // @todo Perhaps output an <error> tag or something.
394 if ( !$title->userCan( 'read', $this->getUser() ) ) {
395 // @todo Perhaps output an <error> tag or something.
399 $exporter->pageByTitle( $title );
403 $exporter->closeStream();
411 * @param Title $title
414 private function getPagesFromCategory( $title ) {
417 $name = $title->getDBkey();
419 $dbr = wfGetDB( DB_SLAVE
);
421 array( 'page', 'categorylinks' ),
422 array( 'page_namespace', 'page_title' ),
423 array( 'cl_from=page_id', 'cl_to' => $name ),
425 array( 'LIMIT' => '5000' )
430 foreach ( $res as $row ) {
431 $n = $row->page_title
;
432 if ( $row->page_namespace
) {
433 $ns = $wgContLang->getNsText( $row->page_namespace
);
444 * @param int $nsindex
447 private function getPagesFromNamespace( $nsindex ) {
450 $dbr = wfGetDB( DB_SLAVE
);
453 array( 'page_namespace', 'page_title' ),
454 array( 'page_namespace' => $nsindex ),
456 array( 'LIMIT' => '5000' )
461 foreach ( $res as $row ) {
462 $n = $row->page_title
;
464 if ( $row->page_namespace
) {
465 $ns = $wgContLang->getNsText( $row->page_namespace
);
476 * Expand a list of pages to include templates used in those pages.
477 * @param array $inputPages List of titles to look up
478 * @param array $pageSet Associative array indexed by titles for output
479 * @return array Associative array index by titles
481 private function getTemplates( $inputPages, $pageSet ) {
482 return $this->getLinks( $inputPages, $pageSet,
484 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
485 array( 'page_id=tl_from' )
490 * Validate link depth setting, if available.
494 private function validateLinkDepth( $depth ) {
499 if ( !$this->userCanOverrideExportDepth() ) {
500 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
501 if ( $depth > $maxLinkDepth ) {
502 return $maxLinkDepth;
507 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
508 * crazy-big export from being done by someone setting the depth
509 * number too high. In other words, last resort safety net.
512 return intval( min( $depth, 5 ) );
516 * Expand a list of pages to include pages linked to from that page.
517 * @param array $inputPages
518 * @param array $pageSet
522 private function getPageLinks( $inputPages, $pageSet, $depth ) {
523 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
524 for ( ; $depth > 0; --$depth ) {
525 // @codingStandardsIgnoreEnd
526 $pageSet = $this->getLinks(
527 $inputPages, $pageSet, 'pagelinks',
528 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
529 array( 'page_id=pl_from' )
531 $inputPages = array_keys( $pageSet );
538 * Expand a list of pages to include images used in those pages.
540 * @param array $inputPages List of titles to look up
541 * @param array $pageSet Associative array indexed by titles for output
543 * @return array Associative array index by titles
545 private function getImages( $inputPages, $pageSet ) {
546 return $this->getLinks(
550 array( 'namespace' => NS_FILE
, 'title' => 'il_to' ),
551 array( 'page_id=il_from' )
556 * Expand a list of pages to include items used in those pages.
557 * @param array $inputPages Array of page titles
558 * @param array $pageSet
559 * @param string $table
560 * @param array $fields Array of field names
564 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
565 $dbr = wfGetDB( DB_SLAVE
);
567 foreach ( $inputPages as $page ) {
568 $title = Title
::newFromText( $page );
571 $pageSet[$title->getPrefixedText()] = true;
572 /// @todo FIXME: May or may not be more efficient to batch these
573 /// by namespace when given multiple input pages.
574 $result = $dbr->select(
575 array( 'page', $table ),
580 'page_namespace' => $title->getNamespace(),
581 'page_title' => $title->getDBkey()
587 foreach ( $result as $row ) {
588 $template = Title
::makeTitle( $row->namespace, $row->title
);
589 $pageSet[$template->getPrefixedText()] = true;
597 protected function getGroupName() {