/pearweb_election.php

https://github.com/till/pearweb · PHP · 169 lines · 136 code · 3 blank · 30 comment · 23 complexity · a7ed45670405ad12ef054132cff3a047 MD5 · raw file

  1. <?php
  2. require_once 'MDB2/Schema.php';
  3. class pearweb_election_postinstall
  4. {
  5. var $lastversion;
  6. var $dsn;
  7. /**
  8. * Frontend object
  9. * @var PEAR_Frontend
  10. * @access private
  11. */
  12. var $_ui;
  13. function init(&$config, &$pkg, $lastversion)
  14. {
  15. $this->_ui = &PEAR_Frontend::singleton();
  16. $this->lastversion = $lastversion;
  17. return true;
  18. }
  19. function run($answers, $phase)
  20. {
  21. switch ($phase) {
  22. case 'askdb' :
  23. if ($answers['yesno'] == 'n') {
  24. $this->_ui->skipParamgroup('init');
  25. }
  26. return true;
  27. break;
  28. case 'init' :
  29. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  30. if (PEAR::isError($err = MDB2::loadFile('Driver' . DIRECTORY_SEPARATOR .
  31. $answers['driver']))) {
  32. PEAR::popErrorHandling();
  33. $this->_ui->outputData('ERROR: Unknown MDB2 driver "' .
  34. $answers['driver'] . '": ' .
  35. $err->getUserInfo() . '. Be sure you have installed ' .
  36. 'MDB2_Driver_' . $answers['driver']);
  37. return false;
  38. }
  39. PEAR::popErrorHandling();
  40. if ($answers['driver'] !== 'mysqli') {
  41. $this->_ui->outputData('pearweb only supports mysqli, ' .
  42. 'not ' . $answers['driver']);
  43. return false;
  44. }
  45. return $this->initializeDatabase($answers);
  46. break;
  47. }
  48. return true;
  49. }
  50. /**
  51. * Create or upgrade the database needed for pearweb
  52. *
  53. * This helper function scans for previous database versions,
  54. * and upgrades the database based on differences between the
  55. * previous version's schema and the one distributed with this
  56. * version.
  57. *
  58. * If the database has never been created, then it is created.
  59. *
  60. * @param array $answers
  61. * @return boolean
  62. */
  63. function initializeDatabase($answers)
  64. {
  65. //include_once dirname(__FILE__) . 'include/pear-config.php';
  66. //$a = MDB2_Schema::factory(PEAR_DATABASE_DSN,
  67. $this->dsn = array(
  68. 'phptype' => $answers['driver'],
  69. 'username' => $answers['user'],
  70. 'password' => $answers['password'],
  71. 'hostspec' => $answers['host'],
  72. 'database' => $answers['database']);
  73. $a = MDB2_Schema::factory($this->dsn,
  74. array('idxname_format' => '%s',
  75. 'seqname_format' => 'id',
  76. 'quote_identifier' => true));
  77. // for upgrade purposes
  78. if (!file_exists('@www-dir@' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR .
  79. '.pearweb-upgrade')) {
  80. if (!mkdir('@www-dir@' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR .
  81. '.pearweb-upgrade')) {
  82. $this->_ui->outputData('error - make sure we can create directories');
  83. return false;
  84. }
  85. }
  86. PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  87. $c = $a->parseDatabaseDefinitionFile(
  88. realpath('@www-dir@/sql/pearweb_election.xml'));
  89. PEAR::staticPopErrorHandling();
  90. if (PEAR::isError($c)) {
  91. $extra = '';
  92. if (MDB2_Schema::isError($c) || MDB2::isError($c)) {
  93. $extra = "\n" . $c->getUserInfo();
  94. }
  95. $this->_ui->outputData('ERROR: ' . $c->getMessage() . $extra);
  96. return false;
  97. }
  98. $c['name'] = $answers['database'];
  99. $c['create'] = 1;
  100. $c['overwrite'] = 0;
  101. $dir = opendir('@www-dir@/sql/.pearweb-upgrade');
  102. $oldversion = false;
  103. while (false !== ($entry = readdir($dir))) {
  104. if ($entry[0] === '.') {
  105. continue;
  106. }
  107. if (strpos($entry, $answers['database']) === 0) {
  108. // this is one of ours
  109. // strip databasename-
  110. $entry = substr($entry, strlen($answers['database']) + 1);
  111. // strip ".ser"
  112. $entry = substr($entry, 0, strlen($entry) - 4);
  113. // ... and we're left with just the version
  114. if (!$oldversion) {
  115. $oldversion = $entry;
  116. continue;
  117. }
  118. if (version_compare($entry, $oldversion, '>')) {
  119. $oldversion = $entry;
  120. }
  121. }
  122. }
  123. if (!file_exists('@www-dir@/sql/.pearweb-upgrade/' .
  124. $answers['database'] . '-@version@.ser')) {
  125. $fp = fopen('@www-dir@/sql/.pearweb-upgrade/' .
  126. $answers['database'] . '-@version@.ser', 'w');
  127. fwrite($fp, serialize($c));
  128. fclose($fp);
  129. }
  130. if ($oldversion == '@version@') {
  131. // this is where to change if we need to add a "force upgrade of
  132. // structure" option
  133. // we would uncomment the following line:
  134. //$c['overwrite'] = true;
  135. $oldversion = false;
  136. }
  137. if ($oldversion) {
  138. $curdef = unserialize(file_get_contents('@www-dir@/sql/.pearweb-upgrade/' .
  139. $answers['database'] . '-' . $oldversion . '.ser'));
  140. if (!is_array($curdef)) {
  141. $this->_ui->outputData('invalid data returned from previous version');
  142. }
  143. // get a database diff (MDB2_Schema is very useful here)
  144. PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  145. $c = $a->compareDefinitions($c, $curdef);
  146. if (PEAR::isError($c)) {
  147. $this->_ui->outputData($err->getMessage());
  148. $this->_ui->outputData($err->getUserInfo());
  149. $this->_ui->outputData('Unable to automatically update database');
  150. return false;
  151. }
  152. $err = $a->updateDatabase($curdef, $c);
  153. PEAR::staticPopErrorHandling();
  154. } else {
  155. PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  156. $err = $a->createDatabase($c);
  157. PEAR::staticPopErrorHandling();
  158. }
  159. if (PEAR::isError($err)) {
  160. $this->_ui->outputData($err->getUserInfo());
  161. $this->_ui->outputData($err->getMessage());
  162. return false;
  163. }
  164. return true;
  165. }
  166. }