source: branches/2.1/upgrade.php @ 20958

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

remove all svn:mergeinfo properties

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