3 * Copyright (C) 2003-2008 Brion Vibber <brion@pobox.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
26 class SpecialExport
extends SpecialPage
{
28 private $curonly, $doExport, $pageLinkDepth, $templates;
31 public function __construct() {
32 parent
::__construct( 'Export' );
35 public function execute( $par ) {
36 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
37 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
38 global $wgExportFromNamespaces, $wgUser;
41 $this->outputHeader();
44 $this->curonly
= true;
45 $this->doExport
= false;
46 $this->templates
= $wgRequest->getCheck( 'templates' );
47 $this->images
= $wgRequest->getCheck( 'images' ); // Doesn't do anything yet
48 $this->pageLinkDepth
= $this->validateLinkDepth(
49 $wgRequest->getIntOrNull( 'pagelink-depth' ) );
52 if ( $wgRequest->getCheck( 'addcat' ) ) {
53 $page = $wgRequest->getText( 'pages' );
54 $catname = $wgRequest->getText( 'catname' );
56 if ( $catname !== '' && $catname !== null && $catname !== false ) {
57 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
60 * @todo Fixme: this can lead to hitting memory limit for very large
61 * categories. Ideally we would do the lookup synchronously
62 * during the export in a single query.
64 $catpages = $this->getPagesFromCategory( $t );
65 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
69 else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
70 $page = $wgRequest->getText( 'pages' );
71 $nsindex = $wgRequest->getText( 'nsindex', '' );
73 if ( strval( $nsindex ) !== '' ) {
75 * Same implementation as above, so same @todo
77 $nspages = $this->getPagesFromNamespace( $nsindex );
78 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
81 else if( $wgRequest->wasPosted() && $par == '' ) {
82 $page = $wgRequest->getText( 'pages' );
83 $this->curonly
= $wgRequest->getCheck( 'curonly' );
84 $rawOffset = $wgRequest->getVal( 'offset' );
86 $offset = wfTimestamp( TS_MW
, $rawOffset );
90 $limit = $wgRequest->getInt( 'limit' );
91 $dir = $wgRequest->getVal( 'dir' );
95 'limit' => $wgExportMaxHistory,
97 $historyCheck = $wgRequest->getCheck( 'history' );
98 if ( $this->curonly
) {
99 $history = WikiExporter
::CURRENT
;
100 } elseif ( !$historyCheck ) {
101 if ( $limit > 0 && ($wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
102 $history['limit'] = $limit;
104 if ( !is_null( $offset ) ) {
105 $history['offset'] = $offset;
107 if ( strtolower( $dir ) == 'desc' ) {
108 $history['dir'] = 'desc';
112 if( $page != '' ) $this->doExport
= true;
114 // Default to current-only for GET requests
115 $page = $wgRequest->getText( 'pages', $par );
116 $historyCheck = $wgRequest->getCheck( 'history' );
117 if( $historyCheck ) {
118 $history = WikiExporter
::FULL
;
120 $history = WikiExporter
::CURRENT
;
123 if( $page != '' ) $this->doExport
= true;
126 if( !$wgExportAllowHistory ) {
128 $history = WikiExporter
::CURRENT
;
131 $list_authors = $wgRequest->getCheck( 'listauthors' );
132 if ( !$this->curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
134 if ( $this->doExport
) {
136 // Cancel output buffering and gzipping if set
137 // This should provide safer streaming for pages with history
138 wfResetOutputBuffers();
139 $wgRequest->response()->header( "Content-type: application/xml; charset=utf-8" );
140 if( $wgRequest->getCheck( 'wpDownload' ) ) {
141 // Provide a sane filename suggestion
142 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
143 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
145 $this->doExport( $page, $history, $list_authors );
149 $wgOut->addWikiMsg( 'exporttext' );
151 $form = Xml
::openElement( 'form', array( 'method' => 'post',
152 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
153 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
154 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
156 if ( $wgExportFromNamespaces ) {
157 $form .= Xml
::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
158 $form .= Xml
::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
161 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
164 if( $wgExportAllowHistory ) {
165 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
167 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
169 $form .= Xml
::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
170 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
171 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
173 // Enable this when we can do something useful exporting/importing image information. :)
174 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
175 $form .= Xml
::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
177 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'export' ) );
178 $form .= Xml
::closeElement( 'form' );
179 $wgOut->addHTML( $form );
182 private function userCanOverrideExportDepth() {
185 return $wgUser->isAllowed( 'override-export-depth' );
189 * Do the actual page exporting
191 * @param $page String: user input on what page(s) to export
192 * @param $history Mixed: one of the WikiExporter history export constants
193 * @param $list_authors Boolean: Whether to add distinct author list (when
194 * not returning full history)
196 private function doExport( $page, $history, $list_authors ) {
197 $pageSet = array(); // Inverted index of all pages to look up
199 // Split up and normalize input
200 foreach( explode( "\n", $page ) as $pageName ) {
201 $pageName = trim( $pageName );
202 $title = Title
::newFromText( $pageName );
203 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
204 // Only record each page once!
205 $pageSet[$title->getPrefixedText()] = true;
209 // Set of original pages to pass on to further manipulation...
210 $inputPages = array_keys( $pageSet );
212 // Look up any linked pages if asked...
213 if( $this->templates
) {
214 $pageSet = $this->getTemplates( $inputPages, $pageSet );
217 if( $linkDepth = $this->pageLinkDepth
) {
218 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
222 // Enable this when we can do something useful exporting/importing image information. :)
223 if( $this->images ) ) {
224 $pageSet = $this->getImages( $inputPages, $pageSet );
228 $pages = array_keys( $pageSet );
230 // Normalize titles to the same format and remove dupes, see bug 17374
231 foreach( $pages as $k => $v ) {
232 $pages[$k] = str_replace( " ", "_", $v );
234 $pages = array_unique( $pages );
236 /* Ok, let's get to it... */
237 if( $history == WikiExporter
::CURRENT
) {
239 $db = wfGetDB( DB_SLAVE
);
240 $buffer = WikiExporter
::BUFFER
;
242 // Use an unbuffered query; histories may be very long!
243 $lb = wfGetLBFactory()->newMainLB();
244 $db = $lb->getConnection( DB_SLAVE
);
245 $buffer = WikiExporter
::STREAM
;
247 // This might take a while... :D
248 wfSuppressWarnings();
252 $exporter = new WikiExporter( $db, $history, $buffer );
253 $exporter->list_authors
= $list_authors;
254 $exporter->openStream();
255 foreach( $pages as $page ) {
257 if( $wgExportMaxHistory && !$this->curonly ) {
258 $title = Title::newFromText( $page );
260 $count = Revision::countByTitle( $db, $title );
261 if( $count > $wgExportMaxHistory ) {
262 wfDebug( __FUNCTION__ .
263 ": Skipped $page, $count revisions too big\n" );
268 #Bug 8824: Only export pages the user can read
269 $title = Title
::newFromText( $page );
270 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
271 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
273 $exporter->pageByTitle( $title );
276 $exporter->closeStream();
282 private function getPagesFromCategory( $title ) {
285 $name = $title->getDBkey();
287 $dbr = wfGetDB( DB_SLAVE
);
288 $res = $dbr->select( array('page', 'categorylinks' ),
289 array( 'page_namespace', 'page_title' ),
290 array('cl_from=page_id', 'cl_to' => $name ),
291 __METHOD__
, array('LIMIT' => '5000'));
294 while ( $row = $dbr->fetchObject( $res ) ) {
295 $n = $row->page_title
;
296 if ($row->page_namespace
) {
297 $ns = $wgContLang->getNsText( $row->page_namespace
);
303 $dbr->freeResult($res);
308 private function getPagesFromNamespace( $nsindex ) {
311 $dbr = wfGetDB( DB_SLAVE
);
312 $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
313 array('page_namespace' => $nsindex),
314 __METHOD__
, array('LIMIT' => '5000') );
317 while ( $row = $dbr->fetchObject( $res ) ) {
318 $n = $row->page_title
;
319 if ($row->page_namespace
) {
320 $ns = $wgContLang->getNsText( $row->page_namespace
);
326 $dbr->freeResult($res);
332 * Expand a list of pages to include templates used in those pages.
333 * @param $inputPages array, list of titles to look up
334 * @param $pageSet array, associative array indexed by titles for output
335 * @return array associative array index by titles
337 private function getTemplates( $inputPages, $pageSet ) {
338 return $this->getLinks( $inputPages, $pageSet,
340 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
341 array( 'page_id=tl_from' ) );
345 * Validate link depth setting, if available.
347 private function validateLinkDepth( $depth ) {
348 global $wgExportMaxLinkDepth;
352 if ( !$this->userCanOverrideExportDepth() ) {
353 if( $depth > $wgExportMaxLinkDepth ) {
354 return $wgExportMaxLinkDepth;
358 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
359 * crazy-big export from being done by someone setting the depth
360 * number too high. In other words, last resort safety net.
362 return intval( min( $depth, 5 ) );
365 /** Expand a list of pages to include pages linked to from that page. */
366 private function getPageLinks( $inputPages, $pageSet, $depth ) {
367 for( $depth=$depth; $depth>0; --$depth ) {
368 $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
369 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
370 array( 'page_id=pl_from' ) );
371 $inputPages = array_keys( $pageSet );
377 * Expand a list of pages to include images used in those pages.
378 * @param $inputPages array, list of titles to look up
379 * @param $pageSet array, associative array indexed by titles for output
380 * @return array associative array index by titles
382 private function getImages( $inputPages, $pageSet ) {
383 return $this->getLinks( $inputPages, $pageSet,
385 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
386 array( 'page_id=il_from' ) );
390 * Expand a list of pages to include items used in those pages.
393 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
394 $dbr = wfGetDB( DB_SLAVE
);
395 foreach( $inputPages as $page ) {
396 $title = Title
::newFromText( $page );
398 $pageSet[$title->getPrefixedText()] = true;
399 /// @todo Fixme: May or may not be more efficient to batch these
400 /// by namespace when given multiple input pages.
401 $result = $dbr->select(
402 array( 'page', $table ),
406 'page_namespace' => $title->getNamespace(),
407 'page_title' => $title->getDBkey() ) ),
409 foreach( $result as $row ) {
410 $template = Title
::makeTitle( $row->namespace, $row->title
);
411 $pageSet[$template->getPrefixedText()] = true;