security/core#97 Ensure that php scripts where applicable in sql and tools that shoul...
[civicrm-core.git] / tools / bin / scripts / set-version.php
1 #!/usr/bin/env php
2 <?php
3
4 // Update the data-files within this repo to reflect a new version number.
5 // Example usage:
6 // git checkout origin/master -b master-4.7.29
7 // ./tools/bin/scripts/set-version.php 4.7.29 --commit
8 // git commit -m "Update to version 4.7.29"
9 // git push origin master
10
11 /* *********************************************************************** */
12 /* Boot */
13 if (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)) {
14 header("HTTP/1.0 404 Not Found");
15 return;
16 }
17 $civicrm_root = dirname(dirname(dirname(__DIR__)));
18 chdir($civicrm_root);
19
20 /* *********************************************************************** */
21 /* Parse inputs -- $oldVersion, $newVersion, $doCommit */
22
23 $oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no;
24 if (!isVersionValid($oldVersion)) {
25 fatal("failed to read old version from \"xml/version.xml\"\n");
26 }
27
28 $newVersion = @$argv[1];
29 if (!isVersionValid($newVersion)) {
30 fatal("failed to read new version\n");
31 }
32
33 switch (@$argv[2]) {
34 case '--commit':
35 $doCommit = 1;
36 break;
37 case '--no-commit':
38 $doCommit = 0;
39 break;
40 default:
41 fatal("Must specify --commit or --no-commit\n");
42 }
43
44 /* *********************************************************************** */
45 /* Main */
46
47 echo "Changing version from $oldVersion to $newVersion...\n";
48
49 $verName = makeVerName($newVersion);
50 $phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName) {
51 ob_start();
52 global $camelNumber;
53 $camelNumber = $verName;
54 require 'CRM/Upgrade/Incremental/php/Template.php';
55 unset($camelNumber);
56 return ob_get_clean();
57 });
58
59 $sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function () use ($newVersion) {
60 return "{* file to handle db changes in $newVersion during upgrade *}\n";
61 });
62
63 updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
64 return str_replace($oldVersion, $newVersion, $content);
65 });
66
67 if (file_exists("civicrm-version.php")) {
68 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
69 return str_replace($oldVersion, $newVersion, $content);
70 });
71 }
72
73 updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
74 return str_replace($oldVersion, $newVersion, $content);
75 });
76
77 updateFile("sql/test_data_second_domain.mysql", function ($content) use ($newVersion, $oldVersion) {
78 return str_replace($oldVersion, $newVersion, $content);
79 });
80
81 if ($doCommit) {
82 $files = "xml/version.xml sql/civicrm_generated.mysql sql/test_data_second_domain.mysql " . escapeshellarg($phpFile) . ' ' . escapeshellarg($sqlFile);
83 passthru("git add $files");
84 passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion"));
85 }
86
87 /* *********************************************************************** */
88 /* Helper functions */
89
90 /**
91 * Update the content of a file.
92 *
93 * @param string $file
94 * @param callable $callback
95 * Function(string $originalContent) => string $newContent.
96 */
97 function updateFile($file, $callback) {
98 if (!file_exists($file)) {
99 die("File does not exist: $file\n");
100 }
101 echo "Update \"$file\"\n";
102 $content = file_get_contents($file);
103 $content = $callback($content);
104 file_put_contents($file, $content);
105 }
106
107 /**
108 * Initialize a file (if it doesn't already exist).
109 * @param string $file
110 * @param callable $callback
111 * Function() => string $newContent.
112 */
113 function initFile($file, $callback) {
114 if (file_exists($file)) {
115 echo "File \"$file\" already exists.\n";
116 }
117 else {
118 echo "Initialize \"$file\"\n";
119 $content = $callback();
120 file_put_contents($file, $content);
121 }
122 return $file;
123 }
124
125 /**
126 * Render a pretty string for a major/minor version number.
127 *
128 * @param string $version
129 * Ex: '5.10.alpha1'
130 * @return string
131 * Ex: 'FiveTen'.
132 */
133 function makeVerName($version) {
134 list ($a, $b) = explode('.', $version);
135 require_once 'CRM/Utils/EnglishNumber.php';
136 return CRM_Utils_EnglishNumber::toCamelCase($a) . CRM_Utils_EnglishNumber::toCamelCase($b);
137 }
138
139 function isVersionValid($v) {
140 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
141 }
142
143 /**
144 * @param $error
145 */
146 function fatal($error) {
147 echo $error;
148 echo "usage: set-version.php <new-version> [--commit|--no-commit]\n";
149 echo " With --commit, any changes will be committed automatically the current git branch.\n";
150 echo " With --no-commit, any changes will be left uncommitted.\n";
151 exit(1);
152 }