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