Update data/params after Bugzilla 4.2.11 upgrade
[WebKit-https.git] / Websites / perf.webkit.org / public / include / admin-header.php
1 <?php
2
3 require_once('db.php');
4 require_once('manifest.php');
5
6 ?><!DOCTYPE html>
7 <html>
8 <head>
9 <title>WebKit Perf Monitor</title>
10 <link rel="stylesheet" href="/common.css">
11 <link rel="stylesheet" href="/admin/admin.css">
12 </head>
13 <body>
14 <header id="title">
15 <h1><a href="/">WebKit Perf Monitor</a></h1>
16 <ul>
17     <li><a href="/admin/platforms">Platforms</a></li>
18     <li><a href="/admin/tests">Tests</a></li>
19     <li><a href="/admin/aggregators">Aggregators</a></li>
20     <li><a href="/admin/builders">Builders</a></li>
21     <li><a href="/admin/repositories">Repositories</a></li>
22     <li><a href="/admin/bug-trackers">Bug Trackers</a></li>
23 </ul>
24 </header>
25
26 <div id="mainContents">
27 <?php
28
29 function notice($message) {
30     echo "<p class='notice'>$message</p>";
31 }
32
33 $db = new Database;
34 if (!$db->connect()) {
35     notice('Failed to connect to the database');
36     $db = NULL;
37 } else
38     $action = array_key_exists('action', $_POST) ? $_POST['action'] : NULL;
39
40 function execute_query_and_expect_one_row_to_be_affected($query, $params, $success_message, $failure_message) {
41     global $db;
42
43     foreach ($params as &$param) {
44         if ($param == '')
45             $param = NULL;
46     }
47
48     $affected_rows = $db->query_and_get_affected_rows($query, $params);
49     if ($affected_rows) {
50         assert('$affected_rows == 1');
51         notice($success_message);
52         return true;
53     }
54
55     notice($failure_message);
56     return false;
57 }
58
59 function update_field($table, $prefix, $field_name) {
60     global $db;
61
62     if (!array_key_exists('id', $_POST) || (array_get($_POST, 'updated-column') != $field_name && !array_key_exists($field_name, $_POST)))
63         return FALSE;
64
65     $id = intval($_POST['id']);
66     $prefixed_field_name = $prefix . '_' . $field_name;
67     $id_field_name = $prefix . '_id';
68
69     execute_query_and_expect_one_row_to_be_affected("UPDATE $table SET $prefixed_field_name = \$2 WHERE $id_field_name = \$1",
70         array($id, array_get($_POST, $field_name)),
71         "Updated the $prefix $id",
72         "Could not update $prefix $id");
73
74     return TRUE;
75 }
76
77 function regenerate_manifest() {
78     global $db;
79
80     $generator = new ManifestGenerator($db);
81     if (!$generator->generate()) {
82         notice("Failed to generate the manifest (before trying to write into the filesystem).");
83         return FALSE;
84     }
85
86     if (!$generator->store()) {
87         notice("Failed to save the generated manifest into the filesystem");
88         return FALSE;
89     }
90
91     return TRUE;
92 }
93
94 class AdministrativePage {
95     private $table;
96     private $prefix;
97     private $column_to_be_ordered_by;
98     private $column_info;
99
100     function __construct($db, $table, $prefix, $column_info) {
101         $this->db = $db;
102         $this->table = $table;
103         $this->prefix = $prefix;
104         $this->column_info = $column_info;
105     }
106
107     private function name_to_titlecase($name) {
108         return ucwords(str_replace('_', ' ', $name));
109     }
110
111     private function column_label($name) {
112         return array_get($this->column_info[$name], 'label', $this->name_to_titlecase($name));
113     }
114
115     private function render_form_control_for_column($editing_mode, $name, $value = '', $show_update_button_if_needed = FALSE) {
116         $show_update_button = FALSE;
117         switch ($editing_mode) {
118         case 'text':
119             echo <<< END
120 <textarea name="$name" rows="7" cols="50">$value</textarea><br>
121 END;
122             $show_update_button = $show_update_button_if_needed;
123             break;
124         case 'boolean':
125             $checkedness = $this->db->is_true($value) ? ' checked' : '';
126             echo <<< END
127 <input type="checkbox" name="$name"$checkedness>
128 END;
129             $show_update_button = $show_update_button_if_needed;
130             break;
131         case 'url':
132             echo <<< END
133 <input type="text" name="$name" value="$value" size="70">
134 END;
135             break;
136         default:
137             assert($editing_mode == 'string');
138             echo <<< END
139 <input type="text" name="$name" value="$value">
140 END;
141         }
142
143         if ($show_update_button) {
144             echo <<< END
145
146 <button type="submit" name="action" value="update">Update</button>
147 END;
148         }
149     }
150
151     function render_table($column_to_be_ordered_by) {
152         $column_names = array_keys($this->column_info);
153         $column_to_subcolumn_names = array();
154         foreach ($column_names as $name) {
155             if (array_get($this->column_info[$name], 'pre_insertion'))
156                 continue;
157             $subcolumns = array_get($this->column_info[$name], 'subcolumns', array());
158             if (!$subcolumns || !array_get($this->column_info[$name], 'custom'))
159                 continue;
160             $column_to_subcolumn_names[$name] = $subcolumns;
161         }
162
163         $rowspan_if_needed = $column_to_subcolumn_names ? ' rowspan="2"' : '';
164
165         $headers = "<tr><td$rowspan_if_needed>ID</td>";
166         foreach ($column_names as $name) {
167             if (array_get($this->column_info[$name], 'pre_insertion'))
168                 continue;
169             $label = htmlspecialchars($this->column_label($name));
170             if (array_get($column_to_subcolumn_names, $name)) {
171                 $count = count($column_to_subcolumn_names[$name]);
172                 $headers .= "<td colspan=\"$count\">$label</td>";
173             } else
174                 $headers .= "<td$rowspan_if_needed>$label</td>";
175         }
176         $headers .= "</tr>\n";
177
178         if ($column_to_subcolumn_names) {
179             $headers .= '<tr>';
180             foreach ($column_names as $name) {
181                 $subcolumn_names = array_get($column_to_subcolumn_names, $name);
182                 if (!$subcolumn_names)
183                     continue;
184                 foreach ($subcolumn_names as $label)
185                     $headers .= '<td>' . htmlspecialchars($label) . '</td>';
186             }
187             $headers .= "</tr>\n";
188         }
189
190         echo <<< END
191 <table>
192 <thead>
193 $headers
194 </thead>
195 <tbody>
196
197 END;
198
199         assert(ctype_alnum_underscore($column_to_be_ordered_by));
200         $rows = $this->db->fetch_table($this->table, $this->prefix . '_' . $column_to_be_ordered_by);
201         if ($rows) {
202             foreach ($rows as $row) {
203                 $id = intval($row[$this->prefix . '_id']);
204
205                 $custom_cells_list = array();
206                 $maximum_rows = 1;
207                 foreach ($column_names as $name) {
208                     if (array_get($this->column_info[$name], 'pre_insertion'))
209                         continue;
210
211                     if ($custom = array_get($this->column_info[$name], 'custom')) {
212                         $custom_cells_list[$name] = $custom($row);
213                         $maximum_rows = max($maximum_rows, count($custom_cells_list[$name]));
214                     }
215                 }
216
217                 $rowspan_if_needed = $maximum_rows > 1 ? ' rowspan="' . $maximum_rows . '"' : '';
218
219                 echo "<tr>\n<td$rowspan_if_needed>$id</td>\n";
220                 foreach ($column_names as $name) {
221                     if (array_get($this->column_info[$name], 'pre_insertion'))
222                         continue;
223
224                     if (array_key_exists($name, $custom_cells_list)) {
225                         $this->render_custom_cells(array_get($column_to_subcolumn_names, $name), $custom_cells_list[$name], 0);
226                         continue;
227                     }
228
229                     $value = htmlspecialchars($row[$this->prefix . '_' . $name], ENT_QUOTES);
230                     $editing_mode = array_get($this->column_info[$name], 'editing_mode');
231                     if (!$editing_mode) {
232                         echo "<td$rowspan_if_needed>$value</td>\n";
233                         continue;
234                     }
235
236                     echo <<< END
237 <td$rowspan_if_needed>
238 <form method="POST">
239 <input type="hidden" name="id" value="$id">
240 <input type="hidden" name="action" value="update">
241 <input type="hidden" name="updated-column" value="$name">
242
243 END;
244                     $this->render_form_control_for_column($editing_mode, $name, $value, TRUE);
245                     echo "</form></td>\n";
246
247                 }
248                 echo "</tr>\n";
249
250                 for ($row = 1; $row < $maximum_rows; $row++) {
251                     echo "<tr>\n";
252                     foreach ($column_names as $name) {
253                         if (array_key_exists($name, $custom_cells_list))
254                             $this->render_custom_cells(array_get($column_to_subcolumn_names, $name), $custom_cells_list[$name], $row);
255                     }
256                     echo "</tr>\n";
257                 }
258             }
259         }
260         echo <<< END
261 </tbody>
262 </table>
263 END;
264     }
265
266     function render_custom_cells($subcolum_names, $rows, $row_index) {
267         $cells = array_get($rows, $row_index, array());
268         if (!is_array($cells))
269             $cells = array($cells);
270
271         if ($subcolum_names && count($cells) <= 1) {
272             $colspan = count($subcolum_names);
273             $content = $cells ? $cells[0] : '';
274             echo "<td colspan=\"$colspan\">$content</td>\n";
275             return;
276         }
277
278         for ($i = 0; $i < ($subcolum_names ? count($subcolum_names) : 1); $i++)
279             echo '<td>' . array_get($cells, $i, '') . '</td>';
280         echo "\n";
281     }
282
283     function render_form_to_add($title = NULL) {
284
285         if (!$title) # Can't use the table name since it needs to be singular.
286             $title = 'New ' . $this->name_to_titlecase($this->prefix);
287
288 echo <<< END
289 <section class="action-field">
290 <h2>$title</h2>
291 <form method="POST">
292
293 END;
294         foreach (array_keys($this->column_info) as $name) {
295             $editing_mode = array_get($this->column_info[$name], 'editing_mode');
296             if (array_get($this->column_info[$name], 'custom') || !$editing_mode)
297                 continue;
298
299             $label = htmlspecialchars($this->column_label($name));
300             echo "<label>$label<br>\n";
301             $this->render_form_control_for_column($editing_mode, $name);
302             echo "</label><br>\n";
303         }
304
305 echo <<< END
306
307 <button type="submit" name="action" value="add">Add</button>
308 </form>
309 </section>
310 END;
311
312     }
313
314 }
315
316 ?>