svnlogger v0.1

This one is for the Unix freaks (that includes me !).  Remember the textual “ChangeLog” files we used to see in a lot of open source softwares ? It used to have the list of changes made to the software along with revisions number, contributor’s name and timestamp. I always liked the format of a particular kind of ChangeLog which showed all the details in a compact manner without losing any, helps me keep track of ‘What’s new!’ in my next version of the software. Anyways, so how do I make it ? For those of you who have been thinking that they’re hand-written, you’re so wrong ! After all, how can you expect an open source freak to do something ‘manually’ when he can easily automate any task in his computer using cool scripts ?? Anyways, usually they have some kind of Version Tracking System like CVS, SVN or GIT. I happen to use SVN most of the time, actually always, and since I needed to maintain a ChangeLog as well, I found there was a nice command called “svn log” which would generate a textual log of all the commits along with all the information I needed to see. Well, everything was there, but not in a way I wanted it to be. ‘svn log’ is good enough for ‘grep’ but not so good in terms of direct readability. So I wrote this small shell script that is basically a wrapper around the svn log but it uses AWK to reformat the data in a way I like it !

Copy paste the code below into a file called ‘svnlogger.sh’ and then execute it like

> sh svnlogger.sh <path-to-svn-repo> <path-to-changelog>

If you don’t mention either of <path-to-svn-repo> or <path-to-changelog>, it would take the current directory as default. Here’s a sample of the ChangeLog generated :

Revision 285  by  abhishekdelta  on  2011-03-11 20:59:2
Description : Fixed URL Rewrite bugs #115 and #117
——————————————————————————–
Revision 284  by  chakradarraju  on  2011-03-11 18:21:0
Description : Ticket #122, activating/deactivating/deleting multiple users
——————————————————————————–
Revision 283  by  abhishekdelta  on  2011-03-11 04:21:2
Description : Auto detection of Rewrite engine during installation #98
——————————————————————————–
Revision 282  by  chakradarraju  on  2011-03-09 05:36:4
Description : Ticket #120 – Permissions improvement
——————————————————————————–
Revision 281  by  scriptle  on  2011-03-07 22:21:5
Description : User Public Profile – Redirection Only.
——————————————————————————–

….

Here goes the code :
Continue reading

Perfect configuration for Conky

Okay, this one’s about some eye candy. One thing I regretted while parting my ways with Ubuntu was the good looking themes and applets it had. But no more, as they say, ‘thou shall seek, thou shall find’ and I’ve found whatever I needed in Arch Linux. After configuring everything, from kernel modules to gnome, I finally came to peace with the last missing piece in my puzzle – the Conky system monitor. Configuring it to suit the looks of my desktop was a hell of a task but I finally succeeded thanks to numerous tutorials on the internet and needless to say, google. For starters, Conky is just a system monitor that can be configured to monitor everything from your CPU’s temperature to internet speed to processes and hard disk space. You can even configure it to monitor your email or some RSS feeds you like. And the best part, is the looks, okay, take a look :

You like it ? You can have it for yourself. If you’re in Arch Linux like me, just do

pacman -S conky

For any other distro, use the appropriate command. For Ubuntu,

sudo apt-get install conky

For Fedora,

sudo yum install conky

Finally, when you’ve done that. Its time to configure it. Below, I’m posting the perfect configuration which should run out-of-the-box in your system with no dependencies. Copy the below code to a file named “.conkyrc” and put it in your home folder. Final path should be : ~/.conkyrc

Once you’ve done that. Simply type “conky &” in the shell :)

Conky configuration :

# Conky configuration  by Abhishek Shrivastava [http://abhishekdelta.wordpress.com]
# Use Xft?
use_xft yes
xftfont DejaVu Sans:size=8
xftalpha 0.8
text_buffer_size 2048

# Update interval in seconds
update_interval 1

# This is the number of times Conky will update before quitting.
# Set to zero to run forever.
total_run_times 0

# Create own window instead of using desktop (required in nautilus)
own_window yes
own_window_transparent yes
own_window_type override
own_window_hints undecorate,sticky,skip_taskbar,skip_pager

# Use double buffering (reduces flicker, may not work for everyone)
double_buffer yes

# Minimum size of text area
minimum_size 150 0

#maximum_width 150

# Draw shades?
draw_shades no

# Draw outlines?
draw_outline no

# Draw borders around text
draw_borders no

# Stippled borders?
stippled_borders 0

# border margins
border_inner_margin 5

