SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | ini_set('memory_limit', '1G');
| |
| 3 | date_default_timezone_set('Europe/Moscow');
| |
| 4 | require_once 'Db.php'; | |
| 5 | ||
| 6 | function import() {
| |
| 7 | $stmt = Db::getInstance()->prepare('SELECT id, gdeslon_id FROM gdeslon_products');
| |
| 8 | $stmt->execute(); | |
| 9 | $gdeslonProductIds = array(); | |
| 10 | print "\n" . date('Y-m-d H:i:s') . "\tFetching existing product ids";
| |
| 11 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
| |
| 12 | $gdeslonProductIds[$row['gdeslon_id']] = $row['id']; | |
| 13 | } | |
| 14 | print "\n" . date('Y-m-d H:i:s') . "\tDone fetching existing product ids";
| |
| 15 | $xmlFile = 'import.xml'; | |
| 16 | $xmlReader = new XMLReader(); | |
| 17 | $r = $xmlReader->open($xmlFile); | |
| 18 | print "\nStarted to read XML file\t" . date('Y-m-d H:i:s') . "\n";
| |
| 19 | $offersAdded = 0; | |
| 20 | $currentOffersAdded = 0; | |
| 21 | while ($xmlReader->read()) {
| |
| 22 | if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name == 'category') {
| |
| 23 | $categoryId = $xmlReader->getAttribute('id');
| |
| 24 | $parentId = $xmlReader->getAttribute('parentId');
| |
| 25 | $name = html_entity_decode($xmlReader->readString()); | |
| 26 | $sql = "INSERT INTO gdeslon_categories_raw (gdeslon_cat_id, gdeslon_parent_cat_id, name) " . | |
| 27 | "VALUES (:gdeslon_cat_id, :gdeslon_parent_cat_id, :name) ON DUPLICATE KEY UPDATE name = :name"; | |
| 28 | $sth = Db::getInstance()->prepare($sql); | |
| 29 | $queryParams = array(':gdeslon_cat_id' => $categoryId, ':gdeslon_parent_cat_id' => $parentId,
| |
| 30 | ':name' => $name); | |
| 31 | $sth->execute($queryParams); | |
| 32 | } | |
| 33 | if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name == 'offer') {
| |
| 34 | $gdeslonCategoryId = $xmlReader->getAttribute('gs_category_id');
| |
| 35 | $merchantId = $xmlReader->getAttribute('merchant_id');
| |
| 36 | $gdeslonId = $xmlReader->getAttribute('id');
| |
| 37 | unset($gdeslonProductIds[$gdeslonId]); | |
| 38 | $dom = new DOMDocument(); | |
| 39 | $dom->loadXML('<offer>' . trim($xmlReader->readInnerXml()) . '</offer>');
| |
| 40 | $thumbnail = $dom->getElementsByTagName('thumbnail');
| |
| 41 | if ($thumbnail->length > 0) {
| |
| 42 | $thumbnail = $thumbnail->item(0)->nodeValue; | |
| 43 | $picture = $dom->getElementsByTagName('picture')->item(0)->nodeValue;
| |
| 44 | $url = $dom->getElementsByTagName('url')->item(0)->nodeValue;
| |
| 45 | $urlHash = sha1($url); | |
| 46 | $price = $dom->getElementsByTagName('price')->item(0)->nodeValue;
| |
| 47 | $currency = $dom->getElementsByTagName('currencyId')->item(0)->nodeValue;
| |
| 48 | $name = html_entity_decode($dom->getElementsByTagName('name')->item(0)->textContent);
| |
| 49 | $description = html_entity_decode($dom->getElementsByTagName('description')->item(0)->textContent);
| |
| 50 | $categoryId = $dom->getElementsByTagName('categoryId')->item(0)->nodeValue;
| |
| 51 | $vendor = html_entity_decode($dom->getElementsByTagName('vendor')->item(0)->textContent);
| |
| 52 | $sql = "INSERT INTO gdeslon_products (name, description, category_id, currency, gdeslon_id, gs_category_id, merchant_id, " . | |
| 53 | " picture, price, thumbnail, vendor, out_url, out_url_hash) VALUES (:name, :description, :category_id, :currency, :gdeslon_id, " . | |
| 54 | ":gs_category_id, :merchant_id, :picture, :price, :thumbnail, :vendor, :out_url, :out_url_hash)" . | |
| 55 | " ON DUPLICATE KEY UPDATE name = :name, description = :description, price = :price, oldprice = VALUES(price)"; | |
| 56 | $sth = Db::getInstance()->prepare($sql); | |
| 57 | $queryParams = array(':category_id' => $categoryId, ':currency' => $currency, ':gdeslon_id' => $gdeslonId,
| |
| 58 | ':gs_category_id' => $gdeslonCategoryId, ':merchant_id' => $merchantId, ':out_url' => $url, | |
| 59 | ':out_url_hash' => $urlHash, ':picture' => $picture, ':price' => $price, | |
| 60 | ':thumbnail' => $thumbnail, ':vendor' => $vendor, ':name' => $name, ':description' => $description, | |
| 61 | ); | |
| 62 | $sth->execute($queryParams); | |
| 63 | $offersAdded++; | |
| 64 | $currentOffersAdded++; | |
| 65 | if ($currentOffersAdded == 10000) {
| |
| 66 | print "\n" . date("H:i:s") . "\tAdded:\t" . $offersAdded;
| |
| 67 | $currentOffersAdded = 1; | |
| 68 | } | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | print "\n" . date('Y-m-d H:i:s') . "\tFinished to read XML ($offersAdded added)";
| |
| 73 | if (count($gdeslonProductIds)) {
| |
| 74 | print "\n" . date('Y-m-d H:i:s') . "\tDeleting old products:\t" . count($gdeslonProductIds);
| |
| 75 | $sql = "DELETE FROM gdeslon_products WHERE id IN (" . implode(',', array_values($gdeslonProductIds)) . ")";
| |
| 76 | // print $sql; | |
| 77 | $stmt = Db::getInstance()->exec($sql); | |
| 78 | var_dump($stmt); | |
| 79 | } | |
| 80 | updateProductsQuantity(); | |
| 81 | } | |
| 82 | ||
| 83 | function updateProductsQuantity() {
| |
| 84 | print "\n" . date('Y-m-d H:i:s') . "\tUpdating number of products";
| |
| 85 | $sql = "SELECT id, gdeslon_cat_id, gdeslon_parent_cat_id, name FROM gdeslon_categories_raw"; | |
| 86 | $stmt = Db::getInstance()->prepare($sql); | |
| 87 | $stmt->execute(); | |
| 88 | $categoriesUpdated = 0; | |
| 89 | print "\n" . date('Y-m-d H:i:s') . "\tGetting categories from DB";
| |
| 90 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
| |
| 91 | $allCategories[] = $row; | |
| 92 | ||
| 93 | } | |
| 94 | print "\n" . date('Y-m-d H:i:s') . "\tUpdating categories";
| |
| 95 | $sql = 'update gdeslon_categories_raw set products_num = 0, parent_cat_products_num = 0'; | |
| 96 | Db::getInstance()->exec($sql); | |
| 97 | if (count($allCategories)) {
| |
| 98 | foreach ($allCategories as $k => $row) {
| |
| 99 | $data = getProductsQuantity($row['gdeslon_cat_id'], $row['gdeslon_parent_cat_id']); | |
| 100 | print "\n" . date('Y-m-d H:i:s') . "\tUpdating {$row['name']}\tcurrent: {$data['cat']}, parent: {$data['parent']}";
| |
| 101 | $sql = "UPDATE gdeslon_categories_raw SET products_num = {$data['cat']}, parent_cat_products_num = {$data['parent']}" .
| |
| 102 | " WHERE id = {$row['id']}";
| |
| 103 | $stmt = Db::getInstance()->exec($sql); | |
| 104 | $categoriesUpdated++; | |
| 105 | print "\t$categoriesUpdated categories updated"; | |
| 106 | } | |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 110 | function getProductsQuantity($catId, $parentCatId) {
| |
| 111 | $stmt = Db::getInstance()->prepare("SELECT COUNT(*) AS num FROM gdeslon_products WHERE category_id = :category_id");
| |
| 112 | $stmt->execute(array(':category_id' => $catId));
| |
| 113 | $row = $stmt->fetch(PDO::FETCH_ASSOC); | |
| 114 | $numCat = $row['num']; | |
| 115 | $stmt = Db::getInstance()->prepare("SELECT COUNT(*) AS num FROM gdeslon_products WHERE gs_category_id = :category_id");
| |
| 116 | $stmt->execute(array(':category_id' => $parentCatId));
| |
| 117 | $row = $stmt->fetch(PDO::FETCH_ASSOC); | |
| 118 | $numParentCat = $row['num']; | |
| 119 | return array('cat' => $numCat, 'parent' => $numParentCat);
| |
| 120 | } | |
| 121 | ||
| 122 | function downloadFile() {
| |
| 123 | $cmd = "wget 'http://exporter.gdeslon.ru/uploads/exports/6542c8e30ef0b1731413cea0c288e40d033f1f83.xml.zip' -O import.xml.zip"; | |
| 124 | exec($cmd); | |
| 125 | exec('unzip import.xml.zip');
| |
| 126 | exec('mv 6542c8e30ef0b1731413cea0c288e40d033f1f83.xml import.xml');
| |
| 127 | } | |
| 128 | ||
| 129 | //updateProductsQuantity(); | |
| 130 | //downloadFile(); | |
| 131 | import(); |