/** * Populates the properties that store cached dimensions and metrics, * either from the database or from the API directly, depending on when * the data that we have was last fetched. */ private function _refreshColumnMetadata() { /* If we're not using a database, the best we can do is to cache the column metadata for the duration of this process' existence. */ if (self::$_dbConn) { try { $lastFetchData = self::_getLastFetchData('google_analytics_api_columns'); if ($lastFetchData) { $fetchDate = (int) $lastFetchData['fetch_date']; $etag = $lastFetchData['etag']; } else { $fetchDate = null; $etag = null; } $columns = null; $newETag = null; if ($this->_bypassColumnCache || !$fetchDate || $fetchDate <= time() - GOOGLE_ANALYTICS_API_METADATA_CACHE_DURATION) { $this->_bypassColumnCache = false; $request = new APIRequest('metadata/ga/columns'); if ($etag) { $request->setHeader('If-None-Match: ' . $etag); } $columns = $this->_makeRequest($request); if ($columns) { $stmt = self::$_DB_STATEMENTS['google_analytics_api_columns']['insert']; foreach ($columns as $column) { $stmt->bindValue(':name', $column->getName(), \PDO::PARAM_STR); $stmt->bindValue(':type', $column->getType(), \PDO::PARAM_STR); $stmt->bindValue(':data_type', $column->getDataType(), \PDO::PARAM_STR); $stmt->bindValue(':replaced_by', $column->getReplacementColumn(), \PDO::PARAM_STR); $stmt->bindValue(':group', $column->getGroup(), \PDO::PARAM_STR); $stmt->bindValue(':ui_name', $column->getUIName(), \PDO::PARAM_STR); $stmt->bindValue(':description', $column->getDescription(), \PDO::PARAM_STR); $stmt->bindValue(':calculation', $column->getCalculation(), \PDO::PARAM_STR); $stmt->bindValue(':min_template_index', $column->getMinTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':max_template_index', $column->getMaxTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':min_template_index_premium', $column->getPremiumMinTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':max_template_index_premium', $column->getPremiumMaxTemplateIndex(), \PDO::PARAM_INT); $stmt->bindValue(':allowed_in_segments', $column->isAllowedInSegments(), \PDO::PARAM_INT); $stmt->bindValue(':deprecated', $column->isDeprecated(), \PDO::PARAM_INT); $stmt->execute(); } $stmt = null; } if (array_key_exists('etag', $this->_responseParsed) && $this->_responseParsed['etag']) { $newETag = $this->_responseParsed['etag']; } else { $responseHeader = $this->getResponseHeaderAsAssociativeArray(); if (array_key_exists('ETag', $responseHeader) && $responseHeader['ETag']) { $newETag = $responseHeader['ETag']; } } if ($newETag) { self::_storeFetchData('google_analytics_api_columns', count($columns), $newETag); } } if (!$columns) { $columns = array(); $stmt = self::$_dbConn->query('SELECT * FROM google_analytics_api_columns'); $stmt->bindColumn('name', $name, \PDO::PARAM_STR); $stmt->bindColumn('type', $type, \PDO::PARAM_STR); $stmt->bindColumn('data_type', $dataType, \PDO::PARAM_STR); $stmt->bindColumn('replaced_by', $replacement, \PDO::PARAM_STR); $stmt->bindColumn('group', $group, \PDO::PARAM_STR); $stmt->bindColumn('ui_name', $uiName, \PDO::PARAM_STR); $stmt->bindColumn('description', $description, \PDO::PARAM_STR); $stmt->bindColumn('calculation', $calculation, \PDO::PARAM_STR); /* Null values get cast to zeros if I bind them as integers, so we'll rely on the object's setter to take care of the type casting. */ $stmt->bindColumn('min_template_index', $minTemplateIndex, \PDO::PARAM_STR); $stmt->bindColumn('max_template_index', $maxTemplateIndex, \PDO::PARAM_STR); $stmt->bindColumn('min_template_index_premium', $minTemplateIndexPremium, \PDO::PARAM_STR); $stmt->bindColumn('max_template_index_premium', $maxTemplateIndexPremium, \PDO::PARAM_STR); $stmt->bindColumn('allowed_in_segments', $allowedInSegments, \PDO::PARAM_BOOL); $stmt->bindColumn('deprecated', $deprecated, \PDO::PARAM_BOOL); while ($stmt->fetch(\PDO::FETCH_BOUND)) { $column = new Column(); $column->setID('ga:' . $name); $column->setReplacementColumn($replacement); $column->setType($type); $column->setDataType($dataType); $column->setGroup($group); $column->setUIName($uiName); $column->setDescription($description); $column->setCalculation($calculation); if ($minTemplateIndex !== null) { $column->setMinTemplateIndex($minTemplateIndex); } if ($maxTemplateIndex !== null) { $column->setMaxTemplateIndex($maxTemplateIndex); } if ($minTemplateIndexPremium !== null) { $column->setPremiumMinTemplateIndex($minTemplateIndexPremium); } if ($maxTemplateIndexPremium !== null) { $column->setPremiumMaxTemplateIndex($maxTemplateIndexPremium); } $column->isAllowedInSegments($allowedInSegments); $column->isDeprecated($deprecated); $columns[] = $column; } } } catch (\PDOException $e) { throw new RuntimeException('Caught database error while refreshing column metadata.', null, $e); } } else { $columns = $this->_makeRequest(new APIRequest('metadata/ga/columns')); } foreach ($columns as $column) { if ($column->getType() == 'DIMENSION') { $table =& self::$_dimensions; $list =& self::$_dimensionNames; } else { $table =& self::$_metrics; $list =& self::$_metricNames; } $name = $column->getName(); $table[$name] = $column; if (!$column->isDeprecated()) { $list[] = $name; } } usort(self::$_dimensionNames, 'strcasecmp'); usort(self::$_metricNames, 'strcasecmp'); }