u01jmg3/ics-parser

This package is abandoned and no longer maintained. The author suggests using the johngrogg/ics-parser package instead.

ICS Parser

Fund package maintenance!
u01jmg3

Installs: 20 796

Dependents: 1

Suggesters: 0

Security: 0

Stars: 450

Watchers: 33

Forks: 145

Open Issues: 8

v3.4.1 2024-06-26 08:18 UTC

README

Latest Stable Release Total Downloads

Installation

Requirements

Setup

  • Install Composer
    • Add the following dependency to composer.json
      • ⚠️ Note with Composer the owner is johngrogg and not u01jmg3
    • To access the latest stable branch (v3) use the following
      • To access new features you can require dev-master

        {
            "require": {
                "johngrogg/ics-parser": "^3"
            }
        }

Running tests

composer test

How to use

How to instantiate the Parser

What will the parser return?

  • Each key/value pair from the iCal file will be parsed creating an associative array for both the calendar and every event it contains.

  • Also injected will be content under dtstart_tz and dtend_tz for accessing start and end dates with time zone data applied.

  • Where possible DateTime objects are used and returned.

    • ℹ️ Note the parser is limited to relative date formats which can inhibit how complex recurrence rule parts are processed (e.g. BYDAY combined with BYSETPOS)
    // Dump the whole calendar
    var_dump($ical->cal);
    
    // Dump every event
    var_dump($ical->events());
  • Also included are special {property}_array arrays which further resolve the contents of a key/value pair.

    // Dump a parsed event's start date
    var_dump($event->dtstart_array);
    
    // array (size=4)
    //   0 =>
    //     array (size=1)
    //       'TZID' => string 'America/Detroit' (length=15)
    //   1 => string '20160409T090000' (length=15)
    //   2 => int 1460192400
    //   3 => string 'TZID=America/Detroit:20160409T090000' (length=36)

Are you using Outlook?

Outlook has a quirk where it requires the User Agent string to be set in your request headers.

We have done this for you by injecting a default User Agent string, if one has not been specified.

If you wish to provide your own User agent string you can do so by using the httpUserAgent argument when creating your ICal object.

$ical = new ICal($url, array('httpUserAgent' => 'A Different User Agent'));

When Parsing an iCal Feed

Parsing iCal/iCalendar/ICS resources can pose several challenges. One challenge is that the specification is a moving target; the original RFC has only been updated four times in ten years. The other challenge is that vendors were both liberal (read: creative) in interpreting the specification and productive implementing proprietary extensions.

However, what impedes efficient parsing most directly are recurrence rules for events. This library parses the original calendar into an easy to work with memory model. This requires that each recurring event is expanded or exploded. Hence, a single event that occurs daily will generate a new event instance for each day as this parser processes the calendar ($defaultSpan limits this). To get an idea how this is done take a look at the call graph.

As a consequence the entire calendar is parsed line-by-line, and thus loaded into memory, first. As you can imagine large calendars tend to get huge when exploded i.e. with all their recurrence rules evaluated. This is exacerbated when old calendars do not remove past events as they get fatter and fatter every year.

This limitation is particularly painful if you only need a window into the original calendar. It seems wasteful to parse the entire fully exploded calendar into memory if you later are going to call the eventsFromInterval() or eventsFromRange() on it.

In late 2018 #190 added the option to drop all events outside a given range very early in the parsing process at the cost of some precision (time zone calculations are not calculated at that point). This massively reduces the total time for parsing a calendar. The same goes for memory consumption. The precondition is that you know upfront that you don't care about events outside a given range.

Let's say you are only interested in events from yesterday, today and tomorrow. To compensate for the fact that the tricky time zone transformations and calculations have not been executed yet by the time the parser has to decide whether to keep or drop an event you can set it to filter for +-2d instead of +-1d. Once it is done you would then call eventsFromRange() with +-1d to get precisely the events in the window you are interested in. That is what the variables $filterDaysBefore and $filterDaysAfter are for.

In Q1 2019 #213 further improved the performance by immediately dropping non-recurring events once parsed if they are outside that fuzzy window. This greatly reduces the maximum memory consumption for large calendars. PHP by default does not allocate more than 128MB heap and would otherwise crash with Fatal error: Allowed memory size of 134217728 bytes exhausted. It goes without saying that recurring events first need to be evaluated before non-fitting events can be dropped.

API

ICal API

Variables

Methods

Constants

Event API (extends ICal API)

Methods

Constants

Credits

  • Jonathan Goode (programming, bug fixing, codebase enhancement, coding standard adoption)
  • s0600204 (major enhancements to RRULE support, many bug fixes and other contributions)

Tools for Testing