Skip to content

Commit bde9cd3

Browse files
authored
Update sync to use pagination
1 parent 55e7ab4 commit bde9cd3

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

sync.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,44 @@
8989
$localColumns = []; // column names on local side to prepare SQL statements
9090

9191
// fetch the API result
92-
$results = $doGet($client, $settings['endpoint']);
93-
if ($results === false) {
94-
$io->error(sprintf('Failed to sync data for endpoint: %s', $settings['endpoint']));
95-
}
92+
$page = 1;
93+
while (true) {
94+
$separator = (strpos($settings['endpoint'], '?') === false) ? '?' : '&';
95+
$url = sprintf('%s%spage=%s&size=500', $settings['endpoint'], $separator, $page);
96+
$results = $doGet($client, $url);
97+
98+
if ($results === false) {
99+
$io->error(sprintf('Failed to sync data for endpoint: %s', $settings['endpoint']));
100+
break;
101+
}
96102

97-
// prepare the array of all entities for the local database by mapping columns
98-
foreach ($results as $entity) {
99-
$newEntity = [];
100-
foreach ($settings['mapping'] as $kimaiField => $localField) {
101-
$key = $localField;
102-
$value = $entity[$kimaiField];
103-
// some values need to be converted to local format (eg. datetime)
104-
if (is_callable($localField)) {
105-
$tmp = call_user_func($localField, $entity, $kimaiField);
106-
$key = $tmp[0];
107-
$value = $tmp[1];
108-
}
109-
$newEntity[$key] = $value;
103+
if (empty($results)) {
104+
break;
110105
}
111-
if (count($localColumns) === 0) {
112-
$localColumns = array_keys($newEntity);
106+
107+
// prepare the array of all entities for the local database by mapping columns
108+
foreach ($results as $entity) {
109+
$newEntity = [];
110+
foreach ($settings['mapping'] as $kimaiField => $localField) {
111+
$key = $localField;
112+
$value = $entity[$kimaiField];
113+
// some values need to be converted to local format (eg. datetime)
114+
if (is_callable($localField)) {
115+
$tmp = call_user_func($localField, $entity, $kimaiField);
116+
$key = $tmp[0];
117+
$value = $tmp[1];
118+
}
119+
$newEntity[$key] = $value;
120+
}
121+
if (count($localColumns) === 0) {
122+
$localColumns = array_keys($newEntity);
123+
}
124+
$apiEntities[$entity['id']] = $newEntity;
113125
}
114-
$apiEntities[$entity['id']] = $newEntity;
115-
}
116126

117-
unset($results);
127+
$page++;
128+
unset($results);
129+
}
118130

119131
if (count($apiEntities) === 0) {
120132
$io->success('No data found to sync: ' . $title);
@@ -238,7 +250,7 @@
238250

239251
$syncConfig['Timesheets'] = [
240252
'table' => 'timesheet',
241-
'endpoint' => 'timesheets?user=all&modified_after=' . $since->format('Y-m-d\TH:i:s') . '&size=' . PHP_INT_MAX,
253+
'endpoint' => 'timesheets?user=all&modified_after=' . $since->format('Y-m-d\TH:i:s'),
242254
'mapping' => [
243255
'id' => 'kimai_id',
244256
'activity' => 'activity',

0 commit comments

Comments
 (0)