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

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

improvement: tags replace keywords. Better data model, less
limitations. Each image can be associated to as many tag as needed. Tags can
contain non ASCII characters. Oriented navigation with tags by association.

File size: 4.7 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-09-21 00:04:57 +0200 (mer, 21 sep 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 870 $
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('Hacking attempt!');
31}
32
33$upgrade_description = '#images.keywords moved to new table #tags';
34
35// +-----------------------------------------------------------------------+
36// |                              New tables                               |
37// +-----------------------------------------------------------------------+
38
39$query = '
40CREATE TABLE '.PREFIX_TABLE.'tags (
41  id smallint(5) UNSIGNED NOT NULL auto_increment,
42  name varchar(255) BINARY NOT NULL,
43  url_name varchar(255) BINARY NOT NULL,
44  PRIMARY KEY (id)
45) TYPE=MyISAM
46;';
47pwg_query($query);
48
49$query = '
50CREATE TABLE '.PREFIX_TABLE.'image_tag (
51  image_id mediumint(8) UNSIGNED NOT NULL,
52  tag_id smallint(5) UNSIGNED NOT NULL,
53  PRIMARY KEY (image_id,tag_id)
54) TYPE=MyISAM
55;';
56pwg_query($query);
57
58// +-----------------------------------------------------------------------+
59// |                        Move keywords to tags                          |
60// +-----------------------------------------------------------------------+
61
62// each tag label is associated to a numeric identifier
63$tag_id = array();
64// to each tag id (key) a list of image ids (value) is associated
65$tag_images = array();
66
67$current_id = 1;
68
69$query = '
70SELECT id, keywords
71  FROM '.PREFIX_TABLE.'images
72  WHERE keywords IS NOT NULL
73;';
74$result = pwg_query($query);
75while ($row = mysql_fetch_array($result))
76{
77  foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
78  {
79    if (!isset($tag_id[$keyword]))
80    {
81      $tag_id[$keyword] = $current_id++;
82    }
83   
84    if (!isset($tag_images[ $tag_id[$keyword] ]))
85    {
86      $tag_images[ $tag_id[$keyword] ] = array();
87    }
88
89    array_push($tag_images[ $tag_id[$keyword] ], $row['id']);
90  }
91}
92
93$datas = array();
94foreach ($tag_id as $tag_name => $tag_id)
95{
96  array_push(
97    $datas,
98    array(
99      'id'       => $tag_id,
100      'name'     => $tag_name,
101      'url_name' => str2url($tag_name),
102      )
103    );
104}
105mass_inserts(
106  PREFIX_TABLE.'tags',
107  array_keys($datas[0]),
108  $datas
109  );
110
111$datas = array();
112foreach ($tag_images as $tag_id => $images)
113{
114  foreach (array_unique($images) as $image_id)
115  {
116    array_push(
117      $datas,
118      array(
119        'tag_id'   => $tag_id,
120        'image_id' => $image_id,
121        )
122      );
123  }
124}
125
126mass_inserts(
127  PREFIX_TABLE.'image_tag',
128  array_keys($datas[0]),
129  $datas
130  );
131
132// +-----------------------------------------------------------------------+
133// |                         Delete images.keywords                        |
134// +-----------------------------------------------------------------------+
135
136$query = '
137ALTER TABLE '.PREFIX_TABLE.'images DROP COLUMN keywords
138;';
139pwg_query($query);
140
141// +-----------------------------------------------------------------------+
142// |                           End notification                            |
143// +-----------------------------------------------------------------------+
144
145echo
146"\n"
147.'Table '.PREFIX_TABLE.'tags created and filled'."\n"
148.'Table '.PREFIX_TABLE.'image_tag created and filled'."\n"
149.'Column '.PREFIX_TABLE.'images.keywords dropped'."\n"
150;
151?>
Note: See TracBrowser for help on using the repository browser.