Changeset 400

Show
Ignore:
Timestamp:
05/09/06 19:21:16 (3 years ago)
Author:
stefan
Message:

Minor progress on tags:

  • implemented a "urlify_tag" function that currently just calls rawurlencode()
  • implemented unit tests for that function and added a few tests for the parse_tags function.
  • whitespace changes (replacing tabs for spaces).
Location:
branches/plogger-with-tags
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/plogger-with-tags/plog-tag-functions.php

    r399 r400  
    22 
    33function parse_tags($str) { 
    4         // 1. compress all extra whitespaces 
    5         $str = preg_replace('/\s{2,}/',' ',$str); 
    6         // 2. extract any phrases in quotes 
    7         preg_match_all('/(\'|")(.*?)(\1)/',$str,$phrases); 
    8         // 3. now remove the phrases from the string 
    9         $str = preg_replace('/(\'|")(.*?)(\1)/','',$str); 
    10         // 4. get rid of whitespaces at the end that may have been left 
    11         $str = trim($str); 
    12         // 5. get single words 
    13         $words = preg_split('/\s+/',$str); 
    14         // 6. merge single words and phrases 
    15         $tags = array_merge($phrases[2],$words); 
     4   // 1. compress all extra whitespaces 
     5   $str = preg_replace('/\Ts{2,}/',' ',$str); 
     6   // 2. extract any phrases in quotes 
     7   preg_match_all('/(\'|")(.*?)(\1)/',$str,$phrases); 
     8   // 3. now remove the phrases from the string 
     9   $str = preg_replace('/(\'|")(.*?)(\1)/','',$str); 
     10   // 4. get rid of whitespaces at the end that may have been left 
     11   $str = trim($str); 
     12   // 5. get single words 
     13   $words = preg_split('/\s+/',$str); 
     14   // 6. merge single words and phrases 
     15   $tags = array_merge($phrases[2],$words); 
    1616 
    17         return $tags; 
     17   return $tags; 
    1818} 
    1919 
    2020function urlify_tag($tag) { 
    2121   // 1. format the incoming tag to use in a URL 
     22   return rawurlencode($tag); 
    2223} 
    2324 
    2425function get_picture_tags($picture_id) { 
    25         global $config; 
    26         global $TABLE_PREFIX; 
    27         $picture_id = intval($picture_id); 
    28         $picture_tags = array(); 
     26   global $config; 
     27   global $TABLE_PREFIX; 
     28   $picture_id = intval($picture_id); 
     29   $picture_tags = array(); 
    2930 
    30         $query = 'SELECT `t2p`.`tag_id`, `t`.`path` FROM `'.$TABLE_PREFIX.'tag2picture` as `t2p`, `'.$TABLE_PREFIX.'tags` as `t` WHERE `picture_id` = '.$picture_id.' AND `t2p`.`tag_id` = `t`.`id`;'; 
    31         $result = mysql_query($query); 
    32         while($tag_row = mysql_fetch_assoc($result)) { 
    33                 $picture_tags[$tag_row['urlified']] = $tag_row['tag_id']; 
    34         }; 
    35         return $picture_tags; 
     31   $query = 'SELECT `t2p`.`tag_id`, `t`.`path` FROM `'.$TABLE_PREFIX.'tag2picture` as `t2p`, `'.$TABLE_PREFIX.'tags` as `t` WHERE `picture_id` = '.$picture_id.' AND `t2p`.`tag_id` = `t`.`id`;'; 
     32   $result = mysql_query($query); 
     33   while($tag_row = mysql_fetch_assoc($result)) { 
     34      $picture_tags[$tag_row['urlified']] = $tag_row['tag_id']; 
     35   }; 
     36   return $picture_tags; 
    3637} 
    3738 
    38 function get_popular_tags($count_limit) { 
    39    // Return a list of the $count_limit most popular tags 
     39function get_popular_tags($limit) { 
     40   // Return a list of the $limit most popular tags 
    4041} 
    4142 
     
    6162 
    6263function update_picture_tags($picture_id,$tags) { 
    63         global $config; 
    64         global $TABLE_PREFIX; 
    65         $tags = parse_tags(SmartStripSlashes($tags)); 
    66         $picture_id = intval($picture_id); 
     64   global $config; 
     65   global $TABLE_PREFIX; 
     66   $tags = parse_tags(SmartStripSlashes($tags)); 
     67   $picture_id = intval($picture_id); 
    6768 
    68         /* process any tags for the picture */ 
    69         $existing_tags = $existing_rels = array(); 
    70         if (sizeof($tags) > 0) { 
    71                 $tagsql = join('","',$tags); 
    72                 $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")'; 
    73                 $result = mysql_query($sql); 
    74                 while($tag_row = mysql_fetch_assoc($result)) { 
    75                         $existing_tags[$tag_row['tag']] = $tag_row['id']; 
    76                 }; 
     69   /* process any tags for the picture */ 
     70   $existing_tags = $existing_rels = array(); 
     71   if (sizeof($tags) > 0) { 
     72      $tagsql = join('","',$tags); 
     73      $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")'; 
     74      $result = mysql_query($sql); 
     75      while($tag_row = mysql_fetch_assoc($result)) { 
     76         $existing_tags[$tag_row['tag']] = $tag_row['id']; 
     77      }; 
    7778 
    78                 $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"'; 
    79                 $result = mysql_query($sql); 
    80                 while($tag_row = mysql_fetch_assoc($result)) { 
    81                         $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id']; 
    82                 }; 
    83         }; 
     79      $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"'; 
     80      $result = mysql_query($sql); 
     81      while($tag_row = mysql_fetch_assoc($result)) { 
     82         $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id']; 
     83      }; 
     84   }; 
    8485 
    85         foreach($tags as $tag) { 
    86                 if (!isset($existing_tags[$tag])) { 
    87                         // must be a new tag, register it 
    88                         $path = mysql_real_escape_string(preg_replace("/[^\w|\.|'|\-|\[|\]]/","_",$tag)); 
    89                         $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`,`tagdate`,`path`) 
    90                                         VALUES ("'.mysql_real_escape_string($tag).'","'.$path.'",NOW())'; 
    91                         print $sql; 
    92                         $result = mysql_query($sql); 
    93                         $existing_tags[$tag] = mysql_insert_id(); 
    94                 }; 
     86   foreach($tags as $tag) { 
     87      if (!isset($existing_tags[$tag])) { 
     88         // must be a new tag, register it 
     89         $path = mysql_real_escape_string(preg_replace("/[^\w|\.|'|\-|\[|\]]/","_",$tag)); 
     90         $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`,`tagdate`,`path`) 
     91               VALUES ("'.mysql_real_escape_string($tag).'","'.$path.'",NOW())'; 
     92         print $sql; 
     93         $result = mysql_query($sql); 
     94         $existing_tags[$tag] = mysql_insert_id(); 
     95      }; 
    9596 
    96                 if (!isset($existing_rels[$existing_tags[$tag]])) { 
    97                         // no connection between tag and picture? create if 
    98                         $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`,`tag_id`,`tagdate`) 
    99                                         VALUES ("'.$picture_id.'","'.$existing_tags[$tag].'",NOW())'; 
    100                         mysql_query($sql); 
    101                 }; 
    102         } 
     97      if (!isset($existing_rels[$existing_tags[$tag]])) { 
     98         // no connection between tag and picture? create if 
     99         $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`,`tag_id`,`tagdate`) 
     100               VALUES ("'.$picture_id.'","'.$existing_tags[$tag].'",NOW())'; 
     101         mysql_query($sql); 
     102      }; 
     103   } 
    103104 
    104         // now remove links to any tags that have been deleted 
    105         foreach($existing_rels as $tag_id => $pic_id) { 
    106                 if (!in_array($tag_id,$existing_tags)) { 
    107                         $sql = "DELETE FROM `".$TABLE_PREFIX."tag2picture` 
    108                                         WHERE `picture_id` = '$picture_id' AND `tag_id` = '$tag_id'";   
    109                         mysql_query($sql); 
    110                 }; 
     105   // now remove links to any tags that have been deleted 
     106   foreach($existing_rels as $tag_id => $pic_id) { 
     107      if (!in_array($tag_id,$existing_tags)) { 
     108         $sql = "DELETE FROM `".$TABLE_PREFIX."tag2picture` 
     109               WHERE `picture_id` = '$picture_id' AND `tag_id` = '$tag_id'";   
     110         mysql_query($sql); 
     111      }; 
    111112 
    112         } 
     113   } 
    113114} 
    114115 
  • branches/plogger-with-tags/unittests/tag-tests.php

    r395 r400  
    22   require_once 'lib/phpunit/PHPUnit.php'; 
    33   require_once 'lib/phpunit/PHPUnit/GUI/HTML.php'; 
    4     
    54   require_once '../plog-tag-functions.php'; 
    65 
    76   function implode_with_key($assoc, $inglue = '=', $outglue = ' & ') 
    87   { 
    9            foreach ($assoc as $tk => $tv) { 
    10                 $return = (isset($return) ? $return . $outglue : '') . $tk . $inglue . $tv; 
    11            } 
    12            return $return; 
     8      foreach ($assoc as $tk => $tv) { 
     9         $return = (isset($return) ? $return . $outglue : '') . $tk . $inglue . $tv; 
     10      } 
     11      return $return; 
    1312   } 
    14              
     13 
    1514   class TagTests extends PHPUnit_TestCase { 
    16        function test_parse_tags() { 
     15      function test_parse_tags() { 
    1716         $stuff = array(); 
    18           
     17 
    1918         $stuff = parse_tags("one    two"); 
    2019         $res_content = "{ ".implode(", ", $stuff)." }"; 
     
    5352         $this->assertContains("ten", $stuff, "Could not find tag 'ten' in: ".$res_content); 
    5453         $this->assertEquals(count($stuff), 5, "parse_tags result contains too many elements: ".$res_content); 
    55           
    56          //More ideas: 
    57          // "text containing <html> symbols & ;" 
    58          // "text containing UTF characters áéÊðöß" 
    59        } 
     54 
     55         // text containing HTML symbols 
     56         $stuff = parse_tags("<html> & ;"); 
     57         $res_content = "{ ".implode(", ", $stuff)." }"; 
     58         $this->assertContains("<html>", $stuff, "Could not find tag '<html>' in: ".$res_content); 
     59         $this->assertContains("&", $stuff, "Could not find tag '&' in: ".$res_content); 
     60         $this->assertContains(";", $stuff, "Could not find tag ';' in: ".$res_content); 
     61         $this->assertEquals(count($stuff), 3, "parse_tags result contains too many elements: ".$res_content); 
     62 
     63         // text containing UTF symbols 
     64         $stuff = parse_tags("áé íó úÜ Êß ðö"); 
     65         $res_content = "{ ".implode(", ", $stuff)." }"; 
     66         $this->assertContains("áé", $stuff, "Could not find tag 'áé' in: ".$res_content ); 
     67         $this->assertContains("íó", $stuff, "Could not find tag 'íó' in: ".$res_content); 
     68         $this->assertContains("úÜ", $stuff, "Could not find tag 'úÜ' in: ".$res_content); 
     69         $this->assertContains("Êß", $stuff, "Could not find tag 'Êß' in: ".$res_content); 
     70         $this->assertContains("ðö", $stuff, "Could not find tag 'ðö' in: ".$res_content); 
     71         $this->assertEquals(count($stuff), 5, "parse_tags result contains too many elements: ".$res_content); 
     72      } 
     73 
     74      function test_urlify_tag() 
     75      { 
     76         $this->assertEquals("%3Chtml%3E",urlify_tag("<html>")); 
     77         $this->assertEquals("this%26that",urlify_tag("this&that")); 
     78         $this->assertEquals("this%20%26%20that",urlify_tag("this & that")); 
     79         $this->assertEquals("here%20%2F%20there",urlify_tag("here / there")); 
     80      } 
    6081   } 
    6182?>