source: trunk/upgrade.php @ 5123

Last change on this file since 5123 was 5123, checked in by plg, 15 years ago

feature 1502: based on Dotclear model, P@t has reorganized the way Piwigo
manages template/theme in a simpler "theme only level" architecture. It
supports multiple level inheritance.

File size: 11.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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
26// load config file
27$config_file = PHPWG_ROOT_PATH.'include/config_database.inc.php';
28$config_file_contents = @file_get_contents($config_file);
29if ($config_file_contents === false)
30{
31  die('Cannot load '.$config_file);
32}
33$php_end_tag = strrpos($config_file_contents, '?'.'>');
34if ($php_end_tag === false)
35{
36  die('Cannot find php end tag in '.$config_file);
37}
38
39include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
40include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
41include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
42
43include(PHPWG_ROOT_PATH.'include/config_database.inc.php');
44include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
45@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
46include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php');
47
48prepare_conf_upgrade();
49
50include_once(PHPWG_ROOT_PATH.'include/constants.php');
51define('PREFIX_TABLE', $prefixeTable);
52
53// Database connection
54$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], 
55                              $conf['db_password'], $conf['db_base']) 
56  or my_error('pwg_db_connect', true);
57
58pwg_db_check_charset();
59
60// +-----------------------------------------------------------------------+
61// |                              functions                                |
62// +-----------------------------------------------------------------------+
63
64/**
65 * list all tables in an array
66 *
67 * @return array
68 */
69function get_tables()
70{
71  $tables = array();
72
73  $query = '
74SHOW TABLES
75;';
76  $result = pwg_query($query);
77
78  while ($row = pwg_db_fetch_row($result))
79  {
80    if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
81    {
82      array_push($tables, $row[0]);
83    }
84  }
85
86  return $tables;
87}
88
89/**
90 * list all columns of each given table
91 *
92 * @return array of array
93 */
94function get_columns_of($tables)
95{
96  $columns_of = array();
97
98  foreach ($tables as $table)
99  {
100    $query = '
101DESC '.$table.'
102;';
103    $result = pwg_query($query);
104
105    $columns_of[$table] = array();
106
107    while ($row = pwg_db_fetch_row($result))
108    {
109      array_push($columns_of[$table], $row[0]);
110    }
111  }
112
113  return $columns_of;
114}
115
116/**
117 */
118function print_time($message)
119{
120  global $last_time;
121
122  $new_time = get_moment();
123  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
124  echo ' '.$message;
125  echo '</pre>';
126  flush();
127  $last_time = $new_time;
128}
129
130// +-----------------------------------------------------------------------+
131// |                             playing zone                              |
132// +-----------------------------------------------------------------------+
133
134// echo implode('<br>', get_tables());
135// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
136
137// foreach (get_available_upgrade_ids() as $upgrade_id)
138// {
139//   echo $upgrade_id, '<br>';
140// }
141
142// +-----------------------------------------------------------------------+
143// |                             language                                  |
144// +-----------------------------------------------------------------------+
145if (isset($_GET['language']))
146{
147  $language = strip_tags($_GET['language']);
148}
149else
150{
151  $language = 'en_UK';
152  // Try to get browser language
153  foreach (get_languages('utf-8') as $language_code => $language_name)
154  {
155    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
156    {
157      $language = $language_code;
158      break;
159    }
160  }
161}
162
163if ('fr_FR' == $language) {
164  define('PHPWG_DOMAIN', 'fr.piwigo.org');
165}
166else if ('pl_PL' == $language) {
167  define('PHPWG_DOMAIN', 'pl.piwigo.org');
168}
169else if ('zh_CN' == $language) {
170  define('PHPWG_DOMAIN', 'cn.piwigo.org');
171}
172else {
173  define('PHPWG_DOMAIN', 'piwigo.org');
174}
175define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
176
177load_language( 'common.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
178load_language( 'admin.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
179load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
180load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
181
182// check php version
183if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
184{
185  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
186}
187
188// +-----------------------------------------------------------------------+
189// |                        template initialization                        |
190// +-----------------------------------------------------------------------+
191
192include( PHPWG_ROOT_PATH .'include/template.class.php');
193$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
194$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
195$template->assign(array(
196  'RELEASE' => PHPWG_VERSION,
197  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href=\"%s\">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
198  )
199);
200
201// +-----------------------------------------------------------------------+
202// |                            upgrade choice                             |
203// +-----------------------------------------------------------------------+
204
205$tables = get_tables();
206$columns_of = get_columns_of($tables);
207
208// find the current release
209if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
210{
211  // we're in branch 1.3, important upgrade, isn't it?
212  if (in_array(PREFIX_TABLE.'user_category', $tables))
213  {
214    $current_release = '1.3.1';
215  }
216  else
217  {
218    $current_release = '1.3.0';
219  }
220}
221else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
222{
223  $current_release = '1.4.0';
224}
225else if (!in_array(PREFIX_TABLE.'tags', $tables))
226{
227  $current_release = '1.5.0';
228}
229else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
230{
231  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
232  {
233    $current_release = '1.6.0';
234  }
235  else
236  {
237    $current_release = '1.6.2';
238  }
239}
240else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
241{
242  $current_release = '1.7.0';
243}
244else
245{
246  die('No upgrade required, the database structure is up to date');
247}
248
249// +-----------------------------------------------------------------------+
250// |                            upgrade launch                             |
251// +-----------------------------------------------------------------------+
252$page['infos'] = array();
253$page['errors'] = array();
254$mysql_changes = array();
255
256if (isset($_POST['username']) and isset($_POST['password']))
257{
258  check_upgrade_access_rights($current_release, $_POST['username'], $_POST['password']);
259}
260
261if (isset($_POST['submit']) and check_upgrade())
262{
263  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
264  if (is_file($upgrade_file))
265  {
266    $page['upgrade_start'] = get_moment();
267    $conf['die_on_sql_error'] = false;
268    include($upgrade_file);
269
270    // Something to add in config_database.inc.php?
271    if (!empty($mysql_changes))
272    {
273      $config_file_contents = 
274        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
275        . implode("\r\n" , $mysql_changes) . "\r\n"
276        . substr($config_file_contents, $php_end_tag);
277
278      if (!@file_put_contents($config_file, $config_file_contents))
279      {
280        array_push($page['infos'],
281                   l10n_args('in <i>%s</i>, before <b>?></b>, insert:', 
282                             'include/config_database.inc.php') . 
283                   '<p><textarea rows="4" cols="40">' .
284                   implode("\r\n" , $mysql_changes).'</textarea></p>'
285                   );
286      }
287    }
288
289    // Plugins deactivation
290    if (in_array(PREFIX_TABLE.'plugins', $tables))
291    {
292      deactivate_non_standard_plugins();
293    }
294
295    // Create empty local files to avoid log errors
296    create_empty_local_files();
297
298    $page['upgrade_end'] = get_moment();
299
300    $template->assign(
301      'upgrade',
302      array(
303        'VERSION' => $current_release,
304        'TOTAL_TIME' => get_elapsed_time(
305          $page['upgrade_start'],
306          $page['upgrade_end']
307          ),
308        'SQL_TIME' => number_format(
309          $page['queries_time'],
310          3,
311          '.',
312          ' '
313          ).' s',
314        'NB_QUERIES' => $page['count_queries']
315        )
316      );
317
318    array_push($page['infos'],
319      l10n('Perform a maintenance check in [Administration>Specials>Maintenance] if you encounter any problem.')
320      );
321
322    // Save $page['infos'] in order to restore after maintenance actions
323    $page['infos_sav'] = $page['infos'];
324    $page['infos'] = array();
325
326    // c13y_upgrade plugin means "check integrity after upgrade", so it
327    // becomes useful just after an upgrade
328    $query = '
329REPLACE INTO '.PLUGINS_TABLE.'
330  (id, state)
331  VALUES (\'c13y_upgrade\', \'active\')
332;';
333    pwg_query($query);
334
335    // Delete cache data
336    invalidate_user_cache(true);
337    $template->delete_compiled_templates();
338
339    // Tables Maintenance
340    do_maintenance_all_tables();
341
342    // Restore $page['infos'] in order to hide informations messages from functions calles
343    // errors messages are not hide
344    $page['infos'] = $page['infos_sav'];
345
346  }
347}
348
349// +-----------------------------------------------------------------------+
350// |                          start template output                        |
351// +-----------------------------------------------------------------------+
352else
353{
354  foreach (get_languages('utf-8') as $language_code => $language_name)
355  {
356    if ($language == $language_code)
357    {
358      $template->assign('language_selection', $language_code);
359    }
360    $languages_options[$language_code] = $language_name;
361  }
362  $template->assign('language_options', $languages_options);
363
364  $template->assign('introduction', array(
365    'CURRENT_RELEASE' => $current_release,
366    'F_ACTION' => 'upgrade.php?language=' . $language));
367
368  if (!check_upgrade())
369  {
370    $template->assign('login', true);
371  }
372}
373
374if (count($page['errors']) != 0)
375{
376  $template->assign('errors', $page['errors']);
377}
378
379if (count($page['infos']) != 0)
380{
381  $template->assign('infos', $page['infos']);
382}
383
384// +-----------------------------------------------------------------------+
385// |                          sending html code                            |
386// +-----------------------------------------------------------------------+
387
388$template->pparse('upgrade');
389?>
Note: See TracBrowser for help on using the repository browser.