ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilDatabasePopulatedObjective.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
8{
9 const MIN_NUMBER_OF_ILIAS_TABLES = 200; // educated guess
10
11 public function getHash() : string
12 {
13 return hash("sha256", implode("-", [
14 self::class,
15 $this->config->getHost(),
16 $this->config->getPort(),
17 $this->config->getDatabase()
18 ]));
19 }
20
21 public function getLabel() : string
22 {
23 return "The database is populated with ILIAS-tables.";
24 }
25
26 public function isNotable() : bool
27 {
28 return true;
29 }
30
31 public function getPreconditions(Setup\Environment $environment) : array
32 {
33 if ($environment->getResource(Setup\Environment::RESOURCE_DATABASE)) {
34 return [];
35 }
36 return [
37 new \ilDatabaseExistsObjective($this->config)
38 ];
39 }
40
41 public function achieve(Setup\Environment $environment) : Setup\Environment
42 {
43 $db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
44
45 if ($this->isDatabasePopulated($db)) {
46 return $environment;
47 }
48
49 $path_to_db_dump = $this->config->getPathToDBDump();
50 if (!is_file(realpath($path_to_db_dump)) ||
51 !is_readable(realpath($path_to_db_dump))) {
53 "Cannot read database dump file: $path_to_db_dump"
54 );
55 }
56
57 $sql = file_get_contents(realpath($path_to_db_dump));
58 $statement = $db->prepareManip($sql);
59 $db->execute($statement);
60
61 return $environment;
62 }
63
64 protected function isDatabasePopulated(\ilDBInterface $db)
65 {
66 $probe_tables = ['usr_data', 'object_data', 'object_reference'];
67 $number_of_probe_tables = count($probe_tables);
68 $tables = $db->listTables();
69 $number_of_tables = count($tables);
70
71 return
72 $number_of_tables > self::MIN_NUMBER_OF_ILIAS_TABLES
73 && count(array_intersect($tables, $probe_tables)) == $number_of_probe_tables;
74 }
75}
An exception for terminatinating execution or to throw for unit testing.
Signals that some goal won't be achievable by actions of the system ever.
getPreconditions(Setup\Environment $environment)
An environment holds resources to be used in the setup process.
Definition: Environment.php:12
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
Interface ilDBInterface.