(bug 15641) tweak Title::checkUserBlock() so that Title::getUserPermissionsErrors...
[lhc/web/wiklou.git] / includes / specials / SpecialImport.php
1 <?php
2 /**
3 * Implements Special:Import
4 *
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup SpecialPage
25 */
26
27 /**
28 * MediaWiki page data importer
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialImport extends SpecialPage {
33
34 private $interwiki = false;
35 private $namespace;
36 private $frompage = '';
37 private $logcomment= false;
38 private $history = true;
39 private $includeTemplates = false;
40 private $pageLinkDepth;
41
42 /**
43 * Constructor
44 */
45 public function __construct() {
46 parent::__construct( 'Import', 'import' );
47 global $wgImportTargetNamespace;
48 $this->namespace = $wgImportTargetNamespace;
49 }
50
51 /**
52 * Execute
53 */
54 function execute( $par ) {
55 global $wgRequest, $wgUser, $wgOut;
56
57 $this->setHeaders();
58 $this->outputHeader();
59
60 if ( wfReadOnly() ) {
61 global $wgOut;
62 $wgOut->readOnlyPage();
63 return;
64 }
65
66 if( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
67 return $wgOut->permissionRequired( 'import' );
68 }
69
70 # TODO: allow Title::getUserPermissionsErrors() to take an array
71 # FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
72 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
73 $errors = wfMergeErrorArrays(
74 $this->getTitle()->getUserPermissionsErrors( 'import', $wgUser, true, array( 'ns-specialprotected' ) ),
75 $this->getTitle()->getUserPermissionsErrors( 'importupload', $wgUser, true, array( 'ns-specialprotected' ) )
76 );
77 if( $errors ){
78 $wgOut->showPermissionsErrorPage( $errors );
79 return;
80 }
81
82 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
83 $this->doImport();
84 }
85 $this->showForm();
86 }
87
88 /**
89 * Do the actual import
90 */
91 private function doImport() {
92 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
93 $isUpload = false;
94 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
95 $sourceName = $wgRequest->getVal( "source" );
96
97 $this->logcomment = $wgRequest->getText( 'log-comment' );
98 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
99
100 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
101 $source = Status::newFatal( 'import-token-mismatch' );
102 } elseif ( $sourceName == 'upload' ) {
103 $isUpload = true;
104 if( $wgUser->isAllowed( 'importupload' ) ) {
105 $source = ImportStreamSource::newFromUpload( "xmlimport" );
106 } else {
107 return $wgOut->permissionRequired( 'importupload' );
108 }
109 } elseif ( $sourceName == "interwiki" ) {
110 $this->interwiki = $wgRequest->getVal( 'interwiki' );
111 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
112 $source = Status::newFatal( "import-invalid-interwiki" );
113 } else {
114 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
115 $this->frompage = $wgRequest->getText( "frompage" );
116 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
117 $source = ImportStreamSource::newFromInterwiki(
118 $this->interwiki,
119 $this->frompage,
120 $this->history,
121 $this->includeTemplates,
122 $this->pageLinkDepth );
123 }
124 } else {
125 $source = Status::newFatal( "importunknownsource" );
126 }
127
128 if( !$source->isGood() ) {
129 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
130 } else {
131 $wgOut->addWikiMsg( "importstart" );
132
133 $importer = new WikiImporter( $source->value );
134 if( !is_null( $this->namespace ) ) {
135 $importer->setTargetNamespace( $this->namespace );
136 }
137 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
138 $exception = false;
139
140 $reporter->open();
141 try {
142 $importer->doImport();
143 } catch ( MWException $e ) {
144 $exception = $e;
145 }
146 $result = $reporter->close();
147
148 if ( $exception ) {
149 # No source or XML parse error
150 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
151 } elseif( !$result->isGood() ) {
152 # Zero revisions
153 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
154 } else {
155 # Success!
156 $wgOut->addWikiMsg( 'importsuccess' );
157 }
158 $wgOut->addHTML( '<hr />' );
159 }
160 }
161
162 private function showForm() {
163 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
164
165 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
166
167 if( $wgUser->isAllowed( 'importupload' ) ) {
168 $wgOut->addWikiMsg( "importtext" );
169 $wgOut->addHTML(
170 Xml::fieldset( wfMsg( 'import-upload' ) ).
171 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
172 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
173 Html::hidden( 'action', 'submit' ) .
174 Html::hidden( 'source', 'upload' ) .
175 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
176
177 "<tr>
178 <td class='mw-label'>" .
179 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
180 "</td>
181 <td class='mw-input'>" .
182 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
183 "</td>
184 </tr>
185 <tr>
186 <td class='mw-label'>" .
187 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
188 "</td>
189 <td class='mw-input'>" .
190 Xml::input( 'log-comment', 50, '',
191 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
192 "</td>
193 </tr>
194 <tr>
195 <td></td>
196 <td class='mw-submit'>" .
197 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
198 "</td>
199 </tr>" .
200 Xml::closeElement( 'table' ).
201 Html::hidden( 'editToken', $wgUser->editToken() ) .
202 Xml::closeElement( 'form' ) .
203 Xml::closeElement( 'fieldset' )
204 );
205 } else {
206 if( empty( $wgImportSources ) ) {
207 $wgOut->addWikiMsg( 'importnosources' );
208 }
209 }
210
211 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
212 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
213 $importDepth = '';
214 if( $wgExportMaxLinkDepth > 0 ) {
215 $importDepth = "<tr>
216 <td class='mw-label'>" .
217 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
218 "</td>
219 <td class='mw-input'>" .
220 Xml::input( 'pagelink-depth', 3, 0 ) .
221 "</td>
222 </tr>";
223 }
224
225 $wgOut->addHTML(
226 Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
227 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
228 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
229 Html::hidden( 'action', 'submit' ) .
230 Html::hidden( 'source', 'interwiki' ) .
231 Html::hidden( 'editToken', $wgUser->editToken() ) .
232 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
233 "<tr>
234 <td class='mw-label'>" .
235 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
236 "</td>
237 <td class='mw-input'>" .
238 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
239 );
240 foreach( $wgImportSources as $prefix ) {
241 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
242 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
243 }
244
245 $wgOut->addHTML(
246 Xml::closeElement( 'select' ) .
247 Xml::input( 'frompage', 50, $this->frompage ) .
248 "</td>
249 </tr>
250 <tr>
251 <td>
252 </td>
253 <td class='mw-input'>" .
254 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
255 "</td>
256 </tr>
257 <tr>
258 <td>
259 </td>
260 <td class='mw-input'>" .
261 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
262 "</td>
263 </tr>
264 $importDepth
265 <tr>
266 <td class='mw-label'>" .
267 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
268 "</td>
269 <td class='mw-input'>" .
270 Xml::namespaceSelector( $this->namespace, '' ) .
271 "</td>
272 </tr>
273 <tr>
274 <td class='mw-label'>" .
275 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
276 "</td>
277 <td class='mw-input'>" .
278 Xml::input( 'log-comment', 50, '',
279 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
280 "</td>
281 </tr>
282 <tr>
283 <td>
284 </td>
285 <td class='mw-submit'>" .
286 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
287 "</td>
288 </tr>" .
289 Xml::closeElement( 'table' ).
290 Xml::closeElement( 'form' ) .
291 Xml::closeElement( 'fieldset' )
292 );
293 }
294 }
295 }
296
297 /**
298 * Reporting callback
299 * @ingroup SpecialPage
300 */
301 class ImportReporter {
302 private $reason=false;
303 private $mOriginalLogCallback = null;
304 private $mOriginalPageOutCallback = null;
305 private $mLogItemCount = 0;
306
307 function __construct( $importer, $upload, $interwiki , $reason=false ) {
308 $this->mOriginalPageOutCallback =
309 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
310 $this->mOriginalLogCallback =
311 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
312 $this->mPageCount = 0;
313 $this->mIsUpload = $upload;
314 $this->mInterwiki = $interwiki;
315 $this->reason = $reason;
316 }
317
318 function open() {
319 global $wgOut;
320 $wgOut->addHTML( "<ul>\n" );
321 }
322
323 function reportLogItem( /* ... */ ) {
324 $this->mLogItemCount++;
325 if ( is_callable( $this->mOriginalLogCallback ) ) {
326 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
327 }
328 }
329
330 /**
331 * @param Title $title
332 * @param Title $origTitle
333 * @param int $revisionCount
334 * @param $successCount
335 * @param $pageInfo
336 * @return void
337 */
338 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
339 global $wgOut, $wgUser, $wgLang, $wgContLang;
340
341 $args = func_get_args();
342 call_user_func_array( $this->mOriginalPageOutCallback, $args );
343
344 $skin = $wgUser->getSkin();
345
346 $this->mPageCount++;
347
348 $localCount = $wgLang->formatNum( $successCount );
349 $contentCount = $wgContLang->formatNum( $successCount );
350
351 if( $successCount > 0 ) {
352 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
353 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
354 "</li>\n"
355 );
356
357 $log = new LogPage( 'import' );
358 if( $this->mIsUpload ) {
359 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
360 $contentCount );
361 if ( $this->reason ) {
362 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
363 }
364 $log->addEntry( 'upload', $title, $detail );
365 } else {
366 $interwiki = '[[:' . $this->mInterwiki . ':' .
367 $origTitle->getPrefixedText() . ']]';
368 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
369 $contentCount, $interwiki );
370 if ( $this->reason ) {
371 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
372 }
373 $log->addEntry( 'interwiki', $title, $detail );
374 }
375
376 $comment = $detail; // quick
377 $dbw = wfGetDB( DB_MASTER );
378 $latest = $title->getLatestRevID();
379 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
380 $nullRevision->insertOn( $dbw );
381 $article = new Article( $title );
382 # Update page record
383 $article->updateRevisionOn( $dbw, $nullRevision );
384 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
385 } else {
386 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
387 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
388 }
389 }
390
391 function close() {
392 global $wgOut, $wgLang;
393
394 if ( $this->mLogItemCount > 0 ) {
395 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
396 $wgLang->formatNum( $this->mLogItemCount ) );
397 $wgOut->addHTML( Xml::tags( 'li', null, $msg ) );
398 } elseif( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
399 $wgOut->addHTML( "</ul>\n" );
400 return Status::newFatal( 'importnopages' );
401 }
402 $wgOut->addHTML( "</ul>\n" );
403
404 return Status::newGood( $this->mPageCount );
405 }
406 }