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 $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
42 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
43 global $wgExportFromNamespaces, $wgUser;
46 $this->outputHeader();
49 $this->curonly
= true;
50 $this->doExport
= false;
51 $this->templates
= $wgRequest->getCheck( 'templates' );
52 $this->images
= $wgRequest->getCheck( 'images' ); // Doesn't do anything yet
53 $this->pageLinkDepth
= $this->validateLinkDepth(
54 $wgRequest->getIntOrNull( 'pagelink-depth' )
58 if ( $wgRequest->getCheck( 'addcat' ) ) {
59 $page = $wgRequest->getText( 'pages' );
60 $catname = $wgRequest->getText( 'catname' );
62 if ( $catname !== '' && $catname !== null && $catname !== false ) {
63 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
66 * @todo FIXME: This can lead to hitting memory limit for very large
67 * categories. Ideally we would do the lookup synchronously
68 * during the export in a single query.
70 $catpages = $this->getPagesFromCategory( $t );
72 $page .= "\n" . implode( "\n", $catpages );
77 elseif( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
78 $page = $wgRequest->getText( 'pages' );
79 $nsindex = $wgRequest->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 );
91 elseif( $wgRequest->wasPosted() && $par == '' ) {
92 $page = $wgRequest->getText( 'pages' );
93 $this->curonly
= $wgRequest->getCheck( 'curonly' );
94 $rawOffset = $wgRequest->getVal( 'offset' );
97 $offset = wfTimestamp( TS_MW
, $rawOffset );
102 $limit = $wgRequest->getInt( 'limit' );
103 $dir = $wgRequest->getVal( 'dir' );
107 'limit' => $wgExportMaxHistory,
109 $historyCheck = $wgRequest->getCheck( 'history' );
111 if ( $this->curonly
) {
112 $history = WikiExporter
::CURRENT
;
113 } elseif ( !$historyCheck ) {
114 if ( $limit > 0 && ($wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
115 $history['limit'] = $limit;
117 if ( !is_null( $offset ) ) {
118 $history['offset'] = $offset;
120 if ( strtolower( $dir ) == 'desc' ) {
121 $history['dir'] = 'desc';
126 $this->doExport
= true;
129 // Default to current-only for GET requests.
130 $page = $wgRequest->getText( 'pages', $par );
131 $historyCheck = $wgRequest->getCheck( 'history' );
133 if( $historyCheck ) {
134 $history = WikiExporter
::FULL
;
136 $history = WikiExporter
::CURRENT
;
140 $this->doExport
= true;
144 if( !$wgExportAllowHistory ) {
146 $history = WikiExporter
::CURRENT
;
149 $list_authors = $wgRequest->getCheck( 'listauthors' );
150 if ( !$this->curonly ||
!$wgExportAllowListContributors ) {
151 $list_authors = false ;
154 if ( $this->doExport
) {
157 // Cancel output buffering and gzipping if set
158 // This should provide safer streaming for pages with history
159 wfResetOutputBuffers();
160 $wgRequest->response()->header( "Content-type: application/xml; charset=utf-8" );
162 if( $wgRequest->getCheck( 'wpDownload' ) ) {
163 // Provide a sane filename suggestion
164 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
165 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
168 $this->doExport( $page, $history, $list_authors );
173 $wgOut->addWikiMsg( 'exporttext' );
175 $form = Xml
::openElement( 'form', array( 'method' => 'post',
176 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
177 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
178 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
180 if ( $wgExportFromNamespaces ) {
181 $form .= Xml
::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
182 $form .= Xml
::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
185 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
188 if( $wgExportAllowHistory ) {
189 $form .= Xml
::checkLabel(
190 wfMsg( 'exportcuronly' ),
193 $wgRequest->wasPosted() ?
$wgRequest->getCheck( 'curonly' ) : true
196 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
199 $form .= Xml
::checkLabel(
200 wfMsg( 'export-templates' ),
203 $wgRequest->wasPosted() ?
$wgRequest->getCheck( 'templates' ) : false
206 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
207 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
209 // Enable this when we can do something useful exporting/importing image information. :)
210 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
211 $form .= Xml
::checkLabel(
212 wfMsg( 'export-download' ),
215 $wgRequest->wasPosted() ?
$wgRequest->getCheck( 'wpDownload' ) : true
218 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'export' ) );
219 $form .= Xml
::closeElement( 'form' );
221 $wgOut->addHTML( $form );
224 private function userCanOverrideExportDepth() {
226 return $wgUser->isAllowed( 'override-export-depth' );
230 * Do the actual page exporting
232 * @param $page String: user input on what page(s) to export
233 * @param $history Mixed: one of the WikiExporter history export constants
234 * @param $list_authors Boolean: Whether to add distinct author list (when
235 * not returning full history)
237 private function doExport( $page, $history, $list_authors ) {
238 $pageSet = array(); // Inverted index of all pages to look up
240 // Split up and normalize input
241 foreach( explode( "\n", $page ) as $pageName ) {
242 $pageName = trim( $pageName );
243 $title = Title
::newFromText( $pageName );
244 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
245 // Only record each page once!
246 $pageSet[$title->getPrefixedText()] = true;
250 // Set of original pages to pass on to further manipulation...
251 $inputPages = array_keys( $pageSet );
253 // Look up any linked pages if asked...
254 if( $this->templates
) {
255 $pageSet = $this->getTemplates( $inputPages, $pageSet );
257 $linkDepth = $this->pageLinkDepth
;
259 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
263 // Enable this when we can do something useful exporting/importing image information. :)
264 if( $this->images ) ) {
265 $pageSet = $this->getImages( $inputPages, $pageSet );
269 $pages = array_keys( $pageSet );
271 // Normalize titles to the same format and remove dupes, see bug 17374
272 foreach( $pages as $k => $v ) {
273 $pages[$k] = str_replace( " ", "_", $v );
276 $pages = array_unique( $pages );
278 /* Ok, let's get to it... */
279 if( $history == WikiExporter
::CURRENT
) {
281 $db = wfGetDB( DB_SLAVE
);
282 $buffer = WikiExporter
::BUFFER
;
284 // Use an unbuffered query; histories may be very long!
285 $lb = wfGetLBFactory()->newMainLB();
286 $db = $lb->getConnection( DB_SLAVE
);
287 $buffer = WikiExporter
::STREAM
;
289 // This might take a while... :D
290 wfSuppressWarnings();
295 $exporter = new WikiExporter( $db, $history, $buffer );
296 $exporter->list_authors
= $list_authors;
297 $exporter->openStream();
299 foreach( $pages as $page ) {
301 if( $wgExportMaxHistory && !$this->curonly ) {
302 $title = Title::newFromText( $page );
304 $count = Revision::countByTitle( $db, $title );
305 if( $count > $wgExportMaxHistory ) {
306 wfDebug( __FUNCTION__ .
307 ": Skipped $page, $count revisions too big\n" );
312 #Bug 8824: Only export pages the user can read
313 $title = Title
::newFromText( $page );
314 if( is_null( $title ) ) {
315 continue; #TODO: perhaps output an <error> tag or something.
317 if( !$title->userCanRead() ) {
318 continue; #TODO: perhaps output an <error> tag or something.
321 $exporter->pageByTitle( $title );
324 $exporter->closeStream();
331 private function getPagesFromCategory( $title ) {
334 $name = $title->getDBkey();
336 $dbr = wfGetDB( DB_SLAVE
);
338 array( 'page', 'categorylinks' ),
339 array( 'page_namespace', 'page_title' ),
340 array( 'cl_from=page_id', 'cl_to' => $name ),
342 array( 'LIMIT' => '5000' )
347 foreach ( $res as $row ) {
348 $n = $row->page_title
;
349 if ($row->page_namespace
) {
350 $ns = $wgContLang->getNsText( $row->page_namespace
);
359 private function getPagesFromNamespace( $nsindex ) {
362 $dbr = wfGetDB( DB_SLAVE
);
365 array( 'page_namespace', 'page_title' ),
366 array( 'page_namespace' => $nsindex ),
368 array( 'LIMIT' => '5000' )
373 foreach ( $res as $row ) {
374 $n = $row->page_title
;
376 if ( $row->page_namespace
) {
377 $ns = $wgContLang->getNsText( $row->page_namespace
);
387 * Expand a list of pages to include templates used in those pages.
388 * @param $inputPages array, list of titles to look up
389 * @param $pageSet array, associative array indexed by titles for output
390 * @return array associative array index by titles
392 private function getTemplates( $inputPages, $pageSet ) {
393 return $this->getLinks( $inputPages, $pageSet,
395 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
396 array( 'page_id=tl_from' )
401 * Validate link depth setting, if available.
403 private function validateLinkDepth( $depth ) {
404 global $wgExportMaxLinkDepth;
410 if ( !$this->userCanOverrideExportDepth() ) {
411 if( $depth > $wgExportMaxLinkDepth ) {
412 return $wgExportMaxLinkDepth;
417 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
418 * crazy-big export from being done by someone setting the depth
419 * number too high. In other words, last resort safety net.
421 return intval( min( $depth, 5 ) );
424 /** Expand a list of pages to include pages linked to from that page. */
425 private function getPageLinks( $inputPages, $pageSet, $depth ) {
426 for( ; $depth > 0; --$depth ) {
427 $pageSet = $this->getLinks(
428 $inputPages, $pageSet, 'pagelinks',
429 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
430 array( 'page_id=pl_from' )
432 $inputPages = array_keys( $pageSet );
439 * Expand a list of pages to include images used in those pages.
441 * @param $inputPages array, list of titles to look up
442 * @param $pageSet array, associative array indexed by titles for output
444 * @return array associative array index by titles
446 private function getImages( $inputPages, $pageSet ) {
447 return $this->getLinks(
451 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
452 array( 'page_id=il_from' )
457 * Expand a list of pages to include items used in those pages.
459 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
460 $dbr = wfGetDB( DB_SLAVE
);
462 foreach( $inputPages as $page ) {
463 $title = Title
::newFromText( $page );
466 $pageSet[$title->getPrefixedText()] = true;
467 /// @todo FIXME: May or may not be more efficient to batch these
468 /// by namespace when given multiple input pages.
469 $result = $dbr->select(
470 array( 'page', $table ),
475 'page_namespace' => $title->getNamespace(),
476 'page_title' => $title->getDBkey()
482 foreach( $result as $row ) {
483 $template = Title
::makeTitle( $row->namespace, $row->title
);
484 $pageSet[$template->getPrefixedText()] = true;