source: branches/2.3/upgrade.php @ 12738

Last change on this file since 12738 was 12296, checked in by plg, 13 years ago

feature 2452 added: upgrade script from 2.2 to 2.3

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