2 # Copyright (C) 2003 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 * @addtogroup 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' ) );
66 * Expand a list of pages to include images used in those pages.
67 * @param $inputPages array, list of titles to look up
68 * @param $pageSet array, associative array indexed by titles for output
69 * @return array associative array index by titles
71 function wfExportGetImages( $inputPages, $pageSet ) {
72 return wfExportGetLinks( $inputPages, $pageSet,
74 array( NS_IMAGE
. ' AS namespace', 'il_to AS title' ),
75 array( 'page_id=il_from' ) );
79 * Expand a list of pages to include items used in those pages.
82 function wfExportGetLinks( $inputPages, $pageSet, $table, $fields, $join ) {
83 $dbr = wfGetDB( DB_SLAVE
);
84 foreach( $inputPages as $page ) {
85 $title = Title
::newFromText( $page );
86 $pageSet[$title->getPrefixedText()] = true;
88 /// @fixme May or may not be more efficient to batch these
89 /// by namespace when given multiple input pages.
90 $result = $dbr->select(
91 array( 'page', $table ),
95 'page_namespace' => $title->getNamespace(),
96 'page_title' => $title->getDbKey() ) ),
98 foreach( $result as $row ) {
99 $template = Title
::makeTitle( $row->namespace, $row->title
);
100 $pageSet[$template->getPrefixedText()] = true;
110 function wfSpecialExport( $page = '' ) {
111 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
112 global $wgExportAllowHistory, $wgExportMaxHistory;
117 if ( $wgRequest->getCheck( 'addcat' ) ) {
118 $page = $wgRequest->getText( 'pages' );
119 $catname = $wgRequest->getText( 'catname' );
121 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
122 $t = Title
::makeTitleSafe( NS_CATEGORY
, $catname );
125 * @fixme This can lead to hitting memory limit for very large
126 * categories. Ideally we would do the lookup synchronously
127 * during the export in a single query.
129 $catpages = wfExportGetPagesFromCategory( $t );
130 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
134 else if( $wgRequest->wasPosted() && $page == '' ) {
135 $page = $wgRequest->getText( 'pages' );
136 $curonly = $wgRequest->getCheck( 'curonly' );
137 $rawOffset = $wgRequest->getVal( 'offset' );
139 $offset = wfTimestamp( TS_MW
, $rawOffset );
143 $limit = $wgRequest->getInt( 'limit' );
144 $dir = $wgRequest->getVal( 'dir' );
148 'limit' => $wgExportMaxHistory,
150 $historyCheck = $wgRequest->getCheck( 'history' );
152 $history = WikiExporter
::CURRENT
;
153 } elseif ( !$historyCheck ) {
154 if ( $limit > 0 && $limit < $wgExportMaxHistory ) {
155 $history['limit'] = $limit;
157 if ( !is_null( $offset ) ) {
158 $history['offset'] = $offset;
160 if ( strtolower( $dir ) == 'desc' ) {
161 $history['dir'] = 'desc';
165 if( $page != '' ) $doexport = true;
167 // Default to current-only for GET requests
168 $page = $wgRequest->getText( 'pages', $page );
169 $historyCheck = $wgRequest->getCheck( 'history' );
170 if( $historyCheck ) {
171 $history = WikiExporter
::FULL
;
173 $history = WikiExporter
::CURRENT
;
176 if( $page != '' ) $doexport = true;
179 if( !$wgExportAllowHistory ) {
181 $history = WikiExporter
::CURRENT
;
184 $list_authors = $wgRequest->getCheck( 'listauthors' );
185 if ( !$curonly ||
!$wgExportAllowListContributors ) $list_authors = false ;
190 // Cancel output buffering and gzipping if set
191 // This should provide safer streaming for pages with history
192 wfResetOutputBuffers();
193 header( "Content-type: application/xml; charset=utf-8" );
194 if( $wgRequest->getCheck( 'wpDownload' ) ) {
195 // Provide a sane filename suggestion
196 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
197 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
200 /* Split up the input and look up linked pages */
201 $inputPages = array_filter( explode( "\n", $page ) );
202 $pageSet = array_flip( $inputPages );
204 if( $wgRequest->getCheck( 'templates' ) ) {
205 $pageSet = wfExportGetTemplates( $inputPages, $pageSet );
209 // Enable this when we can do something useful exporting/importing image information. :)
210 if( $wgRequest->getCheck( 'images' ) ) {
211 $pageSet = wfExportGetImages( $inputPages, $pageSet );
215 $pages = array_keys( $pageSet );
217 /* Ok, let's get to it... */
219 $db = wfGetDB( DB_SLAVE
);
220 $exporter = new WikiExporter( $db, $history );
221 $exporter->list_authors
= $list_authors ;
222 $exporter->openStream();
224 foreach( $pages as $page ) {
226 if( $wgExportMaxHistory && !$curonly ) {
227 $title = Title::newFromText( $page );
229 $count = Revision::countByTitle( $db, $title );
230 if( $count > $wgExportMaxHistory ) {
231 wfDebug( __FUNCTION__ .
232 ": Skipped $page, $count revisions too big\n" );
238 #Bug 8824: Only export pages the user can read
239 $title = Title
::newFromText( $page );
240 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
241 if( !$title->userCan( 'read' ) ) continue; #TODO: perhaps output an <error> tag or something.
243 $exporter->pageByTitle( $title );
246 $exporter->closeStream();
250 $self = SpecialPage
::getTitleFor( 'Export' );
251 $wgOut->addHtml( wfMsgExt( 'exporttext', 'parse' ) );
253 $form = Xml
::openElement( 'form', array( 'method' => 'post',
254 'action' => $self->getLocalUrl( 'action=submit' ) ) );
256 $form .= Xml
::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
257 $form .= Xml
::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
259 $form .= Xml
::openElement( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ) );
260 $form .= htmlspecialchars( $page );
261 $form .= Xml
::closeElement( 'textarea' );
264 if( $wgExportAllowHistory ) {
265 $form .= Xml
::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
267 $wgOut->addHtml( wfMsgExt( 'exportnohistory', 'parse' ) );
269 $form .= Xml
::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
270 // Enable this when we can do something useful exporting/importing image information. :)
271 //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
272 $form .= Xml
::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
274 $form .= Xml
::submitButton( wfMsg( 'export-submit' ) );
275 $form .= Xml
::closeElement( 'form' );
276 $wgOut->addHtml( $form );