Changeset 400
- Timestamp:
- 05/09/06 19:21:16 (3 years ago)
- Location:
- branches/plogger-with-tags
- Files:
-
- 2 modified
-
plog-tag-functions.php (modified) (2 diffs)
-
unittests/tag-tests.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/plogger-with-tags/plog-tag-functions.php
r399 r400 2 2 3 3 function parse_tags($str) { 4 // 1. compress all extra whitespaces5 $str = preg_replace('/\s{2,}/',' ',$str);6 // 2. extract any phrases in quotes7 preg_match_all('/(\'|")(.*?)(\1)/',$str,$phrases);8 // 3. now remove the phrases from the string9 $str = preg_replace('/(\'|")(.*?)(\1)/','',$str);10 // 4. get rid of whitespaces at the end that may have been left11 $str = trim($str);12 // 5. get single words13 $words = preg_split('/\s+/',$str);14 // 6. merge single words and phrases15 $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); 16 16 17 return $tags;17 return $tags; 18 18 } 19 19 20 20 function urlify_tag($tag) { 21 21 // 1. format the incoming tag to use in a URL 22 return rawurlencode($tag); 22 23 } 23 24 24 25 function 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(); 29 30 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; 36 37 } 37 38 38 function get_popular_tags($ count_limit) {39 // Return a list of the $ count_limit most popular tags39 function get_popular_tags($limit) { 40 // Return a list of the $limit most popular tags 40 41 } 41 42 … … 61 62 62 63 function 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); 67 68 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 }; 77 78 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 }; 84 85 85 foreach($tags as $tag) {86 if (!isset($existing_tags[$tag])) {87 // must be a new tag, register it88 $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 }; 95 96 96 if (!isset($existing_rels[$existing_tags[$tag]])) {97 // no connection between tag and picture? create if98 $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 } 103 104 104 // now remove links to any tags that have been deleted105 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 }; 111 112 112 }113 } 113 114 } 114 115 -
branches/plogger-with-tags/unittests/tag-tests.php
r395 r400 2 2 require_once 'lib/phpunit/PHPUnit.php'; 3 3 require_once 'lib/phpunit/PHPUnit/GUI/HTML.php'; 4 5 4 require_once '../plog-tag-functions.php'; 6 5 7 6 function implode_with_key($assoc, $inglue = '=', $outglue = ' & ') 8 7 { 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; 13 12 } 14 13 15 14 class TagTests extends PHPUnit_TestCase { 16 function test_parse_tags() {15 function test_parse_tags() { 17 16 $stuff = array(); 18 17 19 18 $stuff = parse_tags("one two"); 20 19 $res_content = "{ ".implode(", ", $stuff)." }"; … … 53 52 $this->assertContains("ten", $stuff, "Could not find tag 'ten' in: ".$res_content); 54 53 $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 } 60 81 } 61 82 ?>
