Archive for the ‘PHP’ Category

Drupal Database Maintenance Script

Friday, August 3rd, 2007

I love Drupal, but its insistence on saving every session its ever created causes the sessions table to become massive. So I usually end up creating a shell script like the following:

drupal-db-maint.sh (I posted a copy to pastebin since WordPress makes the code impossible to read.)

#!/bin/sh
echo `date` - Starting Drupal database maintenance
MYSQL_USERNAME=’insert your MySQL username here
MYSQL_PASSWORD=’insert your MySQL password here
MYSQL_DATABASE=’insert your database name here

# Note: 2592000 = sessions over 30 days old.
# Adjust the timespan as desired.
CLEAN_SESSIONS_SQL=’DELETE FROM `sessions` WHERE `timestamp` < (UNIX_TIMESTAMP() - 2592000)’
OPTIMIZE_CACHES_SQL=’OPTIMIZE TABLE `cache` , `cache_filter` , `cache_menu` , `cache_page` , `cache_views` , `sessions`’

# Note: I have tons of modules installed.
# I tried cleaning out their table names before posting, but you’ll want to double check this SQL.
OPTIMIZE_REST_SQL=’OPTIMIZE TABLE `access` , `accesslog` , `aggregator_category` , `aggregator_category_feed` , `aggregator_category_item` , `aggregator_feed` , `aggregator_item` , `authmap` , `blocks` , `blocks_roles` , `boxes` , `client` , `client_system` , `comments` , `contact` , `files` , `file_revisions` , `filters` , `filter_formats` , `flood` , `history` , `invite` , `menu` , `node` , `node_access` , `node_comment_statistics` , `node_counter` , `node_revisions` , `node_type` , `permission` , `profile_fields` , `profile_values` , `role` , `search_dataset` , `search_index` , `search_total` , `sequences` , `system` , `term_data` , `term_hierarchy` , `term_node` , `term_relation` , `term_synonym` , `url_alias` , `users` , `users_roles` , `variable` , `vocabulary` , `vocabulary_node_types` , `watchdog`’

# Note: I run this script on a Linode which has limited IO.
# Adding a short wait between commands lessens the load on the server.
SLEEP_TIME=5

echo $CLEAN_SESSIONS_SQL | mysql -u $MYSQL_USERNAME -p$MYSQL_PASSWORD $MYSQL_DATABASE
sleep $SLEEP_TIME

echo $OPTIMIZE_CACHES_SQL | mysql -u $MYSQL_USERNAME -p$MYSQL_PASSWORD $MYSQL_DATABASE > /dev/null
sleep $SLEEP_TIME

echo $OPTIMIZE_REST_SQL | mysql -u $MYSQL_USERNAME -p$MYSQL_PASSWORD $MYSQL_DATABASE > /dev/null

echo `date` - Done with Drupal database maintenance

Then just run crontab -e to edit your crontab and add a line similar to:
46 4 * * * /path/to/drupal-db-maint.sh

That will optimize your database nightly at 4:46 AM server time (usually UTC).

If you don’t like my solution, there are plenty of more elegant session cleaning solutions out there. I’m just more comfortable writing shell scripts and crontabs than Drupal modules that use hook_cron.

Posting code in WordPress so that its readable is pretty much impossible. Any suggestions?

Tips on Surviving DreamHost

Sunday, June 10th, 2007

I use DreamHost and it is by far the least reliable hosting service I’ve ever used.

However, they’re quite cheap for the vast amounts of storage and bandwidth provided. I also love their blog, newsletter, status page, control panel, and especially that they’re green!

Bottom line: DreamHost is great for some use cases, and incredibly dangerous for others. Here are some tips:

  1. Do not use IMAP for DreamHost e-mail. IMAP is wonderful…at least over a 100% reliable high-bandwidth, low-latency connection. I would recommend only using DreamHost for POP access or e-mail forwarding.
    I made the mistake of using IMAP and now have over 5,000 wonderfully organized and filtered messages trapped on DreamHost’s unreliable servers. I’m not a big fan of Gmail’s non-hierarchical organization scheme, and both Yahoo! & Hotmail have miserable user interfaces. So now I’m stuck with embarrassingly unreliable e-mail acces.
  2. Do not store any sensitive information on DreamHost. They just had a security breach.
    If you do, make sure that the file permissions only allow your user access to sensitive files: chmod 0600 filename
  3. Do not host sites which require high levels of availability. You’ve been warned. ;)

Enough bad news, here’s what I do recommend using DreamHost for:

  1. Personal sites (like my blog) or sites which don’t require high availability (small businesses or organizations).
  2. Serving static media files for a site hosted elsewhere (although some sort of failover should be used).
  3. Remote backup — as long as you encrypt any sensitive information.
  4. Shell access to a Linux server. If you want to play with Linux + Apache + MySQL + PHP/Perl/Ruby on Rails/Python, DreamHost seems like an excellent choice. Just remember to move to a more reliable hosting service before launching your Web 2.0 startup.

