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
{
33 private $curonly, $doExport, $pageLinkDepth, $templates;
36 public function __construct() {
37 parent
::__construct( 'Export' );
40 public function execute( $par ) {
41 global $wgSitename, $wgExportAllowListContributors, $wgExportFromNamespaces;
42 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
43 global $wgExportAllowAll;
46 $this->outputHeader();
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 );
79 elseif( $request->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
80 $page = $request->getText( 'pages' );
81 $nsindex = $request->getText( 'nsindex', '' );
83 if ( strval( $nsindex ) !== '' ) {
85 * Same implementation as above, so same @todo
87 $nspages = $this->getPagesFromNamespace( $nsindex );
89 $page .= "\n" . implode( "\n", $nspages );
93 elseif( $request->getCheck( 'exportall' ) && $wgExportAllowAll ) {
94 $this->doExport
= true;
97 /* Although $page and $history are not used later on, we
98 nevertheless set them to avoid that PHP notices about using
99 undefined variables foul up our XML output (see call to
100 doExport(...) further down) */
104 elseif( $request->wasPosted() && $par == '' ) {
105 $page = $request->getText( 'pages' );
106 $this->curonly
= $request->getCheck( 'curonly' );
107 $rawOffset = $request->getVal( 'offset' );
110 $offset = wfTimestamp( TS_MW
, $rawOffset );
115 $limit = $request->getInt( 'limit' );
116 $dir = $request->getVal( 'dir' );
120 'limit' => $wgExportMaxHistory,
122 $historyCheck = $request->getCheck( 'history' );
124 if ( $this->curonly
) {
125 $history = WikiExporter
::CURRENT
;
126 } elseif ( !$historyCheck ) {
127 if ( $limit > 0 && ($wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
128 $history['limit'] = $limit;
130 if ( !is_null( $offset ) ) {
131 $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( !$wgExportAllowHistory ) {
159 $history = WikiExporter
::CURRENT
;
162 $list_authors = $request->getCheck( 'listauthors' );
163 if ( !$this->curonly ||
!$wgExportAllowListContributors ) {
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( $wgSitename . '-' . 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->getTitle()->getLocalUrl( 'action=submit' ) ) );
191 $form .= Xml
::inputLabel( $this->msg( 'export-addcattext' )->text(), 'catname', 'catname', 40 ) . ' ';
192 $form .= Xml
::submitButton( $this->msg( 'export-addcat' )->text(), array( 'name' => 'addcat' ) ) . '<br />';
194 if ( $wgExportFromNamespaces ) {
195 $form .= Html
::namespaceSelector(
197 'selected' => $nsindex,
198 'label' => $this->msg( 'export-addnstext' )->text()
202 'class' => 'namespaceselector',
205 $form .= Xml
::submitButton( $this->msg( 'export-addns' )->text(), array( 'name' => 'addns' ) ) . '<br />';
208 if ( $wgExportAllowAll ) {
209 $form .= Xml
::checkLabel(
210 $this->msg( 'exportall' )->text(),
213 $request->wasPosted() ?
$request->getCheck( 'exportall' ) : false
217 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
220 if( $wgExportAllowHistory ) {
221 $form .= Xml
::checkLabel(
222 $this->msg( 'exportcuronly' )->text(),
225 $request->wasPosted() ?
$request->getCheck( 'curonly' ) : true
228 $out->addWikiMsg( 'exportnohistory' );
231 $form .= Xml
::checkLabel(
232 $this->msg( 'export-templates' )->text(),
235 $request->wasPosted() ?
$request->getCheck( 'templates' ) : false
238 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
239 $form .= Xml
::inputLabel( $this->msg( 'export-pagelinks' )->text(), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
241 // Enable this when we can do something useful exporting/importing image information. :)
242 //$form .= Xml::checkLabel( $this->msg( 'export-images' )->text(), 'images', 'wpExportImages', false ) . '<br />';
243 $form .= Xml
::checkLabel(
244 $this->msg( 'export-download' )->text(),
247 $request->wasPosted() ?
$request->getCheck( 'wpDownload' ) : true
250 if ( $wgExportAllowListContributors ) {
251 $form .= Xml
::checkLabel(
252 $this->msg( 'exportlistauthors' )->text(),
255 $request->wasPosted() ?
$request->getCheck( 'listauthors' ) : false
259 $form .= Xml
::submitButton( $this->msg( 'export-submit' )->text(), Linker
::tooltipAndAccesskeyAttribs( 'export' ) );
260 $form .= Xml
::closeElement( 'form' );
262 $out->addHTML( $form );
268 private function userCanOverrideExportDepth() {
269 return $this->getUser()->isAllowed( 'override-export-depth' );
273 * Do the actual page exporting
275 * @param $page String: user input on what page(s) to export
276 * @param $history Mixed: one of the WikiExporter history export constants
277 * @param $list_authors Boolean: Whether to add distinct author list (when
278 * not returning full history)
279 * @param $exportall Boolean: Whether to export everything
281 private function doExport( $page, $history, $list_authors, $exportall ) {
283 // If we are grabbing everything, enable full history and ignore the rest
285 $history = WikiExporter
::FULL
;
288 $pageSet = array(); // Inverted index of all pages to look up
290 // Split up and normalize input
291 foreach( explode( "\n", $page ) as $pageName ) {
292 $pageName = trim( $pageName );
293 $title = Title
::newFromText( $pageName );
294 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
295 // Only record each page once!
296 $pageSet[$title->getPrefixedText()] = true;
300 // Set of original pages to pass on to further manipulation...
301 $inputPages = array_keys( $pageSet );
303 // Look up any linked pages if asked...
304 if( $this->templates
) {
305 $pageSet = $this->getTemplates( $inputPages, $pageSet );
307 $linkDepth = $this->pageLinkDepth
;
309 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
313 // Enable this when we can do something useful exporting/importing image information. :)
314 if( $this->images ) ) {
315 $pageSet = $this->getImages( $inputPages, $pageSet );
319 $pages = array_keys( $pageSet );
321 // Normalize titles to the same format and remove dupes, see bug 17374
322 foreach( $pages as $k => $v ) {
323 $pages[$k] = str_replace( " ", "_", $v );
326 $pages = array_unique( $pages );
329 /* Ok, let's get to it... */
330 if( $history == WikiExporter
::CURRENT
) {
332 $db = wfGetDB( DB_SLAVE
);
333 $buffer = WikiExporter
::BUFFER
;
335 // Use an unbuffered query; histories may be very long!
336 $lb = wfGetLBFactory()->newMainLB();
337 $db = $lb->getConnection( DB_SLAVE
);
338 $buffer = WikiExporter
::STREAM
;
340 // This might take a while... :D
341 wfSuppressWarnings();
346 $exporter = new WikiExporter( $db, $history, $buffer );
347 $exporter->list_authors
= $list_authors;
348 $exporter->openStream();
351 $exporter->allPages();
353 foreach( $pages as $page ) {
355 if( $wgExportMaxHistory && !$this->curonly ) {
356 $title = Title::newFromText( $page );
358 $count = Revision::countByTitle( $db, $title );
359 if( $count > $wgExportMaxHistory ) {
360 wfDebug( __FUNCTION__ .
361 ": Skipped $page, $count revisions too big\n" );
366 #Bug 8824: Only export pages the user can read
367 $title = Title
::newFromText( $page );
368 if( is_null( $title ) ) {
369 continue; #TODO: perhaps output an <error> tag or something.
371 if( !$title->userCan( 'read', $this->getUser() ) ) {
372 continue; #TODO: perhaps output an <error> tag or something.
375 $exporter->pageByTitle( $title );
379 $exporter->closeStream();
387 * @param $title Title
390 private function getPagesFromCategory( $title ) {
393 $name = $title->getDBkey();
395 $dbr = wfGetDB( DB_SLAVE
);
397 array( 'page', 'categorylinks' ),
398 array( 'page_namespace', 'page_title' ),
399 array( 'cl_from=page_id', 'cl_to' => $name ),
401 array( 'LIMIT' => '5000' )
406 foreach ( $res as $row ) {
407 $n = $row->page_title
;
408 if ($row->page_namespace
) {
409 $ns = $wgContLang->getNsText( $row->page_namespace
);
419 * @param $nsindex int
422 private function getPagesFromNamespace( $nsindex ) {
425 $dbr = wfGetDB( DB_SLAVE
);
428 array( 'page_namespace', 'page_title' ),
429 array( 'page_namespace' => $nsindex ),
431 array( 'LIMIT' => '5000' )
436 foreach ( $res as $row ) {
437 $n = $row->page_title
;
439 if ( $row->page_namespace
) {
440 $ns = $wgContLang->getNsText( $row->page_namespace
);
450 * Expand a list of pages to include templates used in those pages.
451 * @param $inputPages array, list of titles to look up
452 * @param $pageSet array, associative array indexed by titles for output
453 * @return array associative array index by titles
455 private function getTemplates( $inputPages, $pageSet ) {
456 return $this->getLinks( $inputPages, $pageSet,
458 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
459 array( 'page_id=tl_from' )
464 * Validate link depth setting, if available.
468 private function validateLinkDepth( $depth ) {
469 global $wgExportMaxLinkDepth;
475 if ( !$this->userCanOverrideExportDepth() ) {
476 if( $depth > $wgExportMaxLinkDepth ) {
477 return $wgExportMaxLinkDepth;
482 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
483 * crazy-big export from being done by someone setting the depth
484 * number too high. In other words, last resort safety net.
486 return intval( min( $depth, 5 ) );
490 * Expand a list of pages to include pages linked to from that page.
491 * @param $inputPages array
492 * @param $pageSet array
496 private function getPageLinks( $inputPages, $pageSet, $depth ) {
497 for( ; $depth > 0; --$depth ) {
498 $pageSet = $this->getLinks(
499 $inputPages, $pageSet, 'pagelinks',
500 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
501 array( 'page_id=pl_from' )
503 $inputPages = array_keys( $pageSet );
510 * Expand a list of pages to include images used in those pages.
512 * @param $inputPages array, list of titles to look up
513 * @param $pageSet array, associative array indexed by titles for output
515 * @return array associative array index by titles
517 private function getImages( $inputPages, $pageSet ) {
518 return $this->getLinks(
522 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
523 array( 'page_id=il_from' )
528 * Expand a list of pages to include items used in those pages.
531 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
532 $dbr = wfGetDB( DB_SLAVE
);
534 foreach( $inputPages as $page ) {
535 $title = Title
::newFromText( $page );
538 $pageSet[$title->getPrefixedText()] = true;
539 /// @todo FIXME: May or may not be more efficient to batch these
540 /// by namespace when given multiple input pages.
541 $result = $dbr->select(
542 array( 'page', $table ),
547 'page_namespace' => $title->getNamespace(),
548 'page_title' => $title->getDBkey()
554 foreach( $result as $row ) {
555 $template = Title
::makeTitle( $row->namespace, $row->title
);
556 $pageSet[$template->getPrefixedText()] = true;