Rewrite Special:Export to subclass SpecialPage.
[lhc/web/wiklou.git] / includes / specials / SpecialImport.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 class SpecialImport extends SpecialPage {
27
28 private $interwiki = false;
29 private $namespace;
30 private $frompage = '';
31 private $logcomment= false;
32 private $history = true;
33 private $includeTemplates = false;
34
35 /**
36 * Constructor
37 */
38 public function __construct() {
39 parent::__construct( 'Import', 'import' );
40 global $wgImportTargetNamespace;
41 $this->namespace = $wgImportTargetNamespace;
42 }
43
44 /**
45 * Execute
46 */
47 function execute( $par ) {
48 global $wgRequest;
49
50 $this->setHeaders();
51 $this->outputHeader();
52
53 if ( wfReadOnly() ) {
54 global $wgOut;
55 $wgOut->readOnlyPage();
56 return;
57 }
58
59 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
60 $this->doImport();
61 }
62 $this->showForm();
63 }
64
65 /**
66 * Do the actual import
67 */
68 private function doImport() {
69 global $wgOut, $wgRequest, $wgUser, $wgImportSources;
70 $isUpload = false;
71 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
72 $sourceName = $wgRequest->getVal( "source" );
73
74 $this->logcomment = $wgRequest->getText( 'log-comment' );
75 $this->pageLinkDepth = $wgRequest->getIntOrNull( 'pagelink-depth' );
76
77 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
78 $source = new WikiErrorMsg( 'import-token-mismatch' );
79 } elseif ( $sourceName == 'upload' ) {
80 $isUpload = true;
81 if( $wgUser->isAllowed( 'importupload' ) ) {
82 $source = ImportStreamSource::newFromUpload( "xmlimport" );
83 } else {
84 return $wgOut->permissionRequired( 'importupload' );
85 }
86 } elseif ( $sourceName == "interwiki" ) {
87 $this->interwiki = $wgRequest->getVal( 'interwiki' );
88 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
89 $source = new WikiErrorMsg( "import-invalid-interwiki" );
90 } else {
91 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
92 $this->frompage = $wgRequest->getText( "frompage" );
93 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
94 $source = ImportStreamSource::newFromInterwiki(
95 $this->interwiki,
96 $this->frompage,
97 $this->history,
98 $this->includeTemplates,
99 $this->pageLinkDepth );
100 }
101 } else {
102 $source = new WikiErrorMsg( "importunknownsource" );
103 }
104
105 if( WikiError::isError( $source ) ) {
106 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
107 } else {
108 $wgOut->addWikiMsg( "importstart" );
109
110 $importer = new WikiImporter( $source );
111 if( !is_null( $this->namespace ) ) {
112 $importer->setTargetNamespace( $this->namespace );
113 }
114 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
115
116 $reporter->open();
117 $result = $importer->doImport();
118 $resultCount = $reporter->close();
119
120 if( WikiError::isError( $result ) ) {
121 # No source or XML parse error
122 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
123 } elseif( WikiError::isError( $resultCount ) ) {
124 # Zero revisions
125 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
126 } else {
127 # Success!
128 $wgOut->addWikiMsg( 'importsuccess' );
129 }
130 $wgOut->addWikiText( '<hr />' );
131 }
132 }
133
134 private function showForm() {
135 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
136 # FIXME: Quick hack to disable import for non privileged users /Raymond
137 # Regression from 43963
138 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
139 return $wgOut->permissionRequired( 'import' );
140
141 $action = $wgTitle->getLocalUrl( 'action=submit' );
142
143 if( $wgUser->isAllowed( 'importupload' ) ) {
144 $wgOut->addWikiMsg( "importtext" );
145 $wgOut->addHTML(
146 Xml::openElement( 'fieldset' ).
147 Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) .
148 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
149 Xml::hidden( 'action', 'submit' ) .
150 Xml::hidden( 'source', 'upload' ) .
151 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
152
153 "<tr>
154 <td class='mw-label'>" .
155 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
156 "</td>
157 <td class='mw-input'>" .
158 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
159 "</td>
160 </tr>
161 <tr>
162 <td class='mw-label'>" .
163 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
164 "</td>
165 <td class='mw-input'>" .
166 Xml::input( 'log-comment', 50, '',
167 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
168 "</td>
169 </tr>
170 <tr>
171 <td></td>
172 <td class='mw-input'>" .
173 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
174 "</td>
175 </tr>" .
176 Xml::closeElement( 'table' ).
177 Xml::hidden( 'editToken', $wgUser->editToken() ) .
178 Xml::closeElement( 'form' ) .
179 Xml::closeElement( 'fieldset' )
180 );
181 } else {
182 if( empty( $wgImportSources ) ) {
183 $wgOut->addWikiMsg( 'importnosources' );
184 }
185 }
186
187 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
188 $wgOut->addHTML(
189 Xml::openElement( 'fieldset' ) .
190 Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
191 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
192 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
193 Xml::hidden( 'action', 'submit' ) .
194 Xml::hidden( 'source', 'interwiki' ) .
195 Xml::hidden( 'editToken', $wgUser->editToken() ) .
196 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
197 "<tr>
198 <td class='mw-label'>" .
199 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
200 "</td>
201 <td class='mw-input'>" .
202 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
203 );
204 foreach( $wgImportSources as $prefix ) {
205 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
206 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
207 }
208 $wgOut->addHTML(
209 Xml::closeElement( 'select' ) .
210 Xml::input( 'frompage', 50, $this->frompage ) .
211 "</td>
212 </tr>
213 <tr>
214 <td>
215 </td>
216 <td class='mw-input'>" .
217 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
218 "</td>
219 </tr>
220 <tr>
221 <td>
222 </td>
223 <td class='mw-input'>" .
224 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
225 "</td>
226 </tr>
227 <tr>
228 <td>".wfMsgExt( 'export-pagelinks', 'parseinline' )."</td>
229 <td class=\"mw-input\">" . Xml::input( 'pagelink-depth', 20, 0 ) . "<br/>
230 </tr>
231 <tr>
232 <td>" .
233 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
234 "</td>
235 <td class='mw-input'>" .
236 Xml::namespaceSelector( $this->namespace, '' ) .
237 "</td>
238 </tr>
239 <tr>
240 <td class='mw-label'>" .
241 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
242 "</td>
243 <td class='mw-input'>" .
244 Xml::input( 'log-comment', 50, '',
245 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
246 "</td>
247 </tr>
248 <tr>
249 <td>
250 </td>
251 <td class='mw-input'>" .
252 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
253 "</td>
254 </tr>" .
255 Xml::closeElement( 'table' ).
256 Xml::closeElement( 'form' ) .
257 Xml::closeElement( 'fieldset' )
258 );
259 }
260 }
261 }
262
263 /**
264 * Reporting callback
265 * @ingroup SpecialPage
266 */
267 class ImportReporter {
268 private $reason=false;
269
270 function __construct( $importer, $upload, $interwiki , $reason=false ) {
271 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
272 $this->mPageCount = 0;
273 $this->mIsUpload = $upload;
274 $this->mInterwiki = $interwiki;
275 $this->reason = $reason;
276 }
277
278 function open() {
279 global $wgOut;
280 $wgOut->addHTML( "<ul>\n" );
281 }
282
283 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
284 global $wgOut, $wgUser, $wgLang, $wgContLang;
285
286 $skin = $wgUser->getSkin();
287
288 $this->mPageCount++;
289
290 $localCount = $wgLang->formatNum( $successCount );
291 $contentCount = $wgContLang->formatNum( $successCount );
292
293 if( $successCount > 0 ) {
294 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
295 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
296 "</li>\n"
297 );
298
299 $log = new LogPage( 'import' );
300 if( $this->mIsUpload ) {
301 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
302 $contentCount );
303 if ( $this->reason ) {
304 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
305 }
306 $log->addEntry( 'upload', $title, $detail );
307 } else {
308 $interwiki = '[[:' . $this->mInterwiki . ':' .
309 $origTitle->getPrefixedText() . ']]';
310 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
311 $contentCount, $interwiki );
312 if ( $this->reason ) {
313 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
314 }
315 $log->addEntry( 'interwiki', $title, $detail );
316 }
317
318 $comment = $detail; // quick
319 $dbw = wfGetDB( DB_MASTER );
320 $latest = $title->getLatestRevID();
321 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
322 $nullRevision->insertOn( $dbw );
323 $article = new Article( $title );
324 # Update page record
325 $article->updateRevisionOn( $dbw, $nullRevision );
326 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
327 } else {
328 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
329 }
330 }
331
332 function close() {
333 global $wgOut;
334 if( $this->mPageCount == 0 ) {
335 $wgOut->addHTML( "</ul>\n" );
336 return new WikiErrorMsg( "importnopages" );
337 }
338 $wgOut->addHTML( "</ul>\n" );
339
340 return $this->mPageCount;
341 }
342 }