farleyknight-ionize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/README.rdoc +59 -0
  2. data/Rakefile +5 -0
  3. data/bin/ionize +47 -0
  4. data/lib/ionize.rb +75 -0
  5. data/lib/ionize/environment.rb +56 -0
  6. data/lib/ionize/environment/application.rb +58 -0
  7. data/lib/ionize/environment/php_array.rb +95 -0
  8. data/lib/ionize/parser.rb +272 -0
  9. data/lib/ionize/tokenizer.rb +544 -0
  10. data/lib/ionize/translate.rb +34 -0
  11. data/lib/ionize/translate/composite_string_statements.rb +79 -0
  12. data/lib/ionize/translate/debug.rb +16 -0
  13. data/lib/ionize/translate/ext.rb +47 -0
  14. data/lib/ionize/translate/function_args.rb +132 -0
  15. data/lib/ionize/translate/if_statements.rb +42 -0
  16. data/lib/ionize/translate/multiple_statements.rb +22 -0
  17. data/lib/ionize/translate/php_to_ruby.rb +40 -0
  18. data/lib/ionize/translate/rails_for_php.rb +191 -0
  19. data/lib/ionize/translate/rewritable.rb +133 -0
  20. data/lib/ionize/translate/rewrites.rb +51 -0
  21. data/lib/ionize/translate/statements.rb +622 -0
  22. data/lib/ionize/translate/switch_case_statements.rb +52 -0
  23. data/lib/ionize/translate/term_statements.rb +76 -0
  24. data/lib/ionize/translate/translator.rb +52 -0
  25. data/lib/ionize/version.rb +9 -0
  26. data/spec/fixtures/array_lookup.php +10 -0
  27. data/spec/fixtures/boolean_operators.php +5 -0
  28. data/spec/fixtures/boolean_operators.rb +6 -0
  29. data/spec/fixtures/class_def.php +34 -0
  30. data/spec/fixtures/class_def.rb +34 -0
  31. data/spec/fixtures/dangling_else.php +8 -0
  32. data/spec/fixtures/dangling_else.rb +12 -0
  33. data/spec/fixtures/drupal_1.php +663 -0
  34. data/spec/fixtures/drupal_2.php +1152 -0
  35. data/spec/fixtures/empty_string.php +12 -0
  36. data/spec/fixtures/for_loop.php +17 -0
  37. data/spec/fixtures/for_loop2.php +13 -0
  38. data/spec/fixtures/for_loop3.php +16 -0
  39. data/spec/fixtures/for_loop3.rb +17 -0
  40. data/spec/fixtures/for_loop4.php +5 -0
  41. data/spec/fixtures/for_loop4.rb +6 -0
  42. data/spec/fixtures/foreach.php +9 -0
  43. data/spec/fixtures/foreach2.php +8 -0
  44. data/spec/fixtures/foreach3.php +7 -0
  45. data/spec/fixtures/foreach3.rb +7 -0
  46. data/spec/fixtures/fun_def.php +9 -0
  47. data/spec/fixtures/fun_def2.php +30 -0
  48. data/spec/fixtures/fun_def2.rb +30 -0
  49. data/spec/fixtures/fun_def3.php +33 -0
  50. data/spec/fixtures/fun_def4.php +43 -0
  51. data/spec/fixtures/fun_def4.rb +37 -0
  52. data/spec/fixtures/fun_def5.php +36 -0
  53. data/spec/fixtures/fun_with_if.php +6 -0
  54. data/spec/fixtures/fun_with_if.rb +6 -0
  55. data/spec/fixtures/fun_with_ifs.php +12 -0
  56. data/spec/fixtures/fun_with_ifs.rb +14 -0
  57. data/spec/fixtures/hello_world.php +6 -0
  58. data/spec/fixtures/heredoc.php +6 -0
  59. data/spec/fixtures/heredoc.rb +5 -0
  60. data/spec/fixtures/if.php +6 -0
  61. data/spec/fixtures/if.rb +7 -0
  62. data/spec/fixtures/if_boolean.php +5 -0
  63. data/spec/fixtures/if_boolean.rb +5 -0
  64. data/spec/fixtures/if_else.php +11 -0
  65. data/spec/fixtures/if_else1.php +17 -0
  66. data/spec/fixtures/if_else2.php +8 -0
  67. data/spec/fixtures/if_else3.php +15 -0
  68. data/spec/fixtures/if_else_nested.php +14 -0
  69. data/spec/fixtures/if_else_nested.rb +15 -0
  70. data/spec/fixtures/if_else_series.php +12 -0
  71. data/spec/fixtures/if_else_series.rb +12 -0
  72. data/spec/fixtures/if_not.php +5 -0
  73. data/spec/fixtures/if_not.rb +5 -0
  74. data/spec/fixtures/if_with_brackets.php +7 -0
  75. data/spec/fixtures/if_with_brackets.rb +7 -0
  76. data/spec/fixtures/long_if_else.php +10 -0
  77. data/spec/fixtures/long_if_else.rb +9 -0
  78. data/spec/fixtures/oo.php +16 -0
  79. data/spec/fixtures/php_nuke/sql_layer.php +527 -0
  80. data/spec/fixtures/postop.php +3 -0
  81. data/spec/fixtures/preop.php +7 -0
  82. data/spec/fixtures/simple_fun_def.php +4 -0
  83. data/spec/fixtures/switch_case.php +13 -0
  84. data/spec/fixtures/switch_case.rb +14 -0
  85. data/spec/fixtures/switch_case2.php +25 -0
  86. data/spec/fixtures/switch_case3.php +40 -0
  87. data/spec/fixtures/switch_case3.rb +42 -0
  88. data/spec/fixtures/switch_case4.php +56 -0
  89. data/spec/fixtures/switch_case5.php +71 -0
  90. data/spec/fixtures/switch_case_with_rescue_nil.php +43 -0
  91. data/spec/fixtures/switch_case_with_rescue_nil.rb +35 -0
  92. data/spec/fixtures/tertiary.php +3 -0
  93. data/spec/helper.rb +17 -0
  94. data/spec/php_environment_spec.rb +83 -0
  95. data/spec/php_parser_spec.rb +121 -0
  96. data/spec/php_translator_spec.rb +358 -0
  97. data/spec/rails_for_php_spec.rb +303 -0
  98. metadata +191 -0
