Fixed undefined var.
[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 ( $nsindex !== '' && $nsindex !== null && $nsindex !== false ) {
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 && $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 /* Split up the input and look up linked pages */
195 $inputPages = array_filter( explode( "\n", $page ), array( $this, 'filterPage' ) );
196 $pageSet = array_flip( $inputPages );
197
198 if( $this->templates ) {
199 $pageSet = $this->getTemplates( $inputPages, $pageSet );
200 }
201
202 if( $linkDepth = $this->pageLinkDepth ) {
203 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
204 }
205
206 /*
207 // Enable this when we can do something useful exporting/importing image information. :)
208 if( $this->images ) ) {
209 $pageSet = $this->getImages( $inputPages, $pageSet );
210 }
211 */
212
213 $pages = array_keys( $pageSet );
214
215 // Normalize titles to the same format and remove dupes, see bug 17374
216 foreach( $pages as $k => $v ) {
217 $pages[$k] = str_replace( " ", "_", $v );
218 }
219 $pages = array_unique( $pages );
220
221 /* Ok, let's get to it... */
222 if( $history == WikiExporter::CURRENT ) {
223 $lb = false;
224 $db = wfGetDB( DB_SLAVE );
225 $buffer = WikiExporter::BUFFER;
226 } else {
227 // Use an unbuffered query; histories may be very long!
228 $lb = wfGetLBFactory()->newMainLB();
229 $db = $lb->getConnection( DB_SLAVE );
230 $buffer = WikiExporter::STREAM;
231
232 // This might take a while... :D
233 wfSuppressWarnings();
234 set_time_limit(0);
235 wfRestoreWarnings();
236 }
237 $exporter = new WikiExporter( $db, $history, $buffer );
238 $exporter->list_authors = $list_authors;
239 $exporter->openStream();
240 foreach( $pages as $page ) {
241 /*
242 if( $wgExportMaxHistory && !$this->curonly ) {
243 $title = Title::newFromText( $page );
244 if( $title ) {
245 $count = Revision::countByTitle( $db, $title );
246 if( $count > $wgExportMaxHistory ) {
247 wfDebug( __FUNCTION__ .
248 ": Skipped $page, $count revisions too big\n" );
249 continue;
250 }
251 }
252 }*/
253 #Bug 8824: Only export pages the user can read
254 $title = Title::newFromText( $page );
255 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
256 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
257
258 $exporter->pageByTitle( $title );
259 }
260
261 $exporter->closeStream();
262 if( $lb ) {
263 $lb->closeAll();
264 }
265 }
266
267
268 private function getPagesFromCategory( $title ) {
269 global $wgContLang;
270
271 $name = $title->getDBkey();
272
273 $dbr = wfGetDB( DB_SLAVE );
274 $res = $dbr->select( array('page', 'categorylinks' ),
275 array( 'page_namespace', 'page_title' ),
276 array('cl_from=page_id', 'cl_to' => $name ),
277 __METHOD__, array('LIMIT' => '5000'));
278
279 $pages = array();
280 while ( $row = $dbr->fetchObject( $res ) ) {
281 $n = $row->page_title;
282 if ($row->page_namespace) {
283 $ns = $wgContLang->getNsText( $row->page_namespace );
284 $n = $ns . ':' . $n;
285 }
286
287 $pages[] = $n;
288 }
289 $dbr->freeResult($res);
290
291 return $pages;
292 }
293
294 private function getPagesFromNamespace( $nsindex ) {
295 global $wgContLang;
296
297 $dbr = wfGetDB( DB_SLAVE );
298 $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
299 array('page_namespace' => $nsindex),
300 __METHOD__, array('LIMIT' => '5000') );
301
302 $pages = array();
303 while ( $row = $dbr->fetchObject( $res ) ) {
304 $n = $row->page_title;
305 if ($row->page_namespace) {
306 $ns = $wgContLang->getNsText( $row->page_namespace );
307 $n = $ns . ':' . $n;
308 }
309
310 $pages[] = $n;
311 }
312 $dbr->freeResult($res);
313
314 return $pages;
315 }
316 /**
317 * Expand a list of pages to include templates used in those pages.
318 * @param $inputPages array, list of titles to look up
319 * @param $pageSet array, associative array indexed by titles for output
320 * @return array associative array index by titles
321 */
322 private function getTemplates( $inputPages, $pageSet ) {
323 return $this->getLinks( $inputPages, $pageSet,
324 'templatelinks',
325 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
326 array( 'page_id=tl_from' ) );
327 }
328
329 /**
330 * Validate link depth setting, if available.
331 */
332 private function validateLinkDepth( $depth ) {
333 global $wgExportMaxLinkDepth, $wgExportMaxLinkDepthLimit;
334 if( $depth < 0 ) {
335 return 0;
336 }
337 if ( !$this->userCanOverrideExportDepth() ) {
338 if( $depth > $wgExportMaxLinkDepth ) {
339 return $wgExportMaxLinkDepth;
340 }
341 }
342 /*
343 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
344 * crazy-big export from being done by someone setting the depth
345 * number too high. In other words, last resort safety net.
346 */
347 return intval( min( $depth, 5 ) );
348 }
349
350 /** Expand a list of pages to include pages linked to from that page. */
351 private function getPageLinks( $inputPages, $pageSet, $depth ) {
352 for( $depth=$depth; $depth>0; --$depth ) {
353 $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
354 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
355 array( 'page_id=pl_from' ) );
356 }
357 return $pageSet;
358 }
359
360 /**
361 * Expand a list of pages to include images used in those pages.
362 * @param $inputPages array, list of titles to look up
363 * @param $pageSet array, associative array indexed by titles for output
364 * @return array associative array index by titles
365 */
366 private function getImages( $inputPages, $pageSet ) {
367 return $this->getLinks( $inputPages, $pageSet,
368 'imagelinks',
369 array( NS_FILE . ' AS namespace', 'il_to AS title' ),
370 array( 'page_id=il_from' ) );
371 }
372
373 /**
374 * Expand a list of pages to include items used in those pages.
375 * @private
376 */
377 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
378 $dbr = wfGetDB( DB_SLAVE );
379 foreach( $inputPages as $page ) {
380 $title = Title::newFromText( $page );
381 if( $title ) {
382 $pageSet[$title->getPrefixedText()] = true;
383 /// @fixme May or may not be more efficient to batch these
384 /// by namespace when given multiple input pages.
385 $result = $dbr->select(
386 array( 'page', $table ),
387 $fields,
388 array_merge( $join,
389 array(
390 'page_namespace' => $title->getNamespace(),
391 'page_title' => $title->getDBKey() ) ),
392 __METHOD__ );
393 foreach( $result as $row ) {
394 $template = Title::makeTitle( $row->namespace, $row->title );
395 $pageSet[$template->getPrefixedText()] = true;
396 }
397 }
398 }
399 return $pageSet;
400 }
401
402 /**
403 * Callback function to remove empty strings from the pages array.
404 */
405 private function filterPage( $page ) {
406 return $page !== '' && $page !== null;
407 }
408 }
409