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