Monday, 26 January 2009

Doctrine or own database layer

Good Evening fellows,

Last night I commit some changes to github (see at last post). To test that code works properly, I checked out it. (Or checkouted? Sorry for that.) And I realized, I had to modify an accessibility modifier in Doctrine's code. (One variable from private to public.) I had to take this change, because Doctrine has it's own autoloader support. But at the beginning of my development, I decided to use as well. And as we know, two autoloader don't work at the same code. So I built in Doctrine's autoloader in my one, but to work fine, I had to access to $_loadedModelFiles. That's the story. However, I'm wondering, which way I should continue my development. What do you think? The options:

1 - I keep Doctrine, because it is awesome, and create a subclass of Doctrine to populate it with an accessor
2 - Drop this idea and write a thin database layer/connector with a medium level of security features

To make a good decision, I list some pros and cons:

Why I should keep Doctrine?
- it's fully tested
- secure
- OO based
- use the hydrated technology
- use cache
- you can describe relationships between tables, and Doctrine will find out when you try to use something like this: $user->boss->company->ceo->hometown->postcode
- its done
- universal (mysql, postrgesql, oracle, sqlite...)

Why not?
- its huge, probably you don't need it in small sites (and with large sites you chose another framework:)
- its not mine
- you have to download separately
- mess up my autoloader
- not the best for optimizing database queries (I think so, but it could be a very stupid thought)
- its huge (again)

I guess, Doctrine should have to go. But I will break my head on it this week.
Anyway, If you could suggest me anything... please, I would be glad about it.

Cheers!

Saturday, 24 January 2009

PHP development

Hi,

In this post I'm gonna write, what environment I'm working in. I'm always hesitating about what os should be used:
- linux (debian, ubuntu, slackware)
- solaris
- windows (xp, vista)
Making easy the choice, I'm usually forced using windows xp. (It must be some 'LOL' in your head.) I tell why. At first, Adobe Flash/Photoshop runs only under WinXP (and OS-X, but unfortunately I don't have enough money to buy a Mac, and iDeneb (http://ideneb.ihackintosh.net/) doesn't support audio). Next, in my humble opinion, that on TFT display only the plain old monochrome font rendering the best method to display texts. I hate when I can see the red and green/blue offset near the fonts face. And to be honest, some unix distributions have some problems in this field. Even if you managed to setup monochrome fonts in the base system, qt3 and qt4 applications, there are several place where ugly fonts left (eg. in browser). I think if you are a web developer, it matters. Anyway, from my side the main reason the adobe products. I hate to reboot systems every hour. That's, why I use XP. But, as you read before in my blog, I use VirtualBox as well (with Ubuntu on it):)
To code I usually use Eclipse PDT (now version 2.0, hurray!) or GVim. This morning I found a very exciting tool which for debugging PHP: http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/
It wasted half of the day to setup, but totally worth it. To raise your curiosity, I tell only a little about it (more on the page I linked):
With this Vim + remote XDebug you can follow the code running at runtime, line by line, function by function. You can see the function stack, breakpoints ... kicking awesome. (and a cool magic for those you want to enchant)
For tracking my code I get used to using GIT. I'm glad to know GIT. If you know too, you know what I mean, If not, just visit http://git-scm.com/ and discover it. Unfortunately msysGit got across my system (some memory dump, even I uninstalled CygWin completely), so I use Git on CygWin. It's convenient, and can run gitk with graphical interface. I had a small pain pushing my repo to github (because of ssh keys), but at the and it succeeded. If you are enough brave, visit my github repo at: http://github.com/itarato/easy-php-framework/tree/master
I decided to maintain this framework more frequent. Working with Drupal gives me a lot idea how a good framework has to be work. But of course, I don't want to steel:) [Think about it, for a minute! What is stealing? Especially in the development world. Not an easy topic. It needs bears to discuss. A lot!] Anyway its free as Drupal. So, nothing to worry about.
To finish my dev. environment, its important to tell some about the background:
- MySQL 5.06
- Apache 2.2
- PHP 5.2.7 (with APC, PDO, XDebug...)

Thats all folks, If I forgot something, I strive to recover that.
Cheers!

Friday, 23 January 2009

JQuery minimal validation plugin

Hi All,

Tonight I am gonna spread a dummy JQuery plugin:) At first I want to fix that I love jQuery. It's logical, well structured and awesome (like Barney Stinson). One boring night I wanted to create a jquery plugin, just for fun and see how it works. I decided to write a validation plugin. The topmost features of a validation plugins are the follows:
- disable submission until all the fields are fill the requirements
- display some information about the error's cause
- sign somehow the parts of the form are false
- if an item became valid, warning message will disappear (toggling)
And of course, there are several criteria related to different kinds of form elements:
- not empty (required)
- only digits
- email
- min/max value/length
- ... etc

