Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once PROJECT_ROOT . "/classes/utils/SmCheckSystem.class.php";
- /**
- * Log parser - parses log file and saves result to DB
- * @author Chirkin Ivan
- * @package Raznoe
- * @uses DbSimple
- * @since 2010-06-29
- */
- class Raznoe_LogParser extends Object__Parser2
- {
- /**
- * Config
- * @var array
- */
- protected $config = array(
- 'path' => ''
- );
- /**
- * Instance of class
- * @var Raznoe_LogParser
- */
- protected static $instance = null;
- /**
- * Regular for log row
- * @var unknown_type
- */
- const ROW_REGULAR = '#\[(.*?)\] "GET /s.gif\?([0-9]+).*?HTTP/1.1"[^"]*?"(.*?)"#i';
- /**
- * Returns instance
- */
- static function instance($config = array())
- {
- if (!empty(self::$instance)) {
- return self::$instance;
- } else {
- // Inits class
- return self::$instance = new self($config);
- }
- }
- /**
- * Constructor
- * @param array $config
- */
- function __construct($config = array())
- {
- if (is_array($config)) {
- $this->config = array_merge(
- $this->config, $config
- );
- }
- parent::__construct();
- }
- /**
- * Set path of log file
- * @param unknown_type $path
- */
- public static function setPath($path = null)
- {
- self::instance()->config['path'] = $path;
- }
- /**
- * Parse log file
- */
- public static function parse()
- {
- Sprav::getSpravType('click_settings')->emptyCache();
- $is_disabled = Sprav::getName('click_settings', 'is_disabled');
- $_this = self::instance();
- if ($is_disabled == 1) {
- $_this->debug('Парсинг отключен (click_settings.is_disabled=1)');
- exit();
- }
- if (DEBUG) {
- $_this->debug('DEBUG режим');
- }
- if (empty($_this->config['path'])) {
- $_this->error('Не указан путь к файлу');
- return false;
- } elseif (!is_writable($_this->config['path'])) {
- $_this->error('Файл не доступен для записи');
- return false;
- }
- Db::getInstance()->q('SET wait_timeout = 600');
- $_this->info('Read file ' . $_this->config['path']);
- $f = fopen($_this->config['path'], 'a+');
- if (flock($f, LOCK_EX)) {
- $pos = filesize($_this->config['path']) - 1 * pow(1024, 2);
- if ($pos > 0) {
- fseek($f, $pos);
- $curpos = $pos + strlen(fgets($f, 4096));
- } else {
- $curpos = 0;
- }
- while (!feof($f)) {
- $row = fgets($f, 4096);
- if (preg_match(self::ROW_REGULAR, $row, $match)) {
- if (preg_match('#product/(.*?)\.html#', $match[3], $m)) {
- $url = $m[1];
- $prod_id = $_this->getProductIdByUrl($url);
- if ($prod_id > 0) {
- $_this->info('Find ' . $prod_id);
- $_this->insertClickAction(array(
- 'click_id' => $match[2],
- 'name' => 'products_id',
- 'value' => $prod_id
- ));
- $_this->upCount($match[2]);
- SmCheckSystem::checkpoint('add_click');
- }
- }
- }
- }
- if (empty($_GET['debug'])) {
- $_this->info('Clear file');
- ftruncate($f, $curpos);
- }
- flock($f, LOCK_UN);
- fclose($f);
- }
- /*$content = file_get_contents( $_this->config['path'] );
- if ( strlen($content) > 0 ) {
- $_this->success( 'Файл прочитан', 1);
- //$_this->clearFile();
- $_this->info( 'Парсим контент файла ' . $_this->config['path'] );
- if ( preg_match_all(self::ROW_REGULAR,$content,$matches) ) {
- $_this->success( 'Файл успешно отпарсен, найдено ' . count($matches[1]). ' результатов', 1);
- $results = array();
- foreach ( $matches[1] as $k=>$time ) {
- if ( preg_match('#product/(.*?)\.html#',$matches[3][$k], $match) ) {
- $url = $match[1];
- $prod_id = $_this->getProductIdByUrl($url);
- if ( $prod_id > 0 ) {
- $_this->insertClickAction(array(
- 'click_id' => $matches[2][$k],
- 'name' => 'products_id',
- 'value' => $prod_id
- ));
- }
- }
- if ( empty($results[$matches[2][$k]]) ) {
- $results[$matches[2][$k]]['count']=1;
- } else {
- $results[$matches[2][$k]]['count']++;
- }
- }
- SmCheckSystem::checkpoint('add_click');
- self::instance()->saveCounts($results);
- } else {
- $_this->error('Не найдено не одного совпадения', 1);
- }
- } else {
- $_this->debug('Файл пуст', 1);
- }*/
- }
- /**
- * Вставка действия
- * @param array $results
- * @return bool|resource
- */
- function insertClickAction($results = array())
- {
- return Db::getInstance()->q_create_insert(
- 'sotm_click_actions', $results, true
- );
- }
- /**
- * Получить id товара по url
- * @param string $url
- * @return mixed
- */
- function getProductIdByUrl($url = null)
- {
- return Db::getInstance()->q_result(sprintf(
- 'SELECT products_id FROM sotm_products WHERE url = %s',
- Db::quote($url)
- ));
- }
- /**
- * Clears file
- */
- function clearFile()
- {
- if (!DEBUG) {
- $this->info('Очищаем файл');
- $file = fopen($this->config['path'], 'a');
- ftruncate($file, 0);
- fclose($file);
- }
- }
- /**
- * Update count
- * @param $id
- * @return bool|resource
- */
- function upCount($id)
- {
- return Db::getInstance()->q(sprintf(
- 'UPDATE sotm_clicks SET num_pages = num_pages + 1 WHERE id = %d',
- $id
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement