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