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