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