PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/install/cli/upgrade.php

https://github.com/devsatish/ThinkUp
PHP | 139 lines | 87 code | 13 blank | 39 comment | 19 complexity | c55a6051712e57d71c3864dfaf44ed97 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/install/cli/upgrade.php
  5. *
  6. * Copyright (c) 2009-2011 Mark Wilkie
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Command line interface for upgrading up thinkup/data
  25. *
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2011 Mark Wilkie
  28. * @author Mark Wilkie <mwilkie[at]gmail[dot]com>
  29. */
  30. chdir('../..');
  31. require_once 'init.php';
  32. //Avoid "Error: DateTime::__construct(): It is not safe to rely on the system's timezone settings" error
  33. require_once 'config.inc.php';
  34. date_default_timezone_set($THINKUP_CFG['timezone']);
  35. // don't run via the web...
  36. if(isset($_SERVER['SERVER_NAME'])) {
  37. die("This script should only be run via the command line.");
  38. }
  39. // help?
  40. array_shift($argv);
  41. if(isset($argv[0]) && preg_match('/^(\-h|\-\-help)$/i', $argv[0])) {
  42. usage();
  43. }
  44. try {
  45. // do we need a migration?
  46. $db_version = UpgradeController::getCurrentDBVersion($cached = false);
  47. $config = Config::getInstance();
  48. $thinkup_db_version = $config->getValue('THINKUP_VERSION');
  49. $filename = false;
  50. if($db_version == $thinkup_db_version) {
  51. error_log("\nYour ThinkUp database structure is up to date.\n");
  52. exit;
  53. } else {
  54. print "\nThinkup needs to be upgraded to version $thinkup_db_version, proceed => [y|n] ";
  55. $handle = fopen ("php://stdin","r");
  56. $line = fgets($handle);
  57. if(trim($line) != 'y'){
  58. exit;
  59. }
  60. print "\nWould you like to backup your data first? => [y|n] ";
  61. $handle = fopen ("php://stdin","r");
  62. $line = fgets($handle);
  63. if(trim($line) == 'y'){
  64. // we need zip support
  65. if(! BackupController::checkForZipSupport()) {
  66. print "\n Error: ThinkUp backups require Zip support\n\n";
  67. exit(1);
  68. }
  69. print "\nEnter a .zip filename (/path/tp/backup.zip) => ";
  70. $handle = fopen ("php://stdin","r");
  71. $line = fgets($handle);
  72. $filename = trim($line);
  73. }
  74. }
  75. // set global mutex
  76. BackupController::mutexLock();
  77. // run backup first?
  78. if(isset($argv[0]) && preg_match('/^(\-h|\-\-help)$/i', $argv[0])) {
  79. usage();
  80. } else if($filename) {
  81. if( ! preg_match('/\.zip$/', $filename) ) {
  82. error_log("\nError: data file should end in .zip");
  83. usage();
  84. } else {
  85. putenv('BACKUP_VERBOSE=true');
  86. $backup_dao = DAOFactory::getDAO('BackupDAO');
  87. print "\nExporting data to: $filename\n\n";
  88. $backup_dao->export($filename);
  89. print "\nBackup completed.\n\n";
  90. }
  91. }
  92. // run updates...
  93. // get migrations we need to run...
  94. print "\nUpgrading Thinkup to version $thinkup_db_version...\n\n";
  95. $upgrade_start_time = microtime(true);
  96. putenv('CLI_BACKUP=true');
  97. $upgrade_ctl = new UpgradeController();
  98. $migrations = $upgrade_ctl->getMigrationList($db_version);
  99. $install_dao = DAOFactory::getDAO('InstallerDAO');
  100. foreach($migrations as $migration) {
  101. print(" Running migration " . $migration['version'] . "\n");
  102. $install_dao->runMigrationSQL($migration['sql']);
  103. }
  104. $option_dao = DAOFactory::getDAO('OptionDAO');
  105. $option = $option_dao->getOptionByName(OptionDAO::APP_OPTIONS, 'database_version');
  106. if($option) {
  107. $option_dao->updateOptionByName(OptionDAO::APP_OPTIONS, 'database_version',$thinkup_db_version);
  108. } else {
  109. $option_dao->insertOption(OptionDAO::APP_OPTIONS, 'database_version', $thinkup_db_version);
  110. }
  111. // release global mutex
  112. BackupController::mutexLock();
  113. // delete upgrade token if it exists
  114. $upgrade_ctl->deleteTokenFile();
  115. $upgrade_end_time = microtime(true);
  116. $total_time = $upgrade_end_time - $upgrade_start_time;
  117. print "\nUpgrade complete. Total time elapsed: ".round($total_time, 2)." seconds\n\n";
  118. } catch(Exception $e) {
  119. error_log(" Error: " . $e->getMessage() . "\n");
  120. }
  121. function usage() {
  122. print "\n Usage:\n\n";
  123. print " php upgrade.php [--help]\n\n";
  124. print " --help - usage help\n\n";
  125. exit;
  126. }