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