merging latest master
[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
33 private $curonly, $doExport, $pageLinkDepth, $templates;
34 private $images;
35
36 public function __construct() {
37 parent::__construct( 'Export' );
38 }
39
40 public function execute( $par ) {
41 global $wgSitename, $wgExportAllowListContributors, $wgExportFromNamespaces;
42 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
43 global $wgExportAllowAll;
44
45 $this->setHeaders();
46 $this->outputHeader();
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 }
79 elseif( $request->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
80 $page = $request->getText( 'pages' );
81 $nsindex = $request->getText( 'nsindex', '' );
82
83 if ( strval( $nsindex ) !== '' ) {
84 /**
85 * Same implementation as above, so same @todo
86 */
87 $nspages = $this->getPagesFromNamespace( $nsindex );
88 if ( $nspages ) {
89 $page .= "\n" . implode( "\n", $nspages );
90 }
91 }
92 }
93 elseif( $request->getCheck( 'exportall' ) && $wgExportAllowAll ) {
94 $this->doExport = true;
95 $exportall = true;
96
97 /* Although $page and $history are not used later on, we
98 nevertheless set them to avoid that PHP notices about using
99 undefined variables foul up our XML output (see call to
100 doExport(...) further down) */
101 $page = '';
102 $history = '';
103 }
104 elseif( $request->wasPosted() && $par == '' ) {
105 $page = $request->getText( 'pages' );
106 $this->curonly = $request->getCheck( 'curonly' );
107 $rawOffset = $request->getVal( 'offset' );
108
109 if( $rawOffset ) {
110 $offset = wfTimestamp( TS_MW, $rawOffset );
111 } else {
112 $offset = null;
113 }
114
115 $limit = $request->getInt( 'limit' );
116 $dir = $request->getVal( 'dir' );
117 $history = array(
118 'dir' => 'asc',
119 'offset' => false,
120 'limit' => $wgExportMaxHistory,
121 );
122 $historyCheck = $request->getCheck( 'history' );
123
124 if ( $this->curonly ) {
125 $history = WikiExporter::CURRENT;
126 } elseif ( !$historyCheck ) {
127 if ( $limit > 0 && ($wgExportMaxHistory == 0 || $limit < $wgExportMaxHistory ) ) {
128 $history['limit'] = $limit;
129 }
130 if ( !is_null( $offset ) ) {
131 $history['offset'] = $offset;
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( !$wgExportAllowHistory ) {
158 // Override
159 $history = WikiExporter::CURRENT;
160 }
161
162 $list_authors = $request->getCheck( 'listauthors' );
163 if ( !$this->curonly || !$wgExportAllowListContributors ) {
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( $wgSitename . '-' . 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->getTitle()->getLocalUrl( 'action=submit' ) ) );
191 $form .= Xml::inputLabel( $this->msg( 'export-addcattext' )->text(), 'catname', 'catname', 40 ) . '&#160;';
192 $form .= Xml::submitButton( $this->msg( 'export-addcat' )->text(), array( 'name' => 'addcat' ) ) . '<br />';
193
194 if ( $wgExportFromNamespaces ) {
195 $form .= Html::namespaceSelector(
196 array(
197 'selected' => $nsindex,
198 'label' => $this->msg( 'export-addnstext' )->text()
199 ), array(
200 'name' => 'nsindex',
201 'id' => 'namespace',
202 'class' => 'namespaceselector',
203 )
204 ) . '&#160;';
205 $form .= Xml::submitButton( $this->msg( 'export-addns' )->text(), array( 'name' => 'addns' ) ) . '<br />';
206 }
207
208 if ( $wgExportAllowAll ) {
209 $form .= Xml::checkLabel(
210 $this->msg( 'exportall' )->text(),
211 'exportall',
212 'exportall',
213 $request->wasPosted() ? $request->getCheck( 'exportall' ) : false
214 ) . '<br />';
215 }
216
217 $form .= Xml::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
218 $form .= '<br />';
219
220 if( $wgExportAllowHistory ) {
221 $form .= Xml::checkLabel(
222 $this->msg( 'exportcuronly' )->text(),
223 'curonly',
224 'curonly',
225 $request->wasPosted() ? $request->getCheck( 'curonly' ) : true
226 ) . '<br />';
227 } else {
228 $out->addWikiMsg( 'exportnohistory' );
229 }
230
231 $form .= Xml::checkLabel(
232 $this->msg( 'export-templates' )->text(),
233 'templates',
234 'wpExportTemplates',
235 $request->wasPosted() ? $request->getCheck( 'templates' ) : false
236 ) . '<br />';
237
238 if( $wgExportMaxLinkDepth || $this->userCanOverrideExportDepth() ) {
239 $form .= Xml::inputLabel( $this->msg( 'export-pagelinks' )->text(), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
240 }
241 // Enable this when we can do something useful exporting/importing image information. :)
242 //$form .= Xml::checkLabel( $this->msg( 'export-images' )->text(), 'images', 'wpExportImages', false ) . '<br />';
243 $form .= Xml::checkLabel(
244 $this->msg( 'export-download' )->text(),
245 'wpDownload',
246 'wpDownload',
247 $request->wasPosted() ? $request->getCheck( 'wpDownload' ) : true
248 ) . '<br />';
249
250 if ( $wgExportAllowListContributors ) {
251 $form .= Xml::checkLabel(
252 $this->msg( 'exportlistauthors' )->text(),
253 'listauthors',
254 'listauthors',
255 $request->wasPosted() ? $request->getCheck( 'listauthors' ) : false
256 ) . '<br />';
257 }
258
259 $form .= Xml::submitButton( $this->msg( 'export-submit' )->text(), Linker::tooltipAndAccesskeyAttribs( 'export' ) );
260 $form .= Xml::closeElement( 'form' );
261
262 $out->addHTML( $form );
263 }
264
265 /**
266 * @return bool
267 */
268 private function userCanOverrideExportDepth() {
269 return $this->getUser()->isAllowed( 'override-export-depth' );
270 }
271
272 /**
273 * Do the actual page exporting
274 *
275 * @param $page String: user input on what page(s) to export
276 * @param $history Mixed: one of the WikiExporter history export constants
277 * @param $list_authors Boolean: Whether to add distinct author list (when
278 * not returning full history)
279 * @param $exportall Boolean: Whether to export everything
280 */
281 private function doExport( $page, $history, $list_authors, $exportall ) {
282
283 // If we are grabbing everything, enable full history and ignore the rest
284 if ( $exportall ) {
285 $history = WikiExporter::FULL;
286 } else {
287
288 $pageSet = array(); // Inverted index of all pages to look up
289
290 // Split up and normalize input
291 foreach( explode( "\n", $page ) as $pageName ) {
292 $pageName = trim( $pageName );
293 $title = Title::newFromText( $pageName );
294 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
295 // Only record each page once!
296 $pageSet[$title->getPrefixedText()] = true;
297 }
298 }
299
300 // Set of original pages to pass on to further manipulation...
301 $inputPages = array_keys( $pageSet );
302
303 // Look up any linked pages if asked...
304 if( $this->templates ) {
305 $pageSet = $this->getTemplates( $inputPages, $pageSet );
306 }
307 $linkDepth = $this->pageLinkDepth;
308 if( $linkDepth ) {
309 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
310 }
311
312 /*
313 // Enable this when we can do something useful exporting/importing image information. :)
314 if( $this->images ) ) {
315 $pageSet = $this->getImages( $inputPages, $pageSet );
316 }
317 */
318
319 $pages = array_keys( $pageSet );
320
321 // Normalize titles to the same format and remove dupes, see bug 17374
322 foreach( $pages as $k => $v ) {
323 $pages[$k] = str_replace( " ", "_", $v );
324 }
325
326 $pages = array_unique( $pages );
327 }
328
329 /* Ok, let's get to it... */
330 if( $history == WikiExporter::CURRENT ) {
331 $lb = false;
332 $db = wfGetDB( DB_SLAVE );
333 $buffer = WikiExporter::BUFFER;
334 } else {
335 // Use an unbuffered query; histories may be very long!
336 $lb = wfGetLBFactory()->newMainLB();
337 $db = $lb->getConnection( DB_SLAVE );
338 $buffer = WikiExporter::STREAM;
339
340 // This might take a while... :D
341 wfSuppressWarnings();
342 set_time_limit(0);
343 wfRestoreWarnings();
344 }
345
346 $exporter = new WikiExporter( $db, $history, $buffer );
347 $exporter->list_authors = $list_authors;
348 $exporter->openStream();
349
350 if ( $exportall ) {
351 $exporter->allPages();
352 } else {
353 foreach( $pages as $page ) {
354 /*
355 if( $wgExportMaxHistory && !$this->curonly ) {
356 $title = Title::newFromText( $page );
357 if( $title ) {
358 $count = Revision::countByTitle( $db, $title );
359 if( $count > $wgExportMaxHistory ) {
360 wfDebug( __FUNCTION__ .
361 ": Skipped $page, $count revisions too big\n" );
362 continue;
363 }
364 }
365 }*/
366 #Bug 8824: Only export pages the user can read
367 $title = Title::newFromText( $page );
368 if( is_null( $title ) ) {
369 continue; #TODO: perhaps output an <error> tag or something.
370 }
371 if( !$title->userCan( 'read', $this->getUser() ) ) {
372 continue; #TODO: perhaps output an <error> tag or something.
373 }
374
375 $exporter->pageByTitle( $title );
376 }
377 }
378
379 $exporter->closeStream();
380
381 if( $lb ) {
382 $lb->closeAll();
383 }
384 }
385
386 /**
387 * @param $title Title
388 * @return array
389 */
390 private function getPagesFromCategory( $title ) {
391 global $wgContLang;
392
393 $name = $title->getDBkey();
394
395 $dbr = wfGetDB( DB_SLAVE );
396 $res = $dbr->select(
397 array( 'page', 'categorylinks' ),
398 array( 'page_namespace', 'page_title' ),
399 array( 'cl_from=page_id', 'cl_to' => $name ),
400 __METHOD__,
401 array( 'LIMIT' => '5000' )
402 );
403
404 $pages = array();
405
406 foreach ( $res as $row ) {
407 $n = $row->page_title;
408 if ($row->page_namespace) {
409 $ns = $wgContLang->getNsText( $row->page_namespace );
410 $n = $ns . ':' . $n;
411 }
412
413 $pages[] = $n;
414 }
415 return $pages;
416 }
417
418 /**
419 * @param $nsindex int
420 * @return array
421 */
422 private function getPagesFromNamespace( $nsindex ) {
423 global $wgContLang;
424
425 $dbr = wfGetDB( DB_SLAVE );
426 $res = $dbr->select(
427 'page',
428 array( 'page_namespace', 'page_title' ),
429 array( 'page_namespace' => $nsindex ),
430 __METHOD__,
431 array( 'LIMIT' => '5000' )
432 );
433
434 $pages = array();
435
436 foreach ( $res as $row ) {
437 $n = $row->page_title;
438
439 if ( $row->page_namespace ) {
440 $ns = $wgContLang->getNsText( $row->page_namespace );
441 $n = $ns . ':' . $n;
442 }
443
444 $pages[] = $n;
445 }
446 return $pages;
447 }
448
449 /**
450 * Expand a list of pages to include templates used in those pages.
451 * @param $inputPages array, list of titles to look up
452 * @param $pageSet array, associative array indexed by titles for output
453 * @return array associative array index by titles
454 */
455 private function getTemplates( $inputPages, $pageSet ) {
456 return $this->getLinks( $inputPages, $pageSet,
457 'templatelinks',
458 array( 'namespace' => 'tl_namespace', 'title' => 'tl_title' ),
459 array( 'page_id=tl_from' )
460 );
461 }
462
463 /**
464 * Validate link depth setting, if available.
465 * @param $depth int
466 * @return int
467 */
468 private function validateLinkDepth( $depth ) {
469 global $wgExportMaxLinkDepth;
470
471 if( $depth < 0 ) {
472 return 0;
473 }
474
475 if ( !$this->userCanOverrideExportDepth() ) {
476 if( $depth > $wgExportMaxLinkDepth ) {
477 return $wgExportMaxLinkDepth;
478 }
479 }
480
481 /*
482 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
483 * crazy-big export from being done by someone setting the depth
484 * number too high. In other words, last resort safety net.
485 */
486 return intval( min( $depth, 5 ) );
487 }
488
489 /**
490 * Expand a list of pages to include pages linked to from that page.
491 * @param $inputPages array
492 * @param $pageSet array
493 * @param $depth int
494 * @return array
495 */
496 private function getPageLinks( $inputPages, $pageSet, $depth ) {
497 for( ; $depth > 0; --$depth ) {
498 $pageSet = $this->getLinks(
499 $inputPages, $pageSet, 'pagelinks',
500 array( 'namespace' => 'pl_namespace', 'title' => 'pl_title' ),
501 array( 'page_id=pl_from' )
502 );
503 $inputPages = array_keys( $pageSet );
504 }
505
506 return $pageSet;
507 }
508
509 /**
510 * Expand a list of pages to include images used in those pages.
511 *
512 * @param $inputPages array, list of titles to look up
513 * @param $pageSet array, associative array indexed by titles for output
514 *
515 * @return array associative array index by titles
516 */
517 private function getImages( $inputPages, $pageSet ) {
518 return $this->getLinks(
519 $inputPages,
520 $pageSet,
521 'imagelinks',
522 array( 'namespace' => NS_FILE, 'title' => 'il_to' ),
523 array( 'page_id=il_from' )
524 );
525 }
526
527 /**
528 * Expand a list of pages to include items used in those pages.
529 * @return array
530 */
531 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
532 $dbr = wfGetDB( DB_SLAVE );
533
534 foreach( $inputPages as $page ) {
535 $title = Title::newFromText( $page );
536
537 if( $title ) {
538 $pageSet[$title->getPrefixedText()] = true;
539 /// @todo FIXME: May or may not be more efficient to batch these
540 /// by namespace when given multiple input pages.
541 $result = $dbr->select(
542 array( 'page', $table ),
543 $fields,
544 array_merge(
545 $join,
546 array(
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 }