ILIAS  Release_4_4_x_branch Revision 61816
 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 
366  $mr = new ilModuleReader("", "", "");
367  $mr->clearTables();
368  foreach($modules as $module)
369  {
370  $mr = new ilModuleReader(ILIAS_ABSOLUTE_PATH."/Modules/".$module["subdir"]."/module.xml",
371  $module["subdir"], "Modules");
372  $mr->getModules();
373  unset($mr);
374  }
375 
376  $sr = new ilServiceReader("", "", "");
377  $sr->clearTables();
378  foreach($services as $service)
379  {
380  $sr = new ilServiceReader(ILIAS_ABSOLUTE_PATH."/Services/".$service["subdir"]."/service.xml",
381  $service["subdir"], "Services");
382  $sr->getServices();
383  unset($sr);
384  }
385 
386  $ilCtrlStructureReader->readStructure();
387 
388  return true;
389  }
390 
397  function applyUpdateNr($nr, $hotfix = false, $custom_update = false)
398  {
399  global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader,$ilModuleReader,$ilMySQLAbstraction;
400 
401  //search for desired $nr
402  reset($this->filecontent);
403 
404  if (!$hotfix)
405  {
406  $this->setRunningStatus($nr);
407  }
408 
409  //init
410  $i = 0;
411 
412  //go through filecontent
413  while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
414  {
415  $i++;
416  }
417 
418  //update not found
419  if ($i == count($this->filecontent))
420  {
421  $this->error = "update_not_found";
422  return false;
423  }
424 
425  $i++;
426 
427  //update found, now extract this update to a new array
428  $update = array();
429  while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
430  {
431  $update[] = trim($this->filecontent[$i]);
432  $i++;
433  }
434 
435  //now you have the update, now process it
436  $sql = array();
437  $php = array();
438  $mode = "sql";
439 
440  foreach ($update as $row)
441  {
442  if (ereg("<\?php", $row))
443  {
444  if (count($sql)>0)
445  {
446  if ($this->execQuery($this->db, implode("\n", $sql)) == false)
447  {
448  $this->error = $this->error;
449  return false;
450  }
451  $sql = array();
452  }
453  $mode = "php";
454  }
455  elseif (ereg("\?>", $row))
456  {
457  if (count($php)>0)
458  {
459  $code = implode("\n", $php);
460  if (eval($code) === false)
461  {
462  $this->error = "Parse error: ".$code;
463  return false;
464  }
465  $php = array();
466  }
467  $mode = "sql";
468 
469  }
470  else
471  {
472  if ($mode == "sql")
473  {
474  $sql[] = $row;
475  }
476 
477  if ($mode == "php")
478  {
479  $php[] = $row;
480  }
481  } //else
482  } //foreach
483 
484  if ($mode == "sql" && count($sql) > 0)
485  {
486  if ($this->execQuery($this->db, implode("\n", $sql)) == false)
487  {
488  $this->error = "dump_error: ".$this->error;
489  return false;
490  }
491  }
492 
493  //increase db_Version number
494  if (!$hotfix && !$custom_update)
495  {
496  $this->setCurrentVersion($nr);
497  }
498  elseif($hotfix)
499  {
500  $this->setHotfixCurrentVersion($nr);
501  }
502  elseif($custom_update)
503  {
504  $this->setCustomUpdatesCurrentVersion($nr);
505  }
506 
507  if (!$hotfix && !$custom_update)
508  {
509  $this->clearRunningStatus();
510  }
511  //$this->currentVersion = $ilias->getSetting("db_version");
512 
513  return true;
514 
515  }
516 
518  {
519  if ($this->fileVersion > $this->currentVersion)
520  return false;
521  else
522  return true;
523  }
524 
525  function getTables()
526  {
527  $a = array();
528 
529  $query = "SHOW TABLES";
530  $res = $this->db->query($query);
531  while ($row = $res->fetchRow())
532  {
533  $status = $this->getTableStatus($row[0]);
534  $a[] = array(
535  "name" => $status["Table"],
536  "table" => $row[0],
537  "status" => $status["Msg_text"]
538  );
539  }
540  return $a;
541  }
542 
543  function getTableStatus($table)
544  {
545  $a = array();
546 
547  $query = "ANALYZE TABLE ".$table;
548  $res = $this->db->query($query);
549  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
550  return $row;
551  }
552 
553  function optimizeTables($tables)
554  {
555  $msg = array();
556  foreach ($_POST["tables"] as $key => $value)
557  {
558  $query = "OPTIMIZE TABLE ".$key;
559  $res = $this->db->query($query);
560  $msg[] = "table $key: ok";
561  }
562  return $msg;
563  }
564 
568 
569 
574  {
575  $this->readHotfixInfo();
576  return $this->hotfix_current_version;
577  }
578 
582  function setHotfixCurrentVersion($a_version)
583  {
584  $this->readHotfixInfo();
585  $this->hotfix_setting->set("db_hotfixes_".
586  $this->hotfix_version[0]."_".$this->hotfix_version[1], $a_version);
587  $this->hotfix_current_version = $a_version;
588  return true;
589  }
590 
595  {
596  $this->readHotfixInfo();
597  return $this->hotfix_file_version;
598  }
599 
603  function readHotfixFileVersion($a_file_content)
604  {
605  //go through filecontent and search for last occurence of <#x>
606  reset($a_file_content);
607  $regs = array();
608  foreach ($a_file_content as $row)
609  {
610  if (ereg("^<#([0-9]+)>", $row, $regs))
611  {
612  $version = $regs[1];
613  }
614  }
615 
616  return (integer) $version;
617  }
618 
622  function readHotfixInfo($a_force = false)
623  {
624  if ($this->hotfix_info_read && !$a_force)
625  {
626  return;
627  }
628  include_once './Services/Administration/classes/class.ilSetting.php';
629  $GLOBALS["ilDB"] = $this->db;
630  $this->hotfix_setting = new ilSetting("common", true);
631  $ilias_version = ILIAS_VERSION_NUMERIC;
632  $version_array = explode(".", $ilias_version);
633  $this->hotfix_version[0] = $version_array[0];
634  $this->hotfix_version[1] = $version_array[1];
635  $hotfix_file = $this->PATH."setup/sql/".$this->hotfix_version[0]."_".$this->hotfix_version[1]."_hotfixes.php";
636  if (is_file($hotfix_file))
637  {
638  $this->hotfix_content = @file($hotfix_file);
639  $this->hotfix_current_version = (int) $this->hotfix_setting->get("db_hotfixes_".
640  $this->hotfix_version[0]."_".$this->hotfix_version[1]);
641  $this->hotfix_file_version = $this->readHotfixFileVersion($this->hotfix_content);
642  }
643  $this->hotfix_info_read = true;
644  }
645 
649  function hotfixAvailable()
650  {
651  $this->readHotfixInfo();
652  if ($this->hotfix_file_version > $this->hotfix_current_version)
653  {
654  return true;
655  }
656  return false;
657  }
658 
662  function applyHotfix()
663  {
664  global $ilCtrlStructureReader, $ilMySQLAbstraction;
665 
666  include_once './Services/Database/classes/class.ilMySQLAbstraction.php';
667 
668  $ilMySQLAbstraction = new ilMySQLAbstraction();
669  $GLOBALS['ilMySQLAbstraction'] = $ilMySQLAbstraction;
670 
671  $this->readHotfixInfo(true);
672 
673  $f = $this->getHotfixFileVersion();
674  $c = $this->getHotfixCurrentVersion();
675 
676  if ($c < $f)
677  {
678  $msg = array();
679  for ($i=($c+1); $i<=$f; $i++)
680  {
681 // $this->initStep($i); // nothings happens here
682 
683  $this->filecontent = $this->hotfix_content;
684 
685  if ($this->applyUpdateNr($i, true) == false)
686  {
687  $msg[] = array(
688  "msg" => "update_error: ".$this->error,
689  "nr" => $i
690  );
691  $this->updateMsg = $msg;
692  return false;
693  }
694  else
695  {
696  $msg[] = array(
697  "msg" => "hotfix_applied",
698  "nr" => $i
699  );
700  }
701  }
702 
703  $this->updateMsg = $msg;
704  }
705  else
706  {
707  $this->updateMsg = "no_changes";
708  }
709 
710  return $this->loadXMLInfo();
711  }
712 
714  {
715  $this->readCustomUpdatesInfo();
716  return $this->custom_updates_current_version;
717  }
718 
719  public function setCustomUpdatesCurrentVersion($a_version)
720  {
721  $this->readCustomUpdatesInfo();
722  $this->custom_updates_setting->set('db_version_custom', $a_version);
723  $this->custom_updates_current_version = $a_version;
724  return true;
725  }
726 
727  public function getCustomUpdatesFileVersion()
728  {
729  $this->readCustomUpdatesInfo();
730  return $this->custom_updates_file_version;
731  }
732 
733  public function readCustomUpdatesFileVersion($a_file_content)
734  {
735  //go through filecontent and search for last occurence of <#x>
736  reset($a_file_content);
737  $regs = array();
738  foreach ($a_file_content as $row)
739  {
740  if (ereg("^<#([0-9]+)>", $row, $regs))
741  {
742  $version = $regs[1];
743  }
744  }
745 
746  return (integer) $version;
747  }
748 
749  public function readCustomUpdatesInfo($a_force = false)
750  {
751  if ($this->custom_updates_info_read && !$a_force)
752  {
753  return;
754  }
755  include_once './Services/Administration/classes/class.ilSetting.php';
756  $GLOBALS["ilDB"] = $this->db;
757  $this->custom_updates_setting = new ilSetting();
758  $custom_updates_file = $this->PATH."setup/sql/dbupdate_custom.php";
759  if (is_file($custom_updates_file))
760  {
761  $this->custom_updates_content = @file($custom_updates_file);
762  $this->custom_updates_current_version = (int) $this->custom_updates_setting->get('db_version_custom', 0);
763  $this->custom_updates_file_version = $this->readCustomUpdatesFileVersion($this->custom_updates_content);
764  }
765  $this->custom_updates_info_read = true;
766  }
767 
768  public function customUpdatesAvailable()
769  {
770  // trunk does not support custom updates
771  return false;
772 
773  $this->readCustomUpdatesInfo();
774  if ($this->custom_updates_file_version > $this->custom_updates_current_version)
775  {
776  return true;
777  }
778  return false;
779  }
780 
781  public function applyCustomUpdates()
782  {
783  global $ilCtrlStructureReader, $ilMySQLAbstraction;
784 
785  include_once './Services/Database/classes/class.ilMySQLAbstraction.php';
786 
787  $ilMySQLAbstraction = new ilMySQLAbstraction();
788  $GLOBALS['ilMySQLAbstraction'] = $ilMySQLAbstraction;
789 
790  $this->readCustomUpdatesInfo(true);
791 
792  $f = $this->getCustomUpdatesFileVersion();
793  $c = $this->getCustomUpdatesCurrentVersion();
794 
795  if ($c < $f)
796  {
797  $msg = array();
798  for ($i=($c+1); $i<=$f; $i++)
799  {
800 // $this->initStep($i); // nothings happens here
801 
802  $this->filecontent = $this->custom_updates_content;
803 
804  if ($this->applyUpdateNr($i, false, true) == false)
805  {
806  $msg[] = array(
807  "msg" => "update_error: ".$this->error,
808  "nr" => $i
809  );
810  $this->updateMsg = $msg;
811  return false;
812  }
813  else
814  {
815  $msg[] = array(
816  "msg" => "custom_update_applied",
817  "nr" => $i
818  );
819  }
820  }
821 
822  $this->updateMsg = $msg;
823  }
824  else
825  {
826  $this->updateMsg = "no_changes";
827  }
828 
829  return $this->loadXMLInfo();
830  }
831 
837  function getUpdateSteps($a_break = 0)
838  {
839  global $ilCtrlStructureReader, $ilMySQLAbstraction;
840 
841  $str = "";
842 
843  $f = $this->fileVersion;
845 
846  if ($a_break > $this->currentVersion &&
847  $a_break < $this->fileVersion)
848  {
849  $f = $a_break;
850  }
851 
852  if ($c < $f)
853  {
854  $msg = array();
855  for ($i=($c+1); $i<=$f; $i++)
856  {
857  // check wether next update file must be loaded
858  if ($this->current_file != $this->getFileForStep($i))
859  {
860  $this->DB_UPDATE_FILE = $this->PATH."setup/sql/".$this->getFileForStep($i);
861  $this->readDBUpdateFile();
862  }
863 
864  $str.= $this->getUpdateStepNr($i);
865  }
866 
867  }
868  return $str;
869  }
870 
876  function getHotfixSteps()
877  {
878  $this->readHotfixInfo(true);
879 
880  $str = "";
881 
882  $f = $this->getHotfixFileVersion();
883  $c = $this->getHotfixCurrentVersion();
884 
885  if ($c < $f)
886  {
887  $msg = array();
888  for ($i=($c+1); $i<=$f; $i++)
889  {
890  $this->filecontent = $this->hotfix_content;
891 
892  $str.= $this->getUpdateStepNr($i, true);
893  }
894  }
895 
896  return $str;
897  }
898 
899 
903  function getUpdateStepNr($nr, $hotfix = false, $custom_update = false)
904  {
905  global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader,$ilModuleReader,$ilMySQLAbstraction;
906 
907  $str = "";
908 
909  //search for desired $nr
910  reset($this->filecontent);
911 
912  //init
913  $i = 0;
914 
915  //go through filecontent
916  while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
917  {
918  $i++;
919  }
920 
921  //update not found
922  if ($i == count($this->filecontent))
923  {
924  return false;
925  }
926 
927  $i++;
928 
929  //update found, now extract this update to a new array
930  $update = array();
931  while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
932  {
933  $str.= $this->filecontent[$i];
934  $i++;
935  }
936 
937  return "<pre><b><#".$nr."></b>\n".htmlentities($str)."</pre>";
938  }
939 
940 } // END class.DBUdate
941 ?>