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