Completed
Push — master ( 9b0850...10a492 )
by James
11:15 queued 06:11
created

LogfileCommand::getPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @package    Command
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Command;
32
33
use BrowscapPHP\Browscap;
34
use BrowscapPHP\Cache\BrowscapCache;
35
use BrowscapPHP\Cache\BrowscapCacheInterface;
36
use BrowscapPHP\Exception\InvalidArgumentException;
37
use BrowscapPHP\Exception\ReaderException;
38
use BrowscapPHP\Exception\UnknownBrowserException;
39
use BrowscapPHP\Exception\UnknownBrowserTypeException;
40
use BrowscapPHP\Exception\UnknownDeviceException;
41
use BrowscapPHP\Exception\UnknownEngineException;
42
use BrowscapPHP\Exception\UnknownPlatformException;
43
use BrowscapPHP\Helper\Filesystem;
44
use BrowscapPHP\Helper\IniLoader;
45
use BrowscapPHP\Helper\LoggerHelper;
46
use BrowscapPHP\Util\Logfile\ReaderCollection;
47
use BrowscapPHP\Util\Logfile\ReaderFactory;
48
use Symfony\Component\Console\Command\Command;
49
use Symfony\Component\Console\Input\InputArgument;
50
use Symfony\Component\Console\Input\InputInterface;
51
use Symfony\Component\Console\Input\InputOption;
52
use Symfony\Component\Console\Output\OutputInterface;
53
use Symfony\Component\Filesystem\Exception\IOException;
54
use Symfony\Component\Finder\Finder;
55
use Symfony\Component\Finder\SplFileInfo;
56
use WurflCache\Adapter\File;
57
58
/**
59
 * commands to parse a log file and parse the useragents in it
60
 *
61
 * @category   Browscap-PHP
62
 * @package    Command
63
 * @author     Dave Olsen, http://dmolsen.com
64
 * @author     Thomas Müller <[email protected]>
65
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
66
 * @version    3.0
67
 * @license    http://www.opensource.org/licenses/MIT MIT License
68
 * @link       https://github.com/browscap/browscap-php/
69
 */
