open($url)){ die('Failed to open'); } $cnt = 0; $offers = []; $time_start = microtime(true); while($reader->read()){ if($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'offer'){ $offer = simplexml_import_dom($doc->importNode($reader->expand(),true)); if((string)$offer->{'category'} == 'квартира' && $offer->{'x-comission'}){ // if((string)$offer['internal-id'] == '{204F24AA-F332-11EA-BBE6-E83935BA0948}'){ // var_dump((string)$offer['internal-id']); $object_id = $this->is_objects_exist('fbn_id',$offer['internal-id'],'objects'); if($object_id>0){ // echo $object_id; $this->update_kvartira($object_id,$offer,$mdb); }else if($this->search_kvartira($offer,$mdb)==0){ $this->add_kvartira($offer,$mdb); } // break; // } } } } echo "TIME : ".(microtime(true)-$time_start); $reader->close(); } private function preprocess($offer){ $ready = []; $ready['operation_type'] = $offer['type'] == 'Продажа' ? 1 : 0; switch ($offer['category']) { case 'квартира': $ready['type'] = 1; break; case 'комната': $ready['type'] = 2; break; case 'дом': $ready['type'] = 3; break; case 'коммерческая': $ready['type'] = 4; break; case 'часть дома': $ready['type'] = 5; break; case 'коммерческая': $ready['type'] = 6; break; case 'участок': $ready['type'] = 7; break; } } function search_kvartira($offer,$mdb){ $sql ="SELECT id FROM objects WHERE "; $sql.="type=1 "; $sql.="AND operation_type=1 "; $sql.="AND stoim=".(string)$offer->{'price'}->{'value'}." "; $sql.="AND etazh=".(string)$offer->{'floor'}." "; $sql.="AND etazh_iz=".(string)$offer->{'floors-total'}; $rez = mysql_query($sql); $rez = mysql_fetch_row($rez); return $rez ? $rez[0] : 0; } function update_kvartira($obj,$offer,$mdb){ $ready['objects']['sobstv'] = (string)$offer->{'sales-agent'}->{'organization'}; if(isset($offer->{'sales-agent'}->{'phone'})){ $ready['objects']['phone'] = (string)$offer->{'sales-agent'}->{'phone'}; $ready['objects']['phone_search'] = num_search((string)$offer->{'sales-agent'}->{'phone'}); }else if(isset($offer->{'sales-agent'}->{'organization-phone'})){ $ready['phone']['phoneAgency'] = (string)$offer->{'sales-agent'}->{'organization-phone'}; $ready['phone']['phone_search'] = num_search((string)$offer->{'sales-agent'}->{'organization-phone'}); } if(isset($offer->{'sales-agent'}->{'name'})){ $ready['objects']['sobstv'] .= " ".(string)$offer->{'sales-agent'}->{'name'}; } if(isset($offer->{'rooms'})){ $ready['objects']['komnat'] = (string)$offer->{'rooms'}; }else{ $ready['objects']['komnat'] = 1; } list($ready['objects']['id_rf_region'],$ready['objects']['rayon'],$ready['objects']['adres']) = $this->get_location($offer); $ready['objects']['nazv'] = $ready['objects']['komnat']." комнатная квартира"; if(isset($offer->{'studio'})){ $ready['objects']['studio_flag'] = 1; } if(isset($offer->{'area'})){ $ready['objects']['ploshad'] = (string)$offer->{'area'}->{'value'}; } if(isset($offer->{'kitchen-space'})){ $ready['objects']['ploshad_k'] = (string)$offer->{'kitchen-space'}->{'value'}; } if(isset($offer->{'living-space'})){ $ready['objects']['ploshad_komn'] = (string)$offer->{'living-space'}->{'value'}; } if(isset($offer->{'floor'})){ $ready['objects']['etazh'] = (string)$offer->{'floor'}; } if(isset($offer->{'floors-total'})){ $ready['objects']['etazh_iz'] = (string)$offer->{'floors-total'}; } if(isset($offer->{'description'})){ $ready['objects']['opis'] = (string)$offer->{'description'}; } if(isset($offer->{'price'})){ $ready['objects']['stoim'] = (string)$offer->{'price'}->{'value'}; } if(isset($offer->{'x-comission'})){ if(intval((string)$offer->{'x-comission'}->{'value'}) <= 100 ){ $ready['objects']['commission'] =(string)$offer->{'x-comission'}->{'value'}; $ready['objects']['lease'] = 0; }else{ $ready['objects']['lease'] =(string)$offer->{'x-comission'}->{'value'}; $ready['objects']['commission'] = 0; } } if(isset($offer->{'x-hot-water'})){ $ready['objects']['hot_water_id'] = $this->select_or_insert_like( 'name', (string)$offer->{'x-hot-water'}, 'hot_water', $mdb ); } if(isset($offer->{'x-rubbish-chute'})){ $ready['objects']['rubbish_chute'] = (string)$offer->{'x-rubbish-chute'}; } if(isset($offer->{'building-type'})){ $ready['objects']['rent_house_type_id'] = $this->determine_house_type((string)$offer->{'building-type'},$mdb); $ready['sale_objects']['house_type'] = $ready['objects']['rent_house_type_id']; }else{ $ready['sale_objects']['house_type']=0; } if(isset($offer->{'deal-status'})){ $ready['sale_objects']['deal_type'] = $this->determine_deal_type((string)$offer->{'deal-status'},$mdb); }else{ $ready['sale_objects']['deal_type'] = 0; } if(isset($offer->{'x-wall'})){ $ready['sale_objects']['house_material'] = $this->determine_house_material((string)$offer->{'x-wall'},$mdb); }else{ $ready['sale_objects']['house_material'] = 0; } $ready['sale_objects']['object_id'] = $obj; $ready['photos'] = []; $this->update_sale_object($ready['sale_objects'],$mdb); if(isset($offer->{'image'})){ for($i = 0;$i{'image'});$i++){ $ready['photos']['photo'.($i+1)] = (string)$offer->{'image'}[$i]; if(($i+1)==50){ break; } } } if(isset($offer->{'creation-date'})){ $ready['objects']['date_really_add'] = (string)$offer->{'creation-date'}; $ready['objects']['date_really_add'] = str_replace('T',' ',$ready['objects']['date_really_add']); $ready['objects']['date_really_add'] = explode('+',$ready['objects']['date_really_add']); $ready['objects']['date_really_add'] = $ready['objects']['date_really_add'][0]; } if(isset($offer->{'mortgage'})){ $ready['objects']['stoim_prim'] = $offer->{'mortgage'} == '1' ? 'Возможна ипотека':''; } if(isset($offer->{'last-update-date'})){ $ready['objects']['date_add'] = (string)$offer->{'last-update-date'}; $ready['objects']['date_add'] = str_replace('T',' ',$ready['objects']['date_add']); $ready['objects']['date_add'] = explode('+',$ready['objects']['date_add']); $ready['objects']['date_add'] = $ready['objects']['date_add'][0]; } if($offer->{'bathroom-unit'}){ $ready['objects']['bath_type_id'] = $this->determine_bath((string)$offer->{'bathroom-unit'},$mdb); } /////////////////////////////////////////////////////////////////////////////////////////// $this->update_object($obj,$ready['objects'],$mdb); if(isset($offer->{'location'}->{'metro'})){ $this->update_object_metro($obj,(string)$offer->{'location'}->{'metro'}->{'name'},$mdb); } /////////////////////////////////////////////////////////////////////////////////////////// $ready['objects_additional_information']['object_id'] = $obj; if(isset($offer->{'ceiling-height'})){ $ready['objects_additional_information']['ceiling_height'] = (string)$offer->{'ceiling-height'}; } if(isset($offer->{'x-window'})){ $ready['objects_additional_information']['window_view'] = $this->select_or_insert_like( 'name', (string)$offer->{'x-window'}, 'window_view', $mdb); } if(isset($offer->{'balcony'})){ $ready['objects_additional_information'] = array_merge($ready['objects_additional_information'],$this->determine_balcony((string)$offer->{'balcony'})); } if(isset($offer->{'renovation'})){ $ready['objects_additional_information']['renovation'] = $this->select_or_insert( ['name'=> (string)$offer->{'renovation'}], 'renovation', $mdb); } $this->update_additional($ready['objects_additional_information'],$mdb); $this->update_photo($obj, $ready['photos'],$mdb); } function add_kvartira($offer,$mdb){ $ready = []; $ready['objects'] = array( 'id_add_user' => 0, 'operation_type' => 0, 'type' => 1, 'rayon' => 0, 'adres'=> '', 'transport' => 0, 'peshkom' => 0, 'komnat'=>0, 'ploshad'=>0, 'ploshad_k'=>0, 'ploshad_komn'=>0, 'etazh'=>0, 'etazh_iz'=>0, 'opis'=>0, 'stoim'=>0, 'commission'=>0, 'hot_water_id'=>0, 'rubbish_chute'=>0, 'rent_house_type_id'=>0, 'photo1'=>'', 'photo2'=>'', 'photo3'=>'', 'photo4'=>'', 'photo5'=>'', 'photo6'=>'', 'photo7'=>'', 'photo8'=>'', 'photo9'=>'', 'photo10'=>'', 'photo11'=>'', 'photo12'=>'', 'photo13'=>'', 'photo14'=>'', 'photo15'=>'', 'photo16'=>'', 'photo17'=>'', 'photo18'=>'', 'photo19'=>'', 'photo20'=>'', 'stoim_prim'=>'', 'date_really_add'=>'', 'date_add'=>'', 'nazv'=>'', 'srok'=>0, 'holod'=>0, 'stirka'=>0, 'tv'=>0, 'mebel'=>0, 'phone'=>'', 'archive'=>0, 'sobstv'=>0, 'phone_search'=>'', 'id_user'=>0, 'prim_srok'=>'', 'black'=>0, 'vsego_komn'=>0, 'moder'=>0, 'id_kv'=>0 ); $ready['objects']['sobstv'] = (string)$offer->{'sales-agent'}->{'organization'}; if(isset($offer->{'sales-agent'}->{'phone'})){ $ready['objects']['phone'] = (string)$offer->{'sales-agent'}->{'phone'}; $ready['objects']['phone_search'] = (string)$offer->{'sales-agent'}->{'phone'}; }else if(isset($offer->{'sales-agent'}->{'organization-phone'})){ $ready['phone']['phoneAgency'] = (string)$offer->{'sales-agent'}->{'organization-phone'}; $ready['phone']['phone_search'] = (string)$offer->{'sales-agent'}->{'organization-phone'}; } if(isset($offer->{'sales-agent'}->{'name'})){ $ready['objects']['sobstv'] .= " ".(string)$offer->{'sales-agent'}->{'name'}; } ////////////////////////////////////////////////////////////////////// $ready['objects']['operation_type'] = (string)$offer->{'type'} == 'Продажа' ? 1 : 0; $ready['objects']['type'] = 1; list($ready['objects']['id_rf_region'],$ready['objects']['rayon'],$ready['objects']['adres']) = $this->get_location($offer); if(isset($offer->{'location'}->{'metro'})){ if(isset($offer->{'location'}->{'metro'}->{'time-on-transport'})){ $ready['objects']['transport'] = (string)$offer->{'location'}->{'metro'}->{'time-on-transport'}; } if(isset($offer->{'location'}->{'metro'}->{'time-on-foot'})){ $ready['objects']['peshkom'] = (string)$offer->{'location'}->{'metro'}->{'time-on-foot'}; } } if(isset($offer->{'rooms'})){ $ready['objects']['komnat'] = (string)$offer->{'rooms'}; }else{ $ready['objects']['komnat'] = 1; } if(isset($offer->{'studio'})){ $ready['objects']['studio_flag'] = 1; } if(isset($offer->{'area'})){ $ready['objects']['ploshad'] = (string)$offer->{'area'}->{'value'}; } if(isset($offer->{'kitchen-space'})){ $ready['objects']['ploshad_k'] = (string)$offer->{'kitchen-space'}->{'value'}; } if(isset($offer->{'living-space'})){ $ready['objects']['ploshad_komn'] = (string)$offer->{'living-space'}->{'value'}; } if(isset($offer->{'floor'})){ $ready['objects']['etazh'] = (string)$offer->{'floor'}; } if(isset($offer->{'floors-total'})){ $ready['objects']['etazh_iz'] = (string)$offer->{'floors-total'}; } if(isset($offer->{'description'})){ $ready['objects']['opis'] = (string)$offer->{'description'}; } if(isset($offer->{'price'})){ $ready['objects']['stoim'] = (string)$offer->{'price'}->{'value'}; } if(isset($offer->{'x-comission'})){ if(intval((string)$offer->{'x-comission'}->{'value'}) <= 100 ){ $ready['objects']['commission'] =(string)$offer->{'x-comission'}->{'value'}; }else{ $ready['objects']['lease'] =(string)$offer->{'x-comission'}->{'value'}; } } if(isset($offer->{'x-hot-water'})){ $ready['objects']['hot_water_id'] = $this->select_or_insert_like( 'name', (string)$offer->{'x-hot-water'}, 'hot_water', $mdb ); } if(isset($offer->{'x-rubbish-chute'})){ $ready['objects']['rubbish_chute'] = (string)$offer->{'x-rubbish-chute'}; } $ready['photos'] = []; if(isset($offer->{'image'})){ for($i = 0;$i{'image'});$i++){ $ready['photos']['photo'.($i+1)] = (string)$offer->{'image'}[$i]; if(($i+1)==50){ break; } } } if(isset($offer->{'creation-date'})){ $ready['objects']['date_really_add'] = (string)$offer->{'creation-date'}; $ready['objects']['date_really_add'] = str_replace('T',' ',$ready['objects']['date_really_add']); $ready['objects']['date_really_add'] = explode('+',$ready['objects']['date_really_add']); $ready['objects']['date_really_add'] = $ready['objects']['date_really_add'][0]; } if(isset($offer->{'mortgage'})){ $ready['objects']['stoim_prim'] = $offer->{'mortgage'} == '1' ? 'Возможна ипотека':''; } if(isset($offer->{'last-update-date'})){ $ready['objects']['date_add'] = (string)$offer->{'last-update-date'}; $ready['objects']['date_add'] = str_replace('T',' ',$ready['objects']['date_add']); $ready['objects']['date_add'] = explode('+',$ready['objects']['date_add']); $ready['objects']['date_add'] = $ready['objects']['date_add'][0]; } $ready['objects']['nazv'] = $ready['objects']['komnat'] == 0 ? 1 : $ready['objects']['komnat']; $ready['objects']['nazv'] .= " комнатная квартира"; $ready['objects']['fbn_id'] = $offer['internal-id']; if(isset($offer->{'building-type'})){ $ready['objects']['rent_house_type_id'] = $this->determine_house_type((string)$offer->{'building-type'},$mdb); $ready['sale-objects']['house_type'] = $ready['objects']['rent_house_type_id']; } if($offer->{'bathroom-unit'}){ $ready['objects']['bath_type_id'] = $this->determine_bath((string)$offer->{'bathroom-unit'},$mdb); } $object_id = $this->add_object($ready['objects'],$mdb); /////////////////////////////////////////////////////////////////////////// if(isset($offer->{'deal-status'})){ $ready['sale_objects']['deal_type'] = $this->determine_deal_type($offer->{'deal-status'},$mdb); }else{ $ready['sale_objects']['deal_type'] = 0; } if(isset($offer->{'x-wall'})){ $ready['sale_objects']['house_material'] = $this->determine_house_material($offer->{'x-wall'},$mdb); }else{ $ready['sale_objects']['house_material'] = 0; } if(isset($offer->{'building-type'})){ $ready['sale_objects']['house_type'] = $this->determine_house_type($offer->{'building-type'},$mdb); }else{ $ready['sale_objects']['house_type'] = 0; } $ready['sale_objects']['object_id'] = $object_id; $this->add_sale_object($ready['sale_objects'],$mdb); /////////////////////////////////////////////////////////////////////////// if(isset($offer->{'location'}->{'metro'})){ $this->add_metro_to_object($object_id,(string)$offer->{'location'}->{'metro'}->{'name'},$mdb); } //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// $ready['objects_additional_information']['object_id'] = $object_id; if(isset($offer->{'ceiling-height'})){ $ready['objects_additional_information']['ceiling_height'] = (string)$offer->{'ceiling-height'}; } if(isset($offer->{'x-window'})){ $ready['objects_additional_information']['window_view'] = $this->select_or_insert_like( 'name', (string)$offer->{'x-window'}, 'window_view', $mdb); } if(isset($offer->{'balcony'})){ $ready['objects_additional_information'] = array_merge($ready['objects_additional_information'],$this->determine_balcony((string)$offer->{'balcony'})); } if(isset($offer->{'renovation'})){ $ready['objects_additional_information']['renovation'] = $this->select_or_insert( ['name'=> (string)$offer->{'renovation'}], 'renovation', $mdb); } $this->add_addit_info($ready['objects_additional_information'],$mdb); $this->update_photo($object_id, $ready['photos'],$mdb); return $object_id; } function determine_house_material($str,$mdb){ if(strtolower($str) == 'кирпично-монолитный'){ return 5; }else{ return $this->select_or_insert_like( 'name', $str, 'house_material', $mdb); } } function determine_house_type($str,$mdb){ if(strtolower($str) == 'кирпично-монолитный'){ return 15; }else{ return $this->select_or_insert_like( 'name', $str, 'house_type', $mdb); } } function determine_balcony($str){ $arr = explode(' ',$str); $result = []; for($i=0;$i10, 'более 2х санузлов'=>11, 'сауна'=>12, 'в доме'=>13, 'на улице'=>14, 'биотуалет'=>15, 'ванна'=>16, 'бассейн'=>17 ]; $id = 0; if('совмещенный' == $str){ $id = 2; } if('раздельный' == $str){ $id = 3; } if('душ на кухне' == $str){ $id = 7; } if('без ванны' == $str){ $id = 9; } if('душ' == $str){ $id = 1; } if('душ' == $str){ $id = 1; } if('ванна на кухне' == $str){ $id = 7; } if('ванна на кухне' == $str){ $id = 7; } if($id==0){ $id = $this->select_or_insert_like('name',$str,'bath_type',$mdb); } return $id; } function determine_hot_water($str,$mdb){ $sql = "SELECT id FROM hot_water WHERE name LIKE '".$str."'"; $rez = mysql_query($sql); $rez = mysql_fetch_row($rez); return $rez[0]; } function determine_deal_type($str,$mdb){ $sql = "SELECT id FROM deal_type WHERE name LIKE '".$str."'"; $rez = mysql_query($sql); $rez = mysql_fetch_row($rez); return $rez[0]; } function select_or_insert($obj,$table,$mdb){ $cond = []; foreach($obj as $key => $value){ array_push($cond,$key."='".$value."'"); } $sql = "SELECT id FROM ".$table." WHERE ".implode(' AND ',$cond); $rez=mysql_query($sql,$mdb); $rez = mysql_fetch_row($rez); if(!$rez){ $temp = array_map(function($x){ return "'".$x."'"; },$obj); $sql = "INSERT INTO ".$table."(".implode(',',array_keys($obj)).") VALUES (".implode(',',$temp).")"; mysql_query($sql,$mdb); $id = mysql_insert_id(); }else{ $id = $rez[0]; } return $id; } function add_metro_to_object($obj,$metro,$mdb){ $metro = $this->select_or_insert(['metro' => $metro, ],'metro',$mdb); $sql = "INSERT INTO sp_metro (id_metro,id_obj) VALUES (".$metro.",".$obj.")"; mysql_query($sql,$mdb); } function select_or_insert_like($key,$values,$table,$mdb){ $sequnce = explode(' ',$values); $sql = "SELECT * FROM `$table` where "; $sql .= "name LIKE '%".$sequnce[0]."%'"; for($i=1;$iselect_or_insert($obj,'sale_objects',$mdb ); } function add_addit_info($obj,$mdb){ $sale = $this->select_or_insert($obj,'objects_additional_information',$mdb); } function update_object($obj,$offer,$mdb){ $sql = "UPDATE objects SET "; $temp = []; foreach($offer as $key => $value){ $temp[] = $key."='".$value."'"; } $sql .= implode(',',$temp); $sql .= " WHERE id = ".$obj; mysql_query($sql,$mdb); } function update_object_metro($obj,$metro,$mdb){ $metro = $this->select_or_insert(['metro' => $metro, ],'metro',$mdb); $sql = "DELETE from sp_metro WHERE id_obj = ".$obj; mysql_query($sql,$mdb); $sql = "INSERT INTO sp_metro (id_metro,id_obj) VALUES (".$metro.",".$obj.")"; mysql_query($sql,$mdb); } function update_sale_object($offer,$mdb){ $sql = "DELETE from sale_objects WHERE object_id =".$offer['object_id']; mysql_query($sql,$mdb); $this->select_or_insert($offer,'sale_objects',$mdb); } function update_additional($offer,$mdb){ $sql = "DELETE FROM objects_additional_information WHERE object_id = ".$offer['object_id']; mysql_query($sql,$mdb); $this->select_or_insert($offer,'objects_additional_information',$mdb); } function update_photo($objectId, $photos, $mdb) { //удаляем старые фото $sqlSelectPhoto = "select oop.id, oop.object_photo_id from objects_object_photo oop where oop.object_id = $objectId"; $rezSelectPhoto = mysql_query($sqlSelectPhoto); if (mysql_num_rows($rezSelectPhoto) > 0) { while ($ph = mysql_fetch_assoc($rezSelectPhoto)) { $sqlDeletePhoto = "DELETE FROM object_photo where id = $ph[object_photo_id]"; mysql_query($sqlDeletePhoto); $sqlDeletePhotoLinks = "DELETE FROM objects_object_photo where id = $ph[id]"; mysql_query($sqlDeletePhotoLinks); } } //вставляем фото for($num = 1; $num <= 50; $num++) { if (isset($photos['photo' . $num]) && $photos['photo' . $num]) { $p = $photos['photo' . $num]; $sqlInsPhoto = "INSERT INTO object_photo(photo) VALUES('$p')"; mysql_query($sqlInsPhoto, $mdb); $photoId = mysql_insert_id(); $sqlInsPhotoLink = "INSERT INTO objects_object_photo(object_id, object_photo_id, sort_order) VALUES($objectId, $photoId, $num)"; mysql_query($sqlInsPhotoLink, $mdb); } } } function is_objects_exist($key,$value,$table){ $sql = "SELECT id FROM ".$table." WHERE ".$key." = '$value'"; $rez = mysql_query($sql); $rez = mysql_fetch_row($rez); return $rez ? $rez[0]:0; } function is_object_exist_by_like($key,$value,$table,$with_like=false){ $sql = "SELECT id FROM ".$table." WHERE "; $sql .=$key." LIKE '%".$value."%'"; $rez = mysql_query($sql); $rez = mysql_fetch_array($rez); return $rez ? $rez[0] : 0; } function get_location($offer){ $adres = ''; $rayon = 0; if(isset($offer->{'location'}->{'locality-name'})){ $rayon=explode(' ',(string)$offer->{'location'}->{'locality-name'})[0]; if($rayon != $this->city){ $rayon = $this->is_object_exist_by_like('rayon',$rayon,'rayon'); } } if($rayon == 0){ $adres .=(string)$offer->{'location'}->{'locality-name'}.","; } if(isset($offer->{'location'}->{'district'})){ $rayon=explode(' ',(string)$offer->{'location'}->{'district'})[0]; $rayon = $this->is_object_exist_by_like('rayon',$rayon,'rayon'); } if($rayon == 0){ if(isset($offer->{'location'}->{'sub-locality-name'})){ $rayon=explode(' ',(string)$offer->{'location'}->{'sub-locality-name'})[0]; $rayon = $this->is_object_exist_by_like('rayon',$rayon,'rayon'); } } if($rayon != 0){ $sqlRegion = "SELECT is_lo FROM rayon WHERE id = $rayon"; var_dump($sqlRegion); $rez = mysql_query($sqlRegion); if($row = mysql_fetch_row($rez)){ $region = $row[0] == 0 ? 81 : 50; } } if(isset($offer->{'location'}->{'address'})){ $adres .= (string)$offer->{'location'}->{'address'}; } return array($region,$rayon,$adres); } }