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