Any Takers for Drupal in Python?

Thursday, May 10th, 2007

I half-jokingly posted a comment on Dries Buytaert blog about re-implementing Drupal in Python.  Honestly, I expected to either be flamed or ignored, but instead it turns out I’m not the first person to think of this!  Some people even suggested Haskell…  I think something a tad more mainstream like Python would fair much better.

Another commenter suggested Python doesn’t need Yet Another Web Framework which is absolutely true!  However, Python desperately needs an easy to use Content Management System, and Drupal is the best there is!

I posted a follow-up comment asking for anyone interested in pursuing such a wild course of action to contact me.

If you’ve read much of my blog, you know I’m relatively new to Python, but CherryPy and Genshi seem ideal for implementing Drupal.  CherryPy gives you the nice URLs and module layout while Genshi allows for a very similar theming experience.

Debian Etch Released

Sunday, April 8th, 2007

It appears Debian GNU/Linux 4.0, codenamed Etch, (quite a mouthful) has been released as the latest stable version of Debian.

Go download it using BitTorrent to be cool. ;)

Tip #1:  You do not have to download all of the CDs, just get one and Debian’s installer will download the rest.)

I’m a happy and loyal Debian user and strongly encourage any Linux power users to try out Etch. Ubuntu is still my recommendation for users who want things to “Just Work.” However, I highly recommend Debian for any kind of server (file, print, application, database, web, e-mail).

Tip #2: Check out Debian Multimedia for all of your proprietary codec needs!

Mini-Review

I’ve been using Etch (and Sid) for a while now, so its tough to do a real review from a “normal” end user’s perspective.

Some highlights:

  • Developer’s should be happy as PHP, Python, Mono, MySQL, and PostgreSQL are all fairly up-to-date and extremely easy to install.
  • Gnome is stable and speedy.
  • The graphical installer brings Debian out of the 1990s and offers tons of features.
  • As usual, Debian’s package repository is huge. Forget vendor-supplied RPMs or custom installers, if its not in Debian’s repository (or Debian Multimedia), you probably don’t need it. ;)

Some downsides:

  • Relatively ancient version of the standard C library (2.3.6) means poor compatibility with 3rd party binaries (which Debian has always hated anyway).
  • Gnome is 2 stable versions behind in Debian (2.14 vs. 2.18). This is very frustrating as Gnome constantly produces nice incremental improvements.
  • Debian just isn’t as easy to use by default as Ubuntu. Debian needs to install and integrate some of the more cutting edge desktop apps like Beagle, Tomboy, Deskbar, and Ubuntu’s nicer package management interfaces.
  • Secure APT is a great feature, but the error messages it can produce are going to scare the pants off of new users. More effort needs to be made to integrate all of the various places user’s encounter public key crypto. Right now its very schizophrenic and confusing: GPG errors are too common, and command line magic that even confuses me is necessary to add new repositories.
  • Not related to the release, Debian’s support system is a confusing jumble of web interfaces, mailing lists, IRC channels, and an excellent wiki (that I rarely remember to use). Hopefully this is something a new Debian DPL will address.

What did WordPress do with Firefox’s Spell Check?

Friday, February 23rd, 2007

I upgraded to WordPress 2.1.1 last night, and now WordPress wants me to use its spell checker instead of Firefox’s.

This is really annoying as I don’t really want or need a new spell checker. Firefox’s works great. WordPress’s is just slow and laggy because it uses AJAX.

Also, the whole auto-save feature of WordPress would be nice if Firefox didn’t already have the Session Restore feature.

Its as if WordPress’s 2.1 roadmap consisted of reimplementing Firefox 2.0 features in AJAX…

I like WordPress alot, but these new features are just a nuisance.

Anyone know how to turn these features off?

Update: My friend Dan pointed out that someone has submitted a bug to WordPress for the Firefox spellcheck issue.

Even Python Coders Love Drupal

Friday, February 23rd, 2007

Its no secret that I’ve been trying to kick my PHP habit in favor of Python.  However, I started using Drupal (a PHP content management system) around 4.4 or 4.5 and haven’t found any Python solutions that can even begin to compete with its ease of use and features.

With that in mind, I found it funny today when I ran across a site prominently displaying a “powered by python” logo, when its obviously powered by Drupal.

I’m guessing the “powered by python” actually refers to the spiders that the site uses to build its blacklist, but its still a bit misleading, if not outright disingenuous to credit Python without mentioning Drupal.

To be fair the site does look like a useful project.  I’m in favor of anything that reduces the monetary incentive to domain park!