Merge "Set noindex,nofollow on missing (404) pages"
[mediawiki.git] / includes / specials / SpecialExport.php
blobbc8e728ecd08ec8365b37afa25baa2131190f33a
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 {
32 private $curonly, $doExport, $pageLinkDepth, $templates;
33 private $images;
35 public function __construct() {
36 parent::__construct( 'Export' );
39 public function execute( $par ) {
40 global $wgSitename, $wgExportAllowListContributors, $wgExportFromNamespaces;
41 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
42 global $wgExportAllowAll;
44 $this->setHeaders();
45 $this->outputHeader();
47 // Set some variables
48 $this->curonly = true;
49 $this->doExport = false;
50 $request = $this->getRequest();
51 $this->templates = $request->getCheck( 'templates' );
52 $this->images = $request->getCheck( 'images' ); // Doesn't do anything yet
53 $this->pageLinkDepth = $this->validateLinkDepth(
54 $request->getIntOrNull( 'pagelink-depth' )
56 $nsindex = '';
57 $exportall = false;
59 if ( $request->getCheck( 'addcat' ) ) {
60 $page = $request->getText( 'pages' );
61 $catname = $request->getText( 'catname' );
63 if ( $catname !== '' && $catname !== null && $catname !== false ) {
64 $t = Title::makeTitleSafe( NS_MAIN, $catname );
65 if ( $t ) {
66 /**
67 * @todo FIXME: This can lead to hitting memory limit for very large
68 * categories. Ideally we would do the lookup synchronously
69 * during the export in a single query.
71 $catpages = $this->getPagesFromCategory( $t );
72 if ( $catpages ) {
73 $page .= "\n" . implode( "\n", $catpages );
77 } elseif ( $request->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
78 $page = $request->getText( 'pages' );
79 $nsindex = $request->getText( 'nsindex', '' );
81 if ( strval( $nsindex ) !== '' ) {
82 /**
83 * Same implementation as above, so same @todo
85 $nspages = $this->getPagesFromNamespace( $nsindex );
86 if ( $nspages ) {
87 $page .= "\n" . implode( "\n", $nspages );
90 } elseif ( $request->getCheck( 'exportall' ) && $wgExportAllowAll ) {
91 $this->doExport = true;
92 $exportall = true;
94 /* Although $page and $history are not used later on, we
95 nevertheless set them to avoid that PHP notices about using
96 undefined variables foul up our XML output (see call to
97 doExport(...) further down) */
98 $page = '';
99 $history = '';
100 } elseif ( $request->wasPosted() && $par == '' ) {
101 $page = $request->getText( 'pages' );
102 $this->curonly = $request->getCheck( 'curonly' );
103 $rawOffset = $request->getVal( 'offset' );
105 if ( $rawOffset ) {
106 $offset = wfTimestamp( TS_MW, $rawOffset );
107 } else {
108 $offset = null;
111 $limit = $request->getInt( 'limit' );
112 $dir = $request->getVal( 'dir' );
113 $history = array(
114 'dir' => 'asc',
115 'offset' => false,
116 'limit' => $wgExportMaxHistory,
118 $historyCheck = $request->getCheck( 'history' );
120 if ( $this->curonly ) {
121 $history = WikiExporter::CURRENT;
122 } elseif ( !$historyCheck ) {
123 if ( $limit > 0 && ( $wgExportMaxHistory == 0 || $limit < $wgExportMaxHistory ) ) {
124 $history['limit'] = $limit;
127 if ( !is_null( $offset ) ) {
128 $history['offset'] = $offset;
131 if ( strtolower( $dir ) == 'desc' ) {
132 $history['dir'] = 'desc';
136 if ( $page != '' ) {
137 $this->doExport = true;
139 } else {
140 // Default to current-only for GET requests.
141 $page = $request->getText( 'pages', $par );
142 $historyCheck = $request->getCheck( 'history' );
144 if ( $historyCheck ) {
145 $history = WikiExporter::FULL;
146 } else {
147 $history = WikiExporter::CURRENT;
150 if ( $page != '' ) {
151 $this->doExport = true;
155 if ( !$wgExportAllowHistory ) {
156 // Override
157 $history = WikiExporter::CURRENT;
160 $list_authors = $request->getCheck( 'listauthors' );
161 if ( !$this->curonly || !$wgExportAllowListContributors ) {
162 $list_authors = false;
165 if ( $this->doExport ) {
166 $this->getOutput()->disable();
168 // Cancel output buffering and gzipping if set
169 // This should provide safer streaming for pages with history
170 wfResetOutputBuffers();
171 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
173 if ( $request->getCheck( 'wpDownload' ) ) {
174 // Provide a sane filename suggestion
175 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
176 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
179 $this->doExport( $page, $history, $list_authors, $exportall );
181 return;
184 $out = $this->getOutput();
185 $out->addWikiMsg( 'exporttext' );
187 $form = Xml::openElement( 'form', array( 'method' => 'post',
188 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ) ) );
189 $form .= Xml::inputLabel(
190 $this->msg( 'export-addcattext' )->text(),
191 'catname',
192 'catname',
194 ) . '&#160;';
195 $form .= Xml::submitButton(
196 $this->msg( 'export-addcat' )->text(),
197 array( 'name' => 'addcat' )
198 ) . '<br />';
200 if ( $wgExportFromNamespaces ) {
201 $form .= Html::namespaceSelector(
202 array(
203 'selected' => $nsindex,
204 'label' => $this->msg( 'export-addnstext' )->text()
205 ), array(
206 'name' => 'nsindex',
207 'id' => 'namespace',
208 'class' => 'namespaceselector',
210 ) . '&#160;';
211 $form .= Xml::submitButton(
212 $this->msg( 'export-addns' )->text(),
213 array( 'name' => 'addns' )
214 ) . '<br />';
217 if ( $wgExportAllowAll ) {
218 $form .= Xml::checkLabel(
219 $this->msg( 'exportall' )->text(),
220 'exportall',
221 'exportall',
222 $request->wasPosted() ? $request->getCheck( 'exportall' ) : false
223 ) . '<br />';
226 $form .= Xml::element(
227 'textarea',
228 array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ),
229 $page,
230 false
232 $form .= '<br />';
234 if ( $wgExportAllowHistory ) {
235 $form .= Xml::checkLabel(
236 $this->msg( 'exportcuronly' )->text(),
237 'curonly',
238 'curonly',
239 $request->wasPosted() ? $request->getCheck( 'curonly' ) : true
240 ) . '<br />';
241 } else {
242 $out->addWikiMsg( 'exportnohistory' );
245 $form .= Xml::checkLabel(
246 $this->msg( 'export-templates' )->text(),
247 'templates',
248 'wpExportTemplates',
249 $request->wasPosted() ? $request->getCheck( 'templates' ) : false
250 ) . '<br />';
252 if ( $wgExportMaxLinkDepth || $this->userCanOverrideExportDepth() ) {
253 $form .= Xml::inputLabel(
254 $this->msg( 'export-pagelinks' )->text(),
255 'pagelink-depth',
256 'pagelink-depth',
259 ) . '<br />';
262 /* Enable this when we can do something useful exporting/importing image information.
263 $form .= Xml::checkLabel(
264 $this->msg( 'export-images' )->text(),
265 'images',
266 'wpExportImages',
267 false
268 ) . '<br />';
270 $form .= Xml::checkLabel(
271 $this->msg( 'export-download' )->text(),
272 'wpDownload',
273 'wpDownload',
274 $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true
275 ) . '<br />';
277 if ( $wgExportAllowListContributors ) {
278 $form .= Xml::checkLabel(
279 $this->msg( 'exportlistauthors' )->text(),
280 'listauthors',
281 'listauthors',
282 $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false
283 ) . '<br />';
286 $form .= Xml::submitButton(
287 $this->msg( 'export-submit' )->text(),
288 Linker::tooltipAndAccesskeyAttribs( 'export' )
290 $form .= Xml::closeElement( 'form' );
292 $out->addHTML( $form );
296 * @return bool
298 private function userCanOverrideExportDepth() {
299 return $this->getUser()->isAllowed( 'override-export-depth' );
303 * Do the actual page exporting
305 * @param string $page user input on what page(s) to export
306 * @param int $history One of the WikiExporter history export constants
307 * @param bool $list_authors Whether to add distinct author list (when
308 * not returning full history)
309 * @param bool $exportall Whether to export everything
311 private function doExport( $page, $history, $list_authors, $exportall ) {
313 // If we are grabbing everything, enable full history and ignore the rest
314 if ( $exportall ) {
315 $history = WikiExporter::FULL;
316 } else {
318 $pageSet = array(); // Inverted index of all pages to look up
320 // Split up and normalize input
321 foreach ( explode( "\n", $page ) as $pageName ) {
322 $pageName = trim( $pageName );
323 $title = Title::newFromText( $pageName );
324 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
325 // Only record each page once!
326 $pageSet[$title->getPrefixedText()] = true;
330 // Set of original pages to pass on to further manipulation...
331 $inputPages = array_keys( $pageSet );
333 // Look up any linked pages if asked...
334 if ( $this->templates ) {
335 $pageSet = $this->getTemplates( $inputPages, $pageSet );
337 $linkDepth = $this->pageLinkDepth;
338 if ( $linkDepth ) {
339 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
342 // Enable this when we can do something useful exporting/importing image information.
343 // if( $this->images ) ) {
344 // $pageSet = $this->getImages( $inputPages, $pageSet );
345 // }
347 $pages = array_keys( $pageSet );
349 // Normalize titles to the same format and remove dupes, see bug 17374
350 foreach ( $pages as $k => $v ) {
351 $pages[$k] = str_replace( " ", "_", $v );
354 $pages = array_unique( $pages );
357 /* Ok, let's get to it... */
358 if ( $history == WikiExporter::CURRENT ) {
359 $lb = false;
360 $db = wfGetDB( DB_SLAVE );
361 $buffer = WikiExporter::BUFFER;
362 } else {
363 // Use an unbuffered query; histories may be very long!
364 $lb = wfGetLBFactory()->newMainLB();
365 $db = $lb->getConnection( DB_SLAVE );
366 $buffer = WikiExporter::STREAM;
368 // This might take a while... :D
369 wfSuppressWarnings();
370 set_time_limit( 0 );
371 wfRestoreWarnings();
374 $exporter = new WikiExporter( $db, $history, $buffer );
375 $exporter->list_authors = $list_authors;
376 $exporter->openStream();
378 if ( $exportall ) {
379 $exporter->allPages();
380 } else {
381 foreach ( $pages as $page ) {
382 #Bug 8824: Only export pages the user can read
383 $title = Title::newFromText( $page );
384 if ( is_null( $title ) ) {
385 // @todo Perhaps output an <error> tag or something.
386 continue;
389 if ( !$title->userCan( 'read', $this->getUser() ) ) {
390 // @todo Perhaps output an <error> tag or something.
391 continue;
394 $exporter->pageByTitle( $title );
398 $exporter->closeStream();
400 if ( $lb ) {
401 $lb->closeAll();
406 * @param Title $title
407 * @return array
409 private function getPagesFromCategory( $title ) {
410 global $wgContLang;
412 $name = $title->getDBkey();
414 $dbr = wfGetDB( DB_SLAVE );
415 $res = $dbr->select(
416 array( 'page', 'categorylinks' ),
417 array( 'page_namespace', 'page_title' ),
418 array( 'cl_from=page_id', 'cl_to' => $name ),
419 __METHOD__,
420 array( 'LIMIT' => '5000' )
423 $pages = array();
425 foreach ( $res as $row ) {
426 $n = $row->page_title;
427 if ( $row->page_namespace ) {
428 $ns = $wgContLang->getNsText( $row->page_namespace );
429 $n = $ns . ':' . $n;
432 $pages[] = $n;
435 return $pages;
439 * @param int $nsindex
440 * @return array
442 private function getPagesFromNamespace( $nsindex ) {
443 global $wgContLang;
445 $dbr = wfGetDB( DB_SLAVE );
446 $res = $dbr->select(
447 'page',
448 array( 'page_namespace', 'page_title' ),
449 array( 'page_namespace' => $nsindex ),
450 __METHOD__,
451 array( 'LIMIT' => '5000' )
454 $pages = array();
456 foreach ( $res as $row ) {
457 $n = $row->page_title;
459 if ( $row->page_namespace ) {
460 $ns = $wgContLang->getNsText( $row->page_namespace );
461 $n = $ns . ':' . $n;
464 $pages[] = $n;
467 return $pages;
471 * Expand a list of pages to include templates used in those pages.
472 * @param array $inputPages List of titles to look up
473 * @param array $pageSet Associative array indexed by titles for output
474 * @return array Associative array index by titles
476 private function getTemplates( $inputPages, $pageSet ) {
477 return $this->getLinks( $inputPages, $pageSet,
478 'templatelinks',
479 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
480 array( 'page_id=tl_from' )
485 * Validate link depth setting, if available.
486 * @param int $depth
487 * @return int
489 private function validateLinkDepth( $depth ) {
490 global $wgExportMaxLinkDepth;
492 if ( $depth < 0 ) {
493 return 0;
496 if ( !$this->userCanOverrideExportDepth() ) {
497 if ( $depth > $wgExportMaxLinkDepth ) {
498 return $wgExportMaxLinkDepth;
503 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
504 * crazy-big export from being done by someone setting the depth
505 * number too high. In other words, last resort safety net.
508 return intval( min( $depth, 5 ) );
512 * Expand a list of pages to include pages linked to from that page.
513 * @param array $inputPages
514 * @param array $pageSet
515 * @param int $depth
516 * @return array
518 private function getPageLinks( $inputPages, $pageSet, $depth ) {
519 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
520 for ( ; $depth > 0; --$depth ) {
521 // @codingStandardsIgnoreEnd
522 $pageSet = $this->getLinks(
523 $inputPages, $pageSet, 'pagelinks',
524 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
525 array( 'page_id=pl_from' )
527 $inputPages = array_keys( $pageSet );
530 return $pageSet;
534 * Expand a list of pages to include images used in those pages.
536 * @param array $inputPages List of titles to look up
537 * @param array $pageSet Associative array indexed by titles for output
539 * @return array associative array index by titles
541 private function getImages( $inputPages, $pageSet ) {
542 return $this->getLinks(
543 $inputPages,
544 $pageSet,
545 'imagelinks',
546 array( 'namespace' => NS_FILE, 'title' => 'il_to' ),
547 array( 'page_id=il_from' )
552 * Expand a list of pages to include items used in those pages.
553 * @param array $inputPages Array of page titles
554 * @param array $pageSet
555 * @param string $table
556 * @param array $fields Array of field names
557 * @param array $join
558 * @return array
560 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
561 $dbr = wfGetDB( DB_SLAVE );
563 foreach ( $inputPages as $page ) {
564 $title = Title::newFromText( $page );
566 if ( $title ) {
567 $pageSet[$title->getPrefixedText()] = true;
568 /// @todo FIXME: May or may not be more efficient to batch these
569 /// by namespace when given multiple input pages.
570 $result = $dbr->select(
571 array( 'page', $table ),
572 $fields,
573 array_merge(
574 $join,
575 array(
576 'page_namespace' => $title->getNamespace(),
577 'page_title' => $title->getDBkey()
580 __METHOD__
583 foreach ( $result as $row ) {
584 $template = Title::makeTitle( $row->namespace, $row->title );
585 $pageSet[$template->getPrefixedText()] = true;
590 return $pageSet;
593 protected function getGroupName() {
594 return 'pagetools';