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

Last change on this file since 25018 was 25018, checked in by mistic100, 10 years ago

remove all array_push (50% slower than []) + some changes missing for feature:2978

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