phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] CVS: etemplate/inc class.db_tools.inc.php,1.6,1.7


From: Ralf Becker <address@hidden>
Subject: [Phpgroupware-cvs] CVS: etemplate/inc class.db_tools.inc.php,1.6,1.7
Date: Sat, 11 May 2002 16:54:03 -0400

Update of /cvsroot/phpgroupware/etemplate/inc
In directory subversions:/tmp/cvs-serv23274

Modified Files:
        class.db_tools.inc.php 
Log Message:
add functionality to write/update the setup/tables_update.inc.php file

Index: class.db_tools.inc.php
===================================================================
RCS file: /cvsroot/phpgroupware/etemplate/inc/class.db_tools.inc.php,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** class.db_tools.inc.php      11 May 2002 13:01:31 -0000      1.6
--- class.db_tools.inc.php      11 May 2002 20:54:01 -0000      1.7
***************
*** 51,54 ****
--- 51,67 ----
                        'varchar'       => 'varchar'
                );
+               var $setup_header = '<?php
+   
/**************************************************************************\\
+   * phpGroupWare - Setup                                                     *
+   * http://www.phpgroupware.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 the   *
+   *  Free Software Foundation; either version 2 of the License, or (at your  *
+   *  option) any later version.                                              *
+   
\\**************************************************************************/
+ 
+   /* $Id$ */
+ ';
  
                /*!
***************
*** 107,110 ****
--- 120,124 ----
                                        return;
                                }
+                               $this->renames = array();
                        }
                        if (!$this->app)
***************
*** 133,138 ****
                        if (isset($content['write_tables']))
                        {
!                               $msg .= 
$this->messages[$this->write($this->app,$this->data) ?
!                                       'writen' : 'error_writing'];
                        }
                        elseif (isset($content['delete']))
--- 147,155 ----
                        if (isset($content['write_tables']))
                        {
!                               if 
($this->needs_save('',$this->app,$this->table,$this->data[$posted_table]))
!                               {
!                                       return;
!                               }
!                               $msg .= 
$this->messages[$this->write($this->app,$this->data) ? 'writen' : 
'error_writing'];
                        }
                        elseif (isset($content['delete']))
***************
*** 140,147 ****
                                list($col) = each($content['delete']);
  
!                               reset($this->data[$posted_table]['fd']);
!                               while ($col-- > 0 && list($key,$data) = 
each($this->data[$posted_table]['fd'])) ;
  
                                unset($this->data[$posted_table]['fd'][$key]);
                        }
                        elseif (isset($content['add_column']))
--- 157,165 ----
                                list($col) = each($content['delete']);
  
!                               @reset($this->data[$posted_table]['fd']);
!                               while ($col-- > 0 && list($key,$data) = 
@each($this->data[$posted_table]['fd'])) ;
  
                                unset($this->data[$posted_table]['fd'][$key]);
+                               $this->changes[$posted_table][$key] = 
'**deleted**';
                        }
                        elseif (isset($content['add_column']))
***************
*** 220,224 ****
                        }
                        
$this->editor->exec('etemplate.db_tools.edit',$content,$sel_options,$no_button,
!                               array('posted_table' => 
$this->table,'posted_app' => $this->app));
                }
  
--- 238,242 ----
                        }
                        
$this->editor->exec('etemplate.db_tools.edit',$content,$sel_options,$no_button,
!                               array('posted_table' => 
$this->table,'posted_app' => $this->app,'changes' => $this->changes));
                }
  
***************
*** 242,249 ****
                                        $this->read($this->app,$this->data);
                                        $this->data[$this->table] = 
$cont['edited_table'];
!                                       $this->write($this->app,$this->data);
                                        $msg .= 
$this->messages[$this->write($this->app,$this->data) ?
                                                'writen' : 'error_writing'];
                                }
                                // return to edit with everything set, so the 
user gets the table he asked for
                                $this->edit(array(
--- 260,272 ----
                                        $this->read($this->app,$this->data);
                                        $this->data[$this->table] = 
$cont['edited_table'];
!                                       $this->changes = $cont['changes'];
!                                       if ($cont['new_version'])
!                                       {
!                                               
$this->update($this->app,$this->data,$cont['new_version']);
!                                       }
                                        $msg .= 
$this->messages[$this->write($this->app,$this->data) ?
                                                'writen' : 'error_writing'];
                                }
+                               $this->changes = array();
                                // return to edit with everything set, so the 
user gets the table he asked for
                                $this->edit(array(
***************
*** 271,283 ****
                        $content = array(
                                'app' => $posted_app,
!                               'table' => $posted_table
                        );
                        $preserv = $content + array(
                                'new_app' => $new_app,
                                'new_table' => $new_table,
!                               'edited_table' => $edited_table
                        );
                        $tmpl = new etemplate('etemplate.db-tools.ask_save');
  
                        
$tmpl->exec('etemplate.db_tools.needs_save',$content,array(),array(),$preserv);
  
--- 294,315 ----
                        $content = array(
                                'app' => $posted_app,
!                               'table' => $posted_table,
!                               'version' => $this->setup_version($posted_app)
                        );
                        $preserv = $content + array(
                                'new_app' => $new_app,
                                'new_table' => $new_table,
!                               'edited_table' => $edited_table,
!                               'changes' => $this->changes
                        );
+                       $content['new_version'] = $content['version'];
+ 
                        $tmpl = new etemplate('etemplate.db-tools.ask_save');
  
+                       if 
(!file_exists(PHPGW_SERVER_ROOT."/$posted_app/setup/tables_current.inc.php"))
+                       {
+                               $tmpl->disable_cells('version');
+                               $tmpl->disable_cells('new_version');
+                       }
                        
$tmpl->exec('etemplate.db_tools.needs_save',$content,array(),array(),$preserv);
  
***************
*** 324,327 ****
--- 356,366 ----
                function content2table($content)
                {
+                       if (!is_array($this->data))
+                       {
+                               $this->read($content['posted_app'],$this->data);
+                       }
+                       $old_cols = $this->data[$posted_table = 
$content['posted_table']]['fd'];
+                       $this->changes = $content['changes'];
+ 
                        $table = array();
                        $table['fd'] = array(); // do it in the default order 
of tables_*
***************
*** 333,338 ****
--- 372,386 ----
                        {
                                $col = $content["Row$n"];
+ 
+                               while ((list($old_name,$old_col) = 
@each($old_cols)) &&
+                                      $this->changes[$posted_table][$old_name] 
== '**deleted**') ;
+ 
                                if (($name = $col['name']) != '')               
// ignoring lines without column-name
                                {
+                                       if ($col['name'] != $old_name && $n <= 
count($old_cols))        // column renamed --> remeber it
+                                       {
+                                               
$this->changes[$posted_table][$old_name] = $col['name'];
+                                               //echo "<p>content2table: 
$posted_table.$old_name renamed to $col[name]</p>\n";
+                                       }
                                        while (list($prop,$val) = each($col))
                                        {
***************
*** 370,373 ****
--- 418,422 ----
                        {
                                echo "<p>content2table: table ="; 
_debug_array($table);
+                               echo "<p>changes = "; 
_debug_array($this->changes);
                        }
                        return $table;
***************
*** 484,488 ****
                        if (!$header)
                        {
!                               $header = "<?php\n\n";
                        }
                        if (!is_writeable(PHPGW_SERVER_ROOT."/$app/setup") || 
!($f = fopen($file,'w')))
--- 533,537 ----
                        if (!$header)
                        {
!                               $header = $this->setup_header . "\n\n";
                        }
                        if (!is_writeable(PHPGW_SERVER_ROOT."/$app/setup") || 
!($f = fopen($file,'w')))
***************
*** 498,501 ****
--- 547,750 ----
  
                        return True;
+               }
+ 
+               /*!
+               @function setup_version($app,$new = '')
+               @abstract reads and updates the version in file 
$app/setup/setup.inc.php if $new != ''
+               @return the version or False if the file could not be read or 
written
+               */
+               function setup_version($app,$new = '')
+               {
+                       //echo 
"<p>etemplate.db_tools.setup_version('$app','$new')</p>\n";
+ 
+                       $file = PHPGW_SERVER_ROOT."/$app/setup/setup.inc.php";
+                       if (file_exists($file))
+                       {
+                               include($file);
+                       }
+                       if (!is_array($setup_info[$app]) || 
!isset($setup_info[$app]['version']))
+                       {
+                               return False;
+                       }
+                       if ($new == '' || $setup_info[$app]['version'] == $new)
+                       {
+                               return $setup_info[$app]['version'];
+                       }
+                       if (!($f = fopen($file,'r')))
+                       {
+                               return False;
+                       }
+                       $fcontent = fread($f,filesize($file));
+                       fclose ($f);
+ 
+                       if (is_writable(PHPGW_SERVER_ROOT."/$app/setup"))
+                       {
+                               
rename($file,PHPGW_SERVER_ROOT."/$app/setup/setup.old.inc.php");
+                       }
+                       $fnew = 
eregi_replace("(.*\\$"."setup_info\\['$app'\\]\\['version'\\][ \\t]*=[ 
\\t]*')[^']*('.*)","\\1$new"."\\2",$fcontent);
+ 
+                       if (!is_writeable(PHPGW_SERVER_ROOT."/$app/setup") || 
!($f = fopen($file,'w')))
+                       {
+                               return False;
+                       }
+                       fwrite($f,$fnew);
+                       fclose($f);
+ 
+                       return $new;
+               }
+ 
+               /*!
+               @function update($app,$current,$version)
+               @abstract updates file /$app/setup/tables_update.inc.php to 
reflect changes in $current
+               @param $app app-name
+               @param $current new tabledefinitions
+               @param $version new version
+               @return True if file writen else False
+               */
+               function update($app,$current,$version)
+               {
+                       //echo 
"<p>etemplate.db_tools.update('$app',...,'$version')</p>\n";
+ 
+                       if (!is_writable(PHPGW_SERVER_ROOT."/$app/setup"))
+                       {
+                               return False;
+                       }
+                       $file_baseline = 
PHPGW_SERVER_ROOT."/$app/setup/tables_baseline.inc.php";
+                       $file_current  = 
PHPGW_SERVER_ROOT."/$app/setup/tables_current.inc.php";
+                       $file_update   = 
PHPGW_SERVER_ROOT."/$app/setup/tables_update.inc.php";
+ 
+                       if (!file_exists($file_baseline) && 
!copy($file_current,$file_baseline))
+                       {
+                               //echo "<p>Can't copy $file_current to 
$file_baseline !!!</p>\n";
+                               return False;
+                       }
+                       $old_version = $this->setup_version($app);
+                       $old_version_ = str_replace('.','_',$old_version);
+ 
+                       if (file_exists($file_update))
+                       {
+                               $f = fopen($file_update,'r');
+                               $update = fread($f,filesize($file_update));
+                               $update = str_replace('?>','',$update);
+                               fclose($f);
+                               
rename($file_update,PHPGW_SERVER_ROOT."/$app/setup/tables_update.old.inc.php");
+                       }
+                       else
+                       {
+                               $update = $this->setup_header;
+                       }
+                       $update .= "
+       \$test[] = '$old_version';
+       function $app"."_upgrade$old_version_()
+       {\n";
+ 
+                       $update .= $this->update_schema($app,$current);
+ 
+                       $update .= "\n
+               \$GLOBALS['setup_info']['$app']['currentver'] = '$version';
+               return \$GLOBALS['setup_info']['phpgwapi']['currentver'];
+       }
+ ?".">\n";
+                       if (!($f = fopen($file_update,'w')))
+                       {
+                               //echo "<p>Cant open '$update' for writing 
!!!</p>\n";
+                               return False;
+                       }
+                       fwrite($f,$update);
+                       fclose($f);
+ 
+                       $this->setup_version($app,$version);
+ 
+                       return True;
+               }
+ 
+               function remove_from_array(&$arr,$value)
+               {
+                       reset($arr);
+                       while (list($key,$val) = each($arr))
+                       {
+                               if ($val == $value)
+                               {
+                                       unset($arr[$key]);
+                               }
+                       }
+               }
+ 
+               function update_schema($app,$current)
+               {
+                       $this->read($app,$old);
+ 
+                       reset($old);
+                       while (list($name,$table_def) = each($old))
+                       {
+                               if (!isset($current[$name]))    // table $name 
droped
+                               {
+                                       $update .= 
"\t\t\$GLOBALS['phpgw_setup']->oProc->DropTable('$name');\n";
+                               }
+                               else
+                               {
+                                       reset($table_def['fd']);
+                                       while(list($col,$col_def) = 
each($table_def['fd']))
+                                       {
+                                               if 
(!isset($current[$name]['fd'][$col]))        // column $col droped
+                                               {
+                                                       if 
(!isset($this->changes[$name][$col]) || $this->changes[$name][$col] == 
'**deleted**')
+                                                       {
+                                                               $new_table_def 
= $table_def;
+                                                               
unset($new_table_def['fd'][$col]);
+                                                               
$this->remove_from_array($new_table_def['pk'],$col);
+                                                               
$this->remove_from_array($new_table_def['fk'],$col);
+                                                               
$this->remove_from_array($new_table_def['ix'],$col);
+                                                               
$this->remove_from_array($new_table_def['uc'],$col);
+                                                               $update .= 
"\t\t\$GLOBALS['phpgw_setup']->oProc->DropColumn('$name',";
+                                                               $update .= 
$this->write_array($new_table_def,2).",'$col');\n";
+                                                       }
+                                                       else    // column $col 
renamed
+                                                       {
+                                                               $new_col = 
$this->changes[$name][$col];
+                                                               $update .= 
"\t\t\$GLOBALS['phpgw_setup']->oProc->RenameColumn('$name','$col','$new_col');\n";
+                                                       }
+                                               }
+                                       }
+                                       @reset($this->changes[$name]);
+                                       while (list($col,$new_col) = 
@each($this->changes[$name]))
+                                       {
+                                               if ($new_col != '**deleted**')
+                                               {
+                                                       
$old[$name]['fd'][$new_col] = $old[$name]['fd'][$col];  // to be able to detect 
further changes of the definition
+                                                       
unset($old[$name]['fd'][$col]);
+                                               }
+                                       }
+                               }
+                       }
+                       reset($current);
+                       while(list($name,$table_def) = each($current))
+                       {
+                               if (!isset($old[$name]))        // table $name 
added
+                               {
+                                       $update .= 
"\t\t\$GLOBALS['phpgw_setup']->oProc->CreateTable('$name',";
+                                       $update .= 
$this->write_array($table_def,2).");\n";
+                               }
+                               else
+                               {
+                                       $old_norm = 
$this->normalize($old[$name]);
+                                       $new_norm = 
$this->normalize($table_def);
+                                       reset($table_def['fd']);
+                                       while (list($col,$col_def) = 
each($table_def['fd']))
+                                       {
+                                               if (($add = 
!isset($old[$name]['fd'][$col])) || // column $col added
+                                                        
serialize($old_norm['fd'][$col]) != serialize($new_norm['fd'][$col])) // column 
definition altered
+                                               {
+                                                       $update .= 
"\t\t$"."GLOBALS['phpgw_setup']->oProc->".($add ? 'Add' : 
'Alter')."Column('$name','$col',";
+                                                       $update .= 
$this->write_array($col_def,2) . ");\n";
+                                               }
+                                       }
+                               }
+                       }
+                       if ($this->debug)
+                       {
+                               echo "<p>update_schema($app, ...) 
=<br><pre>$update</pre>)</p>\n";
+                       }
+                       return $update;
                }
  




reply via email to

[Prev in Thread] Current Thread [Next in Thread]