source: branches/2.1/install/upgrade_1.5.0.php @ 6410

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