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