2011-07-08

Uncaught TypeError: Cannot read property 'form' of undefined

In my code, that JavaScript error was caused by missing "name" attribute of an "input" tag, having jQuery.validation class "required".

For example:

input class="required" id="agree" type="checkbox" value="yes"
produces the error

input class="required" id="agree" type="checkbox" value="yes" name="agree"
works fine

2011-06-24

Installing SphinxSearch with libstemmer from FreeBSD ports

Environment: Sphinx 0.9, FreeBSD

Problem:
Sphinx port has libstemmer options commented out in Makefile. With automatic upgrades, it loses stemming for anything except for default EN and RU.

Procedure of rebuilding the port with libstemmer

# (become root)
cd /usr/ports/textproc/sphinxsearch


Create file Makefile.local with this line:
CONFIGURE_ARGS+= --with-libstemmer


make clean
make
fetch http://snowball.tartarus.org/dist/libstemmer_c.tgz
cd work/sphinx-*/
tar zxvf ../../libstemmer_c.tgz
cd ../..
make
make deinstall
make reinstall
make clean

Do not forget to rebuild index if you did not use libstemmer before

2011-06-21

__autoload and Zend

Environment: PHP 5.3.6, Zend Framework 1.11.6

This problem occurred when I needed to build a mixed environment where the existing code was "pure PHP" and new code was based on Zend Framework.

The "pure PHP" code's __autoload function stopped working as soon as Zend application ran.

Apparently, when there is a call to spl_autoload_register, __autoload is ignored.

The solution was to rename __autoload to OldAutoLoad and call:
spl_autoload_register('OldAutoload');

Note: after that, the OldAutoload was called first and then Zend's autoload. In order to let OldAutoload ignore Zend classes, I replaced require_once calls with @include_once.

2010-12-17

PHP : "intl" extension, Windows, Apache

PHP Internationalization support ("intl") is bundled as of PHP 5.3.0 and does not require PECL:
http://www.php.net/manual/en/intl.installation.php

