Spiga

Optimizing Zend performance – small tip

January 29, 09 by Gabi Solomon

If you started working with Zend Framework you have notice that it is a bit slower then your regular php application.
This is due more or less to the double sword of having such a big framework to work with.

There are a lot of tips and tutorial out there on how to optimize your Zend Framework, and in all of them you will see the recommendation to remove the require_once from the library and to use Zend_Loader. Wich is a good advice since all those require_onces do put some stress on your application.

But after a bit of testing i camed to the conclusion that Zend_Loader isn't so fast. So i decided to go revert to a simple solution: writing my own autoloader.
I just put this small function at the top of my zend bootstrap file.

PHP:
  1. function __autoload($class) {
  2.     require str_replace('_', '/', $class) . '.php';
  3. }

And because i wrote all my application using Zend / Pear naming standard it works pretty good.

Hope you find this useful.
Cheers

Upload Photos and Videos to Flickr with Flickr Uploadr

January 26, 09 by Gabi Solomon

I have been using flickr for a while now, i even got a Pro account :D but i got to admit that i always have craved for desktop uploading tool. As much as i like the flickr service the fact is that google's picasa has the upper hand when it comes to client software with its picasa client.

Well it seems Yahoo has finally has heard the cryings of its users and released a better client for uploading pictures and videos to your account : Flickr Uploadr 3.0.

The Uploader lets you select photos for upload, add tags, organize them into sets, and change privacy settings.

So basically it does what it says it does .. uploads your pictures. I would have been much more excited if they had integrated more suport for managing sets and colections.

 

Cheers

Multilanguage models in zend framework using Zend_Db_Table

January 20, 09 by Gabi Solomon

As i wrote in a previous post there are more ways to design a database for a multilanguage website, but the one i decided to go with is Coupled Translation table approach, where basicly for each table you have another one where you store a row for each language wich has the primary table id, language id and the other columns that need to be translated.

The Requirements and Implementation

Having this idea in mind i wrote a new class that extends Zend_Db_Table to provide an easy access to the translated tables. Basically the class a couple of things:
Read the rest of this entry »

Zend Framework View partial without reseting variables

January 18, 09 by Gabi Solomon

Important

Ignore this solution, there is already a solution in ZF implemented in View as Jani Hartikainen pointed out in the comment below.
Its $this->render('...');

Zend Framework has a view helper called partial that you can use to render other views in your view file.
This is very useful for reusing the html code.

The problem i sometimes faced with that partial helper is that it resets all the variables and you need to specify them as an array parameter if you want them to be available in the partial view rendered.

The solution

To overcome this all you need to do is make a new view helper ... i called mine GSD_View_Helper_Render witch extends the Zend_View_Helper_Partial and overwrites the methods that clones the view and resets the variables.

PHP:
  1. class GSD_View_Helper_Render extends Zend_View_Helper_Partial {
  2.  
  3.     public function cloneView()
  4.     {
  5.         $view = clone $this->view;
  6.         // $view->clearVars(); ... this was the line that resets all the variables.
  7.         return $view;
  8.     }
  9. }

Thats it. Hope this is of help to you.
Cheers

php class for Unicode Manipulation

January 03, 09 by Gabi Solomon

If you ever had to build a site in a language that had special characters or a multilanguage website, then you have had problems with UTF encoding for sure.

Well recently a new class was published on phpclasses.org by Rubens Takiguti Ribeiro called Unicode Manipulation that will solve your problems.
The class is a complete solution to manipulate Unicode encoded text with support for UTF-16 and UTF-32 besides UTF-8.

This class can be used to manipulate text with Unicode encodings.

It can perform several types of operations that involve text strings encoded as UTF-8, UTF-16 or UTF-32, like:

- Get the text sequence for byte order mark for little and big endian
- Convert a given character code to Unicode encoded text and vice-versa
- Get the byte length of a given Unicode encoded character
- Convert text encoding between UTF-8, UTF-16 and UTF-32
- Get a part of an Unicode encoded string from a given position and an optional length
- Get the string length of an Unicode encoded text
- Determine whether a given string has Unicode encoded text

[Class Page]

Hope you find it usefull,
Cheers.

Install Error: Missing Dependency: perl(URI) >= 1.17 is needed by package subversion

December 29, 08 by Gabi Solomon

Today i finally decided to go ahead with installing subversion, after a bit of reading i opened a ssh session and input :

[root@servername ~]# yum install subversion

