source: trunk/include/functions_tag.inc.php @ 5196

Last change on this file since 5196 was 5196, checked in by plg, 14 years ago

increase copyright year to 2010

File size: 7.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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
25/**
26 * Tags available. Each return tag is represented as an array with its id,
27 * its name, its weight (count), its url name. Tags are not sorted.
28 *
29 * The returned list can be a subset of all existing tags due to
30 * permissions, only if a list of forbidden categories is provided
31 *
32 * @param array forbidden categories
33 * @return array
34 */
35function get_available_tags()
36{
37  // we can find top fatter tags among reachable images
38  $query = '
39SELECT tag_id, COUNT(DISTINCT(it.image_id)) counter
40  FROM '.IMAGE_CATEGORY_TABLE.' ic
41    INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF
42    (
43      array
44        (
45          'forbidden_categories' => 'category_id',
46          'visible_categories' => 'category_id',
47          'visible_images' => 'ic.image_id'
48        ),
49      '
50  WHERE'
51    ).'
52  GROUP BY tag_id';
53  $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
54
55  if ( empty($tag_counters) )
56  {
57    return array();
58  }
59
60  $query = '
61SELECT *
62  FROM '.TAGS_TABLE;
63  $result = pwg_query($query);
64  $tags = array();
65  while ($row = pwg_db_fetch_assoc($result))
66  {
67    $counter = @$tag_counters[ $row['id'] ];
68    if ( $counter )
69    {
70      $row['counter'] = $counter;
71      array_push($tags, $row);
72    }
73  }
74  return $tags;
75}
76
77/**
78 * All tags, even tags associated to no image.
79 *
80 * @return array
81 */
82function get_all_tags()
83{
84  $query = '
85SELECT *
86  FROM '.TAGS_TABLE.'
87;';
88  $result = pwg_query($query);
89  $tags = array();
90  while ($row = pwg_db_fetch_assoc($result))
91  {
92    array_push($tags, $row);
93  }
94
95  usort($tags, 'tag_alpha_compare');
96
97  return $tags;
98}
99
100/**
101 * Giving a set of tags with a counter for each one, calculate the display
102 * level of each tag.
103 *
104 * The level of each tag depends on the average count of tags. This
105 * calcylation method avoid having very different levels for tags having
106 * nearly the same count when set are small.
107 *
108 * @param array tags
109 * @return array
110 */
111function add_level_to_tags($tags)
112{
113  global $conf;
114
115  if (count($tags) == 0)
116  {
117    return $tags;
118  }
119
120  $total_count = 0;
121
122  foreach ($tags as $tag)
123  {
124    $total_count+= $tag['counter'];
125  }
126
127  // average count of available tags will determine the level of each tag
128  $tag_average_count = $total_count / count($tags);
129
130  // tag levels threshold calculation: a tag with an average rate must have
131  // the middle level.
132  for ($i = 1; $i < $conf['tags_levels']; $i++)
133  {
134    $threshold_of_level[$i] =
135      2 * $i * $tag_average_count / $conf['tags_levels'];
136  }
137
138  // display sorted tags
139  foreach (array_keys($tags) as $k)
140  {
141    $tags[$k]['level'] = 1;
142
143    // based on threshold, determine current tag level
144    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
145    {
146      if ($tags[$k]['counter'] > $threshold_of_level[$i])
147      {
148        $tags[$k]['level'] = $i + 1;
149        break;
150      }
151    }
152  }
153
154  return $tags;
155}
156
157/**
158 * return the list of image ids corresponding to given tags. AND & OR mode
159 * supported.
160 *
161 * @param array tag ids
162 * @param string mode
163 * @return array
164 */
165function get_image_ids_for_tags($tag_ids, $mode = 'AND')
166{
167  switch ($mode)
168  {
169    case 'AND':
170    {
171      // strategy is to list images associated to each tag
172      $tag_images = array();
173
174      foreach ($tag_ids as $tag_id)
175      {
176        $query = '
177SELECT image_id
178  FROM '.IMAGE_TAG_TABLE.'
179  WHERE tag_id = '.$tag_id.'
180;';
181        $tag_images[$tag_id] = array_from_query($query, 'image_id');
182      }
183
184      // then we calculate the intersection, the images that are associated to
185      // every tags
186      $items = array_shift($tag_images);
187      foreach ($tag_images as $images)
188      {
189        $items = array_intersect($items, $images);
190      }
191      return $items;
192      break;
193    }
194    case 'OR':
195    {
196      $query = '
197SELECT DISTINCT image_id
198  FROM '.IMAGE_TAG_TABLE.'
199  WHERE tag_id IN ('.implode(',', $tag_ids).')
200;';
201      return array_from_query($query, 'image_id');
202      break;
203    }
204    default:
205    {
206      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
207    }
208  }
209}
210
211/**
212 * return a list of tags corresponding to given items.
213 *
214 * @param array items
215 * @param array max_tags
216 * @param array excluded_tag_ids
217 * @return array
218 */
219function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
220{
221  if (empty($items))
222  {
223    return array();
224  }
225  $query = '
226SELECT t.*, count(*) counter
227  FROM '.IMAGE_TAG_TABLE.'
228    INNER JOIN '.TAGS_TABLE.' t ON tag_id = id
229  WHERE image_id IN ('.implode(',', $items).')';
230  if (!empty($excluded_tag_ids))
231  {
232    $query.='
233    AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
234  }
235  $query .='
236  GROUP BY tag_id, t.id, t.name, t.url_name';
237  if ($max_tags>0)
238  {
239    $query .= '
240  ORDER BY counter DESC
241  LIMIT '.$max_tags;
242  }
243
244  $result = pwg_query($query);
245  $tags = array();
246  while($row = pwg_db_fetch_assoc($result))
247  {
248    array_push($tags, $row);
249  }
250  usort($tags, 'tag_alpha_compare');
251  return $tags;
252}
253
254/**
255 * return a list of tags corresponding to any of ids, url_names, names
256 *
257 * @param array ids
258 * @param array url_names
259 * @param array names
260 * @return array
261 */
262function find_tags($ids, $url_names=array(), $names=array() )
263{
264  $where_clauses = array();
265  if ( !empty($ids) )
266  {
267    $where_clauses[] = 'id IN ('.implode(',', $ids).')';
268  }
269  if ( !empty($url_names) )
270  {
271    $where_clauses[] =
272      'url_name IN ('.
273      implode(
274        ',',
275        array_map(
276          create_function('$s', 'return "\'".$s."\'";'),
277          $url_names
278          )
279        )
280      .')';
281  }
282  if ( !empty($names) )
283  {
284    $where_clauses[] =
285      'name IN ('.
286      implode(
287        ',',
288        array_map(
289          create_function('$s', 'return "\'".$s."\'";'),
290          $names
291          )
292        )
293      .')';
294  }
295  if (empty($where_clauses))
296  {
297    return array();
298  }
299
300  $query = '
301SELECT *
302  FROM '.TAGS_TABLE.'
303  WHERE '. implode( '
304    OR ', $where_clauses);
305
306  $result = pwg_query($query);
307  $tags = array();
308  while ($row = pwg_db_fetch_assoc($result))
309  {
310    array_push($tags, $row);
311  }
312  return $tags;
313}
314?>
Note: See TracBrowser for help on using the repository browser.