source: trunk/upgrade.php @ 12785

Last change on this file since 12785 was 12768, checked in by rvelices, 13 years ago

simplification of 'startup phase' - multisite.inc.php is not required anymore / database.inc.php is included after config.

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