Advertisement
miholeus

LogParser

Feb 6th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.75 KB | None | 0 0
  1. <?php
  2.  
  3. require_once PROJECT_ROOT . "/classes/utils/SmCheckSystem.class.php";
  4.  
  5. /**
  6.  * Log parser - parses log file and saves result to DB
  7.  * @author Chirkin Ivan
  8.  * @package Raznoe
  9.  * @uses DbSimple
  10.  * @since 2010-06-29
  11.  */
  12. class Raznoe_LogParser extends Object__Parser2
  13. {
  14.     /**
  15.      * Config
  16.      * @var array
  17.      */
  18.     protected $config = array(
  19.         'path' => ''
  20.     );
  21.     /**
  22.      * Instance of class
  23.      * @var Raznoe_LogParser
  24.      */
  25.     protected static $instance = null;
  26.     /**
  27.      * Regular for log row
  28.      * @var unknown_type
  29.      */
  30.     const ROW_REGULAR = '#\[(.*?)\] "GET /s.gif\?([0-9]+).*?HTTP/1.1"[^"]*?"(.*?)"#i';
  31.  
  32.     /**
  33.      * Returns instance
  34.      */
  35.     static function instance($config = array())
  36.     {
  37.         if (!empty(self::$instance)) {
  38.             return self::$instance;
  39.         } else {
  40.             // Inits class
  41.             return self::$instance = new self($config);
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Constructor
  47.      * @param array $config
  48.      */
  49.     function __construct($config = array())
  50.     {
  51.         if (is_array($config)) {
  52.             $this->config = array_merge(
  53.                 $this->config, $config
  54.             );
  55.         }
  56.         parent::__construct();
  57.     }
  58.  
  59.     /**
  60.      * Set path of log file
  61.      * @param unknown_type $path
  62.      */
  63.     public static function setPath($path = null)
  64.     {
  65.         self::instance()->config['path'] = $path;
  66.     }
  67.  
  68.     /**
  69.      * Parse log file
  70.      */
  71.     public static function parse()
  72.     {
  73.         Sprav::getSpravType('click_settings')->emptyCache();
  74.         $is_disabled = Sprav::getName('click_settings', 'is_disabled');
  75.         $_this = self::instance();
  76.         if ($is_disabled == 1) {
  77.             $_this->debug('Парсинг отключен (click_settings.is_disabled=1)');
  78.             exit();
  79.         }
  80.         if (DEBUG) {
  81.             $_this->debug('DEBUG режим');
  82.         }
  83.         if (empty($_this->config['path'])) {
  84.             $_this->error('Не указан путь к файлу');
  85.             return false;
  86.         } elseif (!is_writable($_this->config['path'])) {
  87.             $_this->error('Файл не доступен для записи');
  88.             return false;
  89.         }
  90.         Db::getInstance()->q('SET wait_timeout = 600');
  91.         $_this->info('Read file ' . $_this->config['path']);
  92.  
  93.         $f = fopen($_this->config['path'], 'a+');
  94.         if (flock($f, LOCK_EX)) {
  95.             $pos = filesize($_this->config['path']) - 1 * pow(1024, 2);
  96.             if ($pos > 0) {
  97.                 fseek($f, $pos);
  98.                 $curpos = $pos + strlen(fgets($f, 4096));
  99.             } else {
  100.                 $curpos = 0;
  101.             }
  102.             while (!feof($f)) {
  103.                 $row = fgets($f, 4096);
  104.                 if (preg_match(self::ROW_REGULAR, $row, $match)) {
  105.                     if (preg_match('#product/(.*?)\.html#', $match[3], $m)) {
  106.                         $url = $m[1];
  107.                         $prod_id = $_this->getProductIdByUrl($url);
  108.                         if ($prod_id > 0) {
  109.                             $_this->info('Find ' . $prod_id);
  110.                             $_this->insertClickAction(array(
  111.                                 'click_id' => $match[2],
  112.                                 'name' => 'products_id',
  113.                                 'value' => $prod_id
  114.                             ));
  115.                             $_this->upCount($match[2]);
  116.                             SmCheckSystem::checkpoint('add_click');
  117.                         }
  118.                     }
  119.  
  120.                 }
  121.             }
  122.  
  123.             if (empty($_GET['debug'])) {
  124.                 $_this->info('Clear file');
  125.                 ftruncate($f, $curpos);
  126.             }
  127.             flock($f, LOCK_UN);
  128.             fclose($f);
  129.         }
  130.  
  131.         /*$content = file_get_contents( $_this->config['path'] );
  132.         if ( strlen($content) > 0 ) {
  133.             $_this->success( 'Файл прочитан', 1);
  134.             //$_this->clearFile();
  135.             $_this->info( 'Парсим контент файла ' . $_this->config['path'] );
  136.  
  137.             if ( preg_match_all(self::ROW_REGULAR,$content,$matches) ) {
  138.                 $_this->success( 'Файл успешно отпарсен, найдено ' . count($matches[1]). ' результатов', 1);
  139.                 $results = array();
  140.                 foreach ( $matches[1] as $k=>$time ) {
  141.                     if ( preg_match('#product/(.*?)\.html#',$matches[3][$k], $match) ) {
  142.                         $url = $match[1];
  143.                         $prod_id = $_this->getProductIdByUrl($url);
  144.                         if ( $prod_id > 0 ) {
  145.                             $_this->insertClickAction(array(
  146.                                 'click_id' => $matches[2][$k],
  147.                                 'name' => 'products_id',
  148.                                 'value' => $prod_id
  149.                             ));
  150.                         }
  151.                     }
  152.                    
  153.                     if ( empty($results[$matches[2][$k]]) ) {
  154.                         $results[$matches[2][$k]]['count']=1;
  155.                     } else {
  156.                         $results[$matches[2][$k]]['count']++;
  157.                     }
  158.                 }
  159.                 SmCheckSystem::checkpoint('add_click');
  160.                 self::instance()->saveCounts($results);
  161.             } else {
  162.                 $_this->error('Не найдено не одного совпадения', 1);
  163.             }
  164.         } else {
  165.             $_this->debug('Файл пуст', 1);
  166.         }*/
  167.     }
  168.  
  169.     /**
  170.      * Вставка действия
  171.      * @param array $results
  172.      * @return bool|resource
  173.      */
  174.     function insertClickAction($results = array())
  175.     {
  176.         return Db::getInstance()->q_create_insert(
  177.             'sotm_click_actions', $results, true
  178.         );
  179.     }
  180.  
  181.     /**
  182.      * Получить id товара по url
  183.      * @param string $url
  184.      * @return mixed
  185.      */
  186.     function getProductIdByUrl($url = null)
  187.     {
  188.         return Db::getInstance()->q_result(sprintf(
  189.             'SELECT products_id FROM sotm_products WHERE url = %s',
  190.             Db::quote($url)
  191.         ));
  192.     }
  193.  
  194.     /**
  195.      * Clears file
  196.      */
  197.     function clearFile()
  198.     {
  199.         if (!DEBUG) {
  200.             $this->info('Очищаем файл');
  201.             $file = fopen($this->config['path'], 'a');
  202.             ftruncate($file, 0);
  203.             fclose($file);
  204.         }
  205.     }
  206.  
  207.     /**
  208.      * Update count
  209.      * @param $id
  210.      * @return bool|resource
  211.      */
  212.     function upCount($id)
  213.     {
  214.         return Db::getInstance()->q(sprintf(
  215.             'UPDATE sotm_clicks SET num_pages = num_pages + 1 WHERE id = %d',
  216.             $id
  217.         ));
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement