source: branches/1.7/upgrade.php @ 26062

Last change on this file since 26062 was 2103, checked in by patdenice, 17 years ago

0000421 : call a function to create empty local files during install and upgrade
Avoid log errors

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