Changeset 1079 for trunk


Ignore:
Timestamp:
12/14/09 13:02:28 (2 years ago)
Author:
bogdan2412
Message:

Updated tagging backend again.

Added a column in ia_tags for a tag's parent and got rid of the string prefixes we used before.
Review URL: http://reviewboard.infoarena.ro/r/118/

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/common.php

    r1076 r1079  
    247247        return false; 
    248248    } 
    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    ); 
    250257} 
    251258 
  • trunk/common/db/blog.php

    r1069 r1079  
    55function blog_get_range($tag_name, $start, $range) { 
    66    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)); 
    89        if (is_null($tag_id)) { 
    910            $tag_id = -1; 
     
    3839function blog_count($tag_name) { 
    3940    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)); 
    4143        if (is_null($tag_id)) { 
    4244            $tag_id = -1; 
  • trunk/common/db/tags.php

    r1069 r1079  
    44require_once(IA_ROOT_DIR."common/common.php"); 
    55 
    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 
     7function tag_get_all($types = null, $parent = null) { 
     8    $query = "SELECT name, type, parent FROM ia_tags"; 
     9    $where = array(); 
    910    if (!is_null($types)) { 
    1011        log_assert(is_array($types), "types should be an array"); 
     
    1314        } 
    1415 
    15         $query .= sprintf(" WHERE type IN (%s)", 
     16        $where[] = sprintf("(type IN (%s))", 
    1617            implode(',', array_map('db_quote', $types)) 
    1718        ); 
    1819    } 
     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    } 
    1927    return db_fetch_all($query); 
    2028} 
    2129 
    2230// Get list of all tags for a certain object, filtered by type 
    23 function tag_get($obj, $obj_id, $type = null) { 
     31function tag_get($obj, $obj_id, $type = null, $parent = null) { 
    2432    log_assert(is_taggable($obj)); 
    2533    log_assert(is_null($type) || is_tag_type($type)); 
     
    2937        $where_type = sprintf(" AND tags.type = %s", db_quote($type)); 
    3038    } 
     39    if (is_null($parent)) { 
     40        $where_parent = ""; 
     41    } else { 
     42        $where_parent = sprintf(" AND tags.parent = %s", db_quote($parent)); 
     43    } 
    3144    $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 
    3347        FROM ia_%s_tags AS obj_tags 
    3448        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", 
    3650        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 
    3852    ); 
    3953    return db_fetch_all($query); 
     
    4458    log_assert(is_tag($tag)); 
    4559    $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"]) 
    4862    ); 
    4963    $result = db_fetch($query); 
     
    5670    foreach ($tags as $tag) { 
    5771        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        ); 
    6077    } 
    6178    $query = sprintf("SELECT id FROM ia_tags WHERE %s", 
     
    6986    $id = tag_get_id($tag); 
    7087    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        ); 
    7393        db_query($query); 
    7494        return db_insert_id(); 
  • trunk/common/tags.php

    r1077 r1079  
    3131} 
    3232 
    33 function tag_build_list($obj, $obj_id, $type, $remove_prefix = true) { 
    34     $tag_list = tag_get($obj, $obj_id, $type); 
     33function tag_build_list($obj, $obj_id, $type, $parent = null) { 
     34    $tag_list = tag_get($obj, $obj_id, $type, $parent); 
    3535    $tag_names = array(); 
    3636    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']; 
    4338    } 
    4439    return implode(", ", $tag_names); 
    4540} 
    4641 
    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. 
     45function tag_update($obj, $obj_id, $type, $tag_data, $parent = 0) { 
    4846    tag_clear($obj, $obj_id, $type); 
    4947    $tag_data = tag_split($tag_data); 
     48    $tag_ids = array(); 
    5049    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; 
    5253        tag_add($obj, $obj_id, $tag_id); 
    5354    } 
     55    return $tag_ids; 
    5456} 
    5557 
  • trunk/common/textblock.php

    r1069 r1079  
    4949        // 'stiri'. On the other hand, this operation is cached and we 
    5050        // 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)))) { 
    5353            // Don't compute snippet for news -- they should be rendered as 
    5454            // they are in the snippet. 
  • trunk/scripts/migrate-task-tags

    r1072 r1079  
    88    ALTER TABLE `ia_tags` 
    99    ADD COLUMN `type` ENUM('author','contest','year','round','age_group','method','algorithm','tag') NOT NULL, 
     10    ADD COLUMN `parent` INTEGER NOT NULL DEFAULT 0, 
    1011    DROP INDEX `name`, 
    11     ADD UNIQUE INDEX `name_type` USING BTREE(`name`, `type`) 
     12    ADD UNIQUE INDEX `name_type` USING BTREE(`name`, `type`, `parent`) 
    1213"); 
    1314db_query('UPDATE `ia_tags` SET `type` = "tag"'); 
  • trunk/www/controllers/task.php

    r1077 r1079  
    7070    // FIXME: tags that have children such as contest, year or round should have only one tag 
    7171    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)); 
    7974    } 
    8075 
     
    144139 
    145140        // If no errors then do the db monkey 
     141        $tags = array(); 
    146142        if (!$errors) { 
    147143            // FIXME: error handling? Is that even remotely possible in php? 
     
    151147            if (identity_can('task-tag', $new_task)) { 
    152148                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); 
    154155                } 
    155156            } 
Note: See TracChangeset for help on using the changeset viewer.