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