Categories
PHP

The Orchid Experience

For our next project at Leevio, it has been decided that we will use Orchid as our PHP framework. It’s a framework created by Hasin Hayder, the founder of Leevio and the PHP Guru of Bangladesh. Today I was assigned to make myself used to this framework. I don’t have any prior experience of working with a fully fledged frameworks. So I was a bit worried. I checked out the Orchid framework codes from the Google Code SVN repo and installed the framework in my localhost. First I failed to make it work because by mistake I have left the .htaccess file which was hidden in my file explorer by default. Soon, I understood that I have left behind the lifeblood of a framework — the URL rewriting definition in the .htaccess file. I quickly pressed Ctrl + H to unhide the hidden files and copied the .htaccess file into my www/orchid directory. Whoa! I was now able to see the welcome page at http://localhost/orchid/. While I must admit that Hasin vai has a worse sense of what a welcome page should look like, on the other hand I must admit that the messy page took much of my tension away 😉 😛

I examined the sample apps for a while but since I have no prior experience in a whole framework like Orchid, I was having a tough time understanding how the wheel rotates inside Orchid. But I could ocassionaly make some educated guesses and my asking Hasin vai proved that all of them were correct 🙂 I was now more comfortable at exploring this new Island that at first seemed to be so full of vicious creatures.

After a while, I realized that I am that sort of stupid who can’t understand theories until he has done some practical work. I decided to do something on my own. While browsing the source, I found a “cli.php” in the root directory of Orchid. Those who know me or have read enough of blogs, they’d know that I am badly fascinated by cli tools. Examining the cli.php file, I could figure out that this one creates skeleton codes for an app. The cli tool has two options — create a skeleton app or an app with pre-installed controller and views. I chose the second option.

First, I removed the samples those came bundled up with Orchid. Then I opened up my terminal and fired off typind the following codes:

That’s all I needed. First one changed the current working directory to the Orchid directory. The second one invoked the Orchid CLI tool to create a controller for “masnun”. Again, I should criticize Hasin Vai, the CLI tool prints nothing but a single line saying — “Welcome to the CLI tool of Orchid” or something like that. Though it gives an error message on failure, I don’t think thats any credit of the tool in question, rather the error message is generated by PHP itself. On the other hand, on success, you get not even a single line congratulating you or at least saying “hi”. I am very much offended by such rudeness 🙁

Anyway, I was now ready to view my app at: http://localhost/orchid/masnun/

It showed a simple text saying “Called from masnun/base method”. That made sense to me. There is a “base” method inside the “masnun” controller. I opened up the controller file and yeah, there really was a base method and another method named “hello” 🙂 I was curious to see, what this other method does. So, I visited out of curiosity the following URL:

http://localhost/orchid/masnun/hello/

The result was somehow obvious, yeah it printed “hello world!”.

Now I came to think about it. How does Orchid work ? Well, I was feeling stupid now, since it has now been quite clear. The URL format is:

/controller/method.

If you just visit the controller, the base method is called. That is:

“/controller/” == “/controller/base/”

It was easy as pie! Then I moved on to using templating. Opened up the view directory and found two files — “base.php” and “hello.php”. I had no doubt that they had markup for each of the method of the controller. I opened up the controller again to examine.

Aha… I don’t even have to explicitly define the view ? Cool… Hasin vai really has some talent then 😉 Every method auto loads the curresponding view 🙂 That cuts down the work of a programmer for sure. But wait! How can Hasin vai be this stupid ? What if I need to setup a different view ? Oh my god! I have to tell him about this.

I was frustrated with Orchid and closed the current working copies and started examining the sample apps. At that time, I realized that Hasin vai was not stupid at all, he’s a genius ! He does have the “setView()” method. I was relieved and feeling sorry for cursing the gentleman for a while. But still, I would defend myself because if Hasin vai had a good documentation, I would have known it long before and didn’t need to curse him at all. So, you see? It’s his fault after all and he does deserve some curses for not having a total documentation. What do you say ? 😉

I immediately used the following code in the hello method:

Wow ! It does work. I have been able to reproduce the base method view just by changing the view of the hello method. I am damn happy.

Later I found out that, there’s a “layouts” folder inside the “views” directory where I can define the global layout for a controller. I created a “masnun.php” in the layouts directory and put these code:

Cool ! The template was applied to the whole controller — that is to both the base and hello method.

I am just loving this framework! Orchid has some cool built in libraries for additional features. Have a look at it’s core features:

1. Support for Caching
a. SQLite Based Caching
b. MySQL Caching
c. Memcached Caching

