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' )
53 if ( $wgRequest->getCheck( 'addcat' ) ) {
54 $page = $wgRequest->getText( 'pages' );
55 $catname = $wgRequest->getText( 'catname' );
57 if ( $catname !== '' && $catname !== null && $catname !== false ) {
58 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
61 * @todo Fixme: this can lead to hitting memory limit for very large
62 * categories. Ideally we would do the lookup synchronously
63 * during the export in a single query.
65 $catpages = $this->getPagesFromCategory( $t );
66 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
70 else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
71 $page = $wgRequest->getText( 'pages' );
72 $nsindex = $wgRequest->getText( 'nsindex', '' );
74 if ( strval( $nsindex ) !== '' ) {
76 * Same implementation as above, so same @todo
78 $nspages = $this->getPagesFromNamespace( $nsindex );
79 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
82 else if( $wgRequest->wasPosted() && $par == '' ) {
83 $page = $wgRequest->getText( 'pages' );
84 $this->curonly
= $wgRequest->getCheck( 'curonly' );
85 $rawOffset = $wgRequest->getVal( 'offset' );
88 $offset = wfTimestamp( TS_MW
, $rawOffset );
93 $limit = $wgRequest->getInt( 'limit' );
94 $dir = $wgRequest->getVal( 'dir' );
98 'limit' => $wgExportMaxHistory,
100 $historyCheck = $wgRequest->getCheck( 'history' );
102 if ( $this->curonly
) {
103 $history = WikiExporter
::CURRENT
;
104 } elseif ( !$historyCheck ) {
105 if ( $limit > 0 && ($wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
106 $history['limit'] = $limit;
108 if ( !is_null( $offset ) ) {
109 $history['offset'] = $offset;
111 if ( strtolower( $dir ) == 'desc' ) {
112 $history['dir'] = 'desc';
116 if( $page != '' ) $this->doExport
= true;
118 // Default to current-only for GET requests.
119 $page = $wgRequest->getText( 'pages', $par );
120 $historyCheck = $wgRequest->getCheck( 'history' );
122 if( $historyCheck ) {
123 $history = WikiExporter
::FULL
;
125 $history = WikiExporter
::CURRENT
;
128 if( $page != '' ) $this->doExport
= true;
131 if( !$wgExportAllowHistory ) {
133 $history = WikiExporter
::CURRENT
;
136 $list_authors = $wgRequest->getCheck( 'listauthors' );
137 if ( !$this->curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
139 if ( $this->doExport
) {
142 // Cancel output buffering and gzipping if set
143 // This should provide safer streaming for pages with history
144 wfResetOutputBuffers();
145 $wgRequest->response()->header( "Content-type: application/xml; charset=utf-8" );
147 if( $wgRequest->getCheck( 'wpDownload' ) ) {
148 // Provide a sane filename suggestion
149 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
150 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
153 $this->doExport( $page, $history, $list_authors );
158 $wgOut->addWikiMsg( 'exporttext' );
160 $form = Xml
::openElement( 'form', array( 'method' => 'post',
161 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
162 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
163 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
165 if ( $wgExportFromNamespaces ) {
166 $form .= Xml
::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
167 $form .= Xml
::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
170 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
173 if( $wgExportAllowHistory ) {
174 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
176 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
179 $form .= Xml
::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
181 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
182 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
184 // Enable this when we can do something useful exporting/importing image information. :)
185 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
186 $form .= Xml
::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
188 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'export' ) );
189 $form .= Xml
::closeElement( 'form' );
191 $wgOut->addHTML( $form );
194 private function userCanOverrideExportDepth() {
196 return $wgUser->isAllowed( 'override-export-depth' );
200 * Do the actual page exporting
202 * @param $page String: user input on what page(s) to export
203 * @param $history Mixed: one of the WikiExporter history export constants
204 * @param $list_authors Boolean: Whether to add distinct author list (when
205 * not returning full history)
207 private function doExport( $page, $history, $list_authors ) {
208 $pageSet = array(); // Inverted index of all pages to look up
210 // Split up and normalize input
211 foreach( explode( "\n", $page ) as $pageName ) {
212 $pageName = trim( $pageName );
213 $title = Title
::newFromText( $pageName );
214 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
215 // Only record each page once!
216 $pageSet[$title->getPrefixedText()] = true;
220 // Set of original pages to pass on to further manipulation...
221 $inputPages = array_keys( $pageSet );
223 // Look up any linked pages if asked...
224 if( $this->templates
) {
225 $pageSet = $this->getTemplates( $inputPages, $pageSet );
228 if( $linkDepth = $this->pageLinkDepth
) {
229 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
233 // Enable this when we can do something useful exporting/importing image information. :)
234 if( $this->images ) ) {
235 $pageSet = $this->getImages( $inputPages, $pageSet );
239 $pages = array_keys( $pageSet );
241 // Normalize titles to the same format and remove dupes, see bug 17374
242 foreach( $pages as $k => $v ) {
243 $pages[$k] = str_replace( " ", "_", $v );
246 $pages = array_unique( $pages );
248 /* Ok, let's get to it... */
249 if( $history == WikiExporter
::CURRENT
) {
251 $db = wfGetDB( DB_SLAVE
);
252 $buffer = WikiExporter
::BUFFER
;
254 // Use an unbuffered query; histories may be very long!
255 $lb = wfGetLBFactory()->newMainLB();
256 $db = $lb->getConnection( DB_SLAVE
);
257 $buffer = WikiExporter
::STREAM
;
259 // This might take a while... :D
260 wfSuppressWarnings();
265 $exporter = new WikiExporter( $db, $history, $buffer );
266 $exporter->list_authors
= $list_authors;
267 $exporter->openStream();
269 foreach( $pages as $page ) {
271 if( $wgExportMaxHistory && !$this->curonly ) {
272 $title = Title::newFromText( $page );
274 $count = Revision::countByTitle( $db, $title );
275 if( $count > $wgExportMaxHistory ) {
276 wfDebug( __FUNCTION__ .
277 ": Skipped $page, $count revisions too big\n" );
282 #Bug 8824: Only export pages the user can read
283 $title = Title
::newFromText( $page );
284 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
285 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
287 $exporter->pageByTitle( $title );
290 $exporter->closeStream();
297 private function getPagesFromCategory( $title ) {
300 $name = $title->getDBkey();
302 $dbr = wfGetDB( DB_SLAVE
);
304 array( 'page', 'categorylinks' ),
305 array( 'page_namespace', 'page_title' ),
306 array( 'cl_from=page_id', 'cl_to' => $name ),
308 array( 'LIMIT' => '5000' )
313 while ( $row = $dbr->fetchObject( $res ) ) {
314 $n = $row->page_title
;
315 if ($row->page_namespace
) {
316 $ns = $wgContLang->getNsText( $row->page_namespace
);
323 $dbr->freeResult($res);
328 private function getPagesFromNamespace( $nsindex ) {
331 $dbr = wfGetDB( DB_SLAVE
);
334 array( 'page_namespace', 'page_title' ),
335 array( 'page_namespace' => $nsindex ),
337 array( 'LIMIT' => '5000' )
342 while ( $row = $dbr->fetchObject( $res ) ) {
343 $n = $row->page_title
;
345 if ( $row->page_namespace
) {
346 $ns = $wgContLang->getNsText( $row->page_namespace
);
353 $dbr->freeResult( $res );
359 * Expand a list of pages to include templates used in those pages.
360 * @param $inputPages array, list of titles to look up
361 * @param $pageSet array, associative array indexed by titles for output
362 * @return array associative array index by titles
364 private function getTemplates( $inputPages, $pageSet ) {
365 return $this->getLinks( $inputPages, $pageSet,
367 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
368 array( 'page_id=tl_from' )
373 * Validate link depth setting, if available.
375 private function validateLinkDepth( $depth ) {
376 global $wgExportMaxLinkDepth;
382 if ( !$this->userCanOverrideExportDepth() ) {
383 if( $depth > $wgExportMaxLinkDepth ) {
384 return $wgExportMaxLinkDepth;
389 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
390 * crazy-big export from being done by someone setting the depth
391 * number too high. In other words, last resort safety net.
393 return intval( min( $depth, 5 ) );
396 /** Expand a list of pages to include pages linked to from that page. */
397 private function getPageLinks( $inputPages, $pageSet, $depth ) {
398 for(; $depth > 0; --$depth ) {
399 $pageSet = $this->getLinks(
400 $inputPages, $pageSet, 'pagelinks',
401 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
402 array( 'page_id=pl_from' )
404 $inputPages = array_keys( $pageSet );
411 * Expand a list of pages to include images used in those pages.
413 * @param $inputPages array, list of titles to look up
414 * @param $pageSet array, associative array indexed by titles for output
416 * @return array associative array index by titles
418 private function getImages( $inputPages, $pageSet ) {
419 return $this->getLinks(
423 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
424 array( 'page_id=il_from' )
429 * Expand a list of pages to include items used in those pages.
431 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
432 $dbr = wfGetDB( DB_SLAVE
);
434 foreach( $inputPages as $page ) {
435 $title = Title
::newFromText( $page );
438 $pageSet[$title->getPrefixedText()] = true;
439 /// @todo Fixme: May or may not be more efficient to batch these
440 /// by namespace when given multiple input pages.
441 $result = $dbr->select(
442 array( 'page', $table ),
447 'page_namespace' => $title->getNamespace(),
448 'page_title' => $title->getDBkey()
454 foreach( $result as $row ) {
455 $template = Title
::makeTitle( $row->namespace, $row->title
);
456 $pageSet[$template->getPrefixedText()] = true;