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