aboutsummaryrefslogtreecommitdiffstats
path: root/install/upgrade_1.5.0.php
blob: 2f69ffe107bd40dfd8933208ae9b5c2f66b0525b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch        : BSF (Best So Far)
// | file          : $RCSfile$
// | last update   : $Date: 2005-10-23 23:02:21 +0200 (dim, 23 oct 2005) $
// | last modifier : $Author: plg $
// | revision      : $Revision: 911 $
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

if (!defined('PHPWG_ROOT_PATH'))
{
  die ('This page cannot be loaded directly, load upgrade.php');
}
else
{
  if (!defined('PHPWG_IN_UPGRADE') or !PHPWG_IN_UPGRADE)
  {
    die ('Hacking attempt!');
  }
}

/**
 * replace old style #images.keywords by #tags. Requires a big data
 * migration.
 *
 * @return void
 */
function tag_replace_keywords()
{
  // code taken from upgrades 19 and 22
  
  $query = '
CREATE TABLE '.PREFIX_TABLE.'tags (
  id smallint(5) UNSIGNED NOT NULL auto_increment,
  name varchar(255) BINARY NOT NULL,
  url_name varchar(255) BINARY NOT NULL,
  PRIMARY KEY (id)
)
;';
  pwg_query($query);
  
  $query = '
CREATE TABLE '.PREFIX_TABLE.'image_tag (
  image_id mediumint(8) UNSIGNED NOT NULL,
  tag_id smallint(5) UNSIGNED NOT NULL,
  PRIMARY KEY (image_id,tag_id)
)
;';
  pwg_query($query);
  
  //
  // Move keywords to tags
  //

  // each tag label is associated to a numeric identifier
  $tag_id = array();
  // to each tag id (key) a list of image ids (value) is associated
  $tag_images = array();

  $current_id = 1;

  $query = '
SELECT id, keywords
  FROM '.PREFIX_TABLE.'images
  WHERE keywords IS NOT NULL
;';
  $result = pwg_query($query);
  while ($row = mysql_fetch_array($result))
  {
    foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
    {
      if (!isset($tag_id[$keyword]))
      {
        $tag_id[$keyword] = $current_id++;
      }

      if (!isset($tag_images[ $tag_id[$keyword] ]))
      {
        $tag_images[ $tag_id[$keyword] ] = array();
      }

      array_push(
        $tag_images[ $tag_id[$keyword] ],
        $row['id']
        );
    }
  }

  $datas = array();
  foreach ($tag_id as $tag_name => $tag_id)
  {
    array_push(
      $datas,
      array(
        'id'       => $tag_id,
        'name'     => $tag_name,
        'url_name' => str2url($tag_name),
        )
      );
  }
  
  if (!empty($datas))
  {
    mass_inserts(
      PREFIX_TABLE.'tags',
      array_keys($datas[0]),
      $datas
      );
  }

  $datas = array();
  foreach ($tag_images as $tag_id => $images)
  {
    foreach (array_unique($images) as $image_id)
    {
      array_push(
        $datas,
        array(
          'tag_id'   => $tag_id,
          'image_id' => $image_id,
          )
        );
    }
  }
  
  if (!empty($datas))
  {
    mass_inserts(
      PREFIX_TABLE.'image_tag',
      array_keys($datas[0]),
      $datas
      );
  }

  //
  // Delete images.keywords
  //
  $query = '
ALTER TABLE '.PREFIX_TABLE.'images DROP COLUMN keywords
;';
  pwg_query($query);

  //
  // Add useful indexes
  //
  $query = '
ALTER TABLE '.PREFIX_TABLE.'tags
  ADD INDEX tags_i1(url_name)
;';
  pwg_query($query);


  $query = '
ALTER TABLE '.PREFIX_TABLE.'image_tag
  ADD INDEX image_tag_i1(tag_id)
;';
  pwg_query($query);

  // print_time('tags have replaced keywords');
}

tag_replace_keywords();

