create_upgrade.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Creates an upgrade file for Elgg.
  4. *
  5. * Run this from the command line:
  6. * php create_upgrade.php upgrade_name
  7. */
  8. error_reporting(E_ALL);
  9. // only allow from the command line.
  10. if (php_sapi_name() != 'cli') {
  11. die('Upgrades can only be created from the command line.');
  12. }
  13. if (count($argv) < 2) {
  14. elgg_create_upgrade_show_usage('No upgrade name.');
  15. }
  16. $name = $argv[1];
  17. if (strlen($name) > 24) {
  18. elgg_create_upgrade_show_usage('Upgrade names cannot be longer than 24 characters.');
  19. }
  20. require_once '../../../version.php';
  21. $upgrade_path = dirname(__FILE__);
  22. $upgrade_name = strtolower($name);
  23. $upgrade_name = str_replace(array(' ', '-'), '_', $upgrade_name);
  24. $upgrade_release = str_replace(array(' ', '-'), '_', $release);
  25. $time = time();
  26. $upgrade_rnd = substr(md5($time), 0, 16);
  27. $upgrade_date = date('Ymd', $time);
  28. // determine the inc count
  29. $upgrade_inc = 0;
  30. $files = elgg_get_file_list($upgrade_path);
  31. sort($files);
  32. foreach ($files as $filename) {
  33. $filename = basename($filename);
  34. $date = (int)substr($filename, 0, 8);
  35. $inc = (int)substr($filename, 8, 2);
  36. if ($upgrade_date == $date) {
  37. if ($inc >= $upgrade_inc) {
  38. $upgrade_inc = $inc + 1;
  39. }
  40. }
  41. }
  42. // zero-pad
  43. // if there are more than 10 upgrades in a day, someone needs talking to.
  44. if ($upgrade_inc < 10) {
  45. $upgrade_inc = "0$upgrade_inc";
  46. }
  47. $upgrade_version = $upgrade_date . $upgrade_inc;
  48. // make filename
  49. if (substr($release, 0, 3) == '1.7') {
  50. // 1.7 upgrades are YYYYMMDDXX
  51. $upgrade_name = $upgrade_version . '.php';
  52. } else {
  53. // 1.8+ upgrades are YYYYMMDDXX-release-friendly_name-rnd
  54. $upgrade_name = $upgrade_version . "-$upgrade_release-$name-$upgrade_rnd.php";
  55. }
  56. $upgrade_file = $upgrade_path . '/' . $upgrade_name;
  57. if (is_file($upgrade_file)) {
  58. elgg_create_upgrade_show_usage("Upgrade file $upgrade_file already exists. This script has failed you.");
  59. }
  60. $upgrade_code = <<<___UPGRADE
  61. <?php
  62. /**
  63. * Elgg $release upgrade $upgrade_version
  64. * $name
  65. *
  66. * Description
  67. */
  68. // upgrade code here.
  69. ___UPGRADE;
  70. $h = fopen($upgrade_file, 'wb');
  71. if (!$h) {
  72. die("Could not open file $upgrade_file");
  73. }
  74. if (!fwrite($h, $upgrade_code)) {
  75. die("Could not write to $upgrade_file");
  76. } else {
  77. elgg_set_version_dot_php_version($upgrade_version);
  78. echo <<<___MSG
  79. Created upgrade file and updated version.php.
  80. Upgrade file: $upgrade_name
  81. Version: $upgrade_version
  82. ___MSG;
  83. }
  84. fclose($h);
  85. /**
  86. * Updates the version number in elgg/version.php
  87. *
  88. * @param string $version
  89. * @return boolean
  90. */
  91. function elgg_set_version_dot_php_version($version) {
  92. $file = '../../../version.php';
  93. $h = fopen($file, 'r+b');
  94. if (!$h) {
  95. return false;
  96. }
  97. $out = '';
  98. while (($line = fgets($h)) !== false) {
  99. $find = "/\\\$version[ ]?=[ ]?[0-9]{10};/";
  100. $replace = "\$version = $version;";
  101. $out .= preg_replace($find, $replace, $line);
  102. }
  103. rewind($h);
  104. fwrite($h, $out);
  105. fclose($h);
  106. return true;
  107. }
  108. /**
  109. * Shows the usage for the create_upgrade script and dies().
  110. *
  111. * @param string $msg Optional message to display
  112. * @return void
  113. */
  114. function elgg_create_upgrade_show_usage($msg = '') {
  115. $text = <<<___MSG
  116. $msg
  117. Example:
  118. php create_upgrade.php my_upgrade
  119. ___MSG;
  120. die($text);
  121. }
  122. /**
  123. * C&P'd helpers from core to avoid pulling in everything.
  124. */
  125. /**
  126. * Returns a list of files in $directory.
  127. *
  128. * Only returns files. Does not recurse into subdirs.
  129. *
  130. * @param string $directory Directory to look in
  131. * @param array $exceptions Array of filenames to ignore
  132. * @param array $list Array of files to append to
  133. * @param mixed $extensions Array of extensions to allow, NULL for all. Use a dot: array('.php').
  134. *
  135. * @return array Filenames in $directory, in the form $directory/filename.
  136. */
  137. function elgg_get_file_list($directory, $exceptions = array(), $list = array(),
  138. $extensions = NULL) {
  139. $directory = sanitise_filepath($directory);
  140. if ($handle = opendir($directory)) {
  141. while (($file = readdir($handle)) !== FALSE) {
  142. if (!is_file($directory . $file) || in_array($file, $exceptions)) {
  143. continue;
  144. }
  145. if (is_array($extensions)) {
  146. if (in_array(strrchr($file, '.'), $extensions)) {
  147. $list[] = $directory . $file;
  148. }
  149. } else {
  150. $list[] = $directory . $file;
  151. }
  152. }
  153. closedir($handle);
  154. }
  155. return $list;
  156. }
  157. /**
  158. * Sanitise file paths ensuring that they begin and end with slashes etc.
  159. *
  160. * @param string $path The path
  161. * @param bool $append_slash Add tailing slash
  162. *
  163. * @return string
  164. */
  165. function sanitise_filepath($path, $append_slash = TRUE) {
  166. // Convert to correct UNIX paths
  167. $path = str_replace('\\', '/', $path);
  168. $path = str_replace('../', '/', $path);
  169. // replace // with / except when preceeded by :
  170. $path = preg_replace("/([^:])\/\//", "$1/", $path);
  171. // Sort trailing slash
  172. $path = trim($path);
  173. // rtrim defaults plus /
  174. $path = rtrim($path, " \n\t\0\x0B/");
  175. if ($append_slash) {
  176. $path = $path . '/';
  177. }
  178. return $path;
  179. }