source: trunk/upgrade.php @ 5982

Revision 5982, 12.6 KB checked in by plg, 9 years ago (diff)

feature 1630: upgrade to Piwigo 2.1 :-)

bug 1604: only activate core themes not all themes.

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_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
40include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
41
42include(PHPWG_ROOT_PATH.'local/config/database.inc.php');
43include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
44@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
45
46// $conf is not used for users tables - define cannot be re-defined
47define('USERS_TABLE', $prefixeTable.'users');
48include_once(PHPWG_ROOT_PATH.'include/constants.php');
49define('PREFIX_TABLE', $prefixeTable);
50define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
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 {
183  define('PHPWG_DOMAIN', 'piwigo.org');
184}
185define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
186
187load_language( 'common.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
188load_language( 'admin.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
189load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
190load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
191
192// check php version
193if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
194{
195  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
196}
197
198// +-----------------------------------------------------------------------+
199// |                          database connection                          |
200// +-----------------------------------------------------------------------+
201include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
202include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php');
203
204upgrade_db_connect();
205pwg_db_check_charset();
206
207list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
208define('CURRENT_DATE', $dbnow);
209
210// +-----------------------------------------------------------------------+
211// |                        template initialization                        |
212// +-----------------------------------------------------------------------+
213
214include( PHPWG_ROOT_PATH .'include/template.class.php');
215$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
216$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
217$template->assign(array(
218  'RELEASE' => PHPWG_VERSION,
219  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
220  )
221);
222
223// +-----------------------------------------------------------------------+
224// |                            upgrade choice                             |
225// +-----------------------------------------------------------------------+
226
227$tables = get_tables();
228$columns_of = get_columns_of($tables);
229
230// find the current release
231if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
232{
233  // we're in branch 1.3, important upgrade, isn't it?
234  if (in_array(PREFIX_TABLE.'user_category', $tables))
235  {
236    $current_release = '1.3.1';
237  }
238  else
239  {
240    $current_release = '1.3.0';
241  }
242}
243else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
244{
245  $current_release = '1.4.0';
246}
247else if (!in_array(PREFIX_TABLE.'tags', $tables))
248{
249  $current_release = '1.5.0';
250}
251else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
252{
253  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
254  {
255    $current_release = '1.6.0';
256  }
257  else
258  {
259    $current_release = '1.6.2';
260  }
261}
262else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
263{
264  $current_release = '1.7.0';
265}
266else if (!in_array(PREFIX_TABLE.'themes', $tables))
267{
268  $current_release = '2.0.0';
269}
270else
271{
272  die('No upgrade required, the database structure is up to date');
273}
274
275// +-----------------------------------------------------------------------+
276// |                            upgrade launch                             |
277// +-----------------------------------------------------------------------+
278$page['infos'] = array();
279$page['errors'] = array();
280$mysql_changes = array();
281
282if (isset($_POST['username']) and isset($_POST['password']))
283{
284  check_upgrade_access_rights($current_release, $_POST['username'], $_POST['password']);
285}
286
287if (isset($_POST['submit']) and check_upgrade())
288{
289  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
290  if (is_file($upgrade_file))
291  {
292    $page['upgrade_start'] = get_moment();
293    $conf['die_on_sql_error'] = false;
294    include($upgrade_file);
295
296    // Something to add in database.inc.php?
297    if (!empty($mysql_changes))
298    {
299      $config_file_contents = 
300        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
301        . implode("\r\n" , $mysql_changes) . "\r\n"
302        . substr($config_file_contents, $php_end_tag);
303
304      if (!@file_put_contents($config_file, $config_file_contents))
305      {
306        array_push(
307          $page['infos'],
308          sprintf(
309            l10n('In <i>%s</i>, before <b>?></b>, insert:'),
310            'local/config/database.inc.php'
311            )
312          .'<p><textarea rows="4" cols="40">'
313          .implode("\r\n" , $mysql_changes).'</textarea></p>'
314          );
315      }
316    }
317
318    // Plugins deactivation
319    if (in_array(PREFIX_TABLE.'plugins', $tables))
320    {
321      deactivate_non_standard_plugins();
322    }
323
324    $page['upgrade_end'] = get_moment();
325
326    $template->assign(
327      'upgrade',
328      array(
329        'VERSION' => $current_release,
330        'TOTAL_TIME' => get_elapsed_time(
331          $page['upgrade_start'],
332          $page['upgrade_end']
333          ),
334        'SQL_TIME' => number_format(
335          $page['queries_time'],
336          3,
337          '.',
338          ' '
339          ).' s',
340        'NB_QUERIES' => $page['count_queries']
341        )
342      );
343
344    array_push($page['infos'],
345      l10n('Perform a maintenance check in [Administration>Specials>Maintenance] if you encounter any problem.')
346      );
347
348    // Save $page['infos'] in order to restore after maintenance actions
349    $page['infos_sav'] = $page['infos'];
350    $page['infos'] = array();
351
352    // c13y_upgrade plugin means "check integrity after upgrade", so it
353    // becomes useful just after an upgrade
354    $query = '
355REPLACE INTO '.PLUGINS_TABLE.'
356  (id, state)
357  VALUES (\'c13y_upgrade\', \'active\')
358;';
359    pwg_query($query);
360
361    // Delete cache data
362    invalidate_user_cache(true);
363    $template->delete_compiled_templates();
364
365    // Tables Maintenance
366    do_maintenance_all_tables();
367
368    // Restore $page['infos'] in order to hide informations messages from functions calles
369    // errors messages are not hide
370    $page['infos'] = $page['infos_sav'];
371
372  }
373}
374
375// +-----------------------------------------------------------------------+
376// |                          start template output                        |
377// +-----------------------------------------------------------------------+
378else
379{
380  if (!defined('PWG_CHARSET'))
381  {
382    define('PWG_CHARSET', 'utf-8');
383  }
384
385  include_once(PHPWG_ROOT_PATH.'admin/include/languages.class.php');
386  $languages = new languages();
387 
388  foreach ($languages->fs_languages as $language_code => $language_name)
389  {
390    if ($language == $language_code)
391    {
392      $template->assign('language_selection', $language_code);
393    }
394    $languages_options[$language_code] = $language_name;
395  }
396  $template->assign('language_options', $languages_options);
397
398  $template->assign('introduction', array(
399    'CURRENT_RELEASE' => $current_release,
400    'F_ACTION' => 'upgrade.php?language=' . $language));
401
402  if (!check_upgrade())
403  {
404    $template->assign('login', true);
405  }
406}
407
408if (count($page['errors']) != 0)
409{
410  $template->assign('errors', $page['errors']);
411}
412
413if (count($page['infos']) != 0)
414{
415  $template->assign('infos', $page['infos']);
416}
417
418// +-----------------------------------------------------------------------+
419// |                          sending html code                            |
420// +-----------------------------------------------------------------------+
421
422$template->pparse('upgrade');
423?>
Note: See TracBrowser for help on using the repository browser.