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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://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;
use Piwik\Plugin\Report;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
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.')
->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin which does not have a menu defined yet')
->addOption('type', null, InputOption::VALUE_REQUIRED, 'Whether you want to create a "Visit", an "Action" or a "Conversion" dimension')
->addOption('dimensionname', null, InputOption::VALUE_REQUIRED, 'A human readable name of the dimension which will be for instance visible in the UI')
->addOption('columnname', null, InputOption::VALUE_REQUIRED, 'The name of the column in the MySQL database the dimension will be stored under')
->addOption('columntype', null, InputOption::VALUE_REQUIRED, 'The MySQL type for your dimension, for instance "VARCHAR(255) NOT NULL".');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws \InvalidArgumentException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$pluginName = $this->getPluginName($input, $output);
$this->checkAndUpdateRequiredPiwikVersion($pluginName, $output);
$type = $this->getDimensionType($input, $output);
$dimensionName = $this->getDimensionName($input, $output);
if ('non-tracking-dimension' === $type) {
$columnName = '';
$columType = '';
} else {
$columnName = $this->getColumnName($input, $output, $type);
$columType = $this->getColumnType($input, $output);
}
$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 NOT 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($output, array(
sprintf('Columns/%s.php for %s generated.', ucfirst($dimensionClassName), $pluginName),
'You should now implement the events within this file',
'Enjoy!'
));
}
private function getDimensionClassName($dimensionName)
{
$dimensionName = trim($dimensionName);
$dimensionName = str_replace(' ', '', $dimensionName);
$dimensionName = preg_replace("/[^A-Za-z0-9]/", '', $dimensionName);
$dimensionName = ucfirst($dimensionName);
return $dimensionName;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RuntimeException
*/
protected function getDimensionName(InputInterface $input, OutputInterface $output)
{
$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)) {
$dialog = $this->getHelperSet()->get('dialog');
$dimensionName = $dialog->askAndValidate($output, 'Enter a human readable name of your dimension, for instance "Browser": ', $validate);
} else {
$validate($dimensionName);
}
$dimensionName = ucfirst($dimensionName);
return $dimensionName;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $type
* @return array
* @throws \RuntimeException
*/
protected function getColumnName(InputInterface $input, OutputInterface $output, $type)
{
$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)) {
$dialog = $this->getHelperSet()->get('dialog');
$columnName = $dialog->askAndValidate($output, '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;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RuntimeException
*/
protected function getColumnType(InputInterface $input, OutputInterface $output)
{
$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)) {
$dialog = $this->getHelperSet()->get('dialog');
$columnType = $dialog->askAndValidate($output, '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;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RuntimeException
*/
protected function getDimensionType(InputInterface $input, OutputInterface $output)
{
$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)) {
$dialog = $this->getHelperSet()->get('dialog');
$type = $dialog->askAndValidate($output, '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, false, null, $acceptedValues);
} else {
$validate($type);
}
return $type;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RuntimeException
*/
protected function getPluginName(InputInterface $input, OutputInterface $output)
{
$pluginNames = $this->getPluginNames();
$invalidName = 'You have to enter a name of an existing plugin.';
return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName);
}
}
|