# border width
border_width 1

# Default colors and also border colors
default_color white
#default_shade_color white
#default_outline_color black
own_window_colour white

# Text alignment, other possible values are commented
#alignment top_left
alignment top_right
#alignment bottom_left
#alignment bottom_right

# Gap between borders of screen and text
# same thing as passing -x at command line
gap_x 40
gap_y 40

# Subtract file system buffers from used memory?
no_buffers yes

# set to yes if you want all text to be in uppercase
uppercase no

# number of cpu samples to average
# set to 1 to disable averaging
cpu_avg_samples 2

# number of net samples to average
# set to 1 to disable averaging
net_avg_samples 2

# Force UTF8? note that UTF8 support required XFT
override_utf8_locale yes

# Add spaces to keep things from moving about?  This only affects certain objects.
use_spacer none

TEXT
JEREME ARCH LINUX ${hr 2}

${voffset 2}${font OpenLogos:size=16}u${font}   Kernel:  ${alignr}${kernel}
${font StyleBats:size=16}q${font}   Uptime: ${alignr}${uptime}
${font StyleBats:size=16}A${font}   CPU1: ${cpu cpu1}% ${alignr}${cpubar cpu1 8,60}
${font StyleBats:size=16}A${font}   CPU2: ${cpu cpu2}% ${alignr}${cpubar cpu2 8,60}
${font StyleBats:size=16}g${font}   RAM: $memperc% ${alignr}${membar 8,60}
${font Webdings:size=16}~${font}  Battery: ${battery_percent BAT0}% ${alignr}${battery_bar 8,60 BAT0}
${font StyleBats:size=16}q${font}   Uptime: ${alignr}${uptime}

${color white}Cpu: ${color white}${alignr}$cpu%
${color white}${cpugraph 20,170 white white}

${color white}Ram:  ${color white}$memperc%  ${alignr 4}$mem/$memmax
${color white}${membar 3,170}

${color white}Disk IO:${color white} ${alignr 4}$diskio
${color white}${diskiograph 20,170 white white}
${color white}Root:  ${color white}${alignr 4}${fs_free /}
${color white}${fs_bar 3,170 /}

${if_existing /proc/net/route ppp0}
WIRELESS NETWORK ${hr 2}
${voffset 4}${font PizzaDude Bullets:size=14}O${font}${color white}Up:${color white}${alignr 4}${upspeed ppp0}k/s
${color white}${upspeedgraph ppp0 20,170 white white}
${voffset 4}${font PizzaDude Bullets:size=14}N${font}${color white}Total:${color white} ${totalup ppp0}
${voffset 4}${font PizzaDude Bullets:size=14}U${font}${color white}Down:${color white}${alignr 4}${downspeed ppp0}k/s
${color white}${downspeedgraph ppp0 20,170 white white}
${voffset 4}${font PizzaDude Bullets:size=14}T${font}${color white}Total: ${color white}${totaldown ppp0}
${voffset 4}${font PizzaDude Bullets:size=14}a${font}${color white}Local Ip: ${color white}${addr ppp0}
${else}${if_existing /proc/net/route eth0}
ETHERNET NETWORK ${hr 2}
${voffset 4}${font PizzaDude Bullets:size=14}O${font}${color 776241}Up:${color 707070}${alignr 4}${upspeed eth0}k/s
${color 3E3629}${upspeedgraph eth0 20,85 3E3629 776241}
${voffset 4}${font PizzaDude Bullets:size=14}N${color 776241}Total:${color 707070} ${totalup eth0}
${voffset 4}${font PizzaDude Bullets:size=14}U${color 776241}Down:${color 707070}${alignr 4}${downspeed eth0}k/s
${color 3E3629}${downspeedgraph eth0 20,85 3E3629 776241}
${voffset 4}${font PizzaDude Bullets:size=14}T${color 776241}Total: ${color 707070}${totaldown eth0}
${voffset 4}${font PizzaDude Bullets:size=14}a${color white}Local Ip: ${color white}${addr eth0}
${endif}${endif}

PROCESSES ${hr 2}
NAME $alignr PID    CPU
${top name 1} $alignr ${top pid 1} ${top cpu 1}
${top name 2} $alignr ${top pid 2} ${top cpu 2}
${top name 3} $alignr ${top pid 3} ${top cpu 3}
${top name 4} $alignr ${top pid 4} ${top cpu 4}
${top name 5} $alignr ${top pid 5} ${top cpu 5}
${top name 6} $alignr ${top pid 6} ${top cpu 6}

