<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Académie de Bordeaux)

This file is part of Gibii.

Gibii is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Gibii is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Gibii; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

****************************************************************************/

//Fonctions utilisées uniquement par le Sadmin
function optimize($show=0) {
global $db;

if ($show==1) print "<table border=1 align=\"center\"><tr class='rubrique'><td><div align=center>Table</div></td><td><div align=center>Taille</div></td><td><div align=center>Statut</div></td><td><div align=center>Espace récupéré</div></td></tr>";
$tot_data = 0;
$tot_idx = 0;
$total_gain = 0;
$histo=0;
$cpt=0;

setlocale(LC_NUMERIC,"");

$asql = 'SHOW TABLE STATUS FROM '.$db;
$result = query($asql);
if (@mysql_num_rows($result)) {
 while ($row = @mysql_fetch_array($result)) {
    $tot_data = $row['Data_length'];
    $tot_idx  = $row['Index_length'];
    $total = $tot_data + $tot_idx;
    $total = $total / 1024 ;
    $total = round ($total,1);
    $gain= $row['Data_free'];
    $gain = $gain / 1024 ;
    $total_gain += $gain;
    $gain=round($gain,2);
    $local_query = 'OPTIMIZE TABLE '.$row[0];
    $resultat  = query($local_query);
    if ($show==1) {
     if ($gain == 0) {
                  print "<tr class='sadmin_paire'><td>"."$row[0]"."</td>"."<td>"."$total"." Ko"."</td>"."<td>Déjà optimisée</td><td>0 Ko</td></tr>";
             } else {
               print "<tr  class='sadmin_impaire'><td><b>"."$row[0]"."</b></td>"."<td><b>"."$total"." Ko"."</b></td>"."<td><b>Optimisée</b></td><td><b>"."$gain"." Ko</b></td></tr>";
              }
     } //fin $show=1
        }   //fin while

    }
    if ($show==1) {
     print "</table>";
     print "</center>";
     print "<br>";
     $total_fr = round ($total_gain,2);
     print "<center><b>Résultat de l'optimisation</b><br><br>Espace total récupéré :  $total_fr Ko<br>";
    }

    //Stockage historique optimisations
    //Vérifier table _optimize
    $sql_query = "CREATE TABLE IF NOT EXISTS _optimize(gain decimal(10,1))";
    $result = @mysql_query($sql_query);
    //$sql_query= "ALTER TABLE `_optimize` CHANGE `gain` `gain` DECIMAL( 10, 1 ) DEFAULT NULL";
    //$result = @mysql_query($sql_query);
    setlocale(LC_NUMERIC,"us");
    $sql_query = "INSERT INTO _optimize SET gain=$total_fr";
    $result = @mysql_query($sql_query);
    setlocale(LC_NUMERIC,"");
    //On va relire l'historique
    $sql_query = "SELECT * FROM _optimize";
    $result = @mysql_query ($sql_query);
    while ($row = @mysql_fetch_row($result)) {
        $histo += $row[0];
        $cpt += 1;
    }

    if ($show=+1) {
    print "Vous avez effectué l'optimisation $cpt fois<br>"
        ."et récupéré $histo Ko depuis la première fois !</center>";
    print "</table>";
    }

}

?>