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