I made the basic features with checking 'required' criteria. This sample for those guys (ladies) who recently started to work with jquery or javascript. I tested it on newest browsers (FF3, O9, IE7, C1), and worked fine on each of them. If you curious about some other criteria implementation or anything, I will happy to help you.

And the stuff... oh yes. It needs a form, like:
<form action="" method="post" id="form1">
<label for="name">Name</label>
<br />
<input type="text" name="name" class="required"/>
<br />
<label for="email">Email</label>
<br />
<input type="text" name="email" class="required" />
<br />
<label for="description">Description</label>
<br />
<textarea class="required" name="description"></textarea>
<br />
<br />
<input type="submit" value="Submit" />
</form>

and needs a style:
.validation {
background: #f55;
padding: 4px;
border: 2px solid #fff;
color: #fdd;
font-size: 10px;
float: right;
}

and finally needs to bind to a form via jquery:
<script type="text/javascript">
$("form#form1").validate();
</script>


That's it. If you want this working sample:
View it - http://www.bison.hu/public/sandbox4/
Download it - http://www.bison.hu/public/sandbox4/jquery_minimal_validation.zip
[after download - don't forget to get jquery####.js and link in the html]

Sleep well

Tuesday, 20 January 2009

Firefox bookmark export

Hi Everyone,

Once happened this situation. I wanted to export my bookmarks from Firefox (3.0.5), but no one *.html files were created. Ok, I thought, JSON format is still appropriate, but an error message occured, and the exportation was failed.
I find this bug report about it:
http://kb.mozillazine.org/Unable_to_export_bookmarks_to_HTML_file_-_Firefox
So maybe Avast antivirus and PC-Tools Spyaware Doctor was guilty for that matter. But I didn't tend to close those applications, so, here is a tricky way to solve the problem:
Open Google Chrome -> Import Bookmarks and Settings -> Select Firefox and the bookmarks checkbox -> Import ... and now you are able to export the same from Google Chrome.

Sometimes I'm thinking about a site I should develop for saving personal bookmarks online. I can imagine some enchanted feature in it, for example, at first, it could load a speed dial page (like Opera). Or there would be a scored list. So, if you visit a page more times than others, it would be on a better place in the ordered list. I think its sounds logical, because some pages are used more frequent, so its likely to search these links more times.
On the other hand, its on the web. Pretend you need an important website you save before, but you saved it at home, and you are somewhere in the world (or at work). So annoying. Maybe, at default all the links could be private, but you can share any of them (like Google Reader feeds). But now, it is just an idea. And as always, probably there are several sites exists already in the internet ... please inform us if you know a good one:) Thanks!

Cheers!

Friday, 16 January 2009

Save Skype chat history

Hi,