Hope you liked it and appreciated it. You’re free to change the above configuration to suit your needs and distribute it. :)

Upgrading to PHP 5.3 in Ubuntu

I’ve been using PHP 5.2 for a long time now. I’m not so apt towards change but PHP 5.3 has some cool features which I needed very much for my projects. So yesterday I decided to do that conversion. The process was not at all so simple and it took lots of googling and searching the forums, especially for removing the post-installation bugs. Finally, at Dec 27 12:45 AM I achieved 100% transformation to PHP 5.3. As my usual habit, I like documenting the stuff I do, so here it goes.

First thing before starting the process is to make sure that none of my existing projects would break. Because as the developers have said, PHP 5.3 is a major release with lots of changes. Many new features were added while many were removed or deprecated. And to avoid getting unnecessary warnings or notices, it was better to recheck my codes for compatibility with PHP 5.3. The PHP developers have written a good page in the PHP Manual which tells in detail about the things to take care of when migrating to 5.3 from 5.2. You can check it here : http://php.net/manual/en/migration53.php

So don’t forget to go through that page once, at the least the section which tells about the “removed and deprecated” features in PHP 5.3 and use the alternatives as suggested. Now you’re ready to upgrade. BUT, before that, you’ve to know that any software you use like phpMyAdmin or ldapPhpAdmin which uses PHP might broke. Well, you cannot do anything about it but you’ll have to reinstall them. Anyways, moving ahead, take a backup copy of your php.ini configuration file. You may need it for reconfiguring your PHP 5.3. If you’re using Apache 2, it can be found in /etc/php5/apache2/php.ini

Now, just run the following commands one by one,

wget http://us.archive.ubuntu.com/ubuntu/pool/main/k/krb5/libkrb53_1.6.dfsg.4~beta1-5ubuntu2_i386.deb

sudo dpkg -i libkrb53_1.6.dfsg.4~beta1-5ubuntu2_i386.deb

wget http://us.archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu38_3.8-6ubuntu0.2_i386.deb

sudo dpkg -i libicu38_3.8-6ubuntu0.2_i386.deb

Now open sources.list using the following command :

sudo vim /etc/apt/sources.list

Add the following sources and save the file-

deb http://php53.dotdeb.org stable all
deb-src http://php53.dotdeb.org stable all

Now rest of the commands :
sudo apt-get update

sudo apt-get install php5-cli

sudo /etc/init.d/apache2 restart

It may ask what to do about the new php.ini file. Choose to use the packager’s version of it. If its asks about removing phpmyadmin and would you like to use db-common, select NO if you want to preserver your databases. Thats it.

You’re done! Congratulations. You can check the version number from the output of phpinfo(); or in your command prompt type php -v to assure yourself. But the game is not over yet :) for the post-installation bugs are still roaming everywhere in your PHP installation. The bugs may vary for you, as it depends on the exact installation specifications, but I’ll tell you about the bugs I faced and if you have something in common, you can use the solution that worked for me. Lets refer to Post-Installation Bug as “PIB“.
Continue reading

PHP Optimizations

PHP is my favourite web development language. Here are a few tips I follow to make it even more faster :

  • When you’re creating PHP files, you’ll start with <?php tags as usual, but DON’T end the file with ?> tags if its not your main index.php file. Its optional and ignoring it is not just safe but recommended. So always leave the <?php tags unclosed in files which you are going to include in your main file. Do close it properly in your main index.php file though.
  • Don’t use functions in FOR loop condition if the function returns the same result always e.g.

for($i=0; $i&lt;length(arr); $i++)

IS VERY VERY SLOW than
for($i=0, $N=length($arr); $i&lt;$N; $i++)

