Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / specials / SpecialExport.php
1 <?php
2 /**
3 * Implements Special:Export
4 *
5 * Copyright © 2003-2008 Brion Vibber <brion@pobox.com>
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * A special page that allows users to export pages in a XML file
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialExport extends SpecialPage {
32 private $curonly, $doExport, $pageLinkDepth, $templates;
33
34 public function __construct() {
35 parent::__construct( 'Export' );
36 }
37
38 public function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
41 $config = $this->getConfig();
42
43 // Set some variables
44 $this->curonly = true;
45 $this->doExport = false;
46 $request = $this->getRequest();
47 $this->templates = $request->getCheck( 'templates' );
48 $this->pageLinkDepth = $this->validateLinkDepth(
49 $request->getIntOrNull( 'pagelink-depth' )
50 );
51 $nsindex = '';
52 $exportall = false;
53
54 if ( $request->getCheck( 'addcat' ) ) {
55 $page = $request->getText( 'pages' );
56 $catname = $request->getText( 'catname' );
57
58 if ( $catname !== '' && $catname !== null && $catname !== false ) {
59 $t = Title::makeTitleSafe( NS_MAIN, $catname );
60 if ( $t ) {
61 /**
62 * @todo FIXME: This can lead to hitting memory limit for very large
63 * categories. Ideally we would do the lookup synchronously
64 * during the export in a single query.
65 */
66 $catpages = $this->getPagesFromCategory( $t );
67 if ( $catpages ) {
68 $page .= "\n" . implode( "\n", $catpages );
69 }
70 }
71 }
72 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
73 $page = $request->getText( 'pages' );
74 $nsindex = $request->getText( 'nsindex', '' );
75
76 if ( strval( $nsindex ) !== '' ) {
77 /**
78 * Same implementation as above, so same @todo
79 */
80 $nspages = $this->getPagesFromNamespace( $nsindex );
81 if ( $nspages ) {
82 $page .= "\n" . implode( "\n", $nspages );
83 }
84 }
85 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
86 $this->doExport = true;
87 $exportall = true;
88
89 /* Although $page and $history are not used later on, we
90 nevertheless set them to avoid that PHP notices about using
91 undefined variables foul up our XML output (see call to
92 doExport(...) further down) */
93 $page = '';
94 $history = '';
95 } elseif ( $request->wasPosted() && $par == '' ) {
96 $page = $request->getText( 'pages' );
97 $this->curonly = $request->getCheck( 'curonly' );
98 $rawOffset = $request->getVal( 'offset' );
99
100 if ( $rawOffset ) {
101 $offset = wfTimestamp( TS_MW, $rawOffset );
102 } else {
103 $offset = null;
104 }
105
106 $maxHistory = $config->get( 'ExportMaxHistory' );
107 $limit = $request->getInt( 'limit' );
108 $dir = $request->getVal( 'dir' );
109 $history = array(
110 'dir' => 'asc',
111 'offset' => false,
112 'limit' => $maxHistory,
113 );
114 $historyCheck = $request->getCheck( 'history' );
115
116 if ( $this->curonly ) {
117 $history = WikiExporter::CURRENT;
118 } elseif ( !$historyCheck ) {
119 if ( $limit > 0 && ( $maxHistory == 0 || $limit < $maxHistory ) ) {
120 $history['limit'] = $limit;
121 }
122
123 if ( !is_null( $offset ) ) {
124 $history['offset'] = $offset;
125 }
126
127 if ( strtolower( $dir ) == 'desc' ) {
128 $history['dir'] = 'desc';
129 }
130 }
131
132 if ( $page != '' ) {
133 $this->doExport = true;
134 }
135 } else {
136 // Default to current-only for GET requests.
137 $page = $request->getText( 'pages', $par );
138 $historyCheck = $request->getCheck( 'history' );
139
140 if ( $historyCheck ) {
141 $history = WikiExporter::FULL;
142 } else {
143 $history = WikiExporter::CURRENT;
144 }
145
146 if ( $page != '' ) {
147 $this->doExport = true;
148 }
149 }
150
151 if ( !$config->get( 'ExportAllowHistory' ) ) {
152 // Override
153 $history = WikiExporter::CURRENT;
154 }
155
156 $list_authors = $request->getCheck( 'listauthors' );
157 if ( !$this->curonly || !$config->get( 'ExportAllowListContributors' ) ) {
158 $list_authors = false;
159 }
160
161 if ( $this->doExport ) {
162 $this->getOutput()->disable();
163
164 // Cancel output buffering and gzipping if set
165 // This should provide safer streaming for pages with history
166 wfResetOutputBuffers();
167 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
168
169 if ( $request->getCheck( 'wpDownload' ) ) {
170 // Provide a sane filename suggestion
171 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
172 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
173 }
174
175 $this->doExport( $page, $history, $list_authors, $exportall );
176
177 return;
178 }
179
180 $out = $this->getOutput();
181 $out->addWikiMsg( 'exporttext' );
182
183 if ( $page == '' ) {
184 $categoryName = $request->getText( 'catname' );
185 } else {
186 $categoryName = '';
187 }
188
189 $formDescriptor = array(
190 'catname' => array(
191 'type' => 'textwithbutton',
192 'name' => 'catname',
193 'horizontal-label' => true,
194 'label-message' => 'export-addcattext',
195 'default' => $categoryName,
196 'size' => 40,
197 'buttontype' => 'submit',
198 'buttonname' => 'addcat',
199 'buttondefault' => $this->msg( 'export-addcat' )->text(),
200 ),
201 );
202 if ( $config->get( 'ExportFromNamespaces' ) ) {
203 $formDescriptor += array(
204 'nsindex' => array(
205 'type' => 'namespaceselectwithbutton',
206 'default' => $nsindex,
207 'label-message' => 'export-addnstext',
208 'horizontal-label' => true,
209 'name' => 'nsindex',
210 'id' => 'namespace',
211 'cssclass' => 'namespaceselector',
212 'buttontype' => 'submit',
213 'buttonname' => 'addns',
214 'buttondefault' => $this->msg( 'export-addns' )->text(),
215 ),
216 );
217 }
218
219 if ( $config->get( 'ExportAllowAll' ) ) {
220 $formDescriptor += array(
221 'exportall' => array(
222 'type' => 'check',
223 'label-message' => 'exportall',
224 'name' => 'exportall',
225 'id' => 'exportall',
226 'default' => $request->wasPosted() ? $request->getCheck( 'exportall' ) : false,
227 ),
228 );
229 }
230
231 $formDescriptor += array(
232 'textarea' => array(
233 'class' => 'HTMLTextAreaField',
234 'name' => 'pages',
235 'nodata' => true,
236 'cols' => 40,
237 'rows' => 10,
238 'default' => $page,
239 ),
240 );
241
242 if ( $config->get( 'ExportAllowHistory' ) ) {
243 $formDescriptor += array(
244 'curonly' => array(
245 'type' => 'check',
246 'label-message' => 'exportcuronly',
247 'name' => 'curonly',
248 'id' => 'curonly',
249 'default' => $request->wasPosted() ? $request->getCheck( 'curonly' ) : true,
250 ),
251 );
252 } else {
253 $out->addWikiMsg( 'exportnohistory' );
254 }
255
256 $formDescriptor += array(
257 'templates' => array(
258 'type' => 'check',
259 'label-message' => 'export-templates',
260 'name' => 'templates',
261 'id' => 'wpExportTemplates',
262 'default' => $request->wasPosted() ? $request->getCheck( 'templates' ) : false,
263 ),
264 );
265
266 if ( $config->get( 'ExportMaxLinkDepth' ) || $this->userCanOverrideExportDepth() ) {
267 $formDescriptor += array(
268 'pagelink-depth' => array(
269 'type' => 'text',
270 'name' => 'pagelink-depth',
271 'id' => 'pagelink-depth',
272 'label-message' => 'export-pagelinks',
273 'default' => '0',
274 'size' => 20,
275 ),
276 );
277 }
278
279 $formDescriptor += array(
280 'wpDownload' => array(
281 'type' => 'check',
282 'name' =>'wpDownload',
283 'id' => 'wpDownload',
284 'default' => $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true,
285 'label-message' => 'export-download',
286 ),
287 );
288
289 if ( $config->get( 'ExportAllowListContributors' ) ) {
290 $formDescriptor += array(
291 'listauthors' => array(
292 'type' => 'check',
293 'label-message' => 'exportlistauthors',
294 'default' => $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false,
295 'name' => 'listauthors',
296 'id' => 'listauthors',
297 ),
298 );
299 }
300
301 $htmlForm = HTMLForm::factory( 'div', $formDescriptor, $this->getContext() );
302 $htmlForm->setSubmitTextMsg( 'export-submit' );
303 $htmlForm->prepareForm()->displayForm( false );
304 $this->addHelpLink( 'Help:Export' );
305 }
306
307 /**
308 * @return bool
309 */
310 private function userCanOverrideExportDepth() {
311 return $this->getUser()->isAllowed( 'override-export-depth' );
312 }
313
314 /**
315 * Do the actual page exporting
316 *
317 * @param string $page User input on what page(s) to export
318 * @param int $history One of the WikiExporter history export constants
319 * @param bool $list_authors Whether to add distinct author list (when
320 * not returning full history)
321 * @param bool $exportall Whether to export everything
322 */
323 private function doExport( $page, $history, $list_authors, $exportall ) {
324
325 // If we are grabbing everything, enable full history and ignore the rest
326 if ( $exportall ) {
327 $history = WikiExporter::FULL;
328 } else {
329 $pageSet = array(); // Inverted index of all pages to look up
330
331 // Split up and normalize input
332 foreach ( explode( "\n", $page ) as $pageName ) {
333 $pageName = trim( $pageName );
334 $title = Title::newFromText( $pageName );
335 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
336 // Only record each page once!
337 $pageSet[$title->getPrefixedText()] = true;
338 }
339 }
340
341 // Set of original pages to pass on to further manipulation...
342 $inputPages = array_keys( $pageSet );
343
344 // Look up any linked pages if asked...
345 if ( $this->templates ) {
346 $pageSet = $this->getTemplates( $inputPages, $pageSet );
347 }
348 $linkDepth = $this->pageLinkDepth;
349 if ( $linkDepth ) {
350 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
351 }
352
353 $pages = array_keys( $pageSet );
354
355 // Normalize titles to the same format and remove dupes, see bug 17374
356 foreach ( $pages as $k => $v ) {
357 $pages[$k] = str_replace( " ", "_", $v );
358 }
359
360 $pages = array_unique( $pages );
361 }
362
363 /* Ok, let's get to it... */
364 if ( $history == WikiExporter::CURRENT ) {
365 $lb = false;
366 $db = wfGetDB( DB_SLAVE );
367 $buffer = WikiExporter::BUFFER;
368 } else {
369 // Use an unbuffered query; histories may be very long!
370 $lb = wfGetLBFactory()->newMainLB();
371 $db = $lb->getConnection( DB_SLAVE );
372 $buffer = WikiExporter::STREAM;
373
374 // This might take a while... :D
375 MediaWiki\suppressWarnings();
376 set_time_limit( 0 );
377 MediaWiki\restoreWarnings();
378 }
379
380 $exporter = new WikiExporter( $db, $history, $buffer );
381 $exporter->list_authors = $list_authors;
382 $exporter->openStream();
383
384 if ( $exportall ) {
385 $exporter->allPages();
386 } else {
387 foreach ( $pages as $page ) {
388 #Bug 8824: Only export pages the user can read
389 $title = Title::newFromText( $page );
390 if ( is_null( $title ) ) {
391 // @todo Perhaps output an <error> tag or something.
392 continue;
393 }
394
395 if ( !$title->userCan( 'read', $this->getUser() ) ) {
396 // @todo Perhaps output an <error> tag or something.
397 continue;
398 }
399
400 $exporter->pageByTitle( $title );
401 }
402 }
403
404 $exporter->closeStream();
405
406 if ( $lb ) {
407 $lb->closeAll();
408 }
409 }
410
411 /**
412 * @param Title $title
413 * @return array
414 */
415 private function getPagesFromCategory( $title ) {
416 global $wgContLang;
417
418 $name = $title->getDBkey();
419
420 $dbr = wfGetDB( DB_SLAVE );
421 $res = $dbr->select(
422 array( 'page', 'categorylinks' ),
423 array( 'page_namespace', 'page_title' ),
424 array( 'cl_from=page_id', 'cl_to' => $name ),
425 __METHOD__,
426 array( 'LIMIT' => '5000' )
427 );
428
429 $pages = array();
430
431 foreach ( $res as $row ) {
432 $n = $row->page_title;
433 if ( $row->page_namespace ) {
434 $ns = $wgContLang->getNsText( $row->page_namespace );
435 $n = $ns . ':' . $n;
436 }
437
438 $pages[] = $n;
439 }
440
441 return $pages;
442 }
443
444 /**
445 * @param int $nsindex
446 * @return array
447 */
448 private function getPagesFromNamespace( $nsindex ) {
449 global $wgContLang;
450
451 $dbr = wfGetDB( DB_SLAVE );
452 $res = $dbr->select(
453 'page',
454 array( 'page_namespace', 'page_title' ),
455 array( 'page_namespace' => $nsindex ),
456 __METHOD__,
457 array( 'LIMIT' => '5000' )
458 );
459
460 $pages = array();
461
462 foreach ( $res as $row ) {
463 $n = $row->page_title;
464
465 if ( $row->page_namespace ) {
466 $ns = $wgContLang->getNsText( $row->page_namespace );
467 $n = $ns . ':' . $n;
468 }
469
470 $pages[] = $n;
471 }
472
473 return $pages;
474 }
475
476 /**
477 * Expand a list of pages to include templates used in those pages.
478 * @param array $inputPages List of titles to look up
479 * @param array $pageSet Associative array indexed by titles for output
480 * @return array Associative array index by titles
481 */
482 private function getTemplates( $inputPages, $pageSet ) {
483 return $this->getLinks( $inputPages, $pageSet,
484 'templatelinks',
485 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
486 array( 'page_id=tl_from' )
487 );
488 }
489
490 /**
491 * Validate link depth setting, if available.
492 * @param int $depth
493 * @return int
494 */
495 private function validateLinkDepth( $depth ) {
496 if ( $depth < 0 ) {
497 return 0;
498 }
499
500 if ( !$this->userCanOverrideExportDepth() ) {
501 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
502 if ( $depth > $maxLinkDepth ) {
503 return $maxLinkDepth;
504 }
505 }
506
507 /*
508 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
509 * crazy-big export from being done by someone setting the depth
510 * number too high. In other words, last resort safety net.
511 */
512
513 return intval( min( $depth, 5 ) );
514 }
515
516 /**
517 * Expand a list of pages to include pages linked to from that page.
518 * @param array $inputPages
519 * @param array $pageSet
520 * @param int $depth
521 * @return array
522 */
523 private function getPageLinks( $inputPages, $pageSet, $depth ) {
524 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
525 for ( ; $depth > 0; --$depth ) {
526 // @codingStandardsIgnoreEnd
527 $pageSet = $this->getLinks(
528 $inputPages, $pageSet, 'pagelinks',
529 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
530 array( 'page_id=pl_from' )
531 );
532 $inputPages = array_keys( $pageSet );
533 }
534
535 return $pageSet;
536 }
537
538 /**
539 * Expand a list of pages to include items used in those pages.
540 * @param array $inputPages Array of page titles
541 * @param array $pageSet
542 * @param string $table
543 * @param array $fields Array of field names
544 * @param array $join
545 * @return array
546 */
547 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
548 $dbr = wfGetDB( DB_SLAVE );
549
550 foreach ( $inputPages as $page ) {
551 $title = Title::newFromText( $page );
552
553 if ( $title ) {
554 $pageSet[$title->getPrefixedText()] = true;
555 /// @todo FIXME: May or may not be more efficient to batch these
556 /// by namespace when given multiple input pages.
557 $result = $dbr->select(
558 array( 'page', $table ),
559 $fields,
560 array_merge(
561 $join,
562 array(
563 'page_namespace' => $title->getNamespace(),
564 'page_title' => $title->getDBkey()
565 )
566 ),
567 __METHOD__
568 );
569
570 foreach ( $result as $row ) {
571 $template = Title::makeTitle( $row->namespace, $row->title );
572 $pageSet[$template->getPrefixedText()] = true;
573 }
574 }
575 }
576
577 return $pageSet;
578 }
579
580 protected function getGroupName() {
581 return 'pagetools';
582 }
583 }