1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2015, 2016 Stephan Kreutzer, Michael Giesler |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
namespace AdminBundle\Command; |
20
|
|
|
|
21
|
|
|
use AdminBundle\Service\TwineImport\ImportTwine; |
22
|
|
|
use DembeloMain\Document\Importfile; |
23
|
|
|
use DembeloMain\Model\Repository\ImportfileRepositoryInterface; |
24
|
|
|
use DembeloMain\Model\Repository\LicenseeRepositoryInterface; |
25
|
|
|
use DembeloMain\Model\Repository\TopicRepositoryInterface; |
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
29
|
|
|
use Symfony\Component\Console\Input\InputOption; |
30
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
31
|
|
|
use Symfony\Component\Console\Command\Command; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class ImportCommand |
35
|
|
|
*/ |
36
|
|
|
class ImportCommand extends Command |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $twineArchivePath; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var OutputInterface |
45
|
|
|
*/ |
46
|
|
|
private $output; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|null |
50
|
|
|
*/ |
51
|
|
|
private $licenseeId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $topicId; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $author = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $publisher = ''; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var ImportTwine |
70
|
|
|
*/ |
71
|
|
|
private $importTwine; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var LicenseeRepositoryInterface |
75
|
|
|
*/ |
76
|
|
|
private $licenseeRepository; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var TopicRepositoryInterface |
80
|
|
|
*/ |
81
|
|
|
private $topicRepository; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var ImportfileRepositoryInterface |
85
|
|
|
*/ |
86
|
|
|
private $importfileRepository; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ImportTwine $importTwine |
90
|
|
|
* @param LicenseeRepositoryInterface $licenseeRepository |
91
|
|
|
* @param TopicRepositoryInterface $topicRepository |
92
|
|
|
* @param ImportfileRepositoryInterface $importfileRepository |
93
|
|
|
*/ |
94
|
5 |
|
public function __construct(ImportTwine $importTwine, LicenseeRepositoryInterface $licenseeRepository, TopicRepositoryInterface $topicRepository, ImportfileRepositoryInterface $importfileRepository) |
95
|
|
|
{ |
96
|
5 |
|
parent::__construct(); |
97
|
5 |
|
$this->importTwine = $importTwine; |
98
|
5 |
|
$this->licenseeRepository = $licenseeRepository; |
99
|
5 |
|
$this->topicRepository = $topicRepository; |
100
|
5 |
|
$this->importfileRepository = $importfileRepository; |
101
|
5 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* configures the symfony cli command |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
5 |
|
protected function configure(): void |
109
|
|
|
{ |
110
|
|
|
$this |
111
|
5 |
|
->setName('dembelo:import') |
112
|
5 |
|
->setDescription('Twine Archive Import') |
113
|
5 |
|
->addArgument( |
114
|
5 |
|
'twine-archive-file', |
115
|
5 |
|
InputArgument::REQUIRED, |
116
|
5 |
|
'The path of the Twine archive file.' |
117
|
|
|
) |
118
|
5 |
|
->addOption( |
119
|
5 |
|
'licensee-name', |
120
|
5 |
|
'l', |
121
|
5 |
|
InputOption::VALUE_REQUIRED, |
122
|
5 |
|
'The name of the licensee to which the imported textnodes belong to.' |
123
|
|
|
) |
124
|
5 |
|
->addOption( |
125
|
5 |
|
'topic-name', |
126
|
5 |
|
't', |
127
|
5 |
|
InputOption::VALUE_REQUIRED, |
128
|
5 |
|
'The name of the topic to which the imported textnodes belong to.' |
129
|
|
|
) |
130
|
5 |
|
->addOption( |
131
|
5 |
|
'metadata-author', |
132
|
5 |
|
'a', |
133
|
5 |
|
InputOption::VALUE_REQUIRED, |
134
|
5 |
|
'The author of all the stories in the Twine archive file (will end up as metadata).' |
135
|
|
|
) |
136
|
5 |
|
->addOption( |
137
|
5 |
|
'metadata-publisher', |
138
|
5 |
|
'p', |
139
|
5 |
|
InputOption::VALUE_REQUIRED, |
140
|
5 |
|
'The publisher of all the stories in the Twine archive file (will end up as metadata).' |
141
|
|
|
); |
142
|
5 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param InputInterface $input |
146
|
|
|
* @param OutputInterface $output |
147
|
|
|
* |
148
|
|
|
* @return int |
149
|
|
|
* |
150
|
|
|
* @throws \Exception |
151
|
|
|
*/ |
152
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
153
|
|
|
{ |
154
|
4 |
|
$this->output = $output; |
155
|
4 |
|
$this->prepare($input); |
156
|
|
|
|
157
|
4 |
|
if (file_exists($this->twineArchivePath) !== true) { |
158
|
1 |
|
$this->output->writeln("<error>Parameter 'twine-archive-file': File '".$this->twineArchivePath."' doesn't exist.</error>"); |
159
|
|
|
|
160
|
1 |
|
return -1; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
if (is_readable($this->twineArchivePath) !== true) { |
164
|
1 |
|
$this->output->writeln("<error>Parameter 'twine-archive-file': File '".$this->twineArchivePath."' isn't readable.</error>"); |
165
|
|
|
|
166
|
1 |
|
return -1; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
try { |
170
|
2 |
|
$importfile = new Importfile(); |
171
|
2 |
|
$importfile->setFilename($this->twineArchivePath); |
172
|
2 |
|
$importfile->setLicenseeId($this->licenseeId); |
|
|
|
|
173
|
2 |
|
$importfile->setAuthor($this->author); |
174
|
2 |
|
$importfile->setPublisher($this->publisher); |
175
|
2 |
|
$importfile->setTopicId($this->topicId); |
|
|
|
|
176
|
|
|
|
177
|
2 |
|
$this->importfileRepository->save($importfile); |
178
|
|
|
|
179
|
2 |
|
$this->importTwine->run($importfile); |
180
|
|
|
|
181
|
1 |
|
$this->importfileRepository->save($importfile); |
182
|
1 |
|
} catch (\Exception $ex) { |
183
|
1 |
|
$output->writeln('<error>'.$ex->getMessage().'</error>'); |
184
|
|
|
|
185
|
1 |
|
$this->importTwine->parserFree(); |
186
|
|
|
|
187
|
1 |
|
return -1; |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
return 0; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param InputInterface $input |
195
|
|
|
* |
196
|
|
|
* @throws \Exception |
197
|
|
|
*/ |
198
|
4 |
|
private function prepare(InputInterface $input) |
199
|
|
|
{ |
200
|
4 |
|
$styleWarning = new OutputFormatterStyle('black', 'yellow'); |
201
|
4 |
|
$this->output->getFormatter()->setStyle('warning', $styleWarning); |
202
|
|
|
|
203
|
4 |
|
$licensee = $this->licenseeRepository->findOneBy(['name' => $input->getOption('licensee-name')]); |
204
|
4 |
|
if (null === $licensee) { |
205
|
|
|
throw new \Exception(sprintf("<error>A Licensee named '%s' doesn't exist.</error>", $input->getOption('licensee-name'))); |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
$topic = $this->topicRepository->findOneBy(['name' => $input->getOption('topic-name')]); |
209
|
4 |
|
if (null === $topic) { |
210
|
|
|
throw new \Exception(sprintf("<error>A Topic named '%s' doesn't exist.</error>", $input->getOption('topic-name'))); |
211
|
|
|
} |
212
|
4 |
|
$this->topicId = $topic->getId(); |
213
|
|
|
|
214
|
4 |
|
$this->author = $input->getOption('metadata-author'); |
215
|
4 |
|
$this->publisher = $input->getOption('metadata-publisher'); |
216
|
|
|
|
217
|
4 |
|
$this->licenseeId = $licensee->getId(); |
218
|
|
|
|
219
|
4 |
|
$this->twineArchivePath = $input->getArgument('twine-archive-file'); |
220
|
4 |
|
} |
221
|
|
|
} |
222
|
|
|
|