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