Never display empty sections on Special:Version
[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 * https://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 private $interwiki = false;
34 private $namespace;
35 private $rootpage = '';
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 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
48 }
49
50 /**
51 * Execute
52 */
53 function execute( $par ) {
54 $this->setHeaders();
55 $this->outputHeader();
56
57 $user = $this->getUser();
58 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
59 throw new PermissionsError( 'import' );
60 }
61
62 # @todo Allow Title::getUserPermissionsErrors() to take an array
63 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
64 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
65 $errors = wfMergeErrorArrays(
66 $this->getPageTitle()->getUserPermissionsErrors(
67 'import', $user, true,
68 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
69 ),
70 $this->getPageTitle()->getUserPermissionsErrors(
71 'importupload', $user, true,
72 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
73 )
74 );
75
76 if ( $errors ) {
77 throw new PermissionsError( 'import', $errors );
78 }
79
80 $this->checkReadOnly();
81
82 $request = $this->getRequest();
83 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
84 $this->doImport();
85 }
86 $this->showForm();
87 }
88
89 /**
90 * Do the actual import
91 */
92 private function doImport() {
93 $isUpload = false;
94 $request = $this->getRequest();
95 $this->namespace = $request->getIntOrNull( 'namespace' );
96 $sourceName = $request->getVal( "source" );
97
98 $this->logcomment = $request->getText( 'log-comment' );
99 $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
100 ? 0
101 : $request->getIntOrNull( 'pagelink-depth' );
102 $this->rootpage = $request->getText( 'rootpage' );
103
104 $user = $this->getUser();
105 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
106 $source = Status::newFatal( 'import-token-mismatch' );
107 } elseif ( $sourceName == 'upload' ) {
108 $isUpload = true;
109 if ( $user->isAllowed( 'importupload' ) ) {
110 $source = ImportStreamSource::newFromUpload( "xmlimport" );
111 } else {
112 throw new PermissionsError( 'importupload' );
113 }
114 } elseif ( $sourceName == "interwiki" ) {
115 if ( !$user->isAllowed( 'import' ) ) {
116 throw new PermissionsError( 'import' );
117 }
118 $this->interwiki = $request->getVal( 'interwiki' );
119 if ( !in_array( $this->interwiki, $this->getConfig()->get( 'ImportSources' ) ) ) {
120 $source = Status::newFatal( "import-invalid-interwiki" );
121 } else {
122 $this->history = $request->getCheck( 'interwikiHistory' );
123 $this->frompage = $request->getText( "frompage" );
124 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
125 $source = ImportStreamSource::newFromInterwiki(
126 $this->interwiki,
127 $this->frompage,
128 $this->history,
129 $this->includeTemplates,
130 $this->pageLinkDepth );
131 }
132 } else {
133 $source = Status::newFatal( "importunknownsource" );
134 }
135
136 $out = $this->getOutput();
137 if ( !$source->isGood() ) {
138 $out->wrapWikiMsg(
139 "<p class=\"error\">\n$1\n</p>",
140 array( 'importfailed', $source->getWikiText() )
141 );
142 } else {
143 $importer = new WikiImporter( $source->value );
144 if ( !is_null( $this->namespace ) ) {
145 $importer->setTargetNamespace( $this->namespace );
146 }
147 if ( !is_null( $this->rootpage ) ) {
148 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
149 if ( !$statusRootPage->isGood() ) {
150 $out->wrapWikiMsg(
151 "<p class=\"error\">\n$1\n</p>",
152 array(
153 'import-options-wrong',
154 $statusRootPage->getWikiText(),
155 count( $statusRootPage->getErrorsArray() )
156 )
157 );
158
159 return;
160 }
161 }
162
163 $out->addWikiMsg( "importstart" );
164
165 $reporter = new ImportReporter(
166 $importer,
167 $isUpload,
168 $this->interwiki,
169 $this->logcomment
170 );
171 $reporter->setContext( $this->getContext() );
172 $exception = false;
173
174 $reporter->open();
175 try {
176 $importer->doImport();
177 } catch ( MWException $e ) {
178 $exception = $e;
179 }
180 $result = $reporter->close();
181
182 if ( $exception ) {
183 # No source or XML parse error
184 $out->wrapWikiMsg(
185 "<p class=\"error\">\n$1\n</p>",
186 array( 'importfailed', $exception->getMessage() )
187 );
188 } elseif ( !$result->isGood() ) {
189 # Zero revisions
190 $out->wrapWikiMsg(
191 "<p class=\"error\">\n$1\n</p>",
192 array( 'importfailed', $result->getWikiText() )
193 );
194 } else {
195 # Success!
196 $out->addWikiMsg( 'importsuccess' );
197 }
198 $out->addHTML( '<hr />' );
199 }
200 }
201
202 private function showForm() {
203 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
204 $user = $this->getUser();
205 $out = $this->getOutput();
206 $importSources = $this->getConfig()->get( 'ImportSources' );
207
208 if ( $user->isAllowed( 'importupload' ) ) {
209 $out->addHTML(
210 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
211 Xml::openElement(
212 'form',
213 array(
214 'enctype' => 'multipart/form-data',
215 'method' => 'post',
216 'action' => $action,
217 'id' => 'mw-import-upload-form'
218 )
219 ) .
220 $this->msg( 'importtext' )->parseAsBlock() .
221 Html::hidden( 'action', 'submit' ) .
222 Html::hidden( 'source', 'upload' ) .
223 Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
224 "<tr>
225 <td class='mw-label'>" .
226 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
227 "</td>
228 <td class='mw-input'>" .
229 Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
230 "</td>
231 </tr>
232 <tr>
233 <td class='mw-label'>" .
234 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
235 "</td>
236 <td class='mw-input'>" .
237 Xml::input( 'log-comment', 50, '',
238 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
239 "</td>
240 </tr>
241 <tr>
242 <td class='mw-label'>" .
243 Xml::label(
244 $this->msg( 'import-interwiki-rootpage' )->text(),
245 'mw-interwiki-rootpage-upload'
246 ) .
247 "</td>
248 <td class='mw-input'>" .
249 Xml::input( 'rootpage', 50, $this->rootpage,
250 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
251 "</td>
252 </tr>
253 <tr>
254 <td></td>
255 <td class='mw-submit'>" .
256 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
257 "</td>
258 </tr>" .
259 Xml::closeElement( 'table' ) .
260 Html::hidden( 'editToken', $user->getEditToken() ) .
261 Xml::closeElement( 'form' ) .
262 Xml::closeElement( 'fieldset' )
263 );
264 } else {
265 if ( empty( $importSources ) ) {
266 $out->addWikiMsg( 'importnosources' );
267 }
268 }
269
270 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
271 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
272 $importDepth = '';
273 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
274 $importDepth = "<tr>
275 <td class='mw-label'>" .
276 $this->msg( 'export-pagelinks' )->parse() .
277 "</td>
278 <td class='mw-input'>" .
279 Xml::input( 'pagelink-depth', 3, 0 ) .
280 "</td>
281 </tr>";
282 }
283
284 $out->addHTML(
285 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
286 Xml::openElement(
287 'form',
288 array(
289 'method' => 'post',
290 'action' => $action,
291 'id' => 'mw-import-interwiki-form'
292 )
293 ) .
294 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
295 Html::hidden( 'action', 'submit' ) .
296 Html::hidden( 'source', 'interwiki' ) .
297 Html::hidden( 'editToken', $user->getEditToken() ) .
298 Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
299 "<tr>
300 <td class='mw-label'>" .
301 Xml::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
302 "</td>
303 <td class='mw-input'>" .
304 Xml::openElement(
305 'select',
306 array( 'name' => 'interwiki', 'id' => 'interwiki' )
307 )
308 );
309
310 foreach ( $importSources as $prefix ) {
311 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
312 $out->addHTML( Xml::option( $prefix, $prefix, $selected ) );
313 }
314
315 $out->addHTML(
316 Xml::closeElement( 'select' ) .
317 Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
318 "</td>
319 </tr>
320 <tr>
321 <td>
322 </td>
323 <td class='mw-input'>" .
324 Xml::checkLabel(
325 $this->msg( 'import-interwiki-history' )->text(),
326 'interwikiHistory',
327 'interwikiHistory',
328 $this->history
329 ) .
330 "</td>
331 </tr>
332 <tr>
333 <td>
334 </td>
335 <td class='mw-input'>" .
336 Xml::checkLabel(
337 $this->msg( 'import-interwiki-templates' )->text(),
338 'interwikiTemplates',
339 'interwikiTemplates',
340 $this->includeTemplates
341 ) .
342 "</td>
343 </tr>
344 $importDepth
345 <tr>
346 <td class='mw-label'>" .
347 Xml::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
348 "</td>
349 <td class='mw-input'>" .
350 Html::namespaceSelector(
351 array(
352 'selected' => $this->namespace,
353 'all' => '',
354 ), array(
355 'name' => 'namespace',
356 'id' => 'namespace',
357 'class' => 'namespaceselector',
358 )
359 ) .
360 "</td>
361 </tr>
362 <tr>
363 <td class='mw-label'>" .
364 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
365 "</td>
366 <td class='mw-input'>" .
367 Xml::input( 'log-comment', 50, '',
368 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
369 "</td>
370 </tr>
371 <tr>
372 <td class='mw-label'>" .
373 Xml::label(
374 $this->msg( 'import-interwiki-rootpage' )->text(),
375 'mw-interwiki-rootpage-interwiki'
376 ) .
377 "</td>
378 <td class='mw-input'>" .
379 Xml::input( 'rootpage', 50, $this->rootpage,
380 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
381 "</td>
382 </tr>
383 <tr>
384 <td>
385 </td>
386 <td class='mw-submit'>" .
387 Xml::submitButton(
388 $this->msg( 'import-interwiki-submit' )->text(),
389 Linker::tooltipAndAccesskeyAttribs( 'import' )
390 ) .
391 "</td>
392 </tr>" .
393 Xml::closeElement( 'table' ) .
394 Xml::closeElement( 'form' ) .
395 Xml::closeElement( 'fieldset' )
396 );
397 }
398 }
399
400 protected function getGroupName() {
401 return 'pagetools';
402 }
403 }
404
405 /**
406 * Reporting callback
407 * @ingroup SpecialPage
408 */
409 class ImportReporter extends ContextSource {
410 private $reason = false;
411 private $mOriginalLogCallback = null;
412 private $mOriginalPageOutCallback = null;
413 private $mLogItemCount = 0;
414
415 /**
416 * @param WikiImporter $importer
417 * @param bool $upload
418 * @param string $interwiki
419 * @param string|bool $reason
420 */
421 function __construct( $importer, $upload, $interwiki, $reason = false ) {
422 $this->mOriginalPageOutCallback =
423 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
424 $this->mOriginalLogCallback =
425 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
426 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
427 $this->mPageCount = 0;
428 $this->mIsUpload = $upload;
429 $this->mInterwiki = $interwiki;
430 $this->reason = $reason;
431 }
432
433 function open() {
434 $this->getOutput()->addHTML( "<ul>\n" );
435 }
436
437 function reportNotice( $msg, array $params ) {
438 $this->getOutput()->addHTML(
439 Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
440 );
441 }
442
443 function reportLogItem( /* ... */ ) {
444 $this->mLogItemCount++;
445 if ( is_callable( $this->mOriginalLogCallback ) ) {
446 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
447 }
448 }
449
450 /**
451 * @param Title $title
452 * @param Title $origTitle
453 * @param int $revisionCount
454 * @param int $successCount
455 * @param array $pageInfo
456 * @return void
457 */
458 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
459 $args = func_get_args();
460 call_user_func_array( $this->mOriginalPageOutCallback, $args );
461
462 if ( $title === null ) {
463 # Invalid or non-importable title; a notice is already displayed
464 return;
465 }
466
467 $this->mPageCount++;
468
469 if ( $successCount > 0 ) {
470 $this->getOutput()->addHTML(
471 "<li>" . Linker::linkKnown( $title ) . " " .
472 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
473 "</li>\n"
474 );
475
476 $log = new LogPage( 'import' );
477 if ( $this->mIsUpload ) {
478 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
479 $successCount )->inContentLanguage()->text();
480 if ( $this->reason ) {
481 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
482 . $this->reason;
483 }
484 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
485 } else {
486 $interwiki = '[[:' . $this->mInterwiki . ':' .
487 $origTitle->getPrefixedText() . ']]';
488 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
489 $successCount )->params( $interwiki )->inContentLanguage()->text();
490 if ( $this->reason ) {
491 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
492 . $this->reason;
493 }
494 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
495 }
496
497 $comment = $detail; // quick
498 $dbw = wfGetDB( DB_MASTER );
499 $latest = $title->getLatestRevID();
500 $nullRevision = Revision::newNullRevision(
501 $dbw,
502 $title->getArticleID(),
503 $comment,
504 true,
505 $this->getUser()
506 );
507
508 if ( !is_null( $nullRevision ) ) {
509 $nullRevision->insertOn( $dbw );
510 $page = WikiPage::factory( $title );
511 # Update page record
512 $page->updateRevisionOn( $dbw, $nullRevision );
513 wfRunHooks(
514 'NewRevisionFromEditComplete',
515 array( $page, $nullRevision, $latest, $this->getUser() )
516 );
517 }
518 } else {
519 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
520 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
521 }
522 }
523
524 function close() {
525 $out = $this->getOutput();
526 if ( $this->mLogItemCount > 0 ) {
527 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
528 $out->addHTML( Xml::tags( 'li', null, $msg ) );
529 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
530 $out->addHTML( "</ul>\n" );
531
532 return Status::newFatal( 'importnopages' );
533 }
534 $out->addHTML( "</ul>\n" );
535
536 return Status::newGood( $this->mPageCount );
537 }
538 }