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

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

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

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