/**
  * @brief XML파일을 미리 분석하여 개발 단위로 캐싱
  **/
 function procImporterAdminPreProcessing()
 {
     // 이전할 대상 xml파일을 구함
     $xml_file = Context::get('xml_file');
     // 이전할 대상의 type을 구함
     $type = Context::get('type');
     // xml파일에서 정해진 규칙으로 캐싱
     $oExtract = new extract();
     switch ($type) {
         case 'member':
             $output = $oExtract->set($xml_file, '<members ', '</members>', '<member>', '</member>');
             if ($output->toBool()) {
                 $oExtract->saveItems();
             }
             break;
         case 'message':
             $output = $oExtract->set($xml_file, '<messages ', '</messages>', '<message>', '</message>');
             if ($output->toBool()) {
                 $oExtract->saveItems();
             }
             break;
         case 'ttxml':
             // 카테고리 정보를 구함
             $output = $oExtract->set($xml_file, '', '', '', '');
             if ($output->toBool()) {
                 // ttxml 카테고리는 별도로 구함
                 $started = false;
                 $buff = '';
                 while (!feof($oExtract->fd)) {
                     $str = fgets($oExtract->fd, 1024);
                     if (substr($str, 0, strlen('<category>')) == '<category>') {
                         $started = true;
                     }
                     if (substr($str, 0, strlen('<post ')) == '<post ') {
                         break;
                     }
                     if ($started) {
                         $buff .= $str;
                     }
                 }
                 $buff = '<categories>' . $buff . '</categories>';
                 $oExtract->closeFile();
                 $category_filename = sprintf('%s/%s', $oExtract->cache_path, 'category.xml');
                 FileHandler::writeFile($category_filename, $buff);
                 // 방명록 정보를 구함
                 $output = $oExtract->set($xml_file, '', '', '', '');
                 if ($output->toBool()) {
                     $started = false;
                     $buff = '';
                     while (!feof($oExtract->fd)) {
                         $str = fgets($oExtract->fd, 1024);
                         if (substr($str, 0, strlen('<guestbook>')) == '<guestbook>') {
                             $started = true;
                         }
                         if ($started) {
                             $pos = strpos($str, '</guestbook>');
                             if ($pos !== false) {
                                 $buff .= substr($str, 0, $pos + strlen('</guestbook>'));
                                 break;
                             }
                             $buff .= $str;
                         }
                     }
                     $oExtract->closeFile();
                     $guestbook_filename = sprintf('%s/%s', $oExtract->cache_path, 'guestbook.xml');
                     FileHandler::writeFile($guestbook_filename, $buff);
                     // 개별 아이템 구함
                     $output = $oExtract->set($xml_file, '<blog', '</blog>', '<post ', '</post>');
                     if ($output->toBool()) {
                         $oExtract->saveItems();
                     }
                 }
             }
             break;
         default:
             // 카테고리 정보를 먼저 구함
             $output = $oExtract->set($xml_file, '<categories>', '</categories>', '<category', '</category>');
             if ($output->toBool()) {
                 $oExtract->mergeItems('category.xml');
                 // 개별 아이템 구함
                 $output = $oExtract->set($xml_file, '<posts ', '</posts>', '<post>', '</post>');
                 if ($output->toBool()) {
                     $oExtract->saveItems();
                 }
             }
             break;
     }
     if (!$output->toBool()) {
         $this->add('error', 0);
         $this->add('status', -1);
         $this->setMessage($output->getMessage());
         return;
     }
     // extract가 종료됨을 알림
     $this->add('type', $type);
     $this->add('total', $oExtract->getTotalCount());
     $this->add('cur', 0);
     $this->add('key', $oExtract->getKey());
     $this->add('status', 0);
 }
 /**
  * Pre-analyze the xml file and cache it
  * @return void
  */
 function procImporterAdminPreProcessing()
 {
     // Get the target xml file to import
     $xml_file = Context::get('xml_file');
     // Get a type of the target
     $type = Context::get('type');
     // Extract and cache information from the xml file
     $oExtract = new extract();
     switch ($type) {
         case 'member':
             $output = $oExtract->set($xml_file, '<members ', '</members>', '<member>', '</member>');
             if ($output->toBool()) {
                 $oExtract->saveItems();
             }
             break;
         case 'message':
             $output = $oExtract->set($xml_file, '<messages ', '</messages>', '<message>', '</message>');
             if ($output->toBool()) {
                 $oExtract->saveItems();
             }
             break;
         case 'ttxml':
             // Category information
             $output = $oExtract->set($xml_file, '', '', '', '');
             if ($output->toBool()) {
                 // Get a category of ttxml separately
                 $started = false;
                 $buff = '';
                 while (!feof($oExtract->fd)) {
                     $str = fgets($oExtract->fd, 1024);
                     if (strstr($str, '<category>')) {
                         $started = true;
                         $str = strstr($str, '<category>');
                     }
                     if (substr($str, 0, strlen('<post ')) == '<post ') {
                         break;
                     }
                     if ($started) {
                         $buff .= $str;
                     }
                 }
                 $buff = '<categories>' . $buff . '</categories>';
                 $oExtract->closeFile();
                 $category_filename = sprintf('%s/%s', $oExtract->cache_path, 'category.xml');
                 FileHandler::writeFile($category_filename, $buff);
                 // Guestbook information
                 $output = $oExtract->set($xml_file, '', '', '', '');
                 if ($output->toBool()) {
                     $started = false;
                     $buff = '';
                     while (!feof($oExtract->fd)) {
                         $str = fgets($oExtract->fd, 1024);
                         if (strstr($str, '<guestbook>')) {
                             $started = true;
                             $str = strstr($str, '<guestbook>');
                         }
                         if ($started) {
                             $pos = strpos($str, '</guestbook>');
                             if ($pos !== false) {
                                 $buff .= substr($str, 0, $pos + strlen('</guestbook>'));
                                 break;
                             }
                             $buff .= $str;
                         }
                     }
                     $oExtract->closeFile();
                     $guestbook_filename = sprintf('%s/%s', $oExtract->cache_path, 'guestbook.xml');
                     FileHandler::writeFile($guestbook_filename, $buff);
                     // Individual items
                     $output = $oExtract->set($xml_file, '<blog', '</blog>', '<post ', '</post>');
                     if ($output->toBool()) {
                         $oExtract->saveItems();
                     }
                 }
             }
             break;
         default:
             // First get category information
             $output = $oExtract->set($xml_file, '<categories>', '</categories>', '<category', '</category>');
             if ($output->toBool()) {
                 $oExtract->mergeItems('category.xml');
                 // Get each item
                 $output = $oExtract->set($xml_file, '<posts ', '</posts>', '<post>', '</post>');
                 if ($output->toBool()) {
                     $oExtract->saveItems();
                 }
             }
             break;
     }
     if (!$output->toBool()) {
         $this->add('error', 0);
         $this->add('status', -1);
         $this->setMessage($output->getMessage());
         return;
     }
     // Notify that all data completely extracted
     $this->add('type', $type);
     $this->add('total', $oExtract->getTotalCount());
     $this->add('cur', 0);
     $this->add('key', $oExtract->getKey());
     $this->add('status', 0);
 }