Archive for the ‘Programming’ Category

Finding matrix’s max or min element in MatLab

Function max(A) (or min(A)) return a row vector with maximum (minimum) elements from each column of matrix A. If you need to find largest or smallest element in the vhole matrix, you should use max() or min() function once again: max(max(A)) or min(min(A)).

Sunday, May 18th, 2008

Prototype.js formElmt.request() and non-valid HTML

Handy and pretty $('myform').request() can be easily broken if you have really broken HTML. For example:

<table>
<form id="myform">
  <input />
  <input />
  <tr><td></td></tr>
</table></form>

$('myform').serialize() will return empty string for such html. As a result you’ll have ajax request with no parameters, obviously not what you’ve expected. Fix html and everything will work.

Friday, May 16th, 2008

Tracking deployed svn revision

Sometimes it’s useful to know which subversion revision is installed on your website.

I propose simple solution for this. Each time you or somebody update your website, run the command:

svnversion /path/to/your/working/copy > version && scp version login@domain.tld:./version

It will get global revision from your working copy and uploads a file to website, so you can check revision anytime just by opening http://domain.tld/version

Saturday, April 5th, 2008

Exclude WordPress admin from AWStats statistics

This is dead easy, in AWStats config set parameter

SkipFiles="REGEX[^\/wp-admin]"

All newly parsed statistics won’t include admin.

Wednesday, April 2nd, 2008

URL file-access problems in PHP

For many hosting providers it’s standart to disable URL file access. So, if your PHP code has file_get_contents(), require(), require_once(), include() or include_once() commands with some URL as a parameter, you will receive an error message: ‘URL file-access is disabled in the server configuration’.

To avoid this error you can try to use CURL.

Instead of this command:

$content = file_get_contents('http://sample_url');

You should use:

$ch = curl_init();
$timeout = 3;
curl_setopt ($ch, CURLOPT_URL, 'http://sample_url');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);

Sunday, March 30th, 2008

Distance measurement between two locations on Earth in MatLab

Suppose, you need to calculate distance between two locations on Earth in kilometers. And suppose you know their geographical coordinates, i.e. [latitude, longitude]. In MatLab there is a function distance(lat1, lon1, lat2, lon2). It calculates distance between two points in angle units (in degrees there). So, you can calculate distance between two objects in angle units. Next you should to convert angle units into kilometers. Use for this distdim(angle,’deg’,’kilometers’) function.

For example you need to find distance between Minsk, Belarus (53.9, 27,5667 [53° 54′ 0″ N, 27° 34′ 0″ E]) and Caracas, Venezuela (10.5207, -66.9245 in [10° 31′ 14.52″ N, 66° 55′ 28.2″ W]).

Type:

>> distdim(distance(53.9, 27.5667, 10.5207, -66.9245),'deg','kilometers')

Result will be 9355.484 kilometers.

Wednesday, March 19th, 2008

Translating symfony validator yaml configs with xgettext

Previously I wrote how to automate routine translation work with xgettext for symfony framework. Unfortunately xgettext cannot parse YAML configs. Obviously you’ll need to translate at least validator error messages. rgettext is not capable too.

I found some solution but I didn’t like it. I wanted to use PoEdit tool to have possibility for automated translation, syncing with sources and easy interface. So I wrote modified task which looks for all validation configs and creates fake php files with messages, which can be parsed by xgettext.

Usage is simple, just copy attached file to symfony/data/tasks in your PEAR directory and run symfony i18n-fake-php app_name. PHP files will be created in validate/ directories. After that you can use xgettext.

sfpakei18nvalidatorsphp.gz

Sunday, March 9th, 2008

Rsync using checksum in Symfony

Symfony framework allows easy deployment of the project. All you need to deploy using rsync – is to type symfony sync production (add go to the command for real deploying). It takes connection parameters from config/properties.ini.

However it is using files comparison by size and modification date, default for rsync utility. It isn’t in symfony documentation, but you can easily change rsync parameters to whatever.

Default parameters symfony uses are -azC --exclude-from=config/rsync_exclude.txt --force --delete. To use checksum comparison add the line to [production] in properties.ini:

parameters="-azC --exclude-from=config/rsync_exclude.txt --force --delete --checksum"

Wednesday, March 5th, 2008

Form-building components for Joomla

Recently I was creating contact form for one Joomla-based site. It was a standard contact form, everybody did similar hundred of times. But this time I decided to use one of existing components for building forms. There was one commercial, but it wasn’t the case, I didn’t even give it a try.

First one I tried was FacileForms. I just picked up the first one in rating. Well, my first impression was positive. There was everything I needed. Component has a lot of examples and Javascript library. I spent about an hour building my form (there was quite a lot of fields). Finally it was done, I opened frontend for last time.. Fuck. All form elements were absolutely positioned with coordinates I gave through admin. Did you ever heard about absolutely positioned form? I don’t. I decided that it was my mistake or lack of documentation, so I dived into sources. Fuck.
$attribs = 'position:absolute;';. I throw it away. You can do the same or make a code contribution. I didn’t have a time.

Second component was ArtForms. It was much better for my needs, it has customizable template for each form element. And it has nice ajax-based admin, which is much more convenient for editing. But it has disadvantages too. When you create input elements you cannot assing “name” property to them. I could live with it so I stopped my searches.

Friday, February 15th, 2008

How to skip image alignment in WordPress and HTML

For example, you have posted picture with left alignment. And thus all the text is starting from the right side of the picture. How to skip this if you want only part of text to be from the side of the image?

You can use multiple <br> tags, but this is not a good idea in WordPress. It is not working there. It is not good for HTML also, because representation can be different for different browsers, displays etc. There is special parameter for <br> tag, which skip all image alignment. This is <br clear=”all”>

Thursday, February 14th, 2008