Fix cluster action on the admin page
authorGuillaume (ioguix) de Rorthais <ioguix@free.fr>
Sun, 11 Apr 2010 21:56:58 +0000 (23:56 +0200)
committerGuillaume (ioguix) de Rorthais <ioguix@free.fr>
Sun, 11 Apr 2010 21:56:58 +0000 (23:56 +0200)
admin.php
classes/database/Postgres.php
classes/database/Postgres82.php
indexes.php
lang/english.php
lang/recoded/english.php
tables.php

index 60f546f5df392f09cf8708261bf92df254087830..dcd94954070ded25ab10b839ef351a4048929035 100644 (file)
--- a/admin.php
+++ b/admin.php
@@ -2,6 +2,89 @@
 
        $script = ''; // init global value script
        
+       /**
+        * Show confirmation of cluster and perform cluster
+        */
+       function doCluster($type, $confirm=false) {
+               global $script, $data, $misc, $lang;
+
+               if (($type == 'table') && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
+                       doDefault($lang['strspecifytabletocluster']);
+                       return;
+               }
+
+               if ($confirm) {
+                       if (isset($_REQUEST['ma'])) {
+                               $misc->printTrail('schema');
+                               $misc->printTitle($lang['strclusterindex'], 'pg.cluster');
+
+                               echo "<form action=\"{$script}\" method=\"post\">\n";
+                               foreach($_REQUEST['ma'] as $v) {
+                                       $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
+                                       echo "<p>", sprintf($lang['strconfclustertable'], $misc->printVal($a['table'])), "</p>\n";
+                                       echo "<input type=\"hidden\" name=\"table[]\" value=\"", htmlspecialchars($a['table']), "\" />\n";
+                               }
+                       } // END if multi cluster
+                       else {
+                               $misc->printTrail($type);
+                               $misc->printTitle($lang['strclusterindex'], 'pg.cluster');
+                               
+                               echo "<form action=\"{$script}\" method=\"post\">\n";
+                               
+                               if ($type == 'table') {
+                                       echo "<p>", sprintf($lang['strconfclustertable'], $misc->printVal($_REQUEST['object'])), "</p>\n";
+                                       echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['object']), "\" />\n";
+                               }
+                               else {
+                                       echo "<p>", sprintf($lang['strconfclusterdatabase'], $misc->printVal($_REQUEST['object'])), "</p>\n";
+                                       echo "<input type=\"hidden\" name=\"table\" value=\"\" />\n";
+                               }
+                       }
+                       echo "<input type=\"hidden\" name=\"action\" value=\"cluster\" />\n";
+                       
+                       echo $misc->form;
+
+                       echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strcluster']}\" />\n"; //TODO
+                       echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
+                       echo "</form>\n";
+               } // END single cluster
+               else {
+                       //If multi table cluster
+                       if ($type == 'table') { // cluster one or more table
+                               if (is_array($_REQUEST['table'])) {
+                                       $msg='';
+                                       foreach($_REQUEST['table'] as $o) {
+                                               $status = $data->clusterIndex($o);
+                                               if ($status == 0)
+                                                       $msg.= sprintf('%s: %s<br />', htmlentities($o), $lang['strclusteredgood']);
+                                               else {
+                                                       doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o), $lang['strclusteredbad']));
+                                                       return;
+                                               }
+                                       }
+                                        // Everything went fine, back to the Default page....
+                                        doDefault($msg);
+                               }
+                               else {
+                                       $status = $data->clusterIndex($_REQUEST['object']);
+                                       if ($status == 0) {
+                                               doAdmin($type, $lang['strclusteredgood']);
+                                       }
+                                       else
+                                               doAdmin($type, $lang['strclusteredbad']);
+                               }
+                       }
+                       else { // Cluster all tables in database
+                               $status = $data->clusterIndex();
+                               if ($status == 0) {
+                                       doAdmin($type, $lang['strclusteredgood']);
+                               }
+                               else
+                                       doAdmin($type, $lang['strclusteredbad']);
+                       }
+               }
+       }
+       
        /**
         * Show confirmation of reindex and perform reindex
         */
                echo "</form>\n";
                echo "</td>\n";
                