70
class LogfileCommand extends Command
71
{
72
    /**
73
     * @var array
74
     */
75
    private $undefinedClients = array();
76
77
    private $uas         = array();
78
    private $uasWithType = array();
79
80
    private $countOk = 0;
81
    private $countNok = 0;
82
    private $totalCount = 0;
83
84
    /**
85
     * @var BrowscapCacheInterface
86
     */
87
    private $cache = null;
88
89
    /**
90
     * @var string
91
     */
92
    private $defaultCacheFolder;
93
94
    /**
95
     * @param string $defaultCacheFolder
96
     */
97 1
    public function __construct($defaultCacheFolder)
98
    {
99 1
        parent::__construct();
100
101 1
        $this->defaultCacheFolder = $defaultCacheFolder;
102 1
    }
103
104
    /**
105
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
106
     *
107
     * @return $this
108
     */
109 1
    public function setCache(BrowscapCacheInterface $cache)
110
    {
111 1
        $this->cache = $cache;
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * Configures the current command.
118
     */
119 1
    protected function configure()
120
    {
121 1
        $this
122 1
            ->setName('browscap:log')
123 1
            ->setDescription('Parses the supplied webserver log file.')
124 1
            ->addArgument(
125 1
                'output',
126 1
                InputArgument::REQUIRED,
127 1
                'Path to output log file',
128
                null
129 1
            )
130 1
            ->addOption(
131 1
                'log-file',
132 1
                'f',
133 1
                InputOption::VALUE_REQUIRED,
134
                'Path to a webserver log file'
135 1
            )
136 1
            ->addOption(
137 1
                'log-dir',
138 1
                'd',
139 1
                InputOption::VALUE_REQUIRED,
140
                'Path to webserver log directory'
141 1
            )
142 1
            ->addOption(
143 1
                'include',
144 1
                'i',
145 1
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
146 1
                'Include glob expressions for log files in the log directory',
147 1
                array('*.log', '*.log*.gz', '*.log*.bz2')
148 1
            )
149 1
            ->addOption(
150 1
                'exclude',
151 1
                'e',
152 1
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
153 1
                'Exclude glob expressions for log files in the log directory',
154 1
                array('*error*')
155 1
            )
156 1
            ->addOption(
157 1
                'debug',
158 1
                null,
159 1
                InputOption::VALUE_NONE,
160
                'Should the debug mode entered?'
161 1
            )
162 1
            ->addOption(
163 1
                'cache',
164 1
                'c',
165 1
                InputOption::VALUE_REQUIRED,
166 1
                'Where the cache files are located',
167 1
                $this->defaultCacheFolder
168 1
            )
169
        ;
170 1
    }
171
172
    /**
173
     * @param InputInterface  $input
174
     * @param OutputInterface $output
175
     *
176
     * @throws \UnexpectedValueException
177
     * @throws \BrowscapPHP\Exception\InvalidArgumentException
178
     * @return int|null|void
179
     */
180
    protected function execute(InputInterface $input, OutputInterface $output)
181
    {
182
        if (!$input->getOption('log-file') && !$input->getOption('log-dir')) {
183
            throw InvalidArgumentException::oneOfCommandArguments('log-file', 'log-dir');
184
        }
185
186
        $loggerHelper = new LoggerHelper();
187
        $logger       = $loggerHelper->create($input->getOption('debug'));
188
189
        $browscap   = $this->getBrowscap();
190
        $loader     = new IniLoader();
191
        $collection = ReaderFactory::factory();
192
        $fs         = new Filesystem();
193
194
        $browscap
195
            ->setLogger($logger)
196
            ->setCache($this->getCache($input))
197
        ;
198
199
        /** @var $file \Symfony\Component\Finder\SplFileInfo */
200
        foreach ($this->getFiles($input) as $file) {
201
            $this->uas = array();
202
            $path      = $this->getPath($file);
203
204
            $loader->setLocalFile($path);
205
            $internalLoader = $loader->getLoader();
206
207
            $this->countOk  = 0;
208
            $this->countNok = 0;
209
210
            $logger->info('Analyzing file "'.$file->getPathname().'"');
211
212
            if ($internalLoader->isSupportingLoadingLines()) {
213
                if (!$internalLoader->init($path)) {
214
                    $logger->info('Skipping empty file "'.$file->getPathname().'"');
215
                    continue;
216
                }
217
218
                $this->totalCount = 1;
219
220
                while ($internalLoader->isValid()) {
221
                    $this->handleLine(
222
                        $output,
223
                        $collection,
224
                        $browscap,
225
                        $internalLoader->getLine()
226
                    );
227
228
                    $this->totalCount++;
229
                }
230
231
                $internalLoader->close();
232
                $this->totalCount--;
233
            } else {
234
                $lines = file($path);
235
236
                if (empty($lines)) {
237
                    $logger->info('Skipping empty file "'.$file->getPathname().'"');
238
                    continue;
239
                }
240
241
                $this->totalCount = count($lines);
242
243
                foreach ($lines as $line) {
244
                    $this->handleLine(
245
                        $output,
246
                        $collection,
247
                        $browscap,
248
                        $line
249
                    );
250
                }
251
            }
252
253
            $this->outputProgress($output, '', true);
254
255
            arsort($this->uas, SORT_NUMERIC);
256
257
            try {
258
                $fs->dumpFile($input->getArgument('output').'/output.sql', $this->createSqlContent());
259
            } catch (IOException $e) {
260
                // do nothing
261
            }
262
263
            try {
264
                $fs->dumpFile(
265
                    $input->getArgument('output').'/output.txt',
266
                    implode(PHP_EOL, array_unique($this->undefinedClients))
267
                );
268
            } catch (IOException $e) {
269
                // do nothing
270
            }
271
272
            try {
273
                $fs->dumpFile(
274
                    $input->getArgument('output').'/output-with-amount.txt',
275
                    $this->createAmountContent()
276
                );
277
            } catch (IOException $e) {
278
                // do nothing
279
            }
280
281
            try {
282
                $fs->dumpFile(
283
                    $input->getArgument('output').'/output-with-amount-and-type.txt',
284
                    $this->createAmountTypeContent()
285
                );
286
            } catch (IOException $e) {
287
                // do nothing
288
            }
289
        }
290
291
        try {
292
            $fs->dumpFile($input->getArgument('output').'/output.sql', $this->createSqlContent());
293
        } catch (IOException $e) {
294
            // do nothing
295
        }
296
297
        $outputFile = $input->getArgument('output').'/output.txt';
298
299
        try {
300
            $fs->dumpFile(
301
                $outputFile,
302
                implode(PHP_EOL, array_unique($this->undefinedClients))
303
            );
304
        } catch (IOException $e) {
305
            throw new \UnexpectedValueException('writing to file "'.$outputFile.'" failed', 0, $e);
306
        }
307
308
        try {
309
            $fs->dumpFile(
310
                $input->getArgument('output').'/output-with-amount.txt',
311
                $this->createAmountContent()
312
            );
313
        } catch (IOException $e) {
314
            // do nothing
315
        }
316
317
        try {
318
            $fs->dumpFile(
319
                $input->getArgument('output').'/output-with-amount-and-type.txt',
320
                $this->createAmountTypeContent()
321
            );
322
        } catch (IOException $e) {
323
            // do nothing
324
        }
325
    }
326
327
    private function createSqlContent()
328
    {
329
        $content = '';
330
331
        arsort($this->uas, SORT_NUMERIC);
332
333
        foreach ($this->uas as $agentOfLine => $count) {
334
            $content .= "
335
                INSERT INTO `agents` (`agent`, `count`)
336
                VALUES ('".addslashes($agentOfLine)."', ".addslashes($count).")
337
                ON DUPLICATE KEY UPDATE `count`=`count`+".addslashes($count).";
338
            ";
339
        }
340
341
        return $content;
342
    }
343
344
    private function createAmountContent()
345
    {
346
        $counts = array();
347
348
        foreach ($this->uasWithType as $uas) {
349
            foreach ($uas as $userAgentString => $count) {
350
                if (isset($counts[$userAgentString])) {
351
                    $counts[$userAgentString] += $count;
352
                } else {
353
                    $counts[$userAgentString] = $count;
354
                }
355
            }
356
        }
357
358
        $content = '';
359
360
        arsort($counts, SORT_NUMERIC);
361
362
        foreach ($counts as $agentOfLine => $count) {
363
            $content .= "$count\t$agentOfLine\n";
364
        }
365
366
        return $content;
367
    }
368
369
    private function createAmountTypeContent()
370
    {
371
        $content = '';
372
        $types   = array('B', 'T', 'P', 'D', 'N', 'U');
373
374
        foreach ($types as $type) {
375
            if (!isset($this->uasWithType[$type])) {
376
                continue;
377
            }
378
379
            arsort($this->uasWithType[$type], SORT_NUMERIC);
380
381
            foreach ($this->uasWithType[$type] as $agentOfLine => $count) {
382
                $content .= "$type\t$count\t$agentOfLine\n";
383
            }
384
        }
385
386
        return $content;
387
    }
388
389
    /**
390
     * @param \Symfony\Component\Console\Output\OutputInterface $output
391
     * @param \BrowscapPHP\Util\Logfile\ReaderCollection        $collection
392
     * @param \BrowscapPHP\Browscap                             $browscap
393
     * @param integer                                           $line
394
     *
395
     * @throws UnknownBrowserException
396
     * @throws UnknownBrowserTypeException
397
     * @throws UnknownDeviceException
398
     * @throws UnknownEngineException
399
     * @throws UnknownPlatformException
400
     * @throws \Exception
401
     */
402
    private function handleLine(OutputInterface $output, ReaderCollection $collection, Browscap $browscap, $line)
403
    {
404
        $userAgentString = '';
405
406
        try {
407
            $userAgentString = $collection->read($line);
408
409
            try {
410
                $this->getResult($browscap->getBrowser($userAgentString));
411
            } catch (\Exception $e) {
412
                $this->undefinedClients[] = $userAgentString;
413
414
                throw $e;
415
            }
416
417
            $type = '.';
418
            $this->countOk++;
419
        } catch (ReaderException $e) {
420
            $type = 'E';
421
            $this->countNok++;
422
        } catch (UnknownBrowserTypeException $e) {
423
            $type = 'T';
424
            $this->countNok++;
425
        } catch (UnknownBrowserException $e) {
426
            $type = 'B';
427
            $this->countNok++;
428
        } catch (UnknownPlatformException $e) {
429
            $type = 'P';
430
            $this->countNok++;
431
        } catch (UnknownDeviceException $e) {
432
            $type = 'D';
433
            $this->countNok++;
434
        } catch (UnknownEngineException $e) {
435
            $type = 'N';
436
            $this->countNok++;
437
        } catch (\Exception $e) {
438
            $type = 'U';
439
            $this->countNok++;
440
        }
441
442
        $this->outputProgress($output, $type);
443
444
        // count all useragents
445
        if (isset($this->uas[$userAgentString])) {
446
            $this->uas[$userAgentString]++;
447
        } else {
448
            $this->uas[$userAgentString] = 1;
449
        }
450
451
        if ('.' !== $type && 'E' !== $type) {
452
            // count all undetected useragents grouped by detection error
453
            if (!isset($this->uasWithType[$type])) {
454
                $this->uasWithType[$type] = array();
455
            }
456
457
            if (isset($this->uasWithType[$type][$userAgentString])) {
458
                $this->uasWithType[$type][$userAgentString]++;
459
            } else {
460
                $this->uasWithType[$type][$userAgentString] = 1;
461
            }
462
        }
463
    }
464
465
    /**
466
     * @param \Symfony\Component\Console\Output\OutputInterface $output
467
     * @param string                                            $result
468
     * @param bool                                              $end
469
     *
470
     * @return int
471
     */
472
    private function outputProgress(OutputInterface $output, $result, $end = false)
473
    {
474
        if (($this->totalCount % 70) === 0 || $end) {
475
            $formatString = '  %'.strlen($this->countOk).'d OK, %'.strlen($this->countNok).'d NOK, Summary %'
476
                .strlen($this->totalCount).'d';
477
478
            if ($end) {
479
                $result = str_pad($result, 70 - ($this->totalCount % 70), ' ', STR_PAD_RIGHT);
480
            }
481
482
            $endString = sprintf($formatString, $this->countOk, $this->countNok, $this->totalCount);
483
484
            $output->writeln($result.$endString);
485
486
            return;
487
        }
488
489
        $output->write($result);
490
    }
491
492
    /**
493
     * @param \stdClass $result
494
     *
495
     * @return string
496
     */
497
    private function getResult(\stdClass $result)
498
    {
499
        if ('Default Browser' === $result->browser) {
500
            throw new UnknownBrowserException('unknwon browser found');
501
        }
502
503
        if ('unknown' === $result->browser_type) {
504
            throw new UnknownBrowserTypeException('unknwon browser type found');
505
        }
506
507
        if (in_array($result->browser_type, array('Bot/Crawler', 'Library'))) {
508
            return '.';
509
        }
510
511
        if ('unknown' === $result->platform) {
512
            throw new UnknownPlatformException('unknown platform found');
513
        }
514
515
        if ('unknown' === $result->device_type) {
516
            throw new UnknownDeviceException('unknwon device type found');
517
        }
518
519
        if ('unknown' === $result->renderingengine_name) {
520
            throw new UnknownEngineException('unknown rendering engine found');
521
        }
522
523
        return '.';
524
    }
525
526
    /**
527
     * @param \Symfony\Component\Console\Input\InputInterface $input
528
     *
529
     * @return \Symfony\Component\Finder\Finder
530
     */
531
    private function getFiles(InputInterface $input)
532
    {
533
        $finder = Finder::create();
534
535
        if ($input->getOption('log-file')) {
536
            $file = $input->getOption('log-file');
537
            $finder->append(Finder::create()->in(dirname($file))->name(basename($file)));
538
        }
539
540
        if ($input->getOption('log-dir')) {
541
            $dirFinder = Finder::create()
542
                ->in($input->getOption('log-dir'));
543
            array_map(array($dirFinder, 'name'), $input->getOption('include'));
544
            array_map(array($dirFinder, 'notName'), $input->getOption('exclude'));
545
546
            $finder->append($dirFinder);
547
        }
548
549
        return $finder;
550
    }
551
552
    /**
553
     * @param \Symfony\Component\Finder\SplFileInfo $file
554
     *
555
     * @return string
556
     */
557
    private function getPath(SplFileInfo $file)
558
    {
559
        switch ($file->getExtension()) {
560
            case 'gz':
561
                $path = 'compress.zlib://'.$file->getPathname();
562
                break;
563
            case 'bz2':
564
                $path = 'compress.bzip2://'.$file->getPathname();
565
                break;
566
            default:
567
                $path = $file->getPathname();
568
                break;
569
        }
570
571
        return $path;
572
    }
573
574
    private function getBrowscap()
575
    {
576
        return new Browscap();
577
    }
578
579
    /**
580
     * @param InputInterface $input
581
     *
582
     * @return BrowscapCacheInterface
583
     */
584 View Code Duplication
    private function getCache(InputInterface $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
585
    {
586
        if (null === $this->cache) {
587
            $cacheAdapter = new File(array(File::DIR => $input->getOption('cache')));
588
            $this->cache  = new BrowscapCache($cacheAdapter);
589
        }
590
591
        return $this->cache;
592
    }
593
}
594