-
Notifications
You must be signed in to change notification settings - Fork 926
/
Copy pathCSVImportExportPlugin.php
157 lines (134 loc) · 4.34 KB
/
CSVImportExportPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @file plugins/importexport/csv/CSVImportExportPlugin.php
*
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2003-2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CSVImportExportPlugin
*
* @ingroup plugins_importexport_csv
*
* @brief CSV import/export plugin
*/
namespace APP\plugins\importexport\csv;
use APP\core\Application;
use APP\facades\Repo;
use APP\plugins\importexport\csv\classes\commands\IssueCommand;
use APP\plugins\importexport\csv\classes\commands\UserCommand;
use PKP\plugins\ImportExportPlugin;
use PKP\user\User;
class CSVImportExportPlugin extends ImportExportPlugin
{
// Which command is the tool using from CLI. Currently supports "issues" or "users"
private string $command;
// Username passed as parameter from CLI
private string $username;
// User registered on system to perform the CLI command
private User $user;
// The folder containing all CSV files that the command must go through
private string $sourceDir;
// Whether to send welcome email to the user
private bool $sendWelcomeEmail = false;
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if (Application::isUnderMaintenance()) {
return $success;
}
if ($success && $this->getEnabled()) {
$this->addLocaleData();
}
return $success;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.importexport.csv.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription(): string
{
return __('plugins.importexport.csv.description');
}
/**
* @copydoc Plugin::getName()
*/
public function getName(): string
{
return 'CSVImportExportPlugin';
}
/**
* @copydoc PKPImportExportPlugin::usage
*/
public function usage($scriptName)
{
echo __('plugins.importexport.csv.cliUsage', [
'scriptName' => $scriptName,
'pluginName' => $this->getName()
]) . "\n\n";
echo __('plugins.importexport.csv.cliUsage.examples', [
'scriptName' => $scriptName,
'pluginName' => $this->getName()
]) . "\n\n";
}
/**
* @see PKPImportExportPlugin::executeCLI()
*/
public function executeCLI($scriptName, &$args)
{
$startTime = microtime(true);
$this->command = array_shift($args);
$this->username = array_shift($args);
$this->sourceDir = array_shift($args);
$this->sendWelcomeEmail = array_shift($args) ?? false;
if (! in_array($this->command, ['issues', 'users']) || !$this->sourceDir || !$this->username) {
$this->usage($scriptName);
exit(1);
}
if (! is_dir($this->sourceDir)) {
echo __('plugins.importexport.csv.unknownSourceDir', ['sourceDir' => $this->sourceDir]) . "\n";
exit(1);
}
$this->validateUser();
match ($this->command) {
'issues' => (new IssueCommand($this->sourceDir, $this->user))->run(),
'users' => (new UserCommand($this->sourceDir, $this->user, $this->sendWelcomeEmail))->run(),
default => throw new \InvalidArgumentException("Invalid command: {$this->command}"),
};
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
$hours = floor($executionTime / 3600);
$minutes = floor(($executionTime % 3600) / 60);
$seconds = floor($executionTime % 60);
echo sprintf("%dH:%dmin:%dsec\n", $hours, $minutes, $seconds);
}
/**
* Retrieve and validate the User by username
*/
private function validateUser(): void
{
$this->user = $this->getUser();
if (!$this->user) {
echo __('plugins.importexport.csv.unknownUser', ['username' => $this->username]) . "\n";
exit(1);
}
}
/**
* Retrives an user by username
*/
private function getUser(): ?User
{
return Repo::user()->getByUsername($this->username);
}
}