db_importer.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. require_once('../application/startup.inc.php');
  5. require_once('../application/loginwebpage.class.inc.php');
  6. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  7. $sOperation = utils::ReadParam('operation', 'menu');
  8. $oContext = new UserContext();
  9. $oAppContext = new ApplicationContext();
  10. $iActiveNodeId = utils::ReadParam('menu', -1);
  11. $currentOrganization = utils::ReadParam('org_id', '');
  12. $oP = new iTopWebPage("iTop - Database backup & restore", $currentOrganization);
  13. // Main program
  14. switch($sOperation)
  15. {
  16. case 'create':
  17. $oP->add('<form method="get">');
  18. $oP->add('<p style="text-align:center; font-family:Georgia, \'Times New Roman\', Times, serif; font-size:24px;">Creation of an archive for the business model: '.$sBizModel.'</p>');
  19. $oP->p('Title of the archive: <input type="text" name="title" size="40">');
  20. $oP->p('Description of this archive: <textarea name="description" rows="5" cols="40"></textarea>');
  21. $oP->p('Select the packages you want to include into this archive (When restoring the archive you will prompted to pick a package):');
  22. $oP->p('<input type="checkbox" checked name="full" value="1"> The full database (schema + data).');
  23. $oP->p('<input type="checkbox" checked name="full_schema" value="1"> Only the schema but of the complete database.');
  24. $oP->p('<input type="checkbox" checked name="biz" value="1"> The complete business model (all the tables used by the business model, schema + data).');
  25. $oP->p('<input type="checkbox" checked name="biz_schema" value="1"> Only the schema of the business model.');
  26. $oP->add('<input type="hidden" name="biz_model" value="'.$sBizModel.'">');
  27. $oP->add('<input type="hidden" name="operation" value="do_create">');
  28. $oP->add('<input type="submit" name="" value=" Create the archive ">');
  29. $oP->add($oAppContext->GetForForm());
  30. $oP->add('</form>');
  31. $oP->p('<a href="?operation=menu&biz_model='.$sBizModel.'">Back to menu</a>');
  32. break;
  33. case 'do_create':
  34. $sTitle = utils::ReadParam('title', 'Unknown archive');
  35. $sDescription = utils::ReadParam('description', 'No description provided for this archive.');
  36. $bfullDump = utils::ReadParam('full', false);
  37. $bfullSchemaDump = utils::ReadParam('full_schema', false);
  38. $bBizDump = utils::ReadParam('biz', false);
  39. $bBizSchemaDump = utils::ReadParam('biz_schema', false);
  40. $sArchiveFile = '../tmp/archive1.zip';
  41. $oArchive = new iTopArchive($sArchiveFile, iTopArchive::create);
  42. $oArchive->SetTitle($sTitle);
  43. $oArchive->SetDescription($sDescription);
  44. if ($bfullDump)
  45. {
  46. $oArchive->AddDatabaseDump("Full Database Dump", "Choose this option to completely reload your database. All current data will be deleted and replaced by the backup", "full-db.sql", 'itop', array(), false);
  47. }
  48. if ($bfullSchemaDump)
  49. {
  50. $oArchive->AddDatabaseDump("Full Schema Dump", "Choose this option to completely wipe out your database and start from an empty database", "full-schema.sql", 'itop', array(), true);
  51. }
  52. if ($bBizDump || $bBizSchemaDump)
  53. {
  54. // compute the list of the tables involved in the business model
  55. $aBizTables = array();
  56. foreach(MetaModel::GetClasses('bizmodel') as $sClass)
  57. {
  58. $sTable = MetaModel::DBGetTable($sClass);
  59. $aBizTables[$sTable] = $sTable;
  60. }
  61. unset($aBizTables['']);
  62. if ($bfullDump)
  63. {
  64. $oArchive->AddDatabaseDump("Full Business Model Dump", "Choose this option to completely reload the business model part of your database. All current business data will be deleted and replaced by the backup. Application data (like menus...) are preserved.", "biz-db.sql", 'itop', $aBizTables, false);
  65. }
  66. if ($bfullSchemaDump)
  67. {
  68. $oArchive->AddDatabaseDump("Full Business Model Schema Dump", "Choose this option to wipe out the business data and start from an empty database. All current business data will be deleted. Application data (like menus...) are preserved.", "biz-schema.sql", 'itop', $aBizTables, true);
  69. }
  70. }
  71. $oArchive->WriteCatalog();
  72. $oP->p("The archive '$sTitle' has been created in <a href=\"$sArchiveFile\">$sArchiveFile</a>.");
  73. $oP->p('<a href="?operation=menu&biz_model='.$sBizModel.'&'.$oAppContext->GetForLink().'">Back to menu</a>');
  74. break;
  75. case 'select_archive':
  76. $sArchivesDir = '../tmp';
  77. $oP->add('<form method="get">');
  78. $oP->add('<p style="text-align:center; font-family:Georgia, \'Times New Roman\', Times, serif; font-size:24px;">Importation of an archive</p>');
  79. $oP->p('Select the archive you want to import:');
  80. $aArchives = array();
  81. if ($handle = opendir($sArchivesDir))
  82. {
  83. while (false !== ($sFileName = readdir($handle)))
  84. {
  85. if (strtolower(substr($sFileName, -3, 3)) == 'zip')
  86. {
  87. $oArchive = new iTopArchive('../tmp/'.$sFileName, iTopArchive::read);
  88. if ($oArchive->IsValid())
  89. {
  90. $oArchive->ReadCatalog();
  91. $aArchives['../tmp/'.$sFileName] = $oArchive->GetTitle();
  92. }
  93. }
  94. }
  95. closedir($handle);
  96. }
  97. foreach($aArchives as $sFileName => $sTitle)
  98. {
  99. $oP->p('<input type="radio" name="archive_file" value="'.$sFileName.'">'.$sTitle);
  100. }
  101. $oP->add('<input type="hidden" name="biz_model" value="'.$sBizModel.'">');
  102. $oP->add('<input type="hidden" name="operation" value="select_package">');
  103. $oP->add('<input type="submit" name="" value=" Next >> ">');
  104. $oP->add($oAppContext->GetForForm());
  105. $oP->add('</form>');
  106. $oP->p("<small>(Archives are searched into the directory: $sArchivesDir.)</small>");
  107. $oP->p('<a href="?operation=menu&biz_model='.$sBizModel.'&'.$oAppContext->GetForLink().'">Cancel</a>');
  108. break;
  109. case 'select_package':
  110. $sArchiveFile = utils::ReadParam('archive_file', '');
  111. $oArchive = new iTopArchive($sArchiveFile, iTopArchive::read);
  112. $oArchive->ReadCatalog();
  113. $oP->add('<form method="post">');
  114. $oP->add('<p style="text-align:center; font-family:Georgia, \'Times New Roman\', Times, serif; font-size:24px;">Selection of a package inside '.$oArchive->GetTitle().'</p>');
  115. $oP->p('Select the package you want to apply:');
  116. $aPackages = $oArchive->GetPackages();
  117. foreach($aPackages as $sPackageName => $aPackage)
  118. {
  119. $oP->p('<input type="radio" name="package_name" value="'.$sPackageName.'">'.$aPackage['title']);
  120. $oP->p($aPackage['description']);
  121. }
  122. $oP->add('<input type="hidden" name="archive_file" value="'.$sArchiveFile.'">');
  123. $oP->add('<input type="hidden" name="biz_model" value="'.$sBizModel.'">');
  124. $oP->add('<input type="hidden" name="operation" value="import_package">');
  125. $oP->add('<input type="submit" name="" value=" Apply Package ! ">');
  126. $oP->add($oAppContext->GetForForm());
  127. $oP->add('</form>');
  128. $oP->p('<a href="?operation=menu&biz_model='.$sBizModel.'">Cancel</a>');
  129. break;
  130. case 'import_package':
  131. $sArchiveFile = utils::ReadParam('archive_file', '');
  132. $sPackageName = utils::ReadParam('package_name', '');
  133. $oArchive = new iTopArchive($sArchiveFile, iTopArchive::read);
  134. $oArchive->ReadCatalog();
  135. $oP->add('<p style="text-align:center; font-family:Georgia, \'Times New Roman\', Times, serif; font-size:24px;">Applying the package '.$sPackageName.'</p>');
  136. if($oArchive->ImportSQL($sPackageName))
  137. {
  138. $oP->p('Done.');
  139. }
  140. else
  141. {
  142. $oP->p('Sorry, an error occured while applying the package...');
  143. }
  144. $oP->p('<a href="?operation=select_package&biz_model='.$sBizModel.'&archive_file='.$sArchiveFile.'&'.$oAppContext->GetForLink().'">Apply another package from the same archive</a>');
  145. $oP->p('<a href="?operation=select_archive&biz_model='.$sBizModel.'&'.$oAppContext->GetForLink().'">Select another archive</a>');
  146. $oP->p('<a href="?operation=menu&biz_model='.$sBizModel.'&'.$oAppContext->GetForLink().'">Back to the menu</a>');
  147. break;
  148. case 'menu':
  149. default:
  150. $oP->add('<p style="text-align:center; font-family:Georgia, \'Times New Roman\', Times, serif; font-size:24px;">Database backup &amp; restore</p>');
  151. $oP->add('<p style="text-align:center;">Select one of the actions below:</p>');
  152. $oP->add('<p style="text-align:center;"><a href="?operation=create&biz_model='.$sBizModel.'&'.$oAppContext->GetForLink().'">Export the database to an archive</a></p>');
  153. $oP->add('<p style="text-align:center;"><a href="?operation=select_archive&biz_model='.$sBizModel.'&'.$oAppContext->GetForLink().'">Reload the database from an archive</a></p>');
  154. }
  155. $oP->output();
  156. ?>