Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Settings.php « FileSynchronizer « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37f2e5485e17f601beb364d5142b688ec0aaa4f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
 * Copyright (C) Piwik PRO - All rights reserved.
 *
 * Using this code requires that you first get a license from Piwik PRO.
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 *
 * @link http://piwik.pro
 */

namespace Piwik\Plugins\FileSynchronizer;

use Piwik\Common;
use Piwik\Settings\Setting;
use Piwik\Settings\SystemSetting;

/**
 * Defines Settings for FileSynchronizer.
 *
 * Usage like this:
 * $settings = new Settings('FileSynchronizer');
 * $settings->enabled->getValue();
 * $settings->sourceDirectory->getValue();
 */
class Settings extends \Piwik\Plugin\Settings
{
    /** @var SystemSetting */
    public $enabled;

    /** @var SystemSetting */
    public $sourceDirectory;

    /** @var SystemSetting */
    public $filePattern;

    /** @var SystemSetting */
    public $targetDirectory;

    /** @var SystemSetting */
    public $targetFilenameTemplate;

    /** @var SystemSetting */
    public $copyCommandTemplate;

    protected function init()
    {
        $this->createEnabledSetting();
        $this->createSourceDirectorySetting();
        $this->createFilePatternSetting();
        $this->createTargetDirectorySetting();
        $this->createTargetFilenameSetting();
        $this->createCopyTemplateSetting();
    }

    private function createEnabledSetting()
    {
        $this->enabled = new SystemSetting('enabled', 'Enabled');
        $this->enabled->type  = static::TYPE_BOOL;
        $this->enabled->uiControlType = static::CONTROL_CHECKBOX;
        $this->enabled->defaultValue  = false;

        $this->addSetting($this->enabled);
    }

    private function createSourceDirectorySetting()
    {
        $this->sourceDirectory = new SystemSetting('sourceDirectory', 'Source directory');
        $this->configureDirectorySetting($this->sourceDirectory, $checkDir = true);
        $this->sourceDirectory->description = 'Defines the source directory that contains the files that need to be synced.';

        $this->addSetting($this->sourceDirectory);
    }

    private function createFilePatternSetting()
    {
        $this->filePattern = new SystemSetting('filePattern', 'File Pattern');
        $this->filePattern->type  = static::TYPE_STRING;
        $this->filePattern->uiControlType = static::CONTROL_TEXT;
        $this->filePattern->defaultValue = '*';
        $this->filePattern->description = 'If specified, only files matching this pattern will be synced.';
        $this->filePattern->inlineHelp = 'Any shell wildcards can be specified, for example "*.log".';
        $this->filePattern->transform = function ($value) {
            if (!empty($value)) {
                return trim($value);
            }

            return '*';
        };

        $this->addSetting($this->filePattern);
    }

    private function createTargetDirectorySetting()
    {
        $this->targetDirectory = new SystemSetting('targetDirectory', 'Target directory');
        $this->configureDirectorySetting($this->targetDirectory, $checkDir = false);
        // we do not check dir as it might be on a remote computer
        $this->targetDirectory->description = 'Defines the target directory the files should be copied to.';

        $this->addSetting($this->targetDirectory);
    }

    private function createTargetFilenameSetting()
    {
        $this->targetFilenameTemplate = new SystemSetting('targetFilenameTemplate', 'Target filename template');
        $this->targetFilenameTemplate->type = static::TYPE_STRING;
        $this->targetFilenameTemplate->uiControlType = static::CONTROL_TEXT;
        $this->targetFilenameTemplate->defaultValue = '$basename';
        $this->targetFilenameTemplate->description = 'Allows to modify the filename. "$basename" or "$filename" must be specified, "$extension" can be specified optionally.';
        $this->targetFilenameTemplate->inlineHelp = '"$basename" will be replaced with the filename including extension (eg "apache.log"), "$filename" will be replaced by the filename excluding extension (eg "apache") and "$extension" will be replaced by the file extension (eg "log"). This allows to modify the filename in the target directory for example like this: "$filename_idsite_1.$extension"';

        $self = $this;
        $this->targetFilenameTemplate->validate = function ($value, $setting) use ($self) {
            if (empty($value) && !$self->enabled->getValue()) {
                // it is not enabled, an empty value is okay.
                return;
            }

            if (empty($value)) {
                throw new \Exception('A value must be specified');
            }

            if (false === strpos($value, '$filename') && false === strpos($value, '$basename')) {
                throw new \Exception('$filename or $basename must be specified');
            }
        };
        $this->targetFilenameTemplate->transform = function ($value) {
            if (!empty($value)) {
                return trim($value);
            }

            return $value;
        };

        $this->addSetting($this->targetFilenameTemplate);
    }

    private function configureDirectorySetting(Setting $setting, $checkDir)
    {
        $setting->type  = static::TYPE_STRING;
        $setting->uiControlType = static::CONTROL_TEXT;

        $self = $this;
        $setting->validate = function ($value, $setting) use ($self, $checkDir) {
            if (empty($value) && !$self->enabled->getValue()) {
                // it is not enabled, an empty value is okay.
                return;
            }
            if (empty($value)) {
                throw new \Exception('A value must be specified');
            }
            if ($checkDir && !file_exists($value)) {
                throw new \Exception('This path does not exist');
            }
            if ($checkDir && !is_dir($value)) {
                throw new \Exception('Path is not a directory');
            }
            if ($checkDir && !is_readable($value)) {
                throw new \Exception('This directory is not readable');
            }
        };
        $setting->transform = function ($value) {
            if (!empty($value) && Common::stringEndsWith($value, '/')) {
                $value = substr($value, 0, -1);
            }

            return $value;
        };
    }

    private function createCopyTemplateSetting()
    {
        $this->copyCommandTemplate = new SystemSetting('copyCommandTemplate', 'Copy Command Template');
        $this->copyCommandTemplate->type = static::TYPE_STRING;
        $this->copyCommandTemplate->uiControlType = static::CONTROL_TEXT;
        $this->copyCommandTemplate->description = 'The shell command to sync files. "$source" and "$target" must be specified.';
        $this->copyCommandTemplate->inlineHelp = '"$source" will be replaced by the path of the source file, "$target" by the path to the target file.';
        $this->copyCommandTemplate->defaultValue = 'cp $source $target';

        $self = $this;
        $this->copyCommandTemplate->validate = function ($value, $setting) use ($self) {
            if (empty($value) && !$self->enabled->getValue()) {
                // it is not enabled, an empty value is okay.
                return;
            }

            if (empty($value)) {
                throw new \Exception('A value must be specified');
            }
            if (false === strpos($value, '$source')) {
                throw new \Exception('$source is not specified');
            }
            if (false === strpos($value, '$target')) {
                throw new \Exception('$target is not specified');
            }
        };
        $this->copyCommandTemplate->transform = function ($value) {
            if (!empty($value)) {
                return trim($value);
            }

            return $value;
        };

        $this->addSetting($this->copyCommandTemplate);
    }
}