source: branches/1.7/install/upgrade_1.5.0.php @ 9550

Last change on this file since 9550 was 1932, checked in by rub, 18 years ago

o add missing $lang
o use of l10n_dec
o normalize file header

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