Fix error with recent SQLite plugin ref T41
authorAndreas Boehler <andreas@aboehler.at>
Tue, 13 Dec 2016 05:35:23 +0100
changeset 17 8c73076d5dcb
parent 16 9f92b1b42241
child 18 ff3d73d2e467
Fix error with recent SQLite plugin ref T41
helper.php
plugin.info.txt
--- a/helper.php	Fri Jul 01 11:29:04 2016 +0200
+++ b/helper.php	Tue Dec 13 05:35:23 2016 +0100
@@ -18,23 +18,31 @@
     * Constructor to load the configuration
     */
   public function helper_plugin_davcard() {
-    $this->sqlite =& plugin_load('helper', 'sqlite');
-    global $conf;
+
+  }
 
-    if(!$this->sqlite)
-    {
-        if($conf['allowdebug'])
+  /** Establish and initialize the database if not already done
+   * @return sqlite interface or false
+   */
+  private function getDB()
+  {
+      if($this->sqlite === null)
+      {
+        $this->sqlite = plugin_load('helper', 'sqlite');
+        if(!$this->sqlite)
+        {
             dbglog('This plugin requires the sqlite plugin. Please install it.');
-        msg('This plugin requires the sqlite plugin. Please install it.');
-        return;
-    }
-    
-    if(!$this->sqlite->init('davcard', DOKU_PLUGIN.'davcard/db/'))
-    {
-        if($conf['allowdebug'])
-            dbglog('Error initialising the SQLite DB for DAVCard');
-        return;
-    }
+            msg('This plugin requires the sqlite plugin. Please install it.', -1);
+            return false;
+        }
+        if(!$this->sqlite->init('dvacard', DOKU_PLUGIN.'davcard/db/'))
+        {
+            $this->sqlite = null;
+            dbglog('Error initialising the SQLite DB for davcard');
+            return false;
+        }
+      }
+      return $this->sqlite;
   }
   
   /**
@@ -136,9 +144,12 @@
    */
   public function getAddressbookEntries($id)
   {
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false;  
       $query = "SELECT contactdata, uri, formattedname, structuredname FROM addressbookobjects WHERE addressbookid = ? ORDER BY formattedname ASC";
-      $res = $this->sqlite->query($query, $id);
-      return $this->sqlite->res2arr($res);
+      $res = $sqlite->query($query, $id);
+      return $sqlite->res2arr($res);
   }
  
   /**
@@ -246,9 +257,12 @@
    */
   private function getAddressbookEntryByUri($id, $uri)
   {
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
       $query = "SELECT contactdata, addressbookid, etag, uri, formattedname, structuredname FROM addressbookobjects WHERE addressbookid = ? AND uri = ?";
-      $res = $this->sqlite->query($query, $id, $uri);
-      return $this->sqlite->res2row($res);
+      $res = $sqlite->query($query, $id, $uri);
+      return $sqlite->res2row($res);
   }
   
   /**
@@ -283,8 +297,11 @@
       if($bookid === false)
         return $this->createAddressbookForPage($name, $description, $id, $userid);
       
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
       $query = "UPDATE addressbooks SET displayname = ?, description = ? WHERE id = ?";
-      $res = $this->sqlite->query($query, $name, $description, $bookid);
+      $res = $sqlite->query($query, $name, $description, $bookid);
       if($res !== false)
         return true;
       return false;
@@ -305,9 +322,13 @@
           $id = $ID;
       }
       
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
+      
       $query = "SELECT addressbookid FROM pagetoaddressbookmapping WHERE page = ?";
-      $res = $this->sqlite->query($query, $id);
-      $row = $this->sqlite->res2row($res);
+      $res = $sqlite->query($query, $id);
+      $row = $sqlite->res2row($res);
       if(isset($row['addressbookid']))
       {
         $addrbkid = $row['addressbookid'];
@@ -344,6 +365,11 @@
           $userid = uniqid('davcard-');
         }
       }
+      
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
+      
       $values = array('principals/'.$userid, 
                       $name,
                       str_replace(array('/', ' ', ':'), '_', $id), 
@@ -351,21 +377,21 @@
                       1);
       $query = "INSERT INTO addressbooks (principaluri, displayname, uri, description, synctoken) ".
                "VALUES (?, ?, ?, ?, ?)";
-      $res = $this->sqlite->query($query, $values);
+      $res = $sqlite->query($query, $values);
       if($res === false)
         return false;
       
       // Get the new addressbook ID
       $query = "SELECT id FROM addressbooks WHERE principaluri = ? AND displayname = ? AND ".
                "uri = ? AND description = ? AND synctoken = ?";
-      $res = $this->sqlite->query($query, $values);
-      $row = $this->sqlite->res2row($res);
+      $res = $sqlite->query($query, $values);
+      $row = $sqlite->res2row($res);
       
       // Update the pagetocalendarmapping table with the new calendar ID
       if(isset($row['id']))
       {
           $query = "INSERT INTO pagetoaddressbookmapping (page, addressbookid) VALUES (?, ?)";
-          $res = $this->sqlite->query($query, $id, $row['id']);
+          $res = $sqlite->query($query, $id, $row['id']);
           return ($res !== false);
       }
       
@@ -399,10 +425,14 @@
         return $wdc->deleteAddressbookEntry($connectionId, $uri);
       }
       
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
+      
       $addressbookid = $this->getAddressbookIdForPage($id);
       $query = "DELETE FROM addressbookobjects WHERE uri = ? AND addressbookid = ?";
       
-      $res = $this->sqlite->query($query, $uri, $addressbookid);
+      $res = $sqlite->query($query, $uri, $addressbookid);
       if($res !== false)
       {
           $this->updateSyncTokenLog($addressbookid, $uri, 'deleted');
@@ -490,9 +520,12 @@
       }
       else
       {
+          $sqlite = $this->getDB();
+          if(!$sqlite)
+            return false; 
           $now = new \DateTime();
           $query = "UPDATE addressbookobjects SET contactdata = ?, lastmodified = ?, etag = ?, size = ?, formattedname = ?, structuredname = ? WHERE addressbookid = ? AND uri = ?";
-          $res = $this->sqlite->query($query,
+          $res = $sqlite->query($query,
                                       $contactdata,
                                       $now->getTimestamp(),
                                       md5($contactdata),
@@ -563,11 +596,14 @@
       }
       else
       {
+          $sqlite = $this->getDB();
+          if(!$sqlite)
+            return false; 
           $addressbookid = $this->getAddressbookIdForPage($id);
           $uri = uniqid('dokuwiki-').'.vcf';
           $now = new \DateTime();
           $query = "INSERT INTO addressbookobjects (contactdata, uri, addressbookid, lastmodified, etag, size, formattedname, structuredname) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
-          $res = $this->sqlite->query($query, 
+          $res = $sqlite->query($query, 
                                       $contactdata, 
                                       $uri, 
                                       $addressbookid, 
@@ -709,9 +745,12 @@
    */
   public function getAddressbookSettings($addressbookid)
   {
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
       $query = "SELECT id, principaluri, displayname, uri, description, synctoken FROM addressbooks WHERE id= ? ";
-      $res = $this->sqlite->query($query, $addressbookid);
-      $row = $this->sqlite->res2row($res);
+      $res = $sqlite->query($query, $addressbookid);
+      $row = $sqlite->res2row($res);
       return $row;
   }
 
