update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -9,26 +9,32 @@
namespace Piwik\Plugin;
use Piwik\API\DataTablePostProcessor;
use Piwik\API\Proxy;
use Piwik\API\ResponseBuilder;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Date;
use Piwik\Log;
use Piwik\MetricsFormatter;
use Piwik\Metrics\Formatter\Html as HtmlFormatter;
use Piwik\NoAccessException;
use Piwik\Option;
use Piwik\Period;
use Piwik\Piwik;
use Piwik\Plugins\API\API as ApiApi;
use Piwik\Plugins\PrivacyManager\PrivacyManager;
use Piwik\View;
use Piwik\ViewDataTable\Manager as ViewDataTableManager;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\API\Request as ApiRequest;
/**
* The base class for report visualizations that output HTML and use JavaScript.
*
*
* Report visualizations that extend from this class will be displayed like all others in
* the Piwik UI. The following extra UI controls will be displayed around the visualization
* itself:
*
*
* - report documentation,
* - a footer message (if {@link Piwik\ViewDataTable\Config::$show_footer_message} is set),
* - a list of links to related reports (if {@link Piwik\ViewDataTable\Config::$related_reports} is set),
@ -37,33 +43,33 @@ use Piwik\ViewDataTable\Manager as ViewDataTableManager;
* - a limit control that allows users to change the amount of rows displayed (if
* {@link Piwik\ViewDataTable\Config::$show_limit_control} is true),
* - and more depending on the visualization.
*
*
* ### Rendering Process
*
*
* The following process is used to render reports:
*
*
* - The report is loaded through Piwik's Reporting API.
* - The display and request properties that require report data in order to determine a default
* value are defaulted. These properties are:
*
*
* - {@link Piwik\ViewDataTable\Config::$columns_to_display}
* - {@link Piwik\ViewDataTable\RequestConfig::$filter_sort_column}
* - {@link Piwik\ViewDataTable\RequestConfig::$filter_sort_order}
*
*
* - Priority filters are applied to the report (see {@link Piwik\ViewDataTable\Config::$filters}).
* - The filters that are applied to every report in the Reporting API (called **generic filters**)
* are applied. (see {@link Piwik\API\Request})
* - The report's queued filters are applied.
* - A {@link Piwik\View} instance is created and rendered.
*
*
* ### Rendering Hooks
*
*
* The Visualization class defines several overridable methods that are called at specific
* points during the rendering process. Derived classes can override these methods change
* the data that is displayed or set custom properties.
*
*
* The overridable methods (called **rendering hooks**) are as follows:
*
*
* - **beforeLoadDataTable**: Called at the start of the rendering process before any data
* is loaded.
* - **beforeGenericFiltersAreAppliedToLoadedDataTable**: Called after data is loaded and after priority
@ -76,20 +82,20 @@ use Piwik\ViewDataTable\Manager as ViewDataTableManager;
* - **beforeRender**: Called immediately before a {@link Piwik\View} is created and rendered.
* - **isThereDataToDisplay**: Called after a {@link Piwik\View} is created to determine if the report has
* data or not. If not, a message is displayed to the user.
*
*
* ### The DataTable JavaScript class
*
*
* In the UI, visualization behavior is provided by logic in the **DataTable** JavaScript class.
* When creating new visualizations, the **DataTable** JavaScript class (or one of its existing
* descendants) should be extended.
*
*
* To learn more read the [Visualizing Report Data](/guides/visualizing-report-data#creating-new-visualizations)
* guide.
*
* ### Examples
*
*
* **Changing the data that is loaded**
*
*
* class MyVisualization extends Visualization
* {
* // load the previous period's data as well as the requested data. this will change
@ -101,20 +107,20 @@ use Piwik\ViewDataTable\Manager as ViewDataTableManager;
*
* $this->requestConfig->request_parameters_to_modify['date'] = $previousDate . ',' . $date;
* }
*
*
* // since we load the previous period's data too, we need to override the logic to
* // check if there is data or not.
* public function isThereDataToDisplay()
* {
* $tables = $this->dataTable->getDataTables()
* $requestedDataTable = end($tables);
*
*
* return $requestedDataTable->getRowsCount() != 0;
* }
* }
*
*
* **Force properties to be set to certain values**
*
*
* class MyVisualization extends Visualization
* {
* // ensure that some properties are set to certain values before rendering.
@ -133,9 +139,9 @@ class Visualization extends ViewDataTable
{
/**
* The Twig template file to use when rendering, eg, `"@MyPlugin/_myVisualization.twig"`.
*
*
* Must be defined by classes that extend Visualization.
*
*
* @api
*/
const TEMPLATE_FILE = '';
@ -143,8 +149,14 @@ class Visualization extends ViewDataTable
private $templateVars = array();
private $reportLastUpdatedMessage = null;
private $metadata = null;
protected $metricsFormatter = null;
final public function __construct($controllerAction, $apiMethodToRequestDataTable)
/**
* @var Report
*/
protected $report;
final public function __construct($controllerAction, $apiMethodToRequestDataTable, $params = array())
{
$templateFile = static::TEMPLATE_FILE;
@ -152,7 +164,11 @@ class Visualization extends ViewDataTable
throw new \Exception('You have not defined a constant named TEMPLATE_FILE in your visualization class.');
}
parent::__construct($controllerAction, $apiMethodToRequestDataTable);
$this->metricsFormatter = new HtmlFormatter();
parent::__construct($controllerAction, $apiMethodToRequestDataTable, $params);
$this->report = Report::factory($this->requestConfig->getApiModuleToRequest(), $this->requestConfig->getApiMethodToRequest());
}
protected function buildView()
@ -160,26 +176,29 @@ class Visualization extends ViewDataTable
$this->overrideSomeConfigPropertiesIfNeeded();
try {
$this->beforeLoadDataTable();
$this->loadDataTableFromAPI(array('disable_generic_filters' => 1));
$this->loadDataTableFromAPI();
$this->postDataTableLoadedFromAPI();
$requestPropertiesAfterLoadDataTable = $this->requestConfig->getProperties();
$this->applyFilters();
$this->addVisualizationInfoFromMetricMetadata();
$this->afterAllFiltersAreApplied();
$this->beforeRender();
$this->logMessageIfRequestPropertiesHaveChanged($requestPropertiesAfterLoadDataTable);
} catch (NoAccessException $e) {
throw $e;
} catch (\Exception $e) {
Log::warning("Failed to get data from API: " . $e->getMessage() . "\n" . $e->getTraceAsString());
Log::error("Failed to get data from API: " . $e->getMessage() . "\n" . $e->getTraceAsString());
$loadingError = array('message' => $e->getMessage());
$message = $e->getMessage();
if (\Piwik_ShouldPrintBackTraceWithMessage()) {
$message .= "\n" . $e->getTraceAsString();
}
$loadingError = array('message' => $message);
}
$view = new View("@CoreHome/_dataTable");
@ -192,6 +211,7 @@ class Visualization extends ViewDataTable
$view->visualization = $this;
$view->visualizationTemplate = static::TEMPLATE_FILE;
$view->visualizationCssClass = $this->getDefaultDataTableCssClass();
$view->reportMetdadata = $this->getReportMetadata();
if (null === $this->dataTable) {
$view->dataTable = null;
@ -216,17 +236,78 @@ class Visualization extends ViewDataTable
return $view;
}
/**
* @internal
*/
protected function loadDataTableFromAPI()
{
if (!is_null($this->dataTable)) {
// data table is already there
// this happens when setDataTable has been used
return $this->dataTable;
}
// we build the request (URL) to call the API
$request = $this->buildApiRequestArray();
$module = $this->requestConfig->getApiModuleToRequest();
$method = $this->requestConfig->getApiMethodToRequest();
PluginManager::getInstance()->checkIsPluginActivated($module);
$class = ApiRequest::getClassNameAPI($module);
$dataTable = Proxy::getInstance()->call($class, $method, $request);
$response = new ResponseBuilder($format = 'original', $request);
$response->disableSendHeader();
$response->disableDataTablePostProcessor();
$this->dataTable = $response->getResponse($dataTable, $module, $method);
}
private function getReportMetadata()
{
$request = $this->request->getRequestArray() + $_GET + $_POST;
$idSite = Common::getRequestVar('idSite', null, 'string', $request);
$module = $this->requestConfig->getApiModuleToRequest();
$action = $this->requestConfig->getApiMethodToRequest();
$apiParameters = array();
$idDimension = Common::getRequestVar('idDimension', 0, 'int');
$idGoal = Common::getRequestVar('idGoal', 0, 'int');
if ($idDimension > 0) {
$apiParameters['idDimension'] = $idDimension;
}
if ($idGoal > 0) {
$apiParameters['idGoal'] = $idGoal;
}
$metadata = ApiApi::getInstance()->getMetadata($idSite, $module, $action, $apiParameters);
if (!empty($metadata)) {
return array_shift($metadata);
}
return false;
}
private function overrideSomeConfigPropertiesIfNeeded()
{
if (empty($this->config->footer_icons)) {
$this->config->footer_icons = ViewDataTableManager::configureFooterIcons($this);
}
if (!\Piwik\Plugin\Manager::getInstance()->isPluginActivated('Goals')) {
if (!$this->isPluginActivated('Goals')) {
$this->config->show_goals = false;
}
}
private function isPluginActivated($pluginName)
{
return PluginManager::getInstance()->isPluginActivated($pluginName);
}
/**
* Assigns a template variable making it available in the Twig template specified by
* {@link TEMPLATE_FILE}.
@ -248,9 +329,9 @@ class Visualization extends ViewDataTable
/**
* Returns `true` if there is data to display, `false` if otherwise.
*
*
* Derived classes should override this method if they change the amount of data that is loaded.
*
*
* @api
*/
protected function isThereDataToDisplay()
@ -281,7 +362,7 @@ class Visualization extends ViewDataTable
}
if (empty($this->requestConfig->filter_sort_column)) {
$this->requestConfig->setDefaultSort($this->config->columns_to_display, $hasNbUniqVisitors);
$this->requestConfig->setDefaultSort($this->config->columns_to_display, $hasNbUniqVisitors, $columns);
}
// deal w/ table metadata
@ -289,38 +370,78 @@ class Visualization extends ViewDataTable
$this->metadata = $this->dataTable->getAllTableMetadata();
if (isset($this->metadata[DataTable::ARCHIVED_DATE_METADATA_NAME])) {
$this->config->report_last_updated_message = $this->makePrettyArchivedOnText();
$this->reportLastUpdatedMessage = $this->makePrettyArchivedOnText();
}
}
$pivotBy = Common::getRequestVar('pivotBy', false) ?: $this->requestConfig->pivotBy;
if (empty($pivotBy)
&& $this->dataTable instanceof DataTable
) {
$this->config->disablePivotBySubtableIfTableHasNoSubtables($this->dataTable);
}
}
private function addVisualizationInfoFromMetricMetadata()
{
$dataTable = $this->dataTable instanceof DataTable\Map ? $this->dataTable->getFirstRow() : $this->dataTable;
$metrics = Report::getMetricsForTable($dataTable, $this->report);
// TODO: instead of iterating & calling translate everywhere, maybe we can get all translated names in one place.
// may be difficult, though, since translated metrics are specific to the report.
foreach ($metrics as $metric) {
$name = $metric->getName();
if (empty($this->config->translations[$name])) {
$this->config->translations[$name] = $metric->getTranslatedName();
}
if (empty($this->config->metrics_documentation[$name])) {
$this->config->metrics_documentation[$name] = $metric->getDocumentation();
}
}
}
private function applyFilters()
{
list($priorityFilters, $otherFilters) = $this->config->getFiltersToRun();
$postProcessor = $this->makeDataTablePostProcessor(); // must be created after requestConfig is final
$self = $this;
// First, filters that delete rows
foreach ($priorityFilters as $filter) {
$this->dataTable->filter($filter[0], $filter[1]);
}
$postProcessor->setCallbackBeforeGenericFilters(function (DataTable\DataTableInterface $dataTable) use ($self, $postProcessor) {
$this->beforeGenericFiltersAreAppliedToLoadedDataTable();
$self->setDataTable($dataTable);
if (!$this->requestConfig->areGenericFiltersDisabled()) {
$this->applyGenericFilters();
}
// First, filters that delete rows
foreach ($self->config->getPriorityFilters() as $filter) {
$dataTable->filter($filter[0], $filter[1]);
}
$this->afterGenericFiltersAreAppliedToLoadedDataTable();
$self->beforeGenericFiltersAreAppliedToLoadedDataTable();
// queue other filters so they can be applied later if queued filters are disabled
foreach ($otherFilters as $filter) {
$this->dataTable->queueFilter($filter[0], $filter[1]);
}
if (!in_array($self->requestConfig->filter_sort_column, $self->config->columns_to_display)) {
$hasNbUniqVisitors = in_array('nb_uniq_visitors', $self->config->columns_to_display);
$columns = $dataTable->getColumns();
$self->requestConfig->setDefaultSort($self->config->columns_to_display, $hasNbUniqVisitors, $columns);
}
// Finally, apply datatable filters that were queued (should be 'presentation' filters that
// do not affect the number of rows)
if (!$this->requestConfig->areQueuedFiltersDisabled()) {
$this->dataTable->applyQueuedFilters();
}
$postProcessor->setRequest($self->buildApiRequestArray());
});
$postProcessor->setCallbackAfterGenericFilters(function (DataTable\DataTableInterface $dataTable) use ($self) {
$self->setDataTable($dataTable);
$self->afterGenericFiltersAreAppliedToLoadedDataTable();
// queue other filters so they can be applied later if queued filters are disabled
foreach ($self->config->getPresentationFilters() as $filter) {
$dataTable->queueFilter($filter[0], $filter[1]);
}
});
$this->dataTable = $postProcessor->process($this->dataTable);
}
private function removeEmptyColumnsFromDisplay()
@ -355,16 +476,16 @@ class Visualization extends ViewDataTable
$today = mktime(0, 0, 0);
if ($date->getTimestamp() > $today) {
$elapsedSeconds = time() - $date->getTimestamp();
$timeAgo = MetricsFormatter::getPrettyTimeFromSeconds($elapsedSeconds);
$timeAgo = $this->metricsFormatter->getPrettyTimeFromSeconds($elapsedSeconds);
return Piwik::translate('CoreHome_ReportGeneratedXAgo', $timeAgo);
}
$prettyDate = $date->getLocalized("%longYear%, %longMonth% %day%") . $date->toString('S');
$prettyDate = $date->getLocalized(Date::DATE_FORMAT_SHORT);
return Piwik::translate('CoreHome_ReportGeneratedOn', $prettyDate);
$timezoneAppend = ' (UTC)';
return Piwik::translate('CoreHome_ReportGeneratedOn', $prettyDate) . $timezoneAppend;
}
/**
@ -379,7 +500,7 @@ class Visualization extends ViewDataTable
*/
private function hasReportBeenPurged()
{
if (!\Piwik\Plugin\Manager::getInstance()->isPluginActivated('PrivacyManager')) {
if (!$this->isPluginActivated('PrivacyManager')) {
return false;
}
@ -399,7 +520,7 @@ class Visualization extends ViewDataTable
foreach ($this->config->clientSideProperties as $name) {
if (property_exists($this->requestConfig, $name)) {
$result[$name] = $this->getIntIfValueIsBool($this->requestConfig->$name);
} else if (property_exists($this->config, $name)) {
} elseif (property_exists($this->config, $name)) {
$result[$name] = $this->getIntIfValueIsBool($this->config->$name);
}
}
@ -453,7 +574,7 @@ class Visualization extends ViewDataTable
if (property_exists($this->requestConfig, $name)) {
$valueToConvert = $this->requestConfig->$name;
} else if (property_exists($this->config, $name)) {
} elseif (property_exists($this->config, $name)) {
$valueToConvert = $this->config->$name;
}
@ -481,6 +602,7 @@ class Visualization extends ViewDataTable
'filter_excludelowpop',
'filter_excludelowpop_value',
);
foreach ($deleteFromJavascriptVariables as $name) {
if (isset($javascriptVariablesToSet[$name])) {
unset($javascriptVariablesToSet[$name]);
@ -497,9 +619,11 @@ class Visualization extends ViewDataTable
/**
* Hook that is called before loading report data from the API.
*
*
* Use this method to change the request parameters that is sent to the API when requesting
* data.
*
* @api
*/
public function beforeLoadDataTable()
{
@ -507,9 +631,11 @@ class Visualization extends ViewDataTable
/**
* Hook that is executed before generic filters are applied.
*
*
* Use this method if you need access to the entire dataset (since generic filters will
* limit and truncate reports).
*
* @api
*/
public function beforeGenericFiltersAreAppliedToLoadedDataTable()
{
@ -517,6 +643,8 @@ class Visualization extends ViewDataTable
/**
* Hook that is executed after generic filters are applied.
*
* @api
*/
public function afterGenericFiltersAreAppliedToLoadedDataTable()
{
@ -525,6 +653,8 @@ class Visualization extends ViewDataTable
/**
* Hook that is executed after the report data is loaded and after all filters have been applied.
* Use this method to format the report data before the view is rendered.
*
* @api
*/
public function afterAllFiltersAreApplied()
{
@ -533,27 +663,24 @@ class Visualization extends ViewDataTable
/**
* Hook that is executed directly before rendering. Use this hook to force display properties to
* be a certain value, despite changes from plugins and query parameters.
*
* @api
*/
public function beforeRender()
{
// eg $this->config->showFooterColumns = true;
}
/**
* Second, generic filters (Sort, Limit, Replace Column Names, etc.)
*/
private function applyGenericFilters()
private function makeDataTablePostProcessor()
{
$requestArray = $this->request->getRequestArray();
$request = \Piwik\API\Request::getRequestArrayFromString($requestArray);
$request = $this->buildApiRequestArray();
$module = $this->requestConfig->getApiModuleToRequest();
$method = $this->requestConfig->getApiMethodToRequest();
if (false === $this->config->enable_sort) {
$request['filter_sort_column'] = '';
$request['filter_sort_order'] = '';
}
$processor = new DataTablePostProcessor($module, $method, $request);
$processor->setFormatter($this->metricsFormatter);
$genericFilter = new \Piwik\API\DataTableGenericFilter($request);
$genericFilter->filter($this->dataTable);
return $processor;
}
private function logMessageIfRequestPropertiesHaveChanged(array $requestPropertiesBefore)
@ -563,6 +690,15 @@ class Visualization extends ViewDataTable
$diff = array_diff_assoc($this->makeSureArrayContainsOnlyStrings($requestProperties),
$this->makeSureArrayContainsOnlyStrings($requestPropertiesBefore));
if (!empty($diff['filter_sort_column'])) {
// this here might be ok as it can be changed after data loaded but before filters applied
unset($diff['filter_sort_column']);
}
if (!empty($diff['filter_sort_order'])) {
// this here might be ok as it can be changed after data loaded but before filters applied
unset($diff['filter_sort_order']);
}
if (empty($diff)) {
return;
}
@ -592,4 +728,30 @@ class Visualization extends ViewDataTable
return $result;
}
/**
* @internal
*
* @return array
*/
public function buildApiRequestArray()
{
$requestArray = $this->request->getRequestArray();
$request = APIRequest::getRequestArrayFromString($requestArray);
if (false === $this->config->enable_sort) {
$request['filter_sort_column'] = '';
$request['filter_sort_order'] = '';
}
if (!array_key_exists('format_metrics', $request) || $request['format_metrics'] === 'bc') {
$request['format_metrics'] = '1';
}
if (!$this->requestConfig->disable_queued_filters && array_key_exists('disable_queued_filters', $request)) {
unset($request['disable_queued_filters']);
}
return $request;
}
}