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