Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / libs / optimize_functions.php
index b4f4a742fb651b4c99c782c9f3f5f17995428c09..07b24ef8ab17bb308bc83dcd0441d7686e38732b 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 09/17/2004 *
- * ================                             Last change: 09/17/2004 *
+ * Mailer v0.2.1-FINAL                                Start: 09/17/2004 *
+ * ===================                          Last change: 09/17/2004 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : optimize_functions.php                           *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Optimize-Funktionen                              *
  * -------------------------------------------------------------------- *
- *                                                                      *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
+ * $Tag:: 0.2.1-FINAL                                                 $ *
+ * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
-       require($INC);
+if (!defined('__SECURITY')) {
+       die();
 }
-//
+
 // Part taken from admin optimize module of PHPNuke (http://www.phpnuke.org)
-//
-function REPAIR_OPTIMIZE_DB()
-{
-       global $MySQL;
+function repairOptimizeDatabase () {
        $ret = array();
-       $tot_data = 0; $tabs = 0; $opts = 0;
-       $tot_idx = 0; $total_gain = 0;
-       $tot_all = 0;
-       $result = @SQL_QUERY("SHOW TABLE STATUS FROM ".$MySQL['dbase'], __FILE__, __LINE__);
-       $tabs = @SQL_NUMROWS($result);
-       $ret['total_size'] = 0;
-       $ret['total_tabs'] = $tabs;
-       if ($tabs > 0)
-       {
-               while ($row = SQL_FETCHARRAY($result))
-               {
+       $tot_data = '0';
+       $opts = '0';
+       $tot_idx = '0';
+       $total_gain = '0';
+       $tot_all = '0';
+
+       // Get table status
+       $result = sqlQuery('SHOW TABLE STATUS FROM `{?__DB_NAME?}`', __FUNCTION__, __LINE__);
+
+       // Init array
+       $ret['total_size'] = '0';
+       $ret['total_tabs'] = sqlNumRows($result);
+       $ret['tables'] = array();
+
+       // Are there entries?
+       if (!ifSqlHasZeroNumRows($result)) {
+               // Fetch all rows
+               while ($row = sqlFetchArray($result)) {
                        $tot_data = $row['Data_length'];
                        $tot_idx  = $row['Index_length'];
                        $total = $tot_data + $tot_idx;
@@ -66,15 +72,17 @@ function REPAIR_OPTIMIZE_DB()
                        $gain = round ($gain, 3);
 
                        // Repair table
-                       $result1 = SQL_QUERY("REPAIR TABLE ".$row['Name'], __FILE__, __LINE__);
-                       $rep = SQL_FETCHARRAY($result1);
+                       $result1 = sqlQueryEscaped("REPAIR TABLE `%s`",
+                               array($row['Name']), __FUNCTION__, __LINE__);
+                       $rep = sqlFetchArray($result1);
 
                        // Optimize table
-                       $result1 = SQL_QUERY("OPTIMIZE TABLE ".$row['Name'], __FILE__, __LINE__);
-                       $opt = SQL_FETCHARRAY($result1);
+                       $result1 = sqlQueryEscaped("OPTIMIZE TABLE `%s`",
+                               array($row['Name']), __FUNCTION__, __LINE__);
+                       $opt = sqlFetchArray($result1);
 
                        // Add data to array
-                       $ret['tables'][] = array($row['Name'], $opt['Msg_text'], $rep['Msg_text'], $total, $gain);
+                       array_push($ret['tables'], array($row['Name'], $opt['Msg_text'], $rep['Msg_text'], $total, $gain));
 
                        // Count total table data
                        $ret['total_size'] += $total;
@@ -85,21 +93,19 @@ function REPAIR_OPTIMIZE_DB()
        $ret['total_gain'] = $total_gain;
 
        // Insert new row
-       $result = SQL_QUERY("INSERT INTO "._MYSQL_PREFIX."_optimize_gain (gain) VALUES ('".$total_gain."')", __FILE__, __LINE__);
+       $result = sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_optimize_gain` (`gain`) VALUES (%s)",
+               array($total_gain), __FUNCTION__, __LINE__);
 
        // Get total runs and total optimization count
-       $result = SQL_QUERY("SELECT COUNT(id) AS rows, SUM(gain) AS opti FROM "._MYSQL_PREFIX."_optimize_gain", __FILE__, __LINE__);
-       list($total_rows, $total_opti) = SQL_FETCHROW($result);
+       $result = sqlQuery('SELECT COUNT(`id`) AS `total_rows`, SUM(`gain`) AS `total_optimized` FROM `{?_MYSQL_PREFIX?}_optimize_gain`', __FUNCTION__, __LINE__);
+       $ret = merge_array($ret, sqlFetchArray($result));
 
        // Free memory
-       SQL_FREERESULT($result);
-
-       // Transfer data
-       $ret['total_rows'] = $total_rows;
-       $ret['total_opti'] = $total_opti;
+       sqlFreeResult($result);
 
        // Return array
        return $ret;
 }
-//
+
+// [EOF]
 ?>