3 * Implements Special:Export
5 * Copyright © 2003-2008 Brion Vibber <brion@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
27 * A special page that allows users to export pages in a XML file
29 * @ingroup SpecialPage
31 class SpecialExport
extends SpecialPage
{
33 private $curonly, $doExport, $pageLinkDepth, $templates;
36 public function __construct() {
37 parent
::__construct( 'Export' );
40 public function execute( $par ) {
41 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
42 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
43 global $wgExportFromNamespaces, $wgUser;
46 $this->outputHeader();
49 $this->curonly
= true;
50 $this->doExport
= false;
51 $this->templates
= $wgRequest->getCheck( 'templates' );
52 $this->images
= $wgRequest->getCheck( 'images' ); // Doesn't do anything yet
53 $this->pageLinkDepth
= $this->validateLinkDepth(
54 $wgRequest->getIntOrNull( 'pagelink-depth' )
58 if ( $wgRequest->getCheck( 'addcat' ) ) {
59 $page = $wgRequest->getText( 'pages' );
60 $catname = $wgRequest->getText( 'catname' );
62 if ( $catname !== '' && $catname !== null && $catname !== false ) {
63 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
66 * @todo Fixme: this can lead to hitting memory limit for very large
67 * categories. Ideally we would do the lookup synchronously
68 * during the export in a single query.
70 $catpages = $this->getPagesFromCategory( $t );
71 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
75 else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
76 $page = $wgRequest->getText( 'pages' );
77 $nsindex = $wgRequest->getText( 'nsindex', '' );
79 if ( strval( $nsindex ) !== '' ) {
81 * Same implementation as above, so same @todo
83 $nspages = $this->getPagesFromNamespace( $nsindex );
84 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
87 else if( $wgRequest->wasPosted() && $par == '' ) {
88 $page = $wgRequest->getText( 'pages' );
89 $this->curonly
= $wgRequest->getCheck( 'curonly' );
90 $rawOffset = $wgRequest->getVal( 'offset' );
93 $offset = wfTimestamp( TS_MW
, $rawOffset );
98 $limit = $wgRequest->getInt( 'limit' );
99 $dir = $wgRequest->getVal( 'dir' );
103 'limit' => $wgExportMaxHistory,
105 $historyCheck = $wgRequest->getCheck( 'history' );
107 if ( $this->curonly
) {
108 $history = WikiExporter
::CURRENT
;
109 } elseif ( !$historyCheck ) {
110 if ( $limit > 0 && ($wgExportMaxHistory == 0 ||
$limit < $wgExportMaxHistory ) ) {
111 $history['limit'] = $limit;
113 if ( !is_null( $offset ) ) {
114 $history['offset'] = $offset;
116 if ( strtolower( $dir ) == 'desc' ) {
117 $history['dir'] = 'desc';
121 if( $page != '' ) $this->doExport
= true;
123 // Default to current-only for GET requests.
124 $page = $wgRequest->getText( 'pages', $par );
125 $historyCheck = $wgRequest->getCheck( 'history' );
127 if( $historyCheck ) {
128 $history = WikiExporter
::FULL
;
130 $history = WikiExporter
::CURRENT
;
133 if( $page != '' ) $this->doExport
= true;
136 if( !$wgExportAllowHistory ) {
138 $history = WikiExporter
::CURRENT
;
141 $list_authors = $wgRequest->getCheck( 'listauthors' );
142 if ( !$this->curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
144 if ( $this->doExport
) {
147 // Cancel output buffering and gzipping if set
148 // This should provide safer streaming for pages with history
149 wfResetOutputBuffers();
150 $wgRequest->response()->header( "Content-type: application/xml; charset=utf-8" );
152 if( $wgRequest->getCheck( 'wpDownload' ) ) {
153 // Provide a sane filename suggestion
154 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
155 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
158 $this->doExport( $page, $history, $list_authors );
163 $wgOut->addWikiMsg( 'exporttext' );
165 $form = Xml
::openElement( 'form', array( 'method' => 'post',
166 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
167 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
168 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
170 if ( $wgExportFromNamespaces ) {
171 $form .= Xml
::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
172 $form .= Xml
::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
175 $form .= Xml
::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
178 if( $wgExportAllowHistory ) {
179 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
181 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
184 $form .= Xml
::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
186 if( $wgExportMaxLinkDepth ||
$this->userCanOverrideExportDepth() ) {
187 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
189 // Enable this when we can do something useful exporting/importing image information. :)
190 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
191 $form .= Xml
::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
193 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'export' ) );
194 $form .= Xml
::closeElement( 'form' );
196 $wgOut->addHTML( $form );
199 private function userCanOverrideExportDepth() {
201 return $wgUser->isAllowed( 'override-export-depth' );
205 * Do the actual page exporting
207 * @param $page String: user input on what page(s) to export
208 * @param $history Mixed: one of the WikiExporter history export constants
209 * @param $list_authors Boolean: Whether to add distinct author list (when
210 * not returning full history)
212 private function doExport( $page, $history, $list_authors ) {
213 $pageSet = array(); // Inverted index of all pages to look up
215 // Split up and normalize input
216 foreach( explode( "\n", $page ) as $pageName ) {
217 $pageName = trim( $pageName );
218 $title = Title
::newFromText( $pageName );
219 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
220 // Only record each page once!
221 $pageSet[$title->getPrefixedText()] = true;
225 // Set of original pages to pass on to further manipulation...
226 $inputPages = array_keys( $pageSet );
228 // Look up any linked pages if asked...
229 if( $this->templates
) {
230 $pageSet = $this->getTemplates( $inputPages, $pageSet );
233 if( $linkDepth = $this->pageLinkDepth
) {
234 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
238 // Enable this when we can do something useful exporting/importing image information. :)
239 if( $this->images ) ) {
240 $pageSet = $this->getImages( $inputPages, $pageSet );
244 $pages = array_keys( $pageSet );
246 // Normalize titles to the same format and remove dupes, see bug 17374
247 foreach( $pages as $k => $v ) {
248 $pages[$k] = str_replace( " ", "_", $v );
251 $pages = array_unique( $pages );
253 /* Ok, let's get to it... */
254 if( $history == WikiExporter
::CURRENT
) {
256 $db = wfGetDB( DB_SLAVE
);
257 $buffer = WikiExporter
::BUFFER
;
259 // Use an unbuffered query; histories may be very long!
260 $lb = wfGetLBFactory()->newMainLB();
261 $db = $lb->getConnection( DB_SLAVE
);
262 $buffer = WikiExporter
::STREAM
;
264 // This might take a while... :D
265 wfSuppressWarnings();
270 $exporter = new WikiExporter( $db, $history, $buffer );
271 $exporter->list_authors
= $list_authors;
272 $exporter->openStream();
274 foreach( $pages as $page ) {
276 if( $wgExportMaxHistory && !$this->curonly ) {
277 $title = Title::newFromText( $page );
279 $count = Revision::countByTitle( $db, $title );
280 if( $count > $wgExportMaxHistory ) {
281 wfDebug( __FUNCTION__ .
282 ": Skipped $page, $count revisions too big\n" );
287 #Bug 8824: Only export pages the user can read
288 $title = Title
::newFromText( $page );
289 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
290 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
292 $exporter->pageByTitle( $title );
295 $exporter->closeStream();
302 private function getPagesFromCategory( $title ) {
305 $name = $title->getDBkey();
307 $dbr = wfGetDB( DB_SLAVE
);
309 array( 'page', 'categorylinks' ),
310 array( 'page_namespace', 'page_title' ),
311 array( 'cl_from=page_id', 'cl_to' => $name ),
313 array( 'LIMIT' => '5000' )
318 while ( $row = $dbr->fetchObject( $res ) ) {
319 $n = $row->page_title
;
320 if ($row->page_namespace
) {
321 $ns = $wgContLang->getNsText( $row->page_namespace
);
330 private function getPagesFromNamespace( $nsindex ) {
333 $dbr = wfGetDB( DB_SLAVE
);
336 array( 'page_namespace', 'page_title' ),
337 array( 'page_namespace' => $nsindex ),
339 array( 'LIMIT' => '5000' )
344 while ( $row = $dbr->fetchObject( $res ) ) {
345 $n = $row->page_title
;
347 if ( $row->page_namespace
) {
348 $ns = $wgContLang->getNsText( $row->page_namespace
);
358 * Expand a list of pages to include templates used in those pages.
359 * @param $inputPages array, list of titles to look up
360 * @param $pageSet array, associative array indexed by titles for output
361 * @return array associative array index by titles
363 private function getTemplates( $inputPages, $pageSet ) {
364 return $this->getLinks( $inputPages, $pageSet,
366 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
367 array( 'page_id=tl_from' )
372 * Validate link depth setting, if available.
374 private function validateLinkDepth( $depth ) {
375 global $wgExportMaxLinkDepth;
381 if ( !$this->userCanOverrideExportDepth() ) {
382 if( $depth > $wgExportMaxLinkDepth ) {
383 return $wgExportMaxLinkDepth;
388 * There's a HARD CODED limit of 5 levels of recursion here to prevent a
389 * crazy-big export from being done by someone setting the depth
390 * number too high. In other words, last resort safety net.
392 return intval( min( $depth, 5 ) );
395 /** Expand a list of pages to include pages linked to from that page. */
396 private function getPageLinks( $inputPages, $pageSet, $depth ) {
397 for(; $depth > 0; --$depth ) {
398 $pageSet = $this->getLinks(
399 $inputPages, $pageSet, 'pagelinks',
400 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
401 array( 'page_id=pl_from' )
403 $inputPages = array_keys( $pageSet );
410 * Expand a list of pages to include images used in those pages.
412 * @param $inputPages array, list of titles to look up
413 * @param $pageSet array, associative array indexed by titles for output
415 * @return array associative array index by titles
417 private function getImages( $inputPages, $pageSet ) {
418 return $this->getLinks(
422 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
423 array( 'page_id=il_from' )
428 * Expand a list of pages to include items used in those pages.
430 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
431 $dbr = wfGetDB( DB_SLAVE
);
433 foreach( $inputPages as $page ) {
434 $title = Title
::newFromText( $page );
437 $pageSet[$title->getPrefixedText()] = true;
438 /// @todo Fixme: May or may not be more efficient to batch these
439 /// by namespace when given multiple input pages.
440 $result = $dbr->select(
441 array( 'page', $table ),
446 'page_namespace' => $title->getNamespace(),
447 'page_title' => $title->getDBkey()
453 foreach( $result as $row ) {
454 $template = Title
::makeTitle( $row->namespace, $row->title
);
455 $pageSet[$template->getPrefixedText()] = true;