fa976f6b6fa680f2b1665be9858590665d91b552
[lhc/web/wiklou.git] / includes / specials / SpecialImport.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 class SpecialImport extends SpecialPage {
27
28 private $interwiki = false;
29 private $namespace;
30 private $frompage = '';
31 private $logcomment= false;
32 private $history = true;
33 private $includeTemplates = false;
34
35 /**
36 * Constructor
37 */
38 public function __construct() {
39 parent::__construct( 'Import', 'import' );
40 global $wgImportTargetNamespace;
41 $this->namespace = $wgImportTargetNamespace;
42 }
43
44 /**
45 * Execute
46 */
47 function execute( $par ) {
48 global $wgRequest;
49
50 $this->setHeaders();
51 $this->outputHeader();
52
53 if ( wfReadOnly() ) {
54 global $wgOut;
55 $wgOut->readOnlyPage();
56 return;
57 }
58
59 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
60 $this->doImport();
61 }
62 $this->showForm();
63 }
64
65 /**
66 * Do the actual import
67 */
68 private function doImport() {
69 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
70 $isUpload = false;
71 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
72 $sourceName = $wgRequest->getVal( "source" );
73
74 $this->logcomment = $wgRequest->getText( 'log-comment' );
75 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
76
77 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
78 $source = new WikiErrorMsg( 'import-token-mismatch' );
79 } elseif ( $sourceName == 'upload' ) {
80 $isUpload = true;
81 if( $wgUser->isAllowed( 'importupload' ) ) {
82 $source = ImportStreamSource::newFromUpload( "xmlimport" );
83 } else {
84 return $wgOut->permissionRequired( 'importupload' );
85 }
86 } elseif ( $sourceName == "interwiki" ) {
87 $this->interwiki = $wgRequest->getVal( 'interwiki' );
88 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
89 $source = new WikiErrorMsg( "import-invalid-interwiki" );
90 } else {
91 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
92 $this->frompage = $wgRequest->getText( "frompage" );
93 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
94 $source = ImportStreamSource::newFromInterwiki(
95 $this->interwiki,
96 $this->frompage,
97 $this->history,
98 $this->includeTemplates,
99 $this->pageLinkDepth );
100 }
101 } else {
102 $source = new WikiErrorMsg( "importunknownsource" );
103 }
104
105 if( WikiError::isError( $source ) ) {
106 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getMessage() ) );
107 } else {
108 $wgOut->addWikiMsg( "importstart" );
109
110 $importer = new WikiImporter( $source );
111 if( !is_null( $this->namespace ) ) {
112 $importer->setTargetNamespace( $this->namespace );
113 }
114 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
115
116 $reporter->open();
117 $result = $importer->doImport();
118 $resultCount = $reporter->close();
119
120 if( WikiError::isError( $result ) ) {
121 # No source or XML parse error
122 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getMessage() ) );
123 } elseif( WikiError::isError( $resultCount ) ) {
124 # Zero revisions
125 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $resultCount->getMessage() ) );
126 } else {
127 # Success!
128 $wgOut->addWikiMsg( 'importsuccess' );
129 }
130 $wgOut->addWikiText( '<hr />' );
131 }
132 }
133
134 private function showForm() {
135 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
136 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
137 return $wgOut->permissionRequired( 'import' );
138
139 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
140
141 if( $wgUser->isAllowed( 'importupload' ) ) {
142 $wgOut->addWikiMsg( "importtext" );
143 $wgOut->addHTML(
144 Xml::fieldset( wfMsg( 'import-upload' ) ).
145 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
146 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
147 Xml::hidden( 'action', 'submit' ) .
148 Xml::hidden( 'source', 'upload' ) .
149 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
150
151 "<tr>
152 <td class='mw-label'>" .
153 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
154 "</td>
155 <td class='mw-input'>" .
156 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
157 "</td>
158 </tr>
159 <tr>
160 <td class='mw-label'>" .
161 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
162 "</td>
163 <td class='mw-input'>" .
164 Xml::input( 'log-comment', 50, '',
165 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
166 "</td>
167 </tr>
168 <tr>
169 <td></td>
170 <td class='mw-submit'>" .
171 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
172 "</td>
173 </tr>" .
174 Xml::closeElement( 'table' ).
175 Xml::hidden( 'editToken', $wgUser->editToken() ) .
176 Xml::closeElement( 'form' ) .
177 Xml::closeElement( 'fieldset' )
178 );
179 } else {
180 if( empty( $wgImportSources ) ) {
181 $wgOut->addWikiMsg( 'importnosources' );
182 }
183 }
184
185 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
186 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
187 $importDepth = '';
188 if( $wgExportMaxLinkDepth > 0 ) {
189 $importDepth = "<tr>
190 <td class='mw-label'>" .
191 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
192 "</td>
193 <td class='mw-input'>" .
194 Xml::input( 'pagelink-depth', 3, 0 ) .
195 "</td>
196 </tr>";
197 }
198
199 $wgOut->addHTML(
200 Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
201 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
202 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
203 Xml::hidden( 'action', 'submit' ) .
204 Xml::hidden( 'source', 'interwiki' ) .
205 Xml::hidden( 'editToken', $wgUser->editToken() ) .
206 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
207 "<tr>
208 <td class='mw-label'>" .
209 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
210 "</td>
211 <td class='mw-input'>" .
212 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
213 );
214 foreach( $wgImportSources as $prefix ) {
215 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
216 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
217 }
218
219 $wgOut->addHTML(
220 Xml::closeElement( 'select' ) .
221 Xml::input( 'frompage', 50, $this->frompage ) .
222 "</td>
223 </tr>
224 <tr>
225 <td>
226 </td>
227 <td class='mw-input'>" .
228 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
229 "</td>
230 </tr>
231 <tr>
232 <td>
233 </td>
234 <td class='mw-input'>" .
235 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
236 "</td>
237 </tr>
238 $importDepth
239 <tr>
240 <td class='mw-label'>" .
241 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
242 "</td>
243 <td class='mw-input'>" .
244 Xml::namespaceSelector( $this->namespace, '' ) .
245 "</td>
246 </tr>
247 <tr>
248 <td class='mw-label'>" .
249 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
250 "</td>
251 <td class='mw-input'>" .
252 Xml::input( 'log-comment', 50, '',
253 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
254 "</td>
255 </tr>
256 <tr>
257 <td>
258 </td>
259 <td class='mw-submit'>" .
260 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
261 "</td>
262 </tr>" .
263 Xml::closeElement( 'table' ).
264 Xml::closeElement( 'form' ) .
265 Xml::closeElement( 'fieldset' )
266 );
267 }
268 }
269 }
270
271 /**
272 * Reporting callback
273 * @ingroup SpecialPage
274 */
275 class ImportReporter {
276 private $reason=false;
277 private $mOriginalLogCallback = null;
278 private $mOriginalPageOutCallback = null;
279 private $mLogItemCount = 0;
280
281 function __construct( $importer, $upload, $interwiki , $reason=false ) {
282 $this->mOriginalPageOutCallback =
283 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
284 $this->mOriginalLogCallback =
285 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
286 $this->mPageCount = 0;
287 $this->mIsUpload = $upload;
288 $this->mInterwiki = $interwiki;
289 $this->reason = $reason;
290 }
291
292 function open() {
293 global $wgOut;
294 $wgOut->addHTML( "<ul>\n" );
295 }
296
297 function reportLogItem( /* ... */ ) {
298 $this->mLogItemCount++;
299 if ( is_callable( $this->mOriginalLogCallback ) ) {
300 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
301 }
302 }
303
304 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
305 global $wgOut, $wgUser, $wgLang, $wgContLang;
306
307 $args = func_get_args();
308 call_user_func_array( $this->mOriginalPageOutCallback, $args );
309
310 $skin = $wgUser->getSkin();
311
312 $this->mPageCount++;
313
314 $localCount = $wgLang->formatNum( $successCount );
315 $contentCount = $wgContLang->formatNum( $successCount );
316
317 if( $successCount > 0 ) {
318 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
319 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
320 "</li>\n"
321 );
322
323 $log = new LogPage( 'import' );
324 if( $this->mIsUpload ) {
325 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
326 $contentCount );
327 if ( $this->reason ) {
328 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
329 }
330 $log->addEntry( 'upload', $title, $detail );
331 } else {
332 $interwiki = '[[:' . $this->mInterwiki . ':' .
333 $origTitle->getPrefixedText() . ']]';
334 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
335 $contentCount, $interwiki );
336 if ( $this->reason ) {
337 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
338 }
339 $log->addEntry( 'interwiki', $title, $detail );
340 }
341
342 $comment = $detail; // quick
343 $dbw = wfGetDB( DB_MASTER );
344 $latest = $title->getLatestRevID();
345 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
346 $nullRevision->insertOn( $dbw );
347 $article = new Article( $title );
348 # Update page record
349 $article->updateRevisionOn( $dbw, $nullRevision );
350 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
351 } else {
352 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
353 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
354 }
355 }
356
357 function close() {
358 global $wgOut, $wgLang;
359
360 if ( $this->mLogItemCount > 0 ) {
361 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
362 $wgLang->formatNum( $this->mLogItemCount ) );
363 $wgOut->addHTML( Xml::tags( 'li', null, $msg ) );
364 } elseif( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
365 $wgOut->addHTML( "</ul>\n" );
366 return new WikiErrorMsg( "importnopages" );
367 }
368 $wgOut->addHTML( "</ul>\n" );
369
370 return $this->mPageCount;
371 }
372 }