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