XHTML fix
[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
34 /**
35 * Constructor
36 */
37 public function __construct() {
38 parent::__construct( 'Import', 'import' );
39 global $wgImportTargetNamespace;
40 $this->namespace = $wgImportTargetNamespace;
41 }
42
43 /**
44 * Execute
45 */
46 function execute( $par ) {
47 global $wgRequest;
48
49 $this->setHeaders();
50 $this->outputHeader();
51
52 if ( wfReadOnly() ) {
53 global $wgOut;
54 $wgOut->readOnlyPage();
55 return;
56 }
57
58 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
59 $this->doImport();
60 }
61 $this->showForm();
62 }
63
64 /**
65 * Do the actual import
66 */
67 private function doImport() {
68 global $wgOut, $wgRequest, $wgUser, $wgImportSources;
69 $isUpload = false;
70 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
71 $sourceName = $wgRequest->getVal( "source" );
72
73 $this->logcomment = $wgRequest->getText( 'log-comment' );
74
75 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
76 $source = new WikiErrorMsg( 'import-token-mismatch' );
77 } elseif ( $sourceName == 'upload' ) {
78 $isUpload = true;
79 if( $wgUser->isAllowed( 'importupload' ) ) {
80 $source = ImportStreamSource::newFromUpload( "xmlimport" );
81 } else {
82 return $wgOut->permissionRequired( 'importupload' );
83 }
84 } elseif ( $sourceName == "interwiki" ) {
85 $this->interwiki = $wgRequest->getVal( 'interwiki' );
86 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
87 $source = new WikiErrorMsg( "import-invalid-interwiki" );
88 } else {
89 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
90 $this->frompage = $wgRequest->getText( "frompage" );
91 $source = ImportStreamSource::newFromInterwiki(
92 $this->interwiki,
93 $this->frompage,
94 $this->history );
95 }
96 } else {
97 $source = new WikiErrorMsg( "importunknownsource" );
98 }
99
100 if( WikiError::isError( $source ) ) {
101 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
102 } else {
103 $wgOut->addWikiMsg( "importstart" );
104
105 $importer = new WikiImporter( $source );
106 if( !is_null( $this->namespace ) ) {
107 $importer->setTargetNamespace( $this->namespace );
108 }
109 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
110
111 $reporter->open();
112 $result = $importer->doImport();
113 $resultCount = $reporter->close();
114
115 if( WikiError::isError( $result ) ) {
116 # No source or XML parse error
117 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
118 } elseif( WikiError::isError( $resultCount ) ) {
119 # Zero revisions
120 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
121 } else {
122 # Success!
123 $wgOut->addWikiMsg( 'importsuccess' );
124 }
125 $wgOut->addWikiText( '<hr />' );
126 }
127 }
128
129 private function showForm() {
130 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
131 $action = $wgTitle->getLocalUrl( 'action=submit' );
132
133 if( $wgUser->isAllowed( 'importupload' ) ) {
134 $wgOut->addWikiMsg( "importtext" );
135 $wgOut->addHTML(
136 Xml::openElement( 'fieldset' ).
137 Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) .
138 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
139 Xml::hidden( 'action', 'submit' ) .
140 Xml::hidden( 'source', 'upload' ) .
141 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
142
143 "<tr>
144 <td>" .
145 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
146 "</td>
147 </tr>
148 <tr>
149 <td>" .
150 Xml::label( wfMsg('import-upload-comment'), 'log-comment' ) .
151 Xml::input( 'log-comment', 50, '', array( 'id' => 'log-comment', 'type' => 'text' ) ) . ' ' .
152 "</td>
153 </tr>
154 <tr>
155 <td>" .
156 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
157 "</td>
158 </tr>" .
159 Xml::closeElement( 'table' ).
160 Xml::hidden( 'editToken', $wgUser->editToken() ) .
161 Xml::closeElement( 'form' ) .
162 Xml::closeElement( 'fieldset' )
163 );
164 } else {
165 if( empty( $wgImportSources ) ) {
166 $wgOut->addWikiMsg( 'importnosources' );
167 }
168 }
169
170 if( !empty( $wgImportSources ) ) {
171 $wgOut->addHTML(
172 Xml::openElement( 'fieldset' ) .
173 Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
174 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
175 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
176 Xml::hidden( 'action', 'submit' ) .
177 Xml::hidden( 'source', 'interwiki' ) .
178 Xml::hidden( 'editToken', $wgUser->editToken() ) .
179 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
180 "<tr>
181 <td>" .
182 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
183 );
184 foreach( $wgImportSources as $prefix ) {
185 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
186 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
187 }
188 $wgOut->addHTML(
189 Xml::closeElement( 'select' ) .
190 "</td>
191 <td>" .
192 Xml::input( 'frompage', 50, $this->frompage ) .
193 "</td>
194 </tr>
195 <tr>
196 <td>
197 </td>
198 <td>" .
199 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
200 "</td>
201 </tr>
202 <tr>
203 <td>
204 </td>
205 <td>" .
206 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
207 Xml::namespaceSelector( $this->namespace, '' ) .
208 "</td>
209 </tr>
210 <tr>
211 <td>" .
212 Xml::label( wfMsg( 'import-comment' ), 'comment' ) .
213 "</td>
214 <td>" .
215 Xml::input( 'log-comment', 50, '', array( 'type' => 'text' ) ) . ' ' .
216 "</td>
217 </tr>
218 <tr>
219 <td>
220 </td>
221 <td>" .
222 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
223 "</td>
224 </tr>" .
225 Xml::closeElement( 'table' ).
226 Xml::closeElement( 'form' ) .
227 Xml::closeElement( 'fieldset' )
228 );
229 }
230 }
231 }
232
233 /**
234 * Reporting callback
235 * @ingroup SpecialPage
236 */
237 class ImportReporter {
238 private $reason=false;
239
240 function __construct( $importer, $upload, $interwiki , $reason=false ) {
241 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
242 $this->mPageCount = 0;
243 $this->mIsUpload = $upload;
244 $this->mInterwiki = $interwiki;
245 $this->reason = $reason;
246 }
247
248 function open() {
249 global $wgOut;
250 $wgOut->addHTML( "<ul>\n" );
251 }
252
253 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
254 global $wgOut, $wgUser, $wgLang, $wgContLang;
255
256 $skin = $wgUser->getSkin();
257
258 $this->mPageCount++;
259
260 $localCount = $wgLang->formatNum( $successCount );
261 $contentCount = $wgContLang->formatNum( $successCount );
262
263 if( $successCount > 0 ) {
264 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
265 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
266 "</li>\n"
267 );
268
269 $log = new LogPage( 'import' );
270 if( $this->mIsUpload ) {
271 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
272 $contentCount );
273 if ($this->reason) {
274 $detail .= ' (' . $this->reason .')';
275 }
276 $log->addEntry( 'upload', $title, $detail );
277 } else {
278 $interwiki = '[[:' . $this->mInterwiki . ':' .
279 $origTitle->getPrefixedText() . ']]';
280 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
281 $contentCount, $interwiki );
282 if ($this->reason) {
283 $detail .= ' (' . $this->reason .')';
284 }
285 $log->addEntry( 'interwiki', $title, $detail );
286 }
287
288 $comment = $detail; // quick
289 $dbw = wfGetDB( DB_MASTER );
290 $latest = $title->getLatestRevID();
291 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
292 $nullRevision->insertOn( $dbw );
293 $article = new Article( $title );
294 # Update page record
295 $article->updateRevisionOn( $dbw, $nullRevision );
296 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
297 } else {
298 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
299 }
300 }
301
302 function close() {
303 global $wgOut;
304 if( $this->mPageCount == 0 ) {
305 $wgOut->addHTML( "</ul>\n" );
306 return new WikiErrorMsg( "importnopages" );
307 }
308 $wgOut->addHTML( "</ul>\n" );
309
310 return $this->mPageCount;
311 }
312 }