gl-website-deployer/lib/schema.php
2024-11-19 08:02:04 +01:00

232 lines
8.3 KiB
PHP

<?php
require_once dirname(__FILE__).'/../vendor/autoload.php';
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
class PersoSchema extends Schema
{
/**
* @var Connection
*/
private $connection;
private $options;
private $sm;
private $publiTable;
private $publiHistory;
private $currentTime;
private $confirmMessage;
/**
* @param array $options the options could be use to make the table names configurable
*/
public function __construct(array $options = [], Connection $connection = null)
{
$this->connection = $connection;
$schemaConfig = null === $connection ? null : $connection->getSchemaManager()->createSchemaConfig();
parent::__construct([], [], $schemaConfig);
$this->options = $options;
$this->sm = $connection->getSchemaManager();
$this->addPublications();
$this->currentTime = new DateTimeImmutable();
$this->addCitationHistory();
$this->addConfirmMessage();
}
public function migrate()
{
$oldSchema = $this->sm->createSchema();
$db_platform = $this->connection->getDatabasePlatform();
foreach ($oldSchema->getMigrateToSql($this, $db_platform) as $sql) {
$this->connection->executeStatement($sql);
}
return true;
}
public function addPublications()
{
$publiTable = $this->createTable('publications');
$publiTable->addColumn('id', 'integer', ['unsigned' => true, 'customSchemaOptions' => ['unique' => true]]);
$publiTable->addColumn('arxiv', 'string', ['length' => 20, 'notnull' => false]);
$publiTable->addColumn('owned', 'boolean');
$publiTable->addColumn('doi', 'string', ['length' => 100, 'notnull' => false]);
$publiTable->addColumn('ads', 'string', ['length' => 100]);
$publiTable->addColumn('ads_docid', 'integer', ['unsigned' => true]);
$publiTable->addColumn('title', 'string', ['length' => 200]);
$publiTable->addColumn('authors', Types::JSON);
$publiTable->addColumn('citation_count', Types::INTEGER, ['unsigned' => true]);
$publiTable->addColumn('year', Types::INTEGER, ['unsigned' => true]);
$publiTable->addColumn('date', Types::DATETIME_IMMUTABLE);
$publiTable->addColumn('journal', Types::STRING, ['notnull' => false, 'length' => 256]);
$publiTable->addColumn('page', Types::STRING, ['notnull' => false, 'length' => 24]);
$publiTable->addColumn('volume', Types::STRING, ['notnull' => false, 'length' => 24]);
$publiTable->setPrimaryKey(['id']);
// $schema->createSequence("publi_table_seq");
}
public function addConfirmMessage()
{
$confirmMessage = $this->createTable('confirm_message');
$confirmMessage->addColumn('id', Types::INTEGER, ['unsigned' => true]);
$confirmMessage->addColumn('token', Types::STRING, ['length' => 24, 'notnull' => true]);
$confirmMessage->addColumn('sender_name', Types::STRING, ['length' => 80]);
$confirmMessage->addColumn('sender_email', Types::STRING, ['length' => 80]);
$confirmMessage->addColumn('body', Types::STRING, ['length' => 1000]);
}
public function addCitationHistory()
{
$publiHistory = $this->createTable('citation_history');
$publiHistory->addColumn('id', Types::INTEGER, ['unsigned' => true]);
$publiHistory->addColumn('date', Types::DATETIME_IMMUTABLE);
$publiHistory->addColumn('citation_count', Types::INTEGER, ['unsigned' => true]);
}
public function insertPublication(array $doc, bool $update = false)
{
$data = [
'id' => intval($doc['id']),
'ads' => $doc['bibcode'],
'title' => $doc['title'][0],
'ads_docid' => intval($doc['id']),
'authors' => $doc['author'],
'citation_count' => intval($doc['citation_count']),
'year' => intval($doc['year']),
'date' => new DateTimeImmutable($doc['date']),
'journal' => $doc['pub'] ?? null,
'page' => $doc['page'][0] ?? null,
'volume' => $doc['volume'] ?? null,
];
// echo "Inserting " . json_encode($doc);
if (!array_key_exists('doi', $doc) || count($doc['doi']) == 0) {
$doi = null;
} else {
$data['doi'] = $doc['doi'][0];
}
if (array_key_exists('identifier', $doc)) {
foreach ($doc['identifier'] as $pubid) {
if (str_starts_with($pubid, 'arXiv:')) {
$data['arxiv'] = substr($pubid, 6);
break;
}
}
}
$types = ['authors' => Types::JSON, 'date' => Types::DATETIME_IMMUTABLE];
if ($update) {
$this->connection->update(
'publications',
$data,
['id' => $data['id']],
$types
);
} else {
$data['owned'] = true;
$this->connection->insert(
'publications',
$data,
$types
);
}
}
public function refreshHistory(array $doc)
{
$data = [
'id' => intval($doc['id']),
'date' => $this->currentTime,
'citation_count' => intval($doc['citation_count']),
];
$types = ['date' => Types::DATETIME_IMMUTABLE];
$this->connection->insert(
'citation_history',
$data,
$types
);
}
public function submitMessage(string $name, string $email, string $message)
{
$data = [
'sender_name' => $name,
'sender_email' => $email,
'message' => $message,
'token' => $mytoken,
];
$this->connection->insert(
'confirm_message',
$data
);
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.iap.fr'; // Set the SMTP server to send through
$mail->Port = 587; // TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
// Recipients
$mail->setFrom('@iap.fr', 'Web Mailer');
$mail->addAddress($email, 'submitter'); // Add a recipient
// Content
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Guilhem website: confirm message';
$mail->Body = $confirm_body;
$mail->send();
$result_summary = 'Successfully sent';
$result = 'Message has been sent';
} catch (Exception $e) {
$result_summary = 'Error sending message';
$result = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
public function confirmMessage(string $token)
{
// Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
$tpl = $m->loadTemplate('email_sent');
try {
// Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.iap.fr'; // Set the SMTP server to send through
$mail->Port = 587; // TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
// Recipients
$mail->setFrom('guilhem.lavaux@iap.fr', 'Web Mailer');
$mail->addAddress('guilhem.lavaux@iap.fr', 'Guilhem Lavaux'); // Add a recipient
$mail->addReplyTo($email, $name);
// Content
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Guilhem website: Message from '.$name;
$mail->Body = $message;
$mail->send();
$result_summary = 'Successfully sent';
$result = 'Message has been sent';
} catch (Exception $e) {
$result_summary = 'Error sending message';
$result = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
}