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
|
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch : BSF (Best So Far)
// | file : $RCSfile$
// | last update : $Date: 2006-03-16 23:58:16 +0100 (jeu, 16 mar 2006) $
// | last modifier : $Author: rub $
// | revision : $Revision: 1085 $
// | revision : $Revision: 1085 $
// +-----------------------------------------------------------------------+
// | 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. |
// +-----------------------------------------------------------------------+
/**
* Tags available. Each return tag is represented as an array with its id,
* its name, its weight (count), its url name. Tags are not sorted.
*
* The returned list can be a subset of all existing tags due to
* permissions, only if a list of forbidden categories is provided
*
* @param array forbidden categories
* @return array
*/
function get_available_tags()
{
// we can find top fatter tags among reachable images
$tags_query = '
SELECT tag_id, name, url_name, count(*) counter
FROM '.IMAGE_TAG_TABLE.'
INNER JOIN '.TAGS_TABLE.' ON tag_id = id';
$where_tag_img =
get_sql_condition_FandF
(
array
(
'forbidden_categories' => 'category_id',
'visible_categories' => 'category_id',
'visible_images' => 'image_id'
),
'WHERE'
);
if (!is_null($where_tag_img))
{
// first we need all reachable image ids
$images_query = '
SELECT DISTINCT image_id
FROM '.IMAGE_CATEGORY_TABLE.'
'.$where_tag_img.'
;';
$image_ids = array_from_query($images_query, 'image_id');
if ( empty($image_ids) )
{
return array();
}
$tags_query.= '
WHERE image_id IN ('.
wordwrap(
implode(', ', $image_ids),
80,
"\n"
).')';
}
$tags_query.= '
GROUP BY tag_id
;';
$result = pwg_query($tags_query);
$tags = array();
while ($row = mysql_fetch_assoc($result))
{
array_push($tags, $row);
}
return $tags;
}
/**
* All tags, even tags associated to no image.
*
* @return array
*/
function get_all_tags()
{
$query = '
SELECT id AS tag_id,
name,
url_name
FROM '.TAGS_TABLE.'
;';
$result = pwg_query($query);
$tags = array();
while ($row = mysql_fetch_assoc($result))
{
array_push($tags, $row);
}
usort($tags, 'name_compare');
return $tags;
}
/**
* Giving a set of tags with a counter for each one, calculate the display
* level of each tag.
*
* The level of each tag depends on the average count of tags. This
* calcylation method avoid having very different levels for tags having
* nearly the same count when set are small.
*
* @param array tags
* @return array
*/
function add_level_to_tags($tags)
{
global $conf;
if (count($tags) == 0)
{
return $tags;
}
$total_count = 0;
foreach ($tags as $tag)
{
$total_count+= $tag['counter'];
}
// average count of available tags will determine the level of each tag
$tag_average_count = $total_count / count($tags);
// tag levels threshold calculation: a tag with an average rate must have
// the middle level.
for ($i = 1; $i < $conf['tags_levels']; $i++)
{
$threshold_of_level[$i] =
2 * $i * $tag_average_count / $conf['tags_levels'];
}
// display sorted tags
foreach (array_keys($tags) as $k)
{
$tags[$k]['level'] = 1;
// based on threshold, determine current tag level
for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
{
if ($tags[$k]['counter'] > $threshold_of_level[$i])
{
$tags[$k]['level'] = $i + 1;
break;
}
}
}
return $tags;
}
/**
* return the list of image ids corresponding to given tags. AND & OR mode
* supported.
*
* @param array tag ids
* @param string mode
* @return array
*/
function get_image_ids_for_tags($tag_ids, $mode = 'AND')
{
switch ($mode)
{
case 'AND':
{
// strategy is to list images associated to each tag
$tag_images = array();
foreach ($tag_ids as $tag_id)
{
$query = '
SELECT image_id
FROM '.IMAGE_TAG_TABLE.'
WHERE tag_id = '.$tag_id.'
;';
$tag_images[$tag_id] = array_from_query($query, 'image_id');
}
// then we calculate the intersection, the images that are associated to
// every tags
$items = array_shift($tag_images);
foreach ($tag_images as $images)
{
$items = array_intersect($items, $images);
}
return array_unique($items);
break;
}
case 'OR':
{
$query = '
SELECT DISTINCT image_id
FROM '.IMAGE_TAG_TABLE.'
WHERE tag_id IN ('.implode(',', $tag_ids).')
;';
return array_from_query($query, 'image_id');
break;
}
default:
{
die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
}
}
}
/**
* return a list of tags corresponding to given items.
*
* @param array items
* @param array max_tags
* @param array excluded_tag_ids
* @return array
*/
function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
{
if (empty($items))
{
return array();
}
$query = '
SELECT tag_id, name, url_name, count(*) counter
FROM '.IMAGE_TAG_TABLE.'
INNER JOIN '.TAGS_TABLE.' ON tag_id = id
WHERE image_id IN ('.implode(',', $items).')';
if (!empty($excluded_tag_ids))
{
$query.='
AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
}
$query .='
GROUP BY tag_id
ORDER BY counter DESC';
if ($max_tags>0)
{
$query .= '
LIMIT 0,'.$max_tags;
}
$result = pwg_query($query);
$tags = array();
while($row = mysql_fetch_assoc($result))
{
array_push($tags, $row);
}
usort($tags, 'name_compare');
return $tags;
}
?>
|