Porting my blog for the second time, restart |
Porting my blog for the second time, tags |
Porting my blog for the second time, comments
This is post #6 of my series about how I port this blog from Blogengine.NET 2.5 ASPX on a Windows Server 2003 to a Linux Ubuntu server, Apache2, MySQL and PHP. A so called LAMP. The introduction to this project can be found in this blog post https://www.malmgren.nl/post/Porting-my-blog-for-the-second-time-Project-can-start.aspx.
To become friend with LibXML you need to become friend with the CPAN documentation of the library http://search.cpan.org/~shlomif/XML-LibXML-2.0122/lib/XML/LibXML/Node.pod. First I wanted to be able to read in all parts of a comment and then when that works I would work on getting the data into the database. I had to read the attributes spam, deleted and approved. Then I had to figure out how to read the sub nodes of the comment. Here is the program while it could print the values to the screen:
for my $node ($xmldoc-≻findnodes("/post/comments/comment")) { print "Approved: " . $node-≻getAttribute("approved") . "\n"; print "Spam: " . $node-≻getAttribute("spam") . "\n"; print "Deleted: " . $node-≻getAttribute("deleted") . "\n"; print getfield("date", $node); print getfield("author", $node); print getfield("email", $node); print getfield("ip", $node); print getfield("content", $node); } sub getfield { my ($ip_strName, $node) = @_; my @fieldNodeList = $node -≻ findnodes($ip_strName); if (scalar @fieldNodeList ≻ 0) { return $ip_strName . ": " . $fieldNodeList[0] -≻ textContent() . "\n"; } else { return ""; } }
I could not get Perl to return the text of a sub node with one line of code so I made a subroutine getfieldto do it for me. This is what the program prints from my test file:
Approved: True Spam: False Deleted: False date: 2011-11-01 04:41:56 author: oil painting email: no@spam.com ip: 2.2.2.2 content: Oil paints are a great medium of art which has produced some of the greatest artists all over. If one is willing to buy [u]oil paintings[/u], it becomes essential to know at least the biggest artists of all time and their works in order to differentiate between a great painting and cheap paintings.
Now I can continue to load this into the database. When working my way to get the data to the database I decided to create a sub routine for getting the value from the attributes as well and I changed the name of the subroutine getfield into getField as well.
# Load comments. for my $node ($xmldoc-≻findnodes("/post/comments/comment")) { $dbh-≻do( 'INSERT INTO Comment (PostID, Date, Author, Email, IP, Content, IsApproved, IsDeleted, IsSpam)' . 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? )' , undef, $postID, getField("date", $node), getField("author", $node), getField("email", $node), getField("ip", $node), getField("content", $node), getBooleanAttribute("approved", $node), getBooleanAttribute("deleted", $node), getBooleanAttribute("spam", $node) ); } sub getBooleanAttribute { my ($ip_strName, $node) = @_; my $result = 0; if ($node-≻getAttribute($ip_strName) eq "True") { $result = 1; } return $result; } sub getField { my ($ip_strName, $node) = @_; my @fieldNodeList = $node -≻ findnodes($ip_strName); if (scalar @fieldNodeList ≻ 0) { return $fieldNodeList[0] -≻ textContent(); } else { return ""; } }
Notice that the variable $postID got its value earlier in the program when we created the post and retreived the auto increment value of the newly created post. Here is the data model for jensblog with entity Comment added to it.
You also notice that now I added a Tag entity as well. In the old blog I used the Tag feature only once in one blog post. I never understood the point of it. Now when I create my own blog engine I did some research on what the difference is between a tag and a category. There are many opinions about it but I think the best suggestion is that actually it is two different names for the same feature. You can use both as a way to structure the blog but when you use both at the same time you can structure the blog in two dimensions. I liked that idea and implemented the Tag entity exactly the same as Category. I can for example use the years as tags and remove those from the categories. Later on we will see how it turns out.
Next time I will work on loading the Tag entity from the XML files.
I was born 1967 in Stockholm, Sweden. I grew up in the small village Vågdalen in north Sweden. 1989 I moved to Umeå to study Computer Science at University of Umeå. 1995 I moved to the Netherlands where I live in Almere not far from Amsterdam.
Here on this site I let you see my creations.
I create, that is my hobby.