Add $wgExportFromNamespaces for enabling/disabling the "export all from namespace...
[lhc/web/wiklou.git] / includes / specials / SpecialExport.php
1 <?php
2 # Copyright (C) 2003-2008 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19 /**
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 class SpecialExport extends SpecialPage {
25
26 private $curonly, $doExport, $pageLinkDepth, $templates;
27 private $images;
28
29 public function __construct() {
30 parent::__construct( 'Export' );
31 }
32
33 public function execute( $par ) {
34 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
35 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
36 global $wgExportFromNamespaces;
37
38 $this->setHeaders();
39 $this->outputHeader();
40
41 // Set some variables
42 $this->curonly = true;
43 $this->doExport = false;
44 $this->templates = $wgRequest->getCheck( 'templates' );
45 $this->images = $wgRequest->getCheck( 'images' ); // Doesn't do anything yet
46 $this->pageLinkDepth = $this->validateLinkDepth(
47 $wgRequest->getIntOrNull( 'pagelink-depth' ) );
48
49 if ( $wgRequest->getCheck( 'addcat' ) ) {
50 $page = $wgRequest->getText( 'pages' );
51 $catname = $wgRequest->getText( 'catname' );
52
53 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
54 $t = Title::makeTitleSafe( NS_MAIN, $catname );
55 if ( $t ) {
56 /**
57 * @fixme This can lead to hitting memory limit for very large
58 * categories. Ideally we would do the lookup synchronously
59 * during the export in a single query.
60 */
61 $catpages = $this->getPagesFromCategory( $t );
62 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
63 }
64 }
65 }
66 else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
67 $page = $wgRequest->getText( 'pages' );
68 $nsindex = $wgRequest->getText( 'nsindex' );
69
70 if ( $nsindex !== '' && $nsindex !== NULL && $nsindex !== false ) {
71 /**
72 * Same implementation as above, so same @fixme
73 */
74 $nspages = $this->getPagesFromNamespace( $nsindex );
75 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
76 }
77 }
78 else if( $wgRequest->wasPosted() && $par == '' ) {
79 $page = $wgRequest->getText( 'pages' );
80 $this->curonly = $wgRequest->getCheck( 'curonly' );
81 $rawOffset = $wgRequest->getVal( 'offset' );
82 if( $rawOffset ) {
83 $offset = wfTimestamp( TS_MW, $rawOffset );
84 } else {
85 $offset = null;
86 }
87 $limit = $wgRequest->getInt( 'limit' );
88 $dir = $wgRequest->getVal( 'dir' );
89 $history = array(
90 'dir' => 'asc',
91 'offset' => false,
92 'limit' => $wgExportMaxHistory,
93 );
94 $historyCheck = $wgRequest->getCheck( 'history' );
95 if ( $this->curonly ) {
96 $history = WikiExporter::CURRENT;
97 } elseif ( !$historyCheck ) {
98 if ( $limit > 0 && $limit < $wgExportMaxHistory ) {
99 $history['limit'] = $limit;
100 }
101 if ( !is_null( $offset ) ) {
102 $history['offset'] = $offset;
103 }
104 if ( strtolower( $dir ) == 'desc' ) {
105 $history['dir'] = 'desc';
106 }
107 }
108
109 if( $page != '' ) $this->doExport = true;
110 } else {
111 // Default to current-only for GET requests
112 $page = $wgRequest->getText( 'pages', $par );
113 $historyCheck = $wgRequest->getCheck( 'history' );
114 if( $historyCheck ) {
115 $history = WikiExporter::FULL;
116 } else {
117 $history = WikiExporter::CURRENT;
118 }
119
120 if( $page != '' ) $this->doExport = true;
121 }
122
123 if( !$wgExportAllowHistory ) {
124 // Override
125 $history = WikiExporter::CURRENT;
126 }
127
128 $list_authors = $wgRequest->getCheck( 'listauthors' );
129 if ( !$this->curonly || !$wgExportAllowListContributors ) $list_authors = false ;
130
131 if ( $this->doExport ) {
132 $wgOut->disable();
133 // Cancel output buffering and gzipping if set
134 // This should provide safer streaming for pages with history
135 wfResetOutputBuffers();
136 header( "Content-type: application/xml; charset=utf-8" );
137 if( $wgRequest->getCheck( 'wpDownload' ) ) {
138 // Provide a sane filename suggestion
139 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
140 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
141 }
142 $this->doExport( $page, $history, $list_authors );
143 return;
144 }
145
146 $wgOut->addWikiMsg( 'exporttext' );
147
148 $form = Xml::openElement( 'form', array( 'method' => 'post',
149 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
150 $form .= Xml::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . '&nbsp;';
151 $form .= Xml::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
152
153 if ( $wgExportFromNamespaces ) {
154 $form .= Xml::namespaceSelector( '', null, 'nsindex', wfMsg( 'export-addnstext' ) ) . '&nbsp;';
155 $form .= Xml::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
156 }
157
158 $form .= Xml::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
159 $form .= '<br />';
160
161 if( $wgExportAllowHistory ) {
162 $form .= Xml::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
163 } else {
164 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
165 }
166 $form .= Xml::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
167 if( $wgExportMaxLinkDepth || $this->userCanOverrideExportDepth() ) {
168 $form .= Xml::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
169 }
170 // Enable this when we can do something useful exporting/importing image information. :)
171 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
172 $form .= Xml::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
173
174 $form .= Xml::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
175 $form .= Xml::closeElement( 'form' );
176 $wgOut->addHTML( $form );
177 }
178
179 private function userCanOverrideExportDepth() {
180 global $wgUser;
181
182 return $wgUser->isAllowed( 'override-export-depth' );
183 }
184
185 /**
186 * Do the actual page exporting
187 * @param string $page User input on what page(s) to export
188 * @param mixed $history one of the WikiExporter history export constants
189 */
190 private function doExport( $page, $history, $list_authors ) {
191 global $wgExportMaxHistory;
192
193 /* Split up the input and look up linked pages */
194 $inputPages = array_filter( explode( "\n", $page ), array( $this, 'filterPage' ) );
195 $pageSet = array_flip( $inputPages );
196
197 if( $this->templates ) {
198 $pageSet = $this->getTemplates( $inputPages, $pageSet );
199 }
200
201 if( $linkDepth = $this->pageLinkDepth ) {
202 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
203 }
204
205 /*
206 // Enable this when we can do something useful exporting/importing image information. :)
207 if( $this->images ) ) {
208 $pageSet = $this->getImages( $inputPages, $pageSet );
209 }
210 */
211
212 $pages = array_keys( $pageSet );
213
214 /* Ok, let's get to it... */
215 if( $history == WikiExporter::CURRENT ) {
216 $lb = false;
217 $db = wfGetDB( DB_SLAVE );
218 $buffer = WikiExporter::BUFFER;
219 } else {
220 // Use an unbuffered query; histories may be very long!
221 $lb = wfGetLBFactory()->newMainLB();
222 $db = $lb->getConnection( DB_SLAVE );
223 $buffer = WikiExporter::STREAM;
224
225 // This might take a while... :D
226 wfSuppressWarnings();
227 set_time_limit(0);
228 wfRestoreWarnings();
229 }
230 $exporter = new WikiExporter( $db, $history, $buffer );
231 $exporter->list_authors = $list_authors;
232 $exporter->openStream();
233 foreach( $pages as $page ) {
234 /*
235 if( $wgExportMaxHistory && !$this->curonly ) {
236 $title = Title::newFromText( $page );
237 if( $title ) {
238 $count = Revision::countByTitle( $db, $title );
239 if( $count > $wgExportMaxHistory ) {
240 wfDebug( __FUNCTION__ .
241 ": Skipped $page, $count revisions too big\n" );
242 continue;
243 }
244 }
245 }*/
246 #Bug 8824: Only export pages the user can read
247 $title = Title::newFromText( $page );
248 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
249 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
250
251 $exporter->pageByTitle( $title );
252 }
253
254 $exporter->closeStream();
255 if( $lb ) {
256 $lb->closeAll();
257 }
258 }
259
260
261 private function getPagesFromCategory( $title ) {
262 global $wgContLang;
263
264 $name = $title->getDBkey();
265
266 $dbr = wfGetDB( DB_SLAVE );
267 $res = $dbr->select( array('page', 'categorylinks' ),
268 array( 'page_namespace', 'page_title' ),
269 array('cl_from=page_id', 'cl_to' => $name ),
270 __METHOD__, array('LIMIT' => '5000'));
271
272 $pages = array();
273 while ( $row = $dbr->fetchObject( $res ) ) {
274 $n = $row->page_title;
275 if ($row->page_namespace) {
276 $ns = $wgContLang->getNsText( $row->page_namespace );
277 $n = $ns . ':' . $n;
278 }
279
280 $pages[] = $n;
281 }
282 $dbr->freeResult($res);
283
284 return $pages;
285 }
286
287 private function getPagesFromNamespace( $nsindex ) {
288 global $wgContLang;
289
290 $dbr = wfGetDB( DB_SLAVE );
291 $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
292 array('page_namespace' => $nsindex),
293 __METHOD__, array('LIMIT' => '5000') );
294
295 $pages = array();
296 while ( $row = $dbr->fetchObject( $res ) ) {
297 $n = $row->page_title;
298 if ($row->page_namespace) {
299 $ns = $wgContLang->getNsText( $row->page_namespace );
300 $n = $ns . ':' . $n;
301 }
302
303 $pages[] = $n;
304 }
305 $dbr->freeResult($res);
306
307 return $pages;
308 }
309 /**
310 * Expand a list of pages to include templates used in those pages.
311 * @param $inputPages array, list of titles to look up
312 * @param $pageSet array, associative array indexed by titles for output
313 * @return array associative array index by titles
314 */
315 private function getTemplates( $inputPages, $pageSet ) {
316 return $this->getLinks( $inputPages, $pageSet,
317 'templatelinks',
318 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
319 array( 'page_id=tl_from' ) );
320 }
321
322 /**
323 * Validate link depth setting, if available.
324 */
325 private function validateLinkDepth( $depth ) {
326 global $wgExportMaxLinkDepth, $wgExportMaxLinkDepthLimit;
327 if( $depth < 0 ) {
328 return 0;
329 }
330 if ( !$this->userCanOverrideExportDepth() ) {
331 if( $depth > $wgExportMaxLinkDepth ) {
332 return $wgExportMaxLinkDepth;
333 }
334 }
335 /*
336 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
337 * crazy-big export from being done by someone setting the depth
338 * number too high. In other words, last resort safety net.
339 */
340 return intval( min( $depth, 5 ) );
341 }
342
343 /** Expand a list of pages to include pages linked to from that page. */
344 private function getPageLinks( $inputPages, $pageSet, $depth ) {
345 for( $depth=$depth; $depth>0; --$depth ) {
346 $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
347 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
348 array( 'page_id=pl_from' ) );
349 }
350 return $pageSet;
351 }
352
353 /**
354 * Expand a list of pages to include images used in those pages.
355 * @param $inputPages array, list of titles to look up
356 * @param $pageSet array, associative array indexed by titles for output
357 * @return array associative array index by titles
358 */
359 private function getImages( $inputPages, $pageSet ) {
360 return $this->getLinks( $inputPages, $pageSet,
361 'imagelinks',
362 array( NS_FILE . ' AS namespace', 'il_to AS title' ),
363 array( 'page_id=il_from' ) );
364 }
365
366 /**
367 * Expand a list of pages to include items used in those pages.
368 * @private
369 */
370 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
371 $dbr = wfGetDB( DB_SLAVE );
372 foreach( $inputPages as $page ) {
373 $title = Title::newFromText( $page );
374 if( $title ) {
375 $pageSet[$title->getPrefixedText()] = true;
376 /// @fixme May or may not be more efficient to batch these
377 /// by namespace when given multiple input pages.
378 $result = $dbr->select(
379 array( 'page', $table ),
380 $fields,
381 array_merge( $join,
382 array(
383 'page_namespace' => $title->getNamespace(),
384 'page_title' => $title->getDBKey() ) ),
385 __METHOD__ );
386 foreach( $result as $row ) {
387 $template = Title::makeTitle( $row->namespace, $row->title );
388 $pageSet[$template->getPrefixedText()] = true;
389 }
390 }
391 }
392 return $pageSet;
393 }
394
395 /**
396 * Callback function to remove empty strings from the pages array.
397 */
398 private function filterPage( $page ) {
399 return $page !== '' && $page !== null;
400 }
401 }
402