PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/ArchiveProcessor/PluginsArchiver.php

https://github.com/CodeYellowBV/piwik
PHP | 196 lines | 133 code | 27 blank | 36 comment | 12 complexity | 19fbb51805a60ee4537eae476b4f775c MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\ArchiveProcessor;
  10. use Piwik\Archive;
  11. use Piwik\ArchiveProcessor;
  12. use Piwik\DataAccess\ArchiveWriter;
  13. use Piwik\DataTable\Manager;
  14. use Piwik\Metrics;
  15. use Piwik\Plugin\Archiver;
  16. /**
  17. * This class creates the Archiver objects found in plugins and will trigger aggregation,
  18. * so each plugin can process their reports.
  19. */
  20. class PluginsArchiver
  21. {
  22. /**
  23. * @param ArchiveProcessor $archiveProcessor
  24. */
  25. public $archiveProcessor;
  26. /**
  27. * @var Parameters
  28. */
  29. protected $params;
  30. /**
  31. * @var Archiver[] $archivers
  32. */
  33. private static $archivers = array();
  34. public function __construct(Parameters $params, $isTemporaryArchive)
  35. {
  36. $this->params = $params;
  37. $this->archiveWriter = new ArchiveWriter($this->params, $isTemporaryArchive);
  38. $this->archiveWriter->initNewArchive();
  39. $this->archiveProcessor = new ArchiveProcessor($this->params, $this->archiveWriter);
  40. $this->isSingleSiteDayArchive = $this->params->isSingleSiteDayArchive();
  41. }
  42. /**
  43. * If period is day, will get the core metrics (including visits) from the logs.
  44. * If period is != day, will sum the core metrics from the existing archives.
  45. * @return array Core metrics
  46. */
  47. public function callAggregateCoreMetrics()
  48. {
  49. if($this->isSingleSiteDayArchive) {
  50. $metrics = $this->aggregateDayVisitsMetrics();
  51. } else {
  52. $metrics = $this->aggregateMultipleVisitsMetrics();
  53. }
  54. if (empty($metrics)) {
  55. return array(
  56. 'nb_visits' => false,
  57. 'nb_visits_converted' => false
  58. );
  59. }
  60. return array(
  61. 'nb_visits' => $metrics['nb_visits'],
  62. 'nb_visits_converted' => $metrics['nb_visits_converted']
  63. );
  64. }
  65. /**
  66. * Instantiates the Archiver class in each plugin that defines it,
  67. * and triggers Aggregation processing on these plugins.
  68. */
  69. public function callAggregateAllPlugins($visits, $visitsConverted)
  70. {
  71. $this->archiveProcessor->setNumberOfVisits($visits, $visitsConverted);
  72. $archivers = $this->getPluginArchivers();
  73. foreach($archivers as $pluginName => $archiverClass) {
  74. // We clean up below all tables created during this function call (and recursive calls)
  75. $latestUsedTableId = Manager::getInstance()->getMostRecentTableId();
  76. /** @var Archiver $archiver */
  77. $archiver = new $archiverClass($this->archiveProcessor);
  78. if(!$archiver->isEnabled()) {
  79. continue;
  80. }
  81. if($this->shouldProcessReportsForPlugin($pluginName)) {
  82. if($this->isSingleSiteDayArchive) {
  83. $archiver->aggregateDayReport();
  84. } else {
  85. $archiver->aggregateMultipleReports();
  86. }
  87. }
  88. Manager::getInstance()->deleteAll($latestUsedTableId);
  89. unset($archiver);
  90. }
  91. }
  92. public function finalizeArchive()
  93. {
  94. $this->params->logStatusDebug( $this->archiveWriter->isArchiveTemporary );
  95. $this->archiveWriter->finalizeArchive();
  96. return $this->archiveWriter->getIdArchive();
  97. }
  98. /**
  99. * Loads Archiver class from any plugin that defines one.
  100. *
  101. * @return \Piwik\Plugin\Archiver[]
  102. */
  103. protected function getPluginArchivers()
  104. {
  105. if (empty(static::$archivers)) {
  106. $pluginNames = \Piwik\Plugin\Manager::getInstance()->getActivatedPlugins();
  107. $archivers = array();
  108. foreach ($pluginNames as $pluginName) {
  109. $archivers[$pluginName] = self::getPluginArchiverClass($pluginName);
  110. }
  111. static::$archivers = array_filter($archivers);
  112. }
  113. return static::$archivers;
  114. }
  115. private static function getPluginArchiverClass($pluginName)
  116. {
  117. $klassName = 'Piwik\\Plugins\\' . $pluginName . '\\Archiver';
  118. if (class_exists($klassName)
  119. && is_subclass_of($klassName, 'Piwik\\Plugin\\Archiver')) {
  120. return $klassName;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Whether the specified plugin's reports should be archived
  126. * @param string $pluginName
  127. * @return bool
  128. */
  129. protected function shouldProcessReportsForPlugin($pluginName)
  130. {
  131. if ($this->params->getRequestedPlugin() == $pluginName) {
  132. return true;
  133. }
  134. if (Rules::shouldProcessReportsAllPlugins(
  135. $this->params->getIdSites(),
  136. $this->params->getSegment(),
  137. $this->params->getPeriod()->getLabel())) {
  138. return true;
  139. }
  140. if (!\Piwik\Plugin\Manager::getInstance()->isPluginLoaded($this->params->getRequestedPlugin())) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. protected function aggregateDayVisitsMetrics()
  146. {
  147. $query = $this->archiveProcessor->getLogAggregator()->queryVisitsByDimension();
  148. $data = $query->fetch();
  149. $metrics = $this->convertMetricsIdToName($data);
  150. $this->archiveProcessor->insertNumericRecords($metrics);
  151. return $metrics;
  152. }
  153. protected function convertMetricsIdToName($data)
  154. {
  155. $metrics = array();
  156. foreach ($data as $metricId => $value) {
  157. $readableMetric = Metrics::$mappingFromIdToName[$metricId];
  158. $metrics[$readableMetric] = $value;
  159. }
  160. return $metrics;
  161. }
  162. protected function aggregateMultipleVisitsMetrics()
  163. {
  164. $toSum = Metrics::getVisitsMetricNames();
  165. $metrics = $this->archiveProcessor->aggregateNumericMetrics($toSum);
  166. return $metrics;
  167. }
  168. }