ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPluginDBUpdate.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 {
32  protected const PLUGIN_UPDATE_FILE = "/sql/dbupdate.php";
33 
34  private \ilPluginInfo $plugin;
35 
36  private string $db_update_file;
37  private ?int $current_version;
38  private ?int $file_version = null;
39 
40  protected string $error = '';
41 
46  public function __construct(
48  \ilPluginInfo $plugin
49  ) {
50  $this->client_ini = null;
51  $this->db = $db;
52  $this->plugin = $plugin;
53 
54  $this->db_update_file = $this->PATH . $this->getDBUpdateScriptName();
55 
56  $this->current_version = $plugin->getCurrentDBVersion() ?? 0;
57 
58  $this->readDBUpdateFile();
59  $this->readFileVersion();
60 
61  $class_map = require ILIAS_ABSOLUTE_PATH . '/libs/composer/vendor/composer/autoload_classmap.php';
62  $this->ctrl_structure_iterator = new ilCtrlArrayIterator($class_map);
63  }
64 
65  private function readDBUpdateFile(): void
66  {
67  if (!file_exists($this->db_update_file)) {
68  $this->error = 'no_db_update_file';
69  $this->filecontent = [];
70  return;
71  }
72 
73  $this->filecontent = @file($this->db_update_file);
74  }
75 
76  private function readFileVersion(): void
77  {
78  //go through filecontent and search for last occurence of <#x>
79  reset($this->filecontent);
80  $regs = [];
81  $version = 0;
82  foreach ($this->filecontent as $row) {
83  if (preg_match('/^<\#([0-9]+)>/', $row, $regs)) {
84  $version = $regs[1];
85  }
86  }
87 
88  $this->file_version = (int) $version;
89  }
90 
91  public function getCurrentVersion(): int
92  {
94  }
95 
96  protected function setCurrentVersion(?int $a_version): void
97  {
98  $this->current_version = $a_version;
99  }
100 
101  protected function checkQuery(string $q): bool
102  {
103  if ((is_int(stripos($q, 'create table')) || is_int(stripos($q, 'alter table')) ||
104  is_int(stripos($q, 'drop table')))
105  && !is_int(stripos($q, $this->getTablePrefix()))) {
106  return false;
107  }
108 
109  return true;
110  }
111 
112  private function getTablePrefix(): string
113  {
114  $component = $this->plugin->getComponent();
115  $slot = $this->plugin->getPluginSlot();
116  return $component->getId() . '_' . $slot->getId() . '_' . $this->plugin->getId();
117  }
118 
119  private function getDBUpdateScriptName(): string
120  {
121  return $this->plugin->getPath() . self::PLUGIN_UPDATE_FILE;
122  }
123 
128  public function applyUpdate()
129  {
130  $ilCtrlStructureReader = null;
131  $ilDB = null;
132  $this->initGlobalsRequiredForUpdateSteps($ilCtrlStructureReader, $ilDB);
133 
134  $file_version = $this->file_version;
135  $current_version = $this->current_version;
136 
137  $this->updateMsg = 'no_changes';
138  if ($current_version < $file_version) {
139  $msg = [];
140  for ($i = ($current_version + 1); $i <= $file_version; $i++) {
141  if ($this->applyUpdateNr($i) === false) {
142  $msg[] = 'msg: update_error - ' . $this->error . '; nr: ' . $i . ';';
143  $this->updateMsg = implode("\n", $msg);
144 
145  return false;
146  }
147 
148  $msg[] = 'msg: update_applied; nr: ' . $i . ';';
149  }
150 
151  $this->updateMsg = implode('\n', $msg);
152  }
153  }
154 }
Database Update class.
applyUpdate()
Apply update.
applyUpdateNr(int $nr, bool $custom_update=false)
Apply a custom database update or a plugin update.
setCurrentVersion(?int $a_version)
Class ilCtrlArrayIterator.
Simple value class for information about a plugin.
initGlobalsRequiredForUpdateSteps(?ilCtrlStructureReader &$ilCtrlStructureReader, ?ilDBInterface &$ilDB)
Database Update class.
__construct(\ilDBInterface $db, \ilPluginInfo $plugin)
constructor MagicMethodsValidityInspection
ilDBInterface $db
$q
Definition: shib_logout.php:21
$version
Definition: plugin.php:24