Skip to content

Commit

Permalink
fix cursor=0 to start read data from 0 (not from 50)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-sc committed Oct 2, 2024
1 parent af03465 commit 67aca47
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions Classes/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ class IndexCommand extends Command
protected Collection $locales;
protected Collection $localizedCitations;

function __construct(
SiteFinder $siteFinder,
public function __construct(
private readonly SiteFinder $siteFinder,
private readonly LoggerInterface $logger
)
{
) {
parent::__construct();
$this->initLocales();
}

$this->locales = Collection::wrap($siteFinder->getAllSites())->
map(function (Site $site): array { return $site->getLanguages(); })->
flatten()->
map(function (SiteLanguage $language): string { return $language->getHreflang(); });
private function initLocales(): void
{
$this->locales = Collection::wrap($this->siteFinder->getAllSites())
->map(function (Site $site): array { return $site->getLanguages(); })
->flatten()
->map(function (SiteLanguage $language): string { return $language->getHreflang(); });
}

protected function getRequest(): ServerRequestInterface
Expand Down Expand Up @@ -90,7 +93,8 @@ protected function configure(): void
);
}

protected function initialize(InputInterface $input, OutputInterface $output) {
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_bibliography');
$this->client = ElasticClientBuilder::getClient();
$this->apiKey = $this->extConf['zoteroApiKey'];
Expand All @@ -111,11 +115,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->versionedSync($version);
$this->logger->info('Versioned data synchronization successful.');
}

return 0;
return Command::SUCCESS;
}

protected function fullSync($input): void
protected function fullSync(InputInterface $input): void
{
$client = new ZoteroApi($this->extConf['zoteroApiKey']);
$response = $client->
Expand All @@ -133,10 +136,8 @@ protected function fullSync($input): void
// fetch bibliography items bulkwise
$this->io->progressStart($this->total);
$collection = new Collection($response->getBody());
$this->bibliographyItems = $collection->
pluck('data');

$cursor = $this->bulkSize;
$this->bibliographyItems = $collection->pluck('data');
$cursor = 0; // set Cursor to 0, not to bulk size
$index = $this->extConf['elasticIndexName'];
try {
// in older Elasticsearch versions (until 7) exists returns a bool
Expand All @@ -158,7 +159,7 @@ protected function fullSync($input): void

$apiCounter = self::API_TRIALS;

while ($cursor < $this->total + $this->bulkSize) {
while ($cursor < $this->total) {
try {
$this->sync($cursor, 0);

Expand Down Expand Up @@ -198,7 +199,7 @@ protected function versionedSync(int $version): void
if ($apiCounter == 0) {
$this->io->note('Giving up after ' . self::API_TRIALS . ' trials.');
$this->logger->warning('Bibliography sync unseccessful. Zotero API sent {trials} 500 errors.', ['trials' => self::API_TRIALS]);
die;
die; // Todo: die ist not recommended, better throw an exception?
} else {
$this->io->note('Trying again. ' . --$apiCounter . ' trials left.');
}
Expand All @@ -215,7 +216,7 @@ protected function sync(int $cursor = 0, int $version = 0): void
$this->commitBibliography();
}

protected function getVersion($input): int
protected function getVersion(InputInterface $input): int
{
// if -a is specified, perfom a full update
if ($input->getOption('all')) {
Expand Down Expand Up @@ -260,7 +261,7 @@ protected function getVersion($input): int
return 0;
} else {
$this->io->error("Exception: " . $e->getMessage());
die;
die; // Todo: die ist not recommended, better throw an exception?
}
}
}
Expand Down

0 comments on commit 67aca47

Please sign in to comment.