1<?php
2/**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 */
8
9namespace Piwik\Plugins\CustomJsTracker\Commands;
10
11use Piwik\Container\StaticContainer;
12use Piwik\Plugin\ConsoleCommand;
13use Piwik\Plugins\CustomJsTracker\TrackerUpdater;
14use Piwik\Plugins\CustomJsTracker\TrackingCode\PluginTrackerFiles;
15use Symfony\Component\Console\Input\InputInterface;
16use Symfony\Component\Console\Input\InputOption;
17use Symfony\Component\Console\Output\OutputInterface;
18
19class UpdateTracker extends ConsoleCommand
20{
21    protected function configure()
22    {
23        $this->setName('custom-piwik-js:update');
24        $this->setAliases(array('custom-matomo-js:update'));
25        $this->addOption('source-file', null, InputOption::VALUE_REQUIRED, 'Absolute path to source PiwikJS file.', $this->getPathOriginalPiwikJs());
26        $this->addOption('target-file', null, InputOption::VALUE_REQUIRED, 'Absolute path to target file. Useful if your /matomo.js is not writable and you want to replace the file manually', PIWIK_DOCUMENT_ROOT . TrackerUpdater::TARGET_MATOMO_JS);
27        $this->addOption('ignore-minified', null, InputOption::VALUE_NONE, 'Ignore minified tracker files, useful during development so the original source file can be debugged');
28        $this->setDescription('Update the Javascript Tracker with plugin tracker additions');
29    }
30
31    private function getPathOriginalPiwikJs()
32    {
33        return PIWIK_DOCUMENT_ROOT . TrackerUpdater::ORIGINAL_PIWIK_JS;
34    }
35
36    protected function execute(InputInterface $input, OutputInterface $output)
37    {
38        $sourceFile = $input->getOption('source-file');
39        $targetFile = $input->getOption('target-file');
40        $ignoreMinified = (bool)$input->getOption('ignore-minified');
41
42        $this->updateTracker($sourceFile, $targetFile, $ignoreMinified);
43
44        $output->writeln('<info>The Javascript Tracker has been updated</info>');
45    }
46
47    public function updateTracker($sourceFile, $targetFile, $ignoreMinified)
48    {
49        $pluginTrackerFiles = StaticContainer::get('Piwik\Plugins\CustomJsTracker\TrackingCode\PluginTrackerFiles');
50
51        if ($ignoreMinified) {
52            if (empty($sourceFile) || $sourceFile === $this->getPathOriginalPiwikJs()) {
53                // no custom source file was requested
54                $sourceFile = PIWIK_DOCUMENT_ROOT . TrackerUpdater::DEVELOPMENT_PIWIK_JS;
55            }
56            $pluginTrackerFiles->ignoreMinified();
57        }
58
59        $updater = StaticContainer::getContainer()->make('Piwik\Plugins\CustomJsTracker\TrackerUpdater', array(
60            'fromFile' => $sourceFile, 'toFile' => $targetFile
61        ));
62        $updater->setTrackerFiles($pluginTrackerFiles);
63        $updater->checkWillSucceed();
64        $updater->update();
65    }
66}
67