Skip to content

Commit

Permalink
Improve kronolith initialization.
Browse files Browse the repository at this point in the history
First stab at reducing the amount of work needed to initialize Kronolith.
Lazy loads the most resource intesive calendar lists and e.g., no longer
polls for external calendars on each rpc request unless explicitly needed.

Among other things, improves performance of ActiveSync requests since we
don't attempt to load external calendars like Facebook Events or Weather.
  • Loading branch information
mrubinsk committed Sep 28, 2013
1 parent b0bef96 commit e042dd7
Show file tree
Hide file tree
Showing 28 changed files with 680 additions and 390 deletions.
30 changes: 18 additions & 12 deletions kronolith/lib/Ajax/Application/Handler.php
Expand Up @@ -32,13 +32,14 @@ public function poll()
public function listCalendars()
{
Kronolith::initialize();
$all_external_calendars = $GLOBALS['calendar_manager']->get(Kronolith::ALL_EXTERNAL_CALENDARS);
$result = new stdClass;
$auth_name = $GLOBALS['registry']->getAuth();

// Calendars. Do some twisting to sort own calendar before shared
// calendars.
foreach (array(true, false) as $my) {
foreach ($GLOBALS['all_calendars'] as $id => $calendar) {
foreach ($GLOBALS['calendar_manager']->get(Kronolith::ALL_CALENDARS) as $id => $calendar) {
$owner = ($auth_name && ($calendar->owner() == $auth_name));
if (($my && $owner) || (!$my && !$owner)) {
$result->calendars['internal'][$id] = $calendar->toHash();
Expand All @@ -48,12 +49,12 @@ public function listCalendars()
// Tasklists
if (Kronolith::hasApiPermission('tasks')) {
foreach ($GLOBALS['registry']->tasks->listTasklists($my, Horde_Perms::SHOW, false) as $id => $tasklist) {
if (isset($GLOBALS['all_external_calendars']['tasks/' . $id])) {
if (isset($all_external_calendars['tasks/' . $id])) {
$owner = ($auth_name &&
($tasklist->get('owner') == $auth_name));
if (($my && $owner) || (!$my && !$owner)) {
$result->calendars['tasklists']['tasks/' . $id] =
$GLOBALS['all_external_calendars']['tasks/' . $id]->toHash();
$all_external_calendars['tasks/' . $id]->toHash();
}
}
}
Expand All @@ -78,19 +79,19 @@ public function listCalendars()
}

// Timeobjects
foreach ($GLOBALS['all_external_calendars'] as $id => $calendar) {
foreach ($all_external_calendars as $id => $calendar) {
if ($calendar->api() != 'tasks' && $calendar->display()) {
$result->calendars['external'][$id] = $calendar->toHash();
}
}

// Remote calendars
foreach ($GLOBALS['all_remote_calendars'] as $url => $calendar) {
foreach ($GLOBALS['calendar_manager']->get(Kronolith::ALL_REMOTE_CALENDARS) as $url => $calendar) {
$result->calendars['remote'][$url] = $calendar->toHash();
}

// Holidays
foreach ($GLOBALS['all_holidays'] as $id => $calendar) {
foreach ($GLOBALS['calendar_manager']->get(Kronolith::ALL_HOLIDAYS) as $id => $calendar) {
$result->calendars['holiday'][$id] = $calendar->toHash();
}

Expand Down Expand Up @@ -964,9 +965,13 @@ public function saveCalendar()
$all_external = $GLOBALS['session']->get('kronolith', 'all_external_calendars');
$all_external[] = array('a' => 'tasks', 'n' => $tasklistId, 'd' => $tasklist->get('name'));
$GLOBALS['session']->set('kronolith', 'all_external_calendars', $all_external);
$GLOBALS['display_external_calendars'][] = 'tasks/' . $tasklistId;
$GLOBALS['prefs']->setValue('display_external_cals', serialize($GLOBALS['display_external_calendars']));
$GLOBALS['all_external_calendars']['tasks/' . $tasklistId] = $wrapper;
$display_external = $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS);
$display_external[] = 'tasks/' . $tasklistId;
$GLOBALS['calendar_manager']->set(Kronolith::DISPLAY_EXTERNAL_CALENDARS, $display_external);
$GLOBALS['prefs']->setValue('display_external_cals', serialize($display_external));
$all_external = $GLOBALS['calendar_manager']->get(Kronolith::ALL_EXTERNAL_CALENDARS);
$all_external['tasks/' . $tasklistId] = $wrapper;
$GLOBALS['calendar_manager']->set(Kronolith::ALL_EXTERNAL_CALENDARS, $all_external);

$result->saved = true;
$result->id = 'tasks/' . $tasklistId;
Expand Down Expand Up @@ -1193,10 +1198,11 @@ public function deleteCalendar()
public function getCalendar()
{
$result = new stdClass;
if (!isset($GLOBALS['all_calendars'][$this->vars->cal]) && !$GLOBALS['conf']['share']['hidden']) {
$all_calendars = $GLOBALS['calendar_manager']->get(Kronolith::ALL_CALENDARS);
if (!isset($all_calendars[$this->vars->cal]) && !$GLOBALS['conf']['share']['hidden']) {
$GLOBALS['notification']->push(_("You are not allowed to view this calendar."), 'horde.error');
return $result;
} elseif (!isset($GLOBALS['all_calendars'][$this->vars->cal])) {
} elseif (!isset($all_calendars[$this->vars->cal])) {
// Subscribing to a "hidden" share, check perms.
$kronolith_shares = $GLOBALS['injector']->getInstance('Kronolith_Shares');
$share = $kronolith_shares->getShare($this->vars->cal);
Expand All @@ -1206,7 +1212,7 @@ public function getCalendar()
}
$calendar = new Kronolith_Calendar_Internal(array('share' => $share));
} else {
$calendar = $GLOBALS['all_calendars'][$this->vars->cal];
$calendar = $all_calendars[$this->vars->cal];
}

$result->calendar = $calendar->toHash();
Expand Down
20 changes: 10 additions & 10 deletions kronolith/lib/Application.php
Expand Up @@ -222,7 +222,7 @@ public function sidebar($sidebar)
);
foreach (Kronolith::listInternalCalendars() as $id => $calendar) {
$row = array(
'selected' => in_array($id, $GLOBALS['display_calendars']),
'selected' => in_array($id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS)),
'url' => $url->copy()->add('toggle_calendar', $id),
'label' => Kronolith::getLabel($calendar),
'color' => Kronolith::backgroundColor($calendar),
Expand All @@ -239,7 +239,7 @@ public function sidebar($sidebar)
if ($GLOBALS['registry']->isAdmin()) {
foreach ($GLOBALS['injector']->getInstance('Kronolith_Shares')->listSystemShares() as $id => $calendar) {
$row = array(
'selected' => in_array($id, $GLOBALS['display_calendars']),
'selected' => in_array($id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS)),
'url' => $url->copy()->add('toggle_calendar', $id),
'label' => $calendar->get('name'),
'color' => Kronolith::backgroundColor($calendar),
Expand Down Expand Up @@ -288,7 +288,7 @@ public function sidebar($sidebar)
'resource' => $resource
));
$row = array(
'selected' => in_array($resource->get('calendar'), $GLOBALS['display_resource_calendars']),
'selected' => in_array($resource->get('calendar'), $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_RESOURCE_CALENDARS)),
'url' => $url->copy()->add('toggle_calendar', 'resource_' . $resource->get('calendar')),
'label' => $calendar->name(),
'color' => $calendar->background(),
Expand All @@ -301,7 +301,7 @@ public function sidebar($sidebar)
}
}

foreach ($GLOBALS['all_external_calendars'] as $id => $calendar) {
foreach ($GLOBALS['calendar_manager']->get(Kronolith::ALL_EXTERNAL_CALENDARS) as $id => $calendar) {
if (!$calendar->display()) {
continue;
}
Expand All @@ -322,7 +322,7 @@ public function sidebar($sidebar)
);
}
$row = array(
'selected' => in_array($id, $GLOBALS['display_external_calendars']),
'selected' => in_array($id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS)),
'url' => $url->copy()->add('toggle_calendar', 'external_' . $id),
'label' => $calendar->name(),
'color' => $calendar->background(),
Expand All @@ -343,9 +343,9 @@ public function sidebar($sidebar)
),
);
$edit = Horde::url('calendars/remote_edit.php');
foreach ($GLOBALS['all_remote_calendars'] as $id => $calendar) {
foreach ($GLOBALS['calendar_manager']->get(Kronolith::ALL_REMOTE_CALENDARS) as $calendar) {
$row = array(
'selected' => in_array($calendar->url(), $GLOBALS['display_remote_calendars']),
'selected' => in_array($calendar->url(), $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_REMOTE_CALENDARS)),
'url' => $url->copy()->add('toggle_calendar', 'remote_' . $calendar->url()),
'label' => $calendar->name(),
'color' => $calendar->background(),
Expand All @@ -363,9 +363,9 @@ public function sidebar($sidebar)
'collapsed' => true,
),
);
foreach ($GLOBALS['all_holidays'] as $id => $calendar) {
foreach ($GLOBALS['calendar_manager']->get(Kronolith::ALL_HOLIDAYS) as $id => $calendar) {
$row = array(
'selected' => in_array($id, $GLOBALS['display_holidays']),
'selected' => in_array($id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_HOLIDAYS)),
'url' => $url->copy()->add('toggle_calendar', 'holiday_' . $id),
'label' => $calendar->name(),
'color' => $calendar->background(),
Expand Down Expand Up @@ -485,7 +485,7 @@ public function listAlarms($time, $user = null)
$time = new Horde_Date($time);
$calendars = is_null($user)
? array_keys($kronolith_shares->listAllShares())
: $GLOBALS['display_calendars'];
: $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS);
$alarms = Kronolith::listAlarms($time, $calendars, true);
foreach ($alarms as $calendar => $cal_alarms) {
if (!$cal_alarms) {
Expand Down
2 changes: 1 addition & 1 deletion kronolith/lib/Block/Month.php
Expand Up @@ -127,7 +127,7 @@ protected function _content()
$all_events = $driver->listEvents($startDate, $endDate, array(
'show_recurrence' => true));
} else {
$all_events = Kronolith::listEvents($startDate, $endDate, $GLOBALS['display_calendars']);
$all_events = Kronolith::listEvents($startDate, $endDate, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
}
} catch (Exception $e) {
return '<em>' . $e->getMessage() . '</em>';
Expand Down
6 changes: 4 additions & 2 deletions kronolith/lib/Block/Monthlist.php
Expand Up @@ -101,9 +101,11 @@ protected function _content()
$all_events = Kronolith::listEvents(
$startDate,
$endDate,
$GLOBALS['display_calendars'], array(
$GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS),
array(
'has_alarm' => !empty($this->_params['alarms']),
'cover_dates' => false)
'cover_dates' => false
)
);
}
} catch (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion kronolith/lib/Block/Prevmonthlist.php
Expand Up @@ -86,7 +86,7 @@ protected function _content()
$all_events = $driver->listEvents(
$startDate, $endDate, array('show_recurrence' => true));
} else {
$all_events = Kronolith::listEvents($startDate, $endDate, $GLOBALS['display_calendars']);
$all_events = Kronolith::listEvents($startDate, $endDate, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
}
} catch (Exception $e) {
return '<em>' . $e->getMessage() . '</em>';
Expand Down
2 changes: 1 addition & 1 deletion kronolith/lib/Block/Summary.php
Expand Up @@ -107,7 +107,7 @@ protected function _content()
);
} else {
$all_events = Kronolith::listEvents(
$startDate, $endDate, $GLOBALS['display_calendars']);
$startDate, $endDate, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
}
} catch (Exception $e) {
return '<em>' . $e->getMessage() . '</em>';
Expand Down
4 changes: 2 additions & 2 deletions kronolith/lib/Calendar/External.php
Expand Up @@ -84,7 +84,7 @@ public function display()
{
return empty($GLOBALS['conf']['share']['hidden']) ||
$this->_type != 'share' ||
in_array($this->_api . '/' . $this->_id, $GLOBALS['display_external_calendars']);
in_array($this->_api . '/' . $this->_id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS));
}

/**
Expand All @@ -106,7 +106,7 @@ public function toHash()
{
$hash = parent::toHash();
$hash['api'] = $GLOBALS['registry']->get('name', $GLOBALS['registry']->hasInterface($this->api()));
$hash['show'] = in_array($this->_api . '/' . $this->_id, $GLOBALS['display_external_calendars']);
$hash['show'] = in_array($this->_api . '/' . $this->_id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS));
return $hash;
}
}
2 changes: 1 addition & 1 deletion kronolith/lib/Calendar/External/Tasks.php
Expand Up @@ -51,7 +51,7 @@ public function toHash()
$hash['owner'] = $owner;
$hash['fg'] = Kronolith::foregroundColor($this->_share);
$hash['bg'] = Kronolith::backgroundColor($this->_share);
$hash['show'] = in_array('tasks/' . $this->_share->getName(), $GLOBALS['display_external_calendars']);
$hash['show'] = in_array('tasks/' . $this->_share->getName(), $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_EXTERNAL_CALENDARS));
$hash['edit'] = $this->_share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT);
try {
$hash['caldav'] = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . ($GLOBALS['conf']['urls']['pretty'] == 'rewrite' ? '/rpc/calendars/' : '/rpc.php/calendars/'), true, -1)
Expand Down
4 changes: 2 additions & 2 deletions kronolith/lib/Calendar/Holiday.php
Expand Up @@ -52,7 +52,7 @@ public function name()
*/
public function display()
{
return in_array($this->_driver['id'], $GLOBALS['display_holidays']);
return in_array($this->_driver['id'], $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_HOLIDAYS));
}

/**
Expand All @@ -63,7 +63,7 @@ public function display()
public function toHash()
{
$hash = parent::toHash();
$hash['show'] = in_array($this->_driver['id'], $GLOBALS['display_holidays']);
$hash['show'] = in_array($this->_driver['id'], $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_HOLIDAYS));
return $hash;
}
}
4 changes: 2 additions & 2 deletions kronolith/lib/Calendar/Internal.php
Expand Up @@ -108,7 +108,7 @@ public function display()
{
return $this->owner() == $GLOBALS['registry']->getAuth() ||
empty($GLOBALS['conf']['share']['hidden']) ||
in_array($this->_share->getName(), $GLOBALS['display_calendars']);
in_array($this->_share->getName(), $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
}

/**
Expand All @@ -135,7 +135,7 @@ public function toHash()
$hash = parent::toHash();
$hash['name'] = $this->name();
$hash['owner'] = $owner;
$hash['show'] = in_array($id, $GLOBALS['display_calendars']);
$hash['show'] = in_array($id, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
$hash['edit'] = $this->hasPermission(Horde_Perms::EDIT);
try {
$hash['caldav'] = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . ($GLOBALS['conf']['urls']['pretty'] == 'rewrite' ? '/rpc/calendars/' : '/rpc.php/calendars/'), true, -1)
Expand Down
4 changes: 2 additions & 2 deletions kronolith/lib/Calendar/Remote.php
Expand Up @@ -137,7 +137,7 @@ public function hasPermission($permission, $user = null, $creator = null)
*/
public function display()
{
return in_array($this->_url, $GLOBALS['display_remote_calendars']);
return in_array($this->_url, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_REMOTE_CALENDARS));
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ public function toHash()
{
return array_merge(
parent::toHash(),
array('show' => in_array($this->_url, $GLOBALS['display_remote_calendars']),
array('show' => in_array($this->_url, $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_REMOTE_CALENDARS)),
'edit' => $this->hasPermission(Horde_Perms::EDIT)),
$this->credentials()
);
Expand Down
2 changes: 1 addition & 1 deletion kronolith/lib/Calendar/Resource.php
Expand Up @@ -93,7 +93,7 @@ public function hasPermission($permission, $user = null, $creator = null)
*/
public function display()
{
return in_array($this->_resource->get('calendar'), $GLOBALS['display_calendars']);
return in_array($this->_resource->get('calendar'), $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion kronolith/lib/Calendar/ResourceGroup.php
Expand Up @@ -94,7 +94,7 @@ public function hasPermission($permission, $user = null, $creator = null)
*/
public function display()
{
return in_array($this->_resource->get('calendar'), $GLOBALS['display_calendars']);
return in_array($this->_resource->get('calendar'), $GLOBALS['calendar_manager']->get(Kronolith::DISPLAY_CALENDARS));
}

/**
Expand Down

0 comments on commit e042dd7

Please sign in to comment.