2 # Copyright (C) 2003-2008 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
21 * @ingroup SpecialPage
24 class SpecialExport extends SpecialPage {
26 private $curonly, $doExport, $pageLinkDepth, $templates;
29 public function __construct() {
30 parent::__construct( 'Export' );
33 public function execute( $par ) {
34 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
35 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
36 global $wgExportFromNamespaces;
39 $this->outputHeader();
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' ) );
49 if ( $wgRequest->getCheck( 'addcat' ) ) {
50 $page = $wgRequest->getText( 'pages' );
51 $catname = $wgRequest->getText( 'catname' );
53 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
54 $t = Title::makeTitleSafe( NS_MAIN, $catname );
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.
61 $catpages = $this->getPagesFromCategory( $t );
62 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
66 else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
67 $page = $wgRequest->getText( 'pages' );
68 $nsindex = $wgRequest->getText( 'nsindex' );
70 if ( $nsindex !== '' && $nsindex !== NULL && $nsindex !== false ) {
72 * Same implementation as above, so same @fixme
74 $nspages = $this->getPagesFromNamespace( $nsindex );
75 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
78 else if( $wgRequest->wasPosted() && $par == '' ) {
79 $page = $wgRequest->getText( 'pages' );
80 $this->curonly = $wgRequest->getCheck( 'curonly' );
81 $rawOffset = $wgRequest->getVal( 'offset' );
83 $offset = wfTimestamp( TS_MW, $rawOffset );
87 $limit = $wgRequest->getInt( 'limit' );
88 $dir = $wgRequest->getVal( 'dir' );
92 'limit' => $wgExportMaxHistory,
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;
101 if ( !is_null( $offset ) ) {
102 $history['offset'] = $offset;
104 if ( strtolower( $dir ) == 'desc' ) {
105 $history['dir'] = 'desc';
109 if( $page != '' ) $this->doExport = true;
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;
117 $history = WikiExporter::CURRENT;
120 if( $page != '' ) $this->doExport = true;
123 if( !$wgExportAllowHistory ) {
125 $history = WikiExporter::CURRENT;
128 $list_authors = $wgRequest->getCheck( 'listauthors' );
129 if ( !$this->curonly || !$wgExportAllowListContributors ) $list_authors = false ;
131 if ( $this->doExport ) {
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}" );
142 $this->doExport( $page, $history, $list_authors );
146 $wgOut->addWikiMsg( 'exporttext' );
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 ) . ' ';
151 $form .= Xml::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
153 if ( $wgExportFromNamespaces ) {
154 $form .= Xml::namespaceSelector( '', null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
155 $form .= Xml::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
158 $form .= Xml::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
161 if( $wgExportAllowHistory ) {
162 $form .= Xml::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
164 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
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 />';
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 />';
174 $form .= Xml::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
175 $form .= Xml::closeElement( 'form' );
176 $wgOut->addHTML( $form );
179 private function userCanOverrideExportDepth() {
182 return $wgUser->isAllowed( 'override-export-depth' );
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
190 private function doExport( $page, $history, $list_authors ) {
191 global $wgExportMaxHistory;
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 );
197 if( $this->templates ) {
198 $pageSet = $this->getTemplates( $inputPages, $pageSet );
201 if( $linkDepth = $this->pageLinkDepth ) {
202 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
206 // Enable this when we can do something useful exporting/importing image information. :)
207 if( $this->images ) ) {
208 $pageSet = $this->getImages( $inputPages, $pageSet );
212 $pages = array_keys( $pageSet );
214 /* Ok, let's get to it... */
215 if( $history == WikiExporter::CURRENT ) {
217 $db = wfGetDB( DB_SLAVE );
218 $buffer = WikiExporter::BUFFER;
220 // Use an unbuffered query; histories may be very long!
221 $lb = wfGetLBFactory()->newMainLB();
222 $db = $lb->getConnection( DB_SLAVE );
223 $buffer = WikiExporter::STREAM;
225 // This might take a while... :D
226 wfSuppressWarnings();
230 $exporter = new WikiExporter( $db, $history, $buffer );
231 $exporter->list_authors = $list_authors;
232 $exporter->openStream();
233 foreach( $pages as $page ) {
235 if( $wgExportMaxHistory && !$this->curonly ) {
236 $title = Title::newFromText( $page );
238 $count = Revision::countByTitle( $db, $title );
239 if( $count > $wgExportMaxHistory ) {
240 wfDebug( __FUNCTION__ .
241 ": Skipped $page, $count revisions too big\n" );
246 #Bug 8824: Only export pages the user can read
247 $title = Title::newFromText( $page );
248 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
249 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
251 $exporter->pageByTitle( $title );
254 $exporter->closeStream();
261 private function getPagesFromCategory( $title ) {
264 $name = $title->getDBkey();
266 $dbr = wfGetDB( DB_SLAVE );
267 $res = $dbr->select( array('page', 'categorylinks' ),
268 array( 'page_namespace', 'page_title' ),
269 array('cl_from=page_id', 'cl_to' => $name ),
270 __METHOD__, array('LIMIT' => '5000'));
273 while ( $row = $dbr->fetchObject( $res ) ) {
274 $n = $row->page_title;
275 if ($row->page_namespace) {
276 $ns = $wgContLang->getNsText( $row->page_namespace );
282 $dbr->freeResult($res);
287 private function getPagesFromNamespace( $nsindex ) {
290 $dbr = wfGetDB( DB_SLAVE );
291 $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
292 array('page_namespace' => $nsindex),
293 __METHOD__, array('LIMIT' => '5000') );
296 while ( $row = $dbr->fetchObject( $res ) ) {
297 $n = $row->page_title;
298 if ($row->page_namespace) {
299 $ns = $wgContLang->getNsText( $row->page_namespace );
305 $dbr->freeResult($res);
310 * Expand a list of pages to include templates used in those pages.
311 * @param $inputPages array, list of titles to look up
312 * @param $pageSet array, associative array indexed by titles for output
313 * @return array associative array index by titles
315 private function getTemplates( $inputPages, $pageSet ) {
316 return $this->getLinks( $inputPages, $pageSet,
318 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
319 array( 'page_id=tl_from' ) );
323 * Validate link depth setting, if available.
325 private function validateLinkDepth( $depth ) {
326 global $wgExportMaxLinkDepth, $wgExportMaxLinkDepthLimit;
330 if ( !$this->userCanOverrideExportDepth() ) {
331 if( $depth > $wgExportMaxLinkDepth ) {
332 return $wgExportMaxLinkDepth;
336 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
337 * crazy-big export from being done by someone setting the depth
338 * number too high. In other words, last resort safety net.
340 return intval( min( $depth, 5 ) );
343 /** Expand a list of pages to include pages linked to from that page. */
344 private function getPageLinks( $inputPages, $pageSet, $depth ) {
345 for( $depth=$depth; $depth>0; --$depth ) {
346 $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
347 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
348 array( 'page_id=pl_from' ) );
349 $inputPages = array_keys( $pageSet );
355 * Expand a list of pages to include images used in those pages.
356 * @param $inputPages array, list of titles to look up
357 * @param $pageSet array, associative array indexed by titles for output
358 * @return array associative array index by titles
360 private function getImages( $inputPages, $pageSet ) {
361 return $this->getLinks( $inputPages, $pageSet,
363 array( NS_FILE . ' AS namespace', 'il_to AS title' ),
364 array( 'page_id=il_from' ) );
368 * Expand a list of pages to include items used in those pages.
371 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
372 $dbr = wfGetDB( DB_SLAVE );
373 foreach( $inputPages as $page ) {
374 $title = Title::newFromText( $page );
376 $pageSet[$title->getPrefixedText()] = true;
377 /// @fixme May or may not be more efficient to batch these
378 /// by namespace when given multiple input pages.
379 $result = $dbr->select(
380 array( 'page', $table ),
384 'page_namespace' => $title->getNamespace(),
385 'page_title' => $title->getDBKey() ) ),
387 foreach( $result as $row ) {
388 $template = Title::makeTitle( $row->namespace, $row->title );
389 $pageSet[$template->getPrefixedText()] = true;
397 * Callback function to remove empty strings from the pages array.
399 private function filterPage( $page ) {
400 return $page !== '' && $page !== null;