ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ilDBUpdate Class Reference

Database Update class. More...

+ Inheritance diagram for ilDBUpdate:
+ Collaboration diagram for ilDBUpdate:

Public Member Functions

 __construct (ilDBInterface $a_db_handler, ilIniFile $client_ini=null)
 
 getCustomUpdatesCurrentVersion ()
 
 getCustomUpdatesFileVersion ()
 
 applyCustomUpdates ()
 

Protected Member Functions

 checkQuery (string $q)
 
 initGlobalsRequiredForUpdateSteps (?ilCtrlStructureReader &$ilCtrlStructureReader, ?ilDBInterface &$ilDB)
 
 applyUpdateNr (int $nr, bool $custom_update=false)
 Apply a custom database update or a plugin update. More...
 
 setCurrentVersion (?int $a_version)
 

Protected Attributes

string $updateMsg
 
ilDBInterface $db
 
ilIniFile $client_ini = null
 
Iterator $ctrl_structure_iterator
 
string $error
 
string $PATH = './'
 
array $filecontent
 

Private Member Functions

 execQuery (ilDBInterface $db, string $str)
 
 setCustomUpdatesCurrentVersion (?int $a_version)
 
 readCustomUpdatesFileVersion (array $a_file_content)
 
 readCustomUpdatesInfo (bool $a_force=false)
 

Private Attributes

int $custom_updates_current_version = 0
 
int $custom_updates_file_version = null
 
bool $custom_updates_info_read = null
 
ilSetting $custom_updates_setting
 
array $custom_updates_content = []
 

Detailed Description

Database Update class.

Author
Peter Gabriel pgabr.nosp@m.iel@.nosp@m.datab.nosp@m.ay.d.nosp@m.e
Alex Killing alex..nosp@m.kill.nosp@m.ing@g.nosp@m.mx.d.nosp@m.e
Version
$Id$

Definition at line 28 of file class.ilDBUpdate.php.

Constructor & Destructor Documentation

◆ __construct()

ilDBUpdate::__construct ( ilDBInterface  $a_db_handler,
ilIniFile  $client_ini = null 
)

Definition at line 46 of file class.ilDBUpdate.php.

References $client_ini.

47  {
48  $this->client_ini = $client_ini;
49  $this->db = $a_db_handler;
50  $this->PATH = "./";
51 
52  $class_map = require ILIAS_ABSOLUTE_PATH . '/vendor/composer/vendor/composer/autoload_classmap.php';
53  $this->ctrl_structure_iterator = new ilCtrlArrayIterator($class_map);
54  }
Class ilCtrlArrayIterator.
ilIniFile $client_ini

Member Function Documentation

◆ applyCustomUpdates()

ilDBUpdate::applyCustomUpdates ( )

Definition at line 268 of file class.ilDBUpdate.php.

References $custom_updates_content, $error, $ilDB, applyUpdateNr(), getCustomUpdatesCurrentVersion(), getCustomUpdatesFileVersion(), initGlobalsRequiredForUpdateSteps(), and readCustomUpdatesInfo().

268  : bool
269  {
270  $ilCtrlStructureReader = null;
271  $ilDB = null;
272  $this->initGlobalsRequiredForUpdateSteps($ilCtrlStructureReader, $ilDB);
273  $this->readCustomUpdatesInfo(true);
274 
275  $file_version = $this->getCustomUpdatesFileVersion();
276  $current_version = $this->getCustomUpdatesCurrentVersion();
277  $this->filecontent = $this->custom_updates_content;
278 
279  $this->updateMsg = 'no_changes';
280  if ($current_version < $file_version) {
281  $msg = [];
282  for ($i = ($current_version + 1); $i <= $file_version; $i++) {
283  if ($this->applyUpdateNr($i, true) === false) {
284  $msg[] = [
285  'msg' => 'update_error: ' . $this->error,
286  'nr' => $i,
287  ];
288  $this->updateMsg = implode("\n", $msg);
289 
290  return false;
291  }
292 
293  $msg[] = [
294  'msg' => 'custom_update_applied',
295  'nr' => $i,
296  ];
297  }
298 
299  $this->updateMsg = implode("\n", $msg);
300  }
301 
302  return true;
303  }
array $custom_updates_content
applyUpdateNr(int $nr, bool $custom_update=false)
Apply a custom database update or a plugin update.
readCustomUpdatesInfo(bool $a_force=false)
initGlobalsRequiredForUpdateSteps(?ilCtrlStructureReader &$ilCtrlStructureReader, ?ilDBInterface &$ilDB)
getCustomUpdatesCurrentVersion()
+ Here is the call graph for this function:

