[Pkg-owncloud-commits] [php-sabredav] 77/148: PropertyStorage system can now store complex xml.

David Prévot taffit at moszumanska.debian.org
Wed Apr 15 01:37:18 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit 6d6f00a5bf6ccc962f40a2d25e790541935ce54c
Author: Evert Pot <me at evertpot.com>
Date:   Fri Mar 20 19:27:08 2015 -0400

    PropertyStorage system can now store complex xml.
---
 bin/migrateto22.php                     | 118 ++++++++++++++++++++++++++++++++
 examples/sql/mysql.propertystorage.sql  |   1 +
 examples/sql/pgsql.propertystorage.sql  |   1 +
 examples/sql/sqlite.propertystorage.sql |   7 +-
 lib/DAV/PropertyStorage/Backend/PDO.php |  45 ++++++++++--
 5 files changed, 165 insertions(+), 7 deletions(-)

diff --git a/bin/migrateto22.php b/bin/migrateto22.php
new file mode 100755
index 0000000..0aa2572
--- /dev/null
+++ b/bin/migrateto22.php
@@ -0,0 +1,118 @@
+#!/usr/bin/env php
+<?php
+
+echo "SabreDAV migrate script for version 2.2\n";
+
+if ($argc<2) {
+
+    echo <<<HELLO
+
+This script help you migrate from a pre-2.2 database to 2.2 and later
+
+Changes:
+  * Values in the propertystorage table are now serialized using PHP's
+    serialize()
+
+Keep in mind that ALTER TABLE commands will be executed. If you have a large
+dataset this may mean that this process takes a while.
+
+Lastly: Make a back-up first. This script has been tested, but the amount of
+potential variants are extremely high, so it's impossible to deal with every
+possible situation.
+
+In the worst case, you will lose all your data. This is not an overstatement.
+
+Usage:
+
+php {$argv[0]} [pdo-dsn] [username] [password]
+
+For example:
+
+php {$argv[0]} "mysql:host=localhost;dbname=sabredav" root password
+php {$argv[0]} sqlite:data/sabredav.db
+
+HELLO;
+
+    exit();
+
+}
+
+// There's a bunch of places where the autoloader could be, so we'll try all of
+// them.
+$paths = [
+    __DIR__ . '/../vendor/autoload.php',
+    __DIR__ . '/../../../autoload.php',
+];
+
+foreach($paths as $path) {
+    if (file_exists($path)) {
+        include $path;
+        break;
+    }
+}
+
+$dsn = $argv[1];
+$user = isset($argv[2])?$argv[2]:null;
+$pass = isset($argv[3])?$argv[3]:null;
+
+echo "Connecting to database: " . $dsn . "\n";
+
+$pdo = new PDO($dsn, $user, $pass);
+$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
+
+$driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
+
+switch($driver) {
+
+    case 'mysql' :
+        echo "Detected MySQL.\n";
+        break;
+    case 'sqlite' :
+        echo "Detected SQLite.\n";
+        break;
+    default :
+        echo "Error: unsupported driver: " . $driver . "\n";
+        die(-1);
+}
+
+echo "Upgrading 'propertystorage'\n";
+$addValueType = false;
+try {
+    $result = $pdo->query('SELECT * FROM propertystorage LIMIT 1');
+    $row = $result->fetch(\PDO::FETCH_ASSOC);
+
+    if (!$row) {
+        echo "No data in table. Going to try to add the uid field anyway.\n";
+        $addValueType = true;
+    } elseif (array_key_exists('valuetype', $row)) {
+        echo "valuetype field exists. Assuming that this part of the migration has\n";
+        echo "Already been completed.\n";
+    } else {
+        echo "2.1 schema detected. Going to perform upgrade.\n";
+        $addValueType = true;
+    }
+
+} catch (Exception $e) {
+    echo "Could not find a propertystorage table. Skipping this part of the\n";
+    echo "upgrade.\n";
+}
+
+if ($addValueType) {
+
+    switch($driver) {
+        case 'mysql' :
+            $pdo->exec('ALTER TABLE propertystorage ADD valuetype INT UNSIGNED');
+            break;
+        case 'sqlite' :
+            $pdo->exec('ALTER TABLE propertystorage ADD valuetype INT');
+
+            break;
+    }
+
+    $pdo->exec('UPDATE propertystorage SET valuetype = 1 WHERE valuetype IS NULL ');
+
+}
+
+echo "Done.\n";
+echo "Upgrade to 2.2 schema completed.\n";
diff --git a/examples/sql/mysql.propertystorage.sql b/examples/sql/mysql.propertystorage.sql
index c81ab9b..f2a0f6e 100644
--- a/examples/sql/mysql.propertystorage.sql
+++ b/examples/sql/mysql.propertystorage.sql
@@ -2,6 +2,7 @@ CREATE TABLE propertystorage (
     id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
     path VARBINARY(1024) NOT NULL,
     name VARBINARY(100) NOT NULL,
+    valuetype UNSIGNED INT,
     value MEDIUMBLOB
 );
 CREATE UNIQUE INDEX path_property ON propertystorage (path(600), name(100));
