MediaWiki REL1_33
SpecialExport.php
Go to the documentation of this file.
1<?php
27
35
36 public function __construct() {
37 parent::__construct( 'Export' );
38 }
39
40 public function execute( $par ) {
41 $this->setHeaders();
42 $this->outputHeader();
43 $config = $this->getConfig();
44
45 // Set some variables
46 $this->curonly = true;
47 $this->doExport = false;
48 $request = $this->getRequest();
49 $this->templates = $request->getCheck( 'templates' );
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 ) {
68 $catpages = $this->getPagesFromCategory( $t );
69 if ( $catpages ) {
70 if ( $page !== '' ) {
71 $page .= "\n";
72 }
73 $page .= implode( "\n", $catpages );
74 }
75 }
76 }
77 } elseif ( $request->getCheck( 'addns' ) && $config->get( 'ExportFromNamespaces' ) ) {
78 $page = $request->getText( 'pages' );
79 $nsindex = $request->getText( 'nsindex', '' );
80
81 if ( strval( $nsindex ) !== '' ) {
85 $nspages = $this->getPagesFromNamespace( $nsindex );
86 if ( $nspages ) {
87 $page .= "\n" . implode( "\n", $nspages );
88 }
89 }
90 } elseif ( $request->getCheck( 'exportall' ) && $config->get( 'ExportAllowAll' ) ) {
91 $this->doExport = true;
92 $exportall = true;
93
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 // Log to see if certain parameters are actually used.
102 // If not, we could deprecate them and do some cleanup, here and in WikiExporter.
103 LoggerFactory::getInstance( 'export' )->debug(
104 'Special:Export POST, dir: [{dir}], offset: [{offset}], limit: [{limit}]', [
105 'dir' => $request->getRawVal( 'dir' ),
106 'offset' => $request->getRawVal( 'offset' ),
107 'limit' => $request->getRawVal( 'limit' ),
108 ] );
109
110 $page = $request->getText( 'pages' );
111 $this->curonly = $request->getCheck( 'curonly' );
112 $rawOffset = $request->getVal( 'offset' );
113
114 if ( $rawOffset ) {
115 $offset = wfTimestamp( TS_MW, $rawOffset );
116 } else {
117 $offset = null;
118 }
119
120 $maxHistory = $config->get( 'ExportMaxHistory' );
121 $limit = $request->getInt( 'limit' );
122 $dir = $request->getVal( 'dir' );
123 $history = [
124 'dir' => 'asc',
125 'offset' => false,
126 'limit' => $maxHistory,
127 ];
128 $historyCheck = $request->getCheck( 'history' );
129
130 if ( $this->curonly ) {
131 $history = WikiExporter::CURRENT;
132 } elseif ( !$historyCheck ) {
133 if ( $limit > 0 && ( $maxHistory == 0 || $limit < $maxHistory ) ) {
134 $history['limit'] = $limit;
135 }
136
137 if ( !is_null( $offset ) ) {
138 $history['offset'] = $offset;
139 }
140
141 if ( strtolower( $dir ) == 'desc' ) {
142 $history['dir'] = 'desc';
143 }
144 }
145
146 if ( $page != '' ) {
147 $this->doExport = true;
148 }
149 } else {
150 // Default to current-only for GET requests.
151 $page = $request->getText( 'pages', $par );
152 $historyCheck = $request->getCheck( 'history' );
153
154 if ( $historyCheck ) {
155 $history = WikiExporter::FULL;
156 } else {
157 $history = WikiExporter::CURRENT;
158 }
159
160 if ( $page != '' ) {
161 $this->doExport = true;
162 }
163 }
164
165 if ( !$config->get( 'ExportAllowHistory' ) ) {
166 // Override
167 $history = WikiExporter::CURRENT;
168 }
169
170 $list_authors = $request->getCheck( 'listauthors' );
171 if ( !$this->curonly || !$config->get( 'ExportAllowListContributors' ) ) {
172 $list_authors = false;
173 }
174
175 if ( $this->doExport ) {
176 $this->getOutput()->disable();
177
178 // Cancel output buffering and gzipping if set
179 // This should provide safer streaming for pages with history
181 $request->response()->header( "Content-type: application/xml; charset=utf-8" );
182 $request->response()->header( "X-Robots-Tag: noindex,nofollow" );
183
184 if ( $request->getCheck( 'wpDownload' ) ) {
185 // Provide a sane filename suggestion
186 $filename = urlencode( $config->get( 'Sitename' ) . '-' . wfTimestampNow() . '.xml' );
187 $request->response()->header( "Content-disposition: attachment;filename={$filename}" );
188 }
189
190 $this->doExport( $page, $history, $list_authors, $exportall );
191
192 return;
193 }
194
195 $out = $this->getOutput();
196 $out->addWikiMsg( 'exporttext' );
197
198 if ( $page == '' ) {
199 $categoryName = $request->getText( 'catname' );
200 } else {
201 $categoryName = '';
202 }
203
205 'catname' => [
206 'type' => 'textwithbutton',
207 'name' => 'catname',
208 'horizontal-label' => true,
209 'label-message' => 'export-addcattext',
210 'default' => $categoryName,
211 'size' => 40,
212 'buttontype' => 'submit',
213 'buttonname' => 'addcat',
214 'buttondefault' => $this->msg( 'export-addcat' )->text(),
215 'hide-if' => [ '===', 'exportall', '1' ],
216 ],
217 ];
218 if ( $config->get( 'ExportFromNamespaces' ) ) {
219 $formDescriptor += [
220 'nsindex' => [
221 'type' => 'namespaceselectwithbutton',
222 'default' => $nsindex,
223 'label-message' => 'export-addnstext',
224 'horizontal-label' => true,
225 'name' => 'nsindex',
226 'id' => 'namespace',
227 'cssclass' => 'namespaceselector',
228 'buttontype' => 'submit',
229 'buttonname' => 'addns',
230 'buttondefault' => $this->msg( 'export-addns' )->text(),
231 'hide-if' => [ '===', 'exportall', '1' ],
232 ],
233 ];
234 }
235
236 if ( $config->get( 'ExportAllowAll' ) ) {
237 $formDescriptor += [
238 'exportall' => [
239 'type' => 'check',
240 'label-message' => 'exportall',
241 'name' => 'exportall',
242 'id' => 'exportall',
243 'default' => $request->wasPosted() ? $request->getCheck( 'exportall' ) : false,
244 ],
245 ];
246 }
247
248 $formDescriptor += [
249 'textarea' => [
250 'class' => HTMLTextAreaField::class,
251 'name' => 'pages',
252 'label-message' => 'export-manual',
253 'nodata' => true,
254 'rows' => 10,
255 'default' => $page,
256 'hide-if' => [ '===', 'exportall', '1' ],
257 ],
258 ];
259
260 if ( $config->get( 'ExportAllowHistory' ) ) {
261 $formDescriptor += [
262 'curonly' => [
263 'type' => 'check',
264 'label-message' => 'exportcuronly',
265 'name' => 'curonly',
266 'id' => 'curonly',
267 'default' => $request->wasPosted() ? $request->getCheck( 'curonly' ) : true,
268 ],
269 ];
270 } else {
271 $out->addWikiMsg( 'exportnohistory' );
272 }
273
274 $formDescriptor += [
275 'templates' => [
276 'type' => 'check',
277 'label-message' => 'export-templates',
278 'name' => 'templates',
279 'id' => 'wpExportTemplates',
280 'default' => $request->wasPosted() ? $request->getCheck( 'templates' ) : false,
281 ],
282 ];
283
284 if ( $config->get( 'ExportMaxLinkDepth' ) || $this->userCanOverrideExportDepth() ) {
285 $formDescriptor += [
286 'pagelink-depth' => [
287 'type' => 'text',
288 'name' => 'pagelink-depth',
289 'id' => 'pagelink-depth',
290 'label-message' => 'export-pagelinks',
291 'default' => '0',
292 'size' => 20,
293 ],
294 ];
295 }
296
297 $formDescriptor += [
298 'wpDownload' => [
299 'type' => 'check',
300 'name' => 'wpDownload',
301 'id' => 'wpDownload',
302 'default' => $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true,
303 'label-message' => 'export-download',
304 ],
305 ];
306
307 if ( $config->get( 'ExportAllowListContributors' ) ) {
308 $formDescriptor += [
309 'listauthors' => [
310 'type' => 'check',
311 'label-message' => 'exportlistauthors',
312 'default' => $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false,
313 'name' => 'listauthors',
314 'id' => 'listauthors',
315 ],
316 ];
317 }
318
319 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
320 $htmlForm->setSubmitTextMsg( 'export-submit' );
321 $htmlForm->prepareForm()->displayForm( false );
322 $this->addHelpLink( 'Help:Export' );
323 }
324
328 private function userCanOverrideExportDepth() {
329 return $this->getUser()->isAllowed( 'override-export-depth' );
330 }
331
341 private function doExport( $page, $history, $list_authors, $exportall ) {
342 // If we are grabbing everything, enable full history and ignore the rest
343 if ( $exportall ) {
344 $history = WikiExporter::FULL;
345 } else {
346 $pageSet = []; // Inverted index of all pages to look up
347
348 // Split up and normalize input
349 foreach ( explode( "\n", $page ) as $pageName ) {
350 $pageName = trim( $pageName );
351 $title = Title::newFromText( $pageName );
352 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
353 // Only record each page once!
354 $pageSet[$title->getPrefixedText()] = true;
355 }
356 }
357
358 // Set of original pages to pass on to further manipulation...
359 $inputPages = array_keys( $pageSet );
360
361 // Look up any linked pages if asked...
362 if ( $this->templates ) {
363 $pageSet = $this->getTemplates( $inputPages, $pageSet );
364 }
365 $linkDepth = $this->pageLinkDepth;
366 if ( $linkDepth ) {
367 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
368 }
369
370 $pages = array_keys( $pageSet );
371
372 // Normalize titles to the same format and remove dupes, see T19374
373 foreach ( $pages as $k => $v ) {
374 $pages[$k] = str_replace( " ", "_", $v );
375 }
376
377 $pages = array_unique( $pages );
378 }
379
380 /* Ok, let's get to it... */
381 $db = wfGetDB( DB_REPLICA );
382
383 $exporter = new WikiExporter( $db, $history );
384 $exporter->list_authors = $list_authors;
385 $exporter->openStream();
386
387 if ( $exportall ) {
388 $exporter->allPages();
389 } else {
390 foreach ( $pages as $page ) {
391 # T10824: Only export pages the user can read
392 $title = Title::newFromText( $page );
393 if ( is_null( $title ) ) {
394 // @todo Perhaps output an <error> tag or something.
395 continue;
396 }
397
398 if ( !$title->userCan( 'read', $this->getUser() ) ) {
399 // @todo Perhaps output an <error> tag or something.
400 continue;
401 }
402
403 $exporter->pageByTitle( $title );
404 }
405 }
406
407 $exporter->closeStream();
408 }
409
414 private function getPagesFromCategory( $title ) {
415 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
416
417 $name = $title->getDBkey();
418
420 $res = $dbr->select(
421 [ 'page', 'categorylinks' ],
422 [ 'page_namespace', 'page_title' ],
423 [ 'cl_from=page_id', 'cl_to' => $name ],
424 __METHOD__,
425 [ 'LIMIT' => $maxPages ]
426 );
427
428 $pages = [];
429
430 foreach ( $res as $row ) {
431 $pages[] = Title::makeName( $row->page_namespace, $row->page_title );
432 }
433
434 return $pages;
435 }
436
441 private function getPagesFromNamespace( $nsindex ) {
442 $maxPages = $this->getConfig()->get( 'ExportPagelistLimit' );
443
445 $res = $dbr->select(
446 'page',
447 [ 'page_namespace', 'page_title' ],
448 [ 'page_namespace' => $nsindex ],
449 __METHOD__,
450 [ 'LIMIT' => $maxPages ]
451 );
452
453 $pages = [];
454
455 foreach ( $res as $row ) {
456 $pages[] = Title::makeName( $row->page_namespace, $row->page_title );
457 }
458
459 return $pages;
460 }
461
468 private function getTemplates( $inputPages, $pageSet ) {
469 return $this->getLinks( $inputPages, $pageSet,
470 'templatelinks',
471 [ 'namespace' => 'tl_namespace', 'title' => 'tl_title' ],
472 [ 'page_id=tl_from' ]
473 );
474 }
475
481 private function validateLinkDepth( $depth ) {
482 if ( $depth < 0 ) {
483 return 0;
484 }
485
486 if ( !$this->userCanOverrideExportDepth() ) {
487 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
488 if ( $depth > $maxLinkDepth ) {
489 return $maxLinkDepth;
490 }
491 }
492
493 /*
494 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
495 * crazy-big export from being done by someone setting the depth
496 * number too high. In other words, last resort safety net.
497 */
498
499 return intval( min( $depth, 5 ) );
500 }
501
509 private function getPageLinks( $inputPages, $pageSet, $depth ) {
510 for ( ; $depth > 0; --$depth ) {
511 $pageSet = $this->getLinks(
512 $inputPages, $pageSet, 'pagelinks',
513 [ 'namespace' => 'pl_namespace', 'title' => 'pl_title' ],
514 [ 'page_id=pl_from' ]
515 );
516 $inputPages = array_keys( $pageSet );
517 }
518
519 return $pageSet;
520 }
521
531 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
533
534 foreach ( $inputPages as $page ) {
535 $title = Title::newFromText( $page );
536
537 if ( $title ) {
538 $pageSet[$title->getPrefixedText()] = true;
541 $result = $dbr->select(
542 [ 'page', $table ],
543 $fields,
545 $join,
546 [
547 'page_namespace' => $title->getNamespace(),
548 'page_title' => $title->getDBkey()
549 ]
550 ),
551 __METHOD__
552 );
553
554 foreach ( $result as $row ) {
555 $template = Title::makeTitle( $row->namespace, $row->title );
556 $pageSet[$template->getPrefixedText()] = true;
557 }
558 }
559 }
560
561 return $pageSet;
562 }
563
564 protected function getGroupName() {
565 return 'pagetools';
566 }
567}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfResetOutputBuffers( $resetGzipEncoding=true)
Clear away any user-level output buffers, discarding contents.
PSR-3 logger instance factory.
A special page that allows users to export pages in a XML file.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
doExport( $page, $history, $list_authors, $exportall)
Do the actual page exporting.
getPageLinks( $inputPages, $pageSet, $depth)
Expand a list of pages to include pages linked to from that page.
getLinks( $inputPages, $pageSet, $table, $fields, $join)
Expand a list of pages to include items used in those pages.
getPagesFromNamespace( $nsindex)
execute( $par)
Default execute method Checks user permissions.
validateLinkDepth( $depth)
Validate link depth setting, if available.
getTemplates( $inputPages, $pageSet)
Expand a list of pages to include templates used in those pages.
getPagesFromCategory( $title)
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
$res
Definition database.txt:21
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2843
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping $template
Definition hooks.txt:822
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:855
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead & $formDescriptor
Definition hooks.txt:2157
const NS_MAIN
Definition Defines.php:73
const DB_REPLICA
Definition defines.php:25