ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
10class ilDbSetup {
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 $this->client = $client;
43 $this->ilDBInterface = ilDBWrapperFactory::getWrapper($client->getDbType());
44 $this->ilDBInterface->initFromIniFile($this->client->ini);
45 }
46
47
52 public static function getInstanceForClient(\ilClient $client) {
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
60
70 public static function getInstanceForNewClient($client_name, $dbname, $host = 'localhost', $username = 'root', $password = '', $type = ilDBConstants::TYPE_PDO_MYSQL_INNODB) {
71 require_once('./setup/classes/class.ilClient.php');
72 require_once('./Services/Init/classes/class.ilIniFile.php');
73 require_once('./setup/classes/class.ilDBConnections.php');
74
75 $ilClient = new ilClient($client_name, new ilDBConnections());
76 $ilClient->init();
77 $ilClient->setDbHost($host);
78 $ilClient->setDbName($dbname);
79 $ilClient->setDbUser($username);
80 $ilClient->setDbPass($password);
81 $ilClient->setDbType($type);
82 $ilClient->writeIni();
83
84 return self::getInstanceForClient($ilClient);
85 }
86
87
92 public function createDatabase($a_collation) {
93 if ($this->isConnectable()) {
94 switch ($this->ilDBInterface->getDBType()) {
101 $clientIniFile = $this->client->ini;
102
103 if (!$this->ilDBInterface->createDatabase($clientIniFile->readVariable("db", "name"), 'utf8', $a_collation)) {
104 return false;
105 }
106 $this->ilDBInterface->initFromIniFile($this->getClient()->ini);
107
108 return $this->ilDBInterface->connect();
109 break;
110 }
111 }
112
113 return false;
114 }
115
116
117 public function provideGlobalDB() {
118 global $DIC;
119
120 if($DIC->offsetExists('ilDB')) {
121 $DIC->offsetUnset('ilDB');
122 }
123
125 $DIC["ilDB"] = $this->ilDBInterface;
126 $this->client->db = $this->ilDBInterface; // TODO ugly and dirty, but ilClient requires it
127 }
128
129
130 public function revokeGlobalDB() {
131 $GLOBALS["ilDB"] = null;
132 $this->client->db = null; // TODO ugly and dirty, but ilClient requires it
133 }
134
135
141 protected function getline($fp, $delim) {
142 $result = "";
143 while (!feof($fp)) {
144 $tmp = fgetc($fp);
145 if ($tmp == $delim) {
146 return $result;
147 }
148 $result .= $tmp;
149 }
150
151 return $result;
152 }
153
154
160 protected function readDump() {
161 $fp = fopen($this->getSqlDumpFile(), 'r');
162 $q = '';
163 while (!feof($fp)) {
164 $line = trim($this->getline($fp, "\n"));
165
166 if ($line != "" && substr($line, 0, 1) != "#" && substr($line, 0, 1) != "-") {
167 if (substr($line, - 1) == ";") {
168 //query is complete
169 $q .= " " . substr($line, 0, - 1);
170 try {
171 $r = $this->ilDBInterface->query($q);
172 } catch (ilDatabaseException $e) {
173 return false;
174 }
175
176 unset($q);
177 unset($line);
178 } else {
179 $q .= " " . $line;
180 }
181 }
182 }
183
184 fclose($fp);
185 }
186
187
193 protected function readDumpSmall() {
194 $sql = file_get_contents($this->getSqlDumpFile());
195 $lines = explode(';', $sql);
196 foreach ($lines as $line) {
197 if (strlen($line) > 0) {
198 $this->ilDBInterface->manipulate($line);
199 }
200 }
201
202 return true;
203 }
204
205
209 protected function readDumpUltraSmall() {
210 $sql = file_get_contents($this->getSqlDumpFile());
211 $re = $this->ilDBInterface->prepareManip($sql);
212 $this->ilDBInterface->execute($re);
213
214 return true;
215 }
216
217
221 public function installDatabase() {
222 if ($this->canDatabaseBeInstalled()) {
223 $this->provideGlobalDB();
224 switch ($this->ilDBInterface->getDBType()) {
227 $this->ilDBInterface->connect();
228 //$this->dropTables();
229 //$this->readDump();
230 $this->readDumpUltraSmall();
231 $this->getClient()->db_installed = true;
232
233 return true;
234
235 break;
242 include_once("./setup/sql/ilDBTemplate.php");
243 setupILIASDatabase();
244
245 return true;
246 break;
247 }
248 }
249
250 return false;
251 }
252
253
257 public function isDatabaseExisting() {
258 if (!$this->isConnectable()) {
259 return false;
260 }
261 if (!$this->isDatabaseConnectable()) {
262 return false;
263 }
264
265 return true;
266 }
267
268
272 public function isConnectable($keep_connection = false) {
273 switch ($this->ilDBInterface->getDBType()) {
274 default:
275 try {
276 $connect = $this->ilDBInterface->connect();
277 } catch (PDOException $e) {
278 $connect = ($e->getCode() == 1049);
279 }
280 break;
282 $connect = $this->ilDBInterface->connect(true);
283 break;
284 }
285 if ($keep_connection && $connect) {
286 $this->provideGlobalDB();
287 }
288
289 if (!$connect) {
290 $this->client->setError('Database can\'t be reached. Please check the credentials and if database exists');
291 }
292
293 return $connect;
294 }
295
296
300 public function isDatabaseConnectable() {
301 if (!$this->isConnectable()) {
302 return false;
303 }
304
305 return $this->ilDBInterface->connect(true);
306 }
307
308
312 public function isDatabaseInstalled() {
313 if (!$this->isDatabaseExisting()) {
314 return false;
315 }
316
317 $target = array( 'usr_data', 'object_data', 'object_reference' );
318
319 return count(array_intersect($this->ilDBInterface->listTables(), $target)) == count($target);
320 }
321
322
326 protected function canDatabaseBeInstalled() {
327 $connectable = $this->isDatabaseConnectable();
328 $installed = $this->isDatabaseInstalled();
329
330 return ($connectable && !$installed);
331 }
332
333
337 public function getClient() {
338 return $this->client;
339 }
340
341
345 public function setClient($client) {
346 $this->client = $client;
347 }
348
349
353 public function getStatus() {
354 return $this->status;
355 }
356
357
361 public function setStatus($status) {
362 $this->status = $status;
363 }
364
365
369 public function getSqlDumpFile() {
371 }
372
373
378 $this->sql_dump_file = $sql_dump_file;
379 }
380
381
382 public function dropTables() {
383 foreach ($this->ilDBInterface->listTables() as $table) {
384 $this->ilDBInterface->manipulate('DROP TABLE ' . $table);
385 }
386 }
387
388
392 public function getIlDBInterface() {
394 }
395
396
402 }
403}
$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)
getDBType()
Get DSN.
prepareManip($a_query, $a_types=null)
manipulate($query)
connect($return_false_on_error=false)
global $DIC