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