ILIAS  release_7 Revision v7.30-3-g800a261c036
DBSetup Class Reference

Class ilDbSetup. More...

+ Collaboration diagram for DBSetup:

Public Member Functions

 isConnectable ($keep_connection=false)
 
 createDatabase ($a_collation)
 
 provideGlobalDB ()
 
 revokeGlobalDB ()
 
 installDatabase ()
 
 isDatabaseExisting ()
 
 isDatabaseConnectable ()
 
 isDatabaseInstalled ()
 
 getClient ()
 
 setClient ($client)
 
 getStatus ()
 
 setStatus ($status)
 
 getSqlDumpFile ()
 
 setSqlDumpFile ($sql_dump_file)
 
 dropTables ()
 
 getIlDBInterface ()
 
 setIlDBInterface ($ilDBInterface)
 

Static Public Member Functions

static getInstanceForClient (\ilClient $client)
 
static getNewInstanceForClient (\ilClient $client)
 
static getInstanceForNewClient ($client_name, $dbname, $host='localhost', $username='root', $password='', $type=ilDBConstants::TYPE_PDO_MYSQL_INNODB)
 

Data Fields

const STATUS_OK = 1
 
const STATUS_FAILURE = 2
 

Protected Member Functions

 __construct (\ilClient $client)
 ilDbSetup constructor. More...
 
 getline ($fp, $delim)
 
 readDump ()
 legacy version of readdump More...
 
 readDumpSmall ()
 legacy version of readdump More...
 
 readDumpUltraSmall ()
 
 canDatabaseBeInstalled ()
 

Protected Attributes

 $client
 
 $status = self::STATUS_FAILURE
 
 $ilDBInterface
 
 $sql_dump_file = './setup/sql/ilias3.sql'
 

Static Protected Attributes

static $instances = array()
 

Detailed Description

Class ilDbSetup.

Author
Fabian Schmid fs@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch

Definition at line 10 of file class.ilDbSetup.php.

Constructor & Destructor Documentation

◆ __construct()

DBSetup::__construct ( \ilClient  $client)
protected

ilDbSetup constructor.

Parameters
\ilClient$client

Definition at line 67 of file class.ilDbSetup.php.

References $client, and ilDBWrapperFactory\getWrapper().

68  {
69  $this->client = $client;
71  $this->ilDBInterface->initFromIniFile($this->client->ini);
72  }
+ Here is the call graph for this function:

Member Function Documentation

◆ canDatabaseBeInstalled()

DBSetup::canDatabaseBeInstalled ( )
protected
Returns
bool

Definition at line 346 of file class.ilDbSetup.php.

References isDatabaseConnectable(), and isDatabaseInstalled().

Referenced by installDatabase().

347  {
348  $connectable = $this->isDatabaseConnectable();
349  $installed = $this->isDatabaseInstalled();
350 
351  return ($connectable && !$installed);
352  }
isDatabaseConnectable()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createDatabase()

DBSetup::createDatabase (   $a_collation)
Parameters
$a_collation
Returns
bool|mixed

Definition at line 129 of file class.ilDbSetup.php.

References getClient(), isConnectable(), ilDBConstants\TYPE_GALERA, ilDBConstants\TYPE_INNODB, ilDBConstants\TYPE_MYSQL, ilDBConstants\TYPE_PDO_MYSQL_INNODB, ilDBConstants\TYPE_PDO_MYSQL_MYISAM, and ilDBConstants\TYPE_PDO_POSTGRE.

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  }
isConnectable($keep_connection=false)
+ Here is the call graph for this function:

◆ dropTables()

DBSetup::dropTables ( )

Definition at line 409 of file class.ilDbSetup.php.

410  {
411  foreach ($this->ilDBInterface->listTables() as $table) {
412  $this->ilDBInterface->manipulate('DROP TABLE ' . $table);
413  }
414  }

◆ getClient()

DBSetup::getClient ( )
Returns
ilClient

Definition at line 358 of file class.ilDbSetup.php.

References $client.

Referenced by createDatabase(), and installDatabase().

359  {
360  return $this->client;
361  }
+ Here is the caller graph for this function:

◆ getIlDBInterface()

DBSetup::getIlDBInterface ( )
Returns

Definition at line 420 of file class.ilDbSetup.php.