But to my surprise i got an error :(

Error: Missing Dependency: perl(URI) >= 1.17 is needed by package subversion

After a bit of googling i managed to find a solution on a forum and i thought to post it here, maybe other will find it faster :D

First we need to download the perl(URI) with version greater than 1.17 using the following command.

wget http://yum.trixbox.org/centos/5/RPMS/perl-URI-1.35-3.noarch.rpm

Then to install the perl package :

rpm -i perl-URI-1.35-3.noarch.rpm

That is it, you can now install and configure subversion as normal.

Cheers.

Advanced guide to Aspire one

December 28, 08 by Gabi Solomon

A friend of mine just bought a e-pc from acer that runs a custom linux OS called aspire one.
At first sight it seems like it a great solution for basic users since it has a very friendly interface, and it comes preinstalled with a a pack of software that is generally used like IM client, email reader, browser, office suite and much more.

aspire one 10 OS

But although this looks nice, it becomes a problem when you want more from your epc, when you want to install new apps or want to customize something. You dont even have a right menu on the desktop.

How to enable advanced menu [small hack]

To do this you will need to go to Files > My Documents. In the window opened go to the File Menu and click on Terminal.
In the terminal window input xfce-setting-show and hit enter.
This will bring up the Xfce Settings Manager.

Click on the Desktop link and you will get the Desktop Preferences window where you must select the Behavior tab.
Now mark under Menus the Show desktop menu on right click option and close the window.

Now you will bring up the normal desktop menu when you right click somewhere on the desktop which contains more options than the limited user interface on the Aspire One.

Changing the root password

Now for even more advanced usage you will need to use the root user. But after a lot of searching i came to the conclusion that you cant find out what the password for the root is, instead you can change it.

To do so you must click on the desktop and open a terminal window ( you can find it under System > Terminal ).
In the terminal window first type "Sudo –su" and hit Enter.
Then type "passwd" and hit Enter.
You will now be prompted to enter the new password. After typing press enter and retype it.
That is it. You have successfully changed the root password :D

Installing new applications

Now that you have enabled the advanced menu and have the root password you can proceed to installing new applications.
This is quite simple, you just right click on the desktop and select System > Add/Remove Software.
You will be prompted to enter the root password and after you enter it the package manager will launch.
And now the install should be easy as cake and is similar to any Linux based OS.

Adding new shortcuts to your desktop

Even though you have enabled the new advanced menu and you can access your new installed software from that menu, it just doesn't feel right. I bet you want them to be included in the shortcuts from the desktop. Well that is just a bit tricky, you will need to edit an xml config file.
The address for it is :

/home/user/.config/xfce4/desktop/group-a pp.xml

Hope this was helpfull,
Cheers

Multilanguage database design approach

December 26, 08 by Gabi Solomon

Preinfo

Before you go right to the comment section and recommend gettext or other similar ways, know that i am talking about content that is manageable from an admin panel or is added by the user

Also this article is based on personal experience and is not necessary the best way to do this.

Building a metalanguage website poses a lot of problems, and one of them is how you store the content in the database for each language.

If you do a google search you will find little resources about it, and most of the are on forums. This seem a bit strange to me, so after i had decided on a database schema for a metalanguage website i decided to post it here in the hope that other people might find it useful and save them some googling time.

As far as i searched there are more or less 4 databases schemas for metalanguage website.

1. Column approach

This approach is very common and basically it duplicates the column of content for each language.

table pages
-- id (int)
-- title_en (varchar)
-- title_es (varchar)
-- content_en (varchar)
-- content_es (varchar)

The way you would query it is by automatically selecting the right columns according to the language chosen:

SQL:
  1. SELECT `id`, `title_en` AS `title`, `content_en` AS `content` FROM `pages`

Or you could select all and do the column selection from php :

PHP:
  1. echo $rowPage['title' . $_SESSION['currentLanguage']];

Advantages

  • It doesn't have duplicate content, since there is only one row for each record, and only the language columns are duplicated
  • Easy to implement

Disadvantages

  • You need to build the watch what column you are working with depending on the language
  • Hard to maintain. Although this is a easy way for 2-3 languages its becomes a real drag when you have a lot of columns or a lot of languages
  • Hard to add a new language

2. Multirow approach

Another approach that i saw but i have never worked with it. It is simillar to the one above but instead of duplicating the content in columns it does it in rows.

table pages
-- id (int)
-- language_id (int)
-- title (varchar)
-- content (varchar)

So you will basically have 3 rows for the same page if you have 3 languages. The main problem i see with this approach is that it would be a bit tricky to know witch id you will use for the table relations.

Sorry but since i dont really have experience with this i cant show you sql & php examples.

Advantages

  • Ease in adding a new language

Disadvantages

  • Need to watch the table relations
  • A lot of duplicate content. You will have duplicate content for all the columns that are not translated

3. Single Translation table approach

This is an approach that becomes a little more complex then the other 2, but it is more suited for dinamic websites and which have a large number of languages or which intend to add a new language in the future and want to do it with ease.

table languages
-- id (int)
-- name (varchar)

table pages
-- id (int)
-- language_id (int)
-- title (int fk)
-- content (int fk)

table translation
-- id (int)

table translation_entry
-- translation_id (int)
-- language_id (int)
-- content (text)

In this approach you would store the id from the translation table in the title and content columns from the pages table, and then do a join with the translation_entry table based on the language id.

Advantages

  • Proper normalization
  • Ease in adding a new language

Disadvantages

  • Longer joins and query to get the content
  • All the translated content goes into one table
  • For me it just looks hard to work with and maintain

4. Coupled Translation table approach [my aproach :D ]

This is a variation of the above approach that to me seems easier to maintain and work with.
Instead of having just one translation table, you have one for each table. and you move the columns from the pages that need to be translated to the translation table.

table languages
-- id (int)
-- name (varchar)

table pages
-- id (int)

table pages_translation
-- id (int)
-- page_id (int)
-- language_id (int)
-- title (text)
-- content (text)

To get your data you just do a simple join:

SQL:
  1. SELECT * FROM `pages` JOIN `pages_translation` ON `pages`.`id` = `pages_translation`.`page_id` WHERE `map_landmarks_translation`.`language_id`='1'

Advantages

  • Proper normalization
  • Ease in adding a new language
  • Easy to query
  • Columns keep there names

Disadvantages

  • You have to create translation tables for all your tables that have columns that need to be translated

Conclusion

I am sure that there are other methods of doing a multilingual website, this are just the ones that i thought are most commonly used. My solution is the best, its just the best for me, because it works for my project and its easier for me to work with compared to other approaches.
In the end the best approach is the one that is the best for you. The one that you find the most easier to work with and maintain.

Cheers,
and good coding.

Netbeans review

December 15, 08 by Gabi Solomon

There seems to be quite a few talks about netbeans these days. A lot of people blogged about it and sayd its the next big thing or something. I personally tested it a while ago, like 4-5 months i think, i dont remember exaclly what made me drop it so i said i must try this new version to see what all the fuss is about.

Now before i start, i want to give you a heads up :) . I am a big fan of zend studio, (am using 5.5, hate the 6 series) and all its features, so when i test a new editor i tend to compare it to zend. I know its not free and it really inst the cheapest editor, but i was lucky enough (or good enough, if i wasn't this modest) to win it as a prize at the Innovation Award on phpclasses.com.

Now back to NetBeans.

The first thing u should know is that its free. And this always counts as a big plus for any software.

As far as the interface is concerned, i like very much. Its very intuitive and simple to use. You have the general layout with projects on the left, content editor with all the basic features: tabs, sintax highlight, php code assist.
The first plus for netbeans would be the support for html, css, javascript and all the major javascript frameworks. Wich for some people count very much. This is something i admit i miss in Zend. There is a small support for html but that is it.

Another cool feature are the snippets (they caled them templates), this is when you tipe in class and it can automplete a basic class code for you. This templates can be edited so you can add any code snippet that u use very often here for faster writing.

I found that it has the functionality i didnt find in other editor except zend before. Things like :
- code assist for you own functions and classes
- really good code folding (some IDEs tend to get confused and have bad code folding)
- opening the file that contains the function/method on CTRL+click on the name (or any mouse + keyboard combination)

via http://codeutopia.net/

via http://codeutopia.net/

Altough some might disagree about the importance of these functions, i think they are crucial when working on big projects. I tend to forget where a function is on my projects after a while of not working on it, much more when it comes to projects running a script that is not coded by me. So the persons that don't think that is necessary ... try modifying lets say a magento powered store. Lets see how you find where a function is declared, and how much time you loose on this task.

So all these put together really made me think of switching to netbeans.
So i really gave it a try and started to use it to code a project.

I found that it is really great to work with, and personally it seemed a little faster then zend on loading times (sometimes zend would freeze a little on ALT+TAB).

Also found new features that i didn't find in Zend, that really were nice to have:
- Refactoring ( you can rename a variable or a method in your class and it gets renamed whereever its used in your project )
- ToDo list ( it has a small tab where it parses the files for TODO comment lines and shows them in that tab)
- phpdoc comments code assist
- a really nice debuger that integrates with Xdebug
- code assist offers a link to php website for the php functions
- support for CVS, Subversion and Mercurial

The bad side

At this point i was 95% convinced of switching to netbeans. But then it let me down. The Ftp support really sucked. This is another must have feature for me.
I am used of working on remote servers, dont really enjoy working localy. So i want a verry good FTP client in my IDE. I enjoy the way Zend has done it. You just add a ftp server and your done ... you can edit the files at will as if they were localy, you dont care how he downloads them makes the a temporary file etc. As soon as you hit Save the files gets uploaded to the server.

The way that netbeans does it is by downloading all the files localy at first, work on them locally and when you hit RUN in the IDE the ones modified gets transferred to the server. Wich might work when your developing a project on your own ... but when you develop with a team its just not going to work. Or even if your alone, but you just want to do some quick modification to a website that is online, its going to take a while to setup the project in netbeans.

I found a workaround to this problem, you could use an automatic syncronization tool like Fling to do the synk between the files online and the ones locally. But you must be carefull how you setup it so you don't overwrite others coworkers modification on the project.

Another smaller issue in netbeans is the file path. If your working on a file it doesnt show you the path of that file. So you dont know on wich file your actualy working since many times files have the same names either in diferent components of the same project or you can mistakenly edit the same file from a diferent project. It does display it as a tool tip on the tabs name but the best way i see this done is in the application title bar.

Conclusion

I agree that Netbeans is a great IDE and it has a lot of features, even ones i cant find in Zend, and if your a javascript developer thant this would be right for you. But until they redo the FTP client or someone writes a better FTP plugin, i cant use it. So i am stuck with Zend again :D
Altough i will be following the evolution of netbeans and maybe switch when they will have a better ftp support.

Cheers

using Zend Framework Language component

by Gabi Solomon

Today i will continue the series of articles on the zend framework and i am going to talk about how i come to use the language component from zend.

The way i decided to go with it is by having a controller plugin that starts the language component and also detects the language.

But first thing first. We need to make the url to allow us to change the language. And to do that i added a new rule in the bootstrap file:

PHP:
  1. $route = new Zend_Controller_Router_Route(
  2.             ':language/:controller/:action/*',
  3.                 array(
  4.                     'language'   => 'en',
  5.                     'module'     => 'default',
  6.                     'controller' => 'index',
  7.                     'action'     => 'index'
  8.                 )
  9.             );
  10.         $router->addRoute('lang_default', $route);

This rule will produce urls like:

http://www.domain.com/en/controller/action

http://www.domain.com/es/controller/action

I decided to go with this type of URL instead of caching or storing the language in the session to not have google consider our pages as duplicate content. I have read that it better to go with subdomains (ex: http://en.domain.com/controller/action ) that with directories but for now i will do it this way.

Now that we are still in the bootstrapfile, we will also add our plugin:

PHP:
  1. Zend_Controller_Front::getInstance()->registerPlugin(new GSD_Controller_Plugin_Language());

ANd now for the final touch the plugin:

PHP:
  1. <?php
  2.  
  3.  
  4.  
  5. /**
  6. * Front Controller Plugin
  7. *
  8. * @uses       Zend_Controller_Plugin_Abstract
  9. * @category   GSD
  10. * @package    GSD_Controller
  11. * @subpackage Plugins
  12. */
  13. class GSD_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract
  14. {
  15.        
  16.     public function routeShutdown(Zend_Controller_Request_Abstract $request)
  17.     {
  18.        
  19.         $locale = new Zend_Locale();
  20.        
  21.         $options = array('scan' => Zend_Translate::LOCALE_FILENAME);
  22.         $translate = new Zend_Translate('gettext', Zend_Registry::get('siteRootDir') . '/application/languages/', 'auto', $options);
  23.        
  24.         $requestParams = $this->getRequest()->getParams();
  25.         $language = (isset($requestParams['language'])) ? $requestParams['language'] : false;
  26.                 if ($language == false) {
  27.                      $language = ($translate->isAvailable($locale->getLanguage())) ? $locale->getLanguage() : 'en';
  28.                 }   
  29.         if (!$translate->isAvailable($language)) {
  30.             throw new Zend_Controller_Action_Exception('This page dont exist',404);
  31.         } else {
  32.        $locale->setLocale($language);
  33.             $translate->setLocale($locale);
  34.             
  35.             Zend_Form::setDefaultTranslator($translate);                                
  36.    
  37.            
  38.             setcookie('lang', $locale->getLanguage(), null, '/');
  39.             
  40.             Zend_Registry::set('Zend_Locale', $locale);
  41.             Zend_Registry::set('Zend_Translate', $translate);
  42.         }
  43.     }
  44. }

A few explanations on the code.
I am using gettext adapter with auto mode ... witch will basically crawl the language directory to detect what languages can you support. I also use it conjunction with Zend Locale and at the end i store the in the Registry.
Storing Zend translate in the registry will enable you to use it in the view in a very easy way using a built in helper.

Using translate in you controllers

PHP:
  1. $this->view->title = $this->view->translate('default-register-index-title');

Using translate in you views

PHP:
  1. <?php echo $this->translate('default-register-index-title'); ?>

As a note i am not using the full text for translate i am using keys that have a naming conventions like "module-controller-action-message". I find it much more easier then putting the whole string and also much more easier to maintain. Imagine if you misspelled a string, or want to modify it ? you will then have to modify it every in your code.

That is basically it.
Cheers

Update:
I have also published this plugin on phpclasses, you can find it here.