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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
<?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\Plugins\Annotations;
use Exception;
use Piwik\Common;
use Piwik\Date;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Site;
/**
* This class can be used to query & modify annotations for multiple sites
* at once.
*
* Example use:
* $annotations = new AnnotationList($idSites = "1,2,5");
* $annotation = $annotations->get($idSite = 1, $idNote = 4);
* // do stuff w/ annotation
* $annotations->update($idSite = 2, $idNote = 4, $note = "This is the new text.");
* $annotations->save($idSite);
*
* Note: There is a concurrency issue w/ this code. If two users try to save
* an annotation for the same site, it's possible one of their changes will
* never get made (as it will be overwritten by the other's).
*
*/
class AnnotationList
{
const ANNOTATION_COLLECTION_OPTION_SUFFIX = '_annotations';
/**
* List of site IDs this instance holds annotations for.
*
* @var array
*/
private $idSites;
/**
* Array that associates lists of annotations with site IDs.
*
* @var array
*/
private $annotations;
/**
* Constructor. Loads annotations from the database.
*
* @param string|int $idSites The list of site IDs to load annotations for.
*/
public function __construct($idSites)
{
$this->idSites = Site::getIdSitesFromIdSitesString($idSites);
$this->annotations = $this->getAnnotationsForSite();
}
/**
* Returns the list of site IDs this list contains annotations for.
*
* @return array
*/
public function getIdSites()
{
return $this->idSites;
}
/**
* Creates a new annotation for a site. This method does not perist the result.
* To save the new annotation in the database, call $this->save.
*
* @param int $idSite The ID of the site to add an annotation to.
* @param string $date The date the annotation is in reference to.
* @param string $note The text of the new annotation.
* @param int $starred Either 1 or 0. If 1, the new annotation has been starred,
* otherwise it will start out unstarred.
* @return array The added annotation.
* @throws Exception if $idSite is not an ID that was supplied upon construction.
*/
public function add($idSite, $date, $note, $starred = 0)
{
$this->checkIdSiteIsLoaded($idSite);
$date = Date::factory($date)->toString('Y-m-d');
$this->annotations[$idSite][] = self::makeAnnotation($date, $note, $starred);
// get the id of the new annotation
end($this->annotations[$idSite]);
$newNoteId = key($this->annotations[$idSite]);
return $this->get($idSite, $newNoteId);
}
/**
* Persists the annotations list for a site, overwriting whatever exists.
*
* @param int $idSite The ID of the site to save annotations for.
* @throws Exception if $idSite is not an ID that was supplied upon construction.
*/
public function save($idSite)
{
$this->checkIdSiteIsLoaded($idSite);
$optionName = self::getAnnotationCollectionOptionName($idSite);
Option::set($optionName, serialize($this->annotations[$idSite]));
}
/**
* Modifies an annotation in this instance's collection of annotations.
*
* Note: This method does not perist the change in the DB. The save method must
* be called for that.
*
* @param int $idSite The ID of the site whose annotation will be updated.
* @param int $idNote The ID of the note.
* @param string|null $date The new date of the annotation, eg '2012-01-01'. If
* null, no change is made.
* @param string|null $note The new text of the annotation. If null, no change
* is made.
* @param int|null $starred Either 1 or 0, whether the annotation should be
* starred or not. If null, no change is made.
* @throws Exception if $idSite is not an ID that was supplied upon construction.
* @throws Exception if $idNote does not refer to valid note for the site.
*/
public function update($idSite, $idNote, $date = null, $note = null, $starred = null)
{
$this->checkIdSiteIsLoaded($idSite);
$this->checkNoteExists($idSite, $idNote);
$annotation =& $this->annotations[$idSite][$idNote];
if ($date !== null) {
$annotation['date'] = Date::factory($date)->toString('Y-m-d');
}
if ($note !== null) {
$annotation['note'] = $note;
}
if ($starred !== null) {
$annotation['starred'] = $starred;
}
}
/**
* Removes a note from a site's collection of annotations.
*
* Note: This method does not perist the change in the DB. The save method must
* be called for that.
*
* @param int $idSite The ID of the site whose annotation will be updated.
* @param int $idNote The ID of the note.
* @throws Exception if $idSite is not an ID that was supplied upon construction.
* @throws Exception if $idNote does not refer to valid note for the site.
*/
public function remove($idSite, $idNote)
{
$this->checkIdSiteIsLoaded($idSite);
$this->checkNoteExists($idSite, $idNote);
unset($this->annotations[$idSite][$idNote]);
}
/**
* Removes all notes for a single site.
*
* Note: This method does not perist the change in the DB. The save method must
* be called for that.
*
* @param int $idSite The ID of the site to get an annotation for.
* @throws Exception if $idSite is not an ID that was supplied upon construction.
*/
public function removeAll($idSite)
{
$this->checkIdSiteIsLoaded($idSite);
$this->annotations[$idSite] = array();
}
/**
* Retrieves an annotation by ID.
*
* This function returns an array with the following elements:
* - idNote: The ID of the annotation.
* - date: The date of the annotation.
* - note: The text of the annotation.
* - starred: 1 or 0, whether the annotation is stared;
* - user: (unless current user is anonymous) The user that created the annotation.
* - canEditOrDelete: True if the user can edit/delete the annotation.
*
* @param int $idSite The ID of the site to get an annotation for.
* @param int $idNote The ID of the note to get.
* @throws Exception if $idSite is not an ID that was supplied upon construction.
* @throws Exception if $idNote does not refer to valid note for the site.
*/
public function get($idSite, $idNote)
{
$this->checkIdSiteIsLoaded($idSite);
$this->checkNoteExists($idSite, $idNote);
$annotation = $this->annotations[$idSite][$idNote];
$this->augmentAnnotationData($idSite, $idNote, $annotation);
return $annotation;
}
/**
* Returns all annotations within a specific date range. The result is
* an array that maps site IDs with arrays of annotations within the range.
*
* Note: The date range is inclusive.
*
* @see self::get for info on what attributes stored within annotations.
*
* @param Date|bool $startDate The start of the date range.
* @param Date|bool $endDate The end of the date range.
* @param array|bool|int|string $idSite IDs of the sites whose annotations to
* search through.
* @return array Array mapping site IDs with arrays of annotations, eg:
* array(
* '5' => array(
* array(...), // annotation
* array(...), // annotation
* ...
* ),
* '6' => array(
* array(...), // annotation
* array(...), // annotation
* ...
* ),
* )
*/
public function search($startDate, $endDate, $idSite = false)
{
if ($idSite) {
$idSites = Site::getIdSitesFromIdSitesString($idSite);
} else {
$idSites = array_keys($this->annotations);
}
// collect annotations that are within the right date range & belong to the right
// site
$result = array();
foreach ($idSites as $idSite) {
if (!isset($this->annotations[$idSite])) {
continue;
}
foreach ($this->annotations[$idSite] as $idNote => $annotation) {
if ($startDate !== false) {
$annotationDate = Date::factory($annotation['date']);
if ($annotationDate->getTimestamp() < $startDate->getTimestamp()
|| $annotationDate->getTimestamp() > $endDate->getTimestamp()
) {
continue;
}
}
$this->augmentAnnotationData($idSite, $idNote, $annotation);
$result[$idSite][] = $annotation;
}
// sort by annotation date
if (!empty($result[$idSite])) {
uasort($result[$idSite], array($this, 'compareAnnotationDate'));
}
}
return $result;
}
/**
* Counts annotations & starred annotations within a date range and returns
* the counts. The date range includes the start date, but not the end date.
*
* @param int $idSite The ID of the site to count annotations for.
* @param string|false $startDate The start date of the range or false if no
* range check is desired.
* @param string|false $endDate The end date of the range or false if no
* range check is desired.
* @return array eg, array('count' => 5, 'starred' => 2)
*/
public function count($idSite, $startDate, $endDate)
{
$this->checkIdSiteIsLoaded($idSite);
// search includes end date, and count should not, so subtract one from the timestamp
$annotations = $this->search($startDate, Date::factory($endDate->getTimestamp() - 1));
// count the annotations
$count = $starred = 0;
if (!empty($annotations[$idSite])) {
$count = count($annotations[$idSite]);
foreach ($annotations[$idSite] as $annotation) {
if ($annotation['starred']) {
++$starred;
}
}
}
return array('count' => $count, 'starred' => $starred);
}
/**
* Utility function. Creates a new annotation.
*
* @param string $date
* @param string $note
* @param int $starred
* @return array
*/
private function makeAnnotation($date, $note, $starred = 0)
{
return array('date' => $date,
'note' => $note,
'starred' => (int)$starred,
'user' => Piwik::getCurrentUserLogin());
}
/**
* Retrieves annotations from the database for the sites supplied to the
* constructor.
*
* @return array Lists of annotations mapped by site ID.
*/
private function getAnnotationsForSite()
{
$result = array();
foreach ($this->idSites as $id) {
$optionName = self::getAnnotationCollectionOptionName($id);
$serialized = Option::get($optionName);
if ($serialized !== false) {
$result[$id] = Common::safe_unserialize($serialized);
if (empty($result[$id])) {
// in case unserialize failed
$result[$id] = array();
}
} else {
$result[$id] = array();
}
}
return $result;
}
/**
* Utility function that checks if a site ID was supplied and if not,
* throws an exception.
*
* We can only modify/read annotations for sites that we've actually
* loaded the annotations for.
*
* @param int $idSite
* @throws Exception
*/
private function checkIdSiteIsLoaded($idSite)
{
if (!in_array($idSite, $this->idSites)) {
throw new Exception("This AnnotationList was not initialized with idSite '$idSite'.");
}
}
/**
* Utility function that checks if a note exists for a site, and if not,
* throws an exception.
*
* @param int $idSite
* @param int $idNote
* @throws Exception
*/
private function checkNoteExists($idSite, $idNote)
{
if (empty($this->annotations[$idSite][$idNote])) {
throw new Exception("There is no note with id '$idNote' for site with id '$idSite'.");
}
}
/**
* Returns true if the current user can modify or delete a specific annotation.
*
* A user can modify/delete a note if the user has write access for the site OR
* the user has view access, is not the anonymous user and is the user that
* created the note in question.
*
* @param int $idSite The site ID the annotation belongs to.
* @param array $annotation The annotation.
* @return bool
*/
public static function canUserModifyOrDelete($idSite, $annotation)
{
// user can save if user is admin or if has view access, is not anonymous & is user who wrote note
$canEdit = Piwik::isUserHasWriteAccess($idSite)
|| (!Piwik::isUserIsAnonymous()
&& Piwik::getCurrentUserLogin() == $annotation['user']);
return $canEdit;
}
/**
* Adds extra data to an annotation, including the annotation's ID and whether
* the current user can edit or delete it.
*
* Also, if the current user is anonymous, the user attribute is removed.
*
* @param int $idSite
* @param int $idNote
* @param array $annotation
*/
private function augmentAnnotationData($idSite, $idNote, &$annotation)
{
$annotation['idNote'] = $idNote;
$annotation['canEditOrDelete'] = self::canUserModifyOrDelete($idSite, $annotation);
// we don't supply user info if the current user is anonymous
if (Piwik::isUserIsAnonymous()) {
unset($annotation['user']);
}
}
/**
* Utility function that compares two annotations.
*
* @param array $lhs An annotation.
* @param array $rhs An annotation.
* @return int -1, 0 or 1
*/
public function compareAnnotationDate($lhs, $rhs)
{
if ($lhs['date'] == $rhs['date']) {
return $lhs['idNote'] <= $rhs['idNote'] ? -1 : 1;
}
return $lhs['date'] < $rhs['date'] ? -1 : 1; // string comparison works because date format should be YYYY-MM-DD
}
/**
* Returns true if the current user can add notes for a specific site.
*
* @param int $idSite The site to add notes to.
* @return bool
*/
public static function canUserAddNotesFor($idSite)
{
return Piwik::isUserHasViewAccess($idSite)
&& !Piwik::isUserIsAnonymous();
}
/**
* Returns the option name used to store annotations for a site.
*
* @param int $idSite The site ID.
* @return string
*/
public static function getAnnotationCollectionOptionName($idSite)
{
return $idSite . self::ANNOTATION_COLLECTION_OPTION_SUFFIX;
}
}
|