References $ilDBInterface.

421  {
422  return $this->ilDBInterface;
423  }

◆ getInstanceForClient()

static DBSetup::getInstanceForClient ( \ilClient  $client)
static
Parameters
\ilClient$client
Returns
ilDbSetup

Definition at line 79 of file class.ilDbSetup.php.

References $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  }

◆ getInstanceForNewClient()

static DBSetup::getInstanceForNewClient (   $client_name,
  $dbname,
  $host = 'localhost',
  $username = 'root',
  $password = '',
  $type = ilDBConstants::TYPE_PDO_MYSQL_INNODB 
)
static
Parameters
$client_name
$dbname
string$host
string$username
string$password
string$type
Returns

Definition at line 106 of file class.ilDbSetup.php.

References $password, and $type.

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  }
$type
$password
Definition: cron.php:14

◆ getline()

DBSetup::getline (   $fp,
  $delim 
)
protected
Parameters
$fp
$delim
Returns
string

Definition at line 181 of file class.ilDbSetup.php.

References $result.

Referenced by readDump().

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  }
$result
+ Here is the caller graph for this function:

◆ getNewInstanceForClient()

static DBSetup::getNewInstanceForClient ( \ilClient  $client)
static
Parameters
\ilClient$client
Returns

Definition at line 92 of file class.ilDbSetup.php.

References $client.

92  : \ilDbSetup
93  {
94  return new self($client);
95  }

◆ getSqlDumpFile()

DBSetup::getSqlDumpFile ( )
Returns
string

Definition at line 394 of file class.ilDbSetup.php.

References $sql_dump_file.

Referenced by readDump(), readDumpSmall(), and readDumpUltraSmall().

395  {
396  return $this->sql_dump_file;
397  }
+ Here is the caller graph for this function:

◆ getStatus()

DBSetup::getStatus ( )
Returns
mixed

Definition at line 376 of file class.ilDbSetup.php.

References $status.

377  {
378  return $this->status;
379  }

◆ installDatabase()

DBSetup::installDatabase ( )
Returns
bool

Definition at line 265 of file class.ilDbSetup.php.

References canDatabaseBeInstalled(), getClient(), provideGlobalDB(), readDumpUltraSmall(), ilDBConstants\TYPE_GALERA, ilDBConstants\TYPE_INNODB, ilDBConstants\TYPE_MYSQL, ilDBConstants\TYPE_PDO_MYSQL_INNODB, ilDBConstants\TYPE_PDO_MYSQL_MYISAM, ilDBConstants\TYPE_PDO_POSTGRE, and ilDBConstants\TYPE_POSTGRES.

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  }
canDatabaseBeInstalled()
+ Here is the call graph for this function:

◆ isConnectable()

DBSetup::isConnectable (   $keep_connection = false)
Returns
bool|mixed

Definition at line 18 of file class.ilDbSetup.php.

References Vendor\Package\$e, and provideGlobalDB().

Referenced by createDatabase(), isDatabaseConnectable(), and isDatabaseExisting().

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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isDatabaseConnectable()

DBSetup::isDatabaseConnectable ( )
Returns
bool

Definition at line 318 of file class.ilDbSetup.php.

References isConnectable().

Referenced by canDatabaseBeInstalled(), and isDatabaseExisting().

319  {
320  if (!$this->isConnectable()) {
321  return false;
322  }
323 
324  return $this->ilDBInterface->connect(true);
325  }
isConnectable($keep_connection=false)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isDatabaseExisting()

DBSetup::isDatabaseExisting ( )
Returns
bool

Definition at line 301 of file class.ilDbSetup.php.

References isConnectable(), and isDatabaseConnectable().

Referenced by isDatabaseInstalled().

302  {
303  if (!$this->isConnectable()) {
304  return false;
305  }
306  if (!$this->isDatabaseConnectable()) {
307  return false;
308  }
309 
310  return true;
311  }
isConnectable($keep_connection=false)
isDatabaseConnectable()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isDatabaseInstalled()

DBSetup::isDatabaseInstalled ( )
Returns
bool

Definition at line 331 of file class.ilDbSetup.php.

References isDatabaseExisting().

Referenced by canDatabaseBeInstalled().

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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ provideGlobalDB()

DBSetup::provideGlobalDB ( )

Definition at line 155 of file class.ilDbSetup.php.