Tonight I want to present my first VBScript. Before having lunch, I had some free time, so I surfed the net, when bumped into the developer section at Skype.com. I remembered, my every OS-reinstall destroyed Skype chat history. But to be honest, the default format of the history filer is very messy, so it is difficult to read. I decided to delve into the SkypeAPI technology and it turned out that making simple scripts is so easy. You can use PHP, JavaSctipt, JScript, Java and VBScript. I choose the last one, because it seemed to be the most easy to run in Windows. You just put in a file with vbs extension, and run. So here is the code I wrote, I'll tell how it works soon:
' Define global variables
Dim oFSO, chat_file, folder_to_save
' Directory where You want to save history (you can modify it)
' Now it is relative, so it will be created where Your *.vbs script runs
folder_to_save = "SkypeChatHistory"
line_count = 0

' Create FSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
set_next_free_dir()

' Connect to Skype API via COM
Set oSkype = WScript.CreateObject("Skype4COM.Skype", "Skype_")
' Open skype, if it is not running
If Not oSkype.Client.IsRunning Then
oSkype.Client.Start()
End If

WScript.Echo "Skype history will be saved. Found " & oSkype.Chats.Count & " chat group."

' Iterate chats
For Each oChat In oSkype.Chats
names = ""
' First name is You, so it is unnecessary to keep
no_1st_flag = TRUE
For Each oUser In oChat.Members
If no_1st_flag Then
no_1st_flag = FALSE
Else
   names = names & "_" & oUser.FullName
End If
Next
get_file("chat" & names & ".txt")
chat_file.WriteLine(vbNewLine & "==== CHAT HISTORY (" & Replace(names, "_", "") & ") ====" & vbNewLine)
line_count = line_count + oChat.Messages.Count
' Fix by an anonymous commenter
If oChat.Messages.Count > 0 Then
For Each oMsg In oChat.Messages
' Fix by Vadim Kravchenko
On Error Resume Next
chat_file.WriteLine(oMsg.FromDisplayName & " (" & oMsg.Timestamp & "): " & oMsg.Body)
Next
End If
chat_file.Close
Next

WScript.Echo "Backup was finished (" & line_count & " line saved). You can find your chats in: ./" & folder_to_save

' Garbage collection
SET chat_file = NOTHING
SET folder_to_save = NOTHING
SET oFSO = NOTHING
SET oSkype = NOTHING

' Access to a file given by name
Sub get_file(file_name)
' Parameter fix by: rommeech
Set chat_file = oFSO.OpenTextFile(folder_to_save & "/" & file_name, 8, True, -1)
End Sub

' Find an appropriate directory the logs to save, however, to avoid collision with former dirs
Sub set_next_free_dir()
If oFSO.FolderExists(folder_to_save) Then
ext = 1
While oFSO.FolderExists(folder_to_save & "_" & ext) And ext < 100
  ext = ext + 1
Wend
folder_to_save = folder_to_save & "_" & ext
End If
oFSO.CreateFolder(folder_to_save)
End Sub

Save it to your Desktop (*.vbs), and double click on it. At the first time, Skype will ask you, whether you are aware of the security issues, and really want to add permission to connect the SkypeAPI ... if you trust me, just click OK. It will creates a folder called SkypeChatHistory and some files in it. Every filename consists the other member of the chat sessions, so, if you talked 3 times with your friend, John Doe, there will be a 'chat_John Doe.txt' in SkypeChatHistory with 3 conversation in it. If you are keen-eyed, you will notice conversation lines are in reversed order. --homework-- :)
Now, I think, this code is quiet enough to save messages, and learn from it the way it works. If you can write a really small function to invert the collection (oChat.Messages), please comment it, and I will extend the code. Perhaps I make a gui for it with Visual Studio, I don't know. It must be dozen of good Skype backup applications already.

Update: eigenein did a fantastic job making a Windows application for the same purposes: http://eigenein.github.com/skype-historian-website/

Sleep well!

Wednesday, 14 January 2009

Books from Amazon

Today finally I've got my books:

Code Complete (2nd edition)
Programming Ruby (2nd edition)
Pro Drupal Development (2nd edition)

