| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CustomJsTracker; |
|
|
| use Piwik\Common; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Plugins\CustomJsTracker\TrackingCode\PiwikJsManipulator; |
| use Piwik\Plugins\CustomJsTracker\TrackingCode\PluginTrackerFiles; |
| use Piwik\Piwik; |
|
|
| |
| |
| |
| |
| |
| |
| class TrackerUpdater |
| { |
| public const DEVELOPMENT_PIWIK_JS = '/js/piwik.js'; |
| public const ORIGINAL_PIWIK_JS = '/js/piwik.min.js'; |
| public const TARGET_MATOMO_JS = '/matomo.js'; |
|
|
| |
| |
| |
| private $fromFile; |
|
|
| |
| |
| |
| private $toFile; |
|
|
| private $trackerFiles = array(); |
|
|
| |
| |
| |
| |
| public function __construct($fromFile = null, $toFile = null) |
| { |
| if (!isset($fromFile)) { |
| $fromFile = PIWIK_DOCUMENT_ROOT . self::ORIGINAL_PIWIK_JS; |
| } |
|
|
| if (!isset($toFile)) { |
| $toFile = PIWIK_DOCUMENT_ROOT . self::TARGET_MATOMO_JS; |
| } |
|
|
| $this->setFromFile($fromFile); |
| $this->setToFile($toFile); |
| $this->trackerFiles = StaticContainer::get('Piwik\Plugins\CustomJsTracker\TrackingCode\PluginTrackerFiles'); |
| } |
|
|
| public function setFromFile($fromFile) |
| { |
| if (is_string($fromFile)) { |
| $fromFile = new File($fromFile); |
| } |
| $this->fromFile = $fromFile; |
| } |
|
|
| public function getFromFile() |
| { |
| return $this->fromFile; |
| } |
|
|
| public function setToFile($toFile) |
| { |
| if (is_string($toFile)) { |
| $toFile = new File($toFile); |
| } |
| $this->toFile = $toFile; |
| } |
|
|
| public function getToFile() |
| { |
| return $this->toFile; |
| } |
|
|
| public function setTrackerFiles(PluginTrackerFiles $trackerFiles) |
| { |
| $this->trackerFiles = $trackerFiles; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function checkWillSucceed() |
| { |
| $this->fromFile->checkReadable(); |
| $this->toFile->checkWritable(); |
| } |
|
|
| public function getCurrentTrackerFileContent() |
| { |
| return $this->toFile->getContent(); |
| } |
|
|
| public function getUpdatedTrackerFileContent() |
| { |
| $trackingCode = new PiwikJsManipulator($this->fromFile->getContent(), $this->trackerFiles); |
| $newContent = $trackingCode->manipulateContent(); |
|
|
| return $newContent; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function update() |
| { |
| if (!$this->toFile->hasWriteAccess() || !$this->fromFile->hasReadAccess()) { |
| return; |
| } |
|
|
| $newContent = $this->getUpdatedTrackerFileContent(); |
|
|
| if (!$this->toFile->isFileContentSame($newContent)) { |
| $savedFiles = $this->toFile->save($newContent); |
| foreach ($savedFiles as $savedFile) { |
|
|
| |
| |
| |
| |
| |
| Piwik::postEvent('CustomJsTracker.trackerJsChanged', [$savedFile]); |
| } |
| } |
|
|
| |
| $this->updateAlternative('piwik.js', 'matomo.js', $newContent); |
| $this->updateAlternative('matomo.js', 'piwik.js', $newContent); |
| } |
|
|
| private function updateAlternative($fromFile, $toFile, $newContent) |
| { |
| if (Common::stringEndsWith($this->toFile->getName(), $fromFile)) { |
| $alternativeFilename = dirname($this->toFile->getPath()) . DIRECTORY_SEPARATOR . $toFile; |
| $file = $this->toFile->setFile($alternativeFilename); |
| if ($file->hasWriteAccess() && !$file->isFileContentSame($newContent)) { |
| $savedFiles = $file->save($newContent); |
| foreach ($savedFiles as $savedFile) { |
| Piwik::postEvent('CustomJsTracker.trackerJsChanged', [$savedFile]); |
| } |
| } |
| } |
| } |
| } |
|
|