source: trunk/upgrade.php @ 2700

Last change on this file since 2700 was 2700, checked in by rvelices, 16 years ago
  • replace a preg_match with simple comparison (faster)
  • try to get rid of some JS strict warnings in FF (hope it's fixed)
  • template.class.php is included from functions.inc.php instead of common,upgrade,install (maybe later I will try to see the perf impact of compacting/compiling functions.inc.php together with the included files from itself)
  • 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// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24define('PHPWG_ROOT_PATH', './');
25
26include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
27include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
28include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
29
30include(PHPWG_ROOT_PATH.'include/mysql.inc.php');
31include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
32@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
33
34check_upgrade();
35
36prepare_conf_upgrade();
37
38include_once(PHPWG_ROOT_PATH.'include/constants.php');
39define('PREFIX_TABLE', $prefixeTable);
40
41// Database connection
42mysql_connect( $cfgHote, $cfgUser, $cfgPassword ) or die ( "Could not connect to database server" );
43mysql_select_db( $cfgBase ) or die ( "Could not connect to database" );
44if ( version_compare(mysql_get_server_info(), '4.1.0', '>=')
45    and defined('DB_CHARSET') and DB_CHARSET!='' )
46{
47  pwg_query('SET NAMES "'.DB_CHARSET.'"');
48}
49
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('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(
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    // Create empty local files to avoid log errors
251    create_empty_local_files();
252
253    $page['upgrade_end'] = get_moment();
254
255    $template->assign(
256      'upgrade',
257      array(
258        'VERSION' => $_GET['version'],
259        'TOTAL_TIME' => get_elapsed_time(
260          $page['upgrade_start'],
261          $page['upgrade_end']
262          ),
263        'SQL_TIME' => number_format(
264          $page['queries_time'],
265          3,
266          '.',
267          ' '
268          ).' s',
269        'NB_QUERIES' => $page['count_queries']
270        )
271      );
272
273    array_push(
274      $page['infos'],
275      '[security] delete files "upgrade.php", "upgrade_feed.php", "install.php" and "install"
276directory'
277      );
278
279    array_push(
280      $page['infos'],
281      'in include/mysql.inc.php, remove
282<pre style="background-color:lightgray">
283define(\'PHPWG_IN_UPGRADE\', true);
284</pre>'
285      );
286
287    array_push(
288      $page['infos'],
289      'Perform a maintenance check in [Administration>General>Maintenance]
290if you encounter any problem.'
291      );
292
293    $template->assign('infos', $page['infos']);
294
295    $query = '
296UPDATE '.USER_CACHE_TABLE.'
297  SET need_update = \'true\'
298;';
299
300    pwg_query($query);
301    $query = '
302REPLACE INTO '.PLUGINS_TABLE.'
303  (id, state)
304  VALUES (\'c13y_upgrade\', \'active\')
305;';
306    pwg_query($query);
307  }
308  else
309  {
310    die('Hacking attempt');
311  }
312}
313
314// +-----------------------------------------------------------------------+
315// |                          sending html code                            |
316// +-----------------------------------------------------------------------+
317
318$template->pparse('upgrade');
319?>
Note: See TracBrowser for help on using the repository browser.