Update formatting
[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 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 global $wgImportTargetNamespace;
48 $this->namespace = $wgImportTargetNamespace;
49 }
50
51 /**
52 * Execute
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->getTitle()->getUserPermissionsErrors(
68 'import', $user, true,
69 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
70 ),
71 $this->getTitle()->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 global $wgImportSources, $wgExportMaxLinkDepth;
95
96 $isUpload = false;
97 $request = $this->getRequest();
98 $this->namespace = $request->getIntOrNull( 'namespace' );
99 $sourceName = $request->getVal( "source" );
100
101 $this->logcomment = $request->getText( 'log-comment' );
102 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $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, $wgImportSources ) ) {
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 global $wgImportSources, $wgExportMaxLinkDepth;
205
206 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
207 $user = $this->getUser();
208 $out = $this->getOutput();
209
210 if ( $user->isAllowed( 'importupload' ) ) {
211 $out->addHTML(
212 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
213 Xml::openElement(
214 'form',
215 array(
216 'enctype' => 'multipart/form-data',
217 'method' => 'post',
218 'action' => $action,
219 'id' => 'mw-import-upload-form'
220 )
221 ) .
222 $this->msg( 'importtext' )->parseAsBlock() .
223 Html::hidden( 'action', 'submit' ) .
224 Html::hidden( 'source', 'upload' ) .
225 Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
226 "<tr>
227 <td class='mw-label'>" .
228 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
229 "</td>
230 <td class='mw-input'>" .
231 Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
232 "</td>
233 </tr>
234 <tr>
235 <td class='mw-label'>" .
236 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
237 "</td>
238 <td class='mw-input'>" .
239 Xml::input( 'log-comment', 50, '',
240 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
241 "</td>
242 </tr>
243 <tr>
244 <td class='mw-label'>" .
245 Xml::label( $this->msg( 'import-interwiki-rootpage' )->text(), 'mw-interwiki-rootpage-upload' ) .
246 "</td>
247 <td class='mw-input'>" .
248 Xml::input( 'rootpage', 50, $this->rootpage,
249 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
250 "</td>
251 </tr>
252 <tr>
253 <td></td>
254 <td class='mw-submit'>" .
255 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
256 "</td>
257 </tr>" .
258 Xml::closeElement( 'table' ) .
259 Html::hidden( 'editToken', $user->getEditToken() ) .
260 Xml::closeElement( 'form' ) .
261 Xml::closeElement( 'fieldset' )
262 );
263 } else {
264 if ( empty( $wgImportSources ) ) {
265 $out->addWikiMsg( 'importnosources' );
266 }
267 }
268
269 if ( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
270 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
271 $importDepth = '';
272 if ( $wgExportMaxLinkDepth > 0 ) {
273 $importDepth = "<tr>
274 <td class='mw-label'>" .
275 $this->msg( 'export-pagelinks' )->parse() .
276 "</td>
277 <td class='mw-input'>" .
278 Xml::input( 'pagelink-depth', 3, 0 ) .
279 "</td>
280 </tr>";
281 }
282
283 $out->addHTML(
284 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
285 Xml::openElement(
286 'form',
287 array(
288 'method' => 'post',
289 'action' => $action,
290 'id' => 'mw-import-interwiki-form'
291 )
292 ) .
293 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
294 Html::hidden( 'action', 'submit' ) .
295 Html::hidden( 'source', 'interwiki' ) .
296 Html::hidden( 'editToken', $user->getEditToken() ) .
297 Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
298 "<tr>
299 <td class='mw-label'>" .
300 Xml::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
301 "</td>
302 <td class='mw-input'>" .
303 Xml::openElement(
304 'select',
305 array( 'name' => 'interwiki', 'id' => 'interwiki' )
306 )
307 );
308
309 foreach ( $wgImportSources as $prefix ) {
310 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
311 $out->addHTML( Xml::option( $prefix, $prefix, $selected ) );
312 }
313
314 $out->addHTML(
315 Xml::closeElement( 'select' ) .
316 Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
317 "</td>
318 </tr>
319 <tr>
320 <td>
321 </td>
322 <td class='mw-input'>" .
323 Xml::checkLabel(
324 $this->msg( 'import-interwiki-history' )->text(),
325 'interwikiHistory',
326 'interwikiHistory',
327 $this->history
328 ) .
329 "</td>
330 </tr>
331 <tr>
332 <td>
333 </td>
334 <td class='mw-input'>" .
335 Xml::checkLabel(
336 $this->msg( 'import-interwiki-templates' )->text(),
337 'interwikiTemplates',
338 'interwikiTemplates',
339 $this->includeTemplates
340 ) .
341 "</td>
342 </tr>
343 $importDepth
344 <tr>
345 <td class='mw-label'>" .
346 Xml::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
347 "</td>
348 <td class='mw-input'>" .
349 Html::namespaceSelector(
350 array(
351 'selected' => $this->namespace,
352 'all' => '',
353 ), array(
354 'name' => 'namespace',
355 'id' => 'namespace',
356 'class' => 'namespaceselector',
357 )
358 ) .
359 "</td>
360 </tr>
361 <tr>
362 <td class='mw-label'>" .
363 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
364 "</td>
365 <td class='mw-input'>" .
366 Xml::input( 'log-comment', 50, '',
367 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
368 "</td>
369 </tr>
370 <tr>
371 <td class='mw-label'>" .
372 Xml::label(
373 $this->msg( 'import-interwiki-rootpage' )->text(),
374 'mw-interwiki-rootpage-interwiki'
375 ) .
376 "</td>
377 <td class='mw-input'>" .
378 Xml::input( 'rootpage', 50, $this->rootpage,
379 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
380 "</td>
381 </tr>
382 <tr>
383 <td>
384 </td>
385 <td class='mw-submit'>" .
386 Xml::submitButton(
387 $this->msg( 'import-interwiki-submit' )->text(),
388 Linker::tooltipAndAccesskeyAttribs( 'import' )
389 ) .
390 "</td>
391 </tr>" .
392 Xml::closeElement( 'table' ) .
393 Xml::closeElement( 'form' ) .
394 Xml::closeElement( 'fieldset' )
395 );
396 }
397 }
398
399 protected function getGroupName() {
400 return 'pagetools';
401 }
402 }
403
404 /**
405 * Reporting callback
406 * @ingroup SpecialPage
407 */
408 class ImportReporter extends ContextSource {
409 private $reason = false;
410 private $mOriginalLogCallback = null;
411 private $mOriginalPageOutCallback = null;
412 private $mLogItemCount = 0;
413
414
415 /**
416 * @param WikiImporter $importer
417 * @param $upload
418 * @param $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( Html::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
439 }
440
441 function reportLogItem( /* ... */ ) {
442 $this->mLogItemCount++;
443 if ( is_callable( $this->mOriginalLogCallback ) ) {
444 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
445 }
446 }
447
448 /**
449 * @param Title $title
450 * @param Title $origTitle
451 * @param int $revisionCount
452 * @param $successCount
453 * @param $pageInfo
454 * @return void
455 */
456 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
457 $args = func_get_args();
458 call_user_func_array( $this->mOriginalPageOutCallback, $args );
459
460 if ( $title === null ) {
461 # Invalid or non-importable title; a notice is already displayed
462 return;
463 }
464
465 $this->mPageCount++;
466
467 if ( $successCount > 0 ) {
468 $this->getOutput()->addHTML(
469 "<li>" . Linker::linkKnown( $title ) . " " .
470 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
471 "</li>\n"
472 );
473
474 $log = new LogPage( 'import' );
475 if ( $this->mIsUpload ) {
476 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
477 $successCount )->inContentLanguage()->text();
478 if ( $this->reason ) {
479 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason;
480 }
481 $log->addEntry( 'upload', $title, $detail );
482 } else {
483 $interwiki = '[[:' . $this->mInterwiki . ':' .
484 $origTitle->getPrefixedText() . ']]';
485 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
486 $successCount )->params( $interwiki )->inContentLanguage()->text();
487 if ( $this->reason ) {
488 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason;
489 }
490 $log->addEntry( 'interwiki', $title, $detail );
491 }
492
493 $comment = $detail; // quick
494 $dbw = wfGetDB( DB_MASTER );
495 $latest = $title->getLatestRevID();
496 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleID(), $comment, true );
497 if ( !is_null( $nullRevision ) ) {
498 $nullRevision->insertOn( $dbw );
499 $page = WikiPage::factory( $title );
500 # Update page record
501 $page->updateRevisionOn( $dbw, $nullRevision );
502 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
503 }
504 } else {
505 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
506 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
507 }
508 }
509
510 function close() {
511 $out = $this->getOutput();
512 if ( $this->mLogItemCount > 0 ) {
513 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
514 $out->addHTML( Xml::tags( 'li', null, $msg ) );
515 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
516 $out->addHTML( "</ul>\n" );
517
518 return Status::newFatal( 'importnopages' );
519 }
520 $out->addHTML( "</ul>\n" );
521
522 return Status::newGood( $this->mPageCount );
523 }
524 }