$queries = array(
  "
CREATE TABLE ".PREFIX_TABLE."search (
  id int UNSIGNED NOT NULL AUTO_INCREMENT,
  last_seen date DEFAULT NULL,
  rules text,
  PRIMARY KEY  (id)
);",

  "
CREATE TABLE ".PREFIX_TABLE."user_mail_notification (
  user_id smallint(5) NOT NULL default '0',
  check_key varchar(16) binary NOT NULL default '',
  enabled enum('true','false') NOT NULL default 'false',
  last_send datetime default NULL,
  PRIMARY KEY  (user_id),
  UNIQUE KEY uidx_check_key (check_key)
);",

  "
CREATE TABLE ".PREFIX_TABLE."upgrade (
  id varchar(20) NOT NULL default '',
  applied datetime NOT NULL default '0000-00-00 00:00:00',
  description varchar(255) default NULL,
  PRIMARY KEY  (`id`)
);",

  "
ALTER TABLE ".PREFIX_TABLE."config
  MODIFY COLUMN value TEXT
;",

  "
ALTER TABLE ".PREFIX_TABLE."images
  ADD COLUMN has_high enum('true') default NULL
;",

  "
ALTER TABLE ".PREFIX_TABLE."rate
  ADD COLUMN anonymous_id varchar(45) NOT NULL default ''
;",
  "
ALTER TABLE ".PREFIX_TABLE."rate
  ADD COLUMN date date NOT NULL default '0000-00-00'
;",
  "
ALTER TABLE ".PREFIX_TABLE."rate
  DROP PRIMARY KEY
;",
  "
ALTER TABLE ".PREFIX_TABLE."rate
  ADD PRIMARY KEY (element_id,user_id,anonymous_id)
;",
  "
UPDATE ".PREFIX_TABLE."rate
  SET date = CURDATE()
;",
  
  "
DELETE
  FROM ".PREFIX_TABLE."sessions
;",
  "
ALTER TABLE ".PREFIX_TABLE."sessions
  DROP COLUMN user_id
;",
  "
ALTER TABLE ".PREFIX_TABLE."sessions
  ADD COLUMN data text NOT NULL
;",
  
  "
ALTER TABLE ".PREFIX_TABLE."user_cache
  ADD COLUMN nb_total_images mediumint(8) unsigned default NULL
;",
  
  "
ALTER TABLE ".PREFIX_TABLE."user_infos
  CHANGE COLUMN status
     status enum('webmaster','admin','normal','generic','guest')
     NOT NULL default 'guest'
;",
  "
UPDATE ".PREFIX_TABLE."user_infos
  SET status = 'normal'
  WHERE status = 'guest'
;",
  "
UPDATE ".PREFIX_TABLE."user_infos
  SET status = 'guest'
  WHERE user_id = ".$conf['guest_id']."
;",
  "
UPDATE ".PREFIX_TABLE."user_infos
  SET status = 'webmaster'
  WHERE user_id = ".$conf['webmaster_id']."
;",

  "
ALTER TABLE ".PREFIX_TABLE."user_infos
   CHANGE COLUMN template template varchar(255) NOT NULL default 'yoga/clear'
;",

  "
UPDATE ".PREFIX_TABLE."user_infos
  SET template = 'yoga/dark'
  WHERE template = 'yoga-dark'
;",
  "
UPDATE ".PREFIX_TABLE."user_infos
  SET template = 'yoga/clear'
  WHERE template != 'yoga/dark'
;",
  "
ALTER TABLE ".PREFIX_TABLE."user_infos
  ADD COLUMN adviser enum('true','false') NOT NULL default 'false'
;",
  "
ALTER TABLE ".PREFIX_TABLE."user_infos
  ADD COLUMN enabled_high enum('true','false') NOT NULL default 'true'
;",
  "
ALTER TABLE ".PREFIX_TABLE."categories
  CHANGE COLUMN rank rank SMALLINT(5) UNSIGNED DEFAULT NULL
;",
  // configuration table
  "
UPDATE ".PREFIX_TABLE."config
  SET value = 'yoga/clear'
  WHERE param = 'default_template'
;"
  );

foreach ($queries as $query)
{
  pwg_query($query);
}

//
// Move rate, rate_anonymous and gallery_url from config file to database
//
$params = array(
  'gallery_url' => array(
    'http://demo.phpwebgallery.net',
    'URL given in RSS feed'
    ),
  'rate' => array(
    'true',
    'Rating pictures feature is enabled'
    ),
  'rate_anonymous' => array(
    'true',
    'Rating pictures feature is also enabled for visitors'
    )
  );
// Get real values from config file
$conf_save = $conf;
unset($conf);
@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
if ( isset($conf['gallery_url']) )
{
  $params['gallery_url'][0] = $conf['gallery_url'];
}
if ( isset($conf['rate']) and is_bool($conf['rate']) )
{
  $params['rate'][0] = $conf['rate'] ? 'true' : 'false';
}
if ( isset($conf['rate_anonymous']) and is_bool($conf['rate_anonymous']) )
{
  $params['rate_anonymous'][0] = $conf['rate_anonymous'] ? 'true' : 'false';
}
$conf = $conf_save;

// Do I already have them in DB ?
$query = 'SELECT param FROM '.PREFIX_TABLE.'config';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
  unset( $params[ $row['param'] ] );
}

// Perform the insert query
foreach ($params as $param_key => $param_values)
{
  $query = '
INSERT INTO '.PREFIX_TABLE.'config
  (param,value,comment)
  VALUES
 ('."'$param_key','$param_values[0]','$param_values[1]')
;";
  pwg_query($query);
}

$query = "
ALTER TABLE ".PREFIX_TABLE."config MODIFY COLUMN `value` TEXT;";
pwg_query($query);


//
// replace gallery_description by page_banner
//
$query = '
SELECT value
  FROM '.PREFIX_TABLE.'config
  WHERE param=\'gallery_title\'
;';
list($t) = array_from_query($query, 'value');

$query = '
SELECT value
  FROM '.PREFIX_TABLE.'config
  WHERE param=\'gallery_description\'
;';
list($d) = array_from_query($query, 'value');

$page_banner='<h1>'.$t.'</h1><p>'.$d.'</p>';
$page_banner=addslashes($page_banner);
$query = '
INSERT INTO '.PREFIX_TABLE.'config
  (param,value,comment)
  VALUES
  (
    \'page_banner\',
    \''.$page_banner.'\',
    \'html displayed on the top each page of your gallery\'
  )
;';
pwg_query($query);

$query = '
DELETE FROM '.PREFIX_TABLE.'config
  WHERE param=\'gallery_description\'
;';
pwg_query($query);

//
// configuration for notification by mail
//
$query = "
INSERT INTO ".CONFIG_TABLE."
  (param,value,comment)
  VALUES
  (
    'nbm_send_mail_as',
    '',
    'Send mail as param value for notification by mail'
  ),
  (
    'nbm_send_detailed_content',
    'true',
    'Send detailed content for notification by mail'
  ),
  (
    'nbm_complementary_mail_content',
    '',
    'Complementary mail content for notification by mail'
  )
;";
pwg_query($query);

// depending on the way the 1.5.0 was installed (from scratch or by upgrade)
// the database structure has small differences that should be corrected.

$query = '
ALTER TABLE '.PREFIX_TABLE.'users
  CHANGE COLUMN password password varchar(32) default NULL
;';
pwg_query($query);

$to_keep = array('id', 'username', 'password', 'mail_address');
  
$query = '
DESC '.PREFIX_TABLE.'users
;';

$result = pwg_query($query);

while ($row = mysql_fetch_array($result))
{
  if (!in_array($row['Field'], $to_keep))
  {
    $query = '
ALTER TABLE '.PREFIX_TABLE.'users
  DROP COLUMN '.$row['Field'].'
;';
    pwg_query($query);
  }
}

// now we upgrade from 1.6.0 to 1.6.2
include_once(PHPWG_ROOT_PATH.'install/upgrade_1.6.0.php');
?>