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 function wfExportGetPagesFromCategory( $title ) {
27 $name = $title->getDBkey();
29 $dbr = wfGetDB( DB_SLAVE
);
31 list( $page, $categorylinks ) = $dbr->tableNamesN( 'page', 'categorylinks' );
32 $sql = "SELECT page_namespace, page_title FROM $page " .
33 "JOIN $categorylinks ON cl_from = page_id " .
34 "WHERE cl_to = " . $dbr->addQuotes( $name );
37 $res = $dbr->query( $sql, 'wfExportGetPagesFromCategory' );
38 while ( $row = $dbr->fetchObject( $res ) ) {
39 $n = $row->page_title
;
40 if ($row->page_namespace
) {
41 $ns = $wgContLang->getNsText( $row->page_namespace
);
47 $dbr->freeResult($res);
53 * Expand a list of pages to include templates used in those pages.
54 * @param $inputPages array, list of titles to look up
55 * @param $pageSet array, associative array indexed by titles for output
56 * @return array associative array index by titles
58 function wfExportGetTemplates( $inputPages, $pageSet ) {
59 return wfExportGetLinks( $inputPages, $pageSet,
61 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
62 array( 'page_id=tl_from' ) );
65 /** Expand a list of pages to include pages linked to from that page. */
66 function wfExportGetPageLinks( $inputPages, $pageSet, $depth ) {
67 for( $depth=$depth; $depth>0; --$depth ) {
68 $pageSet = wfExportGetLinks( $inputPages, $pageSet, 'pagelinks',
69 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
70 array( 'page_id=pl_from' ) );
77 * Expand a list of pages to include images used in those pages.
78 * @param $inputPages array, list of titles to look up
79 * @param $pageSet array, associative array indexed by titles for output
80 * @return array associative array index by titles
82 function wfExportGetImages( $inputPages, $pageSet ) {
83 return wfExportGetLinks( $inputPages, $pageSet,
85 array( NS_FILE
. ' AS namespace', 'il_to AS title' ),
86 array( 'page_id=il_from' ) );
90 * Expand a list of pages to include items used in those pages.
93 function wfExportGetLinks( $inputPages, $pageSet, $table, $fields, $join ) {
94 $dbr = wfGetDB( DB_SLAVE
);
95 foreach( $inputPages as $page ) {
96 $title = Title
::newFromText( $page );
98 $pageSet[$title->getPrefixedText()] = true;
99 /// @fixme May or may not be more efficient to batch these
100 /// by namespace when given multiple input pages.
101 $result = $dbr->select(
102 array( 'page', $table ),
106 'page_namespace' => $title->getNamespace(),
107 'page_title' => $title->getDBKey() ) ),
109 foreach( $result as $row ) {
110 $template = Title
::makeTitle( $row->namespace, $row->title
);
111 $pageSet[$template->getPrefixedText()] = true;
119 * Callback function to remove empty strings from the pages array.
121 function wfFilterPage( $page ) {
122 return $page !== '' && $page !== null;
128 function wfSpecialExport( $page = '' ) {
129 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
130 global $wgExportAllowHistory, $wgExportMaxHistory;
135 if ( $wgRequest->getCheck( 'addcat' ) ) {
136 $page = $wgRequest->getText( 'pages' );
137 $catname = $wgRequest->getText( 'catname' );
139 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
140 $t = Title
::makeTitleSafe( NS_MAIN
, $catname );
143 * @fixme This can lead to hitting memory limit for very large
144 * categories. Ideally we would do the lookup synchronously
145 * during the export in a single query.
147 $catpages = wfExportGetPagesFromCategory( $t );
148 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
152 else if( $wgRequest->wasPosted() && $page == '' ) {
153 $page = $wgRequest->getText( 'pages' );
154 $curonly = $wgRequest->getCheck( 'curonly' );
155 $rawOffset = $wgRequest->getVal( 'offset' );
157 $offset = wfTimestamp( TS_MW
, $rawOffset );
161 $limit = $wgRequest->getInt( 'limit' );
162 $dir = $wgRequest->getVal( 'dir' );
166 'limit' => $wgExportMaxHistory,
168 $historyCheck = $wgRequest->getCheck( 'history' );
170 $history = WikiExporter
::CURRENT
;
171 } elseif ( !$historyCheck ) {
172 if ( $limit > 0 && $limit < $wgExportMaxHistory ) {
173 $history['limit'] = $limit;
175 if ( !is_null( $offset ) ) {
176 $history['offset'] = $offset;
178 if ( strtolower( $dir ) == 'desc' ) {
179 $history['dir'] = 'desc';
183 if( $page != '' ) $doexport = true;
185 // Default to current-only for GET requests
186 $page = $wgRequest->getText( 'pages', $page );
187 $historyCheck = $wgRequest->getCheck( 'history' );
188 if( $historyCheck ) {
189 $history = WikiExporter
::FULL
;
191 $history = WikiExporter
::CURRENT
;
194 if( $page != '' ) $doexport = true;
197 if( !$wgExportAllowHistory ) {
199 $history = WikiExporter
::CURRENT
;
202 $list_authors = $wgRequest->getCheck( 'listauthors' );
203 if ( !$curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
208 // Cancel output buffering and gzipping if set
209 // This should provide safer streaming for pages with history
210 wfResetOutputBuffers();
211 header( "Content-type: application/xml; charset=utf-8" );
212 if( $wgRequest->getCheck( 'wpDownload' ) ) {
213 // Provide a sane filename suggestion
214 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
215 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
218 /* Split up the input and look up linked pages */
219 $inputPages = array_filter( explode( "\n", $page ), 'wfFilterPage' );
220 $pageSet = array_flip( $inputPages );
222 if( $wgRequest->getCheck( 'templates' ) ) {
223 $pageSet = wfExportGetTemplates( $inputPages, $pageSet );
226 if( $linkDepth = $wgRequest->getIntOrNull( 'pagelink-depth' ) ) {
227 $pageSet = wfExportGetPageLinks( $inputPages, $pageSet, $linkDepth );
231 // Enable this when we can do something useful exporting/importing image information. :)
232 if( $wgRequest->getCheck( 'images' ) ) {
233 $pageSet = wfExportGetImages( $inputPages, $pageSet );
237 $pages = array_keys( $pageSet );
239 /* Ok, let's get to it... */
241 if( $history == WikiExporter
::CURRENT
) {
243 $db = wfGetDB( DB_SLAVE
);
244 $buffer = WikiExporter
::BUFFER
;
246 // Use an unbuffered query; histories may be very long!
247 $lb = wfGetLBFactory()->newMainLB();
248 $db = $lb->getConnection( DB_SLAVE
);
249 $buffer = WikiExporter
::STREAM
;
251 // This might take a while... :D
252 wfSuppressWarnings();
257 $exporter = new WikiExporter( $db, $history, $buffer );
258 $exporter->list_authors
= $list_authors ;
259 $exporter->openStream();
261 foreach( $pages as $page ) {
263 if( $wgExportMaxHistory && !$curonly ) {
264 $title = Title::newFromText( $page );
266 $count = Revision::countByTitle( $db, $title );
267 if( $count > $wgExportMaxHistory ) {
268 wfDebug( __FUNCTION__ .
269 ": Skipped $page, $count revisions too big\n" );
275 #Bug 8824: Only export pages the user can read
276 $title = Title
::newFromText( $page );
277 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
278 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
280 $exporter->pageByTitle( $title );
283 $exporter->closeStream();
290 $self = SpecialPage
::getTitleFor( 'Export' );
291 $wgOut->addHTML( wfMsgExt( 'exporttext', 'parse' ) );
293 $form = Xml
::openElement( 'form', array( 'method' => 'post',
294 'action' => $self->getLocalUrl( 'action=submit' ) ) );
296 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
297 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
299 $form .= Xml
::openElement( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ) );
300 $form .= htmlspecialchars( $page );
301 $form .= Xml
::closeElement( 'textarea' );
304 if( $wgExportAllowHistory ) {
305 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
307 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
309 $form .= Xml
::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
310 $form .= Xml
::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
311 // Enable this when we can do something useful exporting/importing image information. :)
312 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
313 $form .= Xml
::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
315 $form .= Xml
::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
316 $form .= Xml
::closeElement( 'form' );
317 $wgOut->addHTML( $form );