home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / htdocs / phpmyadmin / db_stats.php < prev    next >
PHP Script  |  2002-01-06  |  12KB  |  317 lines

  1. <?php
  2. /* $Id: db_stats.php,v 1.30 2001/11/23 19:13:07 loic1 Exp $ */
  3.  
  4. /** 
  5.  * Gets the variables sent to this script and send headers
  6.  */
  7. require('./libraries/grab_globals.lib.php');
  8. require('./header.inc.php');
  9.  
  10.  
  11. /**
  12.  * Drop databases if required
  13.  */
  14. if ((!empty($submit_mult) && isset($selected_db))
  15.     || isset($mult_btnDrop)) {
  16.     $err_url    = 'db_stats.php'
  17.                 . '?lang=' . $lang
  18.                 . '&server=' . $server;
  19.     $action     = 'db_stats.php';
  20.     $show_query = 'y';
  21.     include('./mult_submits.inc.php');
  22. }
  23.  
  24.  
  25. /**
  26.  * Sorts the databases array according to the user's choice
  27.  *
  28.  * @param   array    a record associated to a database
  29.  * @param   array    a record associated to a database
  30.  *
  31.  * @return  integer  a value representing whether $a should be before $b in the
  32.  *                   sorted array or not
  33.  *
  34.  * @global  mixed    the array to sort
  35.  * @global  mixed    'key' if the table has to be sorted by key, the column
  36.  *                   number to use to sort the array else
  37.  *
  38.  * @access  private
  39.  */
  40. function PMA_dbCmp($a, $b)
  41. {
  42.     global $dbs_array;
  43.     global $col;
  44.  
  45.     $is_asc = ($GLOBALS['sort_order'] == 'asc');
  46.  
  47.     // Sort by key (the db names) if required
  48.     if (!is_int($col) && $col == 'key') {
  49.         return (($is_asc) ? strcasecmp($a, $b) : -strcasecmp($a, $b));
  50.     }
  51.     // Sort by key (the db names) in ascending order if the columns' values are
  52.     // the same
  53.     else if ($dbs_array[$a][$col] == $dbs_array[$b][$col]) {
  54.         return strcasecmp($a, $b);
  55.     }
  56.     // Other cases
  57.     else {
  58.         $tmp = (($dbs_array[$a][$col] < $dbs_array[$b][$col]) ? -1 : 1);
  59.         return (($is_asc) ? $tmp : -$tmp);
  60.     }
  61. } // end of the 'PMA_dbCmp()' function
  62.  
  63.  
  64. /**
  65.  * Get the list and number of available databases.
  66.  * Skipped if no server selected: in this case no database should be displayed
  67.  * before the user choose among available ones at the welcome screen.
  68.  */
  69. if ($server > 0) {
  70.     // Get the valid databases list
  71.     $num_dbs = count($dblist);
  72.     $dbs     = @mysql_list_dbs() or PMA_mysqlDie('', 'mysql_list_dbs()', '', 'main.php?lang' . $lang . '&server=' . $server);
  73.     while ($a_db = mysql_fetch_object($dbs)) {
  74.         if (!$num_dbs) {
  75.             $dblist[]                     = $a_db->Database;
  76.         } else {
  77.             $true_dblist[$a_db->Database] = '';
  78.         }
  79.     } // end while
  80.     mysql_free_result($dbs);
  81.     if ($num_dbs && empty($true_dblist)) {
  82.         $dblist = array();
  83.     } else if ($num_dbs) {
  84.         for ($i = 0; $i < $num_dbs; $i++) {
  85.             if (isset($true_dblist[$dblist[$i]])) {
  86.                 $dblist_valid[] = $dblist[$i];
  87.             }
  88.         }
  89.         if (isset($dblist_valid)) {
  90.             $dblist = $dblist_valid;
  91.             unset($dblist_valid);
  92.         } else {
  93.             $dblist = array();
  94.         }
  95.         unset($true_dblist);
  96.     }
  97.     // Get the valid databases count
  98.     $num_dbs = count($dblist);
  99. } else {
  100.     $num_dbs = 0;
  101. }
  102.  
  103.  
  104.  
  105. /**
  106.  * Displays the page
  107.  */
  108. ?>
  109. <h1 align="center">
  110.     <?php echo ucfirst($strDatabasesStats); ?>
  111. </h1>
  112. <table align="center" border="<?php echo $cfgBorder; ?>" cellpadding="5">
  113. <tr>
  114.     <th align="<?php echo $cell_align_left; ?>"><big><?php echo $strHost . ' :'; ?></big></th>
  115.     <th align="<?php echo $cell_align_left; ?>"><big><?php echo $cfgServer['host']; ?></big></th>
  116. </tr>
  117. <tr>
  118.     <th align="<?php echo $cell_align_left; ?>"><big><?php echo $strGenTime . ' :'; ?></big></th>
  119.     <th align="<?php echo $cell_align_left; ?>"><big><?php echo PMA_localisedDate(); ?></big></th>
  120. </tr>
  121. </table>
  122. <br /><br />
  123.  
  124.  
  125. <?php
  126. /**
  127.  * At least one db -> do the work
  128.  */
  129. if ($num_dbs > 0) {
  130.     // Defines the urls used to sort the table
  131.     $common_url     = 'db_stats.php?lang=' . $lang . '&server=' . $server;
  132.     if (empty($sort_by)) {
  133.         $sort_by                 = 'db_name';
  134.         $sort_order              = 'asc';
  135.     }
  136.     else if (empty($sort_order)) {
  137.         $sort_order              = (($sort_by == 'db_name') ? 'asc' : 'desc');
  138.     }
  139.     $img_tag        = ' ' . "\n"
  140.                     . '            '
  141.                     . '<img src="./images/' . $sort_order . '_order.gif" border="0" width="7" height="7" alt="'
  142.                     . (($sort_order == 'asc') ? $strAscending : $strDescending) . '" />';
  143.     // Default order is ascending for db name, descending for sizes
  144.     for ($i = 0; $i < 5; $i++) {
  145.         $url_sort[$i]['order']   = (($i == 0) ? 'asc' : 'desc');
  146.         $url_sort[$i]['img_tag'] = '';
  147.     }
  148.     if ($sort_by == 'db_name') {
  149.         $url_sort[0]['order']    = (($sort_order == 'asc') ? 'desc' : 'asc');
  150.         $url_sort[0]['img_tag']  = $img_tag;
  151.         $col                     = 'key'; // used in 'PMA_dbCmp()'
  152.     } else if ($sort_by == 'tbl_cnt') {
  153.         $url_sort[1]['order']    = (($sort_order == 'asc') ? 'desc' : 'asc');
  154.         $url_sort[1]['img_tag']  = $img_tag;
  155.         $col                     = 0;
  156.     } else if ($sort_by == 'data_sz') {
  157.         $url_sort[2]['order']    = (($sort_order == 'asc') ? 'desc' : 'asc');
  158.         $url_sort[2]['img_tag']  = $img_tag;
  159.         $col                     = 1;
  160.     } else if ($sort_by == 'idx_sz') {
  161.         $url_sort[3]['order']    = (($sort_order == 'asc') ? 'desc' : 'asc');
  162.         $url_sort[3]['img_tag']  = $img_tag;
  163.         $col                     = 2;
  164.     } else {
  165.         $url_sort[4]['order']    = (($sort_order == 'asc') ? 'desc' : 'asc');
  166.         $url_sort[4]['img_tag']  = $img_tag;
  167.         $col                     = 3;
  168.     }
  169.     ?>
  170. <form action="db_stats.php">
  171.     <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
  172.     <input type="hidden" name="server" value="<?php echo $server; ?>" />
  173.  
  174.     <table align="center" border="<?php echo $cfgBorder; ?>">
  175.     <tr>
  176.         <th> </th>
  177.         <th>
  178.              
  179.             <a href="<?php echo $common_url . '&sort_by=db_name&sort_order=' . $url_sort[0]['order']; ?>">
  180.                 <?php echo ucfirst($strDatabase) . $url_sort[0]['img_tag']; ?></a> 
  181.         </th>
  182.         <th>
  183.              
  184.             <a href="<?php echo $common_url . '&sort_by=tbl_cnt&sort_order=' . $url_sort[1]['order']; ?>">
  185.                 <?php echo ucfirst(trim(sprintf($strTables, ''))) . $url_sort[1]['img_tag']; ?></a> 
  186.         </th>
  187.         <th>
  188.              
  189.             <a href="<?php echo $common_url . '&sort_by=data_sz&sort_order=' . $url_sort[2]['order']; ?>">
  190.                 <?php echo ucfirst($strData) . $url_sort[2]['img_tag']; ?></a> 
  191.         </th>
  192.         <th>
  193.              
  194.             <a href="<?php echo $common_url . '&sort_by=idx_sz&sort_order=' . $url_sort[3]['order']; ?>">
  195.                 <?php echo ucfirst($strIndexes) . $url_sort[3]['img_tag']; ?></a> 
  196.         </th>
  197.         <th>
  198.              
  199.             <a href="<?php echo $common_url . '&sort_by=tot_sz&sort_order=' . $url_sort[4]['order']; ?>">
  200.                 <?php echo ucfirst($strTotal) . $url_sort[4]['img_tag']; ?></a> 
  201.         </th>
  202.     </tr>
  203.     <?php
  204.     unset($url_sort);
  205.     echo "\n";
  206.     
  207.     $total_array[0] = 0;        // number of tables
  208.     $total_array[1] = 0;        // total data size
  209.     $total_array[2] = 0;        // total index size
  210.     $total_array[3] = 0;        // big total size
  211.  
  212.     // Gets the tables stats per database
  213.     for ($i = 0; $i < $num_dbs; $i++) {
  214.         $db         = $dblist[$i];
  215.         $tables     = @mysql_list_tables($db);
  216.  
  217.         // Number of tables
  218.         $dbs_array[$db][0] = @mysql_numrows($tables);
  219.         mysql_free_result($tables);
  220.         $total_array[0]    += $dbs_array[$db][0];
  221.  
  222.         // Size of data and indexes
  223.         $dbs_array[$db][1] = 0; // data size column
  224.         $dbs_array[$db][2] = 0; // index size column
  225.         $dbs_array[$db][3] = 0; // full size column
  226.     
  227.         $local_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
  228.         $result      = @mysql_query($local_query);
  229.         // needs the "@" below otherwise, warnings in case of special DB names
  230.         if (@mysql_num_rows($result)) {
  231.             while ($row = mysql_fetch_array($result)) {
  232.                 $dbs_array[$db][1] += $row['Data_length'];
  233.                 $dbs_array[$db][2] += $row['Index_length'];
  234.             } 
  235.             $dbs_array[$db][3]     = $dbs_array[$db][1] + $dbs_array[$db][2];
  236.             $total_array[1]        += $dbs_array[$db][1];
  237.             $total_array[2]        += $dbs_array[$db][2];
  238.             $total_array[3]        += $dbs_array[$db][3];
  239.         } // end if
  240.         mysql_free_result($result);
  241.     } // end for
  242.     mysql_close();
  243.  
  244.     // Sorts the dbs arrays
  245.     uksort($dbs_array, 'PMA_dbCmp');
  246.     reset($dbs_array);
  247.  
  248.     // Displays the tables stats per database
  249.     $i = 0;
  250.     while (list($db_name, $db_prop) = each($dbs_array)) {
  251.         $bgcolor = ($i % 2) ? $cfgBgcolorOne : $cfgBgcolorTwo;
  252.  
  253.         list($data_size, $data_unit) = PMA_formatByteDown($dbs_array[$db_name][1], 3, 1);
  254.         list($idx_size, $idx_unit)   = PMA_formatByteDown($dbs_array[$db_name][2], 3, 1);
  255.         list($tot_size, $tot_unit)   = PMA_formatByteDown($dbs_array[$db_name][3], 3, 1);
  256.  
  257.         echo '    <tr>' . "\n";
  258.         echo '        <td align="center" bgcolor="'. $bgcolor . '">' . "\n";
  259.         echo '             <input type="checkbox" name="selected_db[]" value="' . urlencode($db_name) . '" /> ' . "\n";
  260.         echo '        </td>' . "\n";
  261.         echo '        <td bgcolor="'. $bgcolor . '"> <a href="index.php?lang=' . $lang . '&server=' . $server . '&db=' . urlencode($db_name) . '" target="_parent">' . htmlentities($db_name) . '</a> </td>' . "\n";
  262.         echo '        <td align="right" bgcolor="'. $bgcolor . '"> ' . $dbs_array[$db_name][0] . ' </td>' . "\n";
  263.         echo '        <td align="right" bgcolor="'. $bgcolor . '"> ' . $data_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $data_unit . ' </td>' . "\n";
  264.         echo '        <td align="right" bgcolor="'. $bgcolor . '"> ' . $idx_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $idx_unit . ' </td>' . "\n";
  265.         echo '        <td align="right" bgcolor="'. $bgcolor . '"> <b>' . $tot_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $tot_unit . '</b> </td>' . "\n";
  266.         echo '    </tr>' . "\n";
  267.  
  268.         $i++;
  269.     } // end while
  270.     unset($dbs_array);
  271.  
  272.     // Displays the server stats
  273.     list($data_size, $data_unit) = PMA_formatByteDown($total_array[1], 3, 1);
  274.     list($idx_size, $idx_unit)   = PMA_formatByteDown($total_array[2], 3, 1);
  275.     list($tot_size, $tot_unit)   = PMA_formatByteDown($total_array[3], 3, 1);
  276.  
  277.     echo '    <tr>' . "\n";
  278.     echo '        <th> </th>' . "\n";
  279.     echo '        <th> ' . $strSum . ': ' . $num_dbs . '</th>' . "\n";
  280.     echo '        <th align="right"> ' . $total_array[0] . ' </th>' . "\n";
  281.     echo '        <th align="right"> ' . $data_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $data_unit . ' </th>' . "\n";
  282.     echo '        <th align="right"> ' . $idx_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $idx_unit . ' </th>' . "\n";
  283.     echo '        <th align="right"> <b>' . $tot_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $tot_unit . '</b> </th>' . "\n";
  284.     echo '    </tr>' . "\n\n";
  285.  
  286.     echo '    <tr>' . "\n";
  287.     echo '        <td colspan="6">' . "\n";
  288.     echo '            <img src="./images/arrow_' . $text_dir . '.gif" border="0" width="38" height="22" alt="' . $strWithChecked . '" />' . "\n";
  289.     echo '            <i>' . $strWithChecked . '</i>  <input type="submit" name="submit_mult" value="' . $strDrop . '" />' . "\n";
  290.     echo '        </td>' . "\n";
  291.     echo '    </tr>' . "\n";
  292.  
  293.     echo '    </table>' . "\n\n";
  294.  
  295.     echo '</form>' . "\n";
  296.  
  297.     unset($total_array);
  298. } // end if ($num_dbs > 0)
  299.  
  300.  
  301. /**
  302.  * No database case
  303.  */
  304. else {
  305.     ?>
  306. <p align="center"><big>  <?php echo $strNoDatabases; ?></big></p>
  307.     <?php
  308. } // end if ($num_dbs == 0)
  309. echo "\n";
  310.  
  311.  
  312. /**
  313.  * Displays the footer
  314.  */
  315. require('./footer.inc.php');
  316. ?>
  317.