diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-06-25 08:30:50 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-06-25 08:30:50 +0400 |
commit | c7f06bb82097db89b746de503c224011a8c7f0a8 (patch) | |
tree | c2f4118978a58ecc9a87f7633830971427877bd2 /core/Columns | |
parent | 6526e28b143192611cabfcba4e0d8d5cd92464ad (diff) |
started to handle changes to dimensions, for instance if a new dimension is added the platform should detect this and run an update script. also if a dimension suddenly handles new cases such as conversion it should automatically add a column to log_conversion after a user confirms. Have not tested update and/or installation yet
Diffstat (limited to 'core/Columns')
-rw-r--r-- | core/Columns/Dimension.php | 76 | ||||
-rw-r--r-- | core/Columns/Updates.php | 110 |
2 files changed, 186 insertions, 0 deletions
diff --git a/core/Columns/Dimension.php b/core/Columns/Dimension.php new file mode 100644 index 0000000000..26ede26f72 --- /dev/null +++ b/core/Columns/Dimension.php @@ -0,0 +1,76 @@ +<?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\Columns; + +use Piwik\Plugin\Segment; +use Piwik\Translate; + +/** + * @api + * @since 2.5.0 + */ +abstract class Dimension +{ + protected $name; + protected $columnName = ''; + protected $columnType = ''; + protected $segments = array(); + + protected function configureSegments() + { + + } + + public function getVersion() + { + $reflectionClass = new \ReflectionObject($this); + $file = $reflectionClass->getFileName(); + + return filemtime($file); + } + + public function hasImplementedEvent($method) + { + $reflectionObject = new \ReflectionObject($this); + $declaringClass = $reflectionObject->getMethod($method)->getDeclaringClass(); + + return 0 === strpos($declaringClass->name, 'Piwik\Plugins'); + } + + protected function addSegment(Segment $segment) + { + $type = $segment->getType(); + + if (empty($type)) { + $segment->setType(Segment::TYPE_DIMENSION); + } + + $this->segments[] = $segment; + } + + /** + * @return Segment[] + */ + public function getSegments() + { + if (empty($this->segments)) { + $this->configureSegments(); + } + + return $this->segments; + } + + public function getColumnName() + { + return $this->columnName; + } + + abstract public function getName(); + +} diff --git a/core/Columns/Updates.php b/core/Columns/Updates.php new file mode 100644 index 0000000000..30cfa017cd --- /dev/null +++ b/core/Columns/Updates.php @@ -0,0 +1,110 @@ +<?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\Columns; +use Piwik\Common; +use Piwik\DbHelper; +use Piwik\Plugin\ActionDimension; +use Piwik\Plugin\VisitDimension; +use Piwik\Db; +use Piwik\Updater; + +/** + * Class that handles dimension updates + */ +class Updates extends \Piwik\Updates +{ + /** + * Return SQL to be executed in this update + * + * @return array( + * 'ALTER .... ' => '1234', // if the query fails, it will be ignored if the error code is 1234 + * 'ALTER .... ' => false, // if an error occurs, the update will stop and fail + * // and user will have to manually run the query + * ) + */ + public static function getSql() + { + $sqls = array(); + + $changingColumns = self::getUpdates(); + + foreach ($changingColumns as $table => $columns) { + $sqls["ALTER TABLE `" . $table . "` " . implode(', ', $columns)] = false; + } + + return $sqls; + } + + /** + * Incremental version update + */ + public static function update() + { + foreach (self::getSql() as $sql => $errorCode) { + try { + Db::exec($sql); + } catch (\Exception $e) { + if (!Db::get()->isErrNo($e, '1091') && !Db::get()->isErrNo($e, '1060')) { + Updater::handleQueryError($e, $sql, false, __FILE__); + } + } + } + } + + private static function getUpdates() + { + $visitColumns = DbHelper::getTableColumns(Common::prefixTable('log_visit')); + $actionColumns = DbHelper::getTableColumns(Common::prefixTable('log_link_visit_action')); + $conversionColumns = DbHelper::getTableColumns(Common::prefixTable('log_conversion')); + + $changingColumns = array(); + + foreach (VisitDimension::getAllDimensions() as $dimension) { + $columns = $dimension->install($visitColumns, $conversionColumns); + if (!empty($columns)) { + foreach ($columns as $table => $col) { + if (empty($changingColumns[$table])) { + $changingColumns[$table] = $col; + } else { + $changingColumns[$table] = array_merge($changingColumns[$table], $col); + } + } + } + } + + foreach (ActionDimension::getAllDimensions() as $dimension) { + $columns = $dimension->install($actionColumns); + if (!empty($columns)) { + foreach ($columns as $table => $col) { + if (empty($changingColumns[$table])) { + $changingColumns[$table] = $col; + } else { + $changingColumns[$table] = array_merge($changingColumns[$table], $col); + } + } + } + } + + return $changingColumns; + } + + public static function hasUpdates() + { + $changingColumns = self::getUpdates(); + + foreach ($changingColumns as $table => $columns) { + if (!empty($columns)) { + return true; + } + } + + return false; + } + +} |