From 42721e95726559b4a601240bb5b0fe4e5d755b2a Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 27 Nov 2019 23:43:21 +0100 Subject: Added Schedule parsing and replaced old Fahrplan importer Resolves #553 (Change Frab Import from xCal to XML) Resolves #538 (Feature Request: Multi Frab Import) --- src/Helpers/Schedule/XmlParser.php | 172 +++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/Helpers/Schedule/XmlParser.php (limited to 'src/Helpers/Schedule/XmlParser.php') diff --git a/src/Helpers/Schedule/XmlParser.php b/src/Helpers/Schedule/XmlParser.php new file mode 100644 index 00000000..1492aaca --- /dev/null +++ b/src/Helpers/Schedule/XmlParser.php @@ -0,0 +1,172 @@ +scheduleXML = simplexml_load_string($xml); + + if (!$this->scheduleXML) { + return false; + } + + $this->parseXml(); + + return true; + } + + /** + * Parse the predefined XML content + */ + protected function parseXml(): void + { + $version = $this->getFirstXpathContent('version'); + $conference = new Conference( + $this->getFirstXpathContent('conference/title'), + $this->getFirstXpathContent('conference/acronym'), + $this->getFirstXpathContent('conference/start'), + $this->getFirstXpathContent('conference/end'), + (int)$this->getFirstXpathContent('conference/days'), + $this->getFirstXpathContent('conference/timeslot_duration'), + $this->getFirstXpathContent('conference/base_url') + ); + $days = []; + + foreach ($this->scheduleXML->xpath('day') as $day) { + $rooms = []; + + foreach ($day->xpath('room') as $roomElement) { + $room = new Room( + (string)$roomElement->attributes()['name'] + ); + + $events = $this->parseEvents($roomElement->xpath('event'), $room); + $room->setEvent($events); + $rooms[] = $room; + } + + $days[] = new Day( + (string)$day->attributes()['date'], + new Carbon($day->attributes()['start']), + new Carbon($day->attributes()['end']), + (int)$day->attributes()['index'], + $rooms + ); + } + + $this->schedule = new Schedule( + $version, + $conference, + $days + ); + } + + /** + * @param SimpleXMLElement[] $eventElements + * @param Room $room + * @return array + */ + protected function parseEvents(array $eventElements, Room $room): array + { + $events = []; + + foreach ($eventElements as $event) { + $persons = $this->getListFromSequence($event, 'persons', 'person', 'id'); + $links = $this->getListFromSequence($event, 'links', 'link', 'href'); + $attachments = $this->getListFromSequence($event, 'attachments', 'attachment', 'href'); + + $recording = ''; + $recordingElement = $event->xpath('recording')[0]; + if ($this->getFirstXpathContent('optout', $recordingElement) == 'false') { + $recording = $this->getFirstXpathContent('license', $recordingElement); + } + + $events[] = new Event( + (string)$event->attributes()['guid'], + (int)$event->attributes()['id'], + $room, + $this->getFirstXpathContent('title', $event), + $this->getFirstXpathContent('subtitle', $event), + $this->getFirstXpathContent('type', $event), + new Carbon($this->getFirstXpathContent('date', $event)), + $this->getFirstXpathContent('start', $event), + $this->getFirstXpathContent('duration', $event), + $this->getFirstXpathContent('abstract', $event), + $this->getFirstXpathContent('slug', $event), + $this->getFirstXpathContent('track', $event), + $this->getFirstXpathContent('logo', $event) ?: null, + $persons, + $this->getFirstXpathContent('language', $event) ?: null, + $this->getFirstXpathContent('description', $event) ?: null, + $recording, + $links, + $attachments, + $this->getFirstXpathContent('url', $event) ?: null, + $this->getFirstXpathContent('video_download_url', $event) ?: null + ); + } + + return $events; + } + + /** + * @param string $path + * @param SimpleXMLElement|null $xml + * @return string + */ + protected function getFirstXpathContent(string $path, ?SimpleXMLElement $xml = null): string + { + $element = ($xml ?: $this->scheduleXML)->xpath($path); + + return $element ? (string)$element[0] : ''; + } + + /** + * Resolves a list from a sequence of elements + * + * @param SimpleXMLElement $element + * @param string $firstElement + * @param string $secondElement + * @param string $idAttribute + * @return array + */ + protected function getListFromSequence( + SimpleXMLElement $element, + string $firstElement, + string $secondElement, + string $idAttribute + ): array { + $items = []; + + foreach ($element->xpath($firstElement)[0]->xpath($secondElement) as $item) { + $items[(string)$item->attributes()[$idAttribute]] = (string)$item; + } + + return $items; + } + + /** + * @return Schedule + */ + public function getSchedule(): Schedule + { + return $this->schedule; + } +} -- cgit v1.2.3-54-g00ecf