New flakiness dashboard should hyperlink test names, WebKit revisions, and bubbles
[WebKit-https.git] / Websites / test-results / include / admin-header.php
1 <?php
2
3 require_once('db.php');
4
5 ?><!DOCTYPE html>
6 <html>
7 <head>
8 <title>WebKit Test Results</title>
9 <link rel="stylesheet" href="/common.css">
10 <link rel="stylesheet" href="/admin/admin.css">
11 </head>
12 <body>
13 <header id="title">
14 <h1><a href="/">WebKit Perf Monitor</a></h1>
15 <ul>
16     <li><a href="/admin/">Admin</a></li>
17     <li><a href="/admin/builders">Builders</a></li>
18     <li><a href="/admin/repositories">Repositories</a></li>
19 </ul>
20 </header>
21
22 <div id="mainContents">
23 <?php
24
25 function regenerate_manifest() {
26     // manifest.php doesn't need to be regenerated but WebKit Perf Monitor generates a static manifest.json.
27 }
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_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 ? $prefix . '_id' : '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, $_POST[$field_name]),
71         "Updated the $prefix $id",
72         "Could not update $prefix $id");
73
74     return TRUE;
75 }
76
77 class AdministrativePage {
78     private $table;
79     private $prefix;
80     private $column_to_be_ordered_by;
81     private $column_info;
82
83     function __construct($db, $table, $prefix, $column_info) {
84         $this->db = $db;
85         $this->table = $table;
86         $this->prefix = $prefix ? $prefix . '_' : '';
87         $this->column_info = $column_info;
88     }
89
90     private function name_to_titlecase($name) {
91         return ucwords(str_replace('_', ' ', $name));
92     }
93
94     private function column_label($name) {
95         return array_get($this->column_info[$name], 'label', $this->name_to_titlecase($name));
96     }
97
98     private function render_form_control_for_column($editing_mode, $name, $value = '', $show_update_button_if_needed = FALSE, $size = NULL) {
99         if ($editing_mode == 'text') {
100             echo <<< END
101 <textarea name="$name" rows="7" cols="50">$value</textarea><br>
102 END;
103             if ($show_update_button_if_needed) {
104                 echo <<< END
105
106 <button type="submit" name="action" value="update">Update</button>
107 END;
108             }
109             return;
110         }
111
112         if ($editing_mode == 'url') {
113             if (!$size)
114                 $size = 70;
115             echo <<< END
116 <input type="text" name="$name" value="$value" size="$size">
117 END;
118             return;
119         }
120
121         $sizeIfExits = $size ? " size=\"$size\"" : '';
122         echo <<< END
123 <input type="text" name="$name" value="$value"$sizeIfExits>
124 END;
125     }
126
127     function render_table($column_to_be_ordered_by) {
128         $column_names = array_keys($this->column_info);
129         $labels = array();
130         foreach ($column_names as $name) {
131             if (array_get($this->column_info[$name], 'pre_insertion'))
132                 continue;
133             array_push($labels, htmlspecialchars($this->column_label($name)));
134         }
135
136
137         $headers = join('</td><td>', $labels);
138         echo <<< END
139 <table>
140 <thead><tr><td>ID</td><td>$headers</td></tr></thead>
141 <tbody>
142
143 END;
144
145         assert(ctype_alnum_underscore($column_to_be_ordered_by));
146         $rows = $this->db->fetch_table($this->table, $this->prefix . $column_to_be_ordered_by);
147         if ($rows) {
148             foreach ($rows as $row) {
149                 $id = intval($row[$this->prefix . 'id']);
150                 echo "<tr>\n<td>$id</td>\n";
151                 foreach ($column_names as $name) {
152                     if (array_get($this->column_info[$name], 'pre_insertion'))
153                         continue;
154
155                     $custom = array_get($this->column_info[$name], 'custom');
156                     if ($custom) {
157                         echo "<td>";
158                         $custom($row);
159                         echo "</td>\n";
160                         continue;
161                     }
162
163                     $value = htmlspecialchars($row[$this->prefix . $name], ENT_QUOTES);
164                     $editing_mode = array_get($this->column_info[$name], 'editing_mode');
165                     if (!$editing_mode) {
166                         echo "<td>$value</td>\n";
167                         continue;
168                     }
169
170                     echo <<< END
171 <td>
172 <form method="POST">
173 <input type="hidden" name="id" value="$id">
174 <input type="hidden" name="action" value="update">
175
176 END;
177                     $size = array_get($this->column_info[$name], 'size');
178                     $this->render_form_control_for_column($editing_mode, $name, $value, TRUE, $size);
179                     echo "</form></td>\n";
180
181                 }
182                 echo "</tr>\n";
183             }
184         }
185         echo <<< END
186 </tbody>
187 </table>
188 END;
189     }
190
191     function render_form_to_add($title = NULL) {
192
193         if (!$title) # Can't use the table name since it needs to be singular.
194             $title = 'New ' . $this->name_to_titlecase($this->prefix);
195
196 echo <<< END
197 <section class="action-field">
198 <h2>$title</h2>
199 <form method="POST">
200
201 END;
202         foreach (array_keys($this->column_info) as $name) {
203             $editing_mode = array_get($this->column_info[$name], 'editing_mode');
204             if (array_get($this->column_info[$name], 'custom') || !$editing_mode)
205                 continue;
206
207             $label = htmlspecialchars($this->column_label($name));
208             echo "<label>$label<br>\n";
209             $this->render_form_control_for_column($editing_mode, $name);
210             echo "</label><br>\n";
211         }
212
213 echo <<< END
214
215 <button type="submit" name="action" value="add">Add</button>
216 </form>
217 </section>
218 END;
219
220     }
221
222 }
223
224 ?>