Skip to content

Commit

Permalink
add option to limit results
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-sc committed Oct 2, 2024
1 parent d2ee23e commit af03465
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions Classes/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ protected function configure(): void
InputArgument::OPTIONAL,
'The version number of the most recently updated data set.'
)->
addOption(
'total',
't',
InputOption::VALUE_REQUIRED,
'Limit the total number of results for dev purposes and force fullSync.'
)->
addOption(
'all',
'a',
Expand All @@ -98,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$version = $this->getVersion($input);
if ($version == 0) {
$this->io->text('Full data synchronization requested.');
$this->fullSync();
$this->fullSync($input);
$this->logger->info('Full data synchronization successful.');
} else {
$this->io->text('Synchronizing all data from version ' . $version);
Expand All @@ -109,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

protected function fullSync(): void
protected function fullSync($input): void
{
$client = new ZoteroApi($this->extConf['zoteroApiKey']);
$response = $client->
Expand All @@ -118,7 +124,11 @@ protected function fullSync(): void
top()->
limit(1)->
send();
$this->total = (int) $response->getHeaders()['Total-Results'][0];
if ($input->getOption('total')) {
$this->total = (int) $input->getOption('total');
} else {
$this->total = (int) $response->getHeaders()['Total-Results'][0];
}

// fetch bibliography items bulkwise
$this->io->progressStart($this->total);
Expand Down Expand Up @@ -212,6 +222,13 @@ protected function getVersion($input): int
return 0;
}

// also set version to 0 for dev tests if the total results are limited
if ($input->getOption('total')) {
$this->io->text('Total results limited to: '. $input->getOption('total'));
return 0;
}


// if a version is manually specified, perform sync from this version
$argumentVersion = $input->getArgument('version');
if ($argumentVersion > 0) {
Expand All @@ -236,14 +253,15 @@ protected function getVersion($input): int
try {
$response = $this->client->search($params);
return (int) $response['aggregations']['max_version']['value'];
} catch (\Elasticsearch\Common\Exceptions\Missing404Exception $e) {
// Index not found, return 0
$this->io->note('No Index with name: ' .$this->extConf['elasticIndexName'] . ' found. Return 0 as Version, create new index in next steps...');
return 0;
} catch (\Exception $e) {
// Handle other potential exceptions if necessary
$this->io->error("Exception: " . $e->getMessage());
die;
if ($e->getCode() === 404) {
// Index not found, return 0
$this->io->note('No Index with name: ' . $this->extConf['elasticIndexName'] . ' found. Return 0 as Version, create new index in next steps...');
return 0;
} else {
$this->io->error("Exception: " . $e->getMessage());
die;
}
}
}

Expand Down

0 comments on commit af03465

Please sign in to comment.