From 525e916fdddcf1bf01db2cfff049f525c0392d47 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Mon, 25 Sep 2017 21:14:04 +0100 Subject: [PATCH] First set of classes and process to read in the iCal file and output all events to the console --- .gitignore | 2 + import.php | 64 +++++++++++++++++++++++++++ includes/Event.php | 90 ++++++++++++++++++++++++++++++++++++++ includes/FileReader.php | 56 ++++++++++++++++++++++++ includes/ICSFileReader.php | 88 +++++++++++++++++++++++++++++++++++++ 5 files changed, 300 insertions(+) create mode 100644 .gitignore create mode 100644 import.php create mode 100644 includes/Event.php create mode 100644 includes/FileReader.php create mode 100644 includes/ICSFileReader.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5a8c8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +*.ics \ No newline at end of file diff --git a/import.php b/import.php new file mode 100644 index 0000000..5789355 --- /dev/null +++ b/import.php @@ -0,0 +1,64 @@ + ' . PHP_EOL, $argv[0]); + exit(1); +} + +try +{ + $pathToICSFile = trim($argv[1]); + $pathToDrupalConfig = trim($argv[2]); + + $icsReader = new ICSFileReader($pathToICSFile); + $drupalReader = new FileReader($pathToDrupalConfig); + + echo sprintf('Reading iCal data from %s' . PHP_EOL, $icsReader->getPath()); + echo sprintf('Using Drupal connection details from %s' . PHP_EOL, $drupalReader->getPath()); + echo '---' . PHP_EOL; + + $events = $icsReader->getEvents(); + + echo sprintf('%d event%s found' . PHP_EOL, count($events), count($events) == 1 ? '' : 's'); + echo '---' . PHP_EOL; + + /** @var Event $event */ + $index = 0; + foreach ($events as $event) + { + echo sprintf('%d: %s | %s' . PHP_EOL, $index, $event->getUid(), $event->getStartDate()->format('H:i:s d/M/Y')); + echo $event->getTitle() . PHP_EOL; + + if (strlen($event->getDescription())) + { + echo PHP_EOL . $event->getDescription() . PHP_EOL; + } + + echo '---' . PHP_EOL; + $index++; + } +} +catch (\Exception $e) +{ + echo sprintf('Caught exception: %s' . PHP_EOL, $e->getMessage()); + echo $e->getTraceAsString(); +} \ No newline at end of file diff --git a/includes/Event.php b/includes/Event.php new file mode 100644 index 0000000..b9926a2 --- /dev/null +++ b/includes/Event.php @@ -0,0 +1,90 @@ +description; + } + + /** + * @param string $description + */ + public function setDescription($description) + { + $this->description = $description; + } + + /** + * @return \DateTime + */ + public function getStartDate() + { + return $this->startDate; + } + + /** + * @param \DateTime $startDate + */ + public function setStartDate($startDate) + { + $this->startDate = $startDate; + } + + /** + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param string $title + */ + public function setTitle($title) + { + $this->title = $title; + } + + /** + * @return string + */ + public function getUid() + { + return $this->uid; + } + + /** + * @param string $uid + */ + public function setUid($uid) + { + $this->uid = $uid; + } +} \ No newline at end of file diff --git a/includes/FileReader.php b/includes/FileReader.php new file mode 100644 index 0000000..027243d --- /dev/null +++ b/includes/FileReader.php @@ -0,0 +1,56 @@ +path = substr($filePath, 0, 1) == DIRECTORY_SEPARATOR + ? $filePath + : sprintf('%s/%s', getcwd(), $filePath); + } + + public function __destruct() + { + if (!is_null($this->handle)) + { + @fclose($this->handle); + } + } + + public function getPath() + { + return $this->path; + } + + public function read() + { + $this->open(); + $line = fgets($this->handle); + + if ($line !== false) + { + $line = rtrim($line); + } + + return $line; + } + + private function open() + { + global $php_errormsg; + + if (is_null($this->handle)) + { + $this->handle = @fopen($this->path, 'r'); + if (!is_resource($this->handle)) + { + throw new \Exception(sprintf('Error reading file "%s": %s', basename($this->path), $php_errormsg)); + } + } + } +} \ No newline at end of file diff --git a/includes/ICSFileReader.php b/includes/ICSFileReader.php new file mode 100644 index 0000000..cce1620 --- /dev/null +++ b/includes/ICSFileReader.php @@ -0,0 +1,88 @@ +read()) !== false) + { + if (defined('DEBUG')) + { + echo sprintf('L%d: %s', $lineCount + 1, $line) . PHP_EOL; + } + + // First line should be BEGIN:VCALENDAR + if ($lineCount === 0 && trim($line) !== self::HEADER_VCALENDAR_BEGIN) + { + throw new \Exception('The file does not appear to be a valid iCal file'); + } + + if ($lineCount > 0) + { + if ($line == self::HEADER_VEVENT_BEGIN) + { + $this->currentEvent = new Event(); + } + else if ($line == self::HEADER_VEVENT_END) + { + $events[] = $this->currentEvent; + } + else if (!is_null($this->currentEvent)) + { + if (substr($line, 0, strlen(self::HEADER_UID)) == self::HEADER_UID) + { + $this->currentEvent->setUid(trim(substr($line, strlen(self::HEADER_UID)))); + } + else if (substr($line, 0, strlen(self::HEADER_TITLE)) == self::HEADER_TITLE) + { + $this->currentEvent->setTitle(trim(substr($line, strlen(self::HEADER_TITLE)))); + } + else if (substr($line, 0, strlen(self::HEADER_DATE)) == self::HEADER_DATE) + { + $date = \DateTime::createFromFormat('Ymd\\THis\\Z', trim(substr($line, strlen(self::HEADER_DATE)))); + $this->currentEvent->setStartDate($date); + } + else if (substr($line, 0, strlen(self::HEADER_DESCRIPTION)) == self::HEADER_DESCRIPTION) + { + $this->isDescriptionStarted = true; + $description = trim(substr($line, strlen(self::HEADER_DESCRIPTION))); + + while (preg_match('/^\s/', $line = $this->read())) + { + $description .= ' ' . trim($line); + } + + $this->currentEvent->setDescription($description); + } + } + } + + $lineCount++; + } + + return $events; + } +} \ No newline at end of file