ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDBUpdate.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
18 
24 
30 
34  function ilDBUpdate($a_db_handler = 0,$tmp_flag = false)
35  {
36  // workaround to allow setup migration
37  if ($a_db_handler)
38  {
39  $this->db =& $a_db_handler;
40 
41  if ($tmp_flag)
42  {
43  $this->PATH = "./";
44  }
45  else
46  {
47  $this->PATH = "./";
48  //$this->PATH = "../";
49  }
50  }
51  else
52  {
53  global $mySetup;
54  $this->db = $mySetup->db;
55  $this->PATH = "./";
56  }
57 
58  $this->getCurrentVersion();
59 
60  // get update file for current version
61  $updatefile = $this->getFileForStep($this->currentVersion + 1);
62 
63  $this->current_file = $updatefile;
64  $this->DB_UPDATE_FILE = $this->PATH."setup/sql/".$updatefile;
65 
66  //
67  // NOTE: IF YOU SET THIS TO THE NEWEST FILE, CHANGE ALSO getFileForStep()
68  //
69  $this->LAST_UPDATE_FILE = $this->PATH."setup/sql/dbupdate_03.php";
70 
71  $this->readDBUpdateFile();
72  $this->readLastUpdateFile();
73  $this->readFileVersion();
74  }
75 
79  function getFileForStep($a_version)
80  {
81  //
82  // NOTE: IF YOU ADD A NEW FILE HERE, CHANGE ALSO THE CONSTRUCTOR
83  //
84  if ((int)$a_version > 2948) // last number in previous file
85  {
86  return "dbupdate_03.php";
87  }
88  else if ((int)$a_version > 864) // last number in previous file
89  {
90  return "dbupdate_02.php";
91  }
92  else
93  {
94  return "dbupdate.php";
95  }
96  }
97 
101  function initStep($i)
102  {
103  //
104  }
105 
111  function _DBUpdate()
112  {
113  $this->db->disconnect();
114  }
115 
116  function readDBUpdateFile()
117  {
118  if (!file_exists($this->DB_UPDATE_FILE))
119  {
120  $this->error = "no_db_update_file";
121  $this->filecontent = array();
122  return false;
123  }
124 
125  $this->filecontent = @file($this->DB_UPDATE_FILE);
126  return true;
127  }
128 
130  {
131  if (!file_exists($this->LAST_UPDATE_FILE))
132  {
133  $this->error = "no_last_update_file";
134  $this->lastfilecontent = array();
135  return false;
136  }
137 
138  $this->lastfilecontent = @file($this->LAST_UPDATE_FILE);
139  return true;
140  }
141 
142  function getCurrentVersion()
143  {
144  $GLOBALS["ilDB"] = $this->db;
145  include_once './Services/Administration/classes/class.ilSetting.php';
146  $set = new ilSetting("common", true);
147  $this->currentVersion = (integer) $set->get("db_version");
148  return $this->currentVersion;
149  }
150 
151  function setCurrentVersion ($a_version)
152  {
153  include_once './Services/Administration/classes/class.ilSetting.php';
154  $set = new ilSetting("common", true);
155  $set->set("db_version", $a_version);
156  $this->currentVersion = $a_version;
157 
158  return true;
159  }
160 
166  function setRunningStatus($a_nr)
167  {
168  include_once './Services/Administration/classes/class.ilSetting.php';
169  $set = new ilSetting("common", true);
170  $set->set("db_update_running", $a_nr);
171  $this->db_update_running = $a_nr;
172  }
173 
179  function getRunningStatus()
180  {
181  include_once './Services/Administration/classes/class.ilSetting.php';
182  $set = new ilSetting("common", true);
183  $this->db_update_running = (integer) $set->get("db_update_running");
184 
185  return $this->db_update_running;
186  }
187 
192  {
193  include_once './Services/Administration/classes/class.ilSetting.php';
194  $set = new ilSetting("common", true);
195  $set->set("db_update_running", 0);
196  $this->db_update_running = 0;
197  }
198 
199  function readFileVersion()
200  {
201  //go through filecontent and search for last occurence of <#x>
202  reset($this->lastfilecontent);
203  $regs = array();
204  foreach ($this->lastfilecontent as $row)
205  {
206  if (ereg("^<#([0-9]+)>", $row, $regs))
207  {
208  $version = $regs[1];
209  }
210  }
211 
212  $this->fileVersion = (integer) $version;
213  return $this->fileVersion;
214  }
215 
219  function getFileVersion()
220  {
221  return $this->fileVersion;
222  }
223 
230  function execQuery($db,$str)
231  {
232  $sql = explode("\n",trim($str));
233  for ($i=0; $i<count($sql); $i++)
234  {
235  $sql[$i] = trim($sql[$i]);
236  if ($sql[$i] != "" && substr($sql[$i],0,1)!="#")
237  {
238  //take line per line, until last char is ";"
239  if (substr($sql[$i],-1)==";")
240  {
241  //query is complete
242  $q .= " ".substr($sql[$i],0,-1);
243  $check = $this->checkQuery($q);
244  if ($check === true)
245  {
246  $r = $db->query($q);
247  if (MDB2::isError($r))
248  {
249  $this->error = $r->getMessage();
250  return false;
251  }
252  }
253  else
254  {
255  $this->error = $check;
256  return false;
257  }
258  unset($q);
259  } //if
260  else
261  {
262  $q .= " ".$sql[$i];
263  } //else
264  } //if
265  } //for
266  if ($q != "")
267  {
268  echo "incomplete_statement: ".$q."<br>";
269  return false;
270  }
271  return true;
272  }
273 
277  function checkQuery($q)
278  {
279  return true;
280  }
281 
285  function applyUpdate($a_break = 0)
286  {
287  global $ilCtrlStructureReader, $ilMySQLAbstraction;
288 
289  include_once './Services/Database/classes/class.ilMySQLAbstraction.php';
290 
291  $ilMySQLAbstraction = new ilMySQLAbstraction();
292  $GLOBALS['ilMySQLAbstraction'] = $ilMySQLAbstraction;
293 
294  $f = $this->fileVersion;
296 
297  if ($a_break > $this->currentVersion &&
298  $a_break < $this->fileVersion)
299  {
300  $f = $a_break;
301  }
302 
303  if ($c < $f)
304  {
305  $msg = array();
306  for ($i=($c+1); $i<=$f; $i++)
307  {
308  // check wether next update file must be loaded
309  if ($this->current_file != $this->getFileForStep($i))
310  {
311  $this->DB_UPDATE_FILE = $this->PATH."setup/sql/".$this->getFileForStep($i);
312  $this->readDBUpdateFile();
313  }
314 
315  $this->initStep($i);
316 
317  if ($this->applyUpdateNr($i) == false)
318  {
319  $msg[] = array(
320  "msg" => "update_error: ".$this->error,
321  "nr" => $i
322  );
323  $this->updateMsg = $msg;
324  return false;
325  }
326  else
327  {
328  $msg[] = array(
329  "msg" => "update_applied",
330  "nr" => $i
331  );
332  }
333  }
334 
335  $this->updateMsg = $msg;
336  }
337  else
338  {
339  $this->updateMsg = "no_changes";
340  }
341 
342  if ($f < $this->fileVersion)
343  {
344  return true;
345  }
346  else
347  {
348  return $this->loadXMLInfo();
349  }
350  }
351 
352  function loadXMLInfo()
353  {
354  global $ilCtrlStructureReader;
355 
356  // read module and service information into db
357  require_once "./setup/classes/class.ilModuleReader.php";
358  require_once "./setup/classes/class.ilServiceReader.php";
359  require_once "./setup/classes/class.ilCtrlStructureReader.php";
360 
361  require_once "./Services/Component/classes/class.ilModule.php";
362  require_once "./Services/Component/classes/class.ilService.php";
365 
367  foreach($modules as $module)
368  {
369  $mr = new ilModuleReader(ILIAS_ABSOLUTE_PATH."/Modules/".$module["subdir"]."/module.xml",
370  $module["subdir"], "Modules");
371  $mr->getModules();
372  unset($mr);
373  }
374 
376  foreach($services as $service)
377  {
378  $sr = new ilServiceReader(ILIAS_ABSOLUTE_PATH."/Services/".$service["subdir"]."/service.xml",
379  $service["subdir"], "Services");
380  $sr->getServices();
381  unset($sr);
382  }
383 
384  $ilCtrlStructureReader->readStructure();
385 
386  return true;
387  }
388 
395  function applyUpdateNr($nr, $hotfix = false)
396  {
397  global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader,$ilModuleReader,$ilMySQLAbstraction;
398 
399  //search for desired $nr
400  reset($this->filecontent);
401 
402  if (!$hotfix)
403  {
404  $this->setRunningStatus($nr);
405  }
406 
407  //init
408  $i = 0;
409 
410  //go through filecontent
411  while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
412  {
413  $i++;
414  }
415 
416  //update not found
417  if ($i == count($this->filecontent))
418  {
419  $this->error = "update_not_found";
420  return false;
421  }
422 
423  $i++;
424 
425  //update found, now extract this update to a new array
426  $update = array();
427  while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
428  {
429  $update[] = trim($this->filecontent[$i]);
430  $i++;
431  }
432 
433  //now you have the update, now process it
434  $sql = array();
435  $php = array();
436  $mode = "sql";
437 
438  foreach ($update as $row)
439  {
440  if (ereg("<\?php", $row))
441  {
442  if (count($sql)>0)
443  {
444  if ($this->execQuery($this->db, implode("\n", $sql)) == false)
445  {
446  $this->error = $this->error;
447  return false;
448  }
449  $sql = array();
450  }
451  $mode = "php";
452  }
453  elseif (ereg("\?>", $row))
454  {
455  if (count($php)>0)
456  {
457  $code = implode("\n", $php);
458  if (eval($code) === false)
459  {
460  $this->error = "Parse error: ".$code;
461  return false;
462  }
463  $php = array();
464  }
465  $mode = "sql";
466 
467  }
468  else
469  {
470  if ($mode == "sql")
471  {
472  $sql[] = $row;
473  }
474 
475  if ($mode == "php")
476  {
477  $php[] = $row;
478  }
479  } //else
480  } //foreach
481 
482  if ($mode == "sql" && count($sql) > 0)
483  {
484  if ($this->execQuery($this->db, implode("\n", $sql)) == false)
485  {
486  $this->error = "dump_error: ".$this->error;
487  return false;
488  }
489  }
490 
491  //increase db_Version number
492  if (!$hotfix)
493  {
494  $this->setCurrentVersion($nr);
495  }
496  else
497  {
498  $this->setHotfixCurrentVersion($nr);
499  }
500 
501  if (!$hotfix)
502  {
503  $this->clearRunningStatus();
504  }
505  //$this->currentVersion = $ilias->getSetting("db_version");
506 
507  return true;
508 
509  }
510 
512  {
513  if ($this->fileVersion > $this->currentVersion)
514  return false;
515  else
516  return true;
517  }
518 
519  function getTables()
520  {
521  $a = array();
522 
523  $query = "SHOW TABLES";
524  $res = $this->db->query($query);
525  while ($row = $res->fetchRow())
526  {
527  $status = $this->getTableStatus($row[0]);
528  $a[] = array(
529  "name" => $status["Table"],
530  "table" => $row[0],
531  "status" => $status["Msg_text"]
532  );
533  }
534  return $a;
535  }
536 
537  function getTableStatus($table)
538  {
539  $a = array();
540 
541  $query = "ANALYZE TABLE ".$table;
542  $res = $this->db->query($query);
543  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
544  return $row;
545  }
546 
547  function optimizeTables($tables)
548  {
549  $msg = array();
550  foreach ($_POST["tables"] as $key => $value)
551  {
552  $query = "OPTIMIZE TABLE ".$key;
553  $res = $this->db->query($query);
554  $msg[] = "table $key: ok";
555  }
556  return $msg;
557  }
558 
562 
563 
568  {
569  $this->readHotfixInfo();
570  return $this->hotfix_current_version;
571  }
572 
576  function setHotfixCurrentVersion($a_version)
577  {
578  $this->readHotfixInfo();
579  $this->hotfix_setting->set("db_hotfixes_".
580  $this->hotfix_version[0]."_".$this->hotfix_version[1], $a_version);
581  $this->hotfix_current_version = $a_version;
582  return true;
583  }
584 
589  {
590  $this->readHotfixInfo();
591  return $this->hotfix_file_version;
592  }
593 
597  function readHotfixFileVersion($a_file_content)
598  {
599  //go through filecontent and search for last occurence of <#x>
600  reset($a_file_content);
601  $regs = array();
602  foreach ($a_file_content as $row)
603  {
604  if (ereg("^<#([0-9]+)>", $row, $regs))
605  {
606  $version = $regs[1];
607  }
608  }
609 
610  return (integer) $version;
611  }
612 
616  function readHotfixInfo($a_force = false)
617  {
618  if ($this->hotfix_info_read && !$a_force)
619  {
620  return;
621  }
622  include_once './Services/Administration/classes/class.ilSetting.php';
623  $GLOBALS["ilDB"] = $this->db;
624  $this->hotfix_setting = new ilSetting("common", true);
625  $ilias_version = ILIAS_VERSION_NUMERIC;
626  $version_array = explode(".", $ilias_version);
627  $this->hotfix_version[0] = $version_array[0];
628  $this->hotfix_version[1] = $version_array[1];
629  $hotfix_file = $this->PATH."setup/sql/".$this->hotfix_version[0]."_".$this->hotfix_version[1]."_hotfixes.php";
630  if (is_file($hotfix_file))
631  {
632  $this->hotfix_content = @file($hotfix_file);
633  $this->hotfix_current_version = (int) $this->hotfix_setting->get("db_hotfixes_".
634  $this->hotfix_version[0]."_".$this->hotfix_version[1]);
635  $this->hotfix_file_version = $this->readHotfixFileVersion($this->hotfix_content);
636  }
637  $this->hotfix_info_read = true;
638  }
639 
643  function hotfixAvailable()
644  {
645  $this->readHotfixInfo();
646  if ($this->hotfix_file_version > $this->hotfix_current_version)
647  {
648  return true;
649  }
650  return false;
651  }
652 
656  function applyHotfix()
657  {
658  global $ilCtrlStructureReader, $ilMySQLAbstraction;
659 
660  include_once './Services/Database/classes/class.ilMySQLAbstraction.php';
661 
662  $ilMySQLAbstraction = new ilMySQLAbstraction();
663  $GLOBALS['ilMySQLAbstraction'] = $ilMySQLAbstraction;
664 
665  $this->readHotfixInfo(true);
666 
667  $f = $this->getHotfixFileVersion();
668  $c = $this->getHotfixCurrentVersion();
669 
670  if ($c < $f)
671  {
672  $msg = array();
673  for ($i=($c+1); $i<=$f; $i++)
674  {
675 // $this->initStep($i); // nothings happens here
676 
677  $this->filecontent = $this->hotfix_content;
678 
679  if ($this->applyUpdateNr($i, true) == false)
680  {
681  $msg[] = array(
682  "msg" => "update_error: ".$this->error,
683  "nr" => $i
684  );
685  $this->updateMsg = $msg;
686  return false;
687  }
688  else
689  {
690  $msg[] = array(
691  "msg" => "hotfix_applied",
692  "nr" => $i
693  );
694  }
695  }
696 
697  $this->updateMsg = $msg;
698  }
699  else
700  {
701  $this->updateMsg = "no_changes";
702  }
703 
704  return $this->loadXMLInfo();
705  }
706 
707 
708 } // END class.DBUdate
709 ?>