Things to check if it does not work under Windows/Apache:

  1. php_intl.dll exists in the "ext" folder of the PHP root ( normally it's c:\Program Files\PHP\ext )
  2. The php.ini file in c:\Windows folder has these lines:
    extension_dir="C:\Program Files\PHP\ext"
    extension=php_intl.dll
    (php.ini in c:\Program Files\PHP may work too, but I keep it in Windows folder so it's not accidentally removed when PHP is upgraded)
  3. There are ICU .dll files in c:\Program Files\PHP:
    icudt*.dll
    icuin*.dll
    icuio*.dll
    icule*.dll
    iculx*.dll
    icutu*.dll
    icuuc*.dll
     (* is version number, for example icudt36.dll)
  4. c:\Program Files\PHP is in the %PATH% (environment variables in Control Panel - System - Advanced - ...)
    Without that, the ICU dlls are not loaded by PHP as Apache module - no error message is displayed, but the extension is not in the phpinfo().

2010-11-23

PHP namespaces and autoload on UNIX

The namespace "path" symbol is backslash. Therefore, the full class name looks like:
MyNameSpace\MyClass. We need to replace the backslash with slash in the __autoload function, in order for it to work under UNIX. Under Windows, it works either way.

function __autoload( $class_name ) {
# ------------------------------------
$class_name = str_replace( '\\', '/', $class_name );
require_once $class_name . ".class.php";
}

2010-11-22

Enabling internal PHP fileinfo on FreeBSD

In a previous post (Upgrading to PHP 5.3 ...), I mentioned a PECL extension pecl-fileinfo-1.0.4

Apparently, installing that extension was a wrong move. The manual says:
This extension is enabled by default as of PHP 5.3.0. Before this time, fileinfo was a PECL extension but is no longer maintained there. However, versions prior to 5.3+ may use the  discontinued PECL extension. 
 Indeed, the PECL version did not work as expected, so I uninstalled it.


Then I discovered that on FreeBSD the internal version of fileinfo was NOT enabled by default.

Here is the relevant part of Makefile found in /usr/ports/lang/php5:
CONFIGURE_ARGS= \
                --with-layout=GNU \
                --localstatedir=/var \
                --with-config-file-scan-dir=${PREFIX}/etc/php \
                --disable-all \
                --enable-libxml \
                --with-libxml-dir=${LOCALBASE} \
                --with-pcre-regex=${LOCALBASE} \
                --with-zlib-dir=/usr \
                --program-prefix=""

 I created a Makefile.local file with just one line
CONFIGURE_ARGS+=--enable-fileinfo

After rebuilding PHP, this test showed that fileinfo is enabled:
$ php --rf finfo_open
Function [ function finfo_open ] {

  - Parameters [2] {
    Parameter #0 [ $options ]
    Parameter #1 [ $arg ]
  }
}

2010-07-06

Upgrading to PHP 5.3 ports on FreeBSD

Environment:
FreeBSD

php5-5.2.12         PHP Scripting Language
php5-curl-5.2.12    The curl shared extension for php
php5-dom-5.2.12     The dom shared extension for php
php5-gd-5.2.12      The gd shared extension for php
php5-iconv-5.2.12   The iconv shared extension for php
php5-mbstring-5.2.12 The mbstring shared extension for php
php5-mysql-5.2.12   The mysql shared extension for php
php5-openssl-5.2.12 The openssl shared extension for php
php5-pcre-5.2.12    The pcre shared extension for php
php5-pdo-5.2.12     The pdo shared extension for php
php5-pdo_mysql-5.2.12 The pdo_mysql shared extension for php
php5-session-5.2.12 The session shared extension for php
php5-simplexml-5.2.12 The simplexml shared extension for php
php5-spl-5.2.12     The spl shared extension for php
php5-xml-5.2.12     The xml shared extension for php
php5-xsl-5.2.12     The xsl shared extension for php
php5-zip-5.2.12     The zip shared extension for php
php5-zlib-5.2.12    The zlib shared extension for php
pecl-stem-1.5.0     A PECL extension which provides word stemming

Task:
Upgrade all ports to php5-5.3.2

  1. Some of the functions are deprecated in PHP 5.3. I had to replace all occurrences of split() with explode() or preg_split() and mysql_escape_string() with mysql_real_escape_string(). Note that mysql_real_escape_string() requires second parameter - DB connection.
  2. Attempt to upgrade ports with `portupgrade php5*` did not work. Some of the extensions do not exist anymore in PHP 5.3, and `pkgdb` was quite confused. I had to delete all php5 ports and then install them again.
  3. PHP 5.3 installed extensions into a new folder /usr/local/lib/php/20090626. My php.ini had reference to the old one, /usr/local/lib/php/20060613. Had to change that manually.
  4. pecl-stem-1.5.0 was not marked for upgrade (pkg_version). However, I had to rebuild it, because it was incompatible with the new PHP libraries.
  5. `php -i` complained about undefined timezone. I had to add date.timezone="America/New_York" to php.ini.
  6. I checked which extensions are now "included with PHP by default" (in docs on http://php.net and in /usr/ports/lang/php5-extensions.Makefile), and installed some of them, in addition to what I had before. There was also a nice "fileinfo" extension that I thought could be useful. Here is the list of extensions after upgrade:
php5-5.3.2_1        PHP Scripting Language
php5-ctype-5.3.2_1  The ctype shared extension for php
php5-curl-5.3.2_1   The curl shared extension for php
php5-dom-5.3.2_1    The dom shared extension for php
php5-filter-5.3.2_1 The filter shared extension for php
php5-gd-5.3.2_1     The gd shared extension for php
php5-hash-5.3.2_1   The hash shared extension for php
php5-iconv-5.3.2_1  The iconv shared extension for php
php5-json-5.3.2_1   The json shared extension for php
php5-mbstring-5.3.2_1 The mbstring shared extension for php
php5-mysql-5.3.2_1  The mysql shared extension for php
php5-openssl-5.3.2_1 The openssl shared extension for php
php5-pdo-5.3.2_1    The pdo shared extension for php
php5-pdo_mysql-5.3.2_1 The pdo_mysql shared extension for php
php5-session-5.3.2_1 The session shared extension for php
php5-simplexml-5.3.2_1 The simplexml shared extension for php
php5-xml-5.3.2_1    The xml shared extension for php
php5-xmlreader-5.3.2_1 The xmlreader shared extension for php
php5-xmlrpc-5.3.2_1 The xmlrpc shared extension for php
php5-xmlwriter-5.3.2_1 The xmlwriter shared extension for php
php5-xsl-5.3.2_1    The xsl shared extension for php
php5-zip-5.3.2_1    The zip shared extension for php
php5-zlib-5.3.2_1   The zlib shared extension for php
pecl-fileinfo-1.0.4 A PECL extension to retrieve info about files
pecl-stem-1.5.0     A PECL extension which provides word stemming