lang
[phpmyadmin/crack.git] / db_stats.php3
blob292fbb554737a308a9909ad9d0b2b6a741f1b782
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets the variables sent to this script and send headers
7 */
8 $js_to_run = 'functions.js';
9 require('./libraries/grab_globals.lib.php3');
10 require('./header.inc.php3');
13 /**
14 * Ensures the current user is super-user
16 if (!@PMA_mysql_query('USE mysql', $userlink)) {
17 echo '<p><b>' . $strError . '</b></p>' . "\n";
18 echo '<p>&nbsp;&nbsp;&nbsp;&nbsp;' . $strNoRights . '</p>' . "\n";
19 include('./footer.inc.php3');
20 exit();
21 } // end if
24 /**
25 * Drop databases if required
27 if ((!empty($submit_mult) && isset($selected_db))
28 || isset($mult_btn)) {
29 $err_url = 'db_stats.php3?' . PMA_generate_common_url();
30 $action = 'db_stats.php3';
31 $show_query = '1';
32 include('./mult_submits.inc.php3');
36 /**
37 * Sorts the databases array according to the user's choice
39 * @param array a record associated to a database
40 * @param array a record associated to a database
42 * @return integer a value representing whether $a should be before $b in the
43 * sorted array or not
45 * @global mixed the array to sort
46 * @global mixed 'key' if the table has to be sorted by key, the column
47 * number to use to sort the array else
49 * @access private
51 function PMA_dbCmp($a, $b)
53 global $dbs_array;
54 global $col;
56 $is_asc = ($GLOBALS['sort_order'] == 'asc');
58 // Sort by key (the db names) if required
59 if (!is_int($col) && $col == 'key') {
60 return (($is_asc) ? strcasecmp($a, $b) : -strcasecmp($a, $b));
62 // Sort by key (the db names) in ascending order if the columns' values are
63 // the same
64 else if ($dbs_array[$a][$col] == $dbs_array[$b][$col]) {
65 return strcasecmp($a, $b);
67 // Other cases
68 else {
69 $tmp = (($dbs_array[$a][$col] < $dbs_array[$b][$col]) ? -1 : 1);
70 return (($is_asc) ? $tmp : -$tmp);
72 } // end of the 'PMA_dbCmp()' function
75 /**
76 * Get the list and number of available databases.
77 * Skipped if no server selected: in this case no database should be displayed
78 * before the user choose among available ones at the welcome screen.
80 if ($server > 0) {
81 // Get the valid databases list
82 $num_dbs = count($dblist);
83 $dbs = @mysql_list_dbs() or PMA_mysqlDie('', 'mysql_list_dbs()', '', 'main.php3?' . PMA_generate_common_url());
84 if ($dbs) {
85 while ($a_db = PMA_mysql_fetch_object($dbs)) {
86 if (!$num_dbs) {
87 $dblist[] = $a_db->Database;
88 } else {
89 $true_dblist[$a_db->Database] = '';
91 } // end while
92 mysql_free_result($dbs);
93 } // end if
94 if ($num_dbs && empty($true_dblist)) {
95 $dblist = array();
96 } else if ($num_dbs) {
97 for ($i = 0; $i < $num_dbs; $i++) {
98 if (isset($true_dblist[$dblist[$i]])) {
99 $dblist_valid[] = $dblist[$i];
102 if (isset($dblist_valid)) {
103 $dblist = $dblist_valid;
104 unset($dblist_valid);
105 } else {
106 $dblist = array();
108 unset($true_dblist);
110 // Get the valid databases count
111 $num_dbs = count($dblist);
112 } else {
113 $num_dbs = 0;
119 * Displays the page
122 <h1 align="center">
123 <?php echo $strDatabasesStats; ?>
124 </h1>
125 <table align="center" border="<?php echo $cfg['Border']; ?>" cellpadding="5">
126 <tr>
127 <th align="<?php echo $cell_align_left; ?>"><big><?php echo $strHost . ' :'; ?></big></th>
128 <th align="<?php echo $cell_align_left; ?>"><big><?php echo $cfg['Server']['host']; ?></big></th>
129 </tr>
130 <tr>
131 <th align="<?php echo $cell_align_left; ?>"><big><?php echo $strGenTime . ' :'; ?></big></th>
132 <th align="<?php echo $cell_align_left; ?>"><big><?php echo PMA_localisedDate(); ?></big></th>
133 </tr>
134 </table>
135 <br /><br />
138 <?php
140 * At least one db -> do the work
142 if ($num_dbs > 0) {
143 // Defines the urls used to sort the table
144 $common_url = 'db_stats.php3?' . PMA_generate_common_url();
145 if (empty($sort_by)) {
146 $sort_by = 'db_name';
147 $sort_order = 'asc';
149 else if (empty($sort_order)) {
150 $sort_order = (($sort_by == 'db_name') ? 'asc' : 'desc');
152 $img_tag = '&nbsp;' . "\n"
153 . ' '
154 . '<img src="./images/' . $sort_order . '_order.gif" border="0" width="7" height="7"'
155 . ' alt="' . (($sort_order == 'asc') ? $strAscending : $strDescending) . '"'
156 . ' title="' . (($sort_order == 'asc') ? $strAscending : $strDescending) . '" />';
157 // Default order is ascending for db name, descending for sizes
158 for ($i = 0; $i < 5; $i++) {
159 $url_sort[$i]['order'] = (($i == 0) ? 'asc' : 'desc');
160 $url_sort[$i]['img_tag'] = '';
162 if ($sort_by == 'db_name') {
163 $url_sort[0]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
164 $url_sort[0]['img_tag'] = $img_tag;
165 $col = 'key'; // used in 'PMA_dbCmp()'
166 } else if ($sort_by == 'tbl_cnt') {
167 $url_sort[1]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
168 $url_sort[1]['img_tag'] = $img_tag;
169 $col = 0;
170 } else if ($sort_by == 'data_sz') {
171 $url_sort[2]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
172 $url_sort[2]['img_tag'] = $img_tag;
173 $col = 1;
174 } else if ($sort_by == 'idx_sz') {
175 $url_sort[3]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
176 $url_sort[3]['img_tag'] = $img_tag;
177 $col = 2;
178 } else {
179 $url_sort[4]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
180 $url_sort[4]['img_tag'] = $img_tag;
181 $col = 3;
184 <form action="db_stats.php3" name="dbStatsForm">
185 <?php echo PMA_generate_common_hidden_inputs(); ?>
187 <table align="center" border="<?php echo $cfg['Border']; ?>">
188 <tr>
189 <th>&nbsp;</th>
190 <th>
191 &nbsp;
192 <a href="<?php echo $common_url . '&amp;sort_by=db_name&amp;sort_order=' . $url_sort[0]['order']; ?>">
193 <?php echo $strDatabase . $url_sort[0]['img_tag']; ?></a>&nbsp;
194 </th>
195 <th>
196 &nbsp;
197 <a href="<?php echo $common_url . '&amp;sort_by=tbl_cnt&amp;sort_order=' . $url_sort[1]['order']; ?>">
198 <?php echo $strNumTables . $url_sort[1]['img_tag']; ?></a>&nbsp;
199 </th>
200 <th>
201 &nbsp;
202 <a href="<?php echo $common_url . '&amp;sort_by=data_sz&amp;sort_order=' . $url_sort[2]['order']; ?>">
203 <?php echo $strData . $url_sort[2]['img_tag']; ?></a>&nbsp;
204 </th>
205 <th>
206 &nbsp;
207 <a href="<?php echo $common_url . '&amp;sort_by=idx_sz&amp;sort_order=' . $url_sort[3]['order']; ?>">
208 <?php echo $strIndexes . $url_sort[3]['img_tag']; ?></a>&nbsp;
209 </th>
210 <th>
211 &nbsp;
212 <a href="<?php echo $common_url . '&amp;sort_by=tot_sz&amp;sort_order=' . $url_sort[4]['order']; ?>">
213 <?php echo $strTotalUC . $url_sort[4]['img_tag']; ?></a>&nbsp;
214 </th>
215 </tr>
216 <?php
217 unset($url_sort);
218 echo "\n";
220 $total_array[0] = 0; // number of tables
221 $total_array[1] = 0; // total data size
222 $total_array[2] = 0; // total index size
223 $total_array[3] = 0; // big total size
225 // Gets the tables stats per database
226 for ($i = 0; $i < $num_dbs; $i++) {
227 $db = $dblist[$i];
228 $tables = @PMA_mysql_list_tables($db);
230 // Number of tables
231 if ($tables) {
232 $dbs_array[$db][0] = mysql_numrows($tables);
233 mysql_free_result($tables);
234 } else {
235 $dbs_array[$db][0] = 0;
237 $total_array[0] += $dbs_array[$db][0];
239 // Size of data and indexes
240 $dbs_array[$db][1] = 0; // data size column
241 $dbs_array[$db][2] = 0; // index size column
242 $dbs_array[$db][3] = 0; // full size column
244 if (PMA_MYSQL_INT_VERSION >= 32303) {
245 $local_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
246 $result = @PMA_mysql_query($local_query);
247 // needs the "@" below otherwise, warnings in case of special DB names
248 if ($result && @mysql_num_rows($result)) {
249 while ($row = PMA_mysql_fetch_array($result)) {
250 $dbs_array[$db][1] += $row['Data_length'];
251 $dbs_array[$db][2] += $row['Index_length'];
253 $dbs_array[$db][3] = $dbs_array[$db][1] + $dbs_array[$db][2];
254 $total_array[1] += $dbs_array[$db][1];
255 $total_array[2] += $dbs_array[$db][2];
256 $total_array[3] += $dbs_array[$db][3];
257 mysql_free_result($result);
258 } // end if
259 } // end if MySQL 3.23.03+
261 } // end for
262 mysql_close();
264 // Sorts the dbs arrays
265 uksort($dbs_array, 'PMA_dbCmp');
266 reset($dbs_array);
268 // Check/unchek all databases url
269 $checkall_url = 'db_stats.php3?'
270 . PMA_generate_common_url()
271 . (empty($sort_by) ? '' : '&amp;sort_by=' . $sort_by)
272 . (empty($sort_order) ? '' : '&amp;sort_order=' . $sort_order);
273 $do_check = (empty($checkall))
274 ? ''
275 : ' checked="checked"';
277 // Displays the tables stats per database
278 $i = 0;
279 while (list($db_name, $db_prop) = each($dbs_array)) {
280 $bgcolor = ($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
282 list($data_size, $data_unit) = PMA_formatByteDown($dbs_array[$db_name][1], 3, 1);
283 list($idx_size, $idx_unit) = PMA_formatByteDown($dbs_array[$db_name][2], 3, 1);
284 list($tot_size, $tot_unit) = PMA_formatByteDown($dbs_array[$db_name][3], 3, 1);
286 echo ' <tr>' . "\n";
287 echo ' <td align="center" bgcolor="'. $bgcolor . '">' . "\n";
288 echo ' &nbsp;<input type="checkbox" name="selected_db[]" value="' . urlencode($db_name) . '"' . $do_check . ' />&nbsp;' . "\n";
289 echo ' </td>' . "\n";
290 echo ' <td bgcolor="'. $bgcolor . '">&nbsp;<a href="index.php3?' . PMA_generate_common_url($db_name) . '" target="_parent">' . htmlspecialchars($db_name) . '</a>&nbsp;</td>' . "\n";
291 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;' . $dbs_array[$db_name][0] . '&nbsp;</td>' . "\n";
292 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;' . $data_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $data_unit . '&nbsp;</td>' . "\n";
293 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;' . $idx_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $idx_unit . '&nbsp;</td>' . "\n";
294 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;<b>' . $tot_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $tot_unit . '</b>&nbsp;</td>' . "\n";
295 echo ' </tr>' . "\n";
297 $i++;
298 } // end while
299 unset($dbs_array);
301 // Displays the server stats
302 list($data_size, $data_unit) = PMA_formatByteDown($total_array[1], 3, 1);
303 list($idx_size, $idx_unit) = PMA_formatByteDown($total_array[2], 3, 1);
304 list($tot_size, $tot_unit) = PMA_formatByteDown($total_array[3], 3, 1);
306 echo ' <tr>' . "\n";
307 echo ' <th>&nbsp;</th>' . "\n";
308 echo ' <th>&nbsp;' . $strSum . ':&nbsp;' . $num_dbs . '</th>' . "\n";
309 echo ' <th align="right">&nbsp;' . $total_array[0] . '&nbsp;</th>' . "\n";
310 echo ' <th align="right">&nbsp;' . $data_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $data_unit . '&nbsp;</th>' . "\n";
311 echo ' <th align="right">&nbsp;' . $idx_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $idx_unit . '&nbsp;</th>' . "\n";
312 echo ' <th align="right">&nbsp;<b>' . $tot_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $tot_unit . '</b>&nbsp;</th>' . "\n";
313 echo ' </tr>' . "\n\n";
315 echo ' <tr>' . "\n";
316 echo ' <td colspan="6">' . "\n";
317 echo ' <img src="./images/arrow_' . $text_dir . '.gif" border="0" width="38" height="22" alt="' . $strWithChecked . '" />' . "\n";
318 echo ' <a href="' . $checkall_url . '&amp;checkall=1" onclick="setCheckboxes(\'dbStatsForm\', true); return false;">' . "\n";
319 echo ' ' . $strCheckAll . '</a>' . "\n";
320 echo ' &nbsp;/&nbsp;' . "\n";
321 echo ' <a href="' . $checkall_url . '" onclick="setCheckboxes(\'dbStatsForm\', false); return false;">' . "\n";
322 echo ' ' . $strUncheckAll . '</a>' . "\n";
323 echo ' &nbsp;&nbsp;&nbsp;' . "\n";
324 echo ' <i>' . $strWithChecked . '</i>&nbsp;&nbsp;<input type="submit" name="submit_mult" value="' . $strDrop . '" />' . "\n";
325 echo ' </td>' . "\n";
326 echo ' </tr>' . "\n";
328 echo ' </table>' . "\n\n";
330 echo '</form>' . "\n";
332 unset($total_array);
333 } // end if ($num_dbs > 0)
337 * No database case
339 else {
341 <p align="center"><big>&nbsp;&nbsp;<?php echo $strNoDatabases; ?></big></p>
342 <?php
343 } // end if ($num_dbs == 0)
344 echo "\n";
348 * Displays the footer
350 require('./footer.inc.php3');