ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilDbSetup.php
Go to the documentation of this file.
1<?php
2
9{
10 const STATUS_OK = 1;
11 const STATUS_FAILURE = 2;
15 protected $client;
23 protected static $instances = array();
27 protected $ilDBInterface;
31 protected $sql_dump_file = './setup/sql/ilias3.sql';
32
33
39 protected function __construct(\ilClient $client)
40 {
41 $this->client = $client;
43 $this->ilDBInterface->initFromIniFile($this->client->ini);
44 }
45
46
51 public static function getInstanceForClient(\ilClient $client)
52 {
53 if (empty(self::$instances[$client->getId()])) {
54 self::$instances[$client->getId()] = new self($client);
55 }
56
57 return self::$instances[$client->getId()];
58 }
59
65 {
66 return new self($client);
67 }
68
78 public static function getInstanceForNewClient($client_name, $dbname, $host = 'localhost', $username = 'root', $password = '', $type = ilDBConstants::TYPE_PDO_MYSQL_INNODB)
79 {
80 require_once('./setup/classes/class.ilClient.php');
81 require_once('./Services/Init/classes/class.ilIniFile.php');
82
83 $ilClient = new ilClient($client_name, new ilDBConnections());
84 $ilClient->init();
85 $ilClient->setDbHost($host);
86 $ilClient->setDbName($dbname);
87 $ilClient->setDbUser($username);
88 $ilClient->setDbPass($password);
89 $ilClient->setDbType($type);
90 $ilClient->writeIni();
91
92 return self::getInstanceForClient($ilClient);
93 }
94
95
100 public function createDatabase($a_collation)
101 {
102 if ($this->isConnectable()) {
103 switch ($this->ilDBInterface->getDBType()) {
110 $clientIniFile = $this->client->ini;
111
112 if (!$this->ilDBInterface->createDatabase($clientIniFile->readVariable("db", "name"), 'utf8', $a_collation)) {
113 return false;
114 }
115 $this->ilDBInterface->initFromIniFile($this->getClient()->ini);
116
117 return $this->ilDBInterface->connect();
118 break;
119 }
120 }
121
122 return false;
123 }
124
125
126 public function provideGlobalDB()
127 {
128 global $DIC;
129
130 if ($DIC->offsetExists('ilDB')) {
131 $DIC->offsetUnset('ilDB');
132 }
133
135 $DIC["ilDB"] = $this->ilDBInterface;
136 $this->client->db = $this->ilDBInterface; // TODO ugly and dirty, but ilClient requires it
137 }
138
139
140 public function revokeGlobalDB()
141 {
142 $GLOBALS["ilDB"] = null;
143 $this->client->db = null; // TODO ugly and dirty, but ilClient requires it
144 }
145
146
152 protected function getline($fp, $delim)
153 {
154 $result = "";
155 while (!feof($fp)) {
156 $tmp = fgetc($fp);
157 if ($tmp == $delim) {
158 return $result;
159 }
160 $result .= $tmp;
161 }
162
163 return $result;
164 }
165
169 protected function readDumpUltraSmall()
170 {
171 $sql = file_get_contents($this->getSqlDumpFile());
172 $re = $this->ilDBInterface->prepareManip($sql);
173 $this->ilDBInterface->execute($re);
174
175 return true;
176 }
177
178
182 public function installDatabase()
183 {
184 if ($this->canDatabaseBeInstalled()) {
185 $this->provideGlobalDB();
186 switch ($this->ilDBInterface->getDBType()) {
189 $this->ilDBInterface->connect();
190 //$this->dropTables();
191 $this->readDumpUltraSmall();
192 $this->getClient()->db_installed = true;
193
194 return true;
195
196 break;
202 include_once("./setup/sql/ilDBTemplate.php");
203 setupILIASDatabase();
204
205 return true;
206 break;
207 }
208 }
209
210 return false;
211 }
212
213
217 public function isDatabaseExisting()
218 {
219 if (!$this->isConnectable()) {
220 return false;
221 }
222 if (!$this->isDatabaseConnectable()) {
223 return false;
224 }
225
226 return true;
227 }
228
229
233 public function isConnectable($keep_connection = false)
234 {
235 switch ($this->ilDBInterface->getDBType()) {
236 default:
237 try {
238 $connect = $this->ilDBInterface->connect();
239 } catch (PDOException $e) {
240 $connect = ($e->getCode() == 1049);
241 }
242 break;
243 }
244 if ($keep_connection && $connect) {
245 $this->provideGlobalDB();
246 }
247
248 if (!$connect) {
249 $this->client->setError('Database can\'t be reached. Please check the credentials and if database exists');
250 }
251
252 return $connect;
253 }
254
255
259 public function isDatabaseConnectable()
260 {
261 if (!$this->isConnectable()) {
262 return false;
263 }
264
265 return $this->ilDBInterface->connect(true);
266 }
267
268
272 public function isDatabaseInstalled()
273 {
274 if (!$this->isDatabaseExisting()) {
275 return false;
276 }
277
278 $target = array( 'usr_data', 'object_data', 'object_reference' );
279
280 return count(array_intersect($this->ilDBInterface->listTables(), $target)) == count($target);
281 }
282
283
287 protected function canDatabaseBeInstalled()
288 {
289 $connectable = $this->isDatabaseConnectable();
290 $installed = $this->isDatabaseInstalled();
291
292 return ($connectable && !$installed);
293 }
294
295
299 public function getClient()
300 {
301 return $this->client;
302 }
303
304
308 public function setClient($client)
309 {
310 $this->client = $client;
311 }
312
313
317 public function getStatus()
318 {
319 return $this->status;
320 }
321
322
326 public function setStatus($status)
327 {
328 $this->status = $status;
329 }
330
331
335 public function getSqlDumpFile()
336 {
338 }
339
340
345 {
346 $this->sql_dump_file = $sql_dump_file;
347 }
348
349
350 public function dropTables()
351 {
352 foreach ($this->ilDBInterface->listTables() as $table) {
353 $this->ilDBInterface->manipulate('DROP TABLE ' . $table);
354 }
355 }
356
357
361 public function getIlDBInterface()
362 {
364 }
365
366
371 {
373 }
374}
$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.
Client Management.
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)
setStatus($status)
static getNewInstanceForClient(\ilClient $client)
isConnectable($keep_connection=false)
setSqlDumpFile($sql_dump_file)
static $instances
const STATUS_FAILURE
setClient($client)
$password
Definition: cron.php:14
Interface ilDBInterface.
execute($stmt, $data=array())
initFromIniFile($tmpClientIniFile=null)
createDatabase($a_name, $a_charset="utf8", $a_collation="")
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
$DIC
Definition: xapitoken.php:46