public function actionFill_prosecutor_reference() { set_time_limit(0); $bufer = ProsecutorsBuffer::model()->findAll(); if (!$bufer) { $gibds = Prosecutors::model()->findAll(); foreach ($gibds as $gibd) { $model = new ProsecutorsBuffer(); $model->attributes = $gibd->attributes; $model->id = $gibd->id; $model->save(); } } $raw_html = file_get_contents('http://genproc.gov.ru/structure/subjects/'); preg_match_all('`<select([\\s\\S]+)</select>`U', $raw_html, $_matches); preg_match_all('`<option value="([\\d]+)"[\\s\\S]*>([\\s\\S]+)</option>`U', $_matches[0][0], $_matches, PREG_SET_ORDER); //$_matches = array ( 0 => array ( 0 => '', 1 => '110', 2 => 'Центральный федеральный округ', ), 1 => array ( 0 => '', 1 => '111', 2 => 'Северо-Западный федеральный округ', ), 2 => array ( 0 => '', 1 => '112', 2 => 'Южный федеральный округ', ), 3 => array ( 0 => '', 1 => '241', 2 => 'Северо-Кавказский федеральный округ', ), 4 => array ( 0 => '', 1 => '113', 2 => 'Приволжский федеральный округ', ), 5 => array ( 0 => '', 1 => '114', 2 => 'Уральский федеральный округ', ), 6 => array ( 0 => '', 1 => '115', 2 => 'Сибирский федеральный округ', ), 7 => array ( 0 => '', 1 => '116', 2 => 'Дальневосточный федеральный округ', ), 8 => array ( 0 => '', 1 => '242', 2 => 'Центральный аппарат', ), ); foreach ($_matches as &$set) { $raw_html = file_get_contents('http://genproc.gov.ru/structure/subjects/district-' . $set[1] . '/'); //echo 'http://genproc.gov.ru/structure/subjects/district-'.$set[1].'/<br />'; if (!$raw_html) { echo $set[1] . ' - fail<br>'; continue; } $raw_html = substr($raw_html, strpos($raw_html, '<dl class="institutions">')); $raw_html = explode('<div>', $raw_html); foreach ($raw_html as &$office) { $office = explode('</a>', $office); if ($office[1]) { $office[0] = strip_tags($office[0]); $subjects = explode("\n", $office[0]); //print_r($subjects); if (isset($subjects[2])) { $itemname = $subjects[2]; $subjects[1] = preg_replace('/\\(.*\\)/i', '', $subjects[1]); //echo $subjects[1].'<br />'; $subjectmodel = RfSubjects::model()->find("name_full LIKE '" . trim($subjects[1]) . "'"); if ($subjectmodel) { $subject = $subjectmodel->id; } else { $subject = $subject = RfSubjects::model()->SearchID($subjects[1]); } } else { $itemname = $subjects[0]; $subject = 0; } $r['name'] = trim(str_replace("\n", ' ', str_replace("\t", ' ', $office[0]))); $r['gibdd_name'] = trim(str_replace("\n", '', str_replace("\t", '', $itemname))); $r['preview_text'] = trim(str_replace("\t", ' ', strip_tags($office[1], '<br>'))); $r['subject_id'] = $subject; if ($r['subject_id'] == 0) { continue; } $model = ProsecutorsBuffer::model()->find('subject_id=' . (int) $subject); if ($model) { foreach ($r as $key => $val) { unset($r['href']); unset($r['subject_name']); unset($r['id']); if (isset($model->{$key}) && trim($model->{$key}) == trim($val)) { unset($r[$key]); } } if ($r) { $curmodel = Prosecutors::model()->findByPk($model->id); if ($curmodel) { $model->attributes = $r; if (!$model->subject_id) { $model->subject_id = 0; } if ($model->update()) { $curmodel->attributes = $r; $curmodel->save(); echo 'Обновлено ' . $curmodel->gibdd_name; print_r($r); echo '<br />'; } print_r($model->errors); } } } else { $model = new Prosecutors(); $model->attributes = $r; if ($model->save()) { $bufer = new ProsecutorsBuffer(); $bufer->attributes = $model->attributes; $bufer->id = $model->id; $bufer->save(); } } } } //echo $set[1].' - ok<br>'; } }