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