ILIAS  trunk Revision v11.0_alpha-1866-gfa368f7776e
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilDBUpdate Class Reference

Database Update class. More...

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

Public Member Functions

 __construct (protected ilDBInterface $db, protected ?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
 
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 ( protected ilDBInterface  $db,
protected ?ilIniFile  $client_ini = null 
)

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

45  {
46  $class_map = require ILIAS_ABSOLUTE_PATH . '/vendor/composer/vendor/composer/autoload_classmap.php';
47  $this->ctrl_structure_iterator = new ilCtrlArrayIterator($class_map);
48  }
Class ilCtrlArrayIterator.

Member Function Documentation

◆ applyCustomUpdates()

ilDBUpdate::applyCustomUpdates ( )

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

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

262  : bool
263  {
264  $ilCtrlStructureReader = null;
265  $ilDB = null;
266  $this->initGlobalsRequiredForUpdateSteps($ilCtrlStructureReader, $ilDB);
267  $this->readCustomUpdatesInfo(true);
268 
269  $file_version = $this->getCustomUpdatesFileVersion();
270  $current_version = $this->getCustomUpdatesCurrentVersion();
271  $this->filecontent = $this->custom_updates_content;
272 
273  $this->updateMsg = 'no_changes';
274  if ($current_version < $file_version) {
275  $msg = [];
276  for ($i = ($current_version + 1); $i <= $file_version; $i++) {
277  if ($this->applyUpdateNr($i, true) === false) {
278  $msg[] = [
279  'msg' => 'update_error: ' . $this->error,
280  'nr' => $i,
281  ];
282  $this->updateMsg = implode("\n", $msg);
283 
284  return false;
285  }
286 
287  $msg[] = [
288  'msg' => 'custom_update_applied',
289  'nr' => $i,
290  ];
291  }
292 
293  $this->updateMsg = implode("\n", $msg);
294  }
295 
296  return true;
297  }
array $custom_updates_content
applyUpdateNr(int $nr, bool $custom_update=false)
Apply a custom database update or a plugin update.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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 123 of file class.ilDBUpdate.php.

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

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

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

Referenced by execQuery().

85  : bool
86  {
87  return true;
88  }
+ Here is the caller graph for this function:

◆ execQuery()

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

PhpUndefinedVariableInspection

PhpUndefinedVariableInspection

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

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

Referenced by applyUpdateNr().

50  : bool
51  {
52  $q = "";
53  $sql = explode("\n", trim($str));
54  foreach ($sql as $i => $statement) {
55  $sql[$i] = trim($statement);
56  if ($statement !== "" && $statement[0] !== "#") {
57  //take line per line, until last char is ";"
58  if (str_ends_with($statement, ";")) {
59  //query is complete
61  $q .= " " . substr($statement, 0, -1);
62  $check = $this->checkQuery($q);
63  if ($check) {
64  $db->query($q);
65  } else {
66  $this->error = (string) $check;
67  return false;
68  }
69  unset($q);
70  } else {
72  $q .= " " . $statement;
73  }
74  }
75  }
76  if (isset($q) && $q !== "") {
77  echo "incomplete_statement: " . $q . "<br>";
78 
79  return false;
80  }
81 
82  return true;
83  }
checkQuery(string $q)
query(string $query)
Run a (read-only) Query on the database.
$q
Definition: shib_logout.php:21
$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 206 of file class.ilDBUpdate.php.

References $custom_updates_current_version, and readCustomUpdatesInfo().

Referenced by applyCustomUpdates().

206  : ?int
207  {
208  $this->readCustomUpdatesInfo();
209 
211  }
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 224 of file class.ilDBUpdate.php.

References $custom_updates_file_version, and readCustomUpdatesInfo().

Referenced by applyCustomUpdates().

224  : ?int
225  {
226  $this->readCustomUpdatesInfo();
227 
229  }
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 90 of file class.ilDBUpdate.php.

References $DIC, $GLOBALS, and null.

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

93  : void {
94  global $DIC;
95 
96  // 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.
97 
98  if (isset($GLOBALS['ilCtrlStructureReader'])) {
99  $ilCtrlStructureReader = $GLOBALS['ilCtrlStructureReader'];
100  } elseif ($DIC->offsetExists('ilCtrlStructureReader')) {
101  $ilCtrlStructureReader = $DIC['ilCtrlStructureReader'];
102  } else {
103  $ilCtrlStructureReader = new ilCtrlStructureReader(
104  $this->ctrl_structure_iterator,
106  );
107  $DIC->offsetSet('ilCtrlStructureReader', $ilCtrlStructureReader);
108  }
109 
110  $GLOBALS['ilCtrlStructureReader'] = $ilCtrlStructureReader;
111 
112  if ($this->client_ini !== null) {
113  $ilCtrlStructureReader->setIniFile($this->client_ini);
114  }
115  $ilDB = $DIC->database();
116  }
Class ilCtrlStructureCidGenerator.
Class ilCtrlStructureReader is responsible for reading ilCtrl&#39;s control structure.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$GLOBALS["DIC"]
Definition: wac.php:53
global $DIC
Definition: shib_login.php:22
+ Here is the caller graph for this function:

◆ readCustomUpdatesFileVersion()

ilDBUpdate::readCustomUpdatesFileVersion ( array  $a_file_content)
private

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

References $version.

Referenced by readCustomUpdatesInfo().

231  : int
232  {
233  //go through file content and search for last occurrence of <#x>
234  reset($a_file_content);
235  $regs = [];
236  $version = 0;
237  foreach ($a_file_content as $row) {
238  if (preg_match("/^<#(\\d+)>/", (string) $row, $regs)) {
239  $version = $regs[1];
240  }
241  }
242 
243  return (int) $version;
244  }
$version
Definition: plugin.php:24
+ Here is the caller graph for this function:

◆ readCustomUpdatesInfo()

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

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

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

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

246  : void
247  {
248  if ($this->custom_updates_info_read && !$a_force) {
249  return;
250  }
251 
252  $this->custom_updates_setting = new ilSetting();
253  $custom_updates_file = $this->PATH . './components/ILIAS/setup_/sql/dbupdate_custom.php';
254  if (is_file($custom_updates_file)) {
255  $this->custom_updates_content = @file($custom_updates_file);
256  $this->custom_updates_current_version = (int) $this->custom_updates_setting->get('db_version_custom', '0');
257  $this->custom_updates_file_version = $this->readCustomUpdatesFileVersion($this->custom_updates_content);
258  }
259  $this->custom_updates_info_read = true;
260  }
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 220 of file class.ilDBUpdate.php.

Referenced by applyUpdateNr().

220  : void
221  {
222  }
+ Here is the caller graph for this function:

◆ setCustomUpdatesCurrentVersion()

ilDBUpdate::setCustomUpdatesCurrentVersion ( ?int  $a_version)
private

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

References readCustomUpdatesInfo().

Referenced by applyUpdateNr().

213  : void
214  {
215  $this->readCustomUpdatesInfo();
216  $this->custom_updates_setting->set('db_version_custom', (string) $a_version);
217  $this->custom_updates_current_version = $a_version;
218  }
readCustomUpdatesInfo(bool $a_force=false)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $ctrl_structure_iterator

Iterator ilDBUpdate::$ctrl_structure_iterator
protected

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

◆ $custom_updates_content

array ilDBUpdate::$custom_updates_content = []
private

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

Referenced by applyCustomUpdates().

◆ $custom_updates_current_version

int ilDBUpdate::$custom_updates_current_version = 0
private

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

Referenced by getCustomUpdatesCurrentVersion().

◆ $custom_updates_file_version

int ilDBUpdate::$custom_updates_file_version = null
private

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

Referenced by getCustomUpdatesFileVersion().

◆ $custom_updates_info_read

bool ilDBUpdate::$custom_updates_info_read = null
private

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

◆ $custom_updates_setting

ilSetting ilDBUpdate::$custom_updates_setting
private

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

◆ $error

string ilDBUpdate::$error
protected

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

Referenced by applyCustomUpdates(), and applyUpdateNr().

◆ $filecontent

array ilDBUpdate::$filecontent
protected

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

◆ $PATH

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

Definition at line 34 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: