File: GenerateDimension.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (265 lines) | stat: -rw-r--r-- 9,530 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CoreConsole\Commands;

use Piwik\Common;
use Piwik\DbHelper;
use Piwik\Plugin\Manager;

class GenerateDimension extends GeneratePluginBase
{
    protected function configure()
    {
        $this->setName('generate:dimension')
            ->setDescription('Adds a new dimension to an existing plugin. This allows you to persist new values during tracking.')
            ->addRequiredValueOption('pluginname', null, 'The name of an existing plugin which does not have a menu defined yet')
            ->addRequiredValueOption('type', null, 'Whether you want to create a "Visit", an "Action" or a "Conversion" dimension')
            ->addRequiredValueOption('dimensionname', null, 'A human readable name of the dimension which will be for instance visible in the UI')
            ->addRequiredValueOption('columnname', null, 'The name of the column in the MySQL database the dimension will be stored under')
            ->addRequiredValueOption('columntype', null, 'The MySQL type for your dimension, for instance "VARCHAR(255) NOT NULL".');
    }

    /**
     * @throws \InvalidArgumentException
     */
    protected function doExecute(): int
    {
        $pluginName = $this->getPluginName();
        $this->checkAndUpdateRequiredPiwikVersion($pluginName);

        $type          = $this->getDimensionType();
        $dimensionName = $this->getDimensionName();

        if ('non-tracking-dimension' === $type) {
            $columnName = '';
            $columType  = '';
        } else {
            $columnName = $this->getColumnName($type);
            $columType  = $this->getColumnType();
        }

        $dimensionClassName      = $this->getDimensionClassName($dimensionName);
        $translatedDimensionName = $this->makeTranslationIfPossible($pluginName, ucfirst($dimensionName));

        $exampleFolder = Manager::getPluginDirectory('ExampleTracker');
        $replace       = array('example_action_dimension'  => strtolower($columnName),
                               'example_visit_dimension'   => strtolower($columnName),
                               'example_conversion_dimension'   => strtolower($columnName),
                               'INTEGER(11) DEFAULT 0 NULL' => strtoupper($columType),
                               'VARCHAR(255) DEFAULT NULL'      => strtoupper($columType),
                               'ExampleDimension'       => $dimensionClassName,
                               'ExampleVisitDimension'  => $dimensionClassName,
                               'ExampleActionDimension' => $dimensionClassName,
                               'ExampleConversionDimension'  => $dimensionClassName,
                               'ExampleTracker_DimensionName' => $translatedDimensionName,
                               'ExampleTracker' => $pluginName,
        );

        $whitelistFiles = array('/Columns');

        if ('visit' == $type) {
            $whitelistFiles[] = '/Columns/ExampleVisitDimension.php';
        } elseif ('action' == $type) {
            $whitelistFiles[] = '/Columns/ExampleActionDimension.php';
        } elseif ('conversion' == $type) {
            $whitelistFiles[] = '/Columns/ExampleConversionDimension.php';
        } elseif ('non-tracking-dimension' == $type) {
            $whitelistFiles[] = '/Columns/ExampleDimension.php';
        } else {
            throw new \InvalidArgumentException('This dimension type is not available');
        }

        $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);

        $this->writeSuccessMessage(array(
            sprintf('Columns/%s.php for %s generated.', ucfirst($dimensionClassName), $pluginName),
            'You should now implement the events within this file',
            'Enjoy!',
        ));

