ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDbSetup.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Database/classes/class.ilDBWrapperFactory.php');
3require_once('./Services/Database/classes/class.ilDBConstants.php');
4
11{
12 const STATUS_OK = 1;
13 const STATUS_FAILURE = 2;
17 protected $client;
25 protected static $instances = array();
29 protected $ilDBInterface;
33 protected $sql_dump_file = './setup/sql/ilias3.sql';
34
35
41 protected function __construct(\ilClient $client)
42 {
43 $this->client = $client;
44 $this->ilDBInterface = ilDBWrapperFactory::getWrapper($client->getDbType());
45 $this->ilDBInterface->initFromIniFile($this->client->ini);
46 }
47
48
53 public static function getInstanceForClient(\ilClient $client)
54 {
55 if (empty(self::$instances[$client->getId()])) {
56 self::$instances[$client->getId()] = new self($client);
57 }
58
59 return self::$instances[$client->getId()];
60 }
61
67 {
68 return new self($client);
69 }
70
80 public static function getInstanceForNewClient($client_name, $dbname, $host = 'localhost', $username = 'root', $password = '', $type = ilDBConstants::TYPE_PDO_MYSQL_INNODB)
81 {
82 require_once('./setup/classes/class.ilClient.php');
83 require_once('./Services/Init/classes/class.ilIniFile.php');
84 require_once('./setup/classes/class.ilDBConnections.php');
85
86 $ilClient = new ilClient($client_name, new ilDBConnections());
87 $ilClient->init();
88 $ilClient->setDbHost($host);
89 $ilClient->setDbName($dbname);
90 $ilClient->setDbUser($username);
91 $ilClient->setDbPass($password);
92 $ilClient->setDbType($type);
93 $ilClient->writeIni();
94
95 return self::getInstanceForClient($ilClient);
96 }
97
98
103 public function createDatabase($a_collation)
104 {
105 if ($this->isConnectable()) {
106 switch ($this->ilDBInterface->getDBType()) {
113 $clientIniFile = $this->client->ini;
114
115 if (!$this->ilDBInterface->createDatabase($clientIniFile->readVariable("db", "name"), 'utf8', $a_collation)) {
116 return false;
117 }
118 $this->ilDBInterface->initFromIniFile($this->getClient()->ini);
119
120 return $this->ilDBInterface->connect();
121 break;
122 }
123 }
124
125 return false;
126 }
127
128
129 public function provideGlobalDB()
130 {
131 global $DIC;
132
133 if ($DIC->offsetExists('ilDB')) {
134 $DIC->offsetUnset('ilDB');
135 }
136
138 $DIC["ilDB"] = $this->ilDBInterface;
139 $this->client->db = $this->ilDBInterface; // TODO ugly and dirty, but ilClient requires it
140 }
141
142
143 public function revokeGlobalDB()
144 {
145 $GLOBALS["ilDB"] = null;
146 $this->client->db = null; // TODO ugly and dirty, but ilClient requires it
147 }
148
149
155 protected function getline($fp, $delim)
156 {
157 $result = "";
158 while (!feof($fp)) {
159 $tmp = fgetc($fp);
160 if ($tmp == $delim) {
161 return $result;
162 }
163 $result .= $tmp;
164 }
165
166 return $result;
167 }
168
169
175 protected function readDump()
176 {
177 $fp = fopen($this->getSqlDumpFile(), 'r');
178 $q = '';
179 while (!feof($fp)) {
180 $line = trim($this->getline($fp, "\n"));
181
182 if ($line != "" && substr($line, 0, 1) != "#" && substr($line, 0, 1) != "-") {
183 if (substr($line, -1) == ";") {
184 //query is complete
185 $q .= " " . substr($line, 0, -1);
186 try {
187 $r = $this->ilDBInterface->query($q);
188 } catch (ilDatabaseException $e) {
189 return false;
190 }
191
192 unset($q);
193 unset($line);
194 } else {
195 $q .= " " . $line;
196 }
197 }
198 }
199
200 fclose($fp);
201 }
202
203
209 protected function readDumpSmall()
210 {
211 $sql = file_get_contents($this->getSqlDumpFile());
212 $lines = explode(';', $sql);
213 foreach ($lines as $line) {
214 if (strlen($line) > 0) {
215 $this->ilDBInterface->manipulate($line);
216 }
217 }
218
219 return true;
220 }
221
222
226 protected function readDumpUltraSmall()
227 {
228 $sql = file_get_contents($this->getSqlDumpFile());
229 $re = $this->ilDBInterface->prepareManip($sql);
230 $this->ilDBInterface->execute($re);
231
232 return true;
233 }
234
235
239 public function installDatabase()
240 {
241 if ($this->canDatabaseBeInstalled()) {
242 $this->provideGlobalDB();
243 switch ($this->ilDBInterface->getDBType()) {
246 $this->ilDBInterface->connect();
247 //$this->dropTables();
248 //$this->readDump();
249 $this->readDumpUltraSmall();
250 $this->getClient()->db_installed = true;
251
252 return true;
253
254 break;
260 include_once("./setup/sql/ilDBTemplate.php");
261 setupILIASDatabase();
262
263 return true;
264 break;
265 }
266 }
267
268 return false;
269 }
270
271
275 public function isDatabaseExisting()
276 {
277 if (!$this->isConnectable()) {
278 return false;
279 }
280 if (!$this->isDatabaseConnectable()) {
281 return false;
282 }
283
284 return true;
285 }
286
287
291 public function isConnectable($keep_connection = false)
292 {
293 switch ($this->ilDBInterface->getDBType()) {
294 default:
295 try {
296 $connect = $this->ilDBInterface->connect();
297 } catch (PDOException $e) {
298 $connect = ($e->getCode() == 1049);
299 }
300 break;
301 }
302 if ($keep_connection && $connect) {
303 $this->provideGlobalDB();
304 }
305
306 if (!$connect) {
307 $this->client->setError('Database can\'t be reached. Please check the credentials and if database exists');
308 }
309
310 return $connect;
311 }
312
313
317 public function isDatabaseConnectable()
318 {
319 if (!$this->isConnectable()) {
320 return false;
321 }
322
323 return $this->ilDBInterface->connect(true);
324 }
325
326
330 public function isDatabaseInstalled()
331 {
332 if (!$this->isDatabaseExisting()) {
333 return false;
334 }
335
336 $target = array( 'usr_data', 'object_data', 'object_reference' );
337
338 return count(array_intersect($this->ilDBInterface->listTables(), $target)) == count($target);
339 }
340
341
345 protected function canDatabaseBeInstalled()
346 {
347 $connectable = $this->isDatabaseConnectable();
348 $installed = $this->isDatabaseInstalled();
349
350 return ($connectable && !$installed);
351 }
352
353
357 public function getClient()
358 {
359 return $this->client;
360 }
361
362
366 public function setClient($client)
367 {
368 $this->client = $client;
369 }
370
371
375 public function getStatus()
376 {
377 return $this->status;
378 }
379
380
384 public function setStatus($status)
385 {
386 $this->status = $status;
387 }
388
389
393 public function getSqlDumpFile()
394 {
396 }
397
398
403 {
404 $this->sql_dump_file = $sql_dump_file;
405 }
406
407
408 public function dropTables()
409 {
410 foreach ($this->ilDBInterface->listTables() as $table) {
411 $this->ilDBInterface->manipulate('DROP TABLE ' . $table);
412 }
413 }
414
415
419 public function getIlDBInterface()
420 {
422 }
423
424
429 {
431 }
432}
$result
An exception for terminatinating execution or to throw for unit testing.
Client Management.
Administrates DB connections in setup.
Class ilDatabaseException.
Class ilDbSetup.
static getInstanceForClient(\ilClient $client)
getline($fp, $delim)
setIlDBInterface($ilDBInterface)
static getInstanceForNewClient($client_name, $dbname, $host='localhost', $username='root', $password='', $type=ilDBConstants::TYPE_PDO_MYSQL_INNODB)
__construct(\ilClient $client)
ilDbSetup constructor.
createDatabase($a_collation)
readDump()
@description legacy version of readdump
setStatus($status)
static getNewInstanceForClient(\ilClient $client)
isConnectable($keep_connection=false)
setSqlDumpFile($sql_dump_file)
static $instances
readDumpSmall()
@description legacy version of readdump
const STATUS_FAILURE
setClient($client)
$password
Definition: cron.php:14
$r
Definition: example_031.php:79
Interface ilDBInterface.
execute($stmt, $data=array())
initFromIniFile($tmpClientIniFile=null)
createDatabase($a_name, $a_charset="utf8", $a_collation="")
query($query)
Run a (read-only) Query on the database.
getDBType()
Get DSN.
prepareManip($a_query, $a_types=null)
manipulate($query)
Run a (write) Query on the database.
connect($return_false_on_error=false)
$target
Definition: test.php:19
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
$type
if(empty($password)) $table
Definition: pwgen.php:24
global $DIC
Definition: saml.php:7