Revert "Deprecate no longer used Skin::getCommonStylePath."
[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 $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
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, $wgImportSources ) ) {
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( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
139 } else {
140 $out->addWikiMsg( "importstart" );
141
142 $importer = new WikiImporter( $source->value );
143 if( !is_null( $this->namespace ) ) {
144 $importer->setTargetNamespace( $this->namespace );
145 }
146 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
147 $reporter->setContext( $this->getContext() );
148 $exception = false;
149
150 $reporter->open();
151 try {
152 $importer->doImport();
153 } catch ( MWException $e ) {
154 $exception = $e;
155 }
156 $result = $reporter->close();
157
158 if ( $exception ) {
159 # No source or XML parse error
160 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
161 } elseif( !$result->isGood() ) {
162 # Zero revisions
163 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
164 } else {
165 # Success!
166 $out->addWikiMsg( 'importsuccess' );
167 }
168 $out->addHTML( '<hr />' );
169 }
170 }
171
172 private function showForm() {
173 global $wgImportSources, $wgExportMaxLinkDepth;
174
175 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
176 $user = $this->getUser();
177 $out = $this->getOutput();
178
179 if( $user->isAllowed( 'importupload' ) ) {
180 $out->addWikiMsg( "importtext" );
181 $out->addHTML(
182 Xml::fieldset( $this->msg( 'import-upload' )->text() ).
183 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
184 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
185 Html::hidden( 'action', 'submit' ) .
186 Html::hidden( 'source', 'upload' ) .
187 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
188
189 "<tr>
190 <td class='mw-label'>" .
191 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
192 "</td>
193 <td class='mw-input'>" .
194 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
195 "</td>
196 </tr>
197 <tr>
198 <td class='mw-label'>" .
199 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
200 "</td>
201 <td class='mw-input'>" .
202 Xml::input( 'log-comment', 50, '',
203 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
204 "</td>
205 </tr>
206 <tr>
207 <td></td>
208 <td class='mw-submit'>" .
209 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
210 "</td>
211 </tr>" .
212 Xml::closeElement( 'table' ).
213 Html::hidden( 'editToken', $user->getEditToken() ) .
214 Xml::closeElement( 'form' ) .
215 Xml::closeElement( 'fieldset' )
216 );
217 } else {
218 if( empty( $wgImportSources ) ) {
219 $out->addWikiMsg( 'importnosources' );
220 }
221 }
222
223 if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
224 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
225 $importDepth = '';
226 if( $wgExportMaxLinkDepth > 0 ) {
227 $importDepth = "<tr>
228 <td class='mw-label'>" .
229 $this->msg( 'export-pagelinks' )->parse() .
230 "</td>
231 <td class='mw-input'>" .
232 Xml::input( 'pagelink-depth', 3, 0 ) .
233 "</td>
234 </tr>";
235 }
236
237 $out->addHTML(
238 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
239 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
240 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
241 Html::hidden( 'action', 'submit' ) .
242 Html::hidden( 'source', 'interwiki' ) .
243 Html::hidden( 'editToken', $user->getEditToken() ) .
244 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
245 "<tr>
246 <td class='mw-label'>" .
247 Xml::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
248 "</td>
249 <td class='mw-input'>" .
250 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
251 );
252 foreach( $wgImportSources as $prefix ) {
253 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
254 $out->addHTML( Xml::option( $prefix, $prefix, $selected ) );
255 }
256
257 $out->addHTML(
258 Xml::closeElement( 'select' ) .
259 Xml::input( 'frompage', 50, $this->frompage ) .
260 "</td>
261 </tr>
262 <tr>
263 <td>
264 </td>
265 <td class='mw-input'>" .
266 Xml::checkLabel( $this->msg( 'import-interwiki-history' )->text(), 'interwikiHistory', 'interwikiHistory', $this->history ) .
267 "</td>
268 </tr>
269 <tr>
270 <td>
271 </td>
272 <td class='mw-input'>" .
273 Xml::checkLabel( $this->msg( 'import-interwiki-templates' )->text(), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
274 "</td>
275 </tr>
276 $importDepth
277 <tr>
278 <td class='mw-label'>" .
279 Xml::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
280 "</td>
281 <td class='mw-input'>" .
282 Html::namespaceSelector(
283 array(
284 'selected' => $this->namespace,
285 'all' => '',
286 ), array(
287 'name' => 'namespace',
288 'id' => 'namespace',
289 'class' => 'namespaceselector',
290 )
291 ) .
292 "</td>
293 </tr>
294 <tr>
295 <td class='mw-label'>" .
296 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
297 "</td>
298 <td class='mw-input'>" .
299 Xml::input( 'log-comment', 50, '',
300 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
301 "</td>
302 </tr>
303 <tr>
304 <td>
305 </td>
306 <td class='mw-submit'>" .
307 Xml::submitButton( $this->msg( 'import-interwiki-submit' )->text(), Linker::tooltipAndAccesskeyAttribs( 'import' ) ) .
308 "</td>
309 </tr>" .
310 Xml::closeElement( 'table' ).
311 Xml::closeElement( 'form' ) .
312 Xml::closeElement( 'fieldset' )
313 );
314 }
315 }
316 }
317
318 /**
319 * Reporting callback
320 * @ingroup SpecialPage
321 */
322 class ImportReporter extends ContextSource {
323 private $reason=false;
324 private $mOriginalLogCallback = null;
325 private $mOriginalPageOutCallback = null;
326 private $mLogItemCount = 0;
327
328 function __construct( $importer, $upload, $interwiki , $reason=false ) {
329 $this->mOriginalPageOutCallback =
330 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
331 $this->mOriginalLogCallback =
332 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
333 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
334 $this->mPageCount = 0;
335 $this->mIsUpload = $upload;
336 $this->mInterwiki = $interwiki;
337 $this->reason = $reason;
338 }
339
340 function open() {
341 $this->getOutput()->addHTML( "<ul>\n" );
342 }
343
344 function reportNotice( $msg, array $params ) {
345 $this->getOutput()->addHTML( Html::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
346 }
347
348 function reportLogItem( /* ... */ ) {
349 $this->mLogItemCount++;
350 if ( is_callable( $this->mOriginalLogCallback ) ) {
351 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
352 }
353 }
354
355 /**
356 * @param Title $title
357 * @param Title $origTitle
358 * @param int $revisionCount
359 * @param $successCount
360 * @param $pageInfo
361 * @return void
362 */
363 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
364 $args = func_get_args();
365 call_user_func_array( $this->mOriginalPageOutCallback, $args );
366
367 if ( $title === null ) {
368 # Invalid or non-importable title; a notice is already displayed
369 return;
370 }
371
372 $this->mPageCount++;
373
374 if( $successCount > 0 ) {
375 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
376 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
377 "</li>\n"
378 );
379
380 $log = new LogPage( 'import' );
381 if( $this->mIsUpload ) {
382 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
383 $successCount )->inContentLanguage()->text();
384 if ( $this->reason ) {
385 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason;
386 }
387 $log->addEntry( 'upload', $title, $detail );
388 } else {
389 $interwiki = '[[:' . $this->mInterwiki . ':' .
390 $origTitle->getPrefixedText() . ']]';
391 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
392 $successCount )->params( $interwiki )->inContentLanguage()->text();
393 if ( $this->reason ) {
394 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason;
395 }
396 $log->addEntry( 'interwiki', $title, $detail );
397 }
398
399 $comment = $detail; // quick
400 $dbw = wfGetDB( DB_MASTER );
401 $latest = $title->getLatestRevID();
402 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleID(), $comment, true );
403 if (!is_null($nullRevision)) {
404 $nullRevision->insertOn( $dbw );
405 $page = WikiPage::factory( $title );
406 # Update page record
407 $page->updateRevisionOn( $dbw, $nullRevision );
408 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
409 }
410 } else {
411 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
412 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
413 }
414 }
415
416 function close() {
417 $out = $this->getOutput();
418 if ( $this->mLogItemCount > 0 ) {
419 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
420 $out->addHTML( Xml::tags( 'li', null, $msg ) );
421 } elseif( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
422 $out->addHTML( "</ul>\n" );
423 return Status::newFatal( 'importnopages' );
424 }
425 $out->addHTML( "</ul>\n" );
426
427 return Status::newGood( $this->mPageCount );
428 }
429 }