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