That’s because since the condition is checked on every iteration. The function is called again and again to check the lenght of the same array every time. Instead since you know the array length is the same, why not save it in $N so that it won’t have to calculate it again and again. Though if your array $arr may change in length, you can use that.

  • Initialize all variables before using, just like C++. Although PHP allows uninitialized variables, DO NOT use them. Don’t forget, this is one of the most important optimizations.
  • There are global variables in PHP which you access via : global $var; . But if you’re operating on global variables more than twice in the same function, then its better to copy them into a local variable and then do it and then update it again on the global variable. Operating directly on the global variables is quite slow.
  • By the way, try to avoid using global variables altogether. Don’t declare your variables global unless you really don’t have any other option left.
  • If you’re using boolean expressions, then use “true” instead of “TRUE” and “false” instead of “FALSE” (quotes for clarity). Although both behaves the same, but small lettered ones are faster (yes, I know, but its true!)
  • Don’t create variables to store temporary data unnecessarily if you can easily avoid them e.g. $tmp = func(); return $tmp; Instead use : return func();
  • Just an optional optimization : Static methods are faster than non-static. If you think your method can be made static, make them. Though no compulsion in this particular case.
  • Although PHP will automatically destroy variables when their scope ends, still if you’ve created some big variable with huge amount of data, use unset($var) to free memory as soon as you no longer need that variable. Don’t wait for the scope to destroy it. Again, this is also optional and apply it only if you feel your variable can be that huge.
  • Avoid using require_once,include_once,__autoload,__get,__set . They’re very costly.
  • If you’re using include and require, give full paths instead of relative paths to avoid PHP wasting time in resolving OS Path.
  • Try to avoid regular expressions matching, they’re very slow. If you want to use simple validation, check out the ctype API. It can be found under section “Function Reference” > “Variable and Type Related Extensions” > “Ctype” in the PHP Manual. I hope you’ve downloaded PHP Manual. There’s no best way to learn PHP than from the Manual. It has lots and lots of examples.
  • Don’t suppress errors (if you intend to) using ‘@’ in front of functions. They’re very slow.
  • Whenever you’re creating strings, use single-quotes instead of double-quotes wherever possible. But remember that you cannot put variables inside single-quotes strings, in that case use double-quotes. e.g.

$name="abhishek";

$hello=" Hi $name, How are you? ";

return $hello;

CAN BE OPTIMIZED TO
$name='abhishek';

$hello=" Hi $name, How are you? ";

return $hello;

MORE optimization can be done by avoiding $hello variable and directly putting that in the return statement like
return " Hi $name, How are you? "

  • Use ‘foreach’ loops instead of ‘for’ loops wherever possible
  • Use ‘while’ loops instead of ‘for’ loops wherever possible and if you can’t even use ‘foreach’ loops there
  • Instead of concatenating strings during echo, use the echo comma operator e.g.

echo $hello.$name." You last logged in on $time". $bye;

use
echo $hello,$name,"You last logged in on $time",$bye;

  • echo is quite slow. So don’t echo many times. Instead as I mentioned in my last point, get all the variables and then pass them in a single echo command using the comma operator.
  • There are some pretty cool PHP extensions available out there. One of them is Memcache that will cache your PHP generated HTML pages and if there’s a HIT in the cache on a page request, your PHP code will not have to be reinterpreted. Caching can improve your code performance by 200 – 300 % ! There are other caching softwares also like PEAR Cache, Cache Lite, etc. Check it out under “Appendices” > “Extension Categorization” > “External Extensions” > “Memcache” in the PHP Manual.
  • Output buffering is another way to optimize transportation of HTML output to the user. You can use the ob_start() function in the beginning of the code and all the echo functions after that will not actually send the output to the client, but will send it to the output buffer. Once you’re echoing part is done, use ob_flush() to send the data to the client in one go. You won’t need this though if you apply the comma-separated echo trick properly. You can learn more about it under section “Function Reference” > “Affecting PHP’s Behaviour” > “Output Control” in the PHP Manual.
  • Another way is to compress your HTML code before sending it to the client. A ob_start(“ob_gzhandler”); in the beginning of your code will do the trick and similar ob_flush() can be used to send the compressed output. But note that while this will save the network bandwidth, it will increase the computation load on your CPU for compression. So there’s a trade-off in this case.
  • Now something about SQL queries, DO NOT USE mysql_query() inside a loop ! Instead, first build the query using the loop and then use a single mysql_query() to execute it.
  • But actually, for security reasons, don’t use mysql_query() at all and use Prepared MySQL queries. This will not only relieve you from having to worry about SQL injection attacks, it will also does some query optimization at its end to server you better. PHP already comes with PDO support. Check it out under “Appendices” > “Extension Categorization” > “Bundled Extensions” > “PDO” in the PHP Manual
  • Enabling Apache’s mod_deflate and mod_gzip can also help.
  • Set “AllowOverride” to “None” in the Apache configuration if you actually don’t need it.
  • Always put static pages into a separate server than the one hosting your PHP codes. And use some fast filesystem server to access them like thttpd.

That’s all I could think of for now. More tweaks can be done from Apache’s side also to improve the performance.