References $DIC, $GLOBALS, and $ilDBInterface.

Referenced by installDatabase(), and isConnectable().

156  {
157  global $DIC;
158 
159  if ($DIC->offsetExists('ilDB')) {
160  $DIC->offsetUnset('ilDB');
161  }
162 
163  $GLOBALS["ilDB"] = $this->ilDBInterface;
164  $DIC["ilDB"] = $this->ilDBInterface;
165  $this->client->db = $this->ilDBInterface; // TODO ugly and dirty, but ilClient requires it
166  }
global $DIC
Definition: goto.php:24
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
+ Here is the caller graph for this function:

◆ readDump()

DBSetup::readDump ( )
protected

legacy version of readdump

Deprecated:
use readDumpUltraSmall
Returns
bool

Definition at line 201 of file class.ilDbSetup.php.

References Vendor\Package\$e, getline(), and getSqlDumpFile().

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  }
Class ilDatabaseException.
getline($fp, $delim)
+ Here is the call graph for this function:

◆ readDumpSmall()

DBSetup::readDumpSmall ( )
protected

legacy version of readdump

Deprecated:
use readDumpUltraSmall
Returns
bool

Definition at line 235 of file class.ilDbSetup.php.

References getSqlDumpFile().

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  }
+ Here is the call graph for this function:

◆ readDumpUltraSmall()

DBSetup::readDumpUltraSmall ( )
protected
Returns
bool

Definition at line 252 of file class.ilDbSetup.php.

References getSqlDumpFile().

Referenced by installDatabase().

253  {
254  $sql = file_get_contents($this->getSqlDumpFile());
255  $re = $this->ilDBInterface->prepareManip($sql);
256  $this->ilDBInterface->execute($re);
257 
258  return true;
259  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ revokeGlobalDB()

DBSetup::revokeGlobalDB ( )

Definition at line 169 of file class.ilDbSetup.php.

References $GLOBALS.

170  {
171  $GLOBALS["ilDB"] = null;
172  $this->client->db = null; // TODO ugly and dirty, but ilClient requires it
173  }
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64

◆ setClient()

DBSetup::setClient (   $client)
Parameters
ilClient$client

Definition at line 367 of file class.ilDbSetup.php.

References $client.

368  {
369  $this->client = $client;
370  }

◆ setIlDBInterface()

DBSetup::setIlDBInterface (   $ilDBInterface)
Parameters
\ilDBInterface$ilDBInterface

Definition at line 429 of file class.ilDbSetup.php.

References $ilDBInterface.

430  {
431  $this->ilDBInterface = $ilDBInterface;
432  }

◆ setSqlDumpFile()

DBSetup::setSqlDumpFile (   $sql_dump_file)
Parameters
string$sql_dump_file

Definition at line 403 of file class.ilDbSetup.php.

References $sql_dump_file.

404  {
405  $this->sql_dump_file = $sql_dump_file;
406  }

◆ setStatus()

DBSetup::setStatus (   $status)
Parameters
mixed$status

Definition at line 385 of file class.ilDbSetup.php.

References $status.

386  {
387  $this->status = $status;
388  }

Field Documentation

◆ $client

DBSetup::$client
protected

◆ $ilDBInterface

DBSetup::$ilDBInterface
protected

Definition at line 55 of file class.ilDbSetup.php.

Referenced by getIlDBInterface(), provideGlobalDB(), and setIlDBInterface().

◆ $instances

DBSetup::$instances = array()
staticprotected

Definition at line 51 of file class.ilDbSetup.php.

◆ $sql_dump_file

DBSetup::$sql_dump_file = './setup/sql/ilias3.sql'
protected

Definition at line 59 of file class.ilDbSetup.php.

Referenced by getSqlDumpFile(), and setSqlDumpFile().

◆ $status

DBSetup::$status = self::STATUS_FAILURE
protected

Definition at line 47 of file class.ilDbSetup.php.

Referenced by getStatus(), and setStatus().

◆ STATUS_FAILURE

const DBSetup::STATUS_FAILURE = 2

Definition at line 13 of file class.ilDbSetup.php.

◆ STATUS_OK

const DBSetup::STATUS_OK = 1

Definition at line 12 of file class.ilDbSetup.php.


The documentation for this class was generated from the following file: