(bug 15283) Interwiki imports should fetch included templates
[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;
70 $isUpload = false;
71 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
72 $sourceName = $wgRequest->getVal( "source" );
73
74 $this->logcomment = $wgRequest->getText( 'log-comment' );
75
76 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
77 $source = new WikiErrorMsg( 'import-token-mismatch' );
78 } elseif ( $sourceName == 'upload' ) {
79 $isUpload = true;
80 if( $wgUser->isAllowed( 'importupload' ) ) {
81 $source = ImportStreamSource::newFromUpload( "xmlimport" );
82 } else {
83 return $wgOut->permissionRequired( 'importupload' );
84 }
85 } elseif ( $sourceName == "interwiki" ) {
86 $this->interwiki = $wgRequest->getVal( 'interwiki' );
87 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
88 $source = new WikiErrorMsg( "import-invalid-interwiki" );
89 } else {
90 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
91 $this->frompage = $wgRequest->getText( "frompage" );
92 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
93 $source = ImportStreamSource::newFromInterwiki(
94 $this->interwiki,
95 $this->frompage,
96 $this->history,
97 $this->includeTemplates );
98 }
99 } else {
100 $source = new WikiErrorMsg( "importunknownsource" );
101 }
102
103 if( WikiError::isError( $source ) ) {
104 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
105 } else {
106 $wgOut->addWikiMsg( "importstart" );
107
108 $importer = new WikiImporter( $source );
109 if( !is_null( $this->namespace ) ) {
110 $importer->setTargetNamespace( $this->namespace );
111 }
112 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
113
114 $reporter->open();
115 $result = $importer->doImport();
116 $resultCount = $reporter->close();
117
118 if( WikiError::isError( $result ) ) {
119 # No source or XML parse error
120 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
121 } elseif( WikiError::isError( $resultCount ) ) {
122 # Zero revisions
123 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
124 } else {
125 # Success!
126 $wgOut->addWikiMsg( 'importsuccess' );
127 }
128 $wgOut->addWikiText( '<hr />' );
129 }
130 }
131
132 private function showForm() {
133 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
134 # FIXME: Quick hack to disable import for non privileged users /Raymond
135 # Regression from 43963
136 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
137 return $wgOut->permissionRequired( 'import' );
138
139 $action = $wgTitle->getLocalUrl( 'action=submit' );
140
141 if( $wgUser->isAllowed( 'importupload' ) ) {
142 $wgOut->addWikiMsg( "importtext" );
143 $wgOut->addHTML(
144 Xml::openElement( 'fieldset' ).
145 Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) .
146 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
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-input'>" .
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 $wgOut->addHTML(
187 Xml::openElement( 'fieldset' ) .
188 Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
189 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
190 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
191 Xml::hidden( 'action', 'submit' ) .
192 Xml::hidden( 'source', 'interwiki' ) .
193 Xml::hidden( 'editToken', $wgUser->editToken() ) .
194 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
195 "<tr>
196 <td class='mw-label'>" .
197 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
198 "</td>
199 <td class='mw-input'>" .
200 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
201 );
202 foreach( $wgImportSources as $prefix ) {
203 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
204 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
205 }
206 $wgOut->addHTML(
207 Xml::closeElement( 'select' ) .
208 Xml::input( 'frompage', 50, $this->frompage ) .
209 "</td>
210 </tr>
211 <tr>
212 <td>
213 </td>
214 <td class='mw-input'>" .
215 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
216 "</td>
217 </tr>
218 <tr>
219 <td>
220 </td>
221 <td class='mw-input'>" .
222 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
223 "</td>
224 <tr>
225 <td>" .
226 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
227 "</td>
228 <td class='mw-input'>" .
229 Xml::namespaceSelector( $this->namespace, '' ) .
230 "</td>
231 </tr>
232 <tr>
233 <td class='mw-label'>" .
234 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
235 "</td>
236 <td class='mw-input'>" .
237 Xml::input( 'log-comment', 50, '',
238 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
239 "</td>
240 </tr>
241 <tr>
242 <td>
243 </td>
244 <td class='mw-input'>" .
245 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
246 "</td>
247 </tr>" .
248 Xml::closeElement( 'table' ).
249 Xml::closeElement( 'form' ) .
250 Xml::closeElement( 'fieldset' )
251 );
252 }
253 }
254 }
255
256 /**
257 * Reporting callback
258 * @ingroup SpecialPage
259 */
260 class ImportReporter {
261 private $reason=false;
262
263 function __construct( $importer, $upload, $interwiki , $reason=false ) {
264 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
265 $this->mPageCount = 0;
266 $this->mIsUpload = $upload;
267 $this->mInterwiki = $interwiki;
268 $this->reason = $reason;
269 }
270
271 function open() {
272 global $wgOut;
273 $wgOut->addHTML( "<ul>\n" );
274 }
275
276 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
277 global $wgOut, $wgUser, $wgLang, $wgContLang;
278
279 $skin = $wgUser->getSkin();
280
281 $this->mPageCount++;
282
283 $localCount = $wgLang->formatNum( $successCount );
284 $contentCount = $wgContLang->formatNum( $successCount );
285
286 if( $successCount > 0 ) {
287 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
288 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
289 "</li>\n"
290 );
291
292 $log = new LogPage( 'import' );
293 if( $this->mIsUpload ) {
294 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
295 $contentCount );
296 if ( $this->reason ) {
297 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
298 }
299 $log->addEntry( 'upload', $title, $detail );
300 } else {
301 $interwiki = '[[:' . $this->mInterwiki . ':' .
302 $origTitle->getPrefixedText() . ']]';
303 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
304 $contentCount, $interwiki );
305 if ( $this->reason ) {
306 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
307 }
308 $log->addEntry( 'interwiki', $title, $detail );
309 }
310
311 $comment = $detail; // quick
312 $dbw = wfGetDB( DB_MASTER );
313 $latest = $title->getLatestRevID();
314 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
315 $nullRevision->insertOn( $dbw );
316 $article = new Article( $title );
317 # Update page record
318 $article->updateRevisionOn( $dbw, $nullRevision );
319 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
320 } else {
321 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
322 }
323 }
324
325 function close() {
326 global $wgOut;
327 if( $this->mPageCount == 0 ) {
328 $wgOut->addHTML( "</ul>\n" );
329 return new WikiErrorMsg( "importnopages" );
330 }
331 $wgOut->addHTML( "</ul>\n" );
332
333 return $this->mPageCount;
334 }
335 }