ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
62
72 public static function getInstanceForNewClient($client_name, $dbname, $host = 'localhost', $username = 'root', $password = '', $type = ilDBConstants::TYPE_PDO_MYSQL_INNODB)
73 {
74 require_once('./setup/classes/class.ilClient.php');
75 require_once('./Services/Init/classes/class.ilIniFile.php');
76 require_once('./setup/classes/class.ilDBConnections.php');
77
78 $ilClient = new ilClient($client_name, new ilDBConnections());
79 $ilClient->init();
80 $ilClient->setDbHost($host);
81 $ilClient->setDbName($dbname);
82 $ilClient->setDbUser($username);
83 $ilClient->setDbPass($password);
84 $ilClient->setDbType($type);
85 $ilClient->writeIni();
86
87 return self::getInstanceForClient($ilClient);
88 }
89
90
95 public function createDatabase($a_collation)
96 {
97 if ($this->isConnectable()) {
98 switch ($this->ilDBInterface->getDBType()) {
105 $clientIniFile = $this->client->ini;
106
107 if (!$this->ilDBInterface->createDatabase($clientIniFile->readVariable("db", "name"), 'utf8', $a_collation)) {
108 return false;
109 }
110 $this->ilDBInterface->initFromIniFile($this->getClient()->ini);
111
112 return $this->ilDBInterface->connect();
113 break;
114 }
115 }
116
117 return false;
118 }
119
120
121 public function provideGlobalDB()
122 {
123 global $DIC;
124
125 if ($DIC->offsetExists('ilDB')) {
126 $DIC->offsetUnset('ilDB');
127 }
128
130 $DIC["ilDB"] = $this->ilDBInterface;
131 $this->client->db = $this->ilDBInterface; // TODO ugly and dirty, but ilClient requires it
132 }
133
134
135 public function revokeGlobalDB()
136 {
137 $GLOBALS["ilDB"] = null;
138 $this->client->db = null; // TODO ugly and dirty, but ilClient requires it
139 }
140
141
147 protected function getline($fp, $delim)
148 {
149 $result = "";
150 while (!feof($fp)) {
151 $tmp = fgetc($fp);
152 if ($tmp == $delim) {
153 return $result;
154 }
155 $result .= $tmp;
156 }
157
158 return $result;
159 }
160
161
167 protected function readDump()
168 {
169 $fp = fopen($this->getSqlDumpFile(), 'r');
170 $q = '';
171 while (!feof($fp)) {
172 $line = trim($this->getline($fp, "\n"));
173
174 if ($line != "" && substr($line, 0, 1) != "#" && substr($line, 0, 1) != "-") {
175 if (substr($line, -1) == ";") {
176 //query is complete
177 $q .= " " . substr($line, 0, -1);
178 try {
179 $r = $this->ilDBInterface->query($q);
180 } catch (ilDatabaseException $e) {
181 return false;
182 }
183
184 unset($q);
185 unset($line);
186 } else {
187 $q .= " " . $line;
188 }
189 }
190 }
191
192 fclose($fp);
193 }
194
195
201 protected function readDumpSmall()
202 {
203 $sql = file_get_contents($this->getSqlDumpFile());
204 $lines = explode(';', $sql);
205 foreach ($lines as $line) {
206 if (strlen($line) > 0) {
207 $this->ilDBInterface->manipulate($line);
208 }
209 }
210
211 return true;
212 }
213
214
218 protected function readDumpUltraSmall()
219 {
220 $sql = file_get_contents($this->getSqlDumpFile());
221 $re = $this->ilDBInterface->prepareManip($sql);
222 $this->ilDBInterface->execute($re);
223
224 return true;
225 }
226
227
231 public function installDatabase()
232 {
233 if ($this->canDatabaseBeInstalled()) {
234 $this->provideGlobalDB();
235 switch ($this->ilDBInterface->getDBType()) {
238 $this->ilDBInterface->connect();
239 //$this->dropTables();
240 //$this->readDump();
241 $this->readDumpUltraSmall();
242 $this->getClient()->db_installed = true;
243
244 return true;
245
246 break;
253 include_once("./setup/sql/ilDBTemplate.php");
254 setupILIASDatabase();
255
256 return true;
257 break;
258 }
259 }
260
261 return false;
262 }
263
264
268 public function isDatabaseExisting()
269 {
270 if (!$this->isConnectable()) {
271 return false;
272 }
273 if (!$this->isDatabaseConnectable()) {
274 return false;
275 }
276
277 return true;
278 }
279
280
284 public function isConnectable($keep_connection = false)
285 {
286 switch ($this->ilDBInterface->getDBType()) {
287 default:
288 try {
289 $connect = $this->ilDBInterface->connect();
290 } catch (PDOException $e) {
291 $connect = ($e->getCode() == 1049);
292 }
293 break;
295 $connect = $this->ilDBInterface->connect(true);
296 break;
297 }
298 if ($keep_connection && $connect) {
299 $this->provideGlobalDB();
300 }
301
302 if (!$connect) {
303 $this->client->setError('Database can\'t be reached. Please check the credentials and if database exists');
304 }
305
306 return $connect;
307 }
308
309
313 public function isDatabaseConnectable()
314 {
315 if (!$this->isConnectable()) {
316 return false;
317 }
318
319 return $this->ilDBInterface->connect(true);
320 }
321
322
326 public function isDatabaseInstalled()
327 {
328 if (!$this->isDatabaseExisting()) {
329 return false;
330 }
331
332 $target = array( 'usr_data', 'object_data', 'object_reference' );
333
334 return count(array_intersect($this->ilDBInterface->listTables(), $target)) == count($target);
335 }
336
337
341 protected function canDatabaseBeInstalled()
342 {
343 $connectable = $this->isDatabaseConnectable();
344 $installed = $this->isDatabaseInstalled();
345
346 return ($connectable && !$installed);
347 }
348
349
353 public function getClient()
354 {
355 return $this->client;
356 }
357
358
362 public function setClient($client)
363 {
364 $this->client = $client;
365 }
366
367
371 public function getStatus()
372 {
373 return $this->status;
374 }
375
376
380 public function setStatus($status)
381 {
382 $this->status = $status;
383 }
384
385
389 public function getSqlDumpFile()
390 {
392 }
393
394
399 {
400 $this->sql_dump_file = $sql_dump_file;
401 }
402
403
404 public function dropTables()
405 {
406 foreach ($this->ilDBInterface->listTables() as $table) {
407 $this->ilDBInterface->manipulate('DROP TABLE ' . $table);
408 }
409 }
410
411
415 public function getIlDBInterface()
416 {
418 }
419
420
425 {
427 }
428}
$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)
isConnectable($keep_connection=false)
setSqlDumpFile($sql_dump_file)
static $instances
readDumpSmall()
@description legacy version of readdump
const STATUS_FAILURE
setClient($client)
$r
Definition: example_031.php:79
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
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)
$type
$password
Definition: pwgen.php:17
if(empty($password)) $table
Definition: pwgen.php:24
global $DIC
Definition: saml.php:7