Kill "* @return void"
[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( wfMsg( 'import-upload' ) ).
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( wfMsg( 'import-upload-filename' ), '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( wfMsg( 'import-comment' ), '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( wfMsg( 'uploadbtn' ) ) .
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 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
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( wfMsg( 'importinterwiki' ) ) .
239 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
240 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
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( wfMsg( 'import-interwiki-source' ), '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( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
267 "</td>
268 </tr>
269 <tr>
270 <td>
271 </td>
272 <td class='mw-input'>" .
273 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
274 "</td>
275 </tr>
276 $importDepth
277 <tr>
278 <td class='mw-label'>" .
279 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
280 "</td>
281 <td class='mw-input'>" .
282 Xml::namespaceSelector( $this->namespace, '' ) .
283 "</td>
284 </tr>
285 <tr>
286 <td class='mw-label'>" .
287 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
288 "</td>
289 <td class='mw-input'>" .
290 Xml::input( 'log-comment', 50, '',
291 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
292 "</td>
293 </tr>
294 <tr>
295 <td>
296 </td>
297 <td class='mw-submit'>" .
298 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), Linker::tooltipAndAccesskeyAttribs( 'import' ) ) .
299 "</td>
300 </tr>" .
301 Xml::closeElement( 'table' ).
302 Xml::closeElement( 'form' ) .
303 Xml::closeElement( 'fieldset' )
304 );
305 }
306 }
307 }
308
309 /**
310 * Reporting callback
311 * @ingroup SpecialPage
312 */
313 class ImportReporter extends ContextSource {
314 private $reason=false;
315 private $mOriginalLogCallback = null;
316 private $mOriginalPageOutCallback = null;
317 private $mLogItemCount = 0;
318
319 function __construct( $importer, $upload, $interwiki , $reason=false ) {
320 $this->mOriginalPageOutCallback =
321 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
322 $this->mOriginalLogCallback =
323 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
324 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
325 $this->mPageCount = 0;
326 $this->mIsUpload = $upload;
327 $this->mInterwiki = $interwiki;
328 $this->reason = $reason;
329 }
330
331 function open() {
332 $this->getOutput()->addHTML( "<ul>\n" );
333 }
334
335 function reportNotice( $msg, array $params ) {
336 $this->getOutput()->addHTML( Html::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
337 }
338
339 function reportLogItem( /* ... */ ) {
340 $this->mLogItemCount++;
341 if ( is_callable( $this->mOriginalLogCallback ) ) {
342 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
343 }
344 }
345
346 /**
347 * @param Title $title
348 * @param Title $origTitle
349 * @param int $revisionCount
350 * @param $successCount
351 * @param $pageInfo
352 */
353 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
354 global $wgContLang;
355
356 $args = func_get_args();
357 call_user_func_array( $this->mOriginalPageOutCallback, $args );
358
359 if ( $title === null ) {
360 # Invalid or non-importable title; a notice is already displayed
361 return;
362 }
363
364 $this->mPageCount++;
365
366 $localCount = $this->getLanguage()->formatNum( $successCount );
367 $contentCount = $wgContLang->formatNum( $successCount );
368
369 if( $successCount > 0 ) {
370 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
371 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
372 "</li>\n"
373 );
374
375 $log = new LogPage( 'import' );
376 if( $this->mIsUpload ) {
377 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
378 $contentCount );
379 if ( $this->reason ) {
380 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
381 }
382 $log->addEntry( 'upload', $title, $detail );
383 } else {
384 $interwiki = '[[:' . $this->mInterwiki . ':' .
385 $origTitle->getPrefixedText() . ']]';
386 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
387 $contentCount, $interwiki );
388 if ( $this->reason ) {
389 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
390 }
391 $log->addEntry( 'interwiki', $title, $detail );
392 }
393
394 $comment = $detail; // quick
395 $dbw = wfGetDB( DB_MASTER );
396 $latest = $title->getLatestRevID();
397 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
398 if (!is_null($nullRevision)) {
399 $nullRevision->insertOn( $dbw );
400 $page = WikiPage::factory( $title );
401 # Update page record
402 $page->updateRevisionOn( $dbw, $nullRevision );
403 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
404 }
405 } else {
406 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
407 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
408 }
409 }
410
411 function close() {
412 $out = $this->getOutput();
413 if ( $this->mLogItemCount > 0 ) {
414 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
415 $this->getLanguage()->formatNum( $this->mLogItemCount ) );
416 $out->addHTML( Xml::tags( 'li', null, $msg ) );
417 } elseif( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
418 $out->addHTML( "</ul>\n" );
419 return Status::newFatal( 'importnopages' );
420 }
421 $out->addHTML( "</ul>\n" );
422
423 return Status::newGood( $this->mPageCount );
424 }
425 }