source: trunk/plog-includes/plog-tag-functions.php @ 591

Revision 591, 6.9 KB checked in by kimparsell, 3 years ago (diff)

+ More code cleanup for consistency/validation
+ Added fix for zip upload write_error from http://www.plogger.org/forum/discussion/2521/zip-upload-writeerror/ (props to sidtheduck for the fix)
+ Replaced theme preview.png files for Air, Default, and Lucid themes

Line 
1<?php
2
3if (basename($_SERVER['PHP_SELF']) == basename( __FILE__ )) {
4        // ignorance is bliss
5        exit();
6}
7
8function parse_tags($str) {
9        // 1. Compress all extra whitespaces
10        $str = preg_replace('/\Ts{2,}/', ' ', $str);
11        // 2. Extract any phrases in quotes
12        preg_match_all('/(\'|")(.*?)(\1)/', $str, $phrases);
13        // 3. Now remove the phrases from the string
14        $str = preg_replace('/(\'|")(.*?)(\1)/', '', $str);
15        // 4. Get rid of whitespaces at the end that may have been left
16        $str = trim($str);
17        // 5. Get single words
18        $words = preg_split('/\s+/', $str);
19        // 6. Merge single words and phrases
20        $tags = array_merge($phrases[2], $words);
21
22        return $tags;
23}
24
25function urlify_tag($tag) {
26        // 1. Format the incoming tag to use in a URL
27        return rawurlencode($tag);
28}
29
30function get_picture_tags($picture_id) {
31        global $config;
32        global $TABLE_PREFIX;
33        $picture_id = intval($picture_id);
34        $picture_tags = array();
35
36        // TODO: Evaluate whether this method should return the same format for the tags (so in this method it would return an array of arrays, each containing 'id', 'tag' and 'urlified').
37
38        $query = 'SELECT `t2p`.`tag_id`, `t`.`urlified`, `t`.`tag` FROM `'.$TABLE_PREFIX.'tag2picture` as `t2p`, `'.$TABLE_PREFIX.'tags` as `t` WHERE `picture_id` = '.$picture_id.' AND `t2p`.`tag_id` = `t`.`id`;';
39        $result = mysql_query($query);
40        while($tag_row = mysql_fetch_assoc($result)) {
41                $picture_tags[$tag_row['urlified']] = $tag_row['tag_id'];
42        }
43        return $picture_tags;
44}
45
46function delete_picture_tags($picture_id) {
47        global $TABLE_PREFIX;
48        $picture_id = intval($picture_id);
49        $sql = 'DELETE FROM '.$TABLE_PREFIX.'tag2picture WHERE picture_id = '.$picture_id;
50        mysql_query($sql);
51}
52
53function get_tag_by_name($tag) {
54        global $TABLE_PREFIX;
55        $existing_tag = array();
56
57        $query = 'SELECT `id`, `tag`, `urlified` FROM `'.$TABLE_PREFIX.'tags` WHERE `tag`="'.$tag.'"';
58        $result = run_query($query);
59        $row = mysql_fetch_assoc($result);
60
61        if (!is_array($row)) {
62                return NULL;
63        }
64        return array('id' => $row['id'], 'tag' => $row['tag'], 'urlified' => $row['urlified']);
65}
66
67function get_tag_by_id($tag_id) {
68        global $TABLE_PREFIX;
69        $existing_tag = array();
70        $tag_id = intval($tag_id);
71
72        $query = 'SELECT `id`, `tag`, `urlified` FROM `'.$TABLE_PREFIX.'tags` WHERE `id`='.$tag_id;
73        $result = run_query($query);
74        $row = mysql_fetch_assoc($result);
75
76        if (!is_array($row)) {
77                return NULL;
78        }
79        return array('id' => $row['tag_id'], 'tag' => $row['tag'], 'urlified' => $row['urlified']);
80}
81
82function get_popular_tags($limit=NULL) {
83        global $TABLE_PREFIX;
84        // Return a list of the $limit most popular tags
85        $query = 'SELECT `t2p`.`tag_id`, COUNT(`t2p`.`tag_id`) AS `popularity`, `t`.`tag`, `t`.`urlified` FROM `'.$TABLE_PREFIX.'tag2picture` AS `t2p`, `'.$TABLE_PREFIX.'tags` AS `t` WHERE `t`.`id`=`t2p`.`tag_id` GROUP BY `t2p`.`tag_id` ORDER BY `popularity` DESC';
86        if( isset($limit) ) {
87                $limit = intval($limit);
88                $query .= ' LIMIT '.$limit;
89        }
90}
91
92function insert_tag($tag) {
93        global $TABLE_PREFIX;
94        $urlified = mysql_real_escape_string(urlify_tag($tag));
95        $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`,`tagdate`,`urlified`)
96        VALUES ("'.mysql_real_escape_string($tag).'", NOW(), "'.$urlified.'")';
97        if( mysql_query($sql) ) {
98                return mysql_insert_id();
99        }
100}
101
102function add_picture_tags($picture_id, $tags) {
103        global $TABLE_PREFIX;
104        $tags = parse_tags($tags);
105        $picture_id = intval($picture_id);
106
107        /* Process any tags for the picture */
108        $existing_tags = $existing_rels = array();
109        if (sizeof($tags) > 0) {
110                $tagsql = join('", "', $tags);
111                $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")';
112                $result = mysql_query($sql);
113                while($tag_row = mysql_fetch_assoc($result)) {
114                        $existing_tags[$tag_row['tag']] = $tag_row['id'];
115                }
116
117                $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"';
118                $result = mysql_query($sql);
119                while($tag_row = mysql_fetch_assoc($result)) {
120                        $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id'];
121                }
122        }
123
124        $added_tag_ids = array();
125        foreach($tags as $tag) {
126                if (!isset($existing_tags[$tag])) {
127                        // Must be a new tag, register it
128                        $existing_tags[$tag] = insert_tag($tag);
129                        $added_tag_ids[] = $existing_tags[$tag];
130                }
131
132                if (!isset($existing_rels[$existing_tags[$tag]])) {
133                        // No connection between tag and picture? create if
134                        $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`,`tag_id`,`tagdate`)
135                        VALUES ("'.$picture_id.'", "'.$existing_tags[$tag].'", NOW())';
136                        mysql_query($sql);
137                }
138        }
139
140        // Make sure that adding the tags 'onetwo' and 'one two' doesn't produce conflicts!
141        return $added_tag_ids;
142}
143
144function delete_tags($tag_ids) {
145        global $TABLE_PREFIX;
146        $tagsql = join(', ', $tag_ids);
147        $sql = 'DELETE FROM '.$TABLE_PREFIX.'tag2picture WHERE tag_id IN ('.$tagsql.')';
148        mysql_query($sql);
149        $sql = 'DELETE FROM '.$TABLE_PREFIX.'tags WHERE id IN ('.$tagsql.')';
150        mysql_query($sql);
151}
152
153function remove_picture_tags($picture_id, $tag_ids) {
154        // 1. Remove the specified tags from the specified picture.
155}
156
157function rename_picture_tag($tag_id, $new_name, $change_urlified=true) {
158        // 1. Rename the specified tag and update 'urlified' only if specified.
159}
160
161function purge_unused_tags() {
162        // 1. Remove all tags that are not associated with any pictures.
163}
164
165function update_picture_tags($picture_id, $tags) {
166        global $config;
167        global $TABLE_PREFIX;
168        $tags = parse_tags($tags);
169        $picture_id = intval($picture_id);
170
171        /* Process any tags for the picture */
172        $existing_tags = $existing_rels = array();
173        if (sizeof($tags) > 0) {
174                $tagsql = join('", "', $tags);
175                $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")';
176                $result = mysql_query($sql);
177                while($tag_row = mysql_fetch_assoc($result)) {
178                        $existing_tags[$tag_row['tag']] = $tag_row['id'];
179                }
180
181                $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"';
182                $result = mysql_query($sql);
183                while($tag_row = mysql_fetch_assoc($result)) {
184                        $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id'];
185                }
186        }
187
188        foreach($tags as $tag) {
189                if (!isset($existing_tags[$tag])) {
190                        // Must be a new tag, register it
191                        $path = mysql_real_escape_string(preg_replace("/[^\w|\.|'|\-|\[|\]]/", "_", $tag));
192                        $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`, `tagdate`, `path`)
193                        VALUES ("'.mysql_real_escape_string($tag).'", "'.$path.'", NOW())';
194                        print $sql;
195                        $result = mysql_query($sql);
196                        $existing_tags[$tag] = mysql_insert_id();
197                }
198
199                if (!isset($existing_rels[$existing_tags[$tag]])) {
200                        // No connection between tag and picture? create if
201                        $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`, `tag_id`, `tagdate`)
202                        VALUES ("'.$picture_id.'", "'.$existing_tags[$tag].'", NOW())';
203                        mysql_query($sql);
204                }
205        }
206
207        // Now remove links to any tags that have been deleted
208        foreach($existing_rels as $tag_id => $pic_id) {
209                if (!in_array($tag_id,$existing_tags)) {
210                        $sql = "DELETE FROM `".$TABLE_PREFIX."tag2picture`
211                        WHERE `picture_id` = '$picture_id' AND `tag_id` = '$tag_id'";
212                        mysql_query($sql);
213                }
214        }
215
216}
217
218?>
Note: See TracBrowser for help on using the repository browser.