]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialImport.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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                 # FIXME: Quick hack to disable import for non privileged users /Raymond 
132                 # Regression from 43963 
133                 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
134                         return $wgOut->permissionRequired( 'import' );
135
136                 $action = $wgTitle->getLocalUrl( 'action=submit' );
137
138                 if( $wgUser->isAllowed( 'importupload' ) ) {
139                         $wgOut->addWikiMsg( "importtext" );
140                         $wgOut->addHTML(
141                                 Xml::openElement( 'fieldset' ).
142                                 Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) .
143                                 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
144                                 Xml::hidden( 'action', 'submit' ) .
145                                 Xml::hidden( 'source', 'upload' ) .
146                                 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
147
148                                 "<tr>
149                                         <td class='mw-label'>" .
150                                                 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
151                                         "</td>
152                                         <td class='mw-input'>" .
153                                                 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
154                                         "</td>
155                                 </tr>
156                                 <tr>
157                                         <td class='mw-label'>" .
158                                                 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
159                                         "</td>
160                                         <td class='mw-input'>" .
161                                                 Xml::input( 'log-comment', 50, '',
162                                                         array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
163                                         "</td>
164                                 </tr>
165                                 <tr>
166                                         <td></td>
167                                         <td class='mw-input'>" .
168                                                 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
169                                         "</td>
170                                 </tr>" .
171                                 Xml::closeElement( 'table' ).
172                                 Xml::hidden( 'editToken', $wgUser->editToken() ) .
173                                 Xml::closeElement( 'form' ) .
174                                 Xml::closeElement( 'fieldset' )
175                         );
176                 } else {
177                         if( empty( $wgImportSources ) ) {
178                                 $wgOut->addWikiMsg( 'importnosources' );
179                         }
180                 }
181
182                 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
183                         $wgOut->addHTML(
184                                 Xml::openElement( 'fieldset' ) .
185                                 Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
186                                 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
187                                 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
188                                 Xml::hidden( 'action', 'submit' ) .
189                                 Xml::hidden( 'source', 'interwiki' ) .
190                                 Xml::hidden( 'editToken', $wgUser->editToken() ) .
191                                 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
192                                 "<tr>
193                                         <td class='mw-label'>" .
194                                                 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
195                                         "</td>
196                                         <td class='mw-input'>" .
197                                                 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
198                         );
199                         foreach( $wgImportSources as $prefix ) {
200                                 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
201                                 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
202                         }
203                         $wgOut->addHTML(
204                                                 Xml::closeElement( 'select' ) .
205                                                 Xml::input( 'frompage', 50, $this->frompage ) .
206                                         "</td>
207                                 </tr>
208                                 <tr>
209                                         <td>
210                                         </td>
211                                         <td class='mw-input'>" .
212                                                 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
213                                         "</td>
214                                 </tr>
215                                 <tr>
216                                         <td>" .
217                                                 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
218                                         "</td>
219                                         <td class='mw-input'>" .
220                                                 Xml::namespaceSelector( $this->namespace, '' ) .
221                                         "</td>
222                                 </tr>
223                                 <tr>
224                                         <td class='mw-label'>" .
225                                                 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
226                                         "</td>
227                                         <td class='mw-input'>" .
228                                                 Xml::input( 'log-comment', 50, '',
229                                                         array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
230                                         "</td>
231                                 </tr>
232                                 <tr>
233                                         <td>
234                                         </td>
235                                         <td class='mw-input'>" .
236                                                 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
237                                         "</td>
238                                 </tr>" .
239                                 Xml::closeElement( 'table' ).
240                                 Xml::closeElement( 'form' ) .
241                                 Xml::closeElement( 'fieldset' )
242                         );
243                 }
244         }
245 }
246
247 /**
248  * Reporting callback
249  * @ingroup SpecialPage
250  */
251 class ImportReporter {
252       private $reason=false;
253
254         function __construct( $importer, $upload, $interwiki , $reason=false ) {
255                 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
256                 $this->mPageCount = 0;
257                 $this->mIsUpload = $upload;
258                 $this->mInterwiki = $interwiki;
259                 $this->reason = $reason;
260         }
261
262         function open() {
263                 global $wgOut;
264                 $wgOut->addHTML( "<ul>\n" );
265         }
266
267         function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
268                 global $wgOut, $wgUser, $wgLang, $wgContLang;
269
270                 $skin = $wgUser->getSkin();
271
272                 $this->mPageCount++;
273
274                 $localCount = $wgLang->formatNum( $successCount );
275                 $contentCount = $wgContLang->formatNum( $successCount );
276
277                 if( $successCount > 0 ) {
278                         $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
279                                 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
280                                 "</li>\n"
281                         );
282
283                         $log = new LogPage( 'import' );
284                         if( $this->mIsUpload ) {
285                                 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
286                                         $contentCount );
287                                 if ( $this->reason ) {
288                                         $detail .=  wfMsgForContent( 'colon-separator' ) . $this->reason;
289                                 }
290                                 $log->addEntry( 'upload', $title, $detail );
291                         } else {
292                                 $interwiki = '[[:' . $this->mInterwiki . ':' .
293                                         $origTitle->getPrefixedText() . ']]';
294                                 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
295                                         $contentCount, $interwiki );
296                                 if ( $this->reason ) {
297                                         $detail .=  wfMsgForContent( 'colon-separator' ) . $this->reason;
298                                 }
299                                 $log->addEntry( 'interwiki', $title, $detail );
300                         }
301
302                         $comment = $detail; // quick
303                         $dbw = wfGetDB( DB_MASTER );
304                         $latest = $title->getLatestRevID();
305                         $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
306                         $nullRevision->insertOn( $dbw );
307                         $article = new Article( $title );
308                         # Update page record
309                         $article->updateRevisionOn( $dbw, $nullRevision );
310                         wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
311                 } else {
312                         $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
313                 }
314         }
315
316         function close() {
317                 global $wgOut;
318                 if( $this->mPageCount == 0 ) {
319                         $wgOut->addHTML( "</ul>\n" );
320                         return new WikiErrorMsg( "importnopages" );
321                 }
322                 $wgOut->addHTML( "</ul>\n" );
323
324                 return $this->mPageCount;
325         }
326 }