@@ -775,13 +814,16 @@
                       $addressbookid,
                       $operationCode
       );
+      $sqlite = $this->getDB();
+      if(!$sqlite)
+        return false; 
       $query = "INSERT INTO addressbookchanges (uri, synctoken, addressbookid, operation) VALUES(?, ?, ?, ?)";
-      $res = $this->sqlite->query($query, $uri, $currentToken, $addressbookid, $operationCode);
+      $res = $sqlite->query($query, $uri, $currentToken, $addressbookid, $operationCode);
       if($res === false)
         return false;
       $currentToken++;
       $query = "UPDATE addressbooks SET synctoken = ? WHERE id = ?";
-      $res = $this->sqlite->query($query, $currentToken, $addressbookid);
+      $res = $sqlite->query($query, $currentToken, $addressbookid);
       return ($res !== false);
   }
  
--- a/plugin.info.txt	Fri Jul 01 11:29:04 2016 +0200
+++ b/plugin.info.txt	Tue Dec 13 05:35:23 2016 +0100
@@ -1,7 +1,7 @@
 base	davcard
 author  Andreas Boehler
 email   dev@aboehler.at
-date    2016-07-01
+date    2016-12-13
 name    Addressbook PlugIn with CardDAV client support
 desc    Show contact information from a CardDAV address book (needs webdavclient)
 url     http://www.dokuwiki.org/plugin:davcard