Started working on the logic to import the events into a Drupal database
This commit is contained in:
parent
525e916fdd
commit
bb66308a6f
33
import.php
33
import.php
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
namespace Pandy06269\iCalImporter;
|
namespace Pandy06269\iCalImporter;
|
||||||
|
|
||||||
|
use Pandy06269\iCalImporter\includes\DrupalConfigFileReader;
|
||||||
|
use Pandy06269\iCalImporter\includes\DrupalDatabaseEventWriter;
|
||||||
use Pandy06269\iCalImporter\includes\Event;
|
use Pandy06269\iCalImporter\includes\Event;
|
||||||
use Pandy06269\iCalImporter\includes\FileReader;
|
|
||||||
use Pandy06269\iCalImporter\includes\ICSFileReader;
|
use Pandy06269\iCalImporter\includes\ICSFileReader;
|
||||||
|
|
||||||
//define('DEBUG', true);
|
//define('DEBUG', true);
|
||||||
@ -30,7 +31,7 @@ try
|
|||||||
$pathToDrupalConfig = trim($argv[2]);
|
$pathToDrupalConfig = trim($argv[2]);
|
||||||
|
|
||||||
$icsReader = new ICSFileReader($pathToICSFile);
|
$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('Reading iCal data from %s' . PHP_EOL, $icsReader->getPath());
|
||||||
echo sprintf('Using Drupal connection details from %s' . PHP_EOL, $drupalReader->getPath());
|
echo sprintf('Using Drupal connection details from %s' . PHP_EOL, $drupalReader->getPath());
|
||||||
@ -56,6 +57,34 @@ try
|
|||||||
echo '---' . PHP_EOL;
|
echo '---' . PHP_EOL;
|
||||||
$index++;
|
$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)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
|
13
includes/DrupalConfigFileReader.php
Normal file
13
includes/DrupalConfigFileReader.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pandy06269\iCalImporter\includes;
|
||||||
|
|
||||||
|
class DrupalConfigFileReader extends FileReader
|
||||||
|
{
|
||||||
|
public function getDatabaseConfig()
|
||||||
|
{
|
||||||
|
global $databases;
|
||||||
|
require $this->path;
|
||||||
|
return $databases['default']['default'];
|
||||||
|
}
|
||||||
|
}
|
56
includes/DrupalDatabaseEventWriter.php
Normal file
56
includes/DrupalDatabaseEventWriter.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pandy06269\iCalImporter\includes;
|
||||||
|
|
||||||
|
class DrupalDatabaseEventWriter extends MysqlEventWriter
|
||||||
|
{
|
||||||
|
public function __construct(DrupalConfigFileReader $configFileReader)
|
||||||
|
{
|
||||||
|
$this->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));
|
||||||
|
}
|
||||||
|
}
|
10
includes/IEventWriter.php
Normal file
10
includes/IEventWriter.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pandy06269\iCalImporter\includes;
|
||||||
|
|
||||||
|
interface IEventWriter
|
||||||
|
{
|
||||||
|
function close();
|
||||||
|
function open();
|
||||||
|
function upload(Event $event);
|
||||||
|
}
|
119
includes/MysqlEventWriter.php
Normal file
119
includes/MysqlEventWriter.php
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pandy06269\iCalImporter\includes;
|
||||||
|
|
||||||
|
abstract class MysqlEventWriter implements IEventWriter
|
||||||
|
{
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \mysqli
|
||||||
|
*/
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
public function close()
|
||||||
|
{
|
||||||
|
if (!is_null($this->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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user