Now I see, each one is second edition. Nice. I'd read before this books in pdf, but both of them consist over 700 pages ... and my eyes burn down if I read it on the screen.
I started to work with Drupal a month ago. It's really interesting, and a little complex at first sight. This book is the best I've heard. But I found some mistake in the sample code in the book:
  • page 23: the 'variable_del(...' line is unnecessary, the page wasn't load with it for me.
  • page 151: need an extra line: in 'function joke_load($node) {
    drupal_add_js('misc/collapse.js');
    return db_fetch_object(...
  • page 216: after '} else if ($delta == 1 && user_access('administer nodes')) {' need an array definition:
    $items = array();

I suggest you to download the original source code from http://drupalbook.com, it has the correct code.
If anybody found some typo, please report me and I will put that in the post. Or if I wrong, tell that as well:)

Bye

P.s.: And one thing for beginners like me with Drupal! My first Drupal sandbox was in WinXP with normal php module collection + APC. (PHP 5.2.8, MySQL 5.06, Apache 2.2) But when I called the modules page in the admin section, it took 17 seconds. Very long time. But I guessed its normal, because when Drupal loads admin module page, it reconfigures all of them, and crawl some new one, if it finds. But last night (just for fun) I tried it in Ubuntu (in VM), page loaded in 1-2 seconds. I was amazed. Even I didn't install apc in the unix environment. So, just count with it.
Anyway, there is a very good article, how to get Drupal to run faster:
http://www.pronovix.com/blog/my-favorite-drupal-performance-hacks

Tuesday, 13 January 2009

Search PHP function from terminal

Hi,

Last week I bought some unix books, beacuse I almost blowed up with the new Slackware. It is happened, that it didn't recognized my wifi network ... I am not aware of unix network layer. So I thought it is the right time to digg in the topic. By the way, my first issue is a shell script, which can find a php function with its phpdoc and exact location.
How it works?

# Usage: ./pfs <your dev. dir> <function name>, eg.:
./pfs ./project1 user_get


And the code itself (pfs):

