Archive for the ‘Programming’ Category

Symfony Eclipse integration plugin

I wrote simple symfony plugin that integrates symfony tasks into Eclipse/PDT.

Plugin features:

  • Run core tasks, plugin tasks and your custom tasks.
  • Ask interactively for required tasks parameters.
  • Tasks list is always up-to-date, it is updated each time you run clear:cache.

Basically this plugin just creates proper xml build-file for Ant Eclipse plugin. Here how it looks like:

(more…)

Sunday, October 24th, 2010

Running remote tasks with symfony

I was tired of logging to production servers and clearing cache each time after deploy. So I wrote this simple plugin which allows to run any remote tasks right from your dev environment. All you need now is to type something like:

./symfony project:remote production cc

To install plugin:

  1. download attached package oxtRemoteTaskPlugin-1.0.0.tgz
  2. install it
    ./symfony plugin:install oxtRemoteTaskPlugin-1.0.0.tgz
  3. clear the cache
    ./symfony cc

Configure remote host if you haven’t done that yet:

[production]
  host=your.hostname
  port=22
  user=username
  dir=/path/to/symfony/project/

Now you are ready to run remote tasks.
If remote task consists of more than one word, enclose it in quotes, just like you do for any command line argument:

symfony project:remote production 'cc --app=frontend'

This plugin is basically a wrapper for ‘ssh’ command. It won’t work on vanilla Windows.

Monday, October 18th, 2010

Symfony Doctrine Sluggable – rebuilding empty slugs

If you add slugs with Sluggable behaviour in the middle of your project you’ll have empty slugs for all old records. To fix it you need to save your models via symfony, slugs will be built automatically.

You can automate it with help of this simple task:
(more…)

Wednesday, August 25th, 2010

Background spell check in TinyMCE

TinyMCE has spellchecker plugin which checks your text when you click button. You can read how to setup it here.

However what we want is to have background spell checking when we type text. Just like in regular word processor. The idea is simple – trigger spell check each time we type something. There’s a problem if we’ll trigger it after each letter – spell check uses Ajax request to server script, it may be very slow. We’ll solve it by triggering spell checking within 3-seconds delay after user has stopped typing.

Here’s the code:
(more…)

Sunday, April 11th, 2010

Extra spacing in TinyMCE after pasting from Word

TinyMCE can add extra empty paragraphs when you insert text from Word. TinyMCE simply wraps each newline into paragraph.
To solve it you need to follow these steps:

  1. Enable paste plugin.
  2. Add handler for parsing inserted text

So your TinyMCE code will look like:

  tinyMCE.init({
        ...
      	plugins: "paste",
      	paste_auto_cleanup_on_paste : true,
      	paste_postprocess : function(pl, o) {
            // remove extra line breaks
            o.node.innerHTML = o.node.innerHTML.replace(/<p.*>\s*(<br>|&nbsp;)\s*<\/p>/ig, "");
        }
  });

See more info on the official wiki

Saturday, March 6th, 2010

Doctrine Sluggable and translit

Doctrine Sluggable behaviour doesn’t work with non-ASCII characters – it just removes them from the slug. For some languages, e.g. Russian with Cyrillic alphabet, it becomes unusable. The obvious way is to use transliteration.
Luckily Doctrine has easy way to do that, you can pass method for building slug:

Artist:
  actAs:
    Sluggable:
      fields: [name]
      builder: [oxtSluggableTranslit, urlize]

The easiest way I found to transliterate non-ASCII text is to use PECL translit package. For Debian-based systems you can install it with dh-make-php.
Here’s the code:

class oxtSluggableTranslit
{
  /**
   * Convert any passed string to a url friendly string. Converts 'My first blog post' to 'my-first-blog-post'
   *
   * @param  string $text  Text to urlize
   * @return string $text  Urlized text
   */
  static public function urlize($text)
  {
    return Doctrine_Inflector::urlize(transliterate($text, array('cyrillic_transliterate', 'remove_punctuation'), 'utf-8', 'utf-8'));
  }
}

You can view all available transliterate filters by running

php -r 'print_r(transliterate_filters_get());'

Monday, February 1st, 2010

Symfony, propel and long running scripts

For long running scripts in symfony with propel ORM you may experience problems with memory usage. To avoid them make sure to disable propel instance caching by calling this code at the beginning of the script:

Propel::disableInstancePooling();

Propel by default caches in memory all fetched database objects. That means that for scripts with extensive db usage or long running time you may hit php memory_limit sooner or later.
Starting with propel 1.3 shipped with symfony 1.3 and 1.4 which are about to be released you’ll be able to disable instance pooling in database yaml config.

Saturday, November 28th, 2009

symfony: sfValidatorFile – invalid mime type (text/plain) for PNG images

On some systems sfValidatorFile may give you an error when trying to upoad PNG images. It says that it has invalid mime type – text/plain, though you have 'mime_types' = 'web_images' and other image formats work well.

It may be caused by wrong result from mime_content_type() function. To check if it’s your case create simple php script with just one line:

echo mime_content_type('path_to_your_png_image');

If it will give you text/plain then you have three options:

  1. Upgrade your PHP to 5.3.0, though I doubt it’s available for most *nix distributions at this time.
  2. Install PECL package Fileinfo.
  3. The easiest, modify form class.

In first two options sfValidatorFile will use finfo_open function prior to mime_content_type and it should give correct results for PNG image. However, upgrading PHP or installing PECL package isn’t always possible.

The simplest way to fix this bug is to apply small modifications to your form class, just reorder mime type guessers:

public function configure()
{
  $this->validatorSchema['image'] = new sfValidatorFile(array(
      'required'	=> false,
      'path'       => sfConfig::get('sf_upload_dir') . '/directory',
      'mime_types' => 'web_images'
    ));
  $this->validatorSchema['image']->setOption('mime_type_guessers', array(
    array($this->validatorSchema['image'], 'guessFromFileinfo'),
    array($this->validatorSchema['image'], 'guessFromFileBinary'),
    array($this->validatorSchema['image'], 'guessFromMimeContentType')
  ));
}

In this case validator will first try fileinfo (which should be available since PHP 5.3.0), then 'file -bi' which should work correctly on *nix platform and only if both failed it will use mime_content_type

Monday, July 27th, 2009

Calling phing tasks from Eclipse

Here you can read how to run phing tasks using built-in Ant tool in Eclipse.

In this post I’ll describe what to do if your phing task requires user input. The solution is simple – ask for user property in ant and pass it back to phing:
(more…)

Thursday, June 11th, 2009

Add absolute link menu item to user menu in Drupal

First of all, user menu (local tasks) in Drupal cannot be edited via admin. To add items to the menu you need to create new module and implement hook_menu() in it.
The following example shows how to do it in Drupal 6:

function yourmodule_menu() {
  $items['http://some.url'] = array(
    'tab_root'	=> 'user',
    'tab_parent'	=> 'user',
    'title' => 'External URL',
    'page callback' => 'user_page',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_LOCAL_TASK,
    'weight'	=> 5
  );
 
  return $items;
}

Change ‘access callback’ to whatever function (or constant) you want, but don’t forget to set ‘page callback’. It is being checked when building menu structure even if we don’t have any page callback actually.

Thursday, May 21st, 2009