source: branches/2.1/upgrade.php @ 10485

Last change on this file since 10485 was 6364, checked in by plg, 14 years ago

remove all svn:mergeinfo properties

  • Property svn:eol-style set to LF
File size: 12.6 KB
RevLine 
[1927]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1927]23
24define('PHPWG_ROOT_PATH', './');
25
[2863]26// load config file
[5213]27$config_file = PHPWG_ROOT_PATH.'local/config/database.inc.php';
[2863]28$config_file_contents = @file_get_contents($config_file);
29if ($config_file_contents === false)
[2836]30{
[2863]31  die('Cannot load '.$config_file);
[2836]32}
[2863]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}
[2836]38
[5213]39include(PHPWG_ROOT_PATH.'local/config/database.inc.php');
[1927]40include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
[5215]41@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
[1927]42
[5387]43// $conf is not used for users tables - define cannot be re-defined
44define('USERS_TABLE', $prefixeTable.'users');
[1927]45include_once(PHPWG_ROOT_PATH.'include/constants.php');
46define('PREFIX_TABLE', $prefixeTable);
[5982]47define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
[1927]48
[6110]49include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
50include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
51
[1927]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();
[2290]64
[1927]65  $query = '
66SHOW TABLES
67;';
[2884]68  $result = pwg_query($query);
[1927]69
[4325]70  while ($row = pwg_db_fetch_row($result))
[1927]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;';
[2884]95    $result = pwg_query($query);
[1927]96
97    $columns_of[$table] = array();
98
[4325]99    while ($row = pwg_db_fetch_row($result))
[1927]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;
[2290]113
[1927]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// +-----------------------------------------------------------------------+
[2819]135// |                             language                                  |
136// +-----------------------------------------------------------------------+
[5387]137include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
138$languages = new languages('utf-8');
139
[2819]140if (isset($_GET['language']))
141{
142  $language = strip_tags($_GET['language']);
143}
144else
145{
146  $language = 'en_UK';
147  // Try to get browser language
[5387]148  foreach ($languages->fs_languages as $language_code => $language_name)
[2819]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
[3203]158if ('fr_FR' == $language) {
159  define('PHPWG_DOMAIN', 'fr.piwigo.org');
160}
[5234]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}
[4816]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}
[5234]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}
[6152]182else if ('nl_NL' == $language) {
183  define('PHPWG_DOMAIN', 'nl.piwigo.org');
184}
[3203]185else {
186  define('PHPWG_DOMAIN', 'piwigo.org');
187}
188define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
189
[2836]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) );
[3203]192load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[2836]193load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[2819]194
[3203]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
[2819]201// +-----------------------------------------------------------------------+
[5387]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
[5982]210list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
211define('CURRENT_DATE', $dbnow);
212
[5387]213// +-----------------------------------------------------------------------+
[1927]214// |                        template initialization                        |
215// +-----------------------------------------------------------------------+
216
[3203]217include( PHPWG_ROOT_PATH .'include/template.class.php');
[5123]218$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
[1927]219$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
[3203]220$template->assign(array(
221  'RELEASE' => PHPWG_VERSION,
[5207]222  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
[3203]223  )
224);
[1927]225
226// +-----------------------------------------------------------------------+
227// |                            upgrade choice                             |
228// +-----------------------------------------------------------------------+
229
[1999]230$tables = get_tables();
231$columns_of = get_columns_of($tables);
232
[2836]233// find the current release
234if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
[1927]235{
[2836]236  // we're in branch 1.3, important upgrade, isn't it?
237  if (in_array(PREFIX_TABLE.'user_category', $tables))
[1927]238  {
[2836]239    $current_release = '1.3.1';
[1927]240  }
[2836]241  else
[1927]242  {
[2836]243    $current_release = '1.3.0';
[1927]244  }
[2836]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}
[2862]254else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
[2836]255{
256  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
[1927]257  {
[2836]258    $current_release = '1.6.0';
[1927]259  }
260  else
261  {
[2836]262    $current_release = '1.6.2';
[1927]263  }
264}
[2836]265else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
266{
267  $current_release = '1.7.0';
268}
[5982]269else if (!in_array(PREFIX_TABLE.'themes', $tables))
270{
271  $current_release = '2.0.0';
272}
[2836]273else
274{
275  die('No upgrade required, the database structure is up to date');
276}
[1927]277
278// +-----------------------------------------------------------------------+
279// |                            upgrade launch                             |
280// +-----------------------------------------------------------------------+
[2836]281$page['infos'] = array();
282$page['errors'] = array();
[2863]283$mysql_changes = array();
[1927]284
[6110]285check_upgrade_access_rights();
[2254]286
[6110]287if ((isset($_POST['submit']) or isset($_GET['now']))
288  and check_upgrade())
[2836]289{
290  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
[1927]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
[5213]297    // Something to add in database.inc.php?
[2863]298    if (!empty($mysql_changes))
299    {
300      $config_file_contents = 
301        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
[2865]302        . implode("\r\n" , $mysql_changes) . "\r\n"
[2863]303        . substr($config_file_contents, $php_end_tag);
304
305      if (!@file_put_contents($config_file, $config_file_contents))
306      {
[5982]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          );
[2863]316      }
317    }
318
[2787]319    // Plugins deactivation
320    if (in_array(PREFIX_TABLE.'plugins', $tables))
321    {
[2815]322      deactivate_non_standard_plugins();
[2787]323    }
324
[1927]325    $page['upgrade_end'] = get_moment();
326
[2254]327    $template->assign(
[1927]328      'upgrade',
329      array(
[2836]330        'VERSION' => $current_release,
[1927]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
[2819]345    array_push($page['infos'],
[6327]346      l10n('Perform a maintenance check in [Administration>Tools>Maintenance] if you encounter any problem.')
[1927]347      );
348
[3072]349    // Save $page['infos'] in order to restore after maintenance actions
350    $page['infos_sav'] = $page['infos'];
351    $page['infos'] = array();
352
[2812]353    // c13y_upgrade plugin means "check integrity after upgrade", so it
354    // becomes useful just after an upgrade
[2808]355    $query = '
356REPLACE INTO '.PLUGINS_TABLE.'
357  (id, state)
358  VALUES (\'c13y_upgrade\', \'active\')
359;';
360    pwg_query($query);
[2890]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
[3072]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
[1927]373  }
[2836]374}
375
376// +-----------------------------------------------------------------------+
377// |                          start template output                        |
378// +-----------------------------------------------------------------------+
379else
380{
[5982]381  if (!defined('PWG_CHARSET'))
[1927]382  {
[5982]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  {
[2836]391    if ($language == $language_code)
392    {
393      $template->assign('language_selection', $language_code);
394    }
395    $languages_options[$language_code] = $language_name;
[1927]396  }
[2836]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  }
[1927]407}
408
[2836]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
[1927]419// +-----------------------------------------------------------------------+
420// |                          sending html code                            |
421// +-----------------------------------------------------------------------+
422
423$template->pparse('upgrade');
424?>
Note: See TracBrowser for help on using the repository browser.