source: tags/release-1_6_0RC1/install/upgrade_1.5.0.php @ 27558

Last change on this file since 27558 was 1209, checked in by plg, 18 years ago

bug fixed: during installation, upgrades from install/db with complex
identifier (X.x) were not correctly inserted.

bug fixed: available upgrades from install/db need to be inserted in
#upgrade table.

deletion: all upgrades from install/db coming from trunk are removed.

new: complete upgrade from any previous release from 1.3.0 to 1.5.2 with
automatic detection detection of the previous release.

File size: 10.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-10-23 23:02:21 +0200 (dim, 23 oct 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 911 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28if (!defined('PHPWG_ROOT_PATH'))
29{
30  die ('This page cannot be loaded directly, load upgrade.php');
31}
32else
33{
34  if (!defined('PHPWG_IN_UPGRADE') or !PHPWG_IN_UPGRADE)
35  {
36    die ('Hacking attempt!');
37  }
38}
39
40/**
41 * replace old style #images.keywords by #tags. Requires a big data
42 * migration.
43 *
44 * @return void
45 */
46function tag_replace_keywords()
47{
48  // code taken from upgrades 19 and 22
49 
50  $query = '
51CREATE TABLE '.PREFIX_TABLE.'tags (
52  id smallint(5) UNSIGNED NOT NULL auto_increment,
53  name varchar(255) BINARY NOT NULL,
54  url_name varchar(255) BINARY NOT NULL,
55  PRIMARY KEY (id)
56)
57;';
58  pwg_query($query);
59 
60  $query = '
61CREATE TABLE '.PREFIX_TABLE.'image_tag (
62  image_id mediumint(8) UNSIGNED NOT NULL,
63  tag_id smallint(5) UNSIGNED NOT NULL,
64  PRIMARY KEY (image_id,tag_id)
65)
66;';
67  pwg_query($query);
68 
69  //
70  // Move keywords to tags
71  //
72
73  // each tag label is associated to a numeric identifier
74  $tag_id = array();
75  // to each tag id (key) a list of image ids (value) is associated
76  $tag_images = array();
77
78  $current_id = 1;
79
80  $query = '
81SELECT id, keywords
82  FROM '.PREFIX_TABLE.'images
83  WHERE keywords IS NOT NULL
84;';
85  $result = pwg_query($query);
86  while ($row = mysql_fetch_array($result))
87  {
88    foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
89    {
90      if (!isset($tag_id[$keyword]))
91      {
92        $tag_id[$keyword] = $current_id++;
93      }
94
95      if (!isset($tag_images[ $tag_id[$keyword] ]))
96      {
97        $tag_images[ $tag_id[$keyword] ] = array();
98      }
99
100      array_push(
101        $tag_images[ $tag_id[$keyword] ],
102        $row['id']
103        );
104    }
105  }
106
107  $datas = array();
108  foreach ($tag_id as $tag_name => $tag_id)
109  {
110    array_push(
111      $datas,
112      array(
113        'id'       => $tag_id,
114        'name'     => $tag_name,
115        'url_name' => str2url($tag_name),
116        )
117      );
118  }
119 
120  if (!empty($datas))
121  {
122    mass_inserts(
123      PREFIX_TABLE.'tags',
124      array_keys($datas[0]),
125      $datas
126      );
127  }
128
129  $datas = array();
130  foreach ($tag_images as $tag_id => $images)
131  {
132    foreach (array_unique($images) as $image_id)
133    {
134      array_push(
135        $datas,
136        array(
137          'tag_id'   => $tag_id,
138          'image_id' => $image_id,
139          )
140        );
141    }
142  }
143 
144  if (!empty($datas))
145  {
146    mass_inserts(
147      PREFIX_TABLE.'image_tag',
148      array_keys($datas[0]),
149      $datas
150      );
151  }
152
153  //
154  // Delete images.keywords
155  //
156  $query = '
157ALTER TABLE '.PREFIX_TABLE.'images DROP COLUMN keywords
158;';
159  pwg_query($query);
160
161  //
162  // Add useful indexes
163  //
164  $query = '
165ALTER TABLE '.PREFIX_TABLE.'tags
166  ADD INDEX tags_i1(url_name)
167;';
168  pwg_query($query);
169
170
171  $query = '
172ALTER TABLE '.PREFIX_TABLE.'image_tag
173  ADD INDEX image_tag_i1(tag_id)
174;';
175  pwg_query($query);
176
177  // print_time('tags have replaced keywords');
178}
179
180tag_replace_keywords();
181
182$queries = array(
183  "
184CREATE TABLE ".PREFIX_TABLE."search (
185  id int UNSIGNED NOT NULL AUTO_INCREMENT,
186  last_seen date DEFAULT NULL,
187  rules text,
188  PRIMARY KEY  (id)
189);",
190
191  "
192CREATE TABLE ".PREFIX_TABLE."user_mail_notification (
193  user_id smallint(5) NOT NULL default '0',
194  check_key varchar(16) binary NOT NULL default '',
195  enabled enum('true','false') NOT NULL default 'false',
196  last_send datetime default NULL,
197  PRIMARY KEY  (user_id),
198  UNIQUE KEY uidx_check_key (check_key)
199);",
200
201  "
202CREATE TABLE ".PREFIX_TABLE."upgrade (
203  id varchar(20) NOT NULL default '',
204  applied datetime NOT NULL default '0000-00-00 00:00:00',
205  description varchar(255) default NULL,
206  PRIMARY KEY  (`id`)
207);",
208
209  "
210ALTER TABLE ".PREFIX_TABLE."config
211  MODIFY COLUMN value TEXT
212;",
213
214  "
215ALTER TABLE ".PREFIX_TABLE."images
216  ADD COLUMN has_high enum('true') default NULL
217;",
218
219  "
220ALTER TABLE ".PREFIX_TABLE."rate
221  ADD COLUMN anonymous_id varchar(45) NOT NULL default ''
222;",
223  "
224ALTER TABLE ".PREFIX_TABLE."rate
225  ADD COLUMN date date NOT NULL default '0000-00-00'
226;",
227  "
228ALTER TABLE ".PREFIX_TABLE."rate
229  DROP PRIMARY KEY
230;",
231  "
232ALTER TABLE ".PREFIX_TABLE."rate
233  ADD PRIMARY KEY (element_id,user_id,anonymous_id)
234;",
235  "
236UPDATE ".PREFIX_TABLE."rate
237  SET date = CURDATE()
238;",
239 
240  "
241DELETE
242  FROM ".PREFIX_TABLE."sessions
243;",
244  "
245ALTER TABLE ".PREFIX_TABLE."sessions
246  DROP COLUMN user_id
247;",
248  "
249ALTER TABLE ".PREFIX_TABLE."sessions
250  ADD COLUMN data text NOT NULL
251;",
252 
253  "
254ALTER TABLE ".PREFIX_TABLE."user_cache
255  ADD COLUMN nb_total_images mediumint(8) unsigned default NULL
256;",
257 
258  "
259ALTER TABLE ".PREFIX_TABLE."user_infos
260  CHANGE COLUMN status
261     status enum('webmaster','admin','normal','generic','guest')
262     NOT NULL default 'guest'
263;",
264  "
265UPDATE ".PREFIX_TABLE."user_infos
266  SET status = 'normal'
267  WHERE status = 'guest'
268;",
269  "
270UPDATE ".PREFIX_TABLE."user_infos
271  SET status = 'guest'
272  WHERE user_id = ".$conf['guest_id']."
273;",
274  "
275UPDATE ".PREFIX_TABLE."user_infos
276  SET status = 'webmaster'
277  WHERE user_id = ".$conf['webmaster_id']."
278;",
279
280  "
281ALTER TABLE ".PREFIX_TABLE."user_infos
282   CHANGE COLUMN template template varchar(255) NOT NULL default 'yoga/clear'
283;",
284
285  "
286UPDATE ".PREFIX_TABLE."user_infos
287  SET template = 'yoga/dark'
288  WHERE template = 'yoga-dark'
289;",
290  "
291UPDATE ".PREFIX_TABLE."user_infos
292  SET template = 'yoga/clear'
293  WHERE template != 'yoga/dark'
294;",
295  "
296ALTER TABLE ".PREFIX_TABLE."user_infos
297  ADD COLUMN adviser enum('true','false') NOT NULL default 'false'
298;",
299  "
300ALTER TABLE ".PREFIX_TABLE."user_infos
301  ADD COLUMN enabled_high enum('true','false') NOT NULL default 'true'
302;",
303
304  // configuration table
305  "
306UPDATE ".PREFIX_TABLE."config
307  SET value = 'yoga/clear'
308  WHERE param = 'default_template'
309;"
310  );
311
312foreach ($queries as $query)
313{
314  pwg_query($query);
315}
316
317//
318// Move rate, rate_anonymous and gallery_url from config file to database
319//
320$params = array(
321  'gallery_url' => array(
322    'http://demo.phpwebgallery.net',
323    'URL given in RSS feed'
324    ),
325  'rate' => array(
326    'true',
327    'Rating pictures feature is enabled'
328    ),
329  'rate_anonymous' => array(
330    'true',
331    'Rating pictures feature is also enabled for visitors'
332    )
333  );
334// Get real values from config file
335$conf_save = $conf;
336unset($conf);
337@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
338if ( isset($conf['gallery_url']) )
339{
340  $params['gallery_url'][0] = $conf['gallery_url'];
341}
342if ( isset($conf['rate']) and is_bool($conf['rate']) )
343{
344  $params['rate'][0] = $conf['rate'] ? 'true' : 'false';
345}
346if ( isset($conf['rate_anonymous']) and is_bool($conf['rate_anonymous']) )
347{
348  $params['rate_anonymous'][0] = $conf['rate_anonymous'] ? 'true' : 'false';
349}
350$conf = $conf_save;
351
352// Do I already have them in DB ?
353$query = 'SELECT param FROM '.PREFIX_TABLE.'config';
354$result = pwg_query($query);
355while ($row = mysql_fetch_array($result))
356{
357  unset( $params[ $row['param'] ] );
358}
359
360// Perform the insert query
361foreach ($params as $param_key => $param_values)
362{
363  $query = '
364INSERT INTO '.PREFIX_TABLE.'config
365  (param,value,comment)
366  VALUES
367 ('."'$param_key','$param_values[0]','$param_values[1]')
368;";
369  pwg_query($query);
370}
371
372$query = "
373ALTER TABLE ".PREFIX_TABLE."config MODIFY COLUMN `value` TEXT;";
374pwg_query($query);
375
376
377//
378// replace gallery_description by page_banner
379//
380$query = '
381SELECT value
382  FROM '.PREFIX_TABLE.'config
383  WHERE param=\'gallery_title\'
384;';
385list($t) = array_from_query($query, 'value');
386
387$query = '
388SELECT value
389  FROM '.PREFIX_TABLE.'config
390  WHERE param=\'gallery_description\'
391;';
392list($d) = array_from_query($query, 'value');
393
394$page_banner='<div id="theHeader"><h1>'.$t.'</h1><p>'.$d.'</p></div>';
395$page_banner=addslashes($page_banner);
396$query = '
397INSERT INTO '.PREFIX_TABLE.'config
398  (param,value,comment)
399  VALUES
400  (
401    \'page_banner\',
402    \''.$page_banner.'\',
403    \'html displayed on the top each page of your gallery\'
404  )
405;';
406pwg_query($query);
407
408$query = '
409DELETE FROM '.PREFIX_TABLE.'config
410  WHERE param=\'gallery_description\'
411;';
412pwg_query($query);
413
414//
415// configuration for notification by mail
416//
417$query = "
418INSERT INTO ".CONFIG_TABLE."
419  (param,value,comment)
420  VALUES
421  (
422    'nbm_send_mail_as',
423    '',
424    'Send mail as param value for notification by mail'
425  ),
426  (
427    'nbm_send_detailed_content',
428    'true',
429    'Send detailed content for notification by mail'
430  ),
431  (
432    'nbm_complementary_mail_content',
433    '',
434    'Complementary mail content for notification by mail'
435  )
436;
437";
438pwg_query($query);
439
440// depending on the way the 1.5.0 was installed (from scratch or by upgrade)
441// the database structure has small differences that should be corrected.
442
443$query = '
444ALTER TABLE '.PREFIX_TABLE.'users
445  CHANGE COLUMN password password varchar(32) default NULL
446;';
447pwg_query($query);
448
449$to_keep = array('id', 'username', 'password', 'mail_address');
450 
451$query = '
452DESC '.PREFIX_TABLE.'users
453;';
454
455$result = pwg_query($query);
456
457while ($row = mysql_fetch_array($result))
458{
459  if (!in_array($row['Field'], $to_keep))
460  {
461    $query = '
462ALTER TABLE '.PREFIX_TABLE.'users
463  DROP COLUMN '.$row['Field'].'
464;';
465    pwg_query($query);
466  }
467}
468
469?>
Note: See TracBrowser for help on using the repository browser.