2. Built in Unit Testing Support
3. Fast and Lightweight
4. Support for MySQL, MySQLi, SQLite, MSSQL and PgSQL using Native DAL and PDO
5. Support for Layout
6. Active Record
7. Easy Application Setup
8. Flexible Configuration Management
9. Builtin Profiling
10. I18N support
11. Session Manager (Native and DB Based)
12. Excellent support for benchmarking and profiling
13. Builtin JSON Encoder and Decoder
14. Google Chart API Support
15. Bundled JS Libraries with Gzip Support
a. Prototype
b. JQuery
c. SWFObject
16. Builtin Library for jQuery Effects and AJAX Support

PS: I am still learning Orchid and hope to keep posted about my progress. I am now concentrating on understanding the models system of Orchid 🙂 A big thanks to Hasin vai for this wondeful framework !

Categories
Uncategorized

Using SVN with GoogleCode Project Hosting

At Leevio, we are going to use SVN or Subversion a lot. For demonstration and playing with the SVN system I chose Google Code Project hosting since it’s free and feature packed. I am a beginner at operating SVN and I had to get a sandbox SVN server to practise what I learn.

I already had the http://masnun.googlecode.com ready for this. So, I started trying out SVN on this account feeling relaxed that no matter what changes I make, no serious harm will be done.

SVN is pretty easy in fact. I read the “SVN Book” and “The Visual Guide to SVN”. None of them helped me grab SVN like the shell command — “svn help”. It was self explanatory and had a list of all available commands.

To start, I had to create a local repository or better said a local mirror of the project. I did the following:

It created a directory named “masnun” inside my linux home directory after I authenticated with my googlecode password.

Now I was ready to add files. So I copied a file named “quotes” into the /home/masnun/masnun (the local mirror) directory and typed in:

I got a text editing environment to write the changelog. After typing in a message, I pressed Ctrl+O to write out and then Ctrl + X to quit the editor.

Now the changes were reflected in the local copies, I had to sync the server. So I used:

And it transmitted all the data to the server 🙂

Now, I made some changes and updated the file using the following command:

Again, wrote the changelog and then :

I liked the simplicity and hated that I spent up much time reading theories telling me why SVN replaced CVS 🙁

Well, now I decided to delete the file:

That was it… Easy !

Categories
Python

HTML and XML Parsing in Python using BeautifulSoup :)

I have been looking for a good library in Python for handling HTML and XML. I knew about BeautifulSoup but never cared about it much. But this time, when I was looking for a way to scrape web sites and harvest links using Python, I came across a nice tutorial that demonstrated the wonderful use of the “BeautifulSoup” module. I was amazed and decided to try it all by myself. It’s just amazing ! In fact it’s a shame if you have worked with Python but haven’t used this module. I later came to know that BeautifulSoup is very well known in the Python World.

How to use it?

First Download it and extract the archive. Then install the package. To install it, use the following command:

Once you install it, you can try if everything is okay by typing this code into the interactive python shell:

If you don’t get any error, then everything went fine and now we can start using the module.

The BeautifulSoup module has two prominent object definitions — BeautifulSoup and BeautifulStoneSoup. We use the first one for HTML parsing and the second one for XML parsing.

So, what are we waiting for ? Let’s dive deeper…

I have a webserver running and the URL at http://localhost/ holds the PHP Info page ( the output of phpinfo(); function of php ). We will play with that beautiful page in this session.

Lets fetch the HTML of that page first. We will use urllib.urlopen() to fetch the page and then the returned object to construct a beutiful soup object.

That’s it — we now have a soup object that we can use to browse the HTML document.

There are a lot of features of BeautifulSoup. But I am going to demonstrate how to find a special tag and extract the data inside.

From looking at the HTML source of the page, I know that the <td> that has class=”e” contains data about php settings. To find all tags that has certain attribute with a fixed value, we use the : findAll() method in this way:

That is, we pass the tag name and a dictionary of it’s attributes and their values to the findAll() method to get a list back with the results. Please remember that, the dictionary we pass to the findAll() method with the attributes should be named “attrs” otherwise it’d not work. It’s because that’s an optional parameter or so called **kwargs that is keyword arguments as key-value pairs. And to define them, we always need to declare the parameter name explicitly in the function definition.

So, after that we store the list as “list”. The len() function is a built-in function that we used to count the number of elements. Yes, there are 382 tags those match our query.

You can explicitly extract any single tag by typing that in the following method:

You can get the string inside a tag using the “string” attribute in this way:

Yeah, BeautifulSoup converts strings into Unicode by default. You can override this behaviour. But I am not going to cover that.

Remember, if one tag is nested in another, the parent tag might not return a string if you use the above method.

For more details about this super cool module, please read their documentation. It’s very user friendly, easy-to-understand and of course extremely informative.

I really love BeautifulSoup and Python ! 🙂