00001 <?php
00026 class SpecialImport extends SpecialPage {
00027
00028 private $interwiki = false;
00029 private $namespace;
00030 private $frompage = '';
00031 private $logcomment= false;
00032 private $history = true;
00033 private $includeTemplates = false;
00034
00038 public function __construct() {
00039 parent::__construct( 'Import', 'import' );
00040 global $wgImportTargetNamespace;
00041 $this->namespace = $wgImportTargetNamespace;
00042 }
00043
00047 function execute( $par ) {
00048 global $wgRequest, $wgUser, $wgOut;
00049
00050 $this->setHeaders();
00051 $this->outputHeader();
00052
00053 if ( wfReadOnly() ) {
00054 global $wgOut;
00055 $wgOut->readOnlyPage();
00056 return;
00057 }
00058
00059 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
00060 return $wgOut->permissionRequired( 'import' );
00061
00062 # TODO: allow Title::getUserPermissionsErrors() to take an array
00063 # FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
00064 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
00065 $errors = wfMergeErrorArrays(
00066 $this->getTitle()->getUserPermissionsErrors(
00067 'import', $wgUser, true,
00068 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
00069 ),
00070 $this->getTitle()->getUserPermissionsErrors(
00071 'importupload', $wgUser, true,
00072 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
00073 )
00074 );
00075
00076 if( $errors ){
00077 $wgOut->showPermissionsErrorPage( $errors );
00078 return;
00079 }
00080
00081 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
00082 $this->doImport();
00083 }
00084 $this->showForm();
00085 }
00086
00090 private function doImport() {
00091 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
00092 $isUpload = false;
00093 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
00094 $sourceName = $wgRequest->getVal( "source" );
00095
00096 $this->logcomment = $wgRequest->getText( 'log-comment' );
00097 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
00098
00099 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
00100 $source = new WikiErrorMsg( 'import-token-mismatch' );
00101 } elseif ( $sourceName == 'upload' ) {
00102 $isUpload = true;
00103 if( $wgUser->isAllowed( 'importupload' ) ) {
00104 $source = ImportStreamSource::newFromUpload( "xmlimport" );
00105 } else {
00106 return $wgOut->permissionRequired( 'importupload' );
00107 }
00108 } elseif ( $sourceName == "interwiki" ) {
00109 if( !$wgUser->isAllowed( 'import' ) ){
00110 return $wgOut->permissionRequired( 'import' );
00111 }
00112 $this->interwiki = $wgRequest->getVal( 'interwiki' );
00113 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
00114 $source = new WikiErrorMsg( "import-invalid-interwiki" );
00115 } else {
00116 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
00117 $this->frompage = $wgRequest->getText( "frompage" );
00118 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
00119 $source = ImportStreamSource::newFromInterwiki(
00120 $this->interwiki,
00121 $this->frompage,
00122 $this->history,
00123 $this->includeTemplates,
00124 $this->pageLinkDepth );
00125 }
00126 } else {
00127 $source = new WikiErrorMsg( "importunknownsource" );
00128 }
00129
00130 if( WikiError::isError( $source ) ) {
00131 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
00132 } else {
00133 $wgOut->addWikiMsg( "importstart" );
00134
00135 $importer = new WikiImporter( $source );
00136 if( !is_null( $this->namespace ) ) {
00137 $importer->setTargetNamespace( $this->namespace );
00138 }
00139 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
00140
00141 $reporter->open();
00142 $result = $importer->doImport();
00143 $resultCount = $reporter->close();
00144
00145 if( WikiError::isError( $result ) ) {
00146 # No source or XML parse error
00147 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
00148 } elseif( WikiError::isError( $resultCount ) ) {
00149 # Zero revisions
00150 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
00151 } else {
00152 # Success!
00153 $wgOut->addWikiMsg( 'importsuccess' );
00154 }
00155 $wgOut->addWikiText( '<hr />' );
00156 }
00157 }
00158
00159 private function showForm() {
00160 global $wgUser, $wgOut, $wgRequest, $wgImportSources, $wgExportMaxLinkDepth;
00161
00162 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
00163
00164 if( $wgUser->isAllowed( 'importupload' ) ) {
00165 $wgOut->addWikiMsg( "importtext" );
00166 $wgOut->addHTML(
00167 Xml::fieldset( wfMsg( 'import-upload' ) ).
00168 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
00169 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
00170 Xml::hidden( 'action', 'submit' ) .
00171 Xml::hidden( 'source', 'upload' ) .
00172 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
00173
00174 "<tr>
00175 <td class='mw-label'>" .
00176 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
00177 "</td>
00178 <td class='mw-input'>" .
00179 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
00180 "</td>
00181 </tr>
00182 <tr>
00183 <td class='mw-label'>" .
00184 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
00185 "</td>
00186 <td class='mw-input'>" .
00187 Xml::input( 'log-comment', 50, '',
00188 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
00189 "</td>
00190 </tr>
00191 <tr>
00192 <td></td>
00193 <td class='mw-submit'>" .
00194 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
00195 "</td>
00196 </tr>" .
00197 Xml::closeElement( 'table' ).
00198 Xml::hidden( 'editToken', $wgUser->editToken() ) .
00199 Xml::closeElement( 'form' ) .
00200 Xml::closeElement( 'fieldset' )
00201 );
00202 } else {
00203 if( empty( $wgImportSources ) ) {
00204 $wgOut->addWikiMsg( 'importnosources' );
00205 }
00206 }
00207
00208 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
00209 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
00210 $importDepth = '';
00211 if( $wgExportMaxLinkDepth > 0 ) {
00212 $importDepth = "<tr>
00213 <td class='mw-label'>" .
00214 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
00215 "</td>
00216 <td class='mw-input'>" .
00217 Xml::input( 'pagelink-depth', 3, 0 ) .
00218 "</td>
00219 </tr>";
00220 }
00221
00222 $wgOut->addHTML(
00223 Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
00224 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
00225 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
00226 Xml::hidden( 'action', 'submit' ) .
00227 Xml::hidden( 'source', 'interwiki' ) .
00228 Xml::hidden( 'editToken', $wgUser->editToken() ) .
00229 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
00230 "<tr>
00231 <td class='mw-label'>" .
00232 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
00233 "</td>
00234 <td class='mw-input'>" .
00235 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
00236 );
00237 foreach( $wgImportSources as $prefix ) {
00238 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
00239 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
00240 }
00241
00242 $wgOut->addHTML(
00243 Xml::closeElement( 'select' ) .
00244 Xml::input( 'frompage', 50, $this->frompage ) .
00245 "</td>
00246 </tr>
00247 <tr>
00248 <td>
00249 </td>
00250 <td class='mw-input'>" .
00251 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
00252 "</td>
00253 </tr>
00254 <tr>
00255 <td>
00256 </td>
00257 <td class='mw-input'>" .
00258 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
00259 "</td>
00260 </tr>
00261 $importDepth
00262 <tr>
00263 <td class='mw-label'>" .
00264 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
00265 "</td>
00266 <td class='mw-input'>" .
00267 Xml::namespaceSelector( $this->namespace, '' ) .
00268 "</td>
00269 </tr>
00270 <tr>
00271 <td class='mw-label'>" .
00272 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
00273 "</td>
00274 <td class='mw-input'>" .
00275 Xml::input( 'log-comment', 50, '',
00276 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
00277 "</td>
00278 </tr>
00279 <tr>
00280 <td>
00281 </td>
00282 <td class='mw-submit'>" .
00283 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
00284 "</td>
00285 </tr>" .
00286 Xml::closeElement( 'table' ).
00287 Xml::closeElement( 'form' ) .
00288 Xml::closeElement( 'fieldset' )
00289 );
00290 }
00291 }
00292 }
00293
00298 class ImportReporter {
00299 private $reason=false;
00300
00301 function __construct( $importer, $upload, $interwiki , $reason=false ) {
00302 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
00303 $this->mPageCount = 0;
00304 $this->mIsUpload = $upload;
00305 $this->mInterwiki = $interwiki;
00306 $this->reason = $reason;
00307 }
00308
00309 function open() {
00310 global $wgOut;
00311 $wgOut->addHTML( "<ul>\n" );
00312 }
00313
00314 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
00315 global $wgOut, $wgUser, $wgLang, $wgContLang;
00316
00317 $skin = $wgUser->getSkin();
00318
00319 $this->mPageCount++;
00320
00321 $localCount = $wgLang->formatNum( $successCount );
00322 $contentCount = $wgContLang->formatNum( $successCount );
00323
00324 if( $successCount > 0 ) {
00325 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
00326 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
00327 "</li>\n"
00328 );
00329
00330 $log = new LogPage( 'import' );
00331 if( $this->mIsUpload ) {
00332 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
00333 $contentCount );
00334 if ( $this->reason ) {
00335 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
00336 }
00337 $log->addEntry( 'upload', $title, $detail );
00338 } else {
00339 $interwiki = '[[:' . $this->mInterwiki . ':' .
00340 $origTitle->getPrefixedText() . ']]';
00341 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
00342 $contentCount, $interwiki );
00343 if ( $this->reason ) {
00344 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
00345 }
00346 $log->addEntry( 'interwiki', $title, $detail );
00347 }
00348
00349 $comment = $detail;
00350 $dbw = wfGetDB( DB_MASTER );
00351 $latest = $title->getLatestRevID();
00352 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
00353 $nullRevision->insertOn( $dbw );
00354 $article = new Article( $title );
00355 # Update page record
00356 $article->updateRevisionOn( $dbw, $nullRevision );
00357 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
00358 } else {
00359 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
00360 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
00361 }
00362 }
00363
00364 function close() {
00365 global $wgOut;
00366 if( $this->mPageCount == 0 ) {
00367 $wgOut->addHTML( "</ul>\n" );
00368 return new WikiErrorMsg( "importnopages" );
00369 }
00370 $wgOut->addHTML( "</ul>\n" );
00371
00372 return $this->mPageCount;
00373 }
00374 }