        return self::SUCCESS;
    }

    private function getDimensionClassName($dimensionName)
    {
        $dimensionName = trim($dimensionName);
        $dimensionName = str_replace(' ', '', $dimensionName);
        $dimensionName = preg_replace("/[^A-Za-z0-9]/", '', $dimensionName);

        $dimensionName = ucfirst($dimensionName);

        return $dimensionName;
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getDimensionName()
    {
        $input = $this->getInput();

        $validate = function ($dimensionName) {
            if (empty($dimensionName)) {
                throw new \InvalidArgumentException('Please enter the name of your dimension');
            }

            if (preg_match("/[^A-Za-z0-9 ]/", $dimensionName)) {
                throw new \InvalidArgumentException('Only alpha numerical characters and whitespaces are allowed');
            }

            return $dimensionName;
        };

        $dimensionName = $input->getOption('dimensionname');

        if (empty($dimensionName)) {
            $dimensionName = $this->askAndValidate(
                'Enter a human readable name of your dimension, for instance "Browser": ',
                $validate
            );
        } else {
            $validate($dimensionName);
        }

        $dimensionName = ucfirst($dimensionName);

        return $dimensionName;
    }

    /**
     * @param string $type
     * @return string
     * @throws \RuntimeException
     */
    protected function getColumnName($type)
    {
        $input = $this->getInput();

        $validate = function ($columnName) use ($type) {
            if (empty($columnName)) {
                throw new \InvalidArgumentException('Please enter the name of the dimension column');
            }

            if (preg_match("/[^A-Za-z0-9_ ]/", $columnName)) {
                throw new \InvalidArgumentException('Only alpha numerical characters, underscores and whitespaces are allowed');
            }

            if ('visit' == $type) {
                $columns = array_keys(DbHelper::getTableColumns(Common::prefixTable('log_visit')));
            } elseif ('action' == $type) {
                $columns = array_keys(DbHelper::getTableColumns(Common::prefixTable('log_link_visit_action')));
            } elseif ('conversion' == $type) {
                $columns = array_keys(DbHelper::getTableColumns(Common::prefixTable('log_conversion')));
            } else {
                $columns = array();
            }

            foreach ($columns as $column) {
                if (strtolower($column) === strtolower($columnName)) {
                    throw new \InvalidArgumentException('This column name is already in use.');
                }
            }

            return $columnName;
        };

        $columnName = $input->getOption('columnname');

        if (empty($columnName)) {
            $columnName = $this->askAndValidate(
                'Enter the name of the column under which it should be stored in the MySQL database, for instance "visit_total_time": ',
                $validate
            );
        } else {
            $validate($columnName);
        }

        return $columnName;
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getColumnType()
    {
        $input = $this->getInput();

        $validate = function ($columnType) {
            if (empty($columnType)) {
                throw new \InvalidArgumentException('Please enter the type of the dimension column');
            }

            return $columnType;
        };

        $columnType = $input->getOption('columntype');

        if (empty($columnType)) {
            $columnType = $this->askAndValidate(
                'Enter the type of the column under which it should be stored in the MySQL database, for instance "VARCHAR(255) NOT NULL": ',
                $validate
            );
        } else {
            $validate($columnType);
        }

        return $columnType;
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getDimensionType()
    {
        $input = $this->getInput();
        $acceptedValues = array('visit', 'action', 'conversion', 'non-tracking-dimension');

        $validate = function ($type) use ($acceptedValues) {
            if (empty($type) || !in_array($type, $acceptedValues)) {
                throw new \InvalidArgumentException('Please enter a valid dimension type (' . implode(', ', $acceptedValues) .  '). Choose "non-tracking-dimension" if you only need a blank dimension having a name: ');
            }

            return $type;
        };

        $type = $input->getOption('type');

        if (empty($type)) {
            $type = $this->askAndValidate(
                'Please choose the type of dimension you want to create (' . implode(
                    ', ',
                    $acceptedValues
                ) . '). Choose "non-tracking-dimension" if you only need a blank dimension having a name: ',
                $validate,
                null,
                $acceptedValues
            );
        } else {
            $validate($type);
        }

        return $type;
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getPluginName()
    {
        $pluginNames = $this->getPluginNames();
        $invalidName = 'You have to enter a name of an existing plugin.';

        return $this->askPluginNameAndValidate($pluginNames, $invalidName);
    }
}