ArchivingStatus.php
2 years ago
Loader.php
1 year ago
LoaderLock.php
1 year ago
Parameters.php
1 year ago
PluginsArchiver.php
1 year ago
PluginsArchiverException.php
2 years ago
Record.php
1 year ago
RecordBuilder.php
1 year ago
Rules.php
1 year ago
Parameters.php
278 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\ArchiveProcessor; |
| 10 | |
| 11 | use Piwik\Date; |
| 12 | use Piwik\Log; |
| 13 | use Piwik\Period; |
| 14 | use Piwik\Piwik; |
| 15 | use Piwik\Segment; |
| 16 | use Piwik\Site; |
| 17 | /** |
| 18 | * Contains the analytics parameters for the reports that are currently being archived. The analytics |
| 19 | * parameters include the **website** the reports describe, the **period** of time the reports describe |
| 20 | * and the **segment** used to limit the visit set. |
| 21 | */ |
| 22 | class Parameters |
| 23 | { |
| 24 | /** |
| 25 | * @var Site |
| 26 | */ |
| 27 | private $site = null; |
| 28 | /** |
| 29 | * @var Period |
| 30 | */ |
| 31 | private $period = null; |
| 32 | /** |
| 33 | * @var Segment |
| 34 | */ |
| 35 | private $segment = null; |
| 36 | /** |
| 37 | * @var string Plugin name which triggered this archive processor |
| 38 | */ |
| 39 | private $requestedPlugin = \false; |
| 40 | private $onlyArchiveRequestedPlugin = \false; |
| 41 | /** |
| 42 | * @var string |
| 43 | */ |
| 44 | private $archiveOnlyReport = null; |
| 45 | /** |
| 46 | * @var bool |
| 47 | */ |
| 48 | private $isArchiveOnlyReportHandled; |
| 49 | /** |
| 50 | * @var string[]|null |
| 51 | */ |
| 52 | private $foundRequestedReports; |
| 53 | /** |
| 54 | * Constructor. |
| 55 | * |
| 56 | * @ignore |
| 57 | */ |
| 58 | public function __construct(Site $site, Period $period, Segment $segment) |
| 59 | { |
| 60 | $this->site = $site; |
| 61 | $this->period = $period; |
| 62 | $this->segment = $segment; |
| 63 | } |
| 64 | /** |
| 65 | * If we want to archive only a single report, we can request that via this method. |
| 66 | * It is up to each plugin's archiver to respect the setting. |
| 67 | * |
| 68 | * @param string|string[] $archiveOnlyReport |
| 69 | * @api |
| 70 | */ |
| 71 | public function setArchiveOnlyReport($archiveOnlyReport) |
| 72 | { |
| 73 | $this->archiveOnlyReport = $archiveOnlyReport; |
| 74 | } |
| 75 | /** |
| 76 | * Gets the report we want to archive specifically, or null if none was specified. |
| 77 | * |
| 78 | * @return string|null |
| 79 | * @api |
| 80 | */ |
| 81 | public function getArchiveOnlyReport() |
| 82 | { |
| 83 | return $this->archiveOnlyReport; |
| 84 | } |
| 85 | /** |
| 86 | * @ignore |
| 87 | */ |
| 88 | public function setRequestedPlugin($plugin) |
| 89 | { |
| 90 | $this->requestedPlugin = $plugin; |
| 91 | } |
| 92 | /** |
| 93 | * @ignore |
| 94 | */ |
| 95 | public function onlyArchiveRequestedPlugin() |
| 96 | { |
| 97 | $this->onlyArchiveRequestedPlugin = \true; |
| 98 | } |
| 99 | /** |
| 100 | * @ignore |
| 101 | */ |
| 102 | public function shouldOnlyArchiveRequestedPlugin() |
| 103 | { |
| 104 | return $this->onlyArchiveRequestedPlugin; |
| 105 | } |
| 106 | /** |
| 107 | * @ignore |
| 108 | */ |
| 109 | public function getRequestedPlugin() |
| 110 | { |
| 111 | return $this->requestedPlugin; |
| 112 | } |
| 113 | /** |
| 114 | * Returns the period we are computing statistics for. |
| 115 | * |
| 116 | * @return Period |
| 117 | * @api |
| 118 | */ |
| 119 | public function getPeriod() |
| 120 | { |
| 121 | return $this->period; |
| 122 | } |
| 123 | /** |
| 124 | * Returns the array of Period which make up this archive. |
| 125 | * |
| 126 | * @return \Piwik\Period[] |
| 127 | * @ignore |
| 128 | */ |
| 129 | public function getSubPeriods() |
| 130 | { |
| 131 | if ($this->getPeriod()->getLabel() == 'day') { |
| 132 | return array($this->getPeriod()); |
| 133 | } |
| 134 | return $this->getPeriod()->getSubperiods(); |
| 135 | } |
| 136 | /** |
| 137 | * @return array |
| 138 | * @ignore |
| 139 | */ |
| 140 | public function getIdSites() |
| 141 | { |
| 142 | $idSite = $this->getSite()->getId(); |
| 143 | $idSites = array($idSite); |
| 144 | Piwik::postEvent('ArchiveProcessor.Parameters.getIdSites', array(&$idSites, $this->getPeriod())); |
| 145 | return $idSites; |
| 146 | } |
| 147 | /** |
| 148 | * Returns the site we are computing statistics for. |
| 149 | * |
| 150 | * @return Site |
| 151 | * @api |
| 152 | */ |
| 153 | public function getSite() |
| 154 | { |
| 155 | return $this->site; |
| 156 | } |
| 157 | /** |
| 158 | * The Segment used to limit the set of visits that are being aggregated. |
| 159 | * |
| 160 | * @return Segment |
| 161 | * @api |
| 162 | */ |
| 163 | public function getSegment() |
| 164 | { |
| 165 | return $this->segment; |
| 166 | } |
| 167 | /** |
| 168 | * Returns the end day of the period in the site's timezone. |
| 169 | * |
| 170 | * @return Date |
| 171 | */ |
| 172 | public function getDateEnd() |
| 173 | { |
| 174 | return $this->getPeriod()->getDateEnd()->setTimezone($this->getSite()->getTimezone()); |
| 175 | } |
| 176 | /** |
| 177 | * Returns the start day of the period in the site's timezone. |
| 178 | * |
| 179 | * @return Date |
| 180 | */ |
| 181 | public function getDateStart() |
| 182 | { |
| 183 | return $this->getPeriod()->getDateStart()->setTimezone($this->getSite()->getTimezone()); |
| 184 | } |
| 185 | /** |
| 186 | * Returns the start day of the period in the site's timezone (includes the time of day). |
| 187 | * |
| 188 | * @return Date |
| 189 | */ |
| 190 | public function getDateTimeStart() |
| 191 | { |
| 192 | return $this->getPeriod()->getDateTimeStart()->setTimezone($this->getSite()->getTimezone()); |
| 193 | } |
| 194 | /** |
| 195 | * Returns the end day of the period in the site's timezone (includes the time of day). |
| 196 | * |
| 197 | * @return Date |
| 198 | */ |
| 199 | public function getDateTimeEnd() |
| 200 | { |
| 201 | return $this->getPeriod()->getDateTimeEnd()->setTimezone($this->getSite()->getTimezone()); |
| 202 | } |
| 203 | /** |
| 204 | * @return bool |
| 205 | */ |
| 206 | public function isSingleSiteDayArchive() |
| 207 | { |
| 208 | return $this->isDayArchive() && $this->isSingleSite(); |
| 209 | } |
| 210 | /** |
| 211 | * @return bool |
| 212 | */ |
| 213 | public function isDayArchive() |
| 214 | { |
| 215 | $period = $this->getPeriod(); |
| 216 | $secondsInPeriod = $period->getDateEnd()->getTimestampUTC() - $period->getDateStart()->getTimestampUTC(); |
| 217 | $oneDay = $secondsInPeriod < Date::NUM_SECONDS_IN_DAY; |
| 218 | return $oneDay; |
| 219 | } |
| 220 | public function isSingleSite() |
| 221 | { |
| 222 | return count($this->getIdSites()) == 1; |
| 223 | } |
| 224 | public function logStatusDebug() |
| 225 | { |
| 226 | $temporary = 'definitive archive'; |
| 227 | Log::debug("%s archive, idSite = %d (%s), segment '%s', plugin = '%s', report = '%s', UTC datetime [%s -> %s]", $this->getPeriod()->getLabel(), $this->getSite()->getId(), $temporary, $this->getSegment()->getString(), $this->getRequestedPlugin(), $this->getArchiveOnlyReport(), $this->getDateStart()->getDateStartUTC(), $this->getDateEnd()->getDateEndUTC()); |
| 228 | } |
| 229 | public function __toString() |
| 230 | { |
| 231 | $requestedReports = $this->getArchiveOnlyReport(); |
| 232 | if (is_array($requestedReports)) { |
| 233 | $requestedReports = implode(', ', $requestedReports); |
| 234 | } |
| 235 | return "[idSite = {$this->getSite()->getId()}, period = {$this->getPeriod()->getLabel()} {$this->getPeriod()->getRangeString()}, segment = {$this->getSegment()->getString()}, plugin = {$this->getRequestedPlugin()}, report = {$requestedReports}]"; |
| 236 | } |
| 237 | /** |
| 238 | * Returns whether the setArchiveOnlyReport() was handled by an Archiver. |
| 239 | * |
| 240 | * @return bool |
| 241 | */ |
| 242 | public function isPartialArchive() |
| 243 | { |
| 244 | if (!$this->getRequestedPlugin()) { |
| 245 | // sanity check, partial archives are only for single reports |
| 246 | return \false; |
| 247 | } |
| 248 | return $this->isArchiveOnlyReportHandled; |
| 249 | } |
| 250 | /** |
| 251 | * If a plugin's archiver handles the setArchiveOnlyReport() setting, it should call this method |
| 252 | * so it is known that the archive only contains the requested report. This should be called |
| 253 | * in an Archiver's __construct method. |
| 254 | * |
| 255 | * @param bool $isArchiveOnlyReportHandled |
| 256 | */ |
| 257 | public function setIsPartialArchive($isArchiveOnlyReportHandled) |
| 258 | { |
| 259 | $this->isArchiveOnlyReportHandled = $isArchiveOnlyReportHandled; |
| 260 | } |
| 261 | public function getArchiveOnlyReportAsArray() |
| 262 | { |
| 263 | $requestedReport = $this->getArchiveOnlyReport(); |
| 264 | if (empty($requestedReport)) { |
| 265 | return []; |
| 266 | } |
| 267 | return is_array($requestedReport) ? $requestedReport : [$requestedReport]; |
| 268 | } |
| 269 | public function setFoundRequestedReports(array $foundRecords) |
| 270 | { |
| 271 | $this->foundRequestedReports = $foundRecords; |
| 272 | } |
| 273 | public function getFoundRequestedReports() |
| 274 | { |
| 275 | return $this->foundRequestedReports ?: []; |
| 276 | } |
| 277 | } |
| 278 |