1#!/usr/bin/env php
2<?php
3
4use Sabre\VObject;
5
6if ($argc<2) {
7    $cmd = $argv[0];
8    fwrite(STDERR, <<<HI
9Fruux test data generator
10
11This script generates a lot of test data. This is used for profiling and stuff.
12Currently it just generates events in a single calendar.
13
14The iCalendar output goes to stdout. Other messages to stderr.
15
16{$cmd} [events]
17
18
19HI
20    );
21    die();
22}
23
24$events = 100;
25
26if (isset($argv[1])) $events = (int)$argv[1];
27
28include __DIR__ . '/../vendor/autoload.php';
29
30fwrite(STDERR, "Generating " . $events . " events\n");
31
32$currentDate = new DateTime('-' . round($events/2) .  ' days');
33
34$calendar = VObject\Component::create('VCALENDAR');
35$calendar->version = '2.0';
36$calendar->calscale = 'GREGORIAN';
37
38$ii=0;
39
40while($ii < $events) {
41
42    $ii++;
43
44    $event = VObject\Component::create('VEVENT');
45    $event->DTSTART = 'bla';
46    $event->SUMMARY = 'Event #' . $ii;
47    $event->UID = md5(microtime(true));
48
49    $doctorRandom = mt_rand(1,1000);
50
51    switch($doctorRandom) {
52        // All-day event
53        case 1 :
54            $event->DTEND = 'bla';
55            $dtStart = clone $currentDate;
56            $dtEnd = clone $currentDate;
57            $dtEnd->modify('+' . mt_rand(1,3) . ' days');
58            $event->DTSTART->setDateTime($dtStart, VObject\Property\DateTime::DATE);
59            $event->DTEND->setDateTime($dtEnd, VObject\Property\DateTime::DATE);
60            break;
61        case 2 :
62            $event->RRULE = 'FREQ=DAILY;COUNT=' . mt_rand(1,10);
63            // No break intentional
64        default :
65            $dtStart = clone $currentDate;
66            $dtStart->setTime(mt_rand(1,23), mt_rand(0,59), mt_rand(0,59));
67            $event->DTSTART->setDateTime($dtStart, VObject\Property\DateTime::UTC);
68            $event->DURATION = 'PT'.mt_rand(1,3).'H';
69            break;
70
71    }
72
73    $calendar->add($event);
74    $currentDate->modify('+ ' . mt_rand(0,3) . ' days');
75
76}
77fwrite(STDERR, "Validating\n");
78
79$result = $calendar->validate();
80if ($result) {
81    fwrite(STDERR, "Errors!\n");
82    fwrite(STDERR, print_r($result,true));
83    die(-1);
84}
85
86fwrite(STDERR, "Serializing this beast\n");
87
88echo $calendar->serialize();
89
90fwrite(STDERR, "done.\n");
91
92