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