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();
48 $this->curonly
= true;
49 $this->doExport
= false;
50 $request = $this->getRequest();
51 $this->templates
= $request->getCheck( 'templates' );
52 $this->images
= $request->getCheck( 'images' ); // Doesn't do anything yet
53 $this->pageLinkDepth
= $this->validateLinkDepth(
54 $request->getIntOrNull( 'pagelink-depth' )
59 if ( $request->getCheck( 'addcat' ) ) {
60 $page = $request->getText( 'pages' );
61 $catname = $request->getText( 'catname' );
63 if ( $catname !== '' && $catname !== null && $catname !== false ) {
64 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
67 * @todo FIXME: This can lead to hitting memory limit for very large
68 * categories. Ideally we would do the lookup synchronously
69 * during the export in a single query.
71 $catpages = $this->getPagesFromCategory( $t );
73 $page .= "\n" . implode( "\n", $catpages );
77 } elseif ( $request->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
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' ) && $wgExportAllowAll ) {
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 $page = $request->getText( 'pages' );
102 $this->curonly
= $request->getCheck( 'curonly' );
103 $rawOffset = $request->getVal( 'offset' );
106 $offset = wfTimestamp( TS_MW
, $rawOffset );
111 $limit = $request->getInt( 'limit' );
112 $dir = $request->getVal( 'dir' );
116 'limit' => $wgExportMaxHistory,
118 $historyCheck = $request->getCheck( 'history' );
120 if ( $this->curonly
) {
121 $history = WikiExporter
::CURRENT
;
122 } elseif ( !$historyCheck ) {
123 if ( $limit > 0 && ( $wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
124 $history['limit'] = $limit;
127 if ( !is_null( $offset ) ) {
128 $history['offset'] = $offset;
131 if ( strtolower( $dir ) == 'desc' ) {
132 $history['dir'] = 'desc';
137 $this->doExport
= true;
140 // Default to current-only for GET requests.
141 $page = $request->getText( 'pages', $par );
142 $historyCheck = $request->getCheck( 'history' );
144 if ( $historyCheck ) {
145 $history = WikiExporter
::FULL
;
147 $history = WikiExporter
::CURRENT
;
151 $this->doExport
= true;
155 if ( !$wgExportAllowHistory ) {
157 $history = WikiExporter
::CURRENT
;
160 $list_authors = $request->getCheck( 'listauthors' );
161 if ( !$this->curonly ||
!$wgExportAllowListContributors ) {
162 $list_authors = false;
165 if ( $this->doExport
) {
166 $this->getOutput()->disable();
168 // Cancel output buffering and gzipping if set
169 // This should provide safer streaming for pages with history
170 wfResetOutputBuffers();
171 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
173 if ( $request->getCheck( 'wpDownload' ) ) {
174 // Provide a sane filename suggestion
175 $filename = urlencode( $wgSitename . '-' . 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' );
187 $form = Xml
::openElement( 'form', array( 'method' => 'post',
188 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ) ) );
189 $form .= Xml
::inputLabel(
190 $this->msg( 'export-addcattext' )->text(),
195 $form .= Xml
::submitButton(
196 $this->msg( 'export-addcat' )->text(),
197 array( 'name' => 'addcat' )
200 if ( $wgExportFromNamespaces ) {
201 $form .= Html
::namespaceSelector(
203 'selected' => $nsindex,
204 'label' => $this->msg( 'export-addnstext' )->text()
208 'class' => 'namespaceselector',
211 $form .= Xml
::submitButton(
212 $this->msg( 'export-addns' )->text(),
213 array( 'name' => 'addns' )
217 if ( $wgExportAllowAll ) {
218 $form .= Xml
::checkLabel(
219 $this->msg( 'exportall' )->text(),
222 $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false
226 $form .= Xml
::element(
228 array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ),
234 if ( $wgExportAllowHistory ) {
235 $form .= Xml
::checkLabel(
236 $this->msg( 'exportcuronly' )->text(),
239 $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true
242 $out->addWikiMsg( 'exportnohistory' );
245 $form .= Xml
::checkLabel(
246 $this->msg( 'export-templates' )->text(),
249 $request->wasPosted() ?
$request->getCheck( 'templates' ) : false
252 if ( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
253 $form .= Xml
::inputLabel(
254 $this->msg( 'export-pagelinks' )->text(),
262 /* Enable this when we can do something useful exporting/importing image information.
263 $form .= Xml::checkLabel(
264 $this->msg( 'export-images' )->text(),
270 $form .= Xml
::checkLabel(
271 $this->msg( 'export-download' )->text(),
274 $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true
277 if ( $wgExportAllowListContributors ) {
278 $form .= Xml
::checkLabel(
279 $this->msg( 'exportlistauthors' )->text(),
282 $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false
286 $form .= Xml
::submitButton(
287 $this->msg( 'export-submit' )->text(),
288 Linker
::tooltipAndAccesskeyAttribs( 'export' )
290 $form .= Xml
::closeElement( 'form' );
292 $out->addHTML( $form );
298 private function userCanOverrideExportDepth() {
299 return $this->getUser()->isAllowed( 'override-export-depth' );
303 * Do the actual page exporting
305 * @param string $page user input on what page(s) to export
306 * @param int $history One of the WikiExporter history export constants
307 * @param bool $list_authors Whether to add distinct author list (when
308 * not returning full history)
309 * @param bool $exportall Whether to export everything
311 private function doExport( $page, $history, $list_authors, $exportall ) {
313 // If we are grabbing everything, enable full history and ignore the rest
315 $history = WikiExporter
::FULL
;
318 $pageSet = array(); // Inverted index of all pages to look up
320 // Split up and normalize input
321 foreach ( explode( "\n", $page ) as $pageName ) {
322 $pageName = trim( $pageName );
323 $title = Title
::newFromText( $pageName );
324 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
325 // Only record each page once!
326 $pageSet[$title->getPrefixedText()] = true;
330 // Set of original pages to pass on to further manipulation...
331 $inputPages = array_keys( $pageSet );
333 // Look up any linked pages if asked...
334 if ( $this->templates
) {
335 $pageSet = $this->getTemplates( $inputPages, $pageSet );
337 $linkDepth = $this->pageLinkDepth
;
339 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
342 // Enable this when we can do something useful exporting/importing image information.
343 // if( $this->images ) ) {
344 // $pageSet = $this->getImages( $inputPages, $pageSet );
347 $pages = array_keys( $pageSet );
349 // Normalize titles to the same format and remove dupes, see bug 17374
350 foreach ( $pages as $k => $v ) {
351 $pages[$k] = str_replace( " ", "_", $v );
354 $pages = array_unique( $pages );
357 /* Ok, let's get to it... */
358 if ( $history == WikiExporter
::CURRENT
) {
360 $db = wfGetDB( DB_SLAVE
);
361 $buffer = WikiExporter
::BUFFER
;
363 // Use an unbuffered query; histories may be very long!
364 $lb = wfGetLBFactory()->newMainLB();
365 $db = $lb->getConnection( DB_SLAVE
);
366 $buffer = WikiExporter
::STREAM
;
368 // This might take a while... :D
369 wfSuppressWarnings();
374 $exporter = new WikiExporter( $db, $history, $buffer );
375 $exporter->list_authors
= $list_authors;
376 $exporter->openStream();
379 $exporter->allPages();
381 foreach ( $pages as $page ) {
382 #Bug 8824: Only export pages the user can read
383 $title = Title
::newFromText( $page );
384 if ( is_null( $title ) ) {
385 // @todo Perhaps output an <error> tag or something.
389 if ( !$title->userCan( 'read', $this->getUser() ) ) {
390 // @todo Perhaps output an <error> tag or something.
394 $exporter->pageByTitle( $title );
398 $exporter->closeStream();
406 * @param Title $title
409 private function getPagesFromCategory( $title ) {
412 $name = $title->getDBkey();
414 $dbr = wfGetDB( DB_SLAVE
);
416 array( 'page', 'categorylinks' ),
417 array( 'page_namespace', 'page_title' ),
418 array( 'cl_from=page_id', 'cl_to' => $name ),
420 array( 'LIMIT' => '5000' )
425 foreach ( $res as $row ) {
426 $n = $row->page_title
;
427 if ( $row->page_namespace
) {
428 $ns = $wgContLang->getNsText( $row->page_namespace
);
439 * @param int $nsindex
442 private function getPagesFromNamespace( $nsindex ) {
445 $dbr = wfGetDB( DB_SLAVE
);
448 array( 'page_namespace', 'page_title' ),
449 array( 'page_namespace' => $nsindex ),
451 array( 'LIMIT' => '5000' )
456 foreach ( $res as $row ) {
457 $n = $row->page_title
;
459 if ( $row->page_namespace
) {
460 $ns = $wgContLang->getNsText( $row->page_namespace
);
471 * Expand a list of pages to include templates used in those pages.
472 * @param array $inputPages List of titles to look up
473 * @param array $pageSet Associative array indexed by titles for output
474 * @return array Associative array index by titles
476 private function getTemplates( $inputPages, $pageSet ) {
477 return $this->getLinks( $inputPages, $pageSet,
479 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
480 array( 'page_id=tl_from' )
485 * Validate link depth setting, if available.
489 private function validateLinkDepth( $depth ) {
490 global $wgExportMaxLinkDepth;
496 if ( !$this->userCanOverrideExportDepth() ) {
497 if ( $depth > $wgExportMaxLinkDepth ) {
498 return $wgExportMaxLinkDepth;
503 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
504 * crazy-big export from being done by someone setting the depth
505 * number too high. In other words, last resort safety net.
508 return intval( min( $depth, 5 ) );
512 * Expand a list of pages to include pages linked to from that page.
513 * @param array $inputPages
514 * @param array $pageSet
518 private function getPageLinks( $inputPages, $pageSet, $depth ) {
519 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
520 for ( ; $depth > 0; --$depth ) {
521 // @codingStandardsIgnoreEnd
522 $pageSet = $this->getLinks(
523 $inputPages, $pageSet, 'pagelinks',
524 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
525 array( 'page_id=pl_from' )
527 $inputPages = array_keys( $pageSet );
534 * Expand a list of pages to include images used in those pages.
536 * @param array $inputPages List of titles to look up
537 * @param array $pageSet Associative array indexed by titles for output
539 * @return array associative array index by titles
541 private function getImages( $inputPages, $pageSet ) {
542 return $this->getLinks(
546 array( 'namespace' => NS_FILE
, 'title' => 'il_to' ),
547 array( 'page_id=il_from' )
552 * Expand a list of pages to include items used in those pages.
553 * @param array $inputPages Array of page titles
554 * @param array $pageSet
555 * @param string $table
556 * @param array $fields Array of field names
560 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
561 $dbr = wfGetDB( DB_SLAVE
);
563 foreach ( $inputPages as $page ) {
564 $title = Title
::newFromText( $page );
567 $pageSet[$title->getPrefixedText()] = true;
568 /// @todo FIXME: May or may not be more efficient to batch these
569 /// by namespace when given multiple input pages.
570 $result = $dbr->select(
571 array( 'page', $table ),
576 'page_namespace' => $title->getNamespace(),
577 'page_title' => $title->getDBkey()
583 foreach ( $result as $row ) {
584 $template = Title
::makeTitle( $row->namespace, $row->title
);
585 $pageSet[$template->getPrefixedText()] = true;
593 protected function getGroupName() {