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