First set of classes and process to read in the iCal file and output all events to the console
This commit is contained in:
parent
60ae167b20
commit
525e916fdd
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
*.ics
|
64
import.php
Normal file
64
import.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Pandy06269\iCalImporter;
|
||||
|
||||
use Pandy06269\iCalImporter\includes\Event;
|
||||
use Pandy06269\iCalImporter\includes\FileReader;
|
||||
use Pandy06269\iCalImporter\includes\ICSFileReader;
|
||||
|
||||
//define('DEBUG', true);
|
||||
|
||||
function autoload_include_file($class)
|
||||
{
|
||||
$class = substr($class, strlen('Pandy06269\\iCalImporter\\'));
|
||||
|
||||
$filePath = sprintf('%s/%s.php', __DIR__, str_replace('\\', DIRECTORY_SEPARATOR, $class));
|
||||
require_once $filePath;
|
||||
}
|
||||
|
||||
spl_autoload_register('\Pandy06269\iCalImporter\autoload_include_file');
|
||||
|
||||
if ($argc !== 3)
|
||||
{
|
||||
echo sprintf('Usage: %s <path to ical file> <path to Drupal config file>' . 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();
|
||||
}
|
90
includes/Event.php
Normal file
90
includes/Event.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Pandy06269\iCalImporter\includes;
|
||||
|
||||
class Event
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @var \DateTime
|
||||
*/
|
||||
private $startDate;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $title;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $uid;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
56
includes/FileReader.php
Normal file
56
includes/FileReader.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Pandy06269\iCalImporter\includes;
|
||||
|
||||
class FileReader
|
||||
{
|
||||
protected $handle;
|
||||
protected $path;
|
||||
|
||||
public function __construct($filePath)
|
||||
{
|
||||
$this->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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
88
includes/ICSFileReader.php
Normal file
88
includes/ICSFileReader.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Pandy06269\iCalImporter\includes;
|
||||
|
||||
class ICSFileReader extends FileReader
|
||||
{
|
||||
const HEADER_VCALENDAR_BEGIN = 'BEGIN:VCALENDAR';
|
||||
const HEADER_VEVENT_BEGIN = 'BEGIN:VEVENT';
|
||||
const HEADER_VEVENT_END = 'END:VEVENT';
|
||||
const HEADER_DATE = 'DTSTART:';
|
||||
const HEADER_DESCRIPTION = 'DESCRIPTION:';
|
||||
const HEADER_TITLE = 'SUMMARY:';
|
||||
const HEADER_UID = 'UID:';
|
||||
|
||||
/**
|
||||
* @var Event
|
||||
*/
|
||||
private $currentEvent = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isDescriptionStarted = false;
|
||||
|
||||
public function getEvents()
|
||||
{
|
||||
$events = [];
|
||||
$lineCount = 0;
|
||||
|
||||
while (($line = $this->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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user