Skip to content

Commit

Permalink
Add more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 6, 2024
1 parent 2f2a41f commit 08a5738
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 9 deletions.
68 changes: 68 additions & 0 deletions src/Console/AddRecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -40,6 +43,36 @@
)]
class AddRecordCommand extends Command
{
/**
* Configures the current command.
*
* @return void
*/
protected function configure(): void
{
$this->addArgument(
'identifier',
InputArgument::REQUIRED,
'The record identifier.'
);
$this->addArgument(
'format',
InputArgument::REQUIRED,
'The metadata prefix.'
);
$this->addArgument(
'file',
InputArgument::REQUIRED,
'The file containing the record content.'
);
$this->addArgument(
'sets',
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
'The list of sets to associate the record with.'
);
parent::configure();
}

/**
* Executes the current command.
*
Expand All @@ -50,7 +83,42 @@ class AddRecordCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var array<string, string> */
$arguments = $input->getArguments();
/** @var Format */
$format = Database::getInstance()->getEntityManager()->getReference(Format::class, $arguments['format']);

$content = file_get_contents($arguments['file']);
if ($content === false) {
$output->writeln([
'',
sprintf(
' [ERROR] File "%s" not found or not readable. ',
$arguments['file']
),
''
]);
return Command::INVALID;
}

$record = new Record($arguments['identifier'], $format);
if (trim($content) !== '') {
$record->setContent($content);
}
// TODO: Add full set support.

Database::getInstance()->addOrUpdateRecord($record);
Database::getInstance()->pruneOrphanSets();

$output->writeln([
'',
sprintf(
' [OK] Record "%s" with metadata prefix "%s" added or updated successfully! ',
$arguments['identifier'],
$arguments['format']
),
''
]);
return Command::SUCCESS;
}
}
63 changes: 60 additions & 3 deletions src/Console/DeleteRecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -41,6 +44,26 @@
)]
class DeleteRecordCommand extends Command
{
/**
* Configures the current command.
*
* @return void
*/
protected function configure(): void
{
$this->addArgument(
'identifier',
InputArgument::REQUIRED,
'The record identifier.'
);
$this->addArgument(
'format',
InputArgument::REQUIRED,
'The metadata prefix.'
);
parent::configure();
}

/**
* Executes the current command.
*
Expand All @@ -51,8 +74,42 @@ class DeleteRecordCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$policy = Configuration::getInstance()->deletedRecords;
Database::getInstance()->pruneOrphanSets();
return Command::SUCCESS;
/** @var array<string, string> */
$arguments = $input->getArguments();
$entityManager = Database::getInstance()->getEntityManager();

$format = $entityManager->getReference(Format::class, $arguments['format']);
$record = $entityManager->find(
Record::class,
[
'identifier' => $arguments['identifier'],
'format' => $format
]
);

if (isset($record)) {
Database::getInstance()->deleteRecord($record);
$output->writeln([
'',
sprintf(
' [OK] Record "%s" with metadata prefix "%s" successfully deleted. ',
$arguments['identifier'],
$arguments['format']
),
''
]);
return Command::SUCCESS;
} else {
$output->writeln([
'',
sprintf(
' [ERROR] Record "%s" with metadata prefix "%s" not found. ',
$arguments['identifier'],
$arguments['format']
),
''
]);
return Command::FAILURE;
}
}
}
2 changes: 1 addition & 1 deletion src/Console/UpdateFormatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
foreach (array_keys($inDatabase) as $prefix) {
if (!in_array($prefix, array_keys($formats), true)) {
Database::getInstance()->removeMetadataFormat($inDatabase[$prefix]);
Database::getInstance()->deleteMetadataFormat($inDatabase[$prefix]);
++$deleted;
$output->writeln([
sprintf(
Expand Down
23 changes: 21 additions & 2 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ public function pruneResumptionTokens(): int
}

/**
* Remove metadata format and all associated records.
* Delete metadata format and all associated records.
*
* @param Format $format The metadata format
*
* @return void
*/
public function removeMetadataFormat(Format $format): void
public function deleteMetadataFormat(Format $format): void
{
$repository = $this->entityManager->getRepository(Record::class);
$criteria = Criteria::create()->where(Criteria::expr()->eq('format', $format));
Expand All @@ -439,6 +439,25 @@ public function removeMetadataFormat(Format $format): void
$this->pruneOrphanSets();
}

/**
* Delete a record.
*
* @param Record $record The record
*
* @return void
*/
public function deleteRecord(Record $record): void
{
if (Configuration::getInstance()->deletedRecords === 'no') {
$this->entityManager->remove($record);
} else {
$record->setContent(null);
$record->setLastChanged(new DateTime());
}
$this->entityManager->flush();
$this->pruneOrphanSets();
}

/**
* This is a singleton class, thus the constructor is private.
*
Expand Down
1 change: 1 addition & 0 deletions src/Middleware/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected function processResponse(ResponseInterface $response): ResponseInterfa
{
// TODO: Add support for content compression
// https://openarchives.org/OAI/openarchivesprotocol.html#ResponseCompression
// https://github.com/middlewares/encoder
return $response->withHeader('Content-Type', 'text/xml');
}

Expand Down
9 changes: 6 additions & 3 deletions src/Middleware/Identify.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ protected function prepareResponse(ServerRequestInterface $request): void
$granularity = $document->createElement('granularity', 'YYYY-MM-DDThh:mm:ssZ');
$identify->appendChild($granularity);

// TODO: Add support for content compression
// $compression = $document->createElement('compression', '...');
// $identify->appendChild($compression);
// TODO: Implement explicit content compression support.
// $compressionDeflate = $document->createElement('compression', 'deflate');
// $identify->appendChild($compressionDeflate);

// $compressionGzip = $document->createElement('compression', 'gzip');
// $identify->appendChild($compressionGzip);

$this->preparedResponse = $document;
}
Expand Down

0 comments on commit 08a5738

Please sign in to comment.