Your IP : 3.149.243.136
<?
class XmlParser
{
protected $MAIN_TAG = 'tree';
protected $INFO_TAGS = array('created', 'title', 'doc');
protected $DB = null;
protected $USER = null;
protected $Context = array();
protected $object = array();
protected $inside_data = false;
protected $log = null;
protected $params = Array(
"max_len" => "100", // обрезает символьный код до 100 символов
"change_case" => "L", // буквы преобразуются к нижнему регистру
"replace_space" => "_", // меняем пробелы на нижнее подчеркивание
"replace_other" => "_", // меняем левые символы на нижнее подчеркивание
"delete_repeat_replace" => "true", // удаляем повторяющиеся нижние подчеркивания
"use_google" => "false", // отключаем использование google
);
protected function parseData($tag)
{
if (isset($this->object['title'])
&& isset($this->object['created'])
&& isset($this->object['doc']))
{
$this->object['doc'] = htmlspecialchars_decode($this->object['doc']);
$this->object['at'] = ConvertTimeStamp(strtotime($this->object['created']), "FULL");
$fields = array(
'IBLOCK_ID' => 11,
'NAME' => $this->object['title'],
'CODE' => CUtil::translit($this->object['title'], "ru", $this->params),
'ACTIVE' => 'Y',
'MODIFIED_BY' => $this->USER->GetID(),
'PREVIEW_TEXT' => $this->object['doc'],
'PREVIEW_TEXT_TYPE' => 'html',
"DATE_ACTIVE_FROM"=> $this->object['at']
);
if($ID = $this->DB->Add($fields))
$this->message_to_log('New ID: '.$ID.' - '.$this->object['title']);
else
{
$this->message_to_log('ERROR: '.$this->DB->LAST_ERROR.' - '.$this->object['title']);
$fields['CODE'] = CUtil::translit($this->object['title'].$this->object['created'], "ru", $this->params);
if($ID = $this->DB->Add($fields))
$this->message_to_log('New ID: '.$ID.' - '.$this->object['title']);
else
{
$this->message_to_log('ERROR: '.$this->DB->LAST_ERROR.' - '.$this->object['title']);
}
}
//print_r($this->object);exit;
}
}
public function beginParsing() {}
public function endParsing() {}
function __construct(&$db, &$user, $log) {
$this->DB = $db;
$this->USER = $user;
$this->log = $log;
}
function tagOpen($parser, $name, $attrs) {
$len = count($this->Context);
array_push($this->Context, $name);
$this->inside_data = false;
}
function tagData($parser, $tagData) {
$len = count($this->Context);
if ($len > 1 && in_array($this->Context[$len - 1], $this->INFO_TAGS)
&& $this->Context[$len - 2] == $this->MAIN_TAG)
{
if ($this->inside_data)
$this->object[$this->Context[$len - 1]] .= $tagData;
else
$this->object[$this->Context[$len - 1]] = $tagData;
}
$this->inside_data = true;
}
function tagClosed($parser, $name) {
$this->inside_data = false;
if ($name == $this->MAIN_TAG)
{
$this->parseData($name);
unset($this->object);
}
array_pop($this->Context);
}
function param($name) {
return isset($this->object[$name]) ? $this->object[$name] : null;
}
public function message_to_log($msg) {
//print $msg."\r\n";
if ($this->log)
{
if ($msg[strlen($msg) - 1] != "\n")
$msg.="\r\n";
fputs($this->log, date("r", time()) . " " . $msg);
}
}
}