2 # Copyright (C) 2003-2008 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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
21 * @ingroup SpecialPage
24 class SpecialExport
extends SpecialPage
{
26 private $curonly, $doExport, $pageLinkDepth, $templates;
29 public function __construct() {
30 parent
::__construct( 'Export' );
33 public function execute( $par ) {
34 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
35 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
38 $this->outputHeader();
41 $this->curonly
= true;
42 $this->doExport
= false;
43 $this->templates
= $wgRequest->getCheck( 'templates' );
44 $this->images
= $wgRequest->getCheck( 'images' ); // Doesn't do anything yet
45 $this->pageLinkDepth
= $this->validateLinkDepth(
46 $wgRequest->getIntOrNull( 'pagelink-depth' ) );
48 if ( $wgRequest->getCheck( 'addcat' ) ) {
49 $page = $wgRequest->getText( 'pages' );
50 $catname = $wgRequest->getText( 'catname' );
52 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
53 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
56 * @fixme This can lead to hitting memory limit for very large
57 * categories. Ideally we would do the lookup synchronously
58 * during the export in a single query.
60 $catpages = $this->getPagesFromCategory( $t );
61 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
65 else if( $wgRequest->getCheck( 'addns' ) ) {
66 $page = $wgRequest->getText( 'pages' );
67 $nsindex = $wgRequest->getText( 'nsindex' );
69 if ( $nsindex !== '' && $nsindex !== NULL && $nsindex !== false ) {
71 * Same implementation as above, so same @fixme
73 $nspages = $this->getPagesFromNamespace( $nsindex );
74 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
77 else if( $wgRequest->wasPosted() && $par == '' ) {
78 $page = $wgRequest->getText( 'pages' );
79 $this->curonly
= $wgRequest->getCheck( 'curonly' );
80 $rawOffset = $wgRequest->getVal( 'offset' );
82 $offset = wfTimestamp( TS_MW
, $rawOffset );
86 $limit = $wgRequest->getInt( 'limit' );
87 $dir = $wgRequest->getVal( 'dir' );
91 'limit' => $wgExportMaxHistory,
93 $historyCheck = $wgRequest->getCheck( 'history' );
94 if ( $this->curonly
) {
95 $history = WikiExporter
::CURRENT
;
96 } elseif ( !$historyCheck ) {
97 if ( $limit > 0 && $limit < $wgExportMaxHistory ) {
98 $history['limit'] = $limit;
100 if ( !is_null( $offset ) ) {
101 $history['offset'] = $offset;
103 if ( strtolower( $dir ) == 'desc' ) {
104 $history['dir'] = 'desc';
108 if( $page != '' ) $this->doExport
= true;
110 // Default to current-only for GET requests
111 $page = $wgRequest->getText( 'pages', $par );
112 $historyCheck = $wgRequest->getCheck( 'history' );
113 if( $historyCheck ) {
114 $history = WikiExporter
::FULL
;
116 $history = WikiExporter
::CURRENT
;
119 if( $page != '' ) $this->doExport
= true;
122 if( !$wgExportAllowHistory ) {
124 $history = WikiExporter
::CURRENT
;
127 $list_authors = $wgRequest->getCheck( 'listauthors' );
128 if ( !$this->curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
130 if ( $this->doExport
) {
132 // Cancel output buffering and gzipping if set
133 // This should provide safer streaming for pages with history
134 wfResetOutputBuffers();
135 header( "Content-type: application/xml; charset=utf-8" );
136 if( $wgRequest->getCheck( 'wpDownload' ) ) {
137 // Provide a sane filename suggestion
138 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
139 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
141 $this->doExport( $page, $history, $list_authors );
145 $wgOut->addWikiMsg( 'exporttext' );
147 $form = Xml
::openElement( 'form', array( 'method' => 'post',
148 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
149 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
150 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
152 $form .= Xml
::namespaceSelector( '', null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
153 $form .= Xml
::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
155 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
158 if( $wgExportAllowHistory ) {
159 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
161 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
163 $form .= Xml
::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
164 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
165 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
167 // Enable this when we can do something useful exporting/importing image information. :)
168 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
169 $form .= Xml
::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
171 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
172 $form .= Xml
::closeElement( 'form' );
173 $wgOut->addHTML( $form );
176 private function userCanOverrideExportDepth() {
179 return $wgUser->isAllowed( 'override-export-depth' );
183 * Do the actual page exporting
184 * @param string $page User input on what page(s) to export
185 * @param mixed $history one of the WikiExporter history export constants
187 private function doExport( $page, $history, $list_authors ) {
188 global $wgExportMaxHistory;
190 /* Split up the input and look up linked pages */
191 $inputPages = array_filter( explode( "\n", $page ), array( $this, 'filterPage' ) );
192 $pageSet = array_flip( $inputPages );
194 if( $this->templates
) {
195 $pageSet = $this->getTemplates( $inputPages, $pageSet );
198 if( $linkDepth = $this->pageLinkDepth
) {
199 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
203 // Enable this when we can do something useful exporting/importing image information. :)
204 if( $this->images ) ) {
205 $pageSet = $this->getImages( $inputPages, $pageSet );
209 $pages = array_keys( $pageSet );
211 /* Ok, let's get to it... */
212 if( $history == WikiExporter
::CURRENT
) {
214 $db = wfGetDB( DB_SLAVE
);
215 $buffer = WikiExporter
::BUFFER
;
217 // Use an unbuffered query; histories may be very long!
218 $lb = wfGetLBFactory()->newMainLB();
219 $db = $lb->getConnection( DB_SLAVE
);
220 $buffer = WikiExporter
::STREAM
;
222 // This might take a while... :D
223 wfSuppressWarnings();
227 $exporter = new WikiExporter( $db, $history, $buffer );
228 $exporter->list_authors
= $list_authors;
229 $exporter->openStream();
230 foreach( $pages as $page ) {
232 if( $wgExportMaxHistory && !$this->curonly ) {
233 $title = Title::newFromText( $page );
235 $count = Revision::countByTitle( $db, $title );
236 if( $count > $wgExportMaxHistory ) {
237 wfDebug( __FUNCTION__ .
238 ": Skipped $page, $count revisions too big\n" );
243 #Bug 8824: Only export pages the user can read
244 $title = Title
::newFromText( $page );
245 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
246 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
248 $exporter->pageByTitle( $title );
251 $exporter->closeStream();
258 private function getPagesFromCategory( $title ) {
261 $name = $title->getDBkey();
263 $dbr = wfGetDB( DB_SLAVE
);
264 $res = $dbr->select( array('page', 'categorylinks' ),
265 array( 'page_namespace', 'page_title' ),
266 array('cl_from=page_id', 'cl_to' => $name ),
267 __METHOD__
, array('LIMIT' => '5000'));
270 while ( $row = $dbr->fetchObject( $res ) ) {
271 $n = $row->page_title
;
272 if ($row->page_namespace
) {
273 $ns = $wgContLang->getNsText( $row->page_namespace
);
279 $dbr->freeResult($res);
284 private function getPagesFromNamespace( $nsindex ) {
287 $dbr = wfGetDB( DB_SLAVE
);
288 $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
289 array('page_namespace' => $nsindex),
290 __METHOD__
, array('LIMIT' => '5000') );
293 while ( $row = $dbr->fetchObject( $res ) ) {
294 $n = $row->page_title
;
295 if ($row->page_namespace
) {
296 $ns = $wgContLang->getNsText( $row->page_namespace
);
302 $dbr->freeResult($res);
307 * Expand a list of pages to include templates used in those pages.
308 * @param $inputPages array, list of titles to look up
309 * @param $pageSet array, associative array indexed by titles for output
310 * @return array associative array index by titles
312 private function getTemplates( $inputPages, $pageSet ) {
313 return $this->getLinks( $inputPages, $pageSet,
315 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
316 array( 'page_id=tl_from' ) );
320 * Validate link depth setting, if available.
322 private function validateLinkDepth( $depth ) {
323 global $wgExportMaxLinkDepth, $wgExportMaxLinkDepthLimit;
327 if ( !$this->userCanOverrideExportDepth() ) {
328 if( $depth > $wgExportMaxLinkDepth ) {
329 return $wgExportMaxLinkDepth;
333 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
334 * crazy-big export from being done by someone setting the depth
335 * number too high. In other words, last resort safety net.
337 return intval( min( $depth, 5 ) );
340 /** Expand a list of pages to include pages linked to from that page. */
341 private function getPageLinks( $inputPages, $pageSet, $depth ) {
342 for( $depth=$depth; $depth>0; --$depth ) {
343 $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
344 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
345 array( 'page_id=pl_from' ) );
351 * Expand a list of pages to include images used in those pages.
352 * @param $inputPages array, list of titles to look up
353 * @param $pageSet array, associative array indexed by titles for output
354 * @return array associative array index by titles
356 private function getImages( $inputPages, $pageSet ) {
357 return $this->getLinks( $inputPages, $pageSet,
359 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
360 array( 'page_id=il_from' ) );
364 * Expand a list of pages to include items used in those pages.
367 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
368 $dbr = wfGetDB( DB_SLAVE
);
369 foreach( $inputPages as $page ) {
370 $title = Title
::newFromText( $page );
372 $pageSet[$title->getPrefixedText()] = true;
373 /// @fixme May or may not be more efficient to batch these
374 /// by namespace when given multiple input pages.
375 $result = $dbr->select(
376 array( 'page', $table ),
380 'page_namespace' => $title->getNamespace(),
381 'page_title' => $title->getDBKey() ) ),
383 foreach( $result as $row ) {
384 $template = Title
::makeTitle( $row->namespace, $row->title
);
385 $pageSet[$template->getPrefixedText()] = true;
393 * Callback function to remove empty strings from the pages array.
395 private function filterPage( $page ) {
396 return $page !== '' && $page !== null;