root/trunk/plog-includes/plog-tag-functions.php

Revision 552, 6.9 KB (checked in by sidtheduck, 5 months ago)

+ cleared out require and include functions from included files (no longer needed if included)
+ added code to stop direct access to included files
+ adding back r549 translatable themes
+ cleaned version of "Air" theme

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}
92
93function insert_tag($tag) {
94    global $TABLE_PREFIX;
95    $urlified = mysql_real_escape_string(urlify_tag($tag));
96    $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`,`tagdate`,`urlified`)
97    VALUES ("'.mysql_real_escape_string($tag).'",NOW(),"'.$urlified.'")';
98    if( mysql_query($sql) ) {
99        return mysql_insert_id();
100    }
101}
102
103function add_picture_tags($picture_id,$tags){
104    global $TABLE_PREFIX;
105    $tags = parse_tags($tags);
106    $picture_id = intval($picture_id);
107
108    /* process any tags for the picture */
109    $existing_tags = $existing_rels = array();
110    if (sizeof($tags) > 0) {
111        $tagsql = join('","',$tags);
112        $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")';
113        $result = mysql_query($sql);
114        while($tag_row = mysql_fetch_assoc($result)) {
115            $existing_tags[$tag_row['tag']] = $tag_row['id'];
116        };
117
118        $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"';
119        $result = mysql_query($sql);
120        while($tag_row = mysql_fetch_assoc($result)) {
121            $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id'];
122        };
123    };
124
125    $added_tag_ids = array();
126    foreach($tags as $tag) {
127        if (!isset($existing_tags[$tag])) {
128            // must be a new tag, register it
129            $existing_tags[$tag] = insert_tag($tag);
130            $added_tag_ids[] = $existing_tags[$tag];
131        };
132
133        if (!isset($existing_rels[$existing_tags[$tag]])) {
134            // no connection between tag and picture? create if
135            $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`,`tag_id`,`tagdate`)
136            VALUES ("'.$picture_id.'","'.$existing_tags[$tag].'",NOW())';
137            mysql_query($sql);
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 browser.