add multi thread support for cli cal dav import
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 29 Jul 2014 15:09:13 +0000 (17:09 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:37 +0000 (11:26 +0200)
splitted import into pure calendar import and calendar data import.
First still one thread needs to import the pure calendars with grants etc. Then multiple threads can do the calendar data import via cli.

Change-Id: Ifda4ce65412d01f170d8e26455fcc613bd6b9bff
Reviewed-on: http://gerrit.tine20.com/customers/910
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Calendar/Frontend/Cli.php
tine20/Calendar/Import/CalDav/Client.php

index f9926d7..cdf8eea 100644 (file)
@@ -29,13 +29,20 @@ class Calendar_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
      * @return void
      */
     protected $_help = array(
-        'importCalDav' => array(
+        'importCalDavData' => array(
             'description'    => 'import calendar/events from a CalDav source',
             'params'         => array(
                 'url'        => 'CalDav source URL',
                 'caldavuserfile' => 'CalDav user file containing utf8 username;pwd',
              )
         ),
+        'importCalDavCalendars' => array(
+            'description'    => 'import calendars without events from a CalDav source',
+            'params'         => array(
+                'url'        => 'CalDav source URL',
+                'caldavuserfile' => 'CalDav user file containing utf8 username;pwd',
+             )
+        ),
         'importegw14' => array(
             'description'    => 'imports calendars/events from egw 1.4',
             'params'         => array(
@@ -144,7 +151,28 @@ class Calendar_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
      * 
      * param Zend_Console_Getopt $_opts
      */
-    public function importCalDav(Zend_Console_Getopt $_opts)
+    public function importCalDavCalendars(Zend_Console_Getopt $_opts)
+    {
+        $args = $this->_parseArgs($_opts, array('url', 'caldavuserfile'));
+        
+        $writer = new Zend_Log_Writer_Stream('php://output');
+        $writer->addFilter(new Zend_Log_Filter_Priority(4));
+        Tinebase_Core::getLogger()->addWriter($writer);
+        
+        $users = $this->_readCalDavUserFile($args['caldavuserfile']);
+        
+        $client = new Calendar_Import_CalDav_Client(array('baseUri' => $args['url']), 'MacOSX');
+        $client->setVerifyPeer(false);
+        
+        $client->importAllCalendarsForUsers($users);
+    }
+    
+    /**
+     * import calendar/events from a CalDav source
+     * 
+     * param Zend_Console_Getopt $_opts
+     */
+    public function importCalDavData(Zend_Console_Getopt $_opts)
     {
         $args = $this->_parseArgs($_opts, array('url', 'caldavuserfile'));
         
index 5ff171d..36e3171 100644 (file)
@@ -172,6 +172,43 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
         }
     }
     
+    public function importAllCalendars()
+    {
+        if (count($this->calendars) < 1 && ! $this->findAllCalendars())
+            return false;
+        
+        Calendar_Controller_Event::getInstance()->sendNotifications(false);
+        Sabre\VObject\Component\VCalendar::$propertyMap['ATTACH'] = '\\Calendar_Import_CalDav_SabreAttachProperty';
+        
+        $this->decorator->initCalendarImport();
+        
+        $modelName = Tinebase_Core::getApplicationInstance('Calendar')->getDefaultModel();
+        $application_id = Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId();
+        $type = Tinebase_Model_Container::TYPE_PERSONAL; //Tinebase_Model_Container::TYPE_SHARED;
+        $defaultContainer = Tinebase_Container::getInstance()->getDefaultContainer('Calendar_Model_Event');
+        
+        //decide which calendar to use as default calendar
+        //if there is a remote default calendar, use that. If not, use the first we find
+        $defaultCalendarsName = '';
+        foreach ($this->calendarICSs as $calUri => $calICSs) {
+            if ($this->mapToDefaultContainer == $this->calendars[$calUri]['displayname']) {
+                $container = Tinebase_Container::getInstance()->getDefaultContainer('Calendar_Model_Event');
+            } elseif ($defaultsCalendarsName === '') {
+                $defaultCalendarsName = $this->calendars[$calUri]['displayname'];
+            }
+        }
+        
+        foreach ($this->calendars as $calUri => $cal) {
+            $container = $this->findContainerForCalendar($calUri, $cal['displayname'], $defaultCalendarsName,
+                    $type, $application_id, $modelName);
+            
+            $this->decorator->setCalendarProperties($container, $this->calendars[$calUri]);
+            
+            $grants = $this->getCalendarGrants($calUri);
+            Tinebase_Container::getInstance()->setGrants($container->getId(), $grants, TRUE, FALSE);
+        }
+    }
+    
     public function importAllCalendarData($onlyCurrentUserOrganizer = false)
     {
         if (count($this->calendarICSs) < 1 && ! $this->findAllCalendarICSs())
@@ -204,8 +241,10 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
             
             $this->decorator->setCalendarProperties($container, $this->calendars[$calUri]);
             
-            $grants = $this->getCalendarGrants($calUri);
-            Tinebase_Container::getInstance()->setGrants($container->getId(), $grants, TRUE, FALSE);
+            // we shouldnt do the grants here as the caldav user file may not contain all users, so setting the grants wont work properly!
+            // use importAllCalendars to have the grants set
+            //$grants = $this->getCalendarGrants($calUri);
+            //Tinebase_Container::getInstance()->setGrants($container->getId(), $grants, TRUE, FALSE);
             
             $start = 0;
             $max = count($calICSs);
@@ -347,6 +386,23 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
         return $result;
     }
     
+    public function importAllCalendarsForUsers(array $users)
+    {
+        if (!$this->findCurrentUserPrincipalForUsers($users))
+            return false;
+        
+        $result = true;
+        foreach ($users as $username => $pwd) {
+            $this->clearCurrentUserCalendarData();
+            $this->userName = $username;
+            $this->password = $pwd;
+            if (!$this->importAllCalendars()) {
+                $result = false;
+            }
+        }
+        return $result;
+    }
+    
     public function clearCurrentUserCalendarData()
     {
         $this->clearCurrentUserData();