Changeset 1069
- Timestamp:
- 12/12/09 20:21:12 (2 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 11 edited
-
common/common.php (modified) (2 diffs)
-
common/db/blog.php (modified) (5 diffs)
-
common/db/tags.php (modified) (6 diffs)
-
common/tags.php (modified) (2 diffs)
-
common/textblock.php (modified) (1 diff)
-
scripts/migrate-task-tags (added)
-
www/controllers/account.php (modified) (3 diffs)
-
www/controllers/blog.php (modified) (2 diffs)
-
www/controllers/round.php (modified) (2 diffs)
-
www/controllers/task.php (modified) (2 diffs)
-
www/controllers/textblock_edit.php (modified) (2 diffs)
-
www/views/tags_header.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/common/common.php
r997 r1069 163 163 164 164 // Check valid score names. 165 // Does not check existence. 165 // Does not check existence. 166 166 function is_score_name($score_name) { 167 167 return preg_match('/^'.IA_RE_SCORE_NAME.'$/xi', $score_name) && … … 205 205 return preg_match('/^'.IA_RE_TAG_NAME.'$/xi', $tag_name) && 206 206 strlen($tag_name) < 64; 207 } 208 209 // Check tag type 210 function is_tag_type($tag_type) { 211 return in_array($tag_type, array( 212 'author', 'contest', 'year', 'round', 'age_group', 213 'method', 'algorithm', 'tag' 214 )); 215 } 216 217 // Check tag 218 function is_tag($tag) { 219 if (!is_array($tag)) { 220 return false; 221 } 222 if (!array_key_exists("name", $tag)) { 223 return false; 224 } 225 if (!array_key_exists("type", $tag)) { 226 return false; 227 } 228 return is_tag_name($tag["name"]) && is_tag_type($tag["type"]); 207 229 } 208 230 -
trunk/common/db/blog.php
r943 r1069 5 5 function blog_get_range($tag_name, $start, $range) { 6 6 if (is_tag_name($tag_name)) { 7 $tag_id = tag_get_id( $tag_name);7 $tag_id = tag_get_id(array("name" => $tag_name, "type" => "tag")); 8 8 if (is_null($tag_id)) { 9 9 $tag_id = -1; … … 13 13 $where = "TRUE"; 14 14 } 15 $query = sprintf("SELECT * FROM ia_textblock 16 WHERE %s AND name LIKE 'blog/%%' 15 $query = sprintf("SELECT * FROM ia_textblock 16 WHERE %s AND name LIKE 'blog/%%' 17 17 AND security <> 'private' 18 18 ORDER BY ia_textblock.creation_timestamp DESC 19 LIMIT %s, %s", 19 LIMIT %s, %s", 20 20 $where, db_quote((int)$start), db_quote((int)$range)); 21 21 return db_fetch_all($query); … … 30 30 31 31 function blog_get_comment_count($topic_id) { 32 $query = sprintf("SELECT `numReplies` FROM ia_smf_topics 32 $query = sprintf("SELECT `numReplies` FROM ia_smf_topics 33 33 WHERE ID_TOPIC = %d", db_escape($topic_id)); 34 34 $result = db_fetch($query); … … 38 38 function blog_count($tag_name) { 39 39 if (is_tag_name($tag_name)) { 40 $tag_id = tag_get_id( $tag_name);40 $tag_id = tag_get_id(array("name" => $tag_name, "type" => "tag")); 41 41 if (is_null($tag_id)) { 42 42 $tag_id = -1; … … 54 54 55 55 function blog_get_tags() { 56 $query = "SELECT name, 56 $query = "SELECT name, 57 57 (SELECT COUNT(*) FROM ia_textblock_tags WHERE tag_id = id AND textblock_id LIKE 'blog/%%') AS cnt 58 FROM ia_tags WHERE id IN 58 FROM ia_tags WHERE id IN 59 59 (SELECT DISTINCT tag_id FROM ia_textblock_tags WHERE textblock_id LIKE 'blog/%%') 60 60 ORDER BY name"; -
trunk/common/db/tags.php
r852 r1069 4 4 require_once(IA_ROOT_DIR."common/common.php"); 5 5 6 // get list of all tag names 7 function tag_get_all_names() { 8 $query = sprintf("SELECT name FROM ia_tags"); 6 // Get list of all tag names, filtered by type 7 function tag_get_all($types = null) { 8 $query = "SELECT name, type FROM ia_tags"; 9 if (!is_null($types)) { 10 log_assert(is_array($types), "types should be an array"); 11 foreach ($types as $type) { 12 log_assert(is_tag_type($type)); 13 } 14 15 $query .= sprintf(" WHERE type IN (%s)", 16 implode(',', array_map('db_quote', $types)) 17 ); 18 } 9 19 return db_fetch_all($query); 10 20 } 11 21 12 function tag_get_names($obj, $obj_id) { 22 // Get list of all tags for a certain object, filtered by type 23 function tag_get($obj, $obj_id, $type = null) { 13 24 log_assert(is_taggable($obj)); 14 $query = sprintf("SELECT %s_id, tag_id, tags.name AS tag_name 15 FROM ia_%s_tags AS obj_tags 16 LEFT JOIN ia_tags AS tags ON obj_tags.tag_id = tags.id 17 WHERE %s_id = %s", 18 db_escape($obj), db_escape($obj), db_escape($obj), db_quote($obj_id)); 19 return db_fetch_all($query); 25 log_assert(is_null($type) || is_tag_type($type)); 26 if (is_null($type)) { 27 $where_type = ""; 28 } else { 29 $where_type = sprintf(" AND tags.type = %s", db_quote($type)); 30 } 31 $query = sprintf( 32 "SELECT %s_id, tag_id, tags.name AS tag_name, tags.type AS tag_type 33 FROM ia_%s_tags AS obj_tags 34 LEFT JOIN ia_tags AS tags ON obj_tags.tag_id = tags.id 35 WHERE %s_id = %s%s", 36 db_escape($obj), db_escape($obj), db_escape($obj), 37 db_quote($obj_id), $where_type 38 ); 39 return db_fetch_all($query); 20 40 } 21 41 22 // get tag id for a certain tag name 23 function tag_get_id($tag_name) { 24 log_assert(is_tag_name($tag_name)); 25 $query = sprintf("SELECT id FROM ia_tags WHERE name = %s", 26 db_quote($tag_name)); 42 // Get tag id for a certain tag 43 function tag_get_id($tag) { 44 log_assert(is_tag($tag)); 45 $query = sprintf( 46 "SELECT id FROM ia_tags WHERE name = %s AND type = %s", 47 db_quote($tag["name"]), db_quote($tag["type"]) 48 ); 27 49 $result = db_fetch($query); 28 50 return $result['id']; 29 51 } 30 52 31 // get list of tag ids for a list of tag names 32 function tag_get_ids($tag_names) { 33 foreach ($tag_names as &$name) { 34 log_assert(is_tag_name($name)); 35 $name = db_quote($name); 53 // Get list of tag ids for a list of tags 54 function tag_get_ids($tags) { 55 $tag_wheres = array(); 56 foreach ($tags as $tag) { 57 log_assert(is_tag($tag)); 58 $tag_wheres[] = sprintf("(name = %s AND type = %s)", 59 db_quote($tag["name"]), db_quote($tag["type"])); 36 60 } 37 $query = sprintf("SELECT id FROM ia_tags WHERE name IN (%s)", implode(", ", $tag_names)); 61 $query = sprintf("SELECT id FROM ia_tags WHERE %s", 62 implode(" OR ", $tag_wheres)); 38 63 return db_fetch_all($query); 39 64 } 40 65 41 // build ugly where clause to be used in subqueries 66 // Assign numeric id to a given tag name 67 function tag_assign_id($tag) { 68 log_assert(is_tag($tag)); 69 $id = tag_get_id($tag); 70 if (is_null($id)) { 71 $query = sprintf("INSERT INTO ia_tags (name, type) VALUES (%s, %s)", 72 db_quote($tag['name']), db_quote($tag['type'])); 73 db_query($query); 74 return db_insert_id(); 75 } 76 return $id; 77 } 78 79 // Build ugly where clause to be used in subqueries 42 80 function tag_build_where($obj, $tag_ids, $parent_table = null) { 43 81 log_assert(is_taggable($obj)); … … 52 90 } 53 91 $where = sprintf("(SELECT COUNT(*) FROM ia_%s_tags WHERE %s_id = %s.%s". 54 " AND tag_id IN (%s)) = %d", 55 db_escape($obj), db_escape($obj), 56 db_escape($parent_table), db_escape($field), 92 " AND tag_id IN (%s)) = %d", 93 db_escape($obj), db_escape($obj), 94 db_escape($parent_table), db_escape($field), 57 95 implode(", ", $tag_ids), count($tag_ids)); 58 96 return $where; 59 97 } 60 98 61 // assign numeric id to a given tag name 62 function tag_assign_id($tag_name) { 63 log_assert(is_tag_name($tag_name)); 64 $id = tag_get_id($tag_name); 65 if (is_null($id)) { 66 $query = sprintf("INSERT INTO ia_tags (name) VALUES (%s)", db_quote($tag_name)); 67 db_query($query); 68 return db_insert_id(); 69 } 70 return $id; 71 } 72 73 // get all objects containting all tags from a list of tag ids 99 // Get all objects containting all tags from a list of tag ids 74 100 function tag_get_objects($obj, $tag_ids, $content = true) { 75 101 log_assert(is_taggable($obj)); … … 77 103 if ($content) { 78 104 $fields = "*"; 79 } 80 else 81 if ($obj == 'textblock') { 105 } elseif ($obj == 'textblock') { 82 106 $fields = "name"; 83 } 84 else { 107 } else { 85 108 $fields = "id"; 86 109 } 87 $query = sprintf("SELECT %s FROM ia_%s WHERE %s", $fields, db_escape($obj), 110 $query = sprintf("SELECT %s FROM ia_%s WHERE %s", $fields, db_escape($obj), 88 111 tag_build_where($obj, $tag_ids)); 89 112 return db_fetch_all($query); 90 113 } 91 114 92 // count the number of objects containing all tags from a list of tag ids115 // Count the number of objects containing all tags from a list of tag ids 93 116 function tag_count_objects($obj, $tag_ids) { 94 117 log_assert(is_taggable($obj)); 95 118 log_assert(is_array($tag_ids)); 96 $query = sprintf("SELECT COUNT(*) as `cnt` FROM ia_%s WHERE %s", db_escape($obj), 119 $query = sprintf("SELECT COUNT(*) as `cnt` FROM ia_%s WHERE %s", db_escape($obj), 97 120 tag_build_where($obj, $tag_ids)); 98 121 $result = db_fetch($query); … … 100 123 } 101 124 102 // check if a certain object has a certain tag 125 // Clear all tags, filtered by type 126 function tag_clear($obj, $obj_id, $type = null) { 127 log_assert(is_taggable($obj)); 128 log_assert(is_null($type) || is_tag_type($type)); 129 $where = sprintf("%s_id = %s", db_escape($obj), db_quote($obj_id)); 130 $query = sprintf("DELETE FROM ia_%s_tags WHERE %s AND tag_id IN (%s)", 131 db_escape($obj), $where, "%s" 132 ); 133 if (is_null($type)) { 134 $join = ""; 135 } else { 136 $join = "LEFT JOIN ia_tags AS tags ON obj_tags.tag_id = tags.id"; 137 $where .= sprintf(" AND tags.type = %s", db_quote($type)); 138 } 139 $subquery = sprintf("SELECT tag_id FROM ia_%s_tags AS obj_tags %s WHERE %s", 140 db_escape($obj), $join, $where); 141 $tag_ids = array(); 142 foreach (db_fetch_all($subquery) as $tag) { 143 $tag_ids[] = $tag["tag_id"]; 144 } 145 if ($tag_ids) { 146 $query = sprintf($query, implode(",", $tag_ids)); 147 db_query($query); 148 } 149 } 150 151 // Check if a certain object has a certain tag 103 152 function tag_exists($obj, $obj_id, $tag_id) { 104 153 log_assert(is_taggable($obj)); … … 110 159 if ($result['cnt'] == 0) { 111 160 return false; 112 } 161 } 113 162 return true; 114 163 } 115 164 116 // clear all tags 117 function tag_clear($obj, $obj_id) { 118 log_assert(is_taggable($obj)); 119 $query = sprintf("DELETE FROM ia_%s_tags WHERE %s_id = %s", db_escape($obj), 120 db_escape($obj), db_quote($obj_id)); 121 db_query($query); 122 } 123 124 // remove a tag 165 // Remove a tag 125 166 function tag_remove($obj, $obj_id, $tag_id) { 126 167 log_assert(is_taggable($obj)); … … 131 172 } 132 173 133 // add a tag174 // Add a tag 134 175 function tag_add($obj, $obj_id, $tag_id) { 135 176 log_assert(is_taggable($obj)); -
trunk/common/tags.php
r852 r1069 30 30 } 31 31 32 function tag_build_list($obj, $obj_id ) {33 $tag_list = tag_get _names($obj, $obj_id);32 function tag_build_list($obj, $obj_id, $type) { 33 $tag_list = tag_get($obj, $obj_id, $type); 34 34 $tag_names = array(); 35 35 foreach ($tag_list as $tag) { … … 39 39 } 40 40 41 function tag_update($obj, $obj_id, $t ag_data) {42 tag_clear($obj, $obj_id );41 function tag_update($obj, $obj_id, $type, $tag_data) { 42 tag_clear($obj, $obj_id, $type); 43 43 $tag_data = tag_split($tag_data); 44 44 foreach ($tag_data as $tag_name) { 45 $tag_id = tag_assign_id( $tag_name);45 $tag_id = tag_assign_id(array("name" => $tag_name, "type" => $type)); 46 46 tag_add($obj, $obj_id, $tag_id); 47 47 } -
trunk/common/textblock.php
r1057 r1069 49 49 // 'stiri'. On the other hand, this operation is cached and we 50 50 // shouldn't worry too much about performance. 51 if ($whole_news && tag_exists('textblock', $tb['name'], tag_get_id('stiri'))) { 51 if ($whole_news && tag_exists('textblock', $tb['name'], 52 tag_get_id(array("name" => 'stiri', "type" => "tag")))) { 52 53 // Don't compute snippet for news -- they should be rendered as 53 54 // they are in the snippet. -
trunk/www/controllers/account.php
r997 r1069 76 76 77 77 // validate tag data 78 $data['tags'] = request('tags', tag_build_list("user", $user['id'] ));78 $data['tags'] = request('tags', tag_build_list("user", $user['id'], "tag")); 79 79 tag_validate($data, $errors); 80 80 … … 162 162 // update tags info 163 163 if (identity_can('user-tag', $new_user)) { 164 tag_update("user", $new_user['id'], $data['tags']);165 } 166 $data['tags'] = tag_build_list("user", $new_user['id'] );164 tag_update("user", $new_user['id'], "tag", $data['tags']); 165 } 166 $data['tags'] = tag_build_list("user", $new_user['id'], "tag"); 167 167 168 168 // done. redirect to same page so user has a strong confirmation … … 175 175 // form is displayed for the first time. Fill in default values 176 176 $data = $user; 177 $data['tags'] = tag_build_list("user", $user['id'] );177 $data['tags'] = tag_build_list("user", $user['id'], "tag"); 178 178 179 179 // unset some fields we do not $data to carry -
trunk/www/controllers/blog.php
r969 r1069 58 58 $subpage['forum_topic'] = blog_get_forum_topic($subpage['name']); 59 59 $subpage['comment_count'] = blog_get_comment_count($subpage['forum_topic']); 60 $subpage['tags'] = tag_get _names("textblock", $subpage['name']);60 $subpage['tags'] = tag_get("textblock", $subpage['name']); 61 61 } 62 62 … … 102 102 $view['textblock'] = $page; 103 103 $view['forum_topic'] = $page['forum_topic']; 104 $view['tags'] = tag_get _names("textblock", $page['name']);104 $view['tags'] = tag_get("textblock", $page['name']); 105 105 $view['first_textblock'] = textblock_get_revision($page_name, 1, true); 106 106 -
trunk/www/controllers/round.php
r1026 r1069 100 100 101 101 // Tag data 102 $values['tags'] = request('tags', tag_build_list("round", $round_id ));102 $values['tags'] = request('tags', tag_build_list("round", $round_id, "tag")); 103 103 104 104 // Validate the monkey. … … 169 169 170 170 if (identity_can('round-tag', $new_round)) { 171 tag_update("round", $new_round['id'], $values['tags']);171 tag_update("round", $new_round['id'], "tag", $values['tags']); 172 172 } 173 173 -
trunk/www/controllers/task.php
r1053 r1069 65 65 66 66 // Tag data 67 $values['tags'] = request('tags', tag_build_list("task", $task_id ));67 $values['tags'] = request('tags', tag_build_list("task", $task_id, "tag")); 68 68 69 69 // Task owner … … 138 138 139 139 if (identity_can('task-tag', $new_task)) { 140 tag_update("task", $new_task['id'], $values['tags']);140 tag_update("task", $new_task['id'], "tag", $values['tags']); 141 141 } 142 142 -
trunk/www/controllers/textblock_edit.php
r997 r1069 36 36 $values['security'] = request('security', $page['security']); 37 37 $values['forum_topic'] = request('forum_topic', $page['forum_topic']); 38 $values['tags'] = request('tags', tag_build_list("textblock", $page_name ));38 $values['tags'] = request('tags', tag_build_list("textblock", $page_name, "tag")); 39 39 $values['creation_timestamp'] = getattr($page, 'creation_timestamp'); 40 40 $values['timestamp'] = null; … … 82 82 remote_ip_info()); 83 83 if (identity_can('textblock-tag', $new_page)) { 84 tag_update("textblock", $new_page['name'], $values['tags']);84 tag_update("textblock", $new_page['name'], "tag", $values['tags']); 85 85 } 86 86 flash('Am actualizat continutul'); -
trunk/www/views/tags_header.php
r1055 r1069 42 42 } 43 43 <?php 44 $tag_names = tag_get_all _names();44 $tag_names = tag_get_all(); 45 45 echo "collection = [\n"; 46 46 foreach ($tag_names as $tag) {
Note: See TracChangeset
for help on using the changeset viewer.
![[infoarena] development](/chrome/site/logo.png)