MediaWiki REL1_31
SpecialImport.php
Go to the documentation of this file.
1<?php
33 private $sourceName = false;
34 private $interwiki = false;
35 private $subproject;
37 private $mapping = 'default';
38 private $namespace;
39 private $rootpage = '';
40 private $frompage = '';
41 private $logcomment = false;
42 private $history = true;
43 private $includeTemplates = false;
48
49 public function __construct() {
50 parent::__construct( 'Import', 'import' );
51 }
52
53 public function doesWrites() {
54 return true;
55 }
56
63 function execute( $par ) {
65
66 $this->setHeaders();
67 $this->outputHeader();
68
69 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
70
71 $this->getOutput()->addModules( 'mediawiki.special.import' );
72
73 $this->importSources = $this->getConfig()->get( 'ImportSources' );
74 Hooks::run( 'ImportSources', [ &$this->importSources ] );
75
76 $user = $this->getUser();
77 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
78 throw new PermissionsError( 'import' );
79 }
80
81 # @todo Allow Title::getUserPermissionsErrors() to take an array
82 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
83 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
84 $errors = wfMergeErrorArrays(
85 $this->getPageTitle()->getUserPermissionsErrors(
86 'import', $user, true,
87 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
88 ),
89 $this->getPageTitle()->getUserPermissionsErrors(
90 'importupload', $user, true,
91 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
92 )
93 );
94
95 if ( $errors ) {
96 throw new PermissionsError( 'import', $errors );
97 }
98
99 $this->checkReadOnly();
100
101 $request = $this->getRequest();
102 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
103 $this->doImport();
104 }
105 $this->showForm();
106 }
107
111 private function doImport() {
112 $isUpload = false;
113 $request = $this->getRequest();
114 $this->sourceName = $request->getVal( "source" );
115 $this->assignKnownUsers = $request->getCheck( 'assignKnownUsers' );
116
117 $this->logcomment = $request->getText( 'log-comment' );
118 $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
119 ? 0
120 : $request->getIntOrNull( 'pagelink-depth' );
121
122 $this->mapping = $request->getVal( 'mapping' );
123 if ( $this->mapping === 'namespace' ) {
124 $this->namespace = $request->getIntOrNull( 'namespace' );
125 } elseif ( $this->mapping === 'subpage' ) {
126 $this->rootpage = $request->getText( 'rootpage' );
127 } else {
128 $this->mapping = 'default';
129 }
130
131 $user = $this->getUser();
132 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
133 $source = Status::newFatal( 'import-token-mismatch' );
134 } elseif ( $this->sourceName === 'upload' ) {
135 $isUpload = true;
136 $this->usernamePrefix = $this->fullInterwikiPrefix = $request->getVal( 'usernamePrefix' );
137 if ( $user->isAllowed( 'importupload' ) ) {
139 } else {
140 throw new PermissionsError( 'importupload' );
141 }
142 } elseif ( $this->sourceName === 'interwiki' ) {
143 if ( !$user->isAllowed( 'import' ) ) {
144 throw new PermissionsError( 'import' );
145 }
146 $this->interwiki = $this->fullInterwikiPrefix = $request->getVal( 'interwiki' );
147 // does this interwiki have subprojects?
148 $hasSubprojects = array_key_exists( $this->interwiki, $this->importSources );
149 if ( !$hasSubprojects && !in_array( $this->interwiki, $this->importSources ) ) {
150 $source = Status::newFatal( "import-invalid-interwiki" );
151 } else {
152 if ( $hasSubprojects ) {
153 $this->subproject = $request->getVal( 'subproject' );
154 $this->fullInterwikiPrefix .= ':' . $request->getVal( 'subproject' );
155 }
156 if ( $hasSubprojects &&
157 !in_array( $this->subproject, $this->importSources[$this->interwiki] )
158 ) {
159 $source = Status::newFatal( "import-invalid-interwiki" );
160 } else {
161 $this->history = $request->getCheck( 'interwikiHistory' );
162 $this->frompage = $request->getText( "frompage" );
163 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
165 $this->fullInterwikiPrefix,
166 $this->frompage,
167 $this->history,
168 $this->includeTemplates,
169 $this->pageLinkDepth );
170 }
171 }
172 } else {
173 $source = Status::newFatal( "importunknownsource" );
174 }
175
176 if ( (string)$this->fullInterwikiPrefix === '' ) {
177 $source->fatal( 'importnoprefix' );
178 }
179
180 $out = $this->getOutput();
181 if ( !$source->isGood() ) {
182 $out->addWikiText( "<p class=\"error\">\n" .
183 $this->msg( 'importfailed', $source->getWikiText() )->parse() . "\n</p>" );
184 } else {
185 $importer = new WikiImporter( $source->value, $this->getConfig() );
186 if ( !is_null( $this->namespace ) ) {
187 $importer->setTargetNamespace( $this->namespace );
188 } elseif ( !is_null( $this->rootpage ) ) {
189 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
190 if ( !$statusRootPage->isGood() ) {
191 $out->wrapWikiMsg(
192 "<p class=\"error\">\n$1\n</p>",
193 [
194 'import-options-wrong',
195 $statusRootPage->getWikiText(),
196 count( $statusRootPage->getErrorsArray() )
197 ]
198 );
199
200 return;
201 }
202 }
203 $importer->setUsernamePrefix( $this->fullInterwikiPrefix, $this->assignKnownUsers );
204
205 $out->addWikiMsg( "importstart" );
206
207 $reporter = new ImportReporter(
208 $importer,
209 $isUpload,
210 $this->fullInterwikiPrefix,
211 $this->logcomment
212 );
213 $reporter->setContext( $this->getContext() );
214 $exception = false;
215
216 $reporter->open();
217 try {
218 $importer->doImport();
219 } catch ( Exception $e ) {
220 $exception = $e;
221 }
222 $result = $reporter->close();
223
224 if ( $exception ) {
225 # No source or XML parse error
226 $out->wrapWikiMsg(
227 "<p class=\"error\">\n$1\n</p>",
228 [ 'importfailed', $exception->getMessage() ]
229 );
230 } elseif ( !$result->isGood() ) {
231 # Zero revisions
232 $out->wrapWikiMsg(
233 "<p class=\"error\">\n$1\n</p>",
234 [ 'importfailed', $result->getWikiText() ]
235 );
236 } else {
237 # Success!
238 $out->addWikiMsg( 'importsuccess' );
239 }
240 $out->addHTML( '<hr />' );
241 }
242 }
243
244 private function getMappingFormPart( $sourceName ) {
245 $isSameSourceAsBefore = ( $this->sourceName === $sourceName );
246 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
247 return "<tr>
248 <td>
249 </td>
250 <td class='mw-input'>" .
251 Xml::radioLabel(
252 $this->msg( 'import-mapping-default' )->text(),
253 'mapping',
254 'default',
255 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
256 "mw-import-mapping-$sourceName-default",
257 ( $isSameSourceAsBefore ?
258 ( $this->mapping === 'default' ) :
259 is_null( $defaultNamespace ) )
260 ) .
261 "</td>
262 </tr>
263 <tr>
264 <td>
265 </td>
266 <td class='mw-input'>" .
267 Xml::radioLabel(
268 $this->msg( 'import-mapping-namespace' )->text(),
269 'mapping',
270 'namespace',
271 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
272 "mw-import-mapping-$sourceName-namespace",
273 ( $isSameSourceAsBefore ?
274 ( $this->mapping === 'namespace' ) :
275 !is_null( $defaultNamespace ) )
276 ) . ' ' .
277 Html::namespaceSelector(
278 [
279 'selected' => ( $isSameSourceAsBefore ?
280 $this->namespace :
281 ( $defaultNamespace || '' ) ),
282 ], [
283 'name' => "namespace",
284 // mw-import-namespace-interwiki, mw-import-namespace-upload
285 'id' => "mw-import-namespace-$sourceName",
286 'class' => 'namespaceselector',
287 ]
288 ) .
289 "</td>
290 </tr>
291 <tr>
292 <td>
293 </td>
294 <td class='mw-input'>" .
295 Xml::radioLabel(
296 $this->msg( 'import-mapping-subpage' )->text(),
297 'mapping',
298 'subpage',
299 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
300 "mw-import-mapping-$sourceName-subpage",
301 ( $isSameSourceAsBefore ? ( $this->mapping === 'subpage' ) : '' )
302 ) . ' ' .
303 Xml::input( 'rootpage', 50,
304 ( $isSameSourceAsBefore ? $this->rootpage : '' ),
305 [
306 // Should be "mw-import-rootpage-...", but we keep this inaccurate
307 // ID for legacy reasons
308 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
309 'id' => "mw-interwiki-rootpage-$sourceName",
310 'type' => 'text'
311 ]
312 ) . ' ' .
313 "</td>
314 </tr>";
315 }
316
317 private function showForm() {
318 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
319 $user = $this->getUser();
320 $out = $this->getOutput();
321 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
322
323 if ( $user->isAllowed( 'importupload' ) ) {
324 $mappingSelection = $this->getMappingFormPart( 'upload' );
325 $out->addHTML(
326 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
327 Xml::openElement(
328 'form',
329 [
330 'enctype' => 'multipart/form-data',
331 'method' => 'post',
332 'action' => $action,
333 'id' => 'mw-import-upload-form'
334 ]
335 ) .
336 $this->msg( 'importtext' )->parseAsBlock() .
337 Html::hidden( 'action', 'submit' ) .
338 Html::hidden( 'source', 'upload' ) .
339 Xml::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
340 "<tr>
341 <td class='mw-label'>" .
342 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
343 "</td>
344 <td class='mw-input'>" .
345 Html::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
346 "</td>
347 </tr>
348 <tr>
349 <td class='mw-label'>" .
350 Xml::label( $this->msg( 'import-upload-username-prefix' )->text(),
351 'mw-import-usernamePrefix' ) .
352 "</td>
353 <td class='mw-input'>" .
354 Xml::input( 'usernamePrefix', 50,
355 $this->usernamePrefix,
356 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
357 "</td>
358 </tr>
359 <tr>
360 <td></td>
361 <td class='mw-input'>" .
362 Xml::checkLabel(
363 $this->msg( 'import-assign-known-users' )->text(),
364 'assignKnownUsers',
365 'assignKnownUsers',
366 $this->assignKnownUsers
367 ) .
368 "</td>
369 </tr>
370 <tr>
371 <td class='mw-label'>" .
372 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
373 "</td>
374 <td class='mw-input'>" .
375 Xml::input( 'log-comment', 50,
376 ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
377 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
378 "</td>
379 </tr>
380 $mappingSelection
381 <tr>
382 <td></td>
383 <td class='mw-submit'>" .
384 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
385 "</td>
386 </tr>" .
387 Xml::closeElement( 'table' ) .
388 Html::hidden( 'editToken', $user->getEditToken() ) .
389 Xml::closeElement( 'form' ) .
390 Xml::closeElement( 'fieldset' )
391 );
392 } else {
393 if ( empty( $this->importSources ) ) {
394 $out->addWikiMsg( 'importnosources' );
395 }
396 }
397
398 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources ) ) {
399 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
400 $importDepth = '';
401 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
402 $importDepth = "<tr>
403 <td class='mw-label'>" .
404 $this->msg( 'export-pagelinks' )->parse() .
405 "</td>
406 <td class='mw-input'>" .
407 Xml::input( 'pagelink-depth', 3, 0 ) .
408 "</td>
409 </tr>";
410 }
411 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
412
413 $out->addHTML(
414 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
415 Xml::openElement(
416 'form',
417 [
418 'method' => 'post',
419 'action' => $action,
420 'id' => 'mw-import-interwiki-form'
421 ]
422 ) .
423 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
424 Html::hidden( 'action', 'submit' ) .
425 Html::hidden( 'source', 'interwiki' ) .
426 Html::hidden( 'editToken', $user->getEditToken() ) .
427 Xml::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
428 "<tr>
429 <td class='mw-label'>" .
430 Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
431 "</td>
432 <td class='mw-input'>" .
433 Xml::openElement(
434 'select',
435 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
436 )
437 );
438
439 $needSubprojectField = false;
440 foreach ( $this->importSources as $key => $value ) {
441 if ( is_int( $key ) ) {
442 $key = $value;
443 } elseif ( $value !== $key ) {
444 $needSubprojectField = true;
445 }
446
447 $attribs = [
448 'value' => $key,
449 ];
450 if ( is_array( $value ) ) {
451 $attribs['data-subprojects'] = implode( ' ', $value );
452 }
453 if ( $this->interwiki === $key ) {
454 $attribs['selected'] = 'selected';
455 }
456 $out->addHTML( Html::element( 'option', $attribs, $key ) );
457 }
458
459 $out->addHTML(
460 Xml::closeElement( 'select' )
461 );
462
463 if ( $needSubprojectField ) {
464 $out->addHTML(
465 Xml::openElement(
466 'select',
467 [ 'name' => 'subproject', 'id' => 'subproject' ]
468 )
469 );
470
471 $subprojectsToAdd = [];
472 foreach ( $this->importSources as $key => $value ) {
473 if ( is_array( $value ) ) {
474 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
475 }
476 }
477 $subprojectsToAdd = array_unique( $subprojectsToAdd );
478 sort( $subprojectsToAdd );
479 foreach ( $subprojectsToAdd as $subproject ) {
480 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
481 }
482
483 $out->addHTML(
484 Xml::closeElement( 'select' )
485 );
486 }
487
488 $out->addHTML(
489 "</td>
490 </tr>
491 <tr>
492 <td class='mw-label'>" .
493 Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
494 "</td>
495 <td class='mw-input'>" .
496 Xml::input( 'frompage', 50, $this->frompage, [ 'id' => 'frompage' ] ) .
497 "</td>
498 </tr>
499 <tr>
500 <td>
501 </td>
502 <td class='mw-input'>" .
503 Xml::checkLabel(
504 $this->msg( 'import-interwiki-history' )->text(),
505 'interwikiHistory',
506 'interwikiHistory',
507 $this->history
508 ) .
509 "</td>
510 </tr>
511 <tr>
512 <td>
513 </td>
514 <td class='mw-input'>" .
515 Xml::checkLabel(
516 $this->msg( 'import-interwiki-templates' )->text(),
517 'interwikiTemplates',
518 'interwikiTemplates',
519 $this->includeTemplates
520 ) .
521 "</td>
522 </tr>
523 <tr>
524 <td></td>
525 <td class='mw-input'>" .
526 Xml::checkLabel(
527 $this->msg( 'import-assign-known-users' )->text(),
528 'assignKnownUsers',
529 'assignKnownUsers',
530 $this->assignKnownUsers
531 ) .
532 "</td>
533 </tr>
534 $importDepth
535 <tr>
536 <td class='mw-label'>" .
537 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
538 "</td>
539 <td class='mw-input'>" .
540 Xml::input( 'log-comment', 50,
541 ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
542 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
543 "</td>
544 </tr>
545 $mappingSelection
546 <tr>
547 <td>
548 </td>
549 <td class='mw-submit'>" .
550 Xml::submitButton(
551 $this->msg( 'import-interwiki-submit' )->text(),
553 ) .
554 "</td>
555 </tr>" .
556 Xml::closeElement( 'table' ) .
557 Xml::closeElement( 'form' ) .
558 Xml::closeElement( 'fieldset' )
559 );
560 }
561 }
562
563 protected function getGroupName() {
564 return 'pagetools';
565 }
566}
wfMergeErrorArrays()
Merge arrays in the style of getUserPermissionsErrors, with duplicate removal e.g.
Reporting callback.
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromUpload( $fieldname="xmlimport")
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null)
Returns the attributes for the tooltip and access key.
Definition Linker.php:2135
Show an error when a user tries to do something they do not have the necessary permissions for.
MediaWiki page data importer.
doesWrites()
Indicates whether this special page may perform database writes.
getMappingFormPart( $sourceName)
execute( $par)
Execute.
doImport()
Do the actual import.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
XML file reader for the page data importer.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc next in line in page history
Definition hooks.txt:1777
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2014
returning false will NOT prevent logging $e
Definition hooks.txt:2176
$source