cleanhistory.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @version $Id: cleanhistory.php 20129 2013-02-04 16:53:59Z moyo $
00004  -------------------------------------------------------------------------
00005  GLPI - Gestionnaire Libre de Parc Informatique
00006  Copyright (C) 2003-2013 by the INDEPNET Development Team.
00007 
00008  http://indepnet.net/   http://glpi-project.org
00009  -------------------------------------------------------------------------
00010 
00011  LICENSE
00012 
00013  This file is part of GLPI.
00014 
00015  GLPI is free software; you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation; either version 2 of the License, or
00018  (at your option) any later version.
00019 
00020  GLPI is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with GLPI. If not, see <http://www.gnu.org/licenses/>.
00027  --------------------------------------------------------------------------
00028  */
00029 
00030 /** @file
00031 * @brief Purge history with some criterias
00032 */
00033 
00034 ini_set("memory_limit", "-1");
00035 ini_set("max_execution_time", "0");
00036 
00037 if ($argv) {
00038    for ($i=1 ; $i<$_SERVER['argc'] ; $i++) {
00039       $it = explode("=",$_SERVER['argv'][$i]);
00040       $it[0] = preg_replace('/^--/','',$it[0]);
00041       $_GET[$it[0]] = $it[1];
00042    }
00043 }
00044 
00045 include ('../inc/includes.php');
00046 
00047 $CFG_GLPI["debug"]=0;
00048 
00049 
00050 if (!isset($_GET['delay'])) {
00051    print "
00052 *******************************************
00053  This script kill babies : don t use it !!
00054 
00055    If you really want to try it:
00056    Do a full backup before use.
00057 *******************************************
00058 
00059 Usage : php cleanhistory.php [ --item=# ] [ --type=# ] [ --old=<regex> ] [ --new=<regex> ]
00060                              [ --run=1 ] [ --optimize=1 ] --delay=#
00061 
00062    With item a string value in  (optionnal):
00063       Computer                Software
00064       NetworkEquipment        SoftwareLicense
00065       Printer                 SoftwareVersion
00066       Monitor                 Ticket
00067       Peripheral
00068       Phone                   Others : see inc/*.class.php
00069    With type integer value in (optionnal):
00070       1 : Add device
00071       2 : Update device          11 : OCS Link
00072       3 : Delete device          12 : Other (often from plugin)
00073       4 : Install software       13 : Delete item (put in dustbin)
00074       5 : Uninstall software     14 : Restore item from dustbin
00075       6 : Disconnect device      15 : Add relation
00076       7 : Connect device         16 : Delete relation
00077       8 : OCS Import             17 : Add sub item
00078       9 : OCS Delete             18 : Update sub item
00079      10 : OCS ID Changed         19 : Delete sub item
00080    With old an optional regex pattern on old_value
00081    With new an optional regex pattern on new_value
00082    With delay in month (mandatory).\n\n";
00083    die();
00084 }
00085 
00086 $table = 'glpi_logs';
00087 echo "    Total entries in history : ".countElementsInTable($table)."\n";
00088 
00089 $where = "`date_mod` < SUBDATE(NOW(), INTERVAL ".$_GET['delay']." month)";
00090 
00091 if (isset($_GET['item'])) {
00092    $where .= " AND `itemtype` = '".$_GET['item']."'";
00093 }
00094 
00095 if (isset($_GET['type'])) {
00096    $where .= " AND `linked_action` = ".intval($_GET['type']);
00097 }
00098 
00099 if (isset($_GET['old'])) {
00100    $where .= " AND `old_value` REGEXP '".$_GET['old']."'";
00101 }
00102 
00103 if (isset($_GET['new'])) {
00104    $where .= " AND `new_value` REGEXP '".$_GET['new']."'";
00105 }
00106 
00107 if (isset($_GET['run'])) {
00108    $query = "DELETE QUICK
00109              FROM `$table`
00110              WHERE $where";
00111    $res = $DB->query($query);
00112 
00113    if (!$res) {
00114       die("SQL request: $query\nSQL error : ".$DB->error()."\n");
00115    }
00116 
00117    echo "  Deleted entries in history : ".$DB->affected_rows()."\n";
00118    echo "Remaining entries in history : ".countElementsInTable($table)."\n";
00119 
00120    if (isset($_GET['optimize'])) {
00121       foreach ($DB->request("OPTIMIZE TABLE `$table`") as $data) {
00122          echo "Table Optimization for ".$data['Table'].": ".$data['Msg_type']." = ".
00123                $data['Msg_text']."\n";
00124       }
00125    }
00126 
00127 } else {
00128    echo " Selected entries in history : ".countElementsInTable($table, $where)."\n";
00129 }
00130 
00131 ?>