-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathLorisInstance.php
209 lines (189 loc) · 5.69 KB
/
LorisInstance.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php declare(strict_types=1);
namespace LORIS;
/**
* A LorisInstance represents an installed instance of a LORIS
* project.
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
*/
class LorisInstance
{
protected $modulesDirs;
/**
* An object representing configuration settings for this LORIS instance.
*
* @var \NDB_Config
*/
private $config;
private $DB;
/**
* Construct a LORISInstance for the install connected to $db
* which uses modules from $moduleDirs.
*
* @param \Database $db A database connection to this
* instance.
* @param \NDB_Config $config A set of configuration settings used by this
* instance.
* @param string[] $moduleDirs A list of directories that may
* contain modules for this instance.
*/
public function __construct(
\Database $db,
\NDB_Config $config,
array $modulesDirs
) {
$this->DB = $db;
$this->config = $config;
$this->modulesDirs = $modulesDirs;
}
/**
* Return an active database connection to this instance.
*
* @return \Database
*/
public function getDatabaseConnection() : \Database
{
return $this->DB;
}
/**
* Return a new database connection to this LORIS instance.
*
* @return \Database
*/
public function getNewDatabaseConnection() : \Database
{
$settings = \NDB_Factory::singleton()->settings();
// Pass the credentials in environment variables, so that they
// don't potentially show up in a stack trace if something goes
// wrong.
$dbname = $settings->dbName();
putenv("LORIS_{$dbname}_USERNAME=" . $settings->dbUserName());
putenv("LORIS_{$dbname}_PASSWORD=" . $settings->dbPassword());
putenv("LORIS_{$dbname}_HOST=" . $settings->dbHost());
$db = new \Database();
$db->connect(
$settings->dbName(),
true,
);
// Unset the variables now that they're no longer needed.
putenv("LORIS_{$dbname}_USERNAME=");
putenv("LORIS_{$dbname}_PASSWORD=");
putenv("LORIS_{$dbname}_HOST=");
return $db;
}
/**
* Return a list of directories on the filesystem which
* may contain modules.
*
* @return string[]
*/
private function getModulesDirs() : array
{
return $this->modulesDirs;
}
/**
* Retrieve all active module descriptors from the given database.
*
* @return \Module[]
*/
public function getActiveModules(): array
{
$mnames = $this->getDatabaseConnection()->pselectCol(
"SELECT Name FROM modules WHERE Active='Y'",
[]
);
$modules = [];
foreach ($mnames as $name) {
try {
$mod = $this->getModule($name);
$modules[] = $mod;
} catch (\LorisModuleMissingException $e) {
error_log($e->getMessage() . " " . $e->getTraceAsString());
}
}
return $modules;
}
/**
* Return true if the LORISInstance has a module named
* $name. To be installed it must be both available on
* the filesystem and active in the modules table.
*
* @param string $name The name of a LORIS module.
*
* @return bool
*/
public function hasModule(string $name) : bool
{
$dirs = $this->getModulesDirs();
$found = false;
foreach ($dirs as $subdir) {
if (is_dir($subdir . "/" . $name)) {
$found = true;
break;
}
}
if ($found === false) {
return false;
}
foreach ($this->getActiveModules() as $module) {
if ($module->getName() == $name) {
return true;
}
}
return false;
}
private array $moduleInstances;
/**
* Get the \Module class for the module named $name,
* if enabled on this LORIS instance or throw an exception
* if it doesn't exist.
*
* @return \Module
*/
public function getModule(string $name) : \Module
{
if (isset($this->moduleInstances[$name])) {
return $this->moduleInstances[$name];
}
foreach ($this->modulesDirs as $modulesDir) {
$mpath = "$modulesDir/$name";
$moduleclasspath = "$mpath/php/module.class.inc";
if (file_exists($moduleclasspath)) {
include_once $moduleclasspath;
$className = "\LORIS\\$name\Module";
$cls = new $className($this, $name, $mpath);
$this->moduleInstances[$name] = $cls;
$cls->registerAutoloader();
return $cls;
}
}
throw new \LorisNoSuchModuleException("No such module: $name");
}
/**
* Returns an NDB_Config object used for interacting with configuration
* settings for this instance.
*
* @return \NDB_Config
*/
public function getConfiguration(): \NDB_Config
{
return $this->config;
}
/**
* Returns a list of Site objects that are valid for this
* Loris instance
*
* @return \Site[]
*/
public function getAllSites() : array
{
$DB = $this->getDatabaseConnection();
$centers = $DB->pselectCol("SELECT CenterID FROM psc", []);
return array_map(
function ($center) {
return \Site::singleton(\CenterID::singleton(intval($center)));
},
$centers
);
}
}