diff --git a/examples/sql/pgsql.propertystorage.sql b/examples/sql/pgsql.propertystorage.sql
index 1bfa83d..1642e22 100644
--- a/examples/sql/pgsql.propertystorage.sql
+++ b/examples/sql/pgsql.propertystorage.sql
@@ -2,6 +2,7 @@ CREATE TABLE propertystorage (
     id SERIAL NOT NULL,
     path VARCHAR(1024) NOT NULL,
     name VARCHAR(100) NOT NULL,
+    valuetype INT,
     value TEXT
 );
 
diff --git a/examples/sql/sqlite.propertystorage.sql b/examples/sql/sqlite.propertystorage.sql
index 1af7f19..4518179 100644
--- a/examples/sql/sqlite.propertystorage.sql
+++ b/examples/sql/sqlite.propertystorage.sql
@@ -1,8 +1,9 @@
 CREATE TABLE propertystorage (
     id integer primary key asc,
-    path TEXT,
-    name TEXT,
-    value TEXT
+    path text,
+    name text,
+    valuetype integer,
+    value string
 );
 
 
diff --git a/lib/DAV/PropertyStorage/Backend/PDO.php b/lib/DAV/PropertyStorage/Backend/PDO.php
index 4d69255..aff279e 100644
--- a/lib/DAV/PropertyStorage/Backend/PDO.php
+++ b/lib/DAV/PropertyStorage/Backend/PDO.php
@@ -5,6 +5,8 @@ namespace Sabre\DAV\PropertyStorage\Backend;
 use Sabre\DAV\PropFind;
 use Sabre\DAV\PropPatch;
 
+use Sabre\DAV\Xml\Property\Complex;
+
 /**
  * PropertyStorage PDO backend.
  *
@@ -20,6 +22,21 @@ use Sabre\DAV\PropPatch;
 class PDO implements BackendInterface {
 
     /**
+     * Value is stored as string.
+     */
+    const VT_STRING = 1;
+
+    /**
+     * Value is stored as XML fragment.
+     */
+    const VT_XML = 2;
+
+    /**
+     * Value is stored as a property object.
+     */
+    const VT_OBJECT = 3;
+
+    /**
      * PDO
      *
      * @var \PDO
@@ -58,12 +75,23 @@ class PDO implements BackendInterface {
             return;
         }
 
-        $query = 'SELECT name, value FROM propertystorage WHERE path = ?';
+        $query = 'SELECT name, value, valuetype FROM propertystorage WHERE path = ?';
         $stmt = $this->pdo->prepare($query);
         $stmt->execute([$path]);
 
         while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
-            $propFind->set($row['name'], $row['value']);
+            switch($row['valuetype']) {
+                case NULL :
+                case self::VT_STRING :
+                    $propFind->set($row['name'], $row['value']);
+                    break;
+                case self::VT_XML :
+                    $propFind->set($row['name'], new Complex($row['value']));
+                    break;
+                case self::VT_OBJECT :
+                    $propFind->set($row['name'], unserialize($row['value']));
+                    break;
+            }
         }
 
     }
@@ -85,13 +113,22 @@ class PDO implements BackendInterface {
 
         $propPatch->handleRemaining(function($properties) use ($path) {
 
-            $updateStmt = $this->pdo->prepare("REPLACE INTO propertystorage (path, name, value) VALUES (?, ?, ?)");
+            $updateStmt = $this->pdo->prepare("REPLACE INTO propertystorage (path, name, valuetype, value) VALUES (?, ?, ?, ?)");
             $deleteStmt = $this->pdo->prepare("DELETE FROM propertystorage WHERE path = ? AND name = ?");
 
             foreach($properties as $name=>$value) {
 
                 if (!is_null($value)) {
-                    $updateStmt->execute([$path, $name, $value]);
+                    if (is_scalar($value)) {
+                        $valueType = self::VT_STRING;
+                    } elseif ($value instanceof Complex) {
+                        $valueType = self::VT_XML;
+                        $value = $value->getXml();
+                    } else {
+                        $valueType = self::VT_OBJECT;
+                        $value = serialize($value);
+                    }
+                    $updateStmt->execute([$path, $name, $valueType, $value]);
                 } else {
                     $deleteStmt->execute([$path, $name]);
                 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list