Locale issues with Zend Lucene search

We use Zend Lucene Search on a PHP ecommerce website and ran into some issues where records added to the search index from the website interface weren’t the same as when created from the command line. It turned out to be a locale issue and setting the locale fixed the problem.

Error messages

A clue that something wasn’t working correctly was this error message:

iconv(): Detected an illegal character in input string

There are a variety of conversions that can be done which fix this error, but our problem appears to have been caused by a non-configured locale, defaulting to “C” and which does not support UTF8.

Check what locale is currently being used

In PHP, you can call “setlocale(LC_ALL, 0)” to find out what the locale is currently set as. Running through Nginx with PHP-FPM, it output this:

C

and from the command line this:

en_NZ.UTF-8

Running “locale” from a SSH terminal session output this:

LANG=en_NZ.UTF-8
 LANGUAGE=
 LC_CTYPE="en_NZ.UTF-8"
 LC_NUMERIC="en_NZ.UTF-8"
 LC_TIME="en_NZ.UTF-8"
 LC_COLLATE="en_NZ.UTF-8"
 LC_MONETARY="en_NZ.UTF-8"
 LC_MESSAGES="en_NZ.UTF-8"
 LC_PAPER="en_NZ.UTF-8"
 LC_NAME="en_NZ.UTF-8"
 LC_ADDRESS="en_NZ.UTF-8"
 LC_TELEPHONE="en_NZ.UTF-8"
 LC_MEASUREMENT="en_NZ.UTF-8"
 LC_IDENTIFICATION="en_NZ.UTF-8"
 LC_ALL=

which would indicate the CLI script is picking the locale from the system, but Nginx/PHP-FPM is not.

How to fix

It’s possible to set the default locale in the php.ini file, although it might not be a good idea if you run many websites on your server as it could cause issues.

Instead use the setlocale() to set it specifically for your website. Check out the www.php.net/setlocale manual page for more information about the function.

In my case, we did this:

setlocale(LC_ALL, 'en_NZ.UTF-8');

This then solved the issue with adding documents to the Lucene index.

Categories PHP

Enable & disable WordPress plugins from the command line

Sometimes installing or upgrading a WordPress plugin breaks both the frontend of your website and the WordPress admin too, making it impossible to disable the plugin with the user interface. If you have command line access, WP-CLI to the rescue!

Downland and install WP-CLI

Go to wp-cli.org to download and install the command line tool for managing WordPress.

List plugins

To list the WordPress plugins you have available, both active and inactive, run this:

wp plugin list

You’ll get something like this:

+---------------------------------+----------+-----------+---------+
 | name| status| update| version |
 +---------------------------------+----------+-----------+---------+
 | akismet| active| none| 3.1.5|
 | wp-conditional-captcha| active| none| 3.7.1|
 | feedburner_feedsmith_plugin_2.3 | active| none| 2.3.1|
 | gd-star-rating| inactive | none| 1.9.17|
 | outbound-link-manager| active| none| 1.11|
 | sexybookmarks| active| available | 7.6.1.9 |
 | tinymce-advanced| active| none| 4.2.5|
 | wptouch-pro| active| none| 2.7|
 | wordpress-seo| active| available | 2.3.5|
 +---------------------------------+----------+-----------+---------+

Deactivate a plugin

Let’s say you wanted to deactivate the wp-conditional-captcha plugin:

wp plugin deactivate wp-conditional-captcha

And the output:

Success: Plugin 'wp-conditional-captcha' deactivated.

To deactivate other plugins, replace wp-conditional-captcha in the command above with the plugin’s name.

Activate a plugin

And if you wanted to enable it again:

wp plugin activate wp-conditional-captcha

And the output:

Success: Plugin 'wp-conditional-captcha' activated.

Doing it directly in the database

It’s also possible to deactivate plugins directly in the database if you don’t have command line access, which I’ll cover in a future post.

Promo Codes, Discount Vouchers, Etc.

This post has promo codes and discount voucher codes that I come across that I find useful and may need in the near future.

Name.com

Use the promo code MACANDCHEESE at checkout for $10.25 .COM and .NET registrations at name.com – the normal price is $10.99. No, it’s not much of a saving and is not as nice at last month’s one which was for $8.99!

The promo code does not apply to renewals or premium registrations.
Valid in November, 2015.

Screen: cannot open your terminal ‘/dev/pts/0’

If you are logged in as a user using “su” or “sudo su” and attempt to use screen, you’ll get the error message “Cannot open your terminal ‘/dev/pts/0’ – please check”. This post shows how to fix this.

Solution 1

Log out of the terminal/SSH session and log back in as the user you want to run screen as instead of using su.

Solution 2

script /dev/null
 screen

Reference

http://serverfault.com/questions/116775/sudo-as-different-user-and-running-screen/116830

This worked for me on Debian 7.8 Wheezy.

How to default a column as a UUID/GUID in MySQL

MySQL does not yet support setting a column’s default value using a function (at least not yet as of version 5.6) but you can use a trigger instead. This post shows how to set a column to a UUID/GUID by default in MySQL using a trigger.