Changeset 1174 for branches/branch-1_6


Ignore:
Timestamp:
Apr 14, 2006, 11:25:49 PM (19 years ago)
Author:
plg
Message:

new: upgrade script from release 1.5.0

improvement: ability to turn off dying on SQL queries failure. Could be
useful for upgrades.

Location:
branches/branch-1_6
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/branch-1_6/include/config_default.inc.php

    r1157 r1174  
    334334$conf['debug_l10n'] = false;
    335335
     336// die_on_sql_error: if an SQL query fails, should everything stop?
     337$conf['die_on_sql_error'] = true;
     338
    336339// +-----------------------------------------------------------------------+
    337340// |                            authentication                             |
  • branches/branch-1_6/include/functions.inc.php

    r1161 r1174  
    652652function my_error($header)
    653653{
     654  global $conf;
     655 
    654656  $error = '<pre>';
    655657  $error.= $header;
     
    657659  $error.= mysql_error();
    658660  $error.= '</pre>';
    659   die ($error);
     661
     662  if ($conf['die_on_sql_error'])
     663  {
     664    die($error);
     665  }
     666  else
     667  {
     668    echo $error;
     669  }
    660670}
    661671
  • branches/branch-1_6/template/yoga/upgrade.tpl

    r859 r1174  
    88
    99  <body>
    10     <!-- BEGIN choices -->
     10    <!-- BEGIN introduction -->
    1111    <h1>Welcome to PhpWebGallery upgrade page.</h1>
    12     <p>This page proposes to upgrade your database corresponding to your old version
    13       of PhpWebGallery to the current version. Select the version you wish to upgrade
    14       :</p>
    1512
    16     <ul>
    17       <!-- BEGIN choice -->
    18       <li><a href="{choices.choice.URL}">{choices.choice.VERSION}</a></li>
    19       <!-- END choice -->
    20     </ul>
    21     <!-- END choices -->
     13    <p>This page proposes to upgrade your database corresponding to your old
     14version of PhpWebGallery to the current version. The upgrade assistant
     15thinks you are currently running a
     16<strong>release {introduction.CURRENT_RELEASE}</strong> (or equivalent).</p>
     17
     18    <p><a href="{introduction.RUN_UPGRADE_URL}">Upgrade from release
     19{introduction.CURRENT_RELEASE} to {RELEASE}</a></p>
     20    <!-- END introduction -->
    2221
    2322    <!-- BEGIN upgrade -->
  • branches/branch-1_6/upgrade.php

    r1075 r1174  
    3030include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
    3131include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     32include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
    3233include(PHPWG_ROOT_PATH.'include/template.php');
    3334
    3435include(PHPWG_ROOT_PATH.'include/mysql.inc.php');
    35 
    36 // +-----------------------------------------------------------------------+
    37 // | Check Access and exit when it is not ok                               |
    38 // +-----------------------------------------------------------------------+
     36include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
     37@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
     38
    3939check_upgrade();
    4040
     
    4444include_once(PHPWG_ROOT_PATH.'include/constants.php');
    4545define('PREFIX_TABLE', $prefixeTable);
    46 
    47 $conf['show_queries'] = false;
    4846
    4947// Database connection
     
    6967
    7068/**
    71  * loads an sql file and executes all queries
     69 * list all tables in an array
    7270 *
    73  * Before executing a query, $replaced is... replaced by $replacing. This is
    74  * useful when the SQL file contains generic words. Drop table queries are
    75  * not executed.
     71 * @return array
     72 */
     73function get_tables()
     74{
     75  $tables = array();
     76 
     77  $query = '
     78SHOW TABLES
     79;';
     80  $result = mysql_query($query);
     81
     82  while ($row = mysql_fetch_row($result))
     83  {
     84    array_push(
     85      $tables,
     86      preg_replace('/^'.PREFIX_TABLE.'/', '', $row[0])
     87      );
     88  }
     89
     90  return $tables;
     91}
     92
     93/**
     94 * list all columns of each given table
    7695 *
    77  * @param string filepath
    78  * @param string replaced
    79  * @param string replacing
     96 * @return array of array
     97 */
     98function get_columns_of($tables)
     99{
     100  $columns_of = array();
     101
     102  foreach ($tables as $table)
     103  {
     104    $query = '
     105DESC '.PREFIX_TABLE.$table.'
     106;';
     107    $result = mysql_query($query);
     108
     109    $columns_of[$table] = array();
     110
     111    while ($row = mysql_fetch_row($result))
     112    {
     113      array_push($columns_of[$table], $row[0]);
     114    }
     115  }
     116
     117  return $columns_of;
     118}
     119
     120/**
     121 */
     122function print_time($message)
     123{
     124  global $last_time;
     125 
     126  $new_time = get_moment();
     127  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
     128  echo ' '.$message;
     129  echo '</pre>';
     130  flush();
     131  $last_time = $new_time;
     132}
     133
     134/**
     135 * replace old style #images.keywords by #tags. Requires a big data
     136 * migration.
     137 *
    80138 * @return void
    81139 */
    82 function execute_sqlfile($filepath, $replaced, $replacing)
    83 {
    84   $sql_lines = file($filepath);
    85   $query = '';
    86   foreach ($sql_lines as $sql_line)
    87   {
    88     $sql_line = trim($sql_line);
    89     if (preg_match('/(^--|^$)/', $sql_line))
    90     {
    91       continue;
    92     }
    93     $query.= ' '.$sql_line;
    94     // if we reached the end of query, we execute it and reinitialize the
    95     // variable "query"
    96     if (preg_match('/;$/', $sql_line))
    97     {
    98       $query = trim($query);
    99       $query = str_replace($replaced, $replacing, $query);
    100       // we don't execute "DROP TABLE" queries
    101       if (!preg_match('/^DROP TABLE/i', $query))
     140function tag_replace_keywords()
     141{
     142  // code taken from upgrades 19 and 22
     143 
     144  $query = '
     145CREATE TABLE '.PREFIX_TABLE.'tags (
     146  id smallint(5) UNSIGNED NOT NULL auto_increment,
     147  name varchar(255) BINARY NOT NULL,
     148  url_name varchar(255) BINARY NOT NULL,
     149  PRIMARY KEY (id)
     150)
     151;';
     152  pwg_query($query);
     153 
     154  $query = '
     155CREATE TABLE '.PREFIX_TABLE.'image_tag (
     156  image_id mediumint(8) UNSIGNED NOT NULL,
     157  tag_id smallint(5) UNSIGNED NOT NULL,
     158  PRIMARY KEY (image_id,tag_id)
     159)
     160;';
     161  pwg_query($query);
     162 
     163  //
     164  // Move keywords to tags
     165  //
     166
     167  // each tag label is associated to a numeric identifier
     168  $tag_id = array();
     169  // to each tag id (key) a list of image ids (value) is associated
     170  $tag_images = array();
     171
     172  $current_id = 1;
     173
     174  $query = '
     175SELECT id, keywords
     176  FROM '.PREFIX_TABLE.'images
     177  WHERE keywords IS NOT NULL
     178;';
     179  $result = pwg_query($query);
     180  while ($row = mysql_fetch_array($result))
     181  {
     182    foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
     183    {
     184      if (!isset($tag_id[$keyword]))
    102185      {
    103         mysql_query($query);
     186        $tag_id[$keyword] = $current_id++;
    104187      }
    105       $query = '';
    106     }
    107   }
    108 }
     188
     189      if (!isset($tag_images[ $tag_id[$keyword] ]))
     190      {
     191        $tag_images[ $tag_id[$keyword] ] = array();
     192      }
     193
     194      array_push(
     195        $tag_images[ $tag_id[$keyword] ],
     196        $row['id']
     197        );
     198    }
     199  }
     200
     201  $datas = array();
     202  foreach ($tag_id as $tag_name => $tag_id)
     203  {
     204    array_push(
     205      $datas,
     206      array(
     207        'id'       => $tag_id,
     208        'name'     => $tag_name,
     209        'url_name' => str2url($tag_name),
     210        )
     211      );
     212  }
     213 
     214  if (!empty($datas))
     215  {
     216    mass_inserts(
     217      PREFIX_TABLE.'tags',
     218      array_keys($datas[0]),
     219      $datas
     220      );
     221  }
     222
     223  $datas = array();
     224  foreach ($tag_images as $tag_id => $images)
     225  {
     226    foreach (array_unique($images) as $image_id)
     227    {
     228      array_push(
     229        $datas,
     230        array(
     231          'tag_id'   => $tag_id,
     232          'image_id' => $image_id,
     233          )
     234        );
     235    }
     236  }
     237 
     238  if (!empty($datas))
     239  {
     240    mass_inserts(
     241      PREFIX_TABLE.'image_tag',
     242      array_keys($datas[0]),
     243      $datas
     244      );
     245  }
     246
     247  //
     248  // Delete images.keywords
     249  //
     250  $query = '
     251ALTER TABLE '.PREFIX_TABLE.'images DROP COLUMN keywords
     252;';
     253  pwg_query($query);
     254
     255  //
     256  // Add useful indexes
     257  //
     258  $query = '
     259ALTER TABLE '.PREFIX_TABLE.'tags
     260  ADD INDEX tags_i1(url_name)
     261;';
     262  pwg_query($query);
     263
     264
     265  $query = '
     266ALTER TABLE '.PREFIX_TABLE.'image_tag
     267  ADD INDEX image_tag_i1(tag_id)
     268;';
     269  pwg_query($query);
     270
     271  print_time('tags have replaced keywords');
     272}
     273
     274// +-----------------------------------------------------------------------+
     275// |                             playing zone                              |
     276// +-----------------------------------------------------------------------+
     277
     278// echo implode('<br>', get_tables());
     279// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
     280
    109281// +-----------------------------------------------------------------------+
    110282// |                        template initialization                        |
     
    116288
    117289// +-----------------------------------------------------------------------+
    118 // |                          versions upgradable                          |
    119 // +-----------------------------------------------------------------------+
    120 $versions = array();
    121 $path = PHPWG_ROOT_PATH.'install';
    122 if ($contents = opendir($path))
    123 {
    124   while (($node = readdir($contents)) !== false)
    125   {
    126     if (is_file($path.'/'.$node)
    127         and preg_match('/^upgrade_(.*?)\.php$/', $node, $match))
    128     {
    129       array_push($versions, $match[1]);
    130     }
    131   }
    132 }
    133 natcasesort($versions);
    134 // +-----------------------------------------------------------------------+
    135290// |                            upgrade choice                             |
    136291// +-----------------------------------------------------------------------+
     292
    137293if (!isset($_GET['version']))
    138294{
    139   $template->assign_block_vars('choices', array());
    140   foreach ($versions as $version)
    141   {
    142     $template->assign_block_vars(
    143       'choices.choice',
    144       array(
    145         'URL' => PHPWG_ROOT_PATH.'upgrade.php?version='.$version,
    146         'VERSION' => $version
    147         ));
    148   }
    149 }
     295  // find the current release
     296  $tables = get_tables();
     297  $columns_of = get_columns_of($tables);
     298
     299  if (!in_array('param', $columns_of['config']))
     300  {
     301    // we're in branch 1.3, important upgrade, isn't it?
     302    if (in_array('user_category', $tables))
     303    {
     304      $current_release = '1.3.1';
     305    }
     306    else
     307    {
     308      $current_release = '1.3.0';
     309    }
     310  }
     311  else if (!in_array('user_cache', $tables))
     312  {
     313    $current_release = '1.4.0';
     314  }
     315  else if (!in_array('tags', $tables))
     316  {
     317    $current_release = '1.5.0';
     318  }
     319  else
     320  {
     321    die('You are already on branch 1.6, no upgrade required');
     322  }
     323 
     324  $template->assign_block_vars(
     325    'introduction',
     326    array(
     327      'CURRENT_RELEASE' => $current_release,
     328      'RUN_UPGRADE_URL' =>
     329        PHPWG_ROOT_PATH.'upgrade.php?version='.$current_release,
     330      )
     331    );
     332}
     333
    150334// +-----------------------------------------------------------------------+
    151335// |                            upgrade launch                             |
    152336// +-----------------------------------------------------------------------+
     337
    153338else
    154339{
    155   $upgrade_file = $path.'/upgrade_'.$_GET['version'].'.php';
     340  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$_GET['version'].'.php';
    156341  if (is_file($upgrade_file))
    157342  {
    158343    $page['upgrade_start'] = get_moment();
     344    $conf['die_on_sql_error'] = false;
    159345    include($upgrade_file);
    160346    $page['upgrade_end'] = get_moment();
     
    164350      array(
    165351        'VERSION' => $_GET['version'],
    166         'TOTAL_TIME' => get_elapsed_time($page['upgrade_start'],
    167                                          $page['upgrade_end']),
    168         'SQL_TIME' => number_format($page['queries_time'], 3, '.', ' ').' s',
     352        'TOTAL_TIME' => get_elapsed_time(
     353          $page['upgrade_start'],
     354          $page['upgrade_end']
     355          ),
     356        'SQL_TIME' => number_format(
     357          $page['queries_time'],
     358          3,
     359          '.',
     360          ' '
     361          ).' s',
    169362        'NB_QUERIES' => $page['count_queries']
    170         ));
     363        )
     364      );
    171365
    172366    if (!isset($infos))
     
    198392    foreach ($infos as $info)
    199393    {
    200       $template->assign_block_vars('upgrade.infos.info',
    201                                    array('CONTENT' => $info));
     394      $template->assign_block_vars(
     395        'upgrade.infos.info',
     396        array(
     397          'CONTENT' => $info,
     398          )
     399        );
    202400    }
    203401  }
     
    207405  }
    208406}
     407
     408$query = '
     409UPDATE '.USER_CACHE_TABLE.'
     410  SET need_update = \'true\'
     411;';
     412pwg_query($query);
     413
    209414// +-----------------------------------------------------------------------+
    210415// |                          sending html code                            |
    211416// +-----------------------------------------------------------------------+
     417
    212418$template->pparse('upgrade');
    213419?>
Note: See TracChangeset for help on using the changeset viewer.