diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-04-03 08:03:19 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-04-03 08:03:19 +0400 |
commit | 5cbbb4f47b69e977aa43b4403fb3459187f0111b (patch) | |
tree | b66c2bfa0b3aa21c739fea8d245e83bd7ed48195 /plugins | |
parent | baa840bf4a5d16bb6097625671c6e55d30becbac (diff) |
some more tweaks and added a comment to get info about currently configured custom vars
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/CustomVariables/Archiver.php | 3 | ||||
-rw-r--r-- | plugins/CustomVariables/Commands/Info.php | 69 | ||||
-rw-r--r-- | plugins/CustomVariables/CustomVariables.php | 5 | ||||
-rw-r--r-- | plugins/CustomVariables/Model.php | 32 | ||||
-rw-r--r-- | plugins/CustomVariables/tests/ModelTest.php | 14 | ||||
-rw-r--r-- | plugins/Live/Visitor.php | 14 |
6 files changed, 123 insertions, 14 deletions
diff --git a/plugins/CustomVariables/Archiver.php b/plugins/CustomVariables/Archiver.php index f2bdd4d6da..d7ae1ac015 100644 --- a/plugins/CustomVariables/Archiver.php +++ b/plugins/CustomVariables/Archiver.php @@ -59,7 +59,8 @@ class Archiver extends \Piwik\Plugin\Archiver { $this->dataArray = new DataArray(); - for ($i = 1; $i <= CustomVariables::getMaxCustomVariables(); $i++) { + $maxCustomVariables = CustomVariables::getMaxCustomVariables(); + for ($i = 1; $i <= $maxCustomVariables; $i++) { $this->aggregateCustomVariable($i); } diff --git a/plugins/CustomVariables/Commands/Info.php b/plugins/CustomVariables/Commands/Info.php new file mode 100644 index 0000000000..5ec0fa0b34 --- /dev/null +++ b/plugins/CustomVariables/Commands/Info.php @@ -0,0 +1,69 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CustomVariables\Commands; + +use Piwik\Common; +use Piwik\Plugin\ConsoleCommand; +use Piwik\Plugins\CustomVariables\CustomVariables; +use Piwik\Plugins\CustomVariables\Model; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + */ +class Check extends ConsoleCommand +{ + protected function configure() + { + $this->setName('customvariables:info'); + $this->setDescription('Get info about configured custom variables'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $maxVars = CustomVariables::getMaxCustomVariables(); + + if ($this->hasEverywhereSameAmountOfVariables()) { + $this->writeSuccessMessage($output, array( + 'Your Piwik is configured for ' . $maxVars . ' custom variables.' + )); + return; + } + + $output->writeln('<error>There is a problem with your custom variables configuration:</error>'); + $output->writeln('<error>Some database tables miss custom variables columns.</error>'); + $output->writeln(''); + $output->writeln('Your Piwik seems to be configured for ' . $maxVars . ' custom variables.'); + $output->writeln('Executing "<comment>./console set-max-custom-variables ' . $maxVars . '</comment>" might fix this issue.'); + $output->writeln('If not check the following tables whether they have the same columns starting with <comment>custom_var_</comment>: '); + foreach (Model::getScopes() as $scope) { + $output->writeln(Common::prefixTable($scope)); + } + } + + private function hasEverywhereSameAmountOfVariables() + { + $indexesBefore = null; + + foreach (Model::getScopes() as $scope) { + $model = new Model($scope); + $indexes = $model->getCustomVarIndexes(); + + if (is_null($indexesBefore)) { + $indexesBefore = $indexes; + } elseif ($indexes != $indexesBefore) { + return false; + } + } + + return true; + } +} diff --git a/plugins/CustomVariables/CustomVariables.php b/plugins/CustomVariables/CustomVariables.php index 6a7ae6618e..b4e71517a3 100644 --- a/plugins/CustomVariables/CustomVariables.php +++ b/plugins/CustomVariables/CustomVariables.php @@ -96,6 +96,7 @@ class CustomVariables extends \Piwik\Plugin public function addConsoleCommands(&$commands) { $commands[] = __NAMESPACE__ . '\\Commands\\SetNumberOfCustomVariables'; + $commands[] = __NAMESPACE__ . '\\Commands\\Info'; } /** @@ -127,7 +128,9 @@ class CustomVariables extends \Piwik\Plugin public function getSegmentsMetadata(&$segments) { - for ($i = 1; $i <= self::getMaxCustomVariables(); $i++) { + $maxCustomVariables = self::getMaxCustomVariables(); + + for ($i = 1; $i <= $maxCustomVariables; $i++) { $segments[] = array( 'type' => 'dimension', 'category' => 'CustomVariables_CustomVariables', diff --git a/plugins/CustomVariables/Model.php b/plugins/CustomVariables/Model.php index 60cbeac947..c2c425b06e 100644 --- a/plugins/CustomVariables/Model.php +++ b/plugins/CustomVariables/Model.php @@ -49,11 +49,9 @@ class Model */ public function getCurrentNumCustomVars() { - $customVarColumns = $this->getCustomVarColumnNames(); + $indexes = $this->getCustomVarIndexes(); - $currentNumCustomVars = count($customVarColumns) / 2; - - return (int) $currentNumCustomVars; + return count($indexes); } /** @@ -71,22 +69,33 @@ class Model */ public function getHighestCustomVarIndex() { + $indexes = $this->getCustomVarIndexes(); + + if (empty($indexes)) { + return 0; + } + + return max($indexes); + } + + public function getCustomVarIndexes() + { $columns = $this->getCustomVarColumnNames(); if (empty($columns)) { - return 0; + return array(); } $indexes = array_map(function ($column) { return Model::getCustomVariableIndexFromFieldName($column); }, $columns); - return max($indexes); + return array_values(array_unique($indexes)); } private function getCustomVarColumnNames() { - $dbTable = Common::prefixTable($this->scope); + $dbTable = $this->getDbTableName(); $columns = Db::getColumnNamesFromTable($dbTable); $customVarColumns = array_filter($columns, function ($column) { @@ -98,7 +107,7 @@ class Model public function removeCustomVariable() { - $dbTable = Common::prefixTable($this->scope); + $dbTable = $this->getDbTableName(); $index = $this->getHighestCustomVarIndex(); if ($index < 1) { @@ -113,7 +122,7 @@ class Model public function addCustomVariable() { - $dbTable = Common::prefixTable($this->scope); + $dbTable = $this->getDbTableName(); $index = $this->getHighestCustomVarIndex() + 1; $maxLen = CustomVariables::getMaxLengthCustomVariables(); @@ -123,6 +132,11 @@ class Model return $index; } + private function getDbTableName() + { + return Common::prefixTable($this->scope); + } + public static function getCustomVariableIndexFromFieldName($fieldName) { $onlyNumber = str_replace(array('custom_var_k', 'custom_var_v'), '', $fieldName); diff --git a/plugins/CustomVariables/tests/ModelTest.php b/plugins/CustomVariables/tests/ModelTest.php index 1cf77ac152..14d8647bf0 100644 --- a/plugins/CustomVariables/tests/ModelTest.php +++ b/plugins/CustomVariables/tests/ModelTest.php @@ -70,6 +70,20 @@ class ModelTest extends \DatabaseTestCase $this->assertEquals(4, $this->getConversionScope()->getCurrentNumCustomVars()); } + public function test_getCustomVarIndexes() + { + $this->assertEquals(array(1,2,3,4,5), $this->getPageScope()->getCustomVarIndexes()); + $this->assertEquals(array(1,2,3,4,5), $this->getVisitScope()->getCustomVarIndexes()); + $this->assertEquals(array(1,2,3,4,5), $this->getConversionScope()->getCustomVarIndexes()); + + $this->getPageScope()->addCustomVariable(); + $this->getConversionScope()->removeCustomVariable(); + + $this->assertEquals(array(1,2,3,4,5,6), $this->getPageScope()->getCustomVarIndexes()); + $this->assertEquals(array(1,2,3,4,5), $this->getVisitScope()->getCustomVarIndexes()); + $this->assertEquals(array(1,2,3,4), $this->getConversionScope()->getCustomVarIndexes()); + } + public function test_getHighestCustomVarIndex_addCustomVariable_removeCustomVariable() { $this->assertEquals(5, $this->getPageScope()->getHighestCustomVarIndex()); diff --git a/plugins/Live/Visitor.php b/plugins/Live/Visitor.php index 37a8c20f77..9e13d9b514 100644 --- a/plugins/Live/Visitor.php +++ b/plugins/Live/Visitor.php @@ -339,7 +339,10 @@ class Visitor function getCustomVariables() { $customVariables = array(); - for ($i = 1; $i <= CustomVariables::getMaxCustomVariables(); $i++) { + + $maxCustomVariables = CustomVariables::getMaxCustomVariables(); + + for ($i = 1; $i <= $maxCustomVariables; $i++) { if (!empty($this->details['custom_var_k' . $i])) { $customVariables[$i] = array( 'customVariableName' . $i => $this->details['custom_var_k' . $i], @@ -724,8 +727,10 @@ class Visitor { $idVisit = $visitorDetailsArray['idVisit']; + $maxCustomVariables = CustomVariables::getMaxCustomVariables(); + $sqlCustomVariables = ''; - for ($i = 1; $i <= CustomVariables::getMaxCustomVariables(); $i++) { + for ($i = 1; $i <= $maxCustomVariables; $i++) { $sqlCustomVariables .= ', custom_var_k' . $i . ', custom_var_v' . $i; } // The second join is a LEFT join to allow returning records that don't have a matching page title @@ -762,7 +767,10 @@ class Visitor foreach ($actionDetails as $actionIdx => &$actionDetail) { $actionDetail =& $actionDetails[$actionIdx]; $customVariablesPage = array(); - for ($i = 1; $i <= CustomVariables::getMaxCustomVariables(); $i++) { + + $maxCustomVariables = CustomVariables::getMaxCustomVariables(); + + for ($i = 1; $i <= $maxCustomVariables; $i++) { if (!empty($actionDetail['custom_var_k' . $i])) { $cvarKey = $actionDetail['custom_var_k' . $i]; $cvarKey = static::getCustomVariablePrettyKey($cvarKey); |