- Timestamp:
- 12/14/09 13:02:28 (2 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
-
common/common.php (modified) (1 diff)
-
common/db/blog.php (modified) (2 diffs)
-
common/db/tags.php (modified) (6 diffs)
-
common/tags.php (modified) (1 diff)
-
common/textblock.php (modified) (1 diff)
-
scripts/migrate-task-tags (modified) (1 diff)
-
www/controllers/task.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/common/common.php
r1076 r1079 247 247 return false; 248 248 } 249 return is_tag_name($tag["name"]) && is_tag_type($tag["type"]); 249 if (!array_key_exists("parent", $tag)) { 250 return false; 251 } 252 return ( 253 is_tag_name($tag["name"]) && 254 is_tag_type($tag["type"]) && 255 is_tag_id($tag["parent"]) 256 ); 250 257 } 251 258 -
trunk/common/db/blog.php
r1069 r1079 5 5 function blog_get_range($tag_name, $start, $range) { 6 6 if (is_tag_name($tag_name)) { 7 $tag_id = tag_get_id(array("name" => $tag_name, "type" => "tag")); 7 $tag_id = tag_get_id(array( 8 "name" => $tag_name, "type" => "tag", "parent" => 0)); 8 9 if (is_null($tag_id)) { 9 10 $tag_id = -1; … … 38 39 function blog_count($tag_name) { 39 40 if (is_tag_name($tag_name)) { 40 $tag_id = tag_get_id(array("name" => $tag_name, "type" => "tag")); 41 $tag_id = tag_get_id(array( 42 "name" => $tag_name, "type" => "tag", "parent" => 0)); 41 43 if (is_null($tag_id)) { 42 44 $tag_id = -1; -
trunk/common/db/tags.php
r1069 r1079 4 4 require_once(IA_ROOT_DIR."common/common.php"); 5 5 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"; 6 // Get list of all tag names, filtered by type and parent 7 function tag_get_all($types = null, $parent = null) { 8 $query = "SELECT name, type, parent FROM ia_tags"; 9 $where = array(); 9 10 if (!is_null($types)) { 10 11 log_assert(is_array($types), "types should be an array"); … … 13 14 } 14 15 15 $ query .= sprintf(" WHERE type IN (%s)",16 $where[] = sprintf("(type IN (%s))", 16 17 implode(',', array_map('db_quote', $types)) 17 18 ); 18 19 } 20 if (!is_null($parent)) { 21 log_assert(is_tag_id($parent)); 22 $where[] = sprintf("(parent = %s)", db_quote($parent)); 23 } 24 if (count($where)) { 25 $query .= sprintf(" WHERE %s", implode(" AND ", $where)); 26 } 19 27 return db_fetch_all($query); 20 28 } 21 29 22 30 // Get list of all tags for a certain object, filtered by type 23 function tag_get($obj, $obj_id, $type = null ) {31 function tag_get($obj, $obj_id, $type = null, $parent = null) { 24 32 log_assert(is_taggable($obj)); 25 33 log_assert(is_null($type) || is_tag_type($type)); … … 29 37 $where_type = sprintf(" AND tags.type = %s", db_quote($type)); 30 38 } 39 if (is_null($parent)) { 40 $where_parent = ""; 41 } else { 42 $where_parent = sprintf(" AND tags.parent = %s", db_quote($parent)); 43 } 31 44 $query = sprintf( 32 "SELECT %s_id, tag_id, tags.name AS tag_name, tags.type AS tag_type 45 "SELECT %s_id, tag_id, 46 tags.name AS tag_name, tags.type AS tag_type, tags.parent AS tag_parent 33 47 FROM ia_%s_tags AS obj_tags 34 48 LEFT JOIN ia_tags AS tags ON obj_tags.tag_id = tags.id 35 WHERE %s_id = %s%s ",49 WHERE %s_id = %s%s%s", 36 50 db_escape($obj), db_escape($obj), db_escape($obj), 37 db_quote($obj_id), $where_type 51 db_quote($obj_id), $where_type, $where_parent 38 52 ); 39 53 return db_fetch_all($query); … … 44 58 log_assert(is_tag($tag)); 45 59 $query = sprintf( 46 "SELECT id FROM ia_tags WHERE name = %s AND type = %s ",47 db_quote($tag["name"]), db_quote($tag["type"]) 60 "SELECT id FROM ia_tags WHERE name = %s AND type = %s AND parent = %s", 61 db_quote($tag["name"]), db_quote($tag["type"]), db_quote($tag["parent"]) 48 62 ); 49 63 $result = db_fetch($query); … … 56 70 foreach ($tags as $tag) { 57 71 log_assert(is_tag($tag)); 58 $tag_wheres[] = sprintf("(name = %s AND type = %s)", 59 db_quote($tag["name"]), db_quote($tag["type"])); 72 $tag_wheres[] = sprintf( 73 "(name = %s AND type = %s AND parent = %s)", 74 db_quote($tag["name"]), db_quote($tag["type"]), 75 db_quote($tag["parent"]) 76 ); 60 77 } 61 78 $query = sprintf("SELECT id FROM ia_tags WHERE %s", … … 69 86 $id = tag_get_id($tag); 70 87 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'])); 88 $query = sprintf( 89 "INSERT INTO ia_tags (name, type, parent) VALUES (%s, %s, %s)", 90 db_quote($tag['name']), db_quote($tag['type']), 91 db_quote($tag['parent']) 92 ); 73 93 db_query($query); 74 94 return db_insert_id(); -
trunk/common/tags.php
r1077 r1079 31 31 } 32 32 33 function tag_build_list($obj, $obj_id, $type, $ remove_prefix = true) {34 $tag_list = tag_get($obj, $obj_id, $type );33 function tag_build_list($obj, $obj_id, $type, $parent = null) { 34 $tag_list = tag_get($obj, $obj_id, $type, $parent); 35 35 $tag_names = array(); 36 36 foreach ($tag_list as $tag) { 37 if ($remove_prefix) { 38 $tag_parts = explode('@', $tag['tag_name']); 39 $tag_names[] = trim($tag_parts[ count($tag_parts) - 1 ]); 40 } else { 41 $tag_names[] = $tag['tag_name']; 42 } 37 $tag_names[] = $tag['tag_name']; 43 38 } 44 39 return implode(", ", $tag_names); 45 40 } 46 41 47 function tag_update($obj, $obj_id, $type, $tag_data, $tag_prefix = "") { 42 // Receives a list of tags of a certain type and, optionally, with a 43 // certain parent and updates the tag list for the specified object. 44 // Returns a list of tag ids. 45 function tag_update($obj, $obj_id, $type, $tag_data, $parent = 0) { 48 46 tag_clear($obj, $obj_id, $type); 49 47 $tag_data = tag_split($tag_data); 48 $tag_ids = array(); 50 49 foreach ($tag_data as $tag_name) { 51 $tag_id = tag_assign_id(array("name" => $tag_prefix.$tag_name, "type" => $type)); 50 $tag_id = tag_assign_id( 51 array("name" => $tag_name, "type" => $type, "parent" => $parent)); 52 $tag_ids[] = $tag_id; 52 53 tag_add($obj, $obj_id, $tag_id); 53 54 } 55 return $tag_ids; 54 56 } 55 57 -
trunk/common/textblock.php
r1069 r1079 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'], 52 tag_get_id(array("name" => 'stiri', "type" => "tag")))) {51 if ($whole_news && tag_exists('textblock', $tb['name'], tag_get_id( 52 array("name" => 'stiri', "type" => "tag", "parent" => 0)))) { 53 53 // Don't compute snippet for news -- they should be rendered as 54 54 // they are in the snippet. -
trunk/scripts/migrate-task-tags
r1072 r1079 8 8 ALTER TABLE `ia_tags` 9 9 ADD COLUMN `type` ENUM('author','contest','year','round','age_group','method','algorithm','tag') NOT NULL, 10 ADD COLUMN `parent` INTEGER NOT NULL DEFAULT 0, 10 11 DROP INDEX `name`, 11 ADD UNIQUE INDEX `name_type` USING BTREE(`name`, `type` )12 ADD UNIQUE INDEX `name_type` USING BTREE(`name`, `type`, `parent`) 12 13 "); 13 14 db_query('UPDATE `ia_tags` SET `type` = "tag"'); -
trunk/www/controllers/task.php
r1077 r1079 70 70 // FIXME: tags that have children such as contest, year or round should have only one tag 71 71 foreach ($tag_types as $type) { 72 $remove_prefix = isset($tag_parents[$type]); 73 $values['tag_'.$type] = request('tag_'.$type, tag_build_list('task', $task_id, $type, $remove_prefix)); 74 $prefix['tag_'.$type] = ''; 75 if (isset($tag_parents[$type])) { 76 $parent_type = $tag_parents[$type]; 77 $prefix['tag_'.$type] = $prefix['tag_'.$parent_type].$values['tag_'.$parent_type]."@"; 78 } 72 $values['tag_'.$type] = request('tag_'.$type, 73 tag_build_list('task', $task_id, $type)); 79 74 } 80 75 … … 144 139 145 140 // If no errors then do the db monkey 141 $tags = array(); 146 142 if (!$errors) { 147 143 // FIXME: error handling? Is that even remotely possible in php? … … 151 147 if (identity_can('task-tag', $new_task)) { 152 148 foreach ($tag_types as $type) { 153 tag_update('task', $new_task['id'], $type, $values['tag_'.$type], $prefix['tag_'.$type]); 149 $parent = 0; 150 if (isset($tag_parents[$type])) { 151 $parent = $tags[$tag_parents[$type]][0]; 152 } 153 $tags[$type] = tag_update('task', $new_task['id'], $type, 154 $values['tag_'.$type], $parent); 154 155 } 155 156 }
Note: See TracChangeset
for help on using the changeset viewer.
![[infoarena] development](/chrome/site/logo.png)