Standardised file description headers; first path
[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 = new WikiErrorMsg( '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 = new WikiErrorMsg( "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 = new WikiErrorMsg( "importunknownsource" );
109 }
110
111 if( WikiError::isError( $source ) ) {
112 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getMessage() ) );
113 } else {
114 $wgOut->addWikiMsg( "importstart" );
115
116 $importer = new WikiImporter( $source );
117 if( !is_null( $this->namespace ) ) {
118 $importer->setTargetNamespace( $this->namespace );
119 }
120 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
121
122 $reporter->open();
123 $result = $importer->doImport();
124 $resultCount = $reporter->close();
125
126 if( WikiError::isError( $result ) ) {
127 # No source or XML parse error
128 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getMessage() ) );
129 } elseif( WikiError::isError( $resultCount ) ) {
130 # Zero revisions
131 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $resultCount->getMessage() ) );
132 } else {
133 # Success!
134 $wgOut->addWikiMsg( 'importsuccess' );
135 }
136 $wgOut->addWikiText( '<hr />' );
137 }
138 }
139
140 private function showForm() {
141 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
142 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
143 return $wgOut->permissionRequired( 'import' );
144
145 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
146
147 if( $wgUser->isAllowed( 'importupload' ) ) {
148 $wgOut->addWikiMsg( "importtext" );
149 $wgOut->addHTML(
150 Xml::fieldset( wfMsg( 'import-upload' ) ).
151 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
152 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
153 Xml::hidden( 'action', 'submit' ) .
154 Xml::hidden( 'source', 'upload' ) .
155 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
156
157 "<tr>
158 <td class='mw-label'>" .
159 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
160 "</td>
161 <td class='mw-input'>" .
162 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
163 "</td>
164 </tr>
165 <tr>
166 <td class='mw-label'>" .
167 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
168 "</td>
169 <td class='mw-input'>" .
170 Xml::input( 'log-comment', 50, '',
171 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
172 "</td>
173 </tr>
174 <tr>
175 <td></td>
176 <td class='mw-submit'>" .
177 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
178 "</td>
179 </tr>" .
180 Xml::closeElement( 'table' ).
181 Xml::hidden( 'editToken', $wgUser->editToken() ) .
182 Xml::closeElement( 'form' ) .
183 Xml::closeElement( 'fieldset' )
184 );
185 } else {
186 if( empty( $wgImportSources ) ) {
187 $wgOut->addWikiMsg( 'importnosources' );
188 }
189 }
190
191 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
192 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
193 $importDepth = '';
194 if( $wgExportMaxLinkDepth > 0 ) {
195 $importDepth = "<tr>
196 <td class='mw-label'>" .
197 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
198 "</td>
199 <td class='mw-input'>" .
200 Xml::input( 'pagelink-depth', 3, 0 ) .
201 "</td>
202 </tr>";
203 }
204
205 $wgOut->addHTML(
206 Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
207 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
208 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
209 Xml::hidden( 'action', 'submit' ) .
210 Xml::hidden( 'source', 'interwiki' ) .
211 Xml::hidden( 'editToken', $wgUser->editToken() ) .
212 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
213 "<tr>
214 <td class='mw-label'>" .
215 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
216 "</td>
217 <td class='mw-input'>" .
218 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
219 );
220 foreach( $wgImportSources as $prefix ) {
221 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
222 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
223 }
224
225 $wgOut->addHTML(
226 Xml::closeElement( 'select' ) .
227 Xml::input( 'frompage', 50, $this->frompage ) .
228 "</td>
229 </tr>
230 <tr>
231 <td>
232 </td>
233 <td class='mw-input'>" .
234 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
235 "</td>
236 </tr>
237 <tr>
238 <td>
239 </td>
240 <td class='mw-input'>" .
241 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
242 "</td>
243 </tr>
244 $importDepth
245 <tr>
246 <td class='mw-label'>" .
247 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
248 "</td>
249 <td class='mw-input'>" .
250 Xml::namespaceSelector( $this->namespace, '' ) .
251 "</td>
252 </tr>
253 <tr>
254 <td class='mw-label'>" .
255 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
256 "</td>
257 <td class='mw-input'>" .
258 Xml::input( 'log-comment', 50, '',
259 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
260 "</td>
261 </tr>
262 <tr>
263 <td>
264 </td>
265 <td class='mw-submit'>" .
266 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
267 "</td>
268 </tr>" .
269 Xml::closeElement( 'table' ).
270 Xml::closeElement( 'form' ) .
271 Xml::closeElement( 'fieldset' )
272 );
273 }
274 }
275 }
276
277 /**
278 * Reporting callback
279 * @ingroup SpecialPage
280 */
281 class ImportReporter {
282 private $reason=false;
283 private $mOriginalLogCallback = null;
284 private $mOriginalPageOutCallback = null;
285 private $mLogItemCount = 0;
286
287 function __construct( $importer, $upload, $interwiki , $reason=false ) {
288 $this->mOriginalPageOutCallback =
289 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
290 $this->mOriginalLogCallback =
291 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
292 $this->mPageCount = 0;
293 $this->mIsUpload = $upload;
294 $this->mInterwiki = $interwiki;
295 $this->reason = $reason;
296 }
297
298 function open() {
299 global $wgOut;
300 $wgOut->addHTML( "<ul>\n" );
301 }
302
303 function reportLogItem( /* ... */ ) {
304 $this->mLogItemCount++;
305 if ( is_callable( $this->mOriginalLogCallback ) ) {
306 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
307 }
308 }
309
310 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
311 global $wgOut, $wgUser, $wgLang, $wgContLang;
312
313 $args = func_get_args();
314 call_user_func_array( $this->mOriginalPageOutCallback, $args );
315
316 $skin = $wgUser->getSkin();
317
318 $this->mPageCount++;
319
320 $localCount = $wgLang->formatNum( $successCount );
321 $contentCount = $wgContLang->formatNum( $successCount );
322
323 if( $successCount > 0 ) {
324 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
325 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
326 "</li>\n"
327 );
328
329 $log = new LogPage( 'import' );
330 if( $this->mIsUpload ) {
331 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
332 $contentCount );
333 if ( $this->reason ) {
334 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
335 }
336 $log->addEntry( 'upload', $title, $detail );
337 } else {
338 $interwiki = '[[:' . $this->mInterwiki . ':' .
339 $origTitle->getPrefixedText() . ']]';
340 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
341 $contentCount, $interwiki );
342 if ( $this->reason ) {
343 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
344 }
345 $log->addEntry( 'interwiki', $title, $detail );
346 }
347
348 $comment = $detail; // quick
349 $dbw = wfGetDB( DB_MASTER );
350 $latest = $title->getLatestRevID();
351 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
352 $nullRevision->insertOn( $dbw );
353 $article = new Article( $title );
354 # Update page record
355 $article->updateRevisionOn( $dbw, $nullRevision );
356 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
357 } else {
358 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
359 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
360 }
361 }
362
363 function close() {
364 global $wgOut, $wgLang;
365
366 if ( $this->mLogItemCount > 0 ) {
367 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
368 $wgLang->formatNum( $this->mLogItemCount ) );
369 $wgOut->addHTML( Xml::tags( 'li', null, $msg ) );
370 } elseif( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
371 $wgOut->addHTML( "</ul>\n" );
372 return new WikiErrorMsg( "importnopages" );
373 }
374 $wgOut->addHTML( "</ul>\n" );
375
376 return $this->mPageCount;
377 }
378 }