Part of the EllisLab Network
   
 
CI implementation of a tagging system / freetag?
Posted: 27 March 2007 01:39 AM   [ Ignore ]  
Summer Student
Total Posts:  16
Joined  11-05-2006

Has anyone written or ported a tagging system for Code Igniter? I’m looking for a tagging library that I can plug into CI.

Freetag (http://code.google.com/p/freetag/) is a very good tagging library, but it is written for ADODB. I’d port it over myself, but that would take a few hours to do properly and I was hoping someone might have a library ready to go.

Thanks in advance!

Profile
 
 
Posted: 27 March 2007 02:24 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

I wrote some tagcloud code a while ago. It can generate folksonomy (no <font> tags, yay!) and goes off a single comma separated list stored in a database (one list per tagged item). All the files are at http://wgilk.com/code/ci/tagcloud/. It’s probably not as advanced as freetag, but it probably wouldn’t be hard to duplicate the API.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 27 March 2007 03:26 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  265
Joined  03-26-2006

I’ve got a tagging system on Filepanda
Which uses 2 DB Fields… ‘tags’ and ‘files_tags’ to remove duplication.

So, in ‘tags’, you might have:

1 => necta,
2 => booya,
3 => groovy

and in files_tags:

file_id => tag_id
1
=> 3,
2 => 1

As a linking table.
It works great, and provides a much more normalized database structure.
I’ll post the actual code up as soon as I can.

Elliot

 Signature 

Want a job in a cool web 2.0 company? - doof is recruiting!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | CodeIgniter CMS
Twitter | Flickr

Profile
 
 
Posted: 27 March 2007 03:42 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  69
Joined  02-20-2007

I’m doing a similar thing to Elliot for an email address management application i’ve built for a client.  i also added ‘Collections’ to the tags (which are basically just tags for tags) which have more or less the same db structure, a table for the collections and a joining table for the tags.

Profile
 
 
Posted: 27 March 2007 06:16 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  265
Joined  03-26-2006

Ok, this is my tagging code:

$tags = str_replace(', ', ',', $this->input->post('tags'));
                
$tags = str_replace('_', ' ', $tags);
                
$tags = explode(',', $tags);
                
                foreach (
$tags as $tag) {
                    
                    $tag
= trim($tag);
                    
                    if (!empty(
$tag))
                    
{
                        $this
->db->where('tag', $tag);
                        
$query = $this->db->get('tags',1);
                    
                        if (
$query->num_rows() == 1 )
                        
{
                            $row
= $query->row_array();
                            
$tag_id = $row['id'];
                        
}
                        
else
                        
{
                            $tag_data
= array('tag' => $tag);
                            
$this->db->insert('tags', $tag_data);
                        
                            
$tag_id = $this->db->insert_id();
                        
}
                    
                        $tag_assoc_data
=    array(
                                                
'tag_id' => $tag_id,
                                                
'file_id' => $file_id
                                            
);
                    
                        
$this->db->insert('files_tags', $tag_assoc_data);
                    
}
                    
                }

It relies on 2 db tables: tags ( id [int, autoincrement], tag [varchar] ) and files_tags ( file_id [int, indexed], tag_id [int, indexed] ).

Then, to get a file’s tags you have to select from files_tags with a join on tags.

Hope this is helpful…
Elliot.

 Signature 

Want a job in a cool web 2.0 company? - doof is recruiting!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | CodeIgniter CMS
Twitter | Flickr

Profile
 
 
Posted: 27 March 2007 11:03 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

I considered using a table for tags, but I honestly think it’s less efficient to use a table for tags, unless you are trying to limit the name or number of tags. There’s a bit more processing involved with using a comma separated list, but imho, it’s more flexible.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 27 March 2007 03:32 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  265
Joined  03-26-2006

all in all… saving in a normalized database method has many advantages.

Firstly, you can keep the tags indexed within the db… which is more efficient than constantly sorting arrays.
Secondly, you save space… potentially huge amounts of space.
Third, you can mass ‘rename’ a tag…. for example, ‘coool’ > ‘cool’.......just edit one record in the DB, and every instance of that tag changes.
Fourthly, you can use the database of tags for other things too…
I have ‘tags’ and ‘files_tags’, but you could also have ‘blog_tags’, ‘images_tags’, all as linking tables to the ‘main’ database tags table.

As a final example… let’s imagine you have 100,000 files stored… and each has an average of 3 tags at 8 characters each.

Thats’ 24 characters + 2 for commas, for each file.
which comes to 2,600,000 characters of text… which is roughly translated at 2.4MB.
using a db, with a linking table, each tag reference is just 2 integer numbers.

2 x 3 = 6 numbers stored.
6 X 100,000 = 600,000   which is 0.57MB

As far as flexibility goes… using a normalized database is still just as flexible, you’ve just got to get your head around the queries that you need to use to do what you want it to do.

Elliot

 Signature 

Want a job in a cool web 2.0 company? - doof is recruiting!

Elliot Haughin CodeIgniter Blog | FilePanda - Free File Sharing | CodeIgniter CMS
Twitter | Flickr

Profile
 
 
Posted: 27 March 2007 04:00 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  16
Joined  11-05-2006

I appreciate the responses. Elliot, you make some great points. I’ve decided to atleast *attempt* to port Freetag over to CI. I’ve spent the last hour or so cleaning out all the ADODB functions and replacing them with the equivalent CI database functions.

Here’s what I’ve got so far. Keep in mind this is still a work in progress and I haven’t even begun to fully test it so I am sure there are plenty of bugs. If you attempt to use this class, please make sure you know what you’re doing and can help me fix the bugs and make improvements as needed. I know for a fact that there are things here that still need to be properly ported, but feel free to point out anything that may look amiss even if it is obvious.

Here it is so far:
http://codeigniter.com/wiki/Freetag/

Profile
 
 
Posted: 24 July 2007 05:39 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  946
Joined  06-11-2007

How would you guys go about implementing a general tagging library that could be used with multiple linking tables for various different things eg: blogs, videos, photos, etc.

Should I just adopt a naming convention for the tables and pass a name on initialization of the class?

meaning tables names are Video_Tags, Blog_tags, Photo_Tags and they all contain objectID and tagID. Then it will take the name of the object id field too. eg:

$options = array('Video_', 'videoID');
$this->load->library('tags', $options)

Should this be the best way to go? I didnt like how freetag restricts you to the table names, seems hugely useless to me!

 Signature 

PhilSturgeon.co.uk - Personal blog site using my new CMS.
StyleDNA Ltd - CodeIgnitor Web Development, Hosting and Design.
_________________

Helpers: Asset
Libraries: Layout, cURL, Command Line

Profile
 
 
Posted: 24 July 2007 06:52 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  42
Joined  07-14-2007

I know that a table based tagging system is the correct was to do these things, but you will get far better real world performance using your DB’s fulltext search function. For example, using a FULLTEXT index on a tags VARCHAR column in a mysql table allows you to write complex queries (”+cheese +mince -rice” means find all rows tagged cheese AND mince BUT NOT rice) that run very, very fast. Note from someone who has done this: My default mysql will not index words of 3 characters or less. Append underscores to tags shorter than this to make them 3 chars and strip them off later!

If you want REAL power you could use the sphinx fulltext search component (although its a pain to set up). But that would allow you to normalise terms (i.e. treat ‘machine’ and ‘machines’ as the same tag)....

Profile
 
 
Posted: 24 July 2007 09:10 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  946
Joined  06-11-2007

Got bored and made a mini one. Seems to work fine for what i’ve tested.

Temp Link

Theifed a few minor bits from freetag but hopefully this should give some people an idea of where to start on their own.

 Signature 

PhilSturgeon.co.uk - Personal blog site using my new CMS.
StyleDNA Ltd - CodeIgnitor Web Development, Hosting and Design.
_________________

Helpers: Asset
Libraries: Layout, cURL, Command Line

Profile
 
 
Posted: 21 October 2007 04:51 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Avatar
Total Posts:  19
Joined  06-13-2007

Elliot thx for sharing. Always checking your blog for new entries.

 Signature 

3,2,1,Ignite, Lift off cheese

Profile
 
 
Posted: 07 March 2008 04:42 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  8
Joined  07-14-2007

Elliot,
Can you please repost you tagging system? Looks like it’s been deleted from filepanda.

Interested in knowing how it works.
Cheers

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66410 Total Logged-in Users: 31
Total Topics: 84748 Total Anonymous Users: 0
Total Replies: 454808 Total Guests: 228
Total Posts: 539556    
Members ( View Memberlist )
Newest Members:  kizerdrixcaptainredmuffquinodligtharttechsivamDjordjesammozzazodman23mbsaarchaic