/** * import xml data either into local or remote wiki, depending on self::$directionToLocal value */ static function importXML( $dstImportToken, $xmldata ) { global $wgUser, $wgTmpDirectory; // {{{ bugfixes global $wgSMTP; // global $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth; global $wgEnableEmail, $wgEnableUserEmail; // }}} list( $fname, $fp ) = self::tempnam_sfx( $wgTmpDirectory . '/', '.xml' ); $flen = strlen( $xmldata ); if ( @fwrite( $fp, $xmldata, $flen ) !== $flen ) { throw new MWException( 'Cannot write xmldata to file ' . $fname . ' in ' . __METHOD__ . ' disk full?' ); } fclose( $fp ); if ( self::$directionToLocal ) { # suppress "pear mail" possible smtp fatal errors # in EmailNotification::actuallyNotifyOnPageChange() $wgSMTP = false; $wgEnableEmail = false; $wgEnableUserEmail = false; /* if ( $wgMaxArticleSize < 8192 ) { $wgMaxArticleSize = 8192; } */ $json_result = new WikiSyncJSONresult( false ); $json_result->setCode( 'import' ); if( !$wgUser->isAllowed( 'importupload' ) ) { @unlink( $fname ); return $json_result->getResult( 'no_import_rights' ); } $source = ImportStreamSource::newFromFile( $fname ); $err_msg = null; if ( $source instanceof Status ) { if ( $source->isOK() ) { $source = $source->value; } else { $err_msg = $source->getWikiText(); } } elseif ( $source instanceof WikiErrorMsg || WikiError::isError( $source ) ) { $err_msg = $source->getMessage(); } if ( $err_msg !== null ) { @unlink( $fname ); return $json_result->getResult( 'import', $err_msg ); } $importer = new WikiImporter( $source ); $reporter = new WikiSyncImportReporter( $importer, false, '', wfMsg( 'wikisync_log_imported_by' ) ); $result = $importer->doImport(); @fclose( $source->mHandle ); if ( !WikiSyncSetup::$debug_mode ) { @unlink( $fname ); } if ( $result instanceof WikiXmlError ) { $r = array( 'line' => $result->mLine, 'column' => $result->mColumn, 'context' => $result->mByte . $result->mContext, 'xmlerror' => xml_error_string( $result->mXmlError ) ); $json_result->append( $r ); return $json_result->getResult( 'import', $result->getMessage() ); } elseif ( WikiError::isError( $result ) ) { return $json_result->getResult( 'import', $source->getMessage() ); } $resultData = $reporter->getData(); $json_result->setStatus( '1' ); // API success return $json_result->getResult(); } else { $APIparams = array( 'action' => 'syncimport', 'format' => 'json', 'token' => $dstImportToken, ); $APIfiles = array( 'xml'=>$fname ); // will POST 'multipart/form-data', because $APIfiles are defined $jr = self::remoteAPIget( self::$remoteContextJSON, $APIparams, $APIfiles, self::RESULT_JSON_ARRAY ); @unlink( $fname ); return $jr; } }
public function execute() { global $wgUser; if ( !$wgUser->isAllowed( 'import' ) ) { $this->dieUsageMsg( array('cantimport') ); } $params = $this->extractRequestParams(); if ( !isset( $params['token'] ) ) { $this->dieUsageMsg( array('missingparam', 'token') ); } if ( !$wgUser->matchEditToken( $params['token'] ) ) { $this->dieUsageMsg( array('sessionfailure') ); } if ( !$wgUser->isAllowed( 'importupload' ) ) { $this->dieUsageMsg( array('cantimport-upload') ); } $source = ImportStreamSource::newFromUpload( 'xml' ); if ( $source instanceof Status ) { if ( $source->isOK() ) { $source = $source->value; } else { $this->dieUsageMsg( array('import-unknownerror', $source->getWikiText() ) ); } } elseif ( $source instanceof WikiErrorMsg ) { $this->dieUsageMsg( array_merge( array($source->getMessageKey()), $source->getMessageArgs() ) ); } elseif ( WikiError::isError( $source ) ) { // This shouldn't happen $this->dieUsageMsg( array('import-unknownerror', $source->getMessage() ) ); } $importer = new WikiImporter( $source ); $reporter = new WikiSyncImportReporter( $importer, true, '', wfMsg( 'wikisync_log_imported_by' ) ); $result = $importer->doImport(); if ( $result instanceof WikiXmlError ) { $this->dieUsageMsg( array('import-xml-error', $result->mLine, $result->mColumn, $result->mByte . $result->mContext, xml_error_string($result->mXmlError) ) ); } elseif ( WikiError::isError( $result ) ) { // This shouldn't happen $this->dieUsageMsg( array('import-unknownerror', $result->getMessage() ) ); } $resultData = $reporter->getData(); $this->getResult()->setIndexedTagName( $resultData, 'page' ); $this->getResult()->addValue( null, $this->getModuleName(), $resultData ); }