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