Exemplo n.º 1
0
 /**
  * Performs the actual export functions.
  */
 private function runPostProcessing()
 {
     $response = new AjaxResponse();
     $this->mimeType = CFileHelper::getMimeType($this->file);
     $this->mimeType = substr($this->mimeType, 0, strpos($this->mimeType, ";"));
     $sqlSplitter = new SqlSplitter();
     $sqlSplitter->ignoreLastQuery = true;
     $readingBuffer = '';
     $queryCount = 0;
     $chunkSize = $this->chunkSize;
     // Open file and set position to last position
     switch ($this->mimeType) {
         // GZip - Files
         case 'application/x-gzip':
             $handle = gzopen($this->file, 'r');
             gzseek($handle, $this->position, SEEK_SET);
             while (!gzeof($handle)) {
                 $readingBuffer .= gzread($handle, $chunkSize);
                 $queryCount = count($sqlSplitter->getQueries($readingBuffer));
                 $chunkSize *= 2;
                 if ($queryCount > 0) {
                     $queries = $sqlSplitter->getQueries();
                     break;
                 }
             }
             gzclose($handle);
             break;
             // BZip - Files
         // BZip - Files
         case 'application/x-bzip2':
             $handle = bzopen($this->file, 'r');
             bzread($handle, $this->position);
             do {
                 $temp = bzread($handle, $chunkSize);
                 if ($temp !== false) {
                     $readingBuffer .= $temp;
                 }
                 $chunkSize *= 2;
                 $queryCount = count($sqlSplitter->getQueries($readingBuffer));
                 if ($queryCount > 0) {
                     $queries = $sqlSplitter->getQueries();
                     break;
                 }
             } while ($temp);
             bzclose($handle);
             break;
             // All other files (plain text)
         // All other files (plain text)
         default:
             $handle = fopen($this->file, 'r');
             fseek($handle, $this->position, SEEK_SET);
             while (!feof($handle)) {
                 $temp = fread($handle, $chunkSize);
                 #$encoding = mb_detect_encoding($temp);
                 $readingBuffer .= $temp;
                 $queryCount = count($sqlSplitter->getQueries($readingBuffer));
                 $chunkSize *= 2;
                 // Skip loop when a complete query was found
                 if ($queryCount > 0) {
                     $queries = $sqlSplitter->getQueries();
                     break;
                 }
             }
             fclose($handle);
             break;
     }
     // No more queries could be found
     if ($queryCount > 0) {
         $newPosition = $this->position + $sqlSplitter->getPosition();
         // Calculate end time
         $end = microtime(true) + $this->timeLimit;
         $executedQueries = 0;
         while ($executedQueries < $queryCount) {
             try {
                 $cmd = $this->db->createCommand($queries[$executedQueries]);
                 $cmd->execute();
                 if ($executedQueries === 0) {
                     $response->executeJavaScript('sideBar.loadTables("' . $this->schema . '")');
                 }
             } catch (CDbException $ex) {
                 $dbException = new DbException($cmd);
                 $response->addData('error', true);
                 $response->addNotification('error', $dbException->getText(), '', $queries[$executedQueries]);
             }
             /*
             				if(YII_DEBUG)
             				{
             					$response->addData('error', true);
             					$response->addNotification('success', Yii::t('core', 'successExecuteQuery'), '', $queries[$executedQueries]);
             				}*/
             $executedQueries++;
             $this->totalExecutedQueries++;
             // If partial import is activated, break current transaction
             if (microtime(true) > $end) {
                 break;
             }
         }
         // Not all queries could be executed, rewind to last executed query position
         if ($queryCount > $executedQueries) {
             $notExecutedCharCount = 0;
             for ($i = $executedQueries; $i < $queryCount; $i++) {
                 $notExecutedCharCount += $sqlSplitter->getQueryLength($i + 1);
             }
             $newPosition -= $notExecutedCharCount;
         }
     } else {
         $response->refresh = true;
         $this->finished = true;
         $response->addNotification('success', Yii::t('core', 'successImportFile'), Yii::t('core', 'executedQueries') . ": " . $this->totalExecutedQueries);
         $response->executeJavaScript('sideBar.loadTables("' . $this->schema . '")');
         @unlink($this->file);
     }
     // Skip delimiter
     $this->position = $newPosition + 1;
     $data = array('position' => $this->position, 'schema' => $this->schema, 'file' => $this->file, 'filesize' => $this->fileSize, 'mimetype' => $this->mimeType, 'finished' => $this->finished, 'totalExecutedQueries' => $this->totalExecutedQueries);
     $response->addData(null, $data);
     return $response;
 }