1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik;
use Piwik\Container\StaticContainer;
use Piwik\Period\Factory as PeriodFactory;
use Piwik\Period\Range;
use Piwik\Translation\Translator;
/**
* Date range representation.
*
* Piwik allows users to view aggregated statistics for single days and for date
* ranges consisting of several days. When requesting data, a **date** string and
* a **period** string must be used to specify the date range that the data regards.
* This is the class Piwik uses to represent and manipulate those date ranges.
*
* There are five types of periods in Piwik: day, week, month, year and range,
* where **range** is any date range. The reason the other periods exist instead
* of just **range** is that Piwik will pre-archive reports for days, weeks, months
* and years, while every custom date range is archived on-demand.
*
* @api
*/
abstract class Period
{
/**
* Array of subperiods
* @var Period[]
*/
protected $subperiods = array();
protected $subperiodsProcessed = false;
/**
* @var string
*/
protected $label = null;
/**
* @var Date
*/
protected $date = null;
/**
* @var Translator
*/
protected $translator;
/**
* Constructor.
*
* @param Date $date
* @ignore
*/
public function __construct(Date $date)
{
$this->date = clone $date;
$this->translator = StaticContainer::get('Piwik\Translation\Translator');
}
/**
* @deprecated Use Factory::build instead
* @param $period
* @param $date
* @return Period
*/
public static function factory($period, $date)
{
return PeriodFactory::build($period, $date);
}
/**
* Returns true if `$dateString` and `$period` represent multiple periods.
*
* Will return true for date/period combinations where date references multiple
* dates and period is not `'range'`. For example, will return true for:
*
* - **date** = `2012-01-01,2012-02-01` and **period** = `'day'`
* - **date** = `2012-01-01,2012-02-01` and **period** = `'week'`
* - **date** = `last7` and **period** = `'month'`
*
* etc.
*
* @static
* @param $dateString string The **date** query parameter value.
* @param $period string The **period** query parameter value.
* @return boolean
*/
public static function isMultiplePeriod($dateString, $period)
{
return is_string($dateString)
&& (preg_match('/^(last|previous){1}([0-9]*)$/D', $dateString, $regs)
|| Range::parseDateRange($dateString))
&& $period != 'range';
}
/**
* Returns the first day of the period.
*
* @return Date
*/
public function getDateStart()
{
$this->generate();
if (count($this->subperiods) == 0) {
return $this->getDate();
}
$periods = $this->getSubperiods();
/** @var $currentPeriod Period */
$currentPeriod = $periods[0];
while ($currentPeriod->getNumberOfSubperiods() > 0) {
$periods = $currentPeriod->getSubperiods();
$currentPeriod = $periods[0];
}
return $currentPeriod->getDate();
}
/**
* Returns the last day of the period.
*
* @return Date
*/
public function getDateEnd()
{
$this->generate();
if (count($this->subperiods) == 0) {
return $this->getDate();
}
$periods = $this->getSubperiods();
/** @var $currentPeriod Period */
$currentPeriod = $periods[count($periods) - 1];
while ($currentPeriod->getNumberOfSubperiods() > 0) {
$periods = $currentPeriod->getSubperiods();
$currentPeriod = $periods[count($periods) - 1];
}
return $currentPeriod->getDate();
}
/**
* Returns the period ID.
*
* @return int A unique integer for this type of period.
*/
public function getId()
{
return Piwik::$idPeriods[$this->getLabel()];
}
/**
* Returns the label for the current period.
*
* @return string `"day"`, `"week"`, `"month"`, `"year"`, `"range"`
*/
public function getLabel()
{
return $this->label;
}
/**
* @return Date
*/
protected function getDate()
{
return $this->date;
}
protected function generate()
{
$this->subperiodsProcessed = true;
}
/**
* Returns the number of available subperiods.
*
* @return int
*/
public function getNumberOfSubperiods()
{
$this->generate();
return count($this->subperiods);
}
/**
* Returns the set of Period instances that together make up this period. For a year,
* this would be 12 months. For a month this would be 28-31 days. Etc.
*
* @return Period[]
*/
public function getSubperiods()
{
$this->generate();
return $this->subperiods;
}
/**
* Add a date to the period.
*
* Protected because adding periods after initialization is not supported.
*
* @param \Piwik\Period $period Valid Period object
* @ignore
*/
protected function addSubperiod($period)
{
$this->subperiods[] = $period;
}
/**
* Returns a list of strings representing the current period.
*
* @param string $format The format of each individual day.
* @return array An array of string dates that this period consists of.
*/
public function toString($format = "Y-m-d")
{
$this->generate();
$dateString = array();
foreach ($this->subperiods as $period) {
$dateString[] = $period->toString($format);
}
return $dateString;
}
/**
* See {@link toString()}.
*
* @return string
*/
public function __toString()
{
return implode(",", $this->toString());
}
/**
* Returns a pretty string describing this period.
*
* @return string
*/
abstract public function getPrettyString();
/**
* Returns a short string description of this period that is localized with the currently used
* language.
*
* @return string
*/
abstract public function getLocalizedShortString();
/**
* Returns a long string description of this period that is localized with the currently used
* language.
*
* @return string
*/
abstract public function getLocalizedLongString();
/**
* Returns the date range string comprising two dates
*
* @return string eg, `'2012-01-01,2012-01-31'`.
*/
public function getRangeString()
{
$dateStart = $this->getDateStart();
$dateEnd = $this->getDateEnd();
return $dateStart->toString("Y-m-d") . "," . $dateEnd->toString("Y-m-d");
}
}
|