Skip to content

Commit

Permalink
fix the bug where the difference between the bulk size * loop and the…
Browse files Browse the repository at this point in the history
… overlapping hits was not taken, improve loop handling and error prevention
  • Loading branch information
thomas-sc committed May 21, 2024
1 parent a1715dd commit c3e0454
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions Classes/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;


/* ToDo:
- exception handling (for networking problems, api down etc.?) and check empty zoteroAPIKey, zoteroGroupId
- sfae the "Last-Modified-Version" Header from zotero and check if the data has changed since the last fetch
- potential infinite loops, exit strategy with loop count in try/catch
- fetch the api in bulks of 50 and save bulk wise in elasticsearch?
*/
class IndexCommand extends Command
{

Expand Down Expand Up @@ -136,22 +143,31 @@ protected function fetchBibliography(): void
$this->total = 1;
$cursor = 0;
// fetch bibliography items bulkwise
while ($cursor < $this->total) {
while ($cursor < ($this->total + $this->bulkSize)) {
$response = $client->
group($this->extConf['zoteroGroupId'])->
items()->
top()->
start($cursor)->
limit($this->bulkSize)->
send();
$this->total = (int)$response->getHeaders()['Total-Results'][0];
$headers = $response->getHeaders();
if (isset($headers['Total-Results'][0])) {
$this->total = (int)$headers['Total-Results'][0];
} else {
$this->io->error('break fetchBibliography loop because of missing Total-Results, ' .$this->total);
break; // break the loop if there is no result header to prevent infinite loops
}
if ($cursor === 0) {
$this->io->progressStart($this->total);
}
$collection = new Collection($response->getBody());
$this->bibliographyItems = $this->bibliographyItems->
concat($collection->pluck('data'));
$this->io->progressAdvance($this->bulkSize);
// adjust progress bar
$remainingItems = $this->total - $cursor;
$advanceBy = min($remainingItems, $this->bulkSize);
$this->io->progressAdvance($advanceBy);
$cursor += $this->bulkSize;
}
$this->io->progressFinish();
Expand All @@ -173,7 +189,7 @@ protected function fetchCitationLocale(string $locale): void
$result = new Collection();
$cursor = 0;
// fetch bibliography items bulkwise
while ($cursor < $this->total) {
while ($cursor < ($this->total + $this->bulkSize)) {
if ($cursor === 0) {
$this->io->progressStart($this->total);
}
Expand All @@ -190,7 +206,10 @@ protected function fetchCitationLocale(string $locale): void
setLocale($locale)->
send();
$result = $result->merge(Collection::wrap($response->getBody())->keyBy('key'));
$this->io->progressAdvance($this->bulkSize);
// adjust progress bar
$remainingItems = $this->total - $cursor;
$advanceBy = min($remainingItems, $this->bulkSize);
$this->io->progressAdvance($advanceBy);
$cursor += $this->bulkSize;
} catch (\Exception $e) {
$this->io->newline(2);
Expand All @@ -210,7 +229,7 @@ protected function fetchTeiData(): void
$this->teiDataSets = new Collection();
$cursor = 0;
// fetch bibliography items bulkwise
while ($cursor < $this->total) {
while ($cursor < ($this->total + $this->bulkSize)) {
if ($cursor === 0) {
$this->io->progressStart($this->total);
}
Expand All @@ -226,7 +245,10 @@ protected function fetchTeiData(): void
$collection = new Collection($response->getBody());
$this->teiDataSets = $this->teiDataSets->
concat($collection->keyBy('key'));
$this->io->progressAdvance($this->bulkSize);
// adjust progress bar
$remainingItems = $this->total - $cursor;
$advanceBy = min($remainingItems, $this->bulkSize);
$this->io->progressAdvance($advanceBy);
$cursor += $this->bulkSize;
} catch (\Exception $e) {
$this->io->newline(2);
Expand Down

0 comments on commit c3e0454

Please sign in to comment.