-               // Recluster
+               // Cluster
                if ($data->hasRecluster()){
                        echo "<td class=\"data1\" style=\"text-align: center; vertical-align: bottom\">\n";
                        echo "<form action=\"{$script}\" method=\"post\">\n";
-                       echo "<p><input type=\"hidden\" name=\"action\" value=\"recluster\" />\n";
                        echo $misc->form;
                        if ($type == 'table') {
-                               echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($table), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['object']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
+                               if ($data->alreadyClustered($_REQUEST['object'])) {
+                                       $disabled = '';
+                               }
+                               else {
+                                       $disabled = 'disabled="disabled" ';
+                                       echo "{$lang['strnoclusteravailable']}<br />";
+                               }
                        }
-                       echo "<input type=\"submit\" value=\"{$lang['strclusterindex']}\" /></p>\n";
+                       echo "<p><input type=\"hidden\" name=\"action\" value=\"confirm_cluster\" />\n";
+                       echo "<input type=\"submit\" value=\"{$lang['strclusterindex']}\" $disabled/></p>\n";
                        echo "</form>\n";
                        echo "</td>\n";
                }
                }
 
                switch ($action) {
-                       //case 'confirm_recluster':
+                       case 'confirm_cluster':
+                               doCluster($type, true);
+                               break;
                        case 'confirm_reindex':
                                doReindex($type, true);
                                break;
                        case 'confirm_vacuum':
                                doVacuum($type, true);
                                break;
-                       //case 'recluster':
+                       case 'cluster':
+                               if (isset($_POST['cluster'])) doCluster($type);
+                               // if multi-action from table canceled: back to the schema default page
+                               else if (($type == 'table') && is_array($_REQUEST['object']) ) doDefault();
+                               else doAdmin($type);
+                               break;
                        case 'reindex':
                                if (isset($_POST['reindex'])) doReindex($type);
                                // if multi-action from table canceled: back to the schema default page
index 817bc51172577721b040b412ab8eadd684114b96..72ac39bff1f16a21b7f081f6c07677528ff3fa6f 100755 (executable)
@@ -3253,6 +3253,34 @@ class Postgres extends ADODB_base {
                return $this->selectSet($sql);
        }
 
+       /** 
+        * test if a table has been clustered on an index
+        * @param $table The table to test
+        * 
+        * @return true if the table has been already clustered
+        */
+       function alreadyClustered($table) {
+               
+               $c_schema = $this->_schema;
+               $this->clean($c_schema);
+               $this->clean($table);
+
+               $sql = "SELECT i.indisclustered
+                       FROM pg_catalog.pg_class c, pg_catalog.pg_index i
+                       WHERE c.relname = '{$table}'
+                               AND c.oid = i.indrelid AND i.indisclustered
+                               AND c.relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
+                                       WHERE nspname='{$c_schema}')
+                               ";
+               
+               $v = $this->selectSet($sql);
+               
+               if ($v->recordCount() == 0)
+                       return false;
+                       
+               return true;
+       }
+       
        /**
         * Creates an index
         * @param $name The index name
@@ -3348,16 +3376,24 @@ class Postgres extends ADODB_base {
         * @param $table The table the index is on
         * @return 0 success
         */
