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;
38 $this->outputHeader();
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' ) );
48 if ( $wgRequest->getCheck( 'addcat' ) ) {
49 $page = $wgRequest->getText( 'pages' );
50 $catname = $wgRequest->getText( 'catname' );
52 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
53 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
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.
60 $catpages = $this->getPagesFromCategory( $t );
61 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
65 else if( $wgRequest->wasPosted() && $par == '' ) {
66 $page = $wgRequest->getText( 'pages' );
67 $this->curonly
= $wgRequest->getCheck( 'curonly' );
68 $rawOffset = $wgRequest->getVal( 'offset' );
70 $offset = wfTimestamp( TS_MW
, $rawOffset );
74 $limit = $wgRequest->getInt( 'limit' );
75 $dir = $wgRequest->getVal( 'dir' );
79 'limit' => $wgExportMaxHistory,
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;
88 if ( !is_null( $offset ) ) {
89 $history['offset'] = $offset;
91 if ( strtolower( $dir ) == 'desc' ) {
92 $history['dir'] = 'desc';
96 if( $page != '' ) $this->doExport
= true;
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
;
104 $history = WikiExporter
::CURRENT
;
107 if( $page != '' ) $this->doExport
= true;
110 if( !$wgExportAllowHistory ) {
112 $history = WikiExporter
::CURRENT
;
115 $list_authors = $wgRequest->getCheck( 'listauthors' );
116 if ( !$this->curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
118 if ( $this->doExport
) {
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}" );
129 $this->doExport( $page, $history, $list_authors );
133 $wgOut->addWikiMsg( 'exporttext' );
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 ) . ' ';
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 );
142 if( $wgExportAllowHistory ) {
143 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
145 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
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 />';
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 />';
155 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
156 $form .= Xml
::closeElement( 'form' );
157 $wgOut->addHTML( $form );
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
165 private function doExport( $page, $history, $list_authors ) {
166 global $wgExportMaxHistory;
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 );
172 if( $this->templates
) {
173 $pageSet = $this->getTemplates( $inputPages, $pageSet );
176 if( $linkDepth = $this->pageLinkDepth
) {
177 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
181 // Enable this when we can do something useful exporting/importing image information. :)
182 if( $this->images ) ) {
183 $pageSet = $this->getImages( $inputPages, $pageSet );
187 $pages = array_keys( $pageSet );
189 /* Ok, let's get to it... */
190 if( $history == WikiExporter
::CURRENT
) {
192 $db = wfGetDB( DB_SLAVE
);
193 $buffer = WikiExporter
::BUFFER
;
195 // Use an unbuffered query; histories may be very long!
196 $lb = wfGetLBFactory()->newMainLB();
197 $db = $lb->getConnection( DB_SLAVE
);
198 $buffer = WikiExporter
::STREAM
;
200 // This might take a while... :D
201 wfSuppressWarnings();
205 $exporter = new WikiExporter( $db, $history, $buffer );
206 $exporter->list_authors
= $list_authors;
207 $exporter->openStream();
208 foreach( $pages as $page ) {
210 if( $wgExportMaxHistory && !$this->curonly ) {
211 $title = Title::newFromText( $page );
213 $count = Revision::countByTitle( $db, $title );
214 if( $count > $wgExportMaxHistory ) {
215 wfDebug( __FUNCTION__ .
216 ": Skipped $page, $count revisions too big\n" );
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.
226 $exporter->pageByTitle( $title );
229 $exporter->closeStream();
235 private function getPagesFromCategory( $title ) {
238 $name = $title->getDBkey();
240 $dbr = wfGetDB( DB_SLAVE
);
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 );
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
);
258 $dbr->freeResult($res);
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
269 private function getTemplates( $inputPages, $pageSet ) {
270 return $this->getLinks( $inputPages, $pageSet,
272 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
273 array( 'page_id=tl_from' ) );
277 * Validate link depth setting, if available.
279 private function validateLinkDepth( $depth ) {
280 global $wgExportMaxLinkDepth;
284 if( $depth > $wgExportMaxLinkDepth ) {
285 return $wgExportMaxLinkDepth;
287 return intval( $depth );
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' ) );
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
306 private function getImages( $inputPages, $pageSet ) {
307 return $this->getLinks( $inputPages, $pageSet,
309 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
310 array( 'page_id=il_from' ) );
314 * Expand a list of pages to include items used in those pages.
317 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
318 $dbr = wfGetDB( DB_SLAVE
);
319 foreach( $inputPages as $page ) {
320 $title = Title
::newFromText( $page );
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 ),
330 'page_namespace' => $title->getNamespace(),
331 'page_title' => $title->getDBKey() ) ),
333 foreach( $result as $row ) {
334 $template = Title
::makeTitle( $row->namespace, $row->title
);
335 $pageSet[$template->getPrefixedText()] = true;
343 * Callback function to remove empty strings from the pages array.
345 private function filterPage( $page ) {
346 return $page !== '' && $page !== null;