source: trunk/upgrade.php @ 2096

Last change on this file since 2096 was 2096, checked in by rub, 17 years ago

Resolved 0000748: Name configuration of all the tables
Resolved 0000750: Check anomalies after upgrade (Feature proposition)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: upgrade.php 2096 2007-09-19 05:26:58Z rub $
8// | last update   : $Date: 2007-09-19 05:26:58 +0000 (Wed, 19 Sep 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2096 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27define('PHPWG_ROOT_PATH', './');
28
29include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
30include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
31include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
32include(PHPWG_ROOT_PATH.'include/template.php');
33
34include(PHPWG_ROOT_PATH.'include/mysql.inc.php');
35include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
36@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
37
38check_upgrade();
39
40prepare_conf_upgrade();
41
42include_once(PHPWG_ROOT_PATH.'include/constants.php');
43define('PREFIX_TABLE', $prefixeTable);
44
45// Database connection
46mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
47or die ( "Could not connect to database server" );
48mysql_select_db( $cfgBase )
49or die ( "Could not connect to database" );
50// +-----------------------------------------------------------------------+
51// |                            tricky output                              |
52// +-----------------------------------------------------------------------+
53echo '<!-- This is an HTML comment given in order to make IE outputs';
54echo ' the code.'."\n";
55echo ' Indeed, IE doesn\'t start to send output until a limit';
56echo ' of XXX bytes '."\n";
57echo str_repeat( ' ', 80 )."\n";
58echo str_repeat( ' ', 80 )."\n";
59echo str_repeat( ' ', 80 )."\n";
60echo '-->'."\n";
61flush();
62// +-----------------------------------------------------------------------+
63// |                              functions                                |
64// +-----------------------------------------------------------------------+
65
66/**
67 * list all tables in an array
68 *
69 * @return array
70 */
71function get_tables()
72{
73  $tables = array();
74 
75  $query = '
76SHOW TABLES
77;';
78  $result = mysql_query($query);
79
80  while ($row = mysql_fetch_row($result))
81  {
82    if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
83    {
84      array_push($tables, $row[0]);
85    }
86  }
87
88  return $tables;
89}
90
91/**
92 * list all columns of each given table
93 *
94 * @return array of array
95 */
96function get_columns_of($tables)
97{
98  $columns_of = array();
99
100  foreach ($tables as $table)
101  {
102    $query = '
103DESC '.$table.'
104;';
105    $result = mysql_query($query);
106
107    $columns_of[$table] = array();
108
109    while ($row = mysql_fetch_row($result))
110    {
111      array_push($columns_of[$table], $row[0]);
112    }
113  }
114
115  return $columns_of;
116}
117
118/**
119 */
120function print_time($message)
121{
122  global $last_time;
123 
124  $new_time = get_moment();
125  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
126  echo ' '.$message;
127  echo '</pre>';
128  flush();
129  $last_time = $new_time;
130}
131
132// +-----------------------------------------------------------------------+
133// |                             playing zone                              |
134// +-----------------------------------------------------------------------+
135
136// echo implode('<br>', get_tables());
137// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
138
139// foreach (get_available_upgrade_ids() as $upgrade_id)
140// {
141//   echo $upgrade_id, '<br>';
142// }
143
144// +-----------------------------------------------------------------------+
145// |                        template initialization                        |
146// +-----------------------------------------------------------------------+
147
148$template = new Template(PHPWG_ROOT_PATH.'template/yoga');
149$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
150$template->assign_vars(array('RELEASE'=>PHPWG_VERSION));
151
152// +-----------------------------------------------------------------------+
153// |                            upgrade choice                             |
154// +-----------------------------------------------------------------------+
155
156$tables = get_tables();
157$columns_of = get_columns_of($tables);
158
159if (!isset($_GET['version']))
160{
161  // find the current release
162  if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
163  {
164    // we're in branch 1.3, important upgrade, isn't it?
165    if (in_array(PREFIX_TABLE.'user_category', $tables))
166    {
167      $current_release = '1.3.1';
168    }
169    else
170    {
171      $current_release = '1.3.0';
172    }
173  }
174  else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
175  {
176    $current_release = '1.4.0';
177  }
178  else if (!in_array(PREFIX_TABLE.'tags', $tables))
179  {
180    $current_release = '1.5.0';
181  }
182  else if (!in_array(PREFIX_TABLE.'history_summary', $tables))
183  {
184    if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
185    {
186      $current_release = '1.6.0';
187    }
188    else
189    {
190      $current_release = '1.6.2';
191    }
192  }
193  else
194  {
195    die('No upgrade required, the database structure is up to date');
196  }
197 
198  $template->assign_block_vars(
199    'introduction',
200    array(
201      'CURRENT_RELEASE' => $current_release,
202      'RUN_UPGRADE_URL' =>
203        PHPWG_ROOT_PATH.'upgrade.php?version='.$current_release,
204      )
205    );
206}
207
208// +-----------------------------------------------------------------------+
209// |                            upgrade launch                             |
210// +-----------------------------------------------------------------------+
211
212else
213{
214  if (in_array(PREFIX_TABLE.'history_summary', $tables))
215  {
216    die('No database upgrade required, do not refresh the page');
217  }
218   
219  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$_GET['version'].'.php';
220  if (is_file($upgrade_file))
221  {
222    $page['infos'] = array();
223    $page['upgrade_start'] = get_moment();
224    $conf['die_on_sql_error'] = false;
225    include($upgrade_file);
226
227    // Available upgrades must be ignored after a fresh installation. To
228    // make PWG avoid upgrading, we must tell it upgrades have already been
229    // made.
230    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
231    define('CURRENT_DATE', $dbnow);
232    $datas = array();
233    foreach (get_available_upgrade_ids() as $upgrade_id)
234    {
235      array_push(
236        $datas,
237        array(
238          'id'          => $upgrade_id,
239          'applied'     => CURRENT_DATE,
240          'description' => 'upgrade included in migration',
241          )
242        );
243    }
244    mass_inserts(
245      UPGRADE_TABLE,
246      array_keys($datas[0]),
247      $datas
248      );
249   
250    $page['upgrade_end'] = get_moment();
251
252    $template->assign_block_vars(
253      'upgrade',
254      array(
255        'VERSION' => $_GET['version'],
256        'TOTAL_TIME' => get_elapsed_time(
257          $page['upgrade_start'],
258          $page['upgrade_end']
259          ),
260        'SQL_TIME' => number_format(
261          $page['queries_time'],
262          3,
263          '.',
264          ' '
265          ).' s',
266        'NB_QUERIES' => $page['count_queries']
267        )
268      );
269
270    array_push(
271      $page['infos'],
272      '[security] delete files "upgrade.php", "upgrade_feed.php", "install.php" and "install"
273directory'
274      );
275
276    array_push(
277      $page['infos'],
278      'in include/mysql.inc.php, remove
279<pre style="background-color:lightgray">
280define(\'PHPWG_IN_UPGRADE\', true);
281</pre>'
282      );
283
284    array_push(
285      $page['infos'],
286      'Perform a maintenance check in [Administration>General>Maintenance]
287if you encounter any problem.'
288      );
289   
290    $template->assign_block_vars('upgrade.infos', array());
291   
292    foreach ($page['infos'] as $info)
293    {
294      $template->assign_block_vars(
295        'upgrade.infos.info',
296        array(
297          'CONTENT' => $info,
298          )
299        );
300    }
301
302    $query = '
303UPDATE '.USER_CACHE_TABLE.'
304  SET need_update = \'true\'
305;';
306
307    pwg_query($query);
308    $query = '
309REPLACE INTO '.PLUGINS_TABLE.'
310  (id, state)
311  VALUES (\'c13y_upgrade\', \'active\')
312;';
313    pwg_query($query);
314  }
315  else
316  {
317    die('Hacking attempt');
318  }
319}
320
321// +-----------------------------------------------------------------------+
322// |                          sending html code                            |
323// +-----------------------------------------------------------------------+
324
325$template->pparse('upgrade');
326?>
Note: See TracBrowser for help on using the repository browser.