Merge "Add help link for 8 more special pages"
[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 $form = Xml::openElement( 'form', array( 'method' => 'post',
192 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ) ) );
193 $form .= Xml::inputLabel(
194 $this->msg( 'export-addcattext' )->text(),
195 'catname',
196 'catname',
197 40,
198 $categoryName
199 ) . '&#160;';
200 $form .= Xml::submitButton(
201 $this->msg( 'export-addcat' )->text(),
202 array( 'name' => 'addcat' )
203 ) . '<br />';
204
205 if ( $config->get( 'ExportFromNamespaces' ) ) {
206 $form .= Html::namespaceSelector(
207 array(
208 'selected' => $nsindex,
209 'label' => $this->msg( 'export-addnstext' )->text()
210 ), array(
211 'name' => 'nsindex',
212 'id' => 'namespace',
213 'class' => 'namespaceselector',
214 )
215 ) . '&#160;';
216 $form .= Xml::submitButton(
217 $this->msg( 'export-addns' )->text(),
218 array( 'name' => 'addns' )
219 ) . '<br />';
220 }
221
222 if ( $config->get( 'ExportAllowAll' ) ) {
223 $form .= Xml::checkLabel(
224 $this->msg( 'exportall' )->text(),
225 'exportall',
226 'exportall',
227 $request->wasPosted() ? $request->getCheck( 'exportall' ) : false
228 ) . '<br />';
229 }
230
231 $form .= Xml::element(
232 'textarea',
233 array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ),
234 $page,
235 false
236 );
237 $form .= '<br />';
238
239 if ( $config->get( 'ExportAllowHistory' ) ) {
240 $form .= Xml::checkLabel(
241 $this->msg( 'exportcuronly' )->text(),
242 'curonly',
243 'curonly',
244 $request->wasPosted() ? $request->getCheck( 'curonly' ) : true
245 ) . '<br />';
246 } else {
247 $out->addWikiMsg( 'exportnohistory' );
248 }
249
250 $form .= Xml::checkLabel(
251 $this->msg( 'export-templates' )->text(),
252 'templates',
253 'wpExportTemplates',
254 $request->wasPosted() ? $request->getCheck( 'templates' ) : false
255 ) . '<br />';
256
257 if ( $config->get( 'ExportMaxLinkDepth' ) || $this->userCanOverrideExportDepth() ) {
258 $form .= Xml::inputLabel(
259 $this->msg( 'export-pagelinks' )->text(),
260 'pagelink-depth',
261 'pagelink-depth',
262 20,
263 0
264 ) . '<br />';
265 }
266
267 /* Enable this when we can do something useful exporting/importing image information.
268 $form .= Xml::checkLabel(
269 $this->msg( 'export-images' )->text(),
270 'images',
271 'wpExportImages',
272 false
273 ) . '<br />';
274 */
275 $form .= Xml::checkLabel(
276 $this->msg( 'export-download' )->text(),
277 'wpDownload',
278 'wpDownload',
279 $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true
280 ) . '<br />';
281
282 if ( $config->get( 'ExportAllowListContributors' ) ) {
283 $form .= Xml::checkLabel(
284 $this->msg( 'exportlistauthors' )->text(),
285 'listauthors',
286 'listauthors',
287 $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false
288 ) . '<br />';
289 }
290
291 $form .= Xml::submitButton(
292 $this->msg( 'export-submit' )->text(),
293 Linker::tooltipAndAccesskeyAttribs( 'export' )
294 );
295 $form .= Xml::closeElement( 'form' );
296
297 $out->addHTML( $form );
298 $this->addHelpLink( 'Help:Export' );
299 }
300
301 /**
302 * @return bool
303 */
304 private function userCanOverrideExportDepth() {
305 return $this->getUser()->isAllowed( 'override-export-depth' );
306 }
307
308 /**
309 * Do the actual page exporting
310 *
311 * @param string $page User input on what page(s) to export
312 * @param int $history One of the WikiExporter history export constants
313 * @param bool $list_authors Whether to add distinct author list (when
314 * not returning full history)
315 * @param bool $exportall Whether to export everything
316 */
317 private function doExport( $page, $history, $list_authors, $exportall ) {
318
319 // If we are grabbing everything, enable full history and ignore the rest
320 if ( $exportall ) {
321 $history = WikiExporter::FULL;
322 } else {
323
324 $pageSet = array(); // Inverted index of all pages to look up
325
326 // Split up and normalize input
327 foreach ( explode( "\n", $page ) as $pageName ) {
328 $pageName = trim( $pageName );
329 $title = Title::newFromText( $pageName );
330 if ( $title && !$title->isExternal() && $title->getText() !== '' ) {
331 // Only record each page once!
332 $pageSet[$title->getPrefixedText()] = true;
333 }
334 }
335
336 // Set of original pages to pass on to further manipulation...
337 $inputPages = array_keys( $pageSet );
338
339 // Look up any linked pages if asked...
340 if ( $this->templates ) {
341 $pageSet = $this->getTemplates( $inputPages, $pageSet );
342 }
343 $linkDepth = $this->pageLinkDepth;
344 if ( $linkDepth ) {
345 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
346 }
347
348 // Enable this when we can do something useful exporting/importing image information.
349 // if( $this->images ) ) {
350 // $pageSet = $this->getImages( $inputPages, $pageSet );
351 // }
352
353 $pages = array_keys( $pageSet );
354
355 // Normalize titles to the same format and remove dupes, see bug 17374
356 foreach ( $pages as $k => $v ) {
357 $pages[$k] = str_replace( " ", "_", $v );
358 }
359
360 $pages = array_unique( $pages );
361 }
362
363 /* Ok, let's get to it... */
364 if ( $history == WikiExporter::CURRENT ) {
365 $lb = false;
366 $db = wfGetDB( DB_SLAVE );
367 $buffer = WikiExporter::BUFFER;
368 } else {
369 // Use an unbuffered query; histories may be very long!
370 $lb = wfGetLBFactory()->newMainLB();
371 $db = $lb->getConnection( DB_SLAVE );
372 $buffer = WikiExporter::STREAM;
373
374 // This might take a while... :D
375 wfSuppressWarnings();
376 set_time_limit( 0 );
377 wfRestoreWarnings();
378 }
379
380 $exporter = new WikiExporter( $db, $history, $buffer );
381 $exporter->list_authors = $list_authors;
382 $exporter->openStream();
383
384 if ( $exportall ) {
385 $exporter->allPages();
386 } else {
387 foreach ( $pages as $page ) {
388 #Bug 8824: Only export pages the user can read
389 $title = Title::newFromText( $page );
390 if ( is_null( $title ) ) {
391 // @todo Perhaps output an <error> tag or something.
392 continue;
393 }
394
395 if ( !$title->userCan( 'read', $this->getUser() ) ) {
396 // @todo Perhaps output an <error> tag or something.
397 continue;
398 }
399
400 $exporter->pageByTitle( $title );
401 }
402 }
403
404 $exporter->closeStream();
405
406 if ( $lb ) {
407 $lb->closeAll();
408 }
409 }
410
411 /**
412 * @param Title $title
413 * @return array
414 */
415 private function getPagesFromCategory( $title ) {
416 global $wgContLang;
417
418 $name = $title->getDBkey();
419
420 $dbr = wfGetDB( DB_SLAVE );
421 $res = $dbr->select(
422 array( 'page', 'categorylinks' ),
423 array( 'page_namespace', 'page_title' ),
424 array( 'cl_from=page_id', 'cl_to' => $name ),
425 __METHOD__,
426 array( 'LIMIT' => '5000' )
427 );
428
429 $pages = array();
430
431 foreach ( $res as $row ) {
432 $n = $row->page_title;
433 if ( $row->page_namespace ) {
434 $ns = $wgContLang->getNsText( $row->page_namespace );
435 $n = $ns . ':' . $n;
436 }
437
438 $pages[] = $n;
439 }
440
441 return $pages;
442 }
443
444 /**
445 * @param int $nsindex
446 * @return array
447 */
448 private function getPagesFromNamespace( $nsindex ) {
449 global $wgContLang;
450
451 $dbr = wfGetDB( DB_SLAVE );
452 $res = $dbr->select(
453 'page',
454 array( 'page_namespace', 'page_title' ),
455 array( 'page_namespace' => $nsindex ),
456 __METHOD__,
457 array( 'LIMIT' => '5000' )
458 );
459
460 $pages = array();
461
462 foreach ( $res as $row ) {
463 $n = $row->page_title;
464
465 if ( $row->page_namespace ) {
466 $ns = $wgContLang->getNsText( $row->page_namespace );
467 $n = $ns . ':' . $n;
468 }
469
470 $pages[] = $n;
471 }
472
473 return $pages;
474 }
475
476 /**
477 * Expand a list of pages to include templates used in those pages.
478 * @param array $inputPages List of titles to look up
479 * @param array $pageSet Associative array indexed by titles for output
480 * @return array Associative array index by titles
481 */
482 private function getTemplates( $inputPages, $pageSet ) {
483 return $this->getLinks( $inputPages, $pageSet,
484 'templatelinks',
485 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
486 array( 'page_id=tl_from' )
487 );
488 }
489
490 /**
491 * Validate link depth setting, if available.
492 * @param int $depth
493 * @return int
494 */
495 private function validateLinkDepth( $depth ) {
496 if ( $depth < 0 ) {
497 return 0;
498 }
499
500 if ( !$this->userCanOverrideExportDepth() ) {
501 $maxLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' );
502 if ( $depth > $maxLinkDepth ) {
503 return $maxLinkDepth;
504 }
505 }
506
507 /*
508 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
509 * crazy-big export from being done by someone setting the depth
510 * number too high. In other words, last resort safety net.
511 */
512
513 return intval( min( $depth, 5 ) );
514 }
515
516 /**
517 * Expand a list of pages to include pages linked to from that page.
518 * @param array $inputPages
519 * @param array $pageSet
520 * @param int $depth
521 * @return array
522 */
523 private function getPageLinks( $inputPages, $pageSet, $depth ) {
524 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
525 for ( ; $depth > 0; --$depth ) {
526 // @codingStandardsIgnoreEnd
527 $pageSet = $this->getLinks(
528 $inputPages, $pageSet, 'pagelinks',
529 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
530 array( 'page_id=pl_from' )
531 );
532 $inputPages = array_keys( $pageSet );
533 }
534
535 return $pageSet;
536 }
537
538 /**
539 * Expand a list of pages to include images used in those pages.
540 *
541 * @param array $inputPages List of titles to look up
542 * @param array $pageSet Associative array indexed by titles for output
543 *
544 * @return array Associative array index by titles
545 */
546 private function getImages( $inputPages, $pageSet ) {
547 return $this->getLinks(
548 $inputPages,
549 $pageSet,
550 'imagelinks',
551 array( 'namespace' => NS_FILE, 'title' => 'il_to' ),
552 array( 'page_id=il_from' )
553 );
554 }
555
556 /**
557 * Expand a list of pages to include items used in those pages.
558 * @param array $inputPages Array of page titles
559 * @param array $pageSet
560 * @param string $table
561 * @param array $fields Array of field names
562 * @param array $join
563 * @return array
564 */
565 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
566 $dbr = wfGetDB( DB_SLAVE );
567
568 foreach ( $inputPages as $page ) {
569 $title = Title::newFromText( $page );
570
571 if ( $title ) {
572 $pageSet[$title->getPrefixedText()] = true;
573 /// @todo FIXME: May or may not be more efficient to batch these
574 /// by namespace when given multiple input pages.
575 $result = $dbr->select(
576 array( 'page', $table ),
577 $fields,
578 array_merge(
579 $join,
580 array(
581 'page_namespace' => $title->getNamespace(),
582 'page_title' => $title->getDBkey()
583 )
584 ),
585 __METHOD__
586 );
587
588 foreach ( $result as $row ) {
589 $template = Title::makeTitle( $row->namespace, $row->title );
590 $pageSet[$template->getPrefixedText()] = true;
591 }
592 }
593 }
594
595 return $pageSet;
596 }
597
598 protected function getGroupName() {
599 return 'pagetools';
600 }
601 }