-       function clusterIndex($index, $table) {
-
-               $f_schema = $this->_schema;
-               $this->fieldClean($f_schema);
-               $this->fieldClean($index);
-               $this->fieldClean($table);
+       function clusterIndex($table='', $index='') {
+               
+               $sql = 'CLUSTER';
                
                // We don't bother with a transaction here, as there's no point rolling
                // back an expensive cluster if a cheap analyze fails for whatever reason
-               $sql = "CLUSTER \"{$f_schema}\".\"{$table}\" USING \"{$index}\"";
+               
+               if (!empty($table)) {
+                       $f_schema = $this->_schema;
+                       $this->fieldClean($f_schema);
+                       $this->fieldClean($table);
+                       $sql .= " \"{$f_schema}\".\"{$table}\"";
+                       
+                       if (!empty($index)) {
+                               $this->fieldClean($index);
+                               $sql .= " USING \"{$index}\"";
+                       }
+               }
 
                return $this->execute($sql);
        }
index 346eace61273d4ba9cf37a301d8256dc3335b01d..a014f8be493c09790f86580699ac4235daf07b92 100644 (file)
@@ -256,15 +256,26 @@ class Postgres82 extends Postgres83 {
         * @param $table The table the index is on
         * @return 0 success
         */
-       function clusterIndex($index, $table) {
-               $f_schema = $this->_schema;
-               $this->fieldClean($f_schema);
-               $this->fieldClean($index);
-               $this->fieldClean($table);
+       function clusterIndex($table='', $index='') {
 
+               $sql = 'CLUSTER';
+               
                // We don't bother with a transaction here, as there's no point rolling
                // back an expensive cluster if a cheap analyze fails for whatever reason
-               $sql = "CLUSTER \"{$index}\" ON \"{$f_schema}\".\"{$table}\"";
+               
+               if (!empty($table)) {
+                       $f_schema = $this->_schema;
+                       $this->fieldClean($f_schema);
+                       $this->fieldClean($table);
+                       
+                       if (!empty($index)) {
+                               $this->fieldClean($index);
+                               $sql .= " \"{$index}\" ON \"{$f_schema}\".\"{$table}\"";
+                       }
+                       else {
+                               $sql .= " \"{$f_schema}\".\"{$table}\"";
+                       }
+               }
 
                return $this->execute($sql);
        }
index 0d57d39eacc56bf695652eec9de8d52ad583c526..23682a2122193e256bf218edf1c9b41d0bb1bb8e 100644 (file)
@@ -39,7 +39,7 @@
                        echo "</form>\n";
                }
                else {
-                       $status = $data->clusterIndex($_POST['index'], $_POST['table']);
+                       $status = $data->clusterIndex($_POST['table'], $_POST['index']);
                        if ($status == 0)
                                if (isset($_POST['analyze'])){
                                        $status = $data->analyzeDB($_POST['table']);
index 76b268ad2ab1a2eb40ef4c4678864bb5a8cf62fb..2683e48c6141a6b8d749f183a6baa7dbcce7eab8 100644 (file)
        $lang['strconfdelhistory'] = 'Really remove this request from history?';
        $lang['strconfclearhistory'] = 'Really clear history?';
        $lang['strnodatabaseselected'] = 'Please, select a database.';
-       
-       // Database
-       $lang['strconfanalyzedatabase'] = 'Are you sure you want to analyze all tables in database "%s"?';
-       $lang['strconfvacuumdatabase'] = 'Are you sure you want to vacuum all tables in database "%s"?';
-       $lang['strconfreindexdatabase'] = 'Are you sure you want to reindex all tables in database "%s"?';
 
        // Database sizes
        $lang['strnoaccess'] = 'No Access'; 
        $lang['strconfvacuumtable'] = 'Are you sure you want to vacuum "%s"?';
        $lang['strconfanalyzetable'] = 'Are you sure you want to analyze "%s"?';
        $lang['strconfreindextable'] = 'Are you sure you want to reindex "%s"?';
+       $lang['strconfclustertable'] = 'Are you sure you want to cluster "%s"?';
        $lang['strestimatedrowcount'] = 'Estimated row count';
        $lang['strspecifytabletoanalyze'] = 'You must specify at least one table to analyze.';
        $lang['strspecifytabletoempty'] = 'You must specify at least one table to empty.';
        $lang['strspecifytabletodrop'] = 'You must specify at least one table to drop.';
        $lang['strspecifytabletovacuum'] = 'You must specify at least one table to vacuum.';
        $lang['strspecifytabletoreindex'] = 'You must specify at least one table to reindex.';
+       $lang['strspecifytabletocluster'] = 'You must specify at least one table to cluster.';
        $lang['strnofieldsforinsert'] = 'You cannot insert a row into a table with no column.';
 
        // Columns
        $lang['strdatabasealteredbad'] = 'Database alter failed.';
        $lang['strspecifydatabasetodrop'] = 'You must specify at least one database to drop.';
        $lang['strtemplatedb'] = 'Template';
+       $lang['strconfanalyzedatabase'] = 'Are you sure you want to analyze all tables in database "%s"?';
+       $lang['strconfvacuumdatabase'] = 'Are you sure you want to vacuum all tables in database "%s"?';
+       $lang['strconfreindexdatabase'] = 'Are you sure you want to reindex all tables in database "%s"?';
+       $lang['strconfclusterdatabase'] = 'Are you sure you want to cluster all tables in database "%s"?';
 
        // Views
        $lang['strview'] = 'View';
        $lang['strindextype'] = 'Type of index';
        $lang['strtablecolumnlist'] = 'Columns in table';
        $lang['strindexcolumnlist'] = 'Columns in index';
-       $lang['strconfcluster'] = 'Are you sure you want to cluster "%s"?';
        $lang['strclusteredgood'] = 'Cluster complete.';
        $lang['strclusteredbad'] = 'Cluster failed.';
        $lang['strconcurrently'] = 'Concurrently';
+       $lang['strnoclusteravailable'] = 'Table not clustered on an index.';
 
        // Rules
        $lang['strrules'] = 'Rules';
index b8f669150788e83b5a9820adc879a6f289e46ce6..173fb1e1318933255d6e0b53f77a184f3280624a 100644 (file)
        $lang['strconfdelhistory'] = 'Really remove this request from history?';
        $lang['strconfclearhistory'] = 'Really clear history?';
        $lang['strnodatabaseselected'] = 'Please, select a database.';
-       
-       // Database
-       $lang['strconfanalyzedatabase'] = 'Are you sure you want to analyze all tables in database &quot;%s&quot;?';
-       $lang['strconfvacuumdatabase'] = 'Are you sure you want to vacuum all tables in database &quot;%s&quot;?';
-       $lang['strconfreindexdatabase'] = 'Are you sure you want to reindex all tables in database &quot;%s&quot;?';
 
        // Database sizes
        $lang['strnoaccess'] = 'No Access'; 
        $lang['strconfvacuumtable'] = 'Are you sure you want to vacuum &quot;%s&quot;?';
        $lang['strconfanalyzetable'] = 'Are you sure you want to analyze &quot;%s&quot;?';
        $lang['strconfreindextable'] = 'Are you sure you want to reindex &quot;%s&quot;?';
+       $lang['strconfclustertable'] = 'Are you sure you want to cluster &quot;%s&quot;?';
        $lang['strestimatedrowcount'] = 'Estimated row count';
        $lang['strspecifytabletoanalyze'] = 'You must specify at least one table to analyze.';
        $lang['strspecifytabletoempty'] = 'You must specify at least one table to empty.';
        $lang['strspecifytabletodrop'] = 'You must specify at least one table to drop.';
        $lang['strspecifytabletovacuum'] = 'You must specify at least one table to vacuum.';
        $lang['strspecifytabletoreindex'] = 'You must specify at least one table to reindex.';
+       $lang['strspecifytabletocluster'] = 'You must specify at least one table to cluster.';
        $lang['strnofieldsforinsert'] = 'You cannot insert a row into a table with no column.';
 
        // Columns
        $lang['strdatabasealteredbad'] = 'Database alter failed.';
        $lang['strspecifydatabasetodrop'] = 'You must specify at least one database to drop.';
        $lang['strtemplatedb'] = 'Template';
+       $lang['strconfanalyzedatabase'] = 'Are you sure you want to analyze all tables in database &quot;%s&quot;?';
+       $lang['strconfvacuumdatabase'] = 'Are you sure you want to vacuum all tables in database &quot;%s&quot;?';
+       $lang['strconfreindexdatabase'] = 'Are you sure you want to reindex all tables in database &quot;%s&quot;?';
+       $lang['strconfclusterdatabase'] = 'Are you sure you want to cluster all tables in database &quot;%s&quot;?';
 
        // Views
        $lang['strview'] = 'View';
        $lang['strindextype'] = 'Type of index';
        $lang['strtablecolumnlist'] = 'Columns in table';
        $lang['strindexcolumnlist'] = 'Columns in index';
-       $lang['strconfcluster'] = 'Are you sure you want to cluster &quot;%s&quot;?';
        $lang['strclusteredgood'] = 'Cluster complete.';
        $lang['strclusteredbad'] = 'Cluster failed.';
        $lang['strconcurrently'] = 'Concurrently';
+       $lang['strnoclusteravailable'] = 'Table not clustered on an index.';
 
        // Rules
        $lang['strrules'] = 'Rules';
index ff6493ec3f75486fc842f80d55a582eae8ca8e19..48b38dad40297245755eab997ba9ed077974ec1a 100644 (file)
                                'vars'  => array('table' => 'relname'),
                                'multiaction' => 'confirm_reindex',
                        ),
+                       //'cluster' TODO ?
                );
 
                if (!$data->hasTablespaces()) unset($columns['tablespace']);