Переводим YML в CSV
17.11.2015
Данный скрипт парсит YML (или аналогичный XML файл) и сохраняет данные в CSV.
index.php (Download)
<? ini_set('display_errors', '1'); error_reporting(2047); require_once('parser.php'); $oParser = new c_yml_parser(); $oParser->Parse('Input/vm2_market.xml', 'Output/file.csv'); // $oParser->Parse('vm2_market_1251.xml'); ?>
parser.php (Download)
<? class c_yml_parser { /** * Main * * @param string $sFN_in - ��� ����� * @param string $sFN_out - ���� �������� �������� (� ��� �������� images �������� ��������) * * @return void */ public function Parse($sFN_in, $sFN_out) { $aImportCats = array(); /* 32; // ������� 14; // ����� 894; // Onika 877; // �������� �� ������ Aquanet 905; // ����� �� ������ ������� 22; // ����� ����������--����+������ 15; // ������������ ����� 37; // ��������� ����� 42; // ����������� ����� */ $aImportCats[] = 12; // ���� $sPicDir = dirname($sFN_out); $oXml = simplexml_load_file($sFN_in); $aList = array(); $aTitles = array( '�������', // '������������', // '������������ (English)', '������������ (�������)', 'ID ������ (���� URL; ���������� � ����� �� ��� �������)', '����', '�������� ���� �������', '�����', '����� �����', '����� ����', '�� ������', '�������', // '��������', '�������� (�������)', // '������� ��������', '������� �������� (�������)', '����������', // '��������� ������', '��������� ������ (�������)', // '�� META keywords', // '�� META description', '�� META keywords (�������)', '�� META description (�������)', '�������� ��������', '��� ��������', '��������� ��������', '���������� �� ��������� ����� �������� (����)', // 20 '���� ��������', '��������� ���� �� ��������', '��������� �������� (���)', '���������' ); // 24 //, ��������� $aList[] = $aTitles; echo '<b>Count:</b>'. count($oXml->shop->categories->category).'<br>'; $aCats = array(); foreach($oXml->shop->categories->category as $oCategory) { $aAttr = $oCategory->attributes(); $id = $aAttr['id']; // echo 'id'.$id.'<br>'; // echo '['.$id.'] '.$oCategory.'<br>'; $aCats["$id"] = (string) $oCategory; } /* echo '<pre>'; print_r($aCats); echo '</pre><hr/>'; //*/ $aResults = array(); echo '<b>Count:</b>'. count($oXml->shop->offers->offer).'<br>'; $curr_category_Id = -1; $i=0; $iGetedItemsCount=0; foreach($oXml->shop->offers->offer as $oOffer) { /* echo '<pre>'; print_r($oOffer); echo '</pre>'; //*/ $oOffer = $this->UTF_2_1251($oOffer); $categoryId = (int) $oOffer->categoryId; if (in_array($categoryId, $aImportCats)) { $PicName = $categoryId .'_'. $this->makePicName($oOffer->picture, $i); if ($curr_category_Id != $categoryId) { // echo $curr_category_Id. ' :: ' .$categoryId.'<br>'; // $oAcrCat = isset($aCats[$categoryId])?$aCats[$categoryId]:Null; if (!isset($aResults[$categoryId])) { $aResults[$categoryId] = array(); } /* if ($oAcrCat) { $oAcrCat = $this->UTF_2_1251($oAcrCat); $aList[] = $this->makeCategory($oAcrCat, $i); } else { echo 'Error $categoryId:'.$categoryId.'<br>'; } */ $curr_category_Id = $categoryId; // echo '['.$categoryId.'] '.$oAcrCat.'<br>'; } //$aList[] = $this->makeItem($oOffer, $i, $PicName); $aResults[$categoryId][] = $this->makeItem($oOffer, $i, $PicName); $iGetedItemsCount++; $sNewFileName = $sPicDir.'/images/'.$PicName; if (!file_exists($sNewFileName)) { copy($oOffer->picture, $sNewFileName); } } $i++; /* if ($i > 2000) { break; } //*/ } foreach($aResults as $categoryId => $aResult) { $oAcrCat = isset($aCats[$categoryId])?$aCats[$categoryId]:Null; if ($oAcrCat) { $oAcrCat = $this->UTF_2_1251($oAcrCat); $aList[] = $this->makeCategory($oAcrCat, $i); foreach($aResult as $lines) // ������������� { $aList[] = $lines; } } else { echo 'Error $categoryId:'.$categoryId.'<br>'; } } $fp = fopen($sFN_out, 'w'); foreach ($aList as $fields) { fputcsv($fp, $fields, ';'); } fclose($fp); echo '��������� �������� ���������: '.$iGetedItemsCount.'<br>'; echo 'memory_get_peak_usage: <b>'.$this->mem_convert(memory_get_peak_usage(True)).'</b><br>'; //- echo 'memory_get_usage: <b>'.$this->mem_convert(memory_get_usage(True)).'</b><br>'; } /** * Text * * @param mixed $OBJ * @return object */ function UTF_2_1251(&$OBJ) { /* @attributes url price currencyId categoryId picture delivery vendor model description */ if (is_object($OBJ)) { $class_vars = get_object_vars($OBJ); foreach ($class_vars as $name => $value) { if (is_string($value)) { $val = ''; try { @$val = iconv("UTF-8", "windows-1251", $value); } catch (Exception $e) { echo '������� ��������: ', $e->getMessage(), "\n"; } if ($val) { $OBJ->$name = $val; // echo 'ok<hr>'; } } } } else { $OBJ = iconv("UTF-8", "windows-1251", $OBJ); } return $OBJ; } /* function getExtension($filename) { return substr(strrchr($fileName, '.'), 1); } */ function makePicName($fileName, $i) { if ($fileName) { $path_parts = pathinfo($fileName); $fileName = ($i+1).'.'.$path_parts['extension']; $fileName = (str_repeat('0', 12 - strlen($fileName))).$fileName; } return $fileName; } /** * Text * * @param object $oOffer * @param int $i * @return array */ function makeItem(&$oOffer, $i, $PicName) { return array( $i, //'�������', // $oOffer->model, //'������������ (English)', $oOffer->model, //'������������ (�������)', '', //'ID ������ (���� URL; ���������� � ����� �� ��� �������)', $oOffer->price, //'����', '', //'�������� ���� �������', '', //'�����', 1, //'����� �����', 0, //'����� ����', 999, //'�� ������', 0, //'�������', $oOffer->description, // '�������� (�������)', $oOffer->description, // '������� �������� (�������)', $i, // '����������', $oOffer->model, //'��������� ������ (�������)', $oOffer->model, //''�� META keywords (�������)' $oOffer->model, //'�� META description (�������)', 0, //'�������� ��������', 0, //'��� ��������', 0, //'��������� ��������', 1, //'���������� �� ��������� ����� �������� (����)', // 20 '', //'���� ��������', 999, //'��������� ���� �� ��������', 0, // '��������� �������� (���)', $PicName.','.$PicName.','.$PicName //'���������' ); } /** * Text * * @param object $oCaregory * @param int $i * @return array */ function makeCategory(&$oCaregory, $i) { return array( '', //'�������', // $oOffer->model, //'������������ (English)', $oCaregory, //'������������ (�������)', '', //'ID ������ (���� URL; ���������� � ����� �� ��� �������)', '', //'����', '', //'�������� ���� �������', '', //'�����', '', //'����� �����', '', //'����� ����', '', //'�� ������', '', //'�������', '', // '�������� (�������)', '', // '������� �������� (�������)', $i, // '����������', '', //'��������� ������ (�������)', '', //''�� META keywords (�������)' '', //'�� META description (�������)', '', //'�������� ��������', '', //'��� ��������', '', //'��������� ��������', '', //'���������� �� ��������� ����� �������� (����)', // 20 '', //'���� ��������', '', //'��������� ���� �� ��������', '', // '��������� �������� (���)', '' //'���������' ); } /** * Text * * @param int $size - * @return string */ function mem_convert($size) { $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; } } ?>