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