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