Merge "Various fixes to Special:Mostcategories."
[mediawiki.git] / includes / specials / SpecialExport.php
blobb00eec82456e0874b57992555188c1bae1cef4e1
1 <?php
2 /**
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
22 * @file
23 * @ingroup SpecialPage
26 /**
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;
34 private $images;
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;
45 $this->setHeaders();
46 $this->outputHeader();
48 // Set some variables
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' )
57 $nsindex = '';
58 $exportall = false;
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 );
66 if ( $t ) {
67 /**
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 );
73 if ( $catpages ) {
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 ) !== '' ) {
84 /**
85 * Same implementation as above, so same @todo
87 $nspages = $this->getPagesFromNamespace( $nsindex );
88 if ( $nspages ) {
89 $page .= "\n" . implode( "\n", $nspages );
93 elseif( $request->getCheck( 'exportall' ) && $wgExportAllowAll ) {
94 $this->doExport = true;
95 $exportall = 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) */
101 $page = '';
102 $history = '';
104 elseif( $request->wasPosted() && $par == '' ) {
105 $page = $request->getText( 'pages' );
106 $this->curonly = $request->getCheck( 'curonly' );
107 $rawOffset = $request->getVal( 'offset' );
109 if( $rawOffset ) {
110 $offset = wfTimestamp( TS_MW, $rawOffset );
111 } else {
112 $offset = null;
115 $limit = $request->getInt( 'limit' );
116 $dir = $request->getVal( 'dir' );
117 $history = array(
118 'dir' => 'asc',
119 'offset' => false,
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';
138 if( $page != '' ) {
139 $this->doExport = true;
141 } else {
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;
148 } else {
149 $history = WikiExporter::CURRENT;
152 if( $page != '' ) {
153 $this->doExport = true;
157 if( !$wgExportAllowHistory ) {
158 // Override
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 );
183 return;
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 ) . '&#160;';
192 $form .= Xml::submitButton( $this->msg( 'export-addcat' )->text(), array( 'name' => 'addcat' ) ) . '<br />';
194 if ( $wgExportFromNamespaces ) {
195 $form .= Html::namespaceSelector(
196 array(
197 'selected' => $nsindex,
198 'label' => $this->msg( 'export-addnstext' )->text()
199 ), array(
200 'name' => 'nsindex',
201 'id' => 'namespace',
202 'class' => 'namespaceselector',
204 ) . '&#160;';
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(),
211 'exportall',
212 'exportall',
213 $request->wasPosted() ? $request->getCheck( 'exportall' ) : false
214 ) . '<br />';
217 $form .= Xml::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
218 $form .= '<br />';
220 if( $wgExportAllowHistory ) {
221 $form .= Xml::checkLabel(
222 $this->msg( 'exportcuronly' )->text(),
223 'curonly',
224 'curonly',
225 $request->wasPosted() ? $request->getCheck( 'curonly' ) : true
226 ) . '<br />';
227 } else {
228 $out->addWikiMsg( 'exportnohistory' );
231 $form .= Xml::checkLabel(
232 $this->msg( 'export-templates' )->text(),
233 'templates',
234 'wpExportTemplates',
235 $request->wasPosted() ? $request->getCheck( 'templates' ) : false
236 ) . '<br />';
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(),
245 'wpDownload',
246 'wpDownload',
247 $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true
248 ) . '<br />';
250 if ( $wgExportAllowListContributors ) {
251 $form .= Xml::checkLabel(
252 $this->msg( 'exportlistauthors' )->text(),
253 'listauthors',
254 'listauthors',
255 $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false
256 ) . '<br />';
259 $form .= Xml::submitButton( $this->msg( 'export-submit' )->text(), Linker::tooltipAndAccesskeyAttribs( 'export' ) );
260 $form .= Xml::closeElement( 'form' );
262 $out->addHTML( $form );
266 * @return bool
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
284 if ( $exportall ) {
285 $history = WikiExporter::FULL;
286 } else {
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;
308 if( $linkDepth ) {
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 ) {
331 $lb = false;
332 $db = wfGetDB( DB_SLAVE );
333 $buffer = WikiExporter::BUFFER;
334 } else {
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();
342 set_time_limit(0);
343 wfRestoreWarnings();
346 $exporter = new WikiExporter( $db, $history, $buffer );
347 $exporter->list_authors = $list_authors;
348 $exporter->openStream();
350 if ( $exportall ) {
351 $exporter->allPages();
352 } else {
353 foreach( $pages as $page ) {
355 if( $wgExportMaxHistory && !$this->curonly ) {
356 $title = Title::newFromText( $page );
357 if( $title ) {
358 $count = Revision::countByTitle( $db, $title );
359 if( $count > $wgExportMaxHistory ) {
360 wfDebug( __FUNCTION__ .
361 ": Skipped $page, $count revisions too big\n" );
362 continue;
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();
381 if( $lb ) {
382 $lb->closeAll();
387 * @param $title Title
388 * @return array
390 private function getPagesFromCategory( $title ) {
391 global $wgContLang;
393 $name = $title->getDBkey();
395 $dbr = wfGetDB( DB_SLAVE );
396 $res = $dbr->select(
397 array( 'page', 'categorylinks' ),
398 array( 'page_namespace', 'page_title' ),
399 array( 'cl_from=page_id', 'cl_to' => $name ),
400 __METHOD__,
401 array( 'LIMIT' => '5000' )
404 $pages = array();
406 foreach ( $res as $row ) {
407 $n = $row->page_title;
408 if ($row->page_namespace) {
409 $ns = $wgContLang->getNsText( $row->page_namespace );
410 $n = $ns . ':' . $n;
413 $pages[] = $n;
415 return $pages;
419 * @param $nsindex int
420 * @return array
422 private function getPagesFromNamespace( $nsindex ) {
423 global $wgContLang;
425 $dbr = wfGetDB( DB_SLAVE );
426 $res = $dbr->select(
427 'page',
428 array( 'page_namespace', 'page_title' ),
429 array( 'page_namespace' => $nsindex ),
430 __METHOD__,
431 array( 'LIMIT' => '5000' )
434 $pages = array();
436 foreach ( $res as $row ) {
437 $n = $row->page_title;
439 if ( $row->page_namespace ) {
440 $ns = $wgContLang->getNsText( $row->page_namespace );
441 $n = $ns . ':' . $n;
444 $pages[] = $n;
446 return $pages;
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,
457 'templatelinks',
458 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
459 array( 'page_id=tl_from' )
464 * Validate link depth setting, if available.
465 * @param $depth int
466 * @return int
468 private function validateLinkDepth( $depth ) {
469 global $wgExportMaxLinkDepth;
471 if( $depth < 0 ) {
472 return 0;
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
493 * @param $depth int
494 * @return 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 );
506 return $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(
519 $inputPages,
520 $pageSet,
521 'imagelinks',
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.
529 * @return array
531 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
532 $dbr = wfGetDB( DB_SLAVE );
534 foreach( $inputPages as $page ) {
535 $title = Title::newFromText( $page );
537 if( $title ) {
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 ),
543 $fields,
544 array_merge(
545 $join,
546 array(
547 'page_namespace' => $title->getNamespace(),
548 'page_title' => $title->getDBkey()
551 __METHOD__
554 foreach( $result as $row ) {
555 $template = Title::makeTitle( $row->namespace, $row->title );
556 $pageSet[$template->getPrefixedText()] = true;
561 return $pageSet;