ILIAS  release_7 Revision v7.30-3-g800a261c036
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;
14
18 public function isConnectable($keep_connection = false)
19 {
20 switch ($this->ilDBInterface->getDBType()) {
21 default:
22 try {
23 $connect = $this->ilDBInterface->connect();
24 } catch (PDOException $e) {
25 $connect = ($e->getCode() == 1049);
26 }
27 break;
28 }
29 if ($keep_connection && $connect) {
30 $this->provideGlobalDB();
31 }
32
33 if (!$connect) {
34 $this->client->setError('Database can\'t be reached. Please check the credentials and if database exists');
35 }
36
37 return $connect;
38 }
39
43 protected $client;
51 protected static $instances = array();
55 protected $ilDBInterface;
59 protected $sql_dump_file = './setup/sql/ilias3.sql';
60
61
67 protected function __construct(\ilClient $client)
68 {
69 $this->client = $client;
71 $this->ilDBInterface->initFromIniFile($this->client->ini);
72 }
73
74
79 public static function getInstanceForClient(\ilClient $client)
80 {
81 if (empty(self::$instances[$client->getId()])) {
82 self::$instances[$client->getId()] = new self($client);
83 }
84
85 return self::$instances[$client->getId()];
86 }
87
92 public static function getNewInstanceForClient(\ilClient $client) : \ilDbSetup
93 {
94 return new self($client);
95 }
96
106 public static function getInstanceForNewClient($client_name, $dbname, $host = 'localhost', $username = 'root', $password = '', $type = ilDBConstants::TYPE_PDO_MYSQL_INNODB)
107 {
108 require_once('./setup/classes/class.ilClient.php');
109 require_once('./Services/Init/classes/class.ilIniFile.php');
110 require_once('./setup/classes/class.ilDBConnections.php');
111
112 $ilClient = new ilClient($client_name, new ilDBConnections());
113 $ilClient->init();
114 $ilClient->setDbHost($host);
115 $ilClient->setDbName($dbname);
116 $ilClient->setDbUser($username);
117 $ilClient->setDbPass($password);
118 $ilClient->setDbType($type);
119 $ilClient->writeIni();
120
121 return self::getInstanceForClient($ilClient);
122 }
123
124
129 public function createDatabase($a_collation)
130 {
131 if ($this->isConnectable()) {
132 switch ($this->ilDBInterface->getDBType()) {
139 $clientIniFile = $this->client->ini;
140
141 if (!$this->ilDBInterface->createDatabase($clientIniFile->readVariable("db", "name"), 'utf8', $a_collation)) {
142 return false;
143 }
144 $this->ilDBInterface->initFromIniFile($this->getClient()->ini);
145
146 return $this->ilDBInterface->connect();
147 break;
148 }
149 }
150
151 return false;
152 }
153
154
155 public function provideGlobalDB()
156 {
157 global $DIC;
158
159 if ($DIC->offsetExists('ilDB')) {
160 $DIC->offsetUnset('ilDB');
161 }
162
164 $DIC["ilDB"] = $this->ilDBInterface;
165 $this->client->db = $this->ilDBInterface; // TODO ugly and dirty, but ilClient requires it
166 }
167
168
169 public function revokeGlobalDB()
170 {
171 $GLOBALS["ilDB"] = null;
172 $this->client->db = null; // TODO ugly and dirty, but ilClient requires it
173 }
174
175
181 protected function getline($fp, $delim)
182 {
183 $result = "";
184 while (!feof($fp)) {
185 $tmp = fgetc($fp);
186 if ($tmp == $delim) {
187 return $result;
188 }
189 $result .= $tmp;
190 }
191
192 return $result;
193 }
194
195
201 protected function readDump()
202 {
203 $fp = fopen($this->getSqlDumpFile(), 'r');
204 $q = '';
205 while (!feof($fp)) {
206 $line = trim($this->getline($fp, "\n"));
207
208 if ($line != "" && substr($line, 0, 1) != "#" && substr($line, 0, 1) != "-") {
209 if (substr($line, -1) == ";") {
210 //query is complete
211 $q .= " " . substr($line, 0, -1);
212 try {
213 $r = $this->ilDBInterface->query($q);
214 } catch (ilDatabaseException $e) {
215 return false;
216 }
217
218 unset($q);
219 unset($line);
220 } else {
221 $q .= " " . $line;
222 }
223 }
224 }
225
226 fclose($fp);
227 }
228
229
235 protected function readDumpSmall()
236 {
237 $sql = file_get_contents($this->getSqlDumpFile());
238 $lines = explode(';', $sql);
239 foreach ($lines as $line) {
240 if (strlen($line) > 0) {
241 $this->ilDBInterface->manipulate($line);
242 }
243 }
244
245 return true;
246 }
247
248
252 protected function readDumpUltraSmall()
253 {
254 $sql = file_get_contents($this->getSqlDumpFile());
255 $re = $this->ilDBInterface->prepareManip($sql);
256 $this->ilDBInterface->execute($re);
257
258 return true;
259 }
260
261
265 public function installDatabase()
266 {
267 if ($this->canDatabaseBeInstalled()) {
268 $this->provideGlobalDB();
269 switch ($this->ilDBInterface->getDBType()) {
272 $this->ilDBInterface->connect();
273 //$this->dropTables();
274 //$this->readDump();
275 $this->readDumpUltraSmall();
276 $this->getClient()->db_installed = true;
277
278 return true;
279
280 break;
286 include_once("./setup/sql/ilDBTemplate.php");
287 setupILIASDatabase();
288
289 return true;
290 break;
291 }
292 }
293
294 return false;
295 }
296
297
301 public function isDatabaseExisting()
302 {
303 if (!$this->isConnectable()) {
304 return false;
305 }
306 if (!$this->isDatabaseConnectable()) {
307 return false;
308 }
309
310 return true;
311 }
312
313
314
318 public function isDatabaseConnectable()
319 {
320 if (!$this->isConnectable()) {
321 return false;
322 }
323
324 return $this->ilDBInterface->connect(true);
325 }
326
327
331 public function isDatabaseInstalled()
332 {
333 if (!$this->isDatabaseExisting()) {
334 return false;
335 }
336
337 $target = array( 'usr_data', 'object_data', 'object_reference' );
338
339 return count(array_intersect($this->ilDBInterface->listTables(), $target)) == count($target);
340 }
341
342
346 protected function canDatabaseBeInstalled()
347 {
348 $connectable = $this->isDatabaseConnectable();
349 $installed = $this->isDatabaseInstalled();
350
351 return ($connectable && !$installed);
352 }
353
354
358 public function getClient()
359 {
360 return $this->client;
361 }
362
363
367 public function setClient($client)
368 {
369 $this->client = $client;
370 }
371
372
376 public function getStatus()
377 {
378 return $this->status;
379 }
380
381
385 public function setStatus($status)
386 {
387 $this->status = $status;
388 }
389
390
394 public function getSqlDumpFile()
395 {
397 }
398
399
404 {
405 $this->sql_dump_file = $sql_dump_file;
406 }
407
408
409 public function dropTables()
410 {
411 foreach ($this->ilDBInterface->listTables() as $table) {
412 $this->ilDBInterface->manipulate('DROP TABLE ' . $table);
413 }
414 }
415
416
420 public function getIlDBInterface()
421 {
423 }
424
425
430 {
432 }
433}
$result
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
Class ilDbSetup.
canDatabaseBeInstalled()
static getNewInstanceForClient(\ilClient $client)
getline($fp, $delim)
readDump()
@description legacy version of readdump
static $instances
static getInstanceForNewClient($client_name, $dbname, $host='localhost', $username='root', $password='', $type=ilDBConstants::TYPE_PDO_MYSQL_INNODB)
createDatabase($a_collation)
setStatus($status)
setIlDBInterface($ilDBInterface)
__construct(\ilClient $client)
ilDbSetup constructor.
setSqlDumpFile($sql_dump_file)
static getInstanceForClient(\ilClient $client)
const STATUS_FAILURE
setClient($client)
isDatabaseConnectable()
readDumpSmall()
@description legacy version of readdump
const STATUS_OK
isConnectable($keep_connection=false)
Class ilDatabaseException.
$password
Definition: cron.php:14
global $DIC
Definition: goto.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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)
$type