MCPcopy Create free account
hub / github.com/Rello/analytics / importClipboard

Method importClipboard

lib/Service/DataloadService.php:589–671  ·  view source on GitHub ↗

* Import clipboard data * * @NoAdminRequired * @param int $objectId * @param $import * @param bool $isDataset * @return array|false * @throws \OCP\DB\Exception */

($objectId, $import, bool $isDataset, $storageMapping = null, $header = null, $delimiter = null)

Source from the content-addressed store, hash-verified

587 * @throws \OCP\DB\Exception
588 */
589 public function importClipboard($objectId, $import, bool $isDataset, $storageMapping = null, $header = null, $delimiter = null)
590 {
591 $datasetId = $this->getDatasetId($objectId, $isDataset);
592 if ($datasetId != '') {
593 $dataset = $this->DatasetService->read((int)$datasetId);
594 if (is_array($dataset) && ($dataset['storageMode'] ?? 'legacy') === 'flexible_shared') {
595 if ($import === '') {
596 return ['insert' => 0, 'update' => 0, 'delete' => 0, 'error' => 1, 'validationErrors' => [[
597 'code' => 'invalid_clipboard_import',
598 'message' => 'No data was provided.',
599 'details' => [],
600 ]]];
601 }
602 try {
603 if ($storageMapping === null) {
604 $delimiter = $this->detectDelimiter($import);
605 $rows = array_map(static fn (string $row): array => str_getcsv($row, $delimiter), str_getcsv($import, "\n"));
606 $columns = $this->FlexibleStorageService->getDescriptor((int)$datasetId)['columns'];
607 $header = array_column($columns, 'name');
608 if ($rows !== [] && array_map('trim', $rows[0]) === $header) {
609 array_shift($rows);
610 }
611 $storageMapping = $this->positionalImportMapping($columns, $header, $rows);
612 } else {
613 if (!is_array($header) || !is_string($delimiter) || $delimiter === '') {
614 throw new FlexibleStorageException('invalid_clipboard_import', 'Flexible clipboard imports require explicit header and delimiter values.');
615 }
616 $rows = array_map(static fn (string $row): array => str_getcsv($row, $delimiter), str_getcsv($import, "\n"));
617 }
618 $result = $this->FlexibleStorageService->executeMappedLoad((int)$datasetId, $storageMapping, $header, $rows, false);
619 $result['delimiter'] = $delimiter;
620 $this->DatasetService->provider((int)$datasetId);
621 return $result;
622 } catch (FlexibleStorageException $e) {
623 return ['insert' => 0, 'update' => 0, 'delete' => 0, 'error' => 1, 'validationErrors' => [$e->toResponse()['error']]];
624 }
625 }
626 $insert = $update = $errorMessage = $errorCounter = 0;
627 $delimiter = '';
628
629 if ($import === '') {
630 $errorMessage = $this->l10n->t('No data');
631 } else {
632 $delimiter = $this->detectDelimiter($import);
633 $rows = str_getcsv($import, "\n");
634
635 foreach ($rows as &$row) {
636 $row = str_getcsv($row, $delimiter);
637 $numberOfColumns = count($row);
638 // last column needs to be a float
639 $row[2] = $this->floatvalue($row[$numberOfColumns - 1]);
640 if ($row[2] === false) {
641 $errorCounter++;
642 } else {
643 if ($numberOfColumns < 3) $row[1] = null;
644 $action = $this->StorageService->update($datasetId, $row[0], $row[1], $row[2]);
645 $insert = $insert + $action['insert'];
646 $update = $update + $action['update'];

Callers

nothing calls this directly

Calls 12

getDatasetIdMethod · 0.95
detectDelimiterMethod · 0.95
floatvalueMethod · 0.95
getDescriptorMethod · 0.80
executeMappedLoadMethod · 0.80
toResponseMethod · 0.80
countFunction · 0.50
readMethod · 0.45
providerMethod · 0.45
tMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected