e4e4bfb3d2bdc724ecc532983fdf53ab77f63dc5
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 ) {
40 global $wgSitename, $wgExportAllowListContributors, $wgExportFromNamespaces;
41 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
42 global $wgExportAllowAll;
45 $this->outputHeader();
46 $config = $this->getConfig();
49 $this->curonly
= true;
50 $this->doExport
= false;
51 $request = $this->getRequest();
52 $this->templates
= $request->getCheck( 'templates' );
53 $this->images
= $request->getCheck( 'images' ); // Doesn't do anything yet
54 $this->pageLinkDepth
= $this->validateLinkDepth(
55 $request->getIntOrNull( 'pagelink-depth' )
60 if ( $request->getCheck( 'addcat' ) ) {
61 $page = $request->getText( 'pages' );
62 $catname = $request->getText( 'catname' );
64 if ( $catname !== '' && $catname !== null && $catname !== false ) {
65 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
68 * @todo FIXME: This can lead to hitting memory limit for very large
69 * categories. Ideally we would do the lookup synchronously
70 * during the export in a single query.
72 $catpages = $this->getPagesFromCategory( $t );
74 $page .= "\n" . 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 $page = $request->getText( 'pages' );
103 $this->curonly
= $request->getCheck( 'curonly' );
104 $rawOffset = $request->getVal( 'offset' );
107 $offset = wfTimestamp( TS_MW
, $rawOffset );
112 $maxHistory = $config->get( 'ExportMaxHistory' );
113 $limit = $request->getInt( 'limit' );
114 $dir = $request->getVal( 'dir' );
118 'limit' => $maxHistory,
120 $historyCheck = $request->getCheck( 'history' );
122 if ( $this->curonly
) {
123 $history = WikiExporter
::CURRENT
;
124 } elseif ( !$historyCheck ) {
125 if ( $limit > 0 && ( $maxHistory == 0 ||
$limit < $maxHistory ) ) {
126 $history['limit'] = $limit;
129 if ( !is_null( $offset ) ) {
130 $history['offset'] = $offset;
133 if ( strtolower( $dir ) == 'desc' ) {
134 $history['dir'] = 'desc';
139 $this->doExport
= true;
142 // Default to current-only for GET requests.
143 $page = $request->getText( 'pages', $par );
144 $historyCheck = $request->getCheck( 'history' );
146 if ( $historyCheck ) {
147 $history = WikiExporter
::FULL
;
149 $history = WikiExporter
::CURRENT
;
153 $this->doExport
= true;
157 if ( !$config->get( 'ExportAllowHistory' ) ) {
159 $history = WikiExporter
::CURRENT
;
162 $list_authors = $request->getCheck( 'listauthors' );
163 if ( !$this->curonly ||
!$config->get( 'ExportAllowListContributors' ) ) {
164 $list_authors = false;
167 if ( $this->doExport
) {
168 $this->getOutput()->disable();
170 // Cancel output buffering and gzipping if set
171 // This should provide safer streaming for pages with history
172 wfResetOutputBuffers();
173 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
175 if ( $request->getCheck( 'wpDownload' ) ) {
176 // Provide a sane filename suggestion
177 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
178 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
181 $this->doExport( $page, $history, $list_authors, $exportall );
186 $out = $this->getOutput();
187 $out->addWikiMsg( 'exporttext' );
189 $form = Xml
::openElement( 'form', array( 'method' => 'post',
190 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ) ) );
191 $form .= Xml
::inputLabel(
192 $this->msg( 'export-addcattext' )->text(),
197 $form .= Xml
::submitButton(
198 $this->msg( 'export-addcat' )->text(),
199 array( 'name' => 'addcat' )
202 if ( $config->get( 'ExportFromNamespaces' ) ) {
203 $form .= Html
::namespaceSelector(
205 'selected' => $nsindex,
206 'label' => $this->msg( 'export-addnstext' )->text()
210 'class' => 'namespaceselector',
213 $form .= Xml
::submitButton(
214 $this->msg( 'export-addns' )->text(),
215 array( 'name' => 'addns' )
219 if ( $config->get( 'ExportAllowAll' ) ) {
220 $form .= Xml
::checkLabel(
221 $this->msg( 'exportall' )->text(),
224 $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false
228 $form .= Xml
::element(
230 array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ),
236 if ( $config->get( 'ExportAllowHistory' ) ) {
237 $form .= Xml
::checkLabel(
238 $this->msg( 'exportcuronly' )->text(),
241 $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true
244 $out->addWikiMsg( 'exportnohistory' );
247 $form .= Xml
::checkLabel(
248 $this->msg( 'export-templates' )->text(),
251 $request->wasPosted() ?
$request->getCheck( 'templates' ) : false
254 if ( $config->get( 'ExportMaxLinkDepth' ) ||
$this->userCanOverrideExportDepth() ) {
255 $form .= Xml
::inputLabel(
256 $this->msg( 'export-pagelinks' )->text(),
264 /* Enable this when we can do something useful exporting/importing image information.
265 $form .= Xml::checkLabel(
266 $this->msg( 'export-images' )->text(),
272 $form .= Xml
::checkLabel(
273 $this->msg( 'export-download' )->text(),
276 $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true
279 if ( $config->get( 'ExportAllowListContributors' ) ) {
280 $form .= Xml
::checkLabel(
281 $this->msg( 'exportlistauthors' )->text(),
284 $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false
288 $form .= Xml
::submitButton(
289 $this->msg( 'export-submit' )->text(),
290 Linker
::tooltipAndAccesskeyAttribs( 'export' )
292 $form .= Xml
::closeElement( 'form' );
294 $out->addHTML( $form );
300 private function userCanOverrideExportDepth() {
301 return $this->getUser()->isAllowed( 'override-export-depth' );
305 * Do the actual page exporting
307 * @param string $page User input on what page(s) to export
308 * @param int $history One of the WikiExporter history export constants
309 * @param bool $list_authors Whether to add distinct author list (when
310 * not returning full history)
311 * @param bool $exportall Whether to export everything
313 private function doExport( $page, $history, $list_authors, $exportall ) {
315 // If we are grabbing everything, enable full history and ignore the rest
317 $history = WikiExporter
::FULL
;
320 $pageSet = array(); // Inverted index of all pages to look up
322 // Split up and normalize input
323 foreach ( explode( "\n", $page ) as $pageName ) {
324 $pageName = trim( $pageName );
325 $title = Title
::newFromText( $pageName );
326 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
327 // Only record each page once!
328 $pageSet[$title->getPrefixedText()] = true;
332 // Set of original pages to pass on to further manipulation...
333 $inputPages = array_keys( $pageSet );
335 // Look up any linked pages if asked...
336 if ( $this->templates
) {
337 $pageSet = $this->getTemplates( $inputPages, $pageSet );
339 $linkDepth = $this->pageLinkDepth
;
341 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
344 // Enable this when we can do something useful exporting/importing image information.
345 // if( $this->images ) ) {
346 // $pageSet = $this->getImages( $inputPages, $pageSet );
349 $pages = array_keys( $pageSet );
351 // Normalize titles to the same format and remove dupes, see bug 17374
352 foreach ( $pages as $k => $v ) {
353 $pages[$k] = str_replace( " ", "_", $v );
356 $pages = array_unique( $pages );
359 /* Ok, let's get to it... */
360 if ( $history == WikiExporter
::CURRENT
) {
362 $db = wfGetDB( DB_SLAVE
);
363 $buffer = WikiExporter
::BUFFER
;
365 // Use an unbuffered query; histories may be very long!
366 $lb = wfGetLBFactory()->newMainLB();
367 $db = $lb->getConnection( DB_SLAVE
);
368 $buffer = WikiExporter
::STREAM
;
370 // This might take a while... :D
371 wfSuppressWarnings();
376 $exporter = new WikiExporter( $db, $history, $buffer );
377 $exporter->list_authors
= $list_authors;
378 $exporter->openStream();
381 $exporter->allPages();
383 foreach ( $pages as $page ) {
384 #Bug 8824: Only export pages the user can read
385 $title = Title
::newFromText( $page );
386 if ( is_null( $title ) ) {
387 // @todo Perhaps output an <error> tag or something.
391 if ( !$title->userCan( 'read', $this->getUser() ) ) {
392 // @todo Perhaps output an <error> tag or something.
396 $exporter->pageByTitle( $title );
400 $exporter->closeStream();
408 * @param Title $title
411 private function getPagesFromCategory( $title ) {
414 $name = $title->getDBkey();
416 $dbr = wfGetDB( DB_SLAVE
);
418 array( 'page', 'categorylinks' ),
419 array( 'page_namespace', 'page_title' ),
420 array( 'cl_from=page_id', 'cl_to' => $name ),
422 array( 'LIMIT' => '5000' )
427 foreach ( $res as $row ) {
428 $n = $row->page_title
;
429 if ( $row->page_namespace
) {
430 $ns = $wgContLang->getNsText( $row->page_namespace
);
441 * @param int $nsindex
444 private function getPagesFromNamespace( $nsindex ) {
447 $dbr = wfGetDB( DB_SLAVE
);
450 array( 'page_namespace', 'page_title' ),
451 array( 'page_namespace' => $nsindex ),
453 array( 'LIMIT' => '5000' )
458 foreach ( $res as $row ) {
459 $n = $row->page_title
;
461 if ( $row->page_namespace
) {
462 $ns = $wgContLang->getNsText( $row->page_namespace
);
473 * Expand a list of pages to include templates used in those pages.
474 * @param array $inputPages List of titles to look up
475 * @param array $pageSet Associative array indexed by titles for output
476 * @return array Associative array index by titles
478 private function getTemplates( $inputPages, $pageSet ) {
479 return $this->getLinks( $inputPages, $pageSet,
481 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
482 array( 'page_id=tl_from' )
487 * Validate link depth setting, if available.
491 private function validateLinkDepth( $depth ) {
496 if ( !$this->userCanOverrideExportDepth() ) {
497 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
498 if ( $depth > $maxLinkDepth ) {
499 return $maxLinkDepth;
504 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
505 * crazy-big export from being done by someone setting the depth
506 * number too high. In other words, last resort safety net.
509 return intval( min( $depth, 5 ) );
513 * Expand a list of pages to include pages linked to from that page.
514 * @param array $inputPages
515 * @param array $pageSet
519 private function getPageLinks( $inputPages, $pageSet, $depth ) {
520 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
521 for ( ; $depth > 0; --$depth ) {
522 // @codingStandardsIgnoreEnd
523 $pageSet = $this->getLinks(
524 $inputPages, $pageSet, 'pagelinks',
525 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
526 array( 'page_id=pl_from' )
528 $inputPages = array_keys( $pageSet );
535 * Expand a list of pages to include images used in those pages.
537 * @param array $inputPages List of titles to look up
538 * @param array $pageSet Associative array indexed by titles for output
540 * @return array Associative array index by titles
542 private function getImages( $inputPages, $pageSet ) {
543 return $this->getLinks(
547 array( 'namespace' => NS_FILE
, 'title' => 'il_to' ),
548 array( 'page_id=il_from' )
553 * Expand a list of pages to include items used in those pages.
554 * @param array $inputPages Array of page titles
555 * @param array $pageSet
556 * @param string $table
557 * @param array $fields Array of field names
561 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
562 $dbr = wfGetDB( DB_SLAVE
);
564 foreach ( $inputPages as $page ) {
565 $title = Title
::newFromText( $page );
568 $pageSet[$title->getPrefixedText()] = true;
569 /// @todo FIXME: May or may not be more efficient to batch these
570 /// by namespace when given multiple input pages.
571 $result = $dbr->select(
572 array( 'page', $table ),
577 'page_namespace' => $title->getNamespace(),
578 'page_title' => $title->getDBkey()
584 foreach ( $result as $row ) {
585 $template = Title
::makeTitle( $row->namespace, $row->title
);
586 $pageSet[$template->getPrefixedText()] = true;
594 protected function getGroupName() {