1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Nicolas CARPi <[email protected]> |
5
|
|
|
* @copyright 2012, 2022 Nicolas CARPi |
6
|
|
|
* @see https://www.elabftw.net Official website |
7
|
|
|
* @license AGPL-3.0 |
8
|
|
|
* @package elabftw |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Elabftw\Import; |
14
|
|
|
|
15
|
|
|
use DateTimeImmutable; |
16
|
|
|
use Elabftw\Enums\EntityType; |
17
|
|
|
use Elabftw\Exceptions\ImproperActionException; |
18
|
|
|
use Elabftw\Models\Users; |
19
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
20
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
|
|
|
21
|
|
|
use Override; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Import entries from a csv file. |
25
|
|
|
*/ |
26
|
|
|
final class Csv extends AbstractCsv |
27
|
|
|
{ |
28
|
|
|
public function __construct( |
29
|
|
|
protected Users $requester, |
30
|
|
|
protected string $canread, |
31
|
|
|
protected string $canwrite, |
32
|
|
|
protected UploadedFile $UploadedFile, |
33
|
|
|
protected LoggerInterface $logger, |
34
|
|
|
protected EntityType $entityType = EntityType::Items, |
35
|
|
|
protected ?int $category = null, |
36
|
|
|
) { |
37
|
|
|
parent::__construct( |
38
|
4 |
|
$requester, |
39
|
|
|
$UploadedFile, |
40
|
|
|
); |
41
|
4 |
|
// we might have been forced to cast to int a null value, so bring it back to null |
42
|
|
|
if ($this->category === 0) { |
43
|
4 |
|
$this->category = null; |
44
|
|
|
} |
45
|
4 |
|
} |
46
|
|
|
|
47
|
4 |
|
/** |
48
|
4 |
|
* Do the work |
49
|
4 |
|
* |
50
|
|
|
* @throws ImproperActionException |
51
|
|
|
*/ |
52
|
4 |
|
#[Override] |
53
|
4 |
|
public function import(): int |
54
|
|
|
{ |
55
|
4 |
|
$entity = $this->entityType->toInstance($this->requester); |
56
|
1 |
|
foreach ($this->reader->getRecords() as $row) { |
57
|
1 |
|
// fail hard if no title column can be found, or we end up with a bunch of Untitled entries |
58
|
|
|
if (empty($row['title'])) { |
59
|
4 |
|
throw new ImproperActionException('Could not find the title column!'); |
60
|
|
|
} |
61
|
|
|
$body = null; |
62
|
4 |
|
if (array_key_exists('body', $row) && !empty($row['body'])) { |
63
|
4 |
|
$body = $row['body']; |
64
|
1 |
|
} |
65
|
|
|
$date = empty($row['date']) ? null : new DateTimeImmutable($row['date']); |
66
|
3 |
|
$category = $this->category; |
67
|
3 |
|
// use the category_title of the row only if we didn't specify a category |
68
|
3 |
|
if (array_key_exists('category_title', $row) && $this->category === null) { |
69
|
3 |
|
$category = $this->getCategoryId($this->entityType, $this->requester, $row['category_title']); |
70
|
3 |
|
} |
71
|
2 |
|
$status = empty($row['status_title']) ? null : $this->getStatusId($this->entityType, $row['status_title']); |
72
|
|
|
$customId = empty($row['custom_id']) ? null : (int) $row['custom_id']; |
73
|
|
|
$metadata = empty($row['metadata']) ? null : (string) $row['metadata']; |
74
|
3 |
|
if ($metadata === null) { |
75
|
2 |
|
$metadata = $this->collectMetadata($row); |
76
|
|
|
} |
77
|
3 |
|
$tags = empty($row['tags']) ? array() : explode(self::TAGS_SEPARATOR, $row['tags']); |
78
|
3 |
|
$canread = empty($row['canread']) ? $this->canread : $row['canread']; |
79
|
3 |
|
$canwrite = empty($row['canwrite']) ? $this->canwrite : $row['canwrite']; |
80
|
3 |
|
|
81
|
3 |
|
$entity->create( |
82
|
3 |
|
title: $row['title'], |
83
|
3 |
|
body: $body, |
84
|
3 |
|
canread: $canread, |
85
|
3 |
|
canwrite: $canwrite, |
86
|
3 |
|
date: $date, |
87
|
3 |
|
tags: $tags, |
88
|
3 |
|
template: $category, |
89
|
3 |
|
// use template and category so it works for items and experiments |
90
|
|
|
category: $category, |
91
|
3 |
|
status: $status, |
92
|
|
|
customId: $customId, |
93
|
|
|
metadata: $metadata, |
94
|
3 |
|
rating: (int) ($row['rating'] ?? 0), |
95
|
3 |
|
); |
96
|
|
|
|
97
|
|
|
$this->inserted++; |
98
|
3 |
|
} |
99
|
|
|
return $this->getInserted(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
#[Override] |
103
|
|
|
protected function getProcessedColumns(): array |
104
|
|
|
{ |
105
|
|
|
return array( |
106
|
|
|
'canread', |
107
|
3 |
|
'canwrite', |
108
|
|
|
'category', |
109
|
|
|
'category_title', |
110
|
3 |
|
'custom_id', |
111
|
|
|
'date', |
112
|
3 |
|
'metadata', |
113
|
|
|
'status', |
114
|
3 |
|
'status_title', |
115
|
|
|
'tags', |
116
|
3 |
|
'title', |
117
|
3 |
|
); |
118
|
3 |
|
} |
119
|
|
|
} |
120
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths