source: trunk/install/db/19-database.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | branch        : BSF (Best So Far)
29// | file          : $RCSfile$
30// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
31// | last modifier : $Author: plg $
32// | revision      : $Revision: 2297 $
33// +-----------------------------------------------------------------------+
34// | This program is free software; you can redistribute it and/or modify  |
35// | it under the terms of the GNU General Public License as published by  |
36// | the Free Software Foundation                                          |
37// |                                                                       |
38// | This program is distributed in the hope that it will be useful, but   |
39// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
40// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
41// | General Public License for more details.                              |
42// |                                                                       |
43// | You should have received a copy of the GNU General Public License     |
44// | along with this program; if not, write to the Free Software           |
45// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
46// | USA.                                                                  |
47// +-----------------------------------------------------------------------+
48
49if (!defined('PHPWG_ROOT_PATH'))
50{
51  die('Hacking attempt!');
52}
53
54$upgrade_description = '#images.keywords moved to new table #tags';
55
56// +-----------------------------------------------------------------------+
57// |                              New tables                               |
58// +-----------------------------------------------------------------------+
59
60$query = '
61CREATE TABLE '.PREFIX_TABLE.'tags (
62  id smallint(5) UNSIGNED NOT NULL auto_increment,
63  name varchar(255) BINARY NOT NULL,
64  url_name varchar(255) BINARY NOT NULL,
65  PRIMARY KEY (id)
66) TYPE=MyISAM
67;';
68pwg_query($query);
69
70$query = '
71CREATE TABLE '.PREFIX_TABLE.'image_tag (
72  image_id mediumint(8) UNSIGNED NOT NULL,
73  tag_id smallint(5) UNSIGNED NOT NULL,
74  PRIMARY KEY (image_id,tag_id)
75) TYPE=MyISAM
76;';
77pwg_query($query);
78
79// +-----------------------------------------------------------------------+
80// |                        Move keywords to tags                          |
81// +-----------------------------------------------------------------------+
82
83// each tag label is associated to a numeric identifier
84$tag_id = array();
85// to each tag id (key) a list of image ids (value) is associated
86$tag_images = array();
87
88$current_id = 1;
89
90$query = '
91SELECT id, keywords
92  FROM '.PREFIX_TABLE.'images
93  WHERE keywords IS NOT NULL
94;';
95$result = pwg_query($query);
96while ($row = mysql_fetch_array($result))
97{
98  foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
99  {
100    if (!isset($tag_id[$keyword]))
101    {
102      $tag_id[$keyword] = $current_id++;
103    }
104
105    if (!isset($tag_images[ $tag_id[$keyword] ]))
106    {
107      $tag_images[ $tag_id[$keyword] ] = array();
108    }
109
110    array_push($tag_images[ $tag_id[$keyword] ], $row['id']);
111  }
112}
113
114$datas = array();
115foreach ($tag_id as $tag_name => $tag_id)
116{
117  array_push(
118    $datas,
119    array(
120      'id'       => $tag_id,
121      'name'     => $tag_name,
122      'url_name' => str2url($tag_name),
123      )
124    );
125}
126if (!empty($datas))
127mass_inserts(
128  PREFIX_TABLE.'tags',
129  array_keys($datas[0]),
130  $datas
131  );
132
133$datas = array();
134foreach ($tag_images as $tag_id => $images)
135{
136  foreach (array_unique($images) as $image_id)
137  {
138    array_push(
139      $datas,
140      array(
141        'tag_id'   => $tag_id,
142        'image_id' => $image_id,
143        )
144      );
145  }
146}
147
148if (!empty($datas))
149mass_inserts(
150  PREFIX_TABLE.'image_tag',
151  array_keys($datas[0]),
152  $datas
153  );
154
155// +-----------------------------------------------------------------------+
156// |                         Delete images.keywords                        |
157// +-----------------------------------------------------------------------+
158
159$query = '
160ALTER TABLE '.PREFIX_TABLE.'images DROP COLUMN keywords
161;';
162pwg_query($query);
163
164// +-----------------------------------------------------------------------+
165// |                           End notification                            |
166// +-----------------------------------------------------------------------+
167
168echo
169"\n"
170.'Table '.PREFIX_TABLE.'tags created and filled'."\n"
171.'Table '.PREFIX_TABLE.'image_tag created and filled'."\n"
172.'Column '.PREFIX_TABLE.'images.keywords dropped'."\n"
173;
174?>
Note: See TracBrowser for help on using the repository browser.