◆ applyUpdateNr()

ilDBUpdate::applyUpdateNr ( int  $nr,
bool  $custom_update = false 
)
protected

Apply a custom database update or a plugin update.

Parameters
int$nrnumber what patch to apply (Reference: Patch for https://mantis.ilias.de/view.php?id=28550) private

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

References $error, $ilDB, execQuery(), initGlobalsRequiredForUpdateSteps(), setCurrentVersion(), and setCustomUpdatesCurrentVersion().

Referenced by applyCustomUpdates(), and ilPluginDBUpdate\applyUpdate().

129  : bool
130  {
131  $ilCtrlStructureReader = null;
132  $ilDB = null;
133  $this->initGlobalsRequiredForUpdateSteps($ilCtrlStructureReader, $ilDB);
134 
135  reset($this->filecontent);
136 
137  //init
138  $i = 0;
139 
140  //go through filecontent
141  while (!preg_match("/^<\#" . $nr . ">/", $this->filecontent[$i]) && $i < count($this->filecontent)) {
142  $i++;
143  }
144 
145  //update not found
146  if ($i === count($this->filecontent)) {
147  $this->error = 'update_not_found';
148 
149  return false;
150  }
151 
152  $i++;
153 
154  //update found, now extract this update to a new array
155  $update = [];
156  while ($i < count($this->filecontent) && !preg_match("/^<#" . ($nr + 1) . ">/", $this->filecontent[$i])) {
157  $update[] = trim($this->filecontent[$i]);
158  $i++;
159  }
160 
161  //now you have the update, now process it
162  $sql = [];
163  $php = [];
164  $mode = 'sql';
165 
166  foreach ($update as $row) {
167  if (preg_match("/<\?php/", $row)) {
168  if (count($sql) > 0) {
169  if ($this->execQuery($this->db, implode("\n", $sql)) === false) {
170  return false;
171  }
172  $sql = [];
173  }
174  $mode = 'php';
175  } elseif (preg_match("/\?>/", $row)) {
176  if (count($php) > 0) {
177  $code = implode("\n", $php);
178  if (eval($code) === false) {
179  $this->error = 'Parse error: ' . $code;
180 
181  return false;
182  }
183  $php = [];
184  }
185  $mode = 'sql';
186  } else {
187  if ($mode === 'sql') {
188  $sql[] = $row;
189  }
190 
191  if ($mode === 'php') {
192  $php[] = $row;
193  }
194  }
195  }
196 
197  if ($mode === 'sql' && count($sql) > 0 && $this->execQuery($this->db, implode("\n", $sql)) === false) {
198  $this->error = "dump_error: " . $this->error;
199 
200  return false;
201  }
202 
203  if ($custom_update) {
204  $this->setCustomUpdatesCurrentVersion($nr);
205  } else {
206  $this->setCurrentVersion($nr);
207  }
208 
209  return true;
210  }
setCurrentVersion(?int $a_version)
execQuery(ilDBInterface $db, string $str)
initGlobalsRequiredForUpdateSteps(?ilCtrlStructureReader &$ilCtrlStructureReader, ?ilDBInterface &$ilDB)
setCustomUpdatesCurrentVersion(?int $a_version)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkQuery()

ilDBUpdate::checkQuery ( string  $q)
protected

Definition at line 91 of file class.ilDBUpdate.php.

Referenced by execQuery().

91  : bool
92  {
93  return true;
94  }
+ Here is the caller graph for this function:

◆ execQuery()

ilDBUpdate::execQuery ( ilDBInterface  $db,
string  $str 
)
private

PhpUndefinedVariableInspection

PhpUndefinedVariableInspection

Definition at line 56 of file class.ilDBUpdate.php.

References $check, $q, checkQuery(), and ilDBInterface\query().

Referenced by applyUpdateNr().

56  : bool
57  {
58  $q = "";
59  $sql = explode("\n", trim($str));
60  foreach ($sql as $i => $statement) {
61  $sql[$i] = trim($statement);
62  if ($statement !== "" && $statement[0] !== "#") {
63  //take line per line, until last char is ";"
64  if (substr($statement, -1) === ";") {
65  //query is complete
67  $q .= " " . substr($statement, 0, -1);
68  $check = $this->checkQuery($q);
69  if ($check === true) {
70  $db->query($q);
71  } else {
72  $this->error = (string) $check;
73  return false;
74  }
75  unset($q);
76  } else {
78  $q .= " " . $statement;
79  }
80  }
81  }
82  if (isset($q) && $q !== "") {
83  echo "incomplete_statement: " . $q . "<br>";
84 
85  return false;
86  }
87 
88  return true;
89  }
checkQuery(string $q)
query(string $query)
Run a (read-only) Query on the database.
$q
Definition: shib_logout.php:18
$check
Definition: buildRTE.php:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCustomUpdatesCurrentVersion()

ilDBUpdate::getCustomUpdatesCurrentVersion ( )

Definition at line 212 of file class.ilDBUpdate.php.

References $custom_updates_current_version, and readCustomUpdatesInfo().

Referenced by applyCustomUpdates().

212  : ?int
213  {
214  $this->readCustomUpdatesInfo();
215 
217  }
readCustomUpdatesInfo(bool $a_force=false)
int $custom_updates_current_version
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCustomUpdatesFileVersion()

ilDBUpdate::getCustomUpdatesFileVersion ( )

Definition at line 230 of file class.ilDBUpdate.php.

References $custom_updates_file_version, and readCustomUpdatesInfo().

Referenced by applyCustomUpdates().

230  : ?int
231  {
232  $this->readCustomUpdatesInfo();
233 
235  }
readCustomUpdatesInfo(bool $a_force=false)
int $custom_updates_file_version
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initGlobalsRequiredForUpdateSteps()

ilDBUpdate::initGlobalsRequiredForUpdateSteps ( ?ilCtrlStructureReader $ilCtrlStructureReader,
?ilDBInterface $ilDB 
)
protected

Definition at line 96 of file class.ilDBUpdate.php.

References $DIC, and $GLOBALS.

Referenced by applyCustomUpdates(), ilPluginDBUpdate\applyUpdate(), and applyUpdateNr().

99  : void {
100  global $DIC;
101 
102  // TODO: There is currently a huge mixup of globals, $DIC and dependencies, esprecially in setup and during DB-Updates. This leads to many problems. The following core tries to provide the needed dependencies for the dbupdate-script. The code hopefully will change in the future.
103 
104  if (isset($GLOBALS['ilCtrlStructureReader'])) {
105  $ilCtrlStructureReader = $GLOBALS['ilCtrlStructureReader'];
106  } elseif ($DIC->offsetExists('ilCtrlStructureReader')) {
107  $ilCtrlStructureReader = $DIC['ilCtrlStructureReader'];
108  } else {
109  $ilCtrlStructureReader = new ilCtrlStructureReader(
110  $this->ctrl_structure_iterator,
112  );
113  $DIC->offsetSet('ilCtrlStructureReader', $ilCtrlStructureReader);
114  }
115 
116  $GLOBALS['ilCtrlStructureReader'] = $ilCtrlStructureReader;
117 
118  if ($this->client_ini) {
119  $ilCtrlStructureReader->setIniFile($this->client_ini);
120  }
121  $ilDB = $DIC->database();
122  }
Class ilCtrlStructureCidGenerator.
Class ilCtrlStructureReader is responsible for reading ilCtrl&#39;s control structure.
$GLOBALS["DIC"]
Definition: wac.php:30
global $DIC
Definition: shib_login.php:25
+ Here is the caller graph for this function:

◆ readCustomUpdatesFileVersion()

ilDBUpdate::readCustomUpdatesFileVersion ( array  $a_file_content)
private

Definition at line 237 of file class.ilDBUpdate.php.

References $version.

Referenced by readCustomUpdatesInfo().

237  : int
238  {
239  //go through file content and search for last occurrence of <#x>
240  reset($a_file_content);
241  $regs = [];
242  $version = 0;
243  foreach ($a_file_content as $row) {
244  if (preg_match("/^<#([0-9]+)>/", $row, $regs)) {
245  $version = $regs[1];
246  }
247  }
248 
249  return (int) $version;
250  }
$version
Definition: plugin.php:25
+ Here is the caller graph for this function:

◆ readCustomUpdatesInfo()

ilDBUpdate::readCustomUpdatesInfo ( bool  $a_force = false)
private

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

References ILIAS\Repository\int(), and readCustomUpdatesFileVersion().

Referenced by applyCustomUpdates(), getCustomUpdatesCurrentVersion(), getCustomUpdatesFileVersion(), and setCustomUpdatesCurrentVersion().

252  : void
253  {
254  if ($this->custom_updates_info_read && !$a_force) {
255  return;
256  }
257 
258  $this->custom_updates_setting = new ilSetting();
259  $custom_updates_file = $this->PATH . './components/ILIAS/setup_/sql/dbupdate_custom.php';
260  if (is_file($custom_updates_file)) {
261  $this->custom_updates_content = @file($custom_updates_file);
262  $this->custom_updates_current_version = (int) $this->custom_updates_setting->get('db_version_custom', '0');
263  $this->custom_updates_file_version = $this->readCustomUpdatesFileVersion($this->custom_updates_content);
264  }
265  $this->custom_updates_info_read = true;
266  }
readCustomUpdatesFileVersion(array $a_file_content)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCurrentVersion()

ilDBUpdate::setCurrentVersion ( ?int  $a_version)
protected

Definition at line 226 of file class.ilDBUpdate.php.

Referenced by applyUpdateNr().

226  : void
227  {
228  }
+ Here is the caller graph for this function:

◆ setCustomUpdatesCurrentVersion()

ilDBUpdate::setCustomUpdatesCurrentVersion ( ?int  $a_version)
private

Definition at line 219 of file class.ilDBUpdate.php.

References readCustomUpdatesInfo().

Referenced by applyUpdateNr().

219  : void
220  {
221  $this->readCustomUpdatesInfo();
222  $this->custom_updates_setting->set('db_version_custom', (string) $a_version);
223  $this->custom_updates_current_version = $a_version;
224  }
readCustomUpdatesInfo(bool $a_force=false)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $client_ini

ilIniFile ilDBUpdate::$client_ini = null
protected

Definition at line 32 of file class.ilDBUpdate.php.

Referenced by __construct().

◆ $ctrl_structure_iterator

Iterator ilDBUpdate::$ctrl_structure_iterator
protected

Definition at line 33 of file class.ilDBUpdate.php.

◆ $custom_updates_content

array ilDBUpdate::$custom_updates_content = []
private

Definition at line 44 of file class.ilDBUpdate.php.

Referenced by applyCustomUpdates().

◆ $custom_updates_current_version

int ilDBUpdate::$custom_updates_current_version = 0
private

Definition at line 40 of file class.ilDBUpdate.php.

Referenced by getCustomUpdatesCurrentVersion().

◆ $custom_updates_file_version

int ilDBUpdate::$custom_updates_file_version = null
private

Definition at line 41 of file class.ilDBUpdate.php.

Referenced by getCustomUpdatesFileVersion().

◆ $custom_updates_info_read

bool ilDBUpdate::$custom_updates_info_read = null
private

Definition at line 42 of file class.ilDBUpdate.php.

◆ $custom_updates_setting

ilSetting ilDBUpdate::$custom_updates_setting
private

Definition at line 43 of file class.ilDBUpdate.php.

◆ $db

ilDBInterface ilDBUpdate::$db
protected

Definition at line 31 of file class.ilDBUpdate.php.

Referenced by ilPluginDBUpdate\__construct().

◆ $error

string ilDBUpdate::$error
protected

Definition at line 35 of file class.ilDBUpdate.php.

Referenced by applyCustomUpdates(), and applyUpdateNr().

◆ $filecontent

array ilDBUpdate::$filecontent
protected

Definition at line 38 of file class.ilDBUpdate.php.

◆ $PATH

string ilDBUpdate::$PATH = './'
protected

Definition at line 36 of file class.ilDBUpdate.php.

◆ $updateMsg

string ilDBUpdate::$updateMsg
protected

Definition at line 30 of file class.ilDBUpdate.php.


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