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