A little refactoring of the input splitting/expansion:
[lhc/web/wiklou.git] / includes / SpecialExport.php
1 <?php
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19 /**
20 *
21 * @addtogroup SpecialPage
22 */
23
24 function wfExportGetPagesFromCategory( $title ) {
25 global $wgContLang;
26
27 $name = $title->getDBkey();
28
29 $dbr = wfGetDB( DB_SLAVE );
30
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 );
35
36 $pages = array();
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 );
42 $n = $ns . ':' . $n;
43 }
44
45 $pages[] = $n;
46 }
47 $dbr->freeResult($res);
48
49 return $pages;
50 }
51
52 /**
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
57 */
58 function wfExportGetTemplates( $inputPages, $pageSet ) {
59 return wfExportGetLinks( $inputPages, $pageSet,
60 'templatelinks',
61 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
62 array( 'page_id=tl_from' ) );
63 }
64
65 /**
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
70 */
71 function wfExportGetImages( $inputPages, $pageSet ) {
72 return wfExportGetLinks( $inputPages, $pageSet,
73 'imagelinks',
74 array( NS_IMAGE . ' AS namespace', 'il_to AS title' ),
75 array( 'page_id=il_from' ) );
76 }
77
78 /**
79 * Expand a list of pages to include items used in those pages.
80 * @private
81 */
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;
87 if( $title ) {
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 ),
92 $fields,
93 array_merge( $join,
94 array(
95 'page_namespace' => $title->getNamespace(),
96 'page_title' => $title->getDbKey() ) ),
97 __METHOD__ );
98 foreach( $result as $row ) {
99 $template = Title::makeTitle( $row->namespace, $row->title );
100 $pageSet[$template->getPrefixedText()] = true;
101 }
102 }
103 }
104 return $pageSet;
105 }
106
107 /**
108 *
109 */
110 function wfSpecialExport( $page = '' ) {
111 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
112 global $wgExportAllowHistory, $wgExportMaxHistory;
113
114 $curonly = true;
115 $doexport = false;
116
117 if ( $wgRequest->getCheck( 'addcat' ) ) {
118 $page = $wgRequest->getText( 'pages' );
119 $catname = $wgRequest->getText( 'catname' );
120
121 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
122 $t = Title::makeTitleSafe( NS_CATEGORY, $catname );
123 if ( $t ) {
124 /**
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.
128 */
129 $catpages = wfExportGetPagesFromCategory( $t );
130 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
131 }
132 }
133 }
134 else if( $wgRequest->wasPosted() && $page == '' ) {
135 $page = $wgRequest->getText( 'pages' );
136 $curonly = $wgRequest->getCheck( 'curonly' );
137 $rawOffset = $wgRequest->getVal( 'offset' );
138 if( $rawOffset ) {
139 $offset = wfTimestamp( TS_MW, $rawOffset );
140 } else {
141 $offset = null;
142 }
143 $limit = $wgRequest->getInt( 'limit' );
144 $dir = $wgRequest->getVal( 'dir' );
145 $history = array(
146 'dir' => 'asc',
147 'offset' => false,
148 'limit' => $wgExportMaxHistory,
149 );
150 $historyCheck = $wgRequest->getCheck( 'history' );
151 if ( $curonly ) {
152 $history = WikiExporter::CURRENT;
153 } elseif ( !$historyCheck ) {
154 if ( $limit > 0 && $limit < $wgExportMaxHistory ) {
155 $history['limit'] = $limit;
156 }
157 if ( !is_null( $offset ) ) {
158 $history['offset'] = $offset;
159 }
160 if ( strtolower( $dir ) == 'desc' ) {
161 $history['dir'] = 'desc';
162 }
163 }
164
165 if( $page != '' ) $doexport = true;
166 } else {
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;
172 } else {
173 $history = WikiExporter::CURRENT;
174 }
175
176 if( $page != '' ) $doexport = true;
177 }
178
179 if( !$wgExportAllowHistory ) {
180 // Override
181 $history = WikiExporter::CURRENT;
182 }
183
184 $list_authors = $wgRequest->getCheck( 'listauthors' );
185 if ( !$curonly || !$wgExportAllowListContributors ) $list_authors = false ;
186
187 if ( $doexport ) {
188 $wgOut->disable();
189
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}" );
198 }
199
200 /* Split up the input and look up linked pages */
201 $inputPages = array_filter( explode( "\n", $page ) );
202 $pageSet = array_flip( $inputPages );
203
204 if( $wgRequest->getCheck( 'templates' ) ) {
205 $pageSet = wfExportGetTemplates( $inputPages, $pageSet );
206 }
207
208 /*
209 // Enable this when we can do something useful exporting/importing image information. :)
210 if( $wgRequest->getCheck( 'images' ) ) {
211 $pageSet = wfExportGetImages( $inputPages, $pageSet );
212 }
213 */
214
215 $pages = array_keys( $pageSet );
216
217 /* Ok, let's get to it... */
218
219 $db = wfGetDB( DB_SLAVE );
220 $exporter = new WikiExporter( $db, $history );
221 $exporter->list_authors = $list_authors ;
222 $exporter->openStream();
223
224 foreach( $pages as $page ) {
225 /*
226 if( $wgExportMaxHistory && !$curonly ) {
227 $title = Title::newFromText( $page );
228 if( $title ) {
229 $count = Revision::countByTitle( $db, $title );
230 if( $count > $wgExportMaxHistory ) {
231 wfDebug( __FUNCTION__ .
232 ": Skipped $page, $count revisions too big\n" );
233 continue;
234 }
235 }
236 }*/
237
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.
242
243 $exporter->pageByTitle( $title );
244 }
245
246 $exporter->closeStream();
247 return;
248 }
249
250 $self = SpecialPage::getTitleFor( 'Export' );
251 $wgOut->addHtml( wfMsgExt( 'exporttext', 'parse' ) );
252
253 $form = Xml::openElement( 'form', array( 'method' => 'post',
254 'action' => $self->getLocalUrl( 'action=submit' ) ) );
255
256 $form .= Xml::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . '&nbsp;';
257 $form .= Xml::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
258
259 $form .= Xml::openElement( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ) );
260 $form .= htmlspecialchars( $page );
261 $form .= Xml::closeElement( 'textarea' );
262 $form .= '<br />';
263
264 if( $wgExportAllowHistory ) {
265 $form .= Xml::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
266 } else {
267 $wgOut->addHtml( wfMsgExt( 'exportnohistory', 'parse' ) );
268 }
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 />';
273
274 $form .= Xml::submitButton( wfMsg( 'export-submit' ) );
275 $form .= Xml::closeElement( 'form' );
276 $wgOut->addHtml( $form );
277 }