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

Last change on this file since 1388 was 1125, checked in by rvelices, 19 years ago

image sort order for tags

very minor code and language simplifications and corrections

File size: 4.8 KB
RevLine 
[1119]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    }
[1125]83
[1119]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}
[1125]105if (!empty($datas))
[1119]106mass_inserts(
107  PREFIX_TABLE.'tags',
108  array_keys($datas[0]),
109  $datas
110  );
111
112$datas = array();
113foreach ($tag_images as $tag_id => $images)
114{
115  foreach (array_unique($images) as $image_id)
116  {
117    array_push(
118      $datas,
119      array(
120        'tag_id'   => $tag_id,
121        'image_id' => $image_id,
122        )
123      );
124  }
125}
126
[1125]127if (!empty($datas))
[1119]128mass_inserts(
129  PREFIX_TABLE.'image_tag',
130  array_keys($datas[0]),
131  $datas
132  );
133
134// +-----------------------------------------------------------------------+
135// |                         Delete images.keywords                        |
136// +-----------------------------------------------------------------------+
137
138$query = '
139ALTER TABLE '.PREFIX_TABLE.'images DROP COLUMN keywords
140;';
141pwg_query($query);
142
143// +-----------------------------------------------------------------------+
144// |                           End notification                            |
145// +-----------------------------------------------------------------------+
146
147echo
148"\n"
149.'Table '.PREFIX_TABLE.'tags created and filled'."\n"
150.'Table '.PREFIX_TABLE.'image_tag created and filled'."\n"
151.'Column '.PREFIX_TABLE.'images.keywords dropped'."\n"
152;
153?>
Note: See TracBrowser for help on using the repository browser.