diff --git a/import.php b/import.php index 5789355..d819233 100644 --- a/import.php +++ b/import.php @@ -2,8 +2,9 @@ namespace Pandy06269\iCalImporter; +use Pandy06269\iCalImporter\includes\DrupalConfigFileReader; +use Pandy06269\iCalImporter\includes\DrupalDatabaseEventWriter; use Pandy06269\iCalImporter\includes\Event; -use Pandy06269\iCalImporter\includes\FileReader; use Pandy06269\iCalImporter\includes\ICSFileReader; //define('DEBUG', true); @@ -30,7 +31,7 @@ try $pathToDrupalConfig = trim($argv[2]); $icsReader = new ICSFileReader($pathToICSFile); - $drupalReader = new FileReader($pathToDrupalConfig); + $drupalReader = new DrupalConfigFileReader($pathToDrupalConfig); echo sprintf('Reading iCal data from %s' . PHP_EOL, $icsReader->getPath()); echo sprintf('Using Drupal connection details from %s' . PHP_EOL, $drupalReader->getPath()); @@ -56,6 +57,34 @@ try echo '---' . PHP_EOL; $index++; } + + echo PHP_EOL . '*** Imported ICS file ***' . PHP_EOL; + + if (count($events) == 0) + { + exit(0); + } + + echo '*** Ready to upload to Drupal ***' . PHP_EOL . PHP_EOL; + + $character = readline('Do you want to continue? '); + if (strtolower($character) !== 'y') + { + exit(0); + } + + echo PHP_EOL; + + $target = new DrupalDatabaseEventWriter($drupalReader); + $target->open(); + + /** @var Event $event */ + foreach ($events as $event) + { + $target->upload($event); + } + + $target->close(); } catch (\Exception $e) { diff --git a/includes/DrupalConfigFileReader.php b/includes/DrupalConfigFileReader.php new file mode 100644 index 0000000..65bbb00 --- /dev/null +++ b/includes/DrupalConfigFileReader.php @@ -0,0 +1,13 @@ +path; + return $databases['default']['default']; + } +} \ No newline at end of file diff --git a/includes/DrupalDatabaseEventWriter.php b/includes/DrupalDatabaseEventWriter.php new file mode 100644 index 0000000..049fe1b --- /dev/null +++ b/includes/DrupalDatabaseEventWriter.php @@ -0,0 +1,56 @@ +config = $configFileReader->getDatabaseConfig(); + } + + public function upload(Event $event) + { + // First insert the `node` record + $this->executeQuery( + 'INSERT INTO `node` (`vid`, `type`, `uuid`, `langcode`) VALUES (?, ?, ?, ?)', + ['i', 's', 's', 's'], + [0, 'private_event', $this->guidv4(), 'en'] + ); + $nodeID = $this->getLastInsertedID(); + + // Now add a `node_revision` record + $this->executeQuery( + 'INSERT INTO `node_revision` (`nid`, `langcode`, `revision_timestamp`, `revision_uid`) VALUES (?, ?, ?, ?)', + ['i', 's', 'i', 'i'], + [$nodeID, 'en', time(), 1] + ); + $revisionID = $this->getLastInsertedID(); + + // Update the original node record with the vid + $this->executeQuery( + 'UPDATE `node` SET `vid` = ? WHERE `nid` = ?', + ['i', 'i'], + [$revisionID, $nodeID] + ); + + // Now add a `node_field_data` record + $this->executeQuery( + 'INSERT INTO `node_field_data` (`nid`, `vid`, `type`, `langcode`, `title`, `uid`, `status`, `created`, `changed`, `promote`, `sticky`, `revision_translation_affected`, `default_langcode`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + ['i', 'i', 's', 's', 's', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], + [$nodeID, $revisionID, 'private_event', 'en', $event->getTitle(), 1, 1, time(), time(), 0, 0, 1, 1] + ); + exit(); + } + + private function guidv4() + { + if (function_exists('com_create_guid') === true) + return trim(com_create_guid(), '{}'); + + $data = openssl_random_pseudo_bytes(16); + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); + } +} \ No newline at end of file diff --git a/includes/IEventWriter.php b/includes/IEventWriter.php new file mode 100644 index 0000000..e103214 --- /dev/null +++ b/includes/IEventWriter.php @@ -0,0 +1,10 @@ +connection) && $this->connection->ping()) + { + $this->connection->close(); + } + } + + public function open() + { + echo sprintf('Connecting to Drupal database called "%s" on "%s:%d"' . PHP_EOL, $this->config['database'], $this->config['host'], $this->config['port']); + + $this->connection = new \mysqli( + $this->config['host'], + $this->config['username'], + $this->config['password'], + //$this->config['database'], + 'drupal_osborne_2', + $this->config['port'] + ); + + if ($this->connection->connect_errno) + { + throw new \Exception(sprintf('Error connecting to database: %s', $this->connection->connect_error)); + } + } + + abstract function upload(Event $event); + + protected function executeQuery($sql, array $paramTypes = [], array $paramValues = []) + { + $statement = $this->prepareQueryStatement($sql, $paramTypes, $paramValues); + + $returnCode = $statement->execute(); + if ($returnCode === false) + { + throw new \Exception(sprintf('Failed executing the query: %s', $this->connection->error)); + } + } + + protected function executeQueryResult($sql, array $paramTypes = [], array $paramValues = []) + { + $statement = $this->prepareQueryStatement($sql, $paramTypes, $paramValues); + + $returnCode = $statement->execute(); + if ($returnCode === false) + { + throw new \Exception(sprintf('Failed executing the query: %s', $this->connection->error)); + } + + $results = []; + $result = $statement->get_result(); + while ($row = $result->fetch_array(MYSQLI_ASSOC)) + { + $results[] = $row; + } + + return $results; + } + + protected function getLastInsertedID() + { + $result = $this->executeQueryResult('SELECT LAST_INSERT_ID() AS id'); + if (count($result) > 0) + { + return intval($result[0]['id']); + } + + return -1; + } + + private function prepareQueryStatement($sql, array $paramTypes = [], array $paramValues = []) + { + $statement = $this->connection->prepare($sql); + if ($statement === false) + { + throw new \Exception(sprintf('Failed preparing the query: %s', $this->connection->error)); + } + + if (count($paramTypes) > 0 && count($paramValues) > 0) + { + $a_params = array(); + + $param_type = ''; + $n = count($paramTypes); + for ($i = 0; $i < $n; $i++) + { + $param_type .= $paramTypes[$i]; + } + + /* with call_user_func_array, array params must be passed by reference */ + $a_params[] = &$param_type; + + for ($i = 0; $i < $n; $i++) + { + /* with call_user_func_array, array params must be passed by reference */ + $a_params[] = &$paramValues[$i]; + } + + /* Prepare statement */ + call_user_func_array(array($statement, 'bind_param'), $a_params); + } + + return $statement; + } +} \ No newline at end of file