diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2015-07-03 03:54:27 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2015-10-06 18:25:13 +0300 |
commit | 9ba8f216fd7856ce5fef06bf82ecb8f8a2e7e630 (patch) | |
tree | 6ce07d18a85d00b39ab720abe042361c0775aead /plugins/API/API.php | |
parent | 8ccc9dc05da021325cdbf141a548637fa52f16b2 (diff) |
generate pages instead of implementing them in each controller
Diffstat (limited to 'plugins/API/API.php')
-rw-r--r-- | plugins/API/API.php | 162 |
1 files changed, 58 insertions, 104 deletions
diff --git a/plugins/API/API.php b/plugins/API/API.php index 4ea0cb4055..8245d0712e 100644 --- a/plugins/API/API.php +++ b/plugins/API/API.php @@ -10,7 +10,10 @@ namespace Piwik\Plugins\API; use Piwik\API\Proxy; use Piwik\API\Request; -use Piwik\Columns\Dimension; +use Piwik\Cache; +use Piwik\CacheId; +use Piwik\Category\CategoryList; +use Piwik\Common; use Piwik\Config; use Piwik\Container\StaticContainer; use Piwik\DataTable; @@ -23,11 +26,13 @@ use Piwik\Period; use Piwik\Period\Range; use Piwik\Piwik; use Piwik\Plugin\Dimension\VisitDimension; +use Piwik\Plugin\Report; use Piwik\Plugins\API\DataTable\MergeDataTables; use Piwik\Plugins\CoreAdminHome\CustomLogo; use Piwik\Translation\Translator; use Piwik\Measurable\Type\TypeManager; use Piwik\Version; +use Piwik\Widget\WidgetsList; require_once PIWIK_INCLUDE_PATH . '/core/Config.php'; @@ -122,83 +127,20 @@ class API extends \Piwik\Plugin\API { $isAuthenticatedWithViewAccess = Piwik::isUserHasViewAccess($idSites) && !Piwik::isUserIsAnonymous(); - $segments = array(); - foreach (Dimension::getAllDimensions() as $dimension) { - foreach ($dimension->getSegments() as $segment) { - if ($segment->isRequiresAtLeastViewAccess()) { - $segment->setPermission($isAuthenticatedWithViewAccess); - } + $sites = (is_array($idSites) ? implode('.', $idSites) : (int) $idSites); + $cache = Cache::getTransientCache(); + $cachKey = 'API.getSegmentsMetadata' . $sites . '_' . (int) $_hideImplementationData . '_' . (int) $isAuthenticatedWithViewAccess; + $cachKey = CacheId::pluginAware($cachKey); - $segments[] = $segment->toArray(); - } + if ($cache->contains($cachKey)) { + return $cache->fetch($cachKey); } - /** - * Triggered when gathering all available segment dimensions. - * - * This event can be used to make new segment dimensions available. - * - * **Example** - * - * public function getSegmentsMetadata(&$segments, $idSites) - * { - * $segments[] = array( - * 'type' => 'dimension', - * 'category' => Piwik::translate('General_Visit'), - * 'name' => 'General_VisitorIP', - * 'segment' => 'visitIp', - * 'acceptedValues' => '13.54.122.1, etc.', - * 'sqlSegment' => 'log_visit.location_ip', - * 'sqlFilter' => array('Piwik\IP', 'P2N'), - * 'permission' => $isAuthenticatedWithViewAccess, - * ); - * } - * - * @param array &$dimensions The list of available segment dimensions. Append to this list to add - * new segments. Each element in this list must contain the - * following information: - * - * - **type**: Either `'metric'` or `'dimension'`. `'metric'` means - * the value is a numeric and `'dimension'` means it is - * a string. Also, `'metric'` values will be displayed - * under **Visit (metrics)** in the Segment Editor. - * - **category**: The segment category name. This can be an existing - * segment category visible in the segment editor. - * - **name**: The pretty name of the segment. Can be a translation token. - * - **segment**: The segment name, eg, `'visitIp'` or `'searches'`. - * - **acceptedValues**: A string describing one or two exacmple values, eg - * `'13.54.122.1, etc.'`. - * - **sqlSegment**: The table column this segment will segment by. - * For example, `'log_visit.location_ip'` for the - * **visitIp** segment. - * - **sqlFilter**: A PHP callback to apply to segment values before - * they are used in SQL. - * - **permission**: True if the current user has view access to this - * segment, false if otherwise. - * @param array $idSites The list of site IDs we're getting the available segments - * for. Some segments (such as Goal segments) depend on the - * site. - */ - Piwik::postEvent('API.getSegmentDimensionMetadata', array(&$segments, $idSites)); - - foreach ($segments as &$segment) { - $segment['name'] = Piwik::translate($segment['name']); - $segment['category'] = Piwik::translate($segment['category']); - - if ($_hideImplementationData) { - unset($segment['sqlFilter']); - unset($segment['sqlFilterValue']); - unset($segment['sqlSegment']); - - if (isset($segment['suggestedValuesCallback']) - && !is_string($segment['suggestedValuesCallback']) - ) { - unset($segment['suggestedValuesCallback']); - } - } - } + $metadata = new SegmentMetadata(); + $segments = $metadata->getSegmentsMetadata($idSites, $_hideImplementationData, $isAuthenticatedWithViewAccess); + + $cache->save($cachKey, $segments); - usort($segments, array($this, 'sortSegments')); return $segments; } @@ -219,32 +161,6 @@ class API extends \Piwik\Plugin\API return $values; } - private function sortSegments($row1, $row2) - { - $customVarCategory = Piwik::translate('CustomVariables_CustomVariables'); - - $columns = array('type', 'category', 'name', 'segment'); - foreach ($columns as $column) { - // Keep segments ordered alphabetically inside categories.. - $type = -1; - if ($column == 'name') $type = 1; - - $compare = $type * strcmp($row1[$column], $row2[$column]); - - // hack so that custom variables "page" are grouped together in the doc - if ($row1['category'] == $customVarCategory - && $row1['category'] == $row2['category'] - ) { - $compare = strcmp($row1['segment'], $row2['segment']); - return $compare; - } - if ($compare != 0) { - return $compare; - } - } - return $compare; - } - /** * Returns the url to application logo (~280x110px) * @@ -343,6 +259,41 @@ class API extends \Piwik\Plugin\API } /** + * Get a list of all pages that shall be shown in a Piwik UI including a list of all widgets that shall + * be shown within each page. + * + * @param int $idSite + * @return array + */ + public function getReportPagesMetadata($idSite) + { + Piwik::checkUserHasViewAccess($idSite); + + $widgetsList = WidgetsList::get(); + $categoryList = CategoryList::get(); + $metadata = new WidgetMetadata(); + + return $metadata->getPagesMetadata($categoryList, $widgetsList); + } + + /** + * Get a list of all widgetizable widgets. + * + * @param int $idSite + * @return array + */ + public function getWidgetMetadata($idSite) + { + Piwik::checkUserHasViewAccess($idSite); + + $widgetsList = WidgetsList::get(); + $categoryList = CategoryList::get(); + $metadata = new WidgetMetadata(); + + return $metadata->getWidgetMetadata($categoryList, $widgetsList); + } + + /** * Get a combined report of the *.get API methods. */ public function get($idSite, $period, $date, $segment = false, $columns = false) @@ -555,7 +506,12 @@ class API extends \Piwik\Plugin\API if ($suggestedValuesCallbackRequiresTable) { $values = call_user_func($segmentFound['suggestedValuesCallback'], $idSite, $maxSuggestionsToReturn, $table); } else { - $values = $this->getSegmentValuesFromVisitorLog($segmentName, $table); + // Cleanup data to return the top suggested (non empty) labels for this segment + $values = $table->getColumn($segmentName); + + // Select also flattened keys (custom variables "page" scope, page URLs for one visit, page titles for one visit) + $valuesBis = $table->getColumnsStartingWith($segmentName . ColumnDelete::APPEND_TO_COLUMN_NAME_TO_KEEP); + $values = array_merge($values, $valuesBis); } $values = $this->getMostFrequentValues($values); @@ -600,9 +556,7 @@ class API extends \Piwik\Plugin\API // If you update this, also update flattenVisitorDetailsArray $segmentsNeedActionsInfo = array('visitConvertedGoalId', 'pageUrl', 'pageTitle', 'siteSearchKeyword', - 'entryPageTitle', 'entryPageUrl', 'exitPageTitle', 'exitPageUrl', - 'outlinkUrl', 'downloadUrl' - ); + 'entryPageTitle', 'entryPageUrl', 'exitPageTitle', 'exitPageUrl'); $isCustomVariablePage = stripos($segmentName, 'customVariablePage') !== false; $isEventSegment = stripos($segmentName, 'event') !== false; $isContentSegment = stripos($segmentName, 'content') !== false; |