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