@@ -0,0 +1,52 @@
1
+ module Ionize
2
+ module Php
3
+ module Translate
4
+ module SwitchCaseStatements
5
+ def handle_switch_case_statement(node)
6
+ condition = transform(node.third)
7
+ body = transform(node[5])
8
+
9
+ debug "Switch case condition #{condition.inspect}"
10
+ debug "Switch case body #{body.inspect}"
11
+ [:case, condition] + body
12
+ end
13
+
14
+ def handle_multi_case(node)
15
+ debug "Multi_case #{node.inspect}"
16
+
17
+ body = transform(node.first)
18
+
19
+ debug "Multi case body #{body.inspect}"
20
+
21
+ result = if body.composite_node?
22
+ # Example: [:case, [:true]] + [[:when, ... ]]
23
+ body + [send(:one_case, node[1..-1])]
24
+ else
25
+ # Example: [...] + [:when, ... ]
26
+ [body] + [send(:one_case, node[1..-1])]
27
+ end
28
+ debug "Result is #{result.inspect}"
29
+ result
30
+ end
31
+
32
+ def handle_default_case(node)
33
+ transform(node.first) + [transform(node[3]).to_block]
34
+ end
35
+
36
+ def handle_one_case(node)
37
+ case_body = if (result = transform(node[3])).nil?
38
+ nil
39
+ else
40
+ result.to_block
41
+ end
42
+
43
+ [:when, [:array, transform(node.second)], case_body]
44
+ end
45
+
46
+ def handle_line_statement(node)
47
+ transform(node.first)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,76 @@
1
+ module Ionize
2
+ module Php
3
+ module Translate
4
+ module TermStatements
5
+ def handle_term_error_guard(node)
6
+ [:rescue, transform(node.second), [:resbody, nil, [:nil]]]
7
+ end
8
+
9
+ def handle_term_assign(node)
10
+ Rewrites.new(self).handle_term_assign(node)
11
+ end
12
+
13
+ def handle_term_array_append(node)
14
+
15
+ end
16
+
17
+ def handle_term_array_assign(node)
18
+ term, lb, attr, rb, eq, expr = *node
19
+ [:attrasgn, transform(term), :[]=, [:array, transform(attr), transform(expr)]]
20
+ end
21
+
22
+ def handle_term_and_expr(node)
23
+ term, op, expr = *node
24
+ [:and, transform(term), transform(expr)]
25
+ end
26
+
27
+ def handle_term_and_expr_with_and(node)
28
+ term, op, expr = *node
29
+ [:and, transform(term), transform(expr)]
30
+ end
31
+
32
+ def handle_term_op_expr(node)
33
+ term, op, expr = *node
34
+ [:call, transform(term), transform(op), [:array, transform(expr)]]
35
+ end
36
+
37
+ def handle_term_or_expr(node)
38
+ term, op, expr = *node
39
+ [:or, transform(term), transform(expr)]
40
+ end
41
+
42
+ def handle_term_or_expr_with_or(node)
43
+ term, op, expr = *node
44
+ [:or, transform(term), transform(expr)]
45
+ end
46
+
47
+ def handle_term_not_equals_expr(node)
48
+ term, op, expr = *node
49
+ [:not, [:call, transform(term), :==, [:array, transform(expr)]]]
50
+ end
51
+
52
+ def handle_term_hashs_to_expr(node)
53
+ [:hash, transform(node.first), transform(node.third)]
54
+ end
55
+
56
+ def handle_reference_term(node)
57
+ # For now, I say screw references..
58
+ transform(node.rest)
59
+ # TODO: But we might change this in the future.. Who knows.
60
+ end
61
+
62
+ { "plus" => :+,
63
+ "minus" => :-,
64
+ "less_than" => :<,
65
+ "greater_than" => :>,
66
+ "concat" => :+,
67
+ }.each do |type, operator|
68
+ define_method "handle_term_#{type}_expr" do |node|
69
+ term, op, expr = *node
70
+ [:call, transform(term), operator, [:array, transform(expr)]]
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,52 @@
1
+
2
+ module Ionize
3
+ module Php
4
+ module Translate
5
+ class Translator
6
+ attr_accessor :parent
7
+
8
+ def initialize(parent)
9
+ @parent = parent
10
+ end
11
+
12
+ def transform(ast)
13
+ if ast.is_a? Array
14
+ if ast.length == 1
15
+ transform(ast.first)
16
+ else
17
+ ast.map {|node| transform(node) }.compact
18
+ end
19
+ elsif ast.is_a? Hash and ast.length == 1
20
+ debug "Ast is hash #{ast.inspect}"
21
+ first, second = *(ast.to_a).first
22
+ send(first, second)
23
+ end
24
+ end
25
+
26
+ def send(method, arg)
27
+ if methods.include? "handle_#{method}"
28
+ __send__("handle_#{method}", arg)
29
+ else
30
+ raise "Couldn't find handler for #{method.inspect} in class #{self.class.name} for argument #{arg.inspect}"
31
+ end
32
+ end
33
+
34
+ def handle_num(node)
35
+ [:lit, node.to_i]
36
+ end
37
+
38
+ def self.handle_node(*names, &block)
39
+ names.each do |name|
40
+ define_method("handle_#{name}", &block)
41
+ end
42
+ end
43
+
44
+ SimpleTransforms = ["single_string", "double_string", "fun_call", "a_string", "positive_number", "number"]
45
+
46
+ handle_node *SimpleTransforms do |node|
47
+ transform(node)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,9 @@
1
+ module Ionize #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 7
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+
2
+
3
+ // Keep in mind that the array lookup node has to come befor
4
+ // the variable node.. Otherwise it will not consider the brackets
5
+ // and then have a parse error
6
+
7
+ $_GET[1];
8
+
9
+
10
+ $_GET['content'];
@@ -0,0 +1,5 @@
1
+
2
+ $something = "value";
3
+
4
+ if ((($something == $something) && !($something == $something)) || ($something == $something))
5
+ print "wtf..?";
@@ -0,0 +1,6 @@
1
+
2
+ something = "value"
3
+
4
+ if ((something == something) and (not (something == something))) or (something == something)
5
+ print "wtf..?"
6
+ end
@@ -0,0 +1,34 @@
1
+
2
+ class ResultSet {
3
+ var $result;
4
+ var $total_rows;
5
+ var $fetched_rows;
6
+
7
+ function set_result( $res ) {
8
+ $this->result = $res;
9
+ }
10
+
11
+ function get_result() {
12
+ return $this->result;
13
+ }
14
+
15
+ function set_total_rows( $rows ) {
16
+ $this->total_rows = $rows;
17
+ }
18
+
19
+ function get_total_rows() {
20
+ return $this->total_rows;
21
+ }
22
+
23
+ function set_fetched_rows( $rows ) {
24
+ $this->fetched_rows = $rows;
25
+ }
26
+
27
+ function get_fetched_rows() {
28
+ return $this->fetched_rows;
29
+ }
30
+
31
+ function increment_fetched_rows() {
32
+ $this->fetched_rows = $this->fetched_rows + 1;
33
+ }
34
+ }
@@ -0,0 +1,34 @@
1
+
2
+ class ResultSet
3
+ attr_accessor :result
4
+ attr_accessor :total_rows
5
+ attr_accessor :fetched_rows
6
+
7
+ def set_result(res)
8
+ self.result = res
9
+ end
10
+
11
+ def get_result
12
+ return self.result
13
+ end
14
+
15
+ def set_total_rows(rows)
16
+ self.total_rows = rows
17
+ end
18
+
19
+ def get_total_rows
20
+ return self.total_rows
21
+ end
22
+
23
+ def set_fetched_rows(rows)
24
+ self.fetched_rows = rows
25
+ end
26
+
27
+ def get_fetched_rows
28
+ return self.fetched_rows
29
+ end
30
+
31
+ def increment_fetched_rows
32
+ self.fetched_rows = self.fetched_rows + 1
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+
2
+
3
+ $something = "value";
4
+ if ($something == "value")
5
+ if ($something == "value")
6
+ print "value";
7
+ else
8
+ print "not value";
@@ -0,0 +1,12 @@
1
+
2
+
3
+
4
+ something = "value"
5
+
6
+ if something == "value"
7
+ if something == "value"
8
+ print "value"
9
+ else
10
+ print "not value"
11
+ end
12
+ end
@@ -0,0 +1,663 @@
1
+ // $Id: update.php,v 1.252 2008/02/03 18:41:16 goba Exp $
2
+
3
+ /**
4
+ * @file
5
+ * Administrative page for handling updates from one Drupal version to another.
6
+ *
7
+ * Point your browser to "http://www.example.com/update.php" and follow the
8
+ * instructions.
9
+ *
10
+ * If you are not logged in as administrator, you will need to modify the access
11
+ * check statement inside your settings.php file. After finishing the upgrade,
12
+ * be sure to open settings.php again, and change it back to its original state!
13
+ */
14
+
15
+ /**
16
+ * Global flag to identify update.php run, and so avoid various unwanted
17
+ * operations, such as hook_init() and hook_exit() invokes, css/js preprocessing
18
+ * and translation, and solve some theming issues. This flag is checked on several
19
+ * places in Drupal code (not just update.php).
20
+ */
21
+ define('MAINTENANCE_MODE', 'update');
22
+
23
+ /**
24
+ * Add a column to a database using syntax appropriate for PostgreSQL.
25
+ * Save result of SQL commands in $ret array.
26
+ *
27
+ * Note: when you add a column with NOT NULL and you are not sure if there are
28
+ * already rows in the table, you MUST also add DEFAULT. Otherwise PostgreSQL
29
+ * won't work when the table is not empty, and db_add_column() will fail.
30
+ * To have an empty string as the default, you must use: 'default' => "''"
31
+ * in the $attributes array. If NOT NULL and DEFAULT are set the PostgreSQL
32
+ * version will set values of the added column in old rows to the
33
+ * DEFAULT value.
34
+ *
35
+ * @param $ret
36
+ * Array to which results will be added.
37
+ * @param $table
38
+ * Name of the table, without {}
39
+ * @param $column
40
+ * Name of the column
41
+ * @param $type
42
+ * Type of column
43
+ * @param $attributes
44
+ * Additional optional attributes. Recognized attributes:
45
+ * not null => TRUE|FALSE
46
+ * default => NULL|FALSE|value (the value must be enclosed in '' marks)
47
+ * @return
48
+ * nothing, but modifies $ret parameter.
49
+ */
50
+ function db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
51
+ if (array_key_exists('not null', $attributes) and $attributes['not null']) {
52
+ $not_null = 'NOT NULL';
53
+ }
54
+ if (array_key_exists('default', $attributes)) {
55
+ if (is_null($attributes['default'])) {
56
+ $default_val = 'NULL';
57
+ $default = 'default NULL';
58
+ }
59
+ elseif ($attributes['default'] === FALSE) {
60
+ $default = '';
61
+ }
62
+ else {
63
+ $default_val = "$attributes[default]";
64
+ $default = "default $attributes[default]";
65
+ }
66
+ }
67
+
68
+ $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column $type");
69
+ if (!empty($default)) {
70
+ $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET $default");
71
+ }
72
+ if (!empty($not_null)) {
73
+ if (!empty($default)) {
74
+ $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val");
75
+ }
76
+ $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Change a column definition using syntax appropriate for PostgreSQL.
82
+ * Save result of SQL commands in $ret array.
83
+ *
84
+ * Remember that changing a column definition involves adding a new column
85
+ * and dropping an old one. This means that any indices, primary keys and
86
+ * sequences from serial-type columns are dropped and might need to be
87
+ * recreated.
88
+ *
89
+ * @param $ret
90
+ * Array to which results will be added.
91
+ * @param $table
92
+ * Name of the table, without {}
93
+ * @param $column
94
+ * Name of the column to change
95
+ * @param $column_new
96
+ * New name for the column (set to the same as $column if you don't want to change the name)
97
+ * @param $type
98
+ * Type of column
99
+ * @param $attributes
100
+ * Additional optional attributes. Recognized attributes:
101
+ * not null => TRUE|FALSE
102
+ * default => NULL|FALSE|value (with or without '', it won't be added)
103
+ * @return
104
+ * nothing, but modifies $ret parameter.
105
+ */
106
+ function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
107
+ if (array_key_exists('not null', $attributes) and $attributes['not null']) {
108
+ $not_null = 'NOT NULL';
109
+ }
110
+ if (array_key_exists('default', $attributes)) {
111
+ if (is_null($attributes['default'])) {
112
+ $default_val = 'NULL';
113
+ $default = 'default NULL';
114
+ }
115
+ elseif ($attributes['default'] === FALSE) {
116
+ $default = '';
117
+ }
118
+ else {
119
+ $default_val = "$attributes[default]";
120
+ $default = "default $attributes[default]";
121
+ }
122
+ }
123
+
124
+ $ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
125
+ $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $type");
126
+ $ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
127
+ if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default"); }
128
+ if ($not_null) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL"); }
129
+ $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old");
130
+ }
131
+
132
+ /**
133
+ * Perform one update and store the results which will later be displayed on
134
+ * the finished page.
135
+ *
136
+ * An update function can force the current and all later updates for this
137
+ * module to abort by returning a $ret array with an element like:
138
+ * $ret['#abort'] = array('success' => FALSE, 'query' => 'What went wrong');
139
+ * The schema version will not be updated in this case, and all the
140
+ * aborted updates will continue to appear on update.php as updates that
141
+ * have not yet been run.
142
+ *
143
+ * @param $module
144
+ * The module whose update will be run.
145
+ * @param $number
146
+ * The update number to run.
147
+ * @param $context
148
+ * The batch context array
149
+ */
150
+ function update_do_one($module, $number, &$context) {
151
+ // If updates for this module have been aborted
152
+ // in a previous step, go no further.
153
+ if (!empty($context['results'][$module]['#abort'])) {
154
+ return;
155
+ }
156
+
157
+ $function = $module .'_update_'. $number;
158
+ if (function_exists($function)) {
159
+ $ret = $function($context['sandbox']);
160
+ }
161
+
162
+ if (isset($ret['#finished'])) {
163
+ $context['finished'] = $ret['#finished'];
164
+ unset($ret['#finished']);
165
+ }
166
+
167
+ if (!isset($context['results'][$module])) {
168
+ $context['results'][$module] = array();
169
+ }
170
+ if (!isset($context['results'][$module][$number])) {
171
+ $context['results'][$module][$number] = array();
172
+ }
173
+ $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret);
174
+
175
+ if (!empty($ret['#abort'])) {
176
+ $context['results'][$module]['#abort'] = TRUE;
177
+ }
178
+ // Record the schema update if it was completed successfully.
179
+ if ($context['finished'] == 1 && empty($context['results'][$module]['#abort'])) {
180
+ drupal_set_installed_schema_version($module, $number);
181
+ }
182
+
183
+ $context['message'] = 'Updating '. check_plain($module) .' module';
184
+ }
185
+
186
+ function update_selection_page() {
187
+ $output = '<p>The version of Drupal you are updating from has been automatically detected. You can select a different version, but you should not need to.</p>';
188
+ $output .= '<p>Click Update to start the update process.</p>';
189
+
190
+ drupal_set_title('Drupal database update');
191
+ $output .= drupal_get_form('update_script_selection_form');
192
+
193
+ update_task_list('select');
194
+
195
+ return $output;
196
+ }
197
+
198
+ function update_script_selection_form() {
199
+ $form = array();
200
+ $form['start'] = array(
201
+ '#tree' => TRUE,
202
+ '#type' => 'fieldset',
203
+ '#title' => 'Select versions',
204
+ '#collapsible' => TRUE,
205
+ '#collapsed' => TRUE,
206
+ );
207
+
208
+ // Ensure system.module's updates appear first
209
+ $form['start']['system'] = array();
210
+
211
+ $modules = drupal_get_installed_schema_version(NULL, FALSE, TRUE);
212
+ foreach ($modules as $module => $schema_version) {
213
+ $updates = drupal_get_schema_versions($module);
214
+ // Skip incompatible module updates completely, otherwise test schema versions.
215
+ if (!update_check_incompatibility($module) && $updates !== FALSE && $schema_version >= 0) {
216
+ // module_invoke returns NULL for nonexisting hooks, so if no updates
217
+ // are removed, it will == 0.
218
+ $last_removed = module_invoke($module, 'update_last_removed');
219
+ if ($schema_version < $last_removed) {
220
+ $form['start'][$module] = array(
221
+ '#value' => '<em>'. $module .'</em> module can not be updated. Its schema version is '. $schema_version .'. Updates up to and including '. $last_removed .' have been removed in this release. In order to update <em>'. $module .'</em> module, you will first <a href="http://drupal.org/upgrade">need to upgrade</a> to the last version in which these updates were available.',
222
+ '#prefix' => '<div class="warning">',
223
+ '#suffix' => '</div>',
224
+ );
225
+ $form['start']['#collapsed'] = FALSE;
226
+ continue;
227
+ }
228
+ $updates = drupal_map_assoc($updates);
229
+ $updates[] = 'No updates available';
230
+ $default = $schema_version;
231
+ foreach (array_keys($updates) as $update) {
232
+ if ($update > $schema_version) {
233
+ $default = $update;
234
+ break;
235
+ }
236
+ }
237
+ $form['start'][$module] = array(
238
+ '#type' => 'select',
239
+ '#title' => $module .' module',
240
+ '#default_value' => $default,
241
+ '#options' => $updates,
242
+ );
243
+ }
244
+ }
245
+
246
+ $form['has_js'] = array(
247
+ '#type' => 'hidden',
248
+ '#default_value' => FALSE,
249
+ '#attributes' => array('id' => 'edit-has_js'),
250
+ );
251
+ $form['submit'] = array(
252
+ '#type' => 'submit',
253
+ '#value' => 'Update',
254
+ );
255
+ return $form;
256
+ }
257
+
258
+ function update_batch() {
259
+ global $base_url;
260
+
261
+ $operations = array();
262
+ // Set the installed version so updates start at the correct place.
263
+ foreach ($_POST['start'] as $module => $version) {
264
+ drupal_set_installed_schema_version($module, $version - 1);
265
+ $updates = drupal_get_schema_versions($module);
266
+ $max_version = max($updates);
267
+ if ($version <= $max_version) {
268
+ foreach ($updates as $update) {
269
+ if ($update >= $version) {
270
+ $operations[] = array('update_do_one', array($module, $update));
271
+ }
272
+ }
273
+ }
274
+ }
275
+ $batch = array(
276
+ 'operations' => $operations,
277
+ 'title' => 'Updating',
278
+ 'init_message' => 'Starting updates',
279
+ 'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
280
+ 'finished' => 'update_finished',
281
+ );
282
+ batch_set($batch);
283
+ batch_process($base_url .'/update.php?op=results', $base_url .'/update.php');
284
+ }
285
+
286
+ function update_finished($success, $results, $operations) {
287
+ // clear the caches in case the data has been updated.
288
+ drupal_flush_all_caches();
289
+
290
+ $_SESSION['update_results'] = $results;
291
+ $_SESSION['update_success'] = $success;
292
+ $_SESSION['updates_remaining'] = $operations;
293
+ }
294
+
295
+ function update_results_page() {
296
+ drupal_set_title('Drupal database update');
297
+ // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
298
+ $links[] = '<a href="'. base_path() .'">Main page</a>';
299
+ $links[] = '<a href="'. base_path() .'?q=admin">Administration pages</a>';
300
+
301
+ update_task_list();
302
+ // Report end result
303
+ if (module_exists('dblog')) {
304
+ $log_message = ' All errors have been <a href="'. base_path() .'?q=admin/reports/dblog">logged</a>.';
305
+ }
306
+ else {
307
+ $log_message = ' All errors have been logged.';
308
+ }
309
+
310
+ if ($_SESSION['update_success']) {
311
+ $output = '<p>Updates were attempted. If you see no failures below, you may proceed happily to the <a href="'. base_path() .'?q=admin">administration pages</a>. Otherwise, you may need to update your database manually.'. $log_message .'</p>';
312
+ }
313
+ else {
314
+ list($module, $version) = array_pop(reset($_SESSION['updates_remaining']));
315
+ $output = '<p class="error">The update process was aborted prematurely while running <strong>update #'. $version .' in '. $module .'.module</strong>.'. $log_message;
316
+ if (module_exists('dblog')) {
317
+ $output .= ' You may need to check the <code>watchdog</code> database table manually.';
318
+ }
319
+ $output .= '</p>';
320
+ }
321
+
322
+ if (!empty($GLOBALS['update_free_access'])) {
323
+ $output .= "<p><strong>Reminder: don't forget to set the <code>\$update_free_access</code> value in your <code>settings.php</code> file back to <code>FALSE</code>.</strong></p>";
324
+ }
325
+
326
+ $output .= theme('item_list', $links);
327
+
328
+ // Output a list of queries executed
329
+ if (!empty($_SESSION['update_results'])) {
330
+ $output .= '<div id="update-results">';
331
+ $output .= '<h2>The following queries were executed</h2>';
332
+ foreach ($_SESSION['update_results'] as $module => $updates) {
333
+ $output .= '<h3>'. $module .' module</h3>';
334
+ foreach ($updates as $number => $queries) {
335
+ if ($number != '#abort') {
336
+ $output .= '<h4>Update #'. $number .'</h4>';
337
+ $output .= '<ul>';
338
+ foreach ($queries as $query) {
339
+ if ($query['success']) {
340
+ $output .= '<li class="success">'. $query['query'] .'</li>';
341
+ }
342
+ else {
343
+ $output .= '<li class="failure"><strong>Failed:</strong> '. $query['query'] .'</li>';
344
+ }
345
+ }
346
+ if (!count($queries)) {
347
+ $output .= '<li class="none">No queries</li>';
348
+ }
349
+ }
350
+ $output .= '</ul>';
351
+ }
352
+ }
353
+ $output .= '</div>';
354
+ }
355
+ unset($_SESSION['update_results']);
356
+ unset($_SESSION['update_success']);
357
+
358
+ return $output;
359
+ }
360
+
361
+ function update_info_page() {
362
+ // Change query-strings on css/js files to enforce reload for all users.
363
+ _drupal_flush_css_js();
364
+ // Flush the cache of all data for the update status module.
365
+ if (db_table_exists('cache_update')) {
366
+ cache_clear_all('*', 'cache_update', TRUE);
367
+ }
368
+
369
+ update_task_list('info');
370
+ drupal_set_title('Drupal database update');
371
+ $output = '<p>Use this utility to update your database whenever a new release of Drupal or a module is installed.</p><p>For more detailed information, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>';
372
+ $output .= "<ol>\n";
373
+ $output .= "<li><strong>Back up your database</strong>. This process will change your database values and in case of emergency you may need to revert to a backup.</li>\n";
374
+ $output .= "<li><strong>Back up your code</strong>. Hint: when backing up module code, do not leave that backup in the 'modules' or 'sites/*/modules' directories as this may confuse Drupal's auto-discovery mechanism.</li>\n";
375
+ $output .= '<li>Put your site into <a href="'. base_path() .'?q=admin/settings/site-maintenance">maintenance mode</a>.</li>'."\n";
376
+ $output .= "<li>Install your new files in the appropriate location, as described in the handbook.</li>\n";
377
+ $output .= "</ol>\n";
378
+ $output .= "<p>When you have performed the steps above, you may proceed.</p>\n";
379
+ $output .= '<form method="post" action="update.php?op=selection"><input type="submit" value="Continue" /></form>';
380
+ $output .= "\n";
381
+ return $output;
382
+ }
383
+
384
+ // Refactored the original to use several one-line strings instead of
385
+ // one huge-ass string.. Our parser cant handle big string yet :(
386
+ function update_access_denied_page() {
387
+ drupal_set_title('Access denied');
388
+ $output = '<p>Access denied. You are not authorized to access this page. Please log in as the admin user (the first user you created). If you cannot log in, you will have to edit <code>settings.php</code> to bypass this access check. To do this:</p>';
389
+ $output .= '<ol>';
390
+ $output .= '<li>With a text editor find the settings.php file on your system. From the main Drupal directory that you installed all the files into, go to <code>sites/your_site_name</code> if such directory exists, or else to <code>sites/default</code> which applies otherwise.</li>';
391
+ $output .= '<li>There is a line inside your settings.php file that says <code>$update_free_access = FALSE;</code>. Change it to <code>$update_free_access = TRUE;</code>.</li>';
392
+ $output .= '<li>As soon as the update.php script is done, you must change the settings.php file back to its original form with <code>$update_free_access = FALSE;</code>.</li>';
393
+ $output .= '<li>To avoid having this problem in future, remember to log in to your website as the admin user (the user you first created) before you backup your database at the beginning of the update process.</li>';
394
+ $output .= '</ol>';
395
+ return $output;
396
+ }
397
+
398
+ /**
399
+ * Create the batch table.
400
+ *
401
+ * This is part of the Drupal 5.x to 6.x migration.
402
+ */
403
+ function update_create_batch_table() {
404
+
405
+ // If batch table exists, update is not necessary
406
+ if (db_table_exists('batch')) {
407
+ return;
408
+ }
409
+
410
+ $schema['batch'] = array(
411
+ 'fields' => array(
412
+ 'bid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
413
+ 'token' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE),
414
+ 'timestamp' => array('type' => 'int', 'not null' => TRUE),
415
+ 'batch' => array('type' => 'text', 'not null' => FALSE, 'size' => 'big')
416
+ ),
417
+ 'primary key' => array('bid'),
418
+ 'indexes' => array('token' => array('token')),
419
+ );
420
+
421
+ $ret = array();
422
+ db_create_table($ret, 'batch', $schema['batch']);
423
+ return $ret;
424
+ }
425
+
426
+ /**
427
+ * Disable anything in the {system} table that is not compatible with the
428
+ * current version of Drupal core.
429
+ */
430
+ function update_fix_compatibility() {
431
+ $ret = array();
432
+ $incompatible = array();
433
+ $query = db_query("SELECT name, type, status FROM {system} WHERE status = 1 AND type IN ('module','theme')");
434
+ while ($result = db_fetch_object($query)) {
435
+ if (update_check_incompatibility($result->name, $result->type)) {
436
+ $incompatible[] = $result->name;
437
+ }
438
+ }
439
+ if (!empty($incompatible)) {
440
+ $ret[] = update_sql("UPDATE {system} SET status = 0 WHERE name IN ('". implode("','", $incompatible) ."')");
441
+ }
442
+ return $ret;
443
+ }
444
+
445
+ /**
446
+ * Helper function to test compatibility of a module or theme.
447
+ */
448
+ function update_check_incompatibility($name, $type = 'module') {
449
+ static $themes, $modules;
450
+
451
+ // Store values of expensive functions for future use.
452
+ if (empty($themes) || empty($modules)) {
453
+ $themes = system_theme_data();
454
+ $modules = module_rebuild_cache();
455
+ }
456
+
457
+ if ($type == 'module' && isset($modules[$name])) {
458
+ $file = $modules[$name];
459
+ }
460
+ else if ($type == 'theme' && isset($themes[$name])) {
461
+ $file = $themes[$name];
462
+ }
463
+ if (!isset($file)
464
+ || !isset($file->info['core'])
465
+ || $file->info['core'] != DRUPAL_CORE_COMPATIBILITY
466
+ || version_compare(phpversion(), $file->info['php']) < 0) {
467
+ return TRUE;
468
+ }
469
+ return FALSE;
470
+ }
471
+
472
+ /**
473
+ * Perform Drupal 5.x to 6.x updates that are required for update.php
474
+ * to function properly.
475
+ *
476
+ * This function runs when update.php is run the first time for 6.x,
477
+ * even before updates are selected or performed. It is important
478
+ * that if updates are not ultimately performed that no changes are
479
+ * made which make it impossible to continue using the prior version.
480
+ * Just adding columns is safe. However, renaming the
481
+ * system.description column to owner is not. Therefore, we add the
482
+ * system.owner column and leave it to system_update_6008() to copy
483
+ * the data from description and remove description. The same for
484
+ * renaming locales_target.locale to locales_target.language, which
485
+ * will be finished by locale_update_6002().
486
+ */
487
+ function update_fix_d6_requirements() {
488
+ $ret = array();
489
+
490
+ if (drupal_get_installed_schema_version('system') < 6000 && !variable_get('update_d6_requirements', FALSE)) {
491
+ $spec = array('type' => 'int', 'size' => 'small', 'default' => 0, 'not null' => TRUE);
492
+ db_add_field($ret, 'cache', 'serialized', $spec);
493
+ db_add_field($ret, 'cache_filter', 'serialized', $spec);
494
+ db_add_field($ret, 'cache_page', 'serialized', $spec);
495
+ db_add_field($ret, 'cache_menu', 'serialized', $spec);
496
+
497
+ db_add_field($ret, 'system', 'info', array('type' => 'text'));
498
+ db_add_field($ret, 'system', 'owner', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
499
+ if (db_table_exists('locales_target')) {
500
+ db_add_field($ret, 'locales_target', 'language', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''));
501
+ }
502
+ if (db_table_exists('locales_source')) {
503
+ db_add_field($ret, 'locales_source', 'textgroup', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'default'));
504
+ db_add_field($ret, 'locales_source', 'version', array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => 'none'));
505
+ }
506
+ variable_set('update_d6_requirements', TRUE);
507
+
508
+ // Create the cache_block table. See system_update_6027() for more details.
509
+ $schema['cache_block'] = array(
510
+ 'fields' => array(
511
+ 'cid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
512
+ 'data' => array('type' => 'blob', 'not null' => FALSE, 'size' => 'big'),
513
+ 'expire' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
514
+ 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
515
+ 'headers' => array('type' => 'text', 'not null' => FALSE),
516
+ 'serialized' => array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0)
517
+ ),
518
+ 'indexes' => array('expire' => array('expire')),
519
+ 'primary key' => array('cid'),
520
+ );
521
+ db_create_table($ret, 'cache_block', $schema['cache_block']);
522
+ }
523
+
524
+ return $ret;
525
+ }
526
+
527
+ /**
528
+ * Add the update task list to the current page.
529
+ */
530
+ function update_task_list($active = NULL) {
531
+ // Default list of tasks.
532
+ $tasks = array(
533
+ 'info' => 'Overview',
534
+ 'select' => 'Select updates',
535
+ 'run' => 'Run updates',
536
+ 'finished' => 'Review log',
537
+ );
538
+
539
+ drupal_set_content('left', theme('task_list', $tasks, $active));
540
+ }
541
+
542
+ /**
543
+ * Check update requirements and report any errors.
544
+ */
545
+ function update_check_requirements() {
546
+ // Check the system module requirements only.
547
+ $requirements = module_invoke('system', 'requirements', 'update');
548
+ $severity = drupal_requirements_severity($requirements);
549
+
550
+ // If there are issues, report them.
551
+ if ($severity != REQUIREMENT_OK) {
552
+ foreach ($requirements as $requirement) {
553
+ if (isset($requirement['severity']) && $requirement['severity'] != REQUIREMENT_OK) {
554
+ $message = isset($requirement['description']) ? $requirement['description'] : '';
555
+ if (isset($requirement['value']) && $requirement['value']) {
556
+ $message .= ' (Currently using '. $requirement['title'] .' '. $requirement['value'] .')';
557
+ }
558
+ drupal_set_message($message, 'warning');
559
+ }
560
+ }
561
+ }
562
+ }
563
+
564
+ // Some unavoidable errors happen because the database is not yet up-to-date.
565
+ // Our custom error handler is not yet installed, so we just suppress them.
566
+ ini_set('display_errors', FALSE);
567
+
568
+ require_once './includes/bootstrap.inc';
569
+
570
+ // We only load DRUPAL_BOOTSTRAP_CONFIGURATION for the update requirements
571
+ // check to avoid reaching the PHP memory limit.
572
+ $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
573
+ if (empty($op)) {
574
+ // Minimum load of components.
575
+ drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
576
+
577
+ require_once './includes/install.inc';
578
+ require_once './includes/file.inc';
579
+ require_once './modules/system/system.install';
580
+
581
+ // Load module basics.
582
+ include_once './includes/module.inc';
583
+ $module_list['system']['filename'] = 'modules/system/system.module';
584
+ $module_list['filter']['filename'] = 'modules/filter/filter.module';
585
+ module_list(TRUE, FALSE, FALSE, $module_list);
586
+ drupal_load('module', 'system');
587
+ drupal_load('module', 'filter');
588
+
589
+ // Set up $language, since the installer components require it.
590
+ drupal_init_language();
591
+
592
+ // Set up theme system for the maintenance page.
593
+ drupal_maintenance_theme();
594
+
595
+ // Check the update requirements for Drupal.
596
+ update_check_requirements();
597
+
598
+ // Display the warning messages (if any) in a dedicated maintenance page,
599
+ // or redirect to the update information page if no message.
600
+ $messages = drupal_set_message();
601
+ if (!empty($messages['warning'])) {
602
+ drupal_maintenance_theme();
603
+ print theme('update_page', '<form method="post" action="update.php?op=info"><input type="submit" value="Continue" /></form>', FALSE);
604
+ exit;
605
+ }
606
+ install_goto('update.php?op=info');
607
+ }
608
+
609
+ drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
610
+ drupal_maintenance_theme();
611
+
612
+ // This must happen *after* drupal_bootstrap(), since it calls
613
+ // variable_(get|set), which only works after a full bootstrap.
614
+ update_create_batch_table();
615
+
616
+ // Turn error reporting back on. From now on, only fatal errors (which are
617
+ // not passed through the error handler) will cause a message to be printed.
618
+ ini_set('display_errors', TRUE);
619
+
620
+ // Access check:
621
+ if (!empty($update_free_access) || $user->uid == 1) {
622
+
623
+ include_once './includes/install.inc';
624
+ include_once './includes/batch.inc';
625
+ drupal_load_updates();
626
+
627
+ update_fix_d6_requirements();
628
+ update_fix_compatibility();
629
+
630
+ $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
631
+ switch ($op) {
632
+ // update.php ops
633
+ case 'info':
634
+ $output = update_info_page();
635
+ break;
636
+
637
+ case 'selection':
638
+ $output = update_selection_page();
639
+ break;
640
+
641
+ case 'Update':
642
+ update_batch();
643
+ break;
644
+
645
+ case 'results':
646
+ $output = update_results_page();
647
+ break;
648
+
649
+ // Regular batch ops : defer to batch processing API
650
+ default:
651
+ update_task_list('run');
652
+ $output = _batch_page();
653
+ break;
654
+ }
655
+ }
656
+ else {
657
+ $output = update_access_denied_page();
658
+ }
659
+ if (isset($output) && $output) {
660
+ // We defer the display of messages until all updates are done.
661
+ $progress_page = ($batch = batch_get()) && isset($batch['running']);
662
+ print theme('update_page', $output, !$progress_page);
663
+ }