#!/bin/bash
# Check params
if [ $# -lt 2 ]
then
echo "Missing parameters.\nUsage: php_function_search [path] [function name]\n"
exit 1
fi

echo

for extension in php inc php3 html htpl
do
for file in `find $1 -type f -name "*$extension"`
do
if [ -f $file ]
then
# Find function line
function_line_array=`cat $file -n | grep "function $2" | awk '{print $1}'`
for function_line in $function_line_array
do
comment_end=`expr $function_line - 1`
# Find the 1st comment line
comment_start=`cat $file -n | sed -n "1,$function_line p" | sed -n '/\/\*\*/ p' | tail -n 1 | awk '{print $1}'`
non_comment_start=`cat $file -n | sed -n "1,$comment_end p" | grep "^[^*]*$" | tail -n 1 | awk '{print $1}'`
if [ $comment_start ]
then
# Print doc
echo -ne "\033[00;32;40m"
echo "Found in <$file> on line ($function_line)."
if [ $comment_start -gt $non_comment_start ]
then
echo -ne "\033[00;36;40m"
sed -n "$comment_start,$comment_end p" $file
fi
echo -ne "\033[00;33;40m"
sed -n "$function_line p" $file
echo -e "\033[00;37;40m"
fi
done
fi
done
done


Some aspects. Function name covers the first part of the function name. For example 'pfs ./ user_get' will match to user_get, user_get_name, user_get_all ...etc. First, the directory given is walked recursively to find all files. Search in each file the function pattern. It gives the line numbers those are in. Then until! that line the script searches the last line starts with '/**' (phpdoc block start). And there is another barbarian hack. It searches the last noncomment line, because if the last noncomment line num is bigger than the last comment line till the function, that means the comment don't relate to the function. So we shouldn't list it. After all, all block displayed width different colors: echo -ne "\033[00;36;40m". Sometimes when you need to know, what parameters a specific function requires, or what is the functions exact name, or whether exists any alternative copies of the function... it seems useful.
Just try it, and if you find a bug, please report it, a fixed it immediately.



Cheers

Monday, 12 January 2009

Ubuntu terminal monochrome font

In Ubuntu (>= 8.07) sometimes you have to do trick to disable font hinting in terminal. (Because in tft I prefer monochrome fonts without hints and antialias.) The process works me everytime is the follows:

sudo -i
<type your password>
tar -cvvf font_links.tar /etc/fonts/conf.d/
rm /etc/fonts/conf.d/10-no-sub-pixel.conf
rm /etc/fonts/conf.d/10-antialias.conf
rm /etc/fonts/conf.d/10-hinting-medium.conf
rm /etc/fonts/conf.d/10-hinting.conf


And restart all terminals. The more elegant way might be dpkg reconfigure, but i like the barbarian way sometimes.

Love and Hate = Passion

OMG!

Today I planned to play with git, but as every time I plan something, something other ruins the whole day. It happened on an Ubuntu (8.10) guest (in Sun's VirtualBox 2.10), which is worked fine until now, but suddenly AltGr stoped working. There is no worse when you have to deal with such an annoying thing. I made some investigation about it, and try all the solutions, but nothing worked. Last, I reinstall it:(
By the way, if you are in the same situation, you should visit these sites:
https://help.ubuntu.com/community/ComposeKey
http://blog.andrewbeacock.com/2007/06/getting-right-alt-key-alt-gr-to-work-in.html

Day 2:
Right now I noticed, all the trouble occured after installing guest additions in VirtualBox. Oh mom!
And that is what I expected:
http://forums.virtualbox.org/viewtopic.php?t=12488&sid=dd3403d3d4ca6f14cbf2917d10bc1e89
The culprit was VBoxGuestAdditions version 2.10!!!. So, if you are in the same shoes, use VBoxGuestAdditions 2.06. Here is the solution:

# switch to super user mode
sudo -i
# install Dynamic Kernel Module Support
apt-get install dkms
# download the previous version of vbox guest addons
wget http://download.virtualbox.org/virtualbox/2.0.6/VBoxGuestAdditions_2.0.6.iso
# mount it
umount /media/cdrom
mount -o loop ./VBoxGuestAdditions_2.0.6.iso /media/cdrom
cd /media/cdrom
# install
# ideally it will reconfigure your current vbox driver set you installed before
./VBoxLinuxAdditions-x86.run


It worked for me:) I hope, Sun provides us a new fixed version in a short time. But I suggest you to take a snapshot sometimes in your VM.

Have a nice day!

Update:
Welcome again,

This morning a read my feeds and saw that a new VirtualBox version (2.1.2, bundled with a new VBoxGuestAdditions.iso) came to light:) [run and get it - http://www.virtualbox.org/] At that moment I downloaded and installed it. And of course setup the new VBoxLinuxAdditions-x86.run. Miracle happens:) Desktop integration works. Mouse integration works. AltGr works. Folder sharing works. SSH remote connection works. Thanks guys from Sun! Very great job!

Introduction

Hi Everyone,

My name is Peter. Once before, I had made a blog, but I thought, It was not gonna worth it. Now, ... I give it a shot. My goal is to practicing my English. On the other hand, sometimes I found myself in a situation, where there would be great to own a blog, where I could take my ideas. Nowadays, I've felt bored myself. I bought some Unix book, and there are another three under shipping from Amazon. I hardly can wait that. Maybe I make a post about books I keep very useful in IT. Oh yes. I did not mentioned. I am some kind of an IT guy. Not the smart one, but the enthusiastic one. I don't know, what blog I really want. I would be happy to receive your responses. If you want, ask anything about programming or web development. I want help. My knowledge is various. I know PHP, (of course html, css), JavaScript (and some JS framework), a little Java. Flash with ActionScript 3. Some Ruby stuff. And a bunch of common things as every normal human.
My original homesite: http://itarato.uw.hu/index.php
And my framework site: http://www.bison.hu

Cheers