<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BigSmoke &#187; PHP</title>
	<atom:link href="http://blog.bigsmoke.us/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bigsmoke.us</link>
	<description>Smokes your problems, coughs fresh air.</description>
	<lastBuildDate>Tue, 07 Sep 2010 08:43:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MediaWiki thumb.php and rewrite rules</title>
		<link>http://blog.bigsmoke.us/2010/03/07/mediawiki-thumb.php-rewrite-rules</link>
		<comments>http://blog.bigsmoke.us/2010/03/07/mediawiki-thumb.php-rewrite-rules#comments</comments>
		<pubDate>Sun, 07 Mar 2010 18:40:26 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[MediaWiki]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=516</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>May, last year, I created an empty draft for this post, because, around that time, I had gone through quite some effort before I got thumbnails for foreign file repos working just right. Now, I&#8217;m taking a dive into my MediaWiki working dirs in preparation of the creation of a separate development environment, so it&#8217;s a good moment to rehash the past experience (almost as good as when I&#8217;d have done it right away).</p>

<p>This is how I configured the <a href="http://www.mediawiki.org/wiki/Manual:$wgForeignFileRepos">foreign file repo</a> to be able to use images uploaded to the English wiki from the Dutch wiki:</p>

<pre class="php"><span style="color: #0000ff;">$wgHashedUploadDirectory</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
<span style="color: #0000ff;">$wgForeignFileRepos</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'ForeignDBRepo'</span>,
    <span style="color: #ff0000;">'name'</span> =&gt; <span style="color: #ff0000;">'en'</span>,
    <span style="color: #ff0000;">'url'</span> =&gt; <span style="color: #ff0000;">&quot;http://wiki.hardwood-investments.net/media&quot;</span>,
    <span style="color: #ff0000;">'hashLevels'</span> =&gt; <span style="color: #cc66cc;">0</span>,
    <span style="color: #808080; font-style: italic;">//'thumbScriptUrl' =&gt; &quot;http://wiki.hardwood-investments.net/thumb.php&quot;,</span>
    <span style="color: #ff0000;">'transformVia404'</span> =&gt; <span style="color: #000000; font-weight: bold;">true</span>,<span style="color: #808080; font-style: italic;">//!$wgGenerateThumbnailOnParse,</span>
    <span style="color: #ff0000;">'dbType'</span> =&gt; <span style="color: #0000ff;">$wgDBtype</span>,
    <span style="color: #ff0000;">'dbServer'</span> =&gt; <span style="color: #0000ff;">$wgDBserver</span>,
    <span style="color: #ff0000;">'dbUser'</span> =&gt; <span style="color: #0000ff;">$wgDBuser</span>,
    <span style="color: #ff0000;">'dbPassword'</span> =&gt; <span style="color: #0000ff;">$wgDBpassword</span>,
    <span style="color: #ff0000;">'dbName'</span> =&gt; <span style="color: #ff0000;">'hardwood'</span>,
    <span style="color: #ff0000;">'tablePrefix'</span> =&gt; <span style="color: #ff0000;">'mw_'</span>,
    <span style="color: #ff0000;">'hasSharedCache'</span> =&gt; <span style="color: #000000; font-weight: bold;">false</span>,
    <span style="color: #ff0000;">'descBaseUrl'</span> =&gt; <span style="color: #ff0000;">'http://wiki.hardwood-investments.net/Image:'</span>,
    <span style="color: #ff0000;">'fetchDescription'</span> =&gt; <span style="color: #000000; font-weight: bold;">false</span>
<span style="color: #66cc66;">&#41;</span>;</pre>

<p>To make thumbnails be generated by <a href="http://www.mediawiki.org/wiki/Manual:Thumb.php"><tt>thumb.php</tt></a> on request I added the following to my <tt>.htaccess</tt> at the other end (and visa versa, because the Dutch wiki actually contains most of the images):</p>

<pre class="php">RewriteCond %<span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> !-f
RewriteCond %<span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> !-d
RewriteRule ^media/thumb/<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>^/<span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>/<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #cc66cc;">-9</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>px-.*$ /thumb.php?f=$<span style="color: #cc66cc;">1</span>&amp;width=$<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#91;</span>L,QSA<span style="color: #66cc66;">&#93;</span></pre>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2010/03/07/mediawiki-thumb.php-rewrite-rules/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>www.stichting-ecosafe.org</title>
		<link>http://blog.bigsmoke.us/2009/09/16/www.stichting-ecosafe.org</link>
		<comments>http://blog.bigsmoke.us/2009/09/16/www.stichting-ecosafe.org#comments</comments>
		<pubDate>Wed, 16 Sep 2009 10:19:56 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[406]]></category>
		<category><![CDATA[AHAH]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[mod_include]]></category>
		<category><![CDATA[mod_negotiation]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SSI]]></category>
		<category><![CDATA[WWW]]></category>
		<category><![CDATA[www.stichting-ecosafe.org]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=683</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Stichting EcoSafe is a Dutch foundation for the safe-keeping of the funds that are necessary for the maintenance of hardwood plantations. In July of 2006, together with Johan Ockels, I created a website for the Foundation. Johan was responsible for the organization of the whole process. This went very smooth and the website ended up being an emblem of simplicity and clarity. That&#8217;s why I wanted to blog a bit about it now, even though there are a few things that I&#8217;d probably end up doing different if I were to start from scratch. [There's actually a disturbing number of things for which this is true, I'm coming to notice.]</p>

<table class="invisible" style="border: none;"><tr><td valign="top">
<div id="attachment_708" class="wp-caption alignnone" style="width: 310px"><a rel="lightbox-screen" href="http://blog.bigsmoke.us/uploads/2009/07/ecosafe-index-en.png"><img src="http://blog.bigsmoke.us/uploads/2009/07/ecosafe-index-en-300x206.png" alt="The Welcome page of the EcoSafe website" title="www.stichting-ecosafe.org/index.en" width="300" height="206" class="size-medium wp-image-708" /></a><p class="wp-caption-text">The Welcome page of the EcoSafe website</p></div>
</td><td valign="top">
<div id="attachment_794" class="wp-caption alignnone" style="width: 310px"><a rel="lightbox-screen" href="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-plantations-en.png"><img src="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-plantations-en-300x206.png" alt="EcoSafe page for plantations" title="www.stichting-ecosafe.org/plantations.en" width="300" height="206" class="size-medium wp-image-794" /></a><p class="wp-caption-text">EcoSafe page for plantations</p></div>
</td><td valign="top">
<div id="attachment_810" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-cost-structure-en.png"><img src="http://blog.bigsmoke.us/wp-factory/../uploads/2009/09/ecosafe-cost-structure-en-300x205.png" alt="EcoSafe cost structure" title="www.stichting-ecosafe.org/cost-structure.en" width="300" height="205" class="size-medium wp-image-810" /></a><p class="wp-caption-text">EcoSafe cost structure</p></div>
</td></tr></table>

<h2 id="file-structure">File structure</h2>

<p>Like with most websites, I started with creating an SVN repo so that I wouldn&#8217;t have to be afraid of ever losing anything.</p>

<p>The file structure was pretty standard:</p>

<ul>
<li>a <tt>css</tt> dir for stylesheets;</li>
<li><tt>img</tt> for images;</li>
<li><tt>inc</tt> for shared PHP and mod_include stuff and for AJAX partials;</li>
<li><tt>jot</tt> for to-do&#8217;s and other notes;</li>
<li>and <tt>js</tt> for JavaScript files and libraries.</li>
</ul>

<h3>Possible file structure improvements</h3>

<p>If I were to redesign this file structure, I&#8217;d collapse <tt>css</tt>, <tt>img</tt> and <tt>js</tt> into one directory called <tt>layout</tt>, because these are typically things that require the same <tt>robots.txt</tt> and caching policy. Also, it is meaningless to organize things by file extension. If you want to sort something by file extension, use <q><tt>ls -X</tt></q> (or <q><tt>ls --sort=extension</tt></q> if you&#8217;re on GNU).</p>


<h2 id="ssi">Server-side includes</h2>

<p>The site would be so simple that I felt that any type of CMS or content transformation would be completely unnecessary. Instead, I decided to rely on Apache&#8217;s <a href="http://httpd.apache.org/docs/1.3/mod/mod_include.html">mod_include</a> and just use a few partials for repeating page elements such as the left sidebar containing the logo and the menu.</p>

<p>Also, because I didn&#8217;t need to transform the HTML files, I decided I could use good ol&#8217; HTML 4 instead of XHTML 1 (which I&#8217;d have to send to the browser with the wrong mime-type anyway).</p>

<p>This is the HTML for <tt>contact.nl.shtml</tt>:</p>

<pre class="php">&lt;!DOCTYPE html PUBLIC <span style="color: #ff0000;">&quot;-//W3C//DTD HTML 4.01//EN&quot;</span> <span style="color: #ff0000;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span>&gt;
&nbsp;
&lt;html lang=<span style="color: #ff0000;">&quot;en&quot;</span>&gt;
  &lt;head&gt;
    &lt;title&gt;Contact EcoSafe&lt;/title&gt;
&nbsp;
    &lt;meta http-equiv=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> content=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> /&gt;
&nbsp;
    &lt;link rel=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> type=<span style="color: #ff0000;">&quot;text/css&quot;</span> href=<span style="color: #ff0000;">&quot;/css/style.css&quot;</span>&gt;&lt;/link&gt;
  &lt;/head&gt;
&nbsp;
  &lt;body&gt;
    &lt;!--<span style="color: #808080; font-style: italic;">#include virtual=&quot;/inc/left-side.en.html&quot;--&gt;</span>
&nbsp;
    &lt;!--<span style="color: #808080; font-style: italic;">#include virtual=&quot;/inc/alt-lang.phtml&quot;--&gt;</span>
&nbsp;
    &lt;div id=<span style="color: #ff0000;">&quot;content&quot;</span>&gt;
      &lt;h1&gt;Contact&lt;/h1&gt;
&nbsp;
      &lt;p&gt;Your email to EcoSafe kan be sent to the following address:
      &lt;a href=<span style="color: #ff0000;">&quot;mailto:service@stichting-ecosafe.org&quot;</span>&gt;service@stichting-ecosafe.org&lt;/a&gt;.
      Or, alternatively, you can fax us at <span style="color: #cc66cc;">+31</span> <span style="color: #cc66cc;">50</span> - <span style="color: #cc66cc;">309</span> <span style="color: #cc66cc;">66</span> <span style="color: #cc66cc;">58</span>.&lt;/p&gt;
&nbsp;
      &lt;h2&gt;About this website&lt;/h2&gt;
&nbsp;
      &lt;p&gt;For comments and/or suggestions concerning this website,
      you can direct an email message at:
      &lt;a href=<span style="color: #ff0000;">&quot;mailto:webmaster@stichting-ecosafe.org&quot;</span>&gt;webmaster@stichting-ecosafe.org&lt;/a&gt;.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>

<div id="attachment_788" class="wp-caption alignright" style="width: 197px"><img src="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-alt-language.png" alt="Alternative language selection" title="www.stichting-ecosafe.org alternative language selection" width="187" height="38" class="size-full wp-image-788" /><p class="wp-caption-text">Alternative language selection</p></div>

<p>I use <tt>&lt;!--#include virtual--&gt;</tt> to include the repeating parts. <tt>&lt;!--#include virtual--&gt;</tt> has several advantages over <tt>&lt;!--#include file--&gt;</tt> in that it allows for content-negotiation, execution of dynamic content etc., but here the only place were it holds an advantage is in the inclusion of <tt>/inc/alt-lang.phtml</tt>. <tt>alt-lang.phtml</tt> is a messy PHP script that figures out which language variants of a page are available and displays a selection of alternative language versions (variants with a language different from the current).</p>

<h3>SSI and caching</h3>

<p>Without the <A href="http://httpd.apache.org/docs/1.3/mod/mod_include.html#xbithack">XBitHack directive</a> set to <tt>full</tt>, all content handled by <tt>mod_include</tt> is sent without a <tt>Last-Modified</tt> header. However, I don&#8217;t want to use <tt>XBitHack</tt> at all, because I don&#8217;t want just any executable file to be handled by <tt>mod_include</tt>; that just too much of a … hack.</p>

<p>If I were to do something similar now, I&#8217;d use some kind of (sed) substitution to pre-process the includes locally so that more of what I end up uploading is simple static content. The dynamic part of the included PHP script, I would simply replace with JavaScript.</p>

<h2 id="visual">Visual design</h2>

<p>As you can see in the HTML example, there&#8217;s hardly anything layout oriented in the HTML source. This is good, and means that I have to touch only the CSS for most minor and major lay-out modifications. (It is a pipe-dream to think that you only need to change the CSS to make the same HTML page look however you want as long as that HTML is rich enough in meaning, but for a site with pages of such simple structure, it&#8217;s a dream that comes pretty close to reality.)</p>

<p>I&#8217;m not much of a designer, but I think design is overrated anyway. Actually, I think that most website suffer from too much design.</p>

<div id="attachment_692" class="wp-caption alignright" style="width: 275px"><img src="http://blog.bigsmoke.us/uploads/2009/07/ecosafe-logo.jpg" alt="The EcoSafe logo" title="EcoSafe logo" width="265" height="217" class="size-full wp-image-692" /><p class="wp-caption-text">The EcoSafe logo</p></div>

<p>To start the design, I got a logo made by <a href="http://wiki.hardwood-investments.net/Huite_Zijlstra">Huite Zijlstra</a>. Because the logo was pretty big and didn&#8217;t look good scaled down, I decided to put it at the left of the content area instead of at the top. This would still leave enough room for the menu (which actually takes less space horizontally than the logo).</p>

<h3 id="colors">Colors</h3>

<p>For the color scheme, I just picked a few colors from the logo. As always, the base of the scheme would be black text on a white background for maximum readability. The print version hardly uses any colors.</p>

<pre class="css"><span style="color: #a1a100;">@media screen {</span>
body            <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #993333;">white</span>;  <span style="color: #66cc66;">&#125;</span>
*               <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #000000; font-weight: bold;">black</span>;             <span style="color: #66cc66;">&#125;</span>
a<span style="color: #3333ff;">:link </span>         <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">585</span>;              <span style="color: #66cc66;">&#125;</span>
h1              <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">880</span>;              <span style="color: #66cc66;">&#125;</span>
h2              <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">888</span>;              <span style="color: #66cc66;">&#125;</span>
strong          <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span>: #a62;              <span style="color: #66cc66;">&#125;</span>
#menu li a      <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">660</span>;              <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>

<h3 id="underlines">Underlines</h3>

<p>I wanted an underline below the level 1 and 2 headings. Because I didn&#8217;t like the effect of <tt>text-decoration:underline</tt> (too thick for <tt>&lt;h2&gt;</tt>s, too dark for <tt>&lt;h1&gt;</tt>s and different from browser to browser) and because <tt>border-bottom</tt> was set too far from the text, I made two simple PNG images that I could <tt>repeat-x</tt> along the bottom edge.</p>

<pre class="css"><span style="color: #a1a100;">@media screen {</span>
h1 <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/img/h1-border-bottom.png'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">bottom</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">repeat-x</span>; <span style="color: #66cc66;">&#125;</span>
h2 <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/img/hx-border-bottom.png'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">bottom</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">repeat-x</span>; <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>

<h3 id="menu">Menu</h3>

<p>The menu is very simple. The markup is part of <tt>inc/left-side.en.html</tt> for the English version and <tt>inc/left-side.nl.html</tt> for the Dutch version:</p>

<pre class="bash">cat inc/left-side.en.html</pre>
<pre class="php">&lt;div id=<span style="color: #ff0000;">&quot;left&quot;</span> lang=<span style="color: #ff0000;">&quot;en&quot;</span>&gt;
  &lt;a <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;logo&quot;</span> href=<span style="color: #ff0000;">&quot;/index.en&quot;</span>&gt;&lt;img <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;logo&quot;</span> alt=<span style="color: #ff0000;">&quot;[Logo]&quot;</span> src=<span style="color: #ff0000;">&quot;/img/logo.jpg&quot;</span>&gt;&lt;/img&gt;&lt;/a&gt;
&nbsp;
  &lt;ul id=<span style="color: #ff0000;">&quot;menu&quot;</span> <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span>&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/index.en&quot;</span> rel=<span style="color: #ff0000;">&quot;start&quot;</span>&gt;Start page&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/plantations.en&quot;</span>&gt;For plantations&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/investors.en&quot;</span>&gt;For investors&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/history.en&quot;</span>&gt;History&lt;/a&gt;&lt;/li&gt;
    &lt;!--&lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/goals&quot;</span>&gt;Goals&lt;/a&gt;&lt;/li&gt;--&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/methods.en&quot;</span>&gt;How it works&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/cost-structure.en&quot;</span>&gt;Cost structure&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/cost-calculator.en&quot;</span>&gt;Cost calculator&lt;/a&gt;&lt;/li&gt;
    &lt;!--&lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/clients.en&quot;</span>&gt;Clients&lt;/a&gt;&lt;/li&gt;--&gt;
    &lt;li&gt;&lt;a href=<span style="color: #ff0000;">&quot;/contact.en&quot;</span>&gt;Contact&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&nbsp;
&lt;script type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> src=<span style="color: #ff0000;">&quot;/js/menu.js&quot;</span>&gt;&lt;/script&gt;</pre>

<div id="attachment_710" class="wp-caption alignright" style="width: 270px"><img src="http://blog.bigsmoke.us/uploads/2009/07/ecosafe-menu-en.png" alt="The EcoSafe menu (in English)" title="EcoSafe menu EN" width="260" height="438" class="size-full wp-image-710" /><p class="wp-caption-text">The EcoSafe menu (in English)</p></div>

<p>As is customary, I started by removing all the default list styles and made the anchors behave as block-level elements. I used the big O from the logo for bullets in the list (using <tt>background-image</tt> instead of <tt>list-style-image</tt> because the latter gives unpredictable cross-browser results and doesn&#8217;t make the bullet clickable).</p>

<pre class="css"><span style="color: #cc00cc;">#menu <span style="color: #66cc66;">&#123;</span></span>
  <span style="color: #000000; font-weight: bold;">margin-top</span>: 2em;
  <span style="color: #000000; font-weight: bold;">margin-left</span>: 2em;
  <span style="color: #000000; font-weight: bold;">list-style-type</span>: <span style="color: #993333;">none</span>;
  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
#menu li <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
#menu li a <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span>: <span style="color: #993333;">block</span>;
  <span style="color: #000000; font-weight: bold;">background-image</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/img/o-21x16.png'</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">background-position</span>: <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span>;
  <span style="color: #000000; font-weight: bold;">background-repeat</span>: <span style="color: #993333;">no-repeat</span>;
  <span style="color: #000000; font-weight: bold;">padding-left</span>: 30px;
  <span style="color: #000000; font-weight: bold;">text-decoration</span>: <span style="color: #993333;">none</span>;
  <span style="color: #000000; font-weight: bold;">font-family</span>: <span style="color: #993333;">sans-serif</span>;
  <span style="color: #000000; font-weight: bold;">font-weight</span>: <span style="color: #993333;">bold</span>;
  <span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">660</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
#menu li a:hover,
#menu li<span style="color: #6666ff;">.active </span>a <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-image</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/img/oSafe-21x16.png'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
#menu a<span style="color: #3333ff;">:hover </span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span>: #<span style="color: #cc66cc;">787800</span>;
<span style="color: #66cc66;">&#125;</span></pre>

<h4>JavaScript menu item activation</h4>

<p>To add the <tt>active</tt> class to the currently active list item (<tt>&lt;li&gt;</tt>), I used a client-side solution using JavaScript. After all, it&#8217;s proper use of JavaScript to enhance your user interface with it (as long as, as many would say, it isn&#8217;t <em>required</em> for the UI to function (as it is in the Cost Calculator)).</p>

<pre class="javascript"><span style="color: #009900; font-style: italic;">// menu.js</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> menu = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> anchors = menu.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> locationHref = window.<span style="color: #006600;">location</span>.<span style="color: #006600;">pathname</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>i = anchors.<span style="color: #006600;">length</span> - <span style="color: #CC0000;">1</span>; i &gt;= <span style="color: #CC0000;">0</span>; i--<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  a = anchors<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
  aHref = a.<span style="color: #006600;">href</span>;
    
  <span style="color: #009900; font-style: italic;">// Does this menu item link to the current page?</span>
  <span style="color: #009900; font-style: italic;">// We find out by looking if the window location contains the URL in the anchor</span>
  <span style="color: #009900; font-style: italic;">// or the other way arround. The reason to look at both is content-negotiation.</span>
  <span style="color: #009900; font-style: italic;">// It's also true if the location is just '/' and we're looking at the anchor of</span>
  <span style="color: #009900; font-style: italic;">// the 'start' page.</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>locationHref === <span style="color: #3366CC;">'/'</span> &amp;&amp; a.<span style="color: #006600;">rel</span> === <span style="color: #3366CC;">'start'</span><span style="color: #66cc66;">&#41;</span> ||
       <span style="color: #66cc66;">&#40;</span>locationHref !== <span style="color: #3366CC;">'/'</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span> locationHref.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span>aHref<span style="color: #66cc66;">&#41;</span> !== <span style="color: #CC0000;">-1</span> ||
                                  aHref.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span>locationHref<span style="color: #66cc66;">&#41;</span> !== <span style="color: #CC0000;">-1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    a.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">'active'</span>;
    <span style="color: #000066; font-weight: bold;">break</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>

<p>I actually just fixed a long-standing bug that was caused by me not being able to fully rely on HTTP language negotiation for the selection of the appropriate language variant, which made me change all links from being language-neutral to including the language in the link target (e.g.: <tt>http:///history</tt> became <tt>http:///history.en</tt> and <tt>http:///history.nl</tt>), the problem with this being that, instead of being able to link to link to <tt>http:///</tt> (<tt>http://www.stichting-ecosafe.org/</tt>), I had to link to <tt>http:///index.en</tt> or <tt>http:///index.nl</tt>, making it more difficult to detect the active anchor if we&#8217;re requesting the home page through <tt>http:///</tt> instead of on of its language-specific URLs.</p>

<p>The JavaScript rested on the assumption that by reverse iterating through all the anchors in the menu and thus processing the link to <tt>http:///</tt> as last, I&#8217;d know that I had struck the home page and wouldn&#8217;t need to worry that <em>any of the links</em> contain a slash. (I don&#8217;t know if I intended it to work this way, but it sure seems to me now that the only way this could ever have worked was as an apparent side-effect of the looping order; the SVN logs seem to agree.)</p>

<p>I could have solved this by redirecting all requests for <tt>http:///</tt> to the appropriate variant. Maybe I should have (to avoid duplicate content). Instead I chose to add a <tt>rel="start"</tt> attribute to the links to the home page, as can be deduced from the JavaScript above. (To resolve the duplicate content issue, I could also add a <a href="http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html">canonical link</a> to the header of the two language variants.)</p>

<p>Anyway, all this brings me to the messy subject of content negotiation.</p>

<h2 id="content-negotiation">Content and language negotiation</h2>

<p>The EcoSafe website would be bi-lingual (English and Dutch) from the onset. Initially, I wanted to use language negotiation to the extend of having completely language-neutral URLs. For example: <q><tt>http:///cost-calculator</tt></q> instead of <q><tt>http:///cost-calculator.en</tt></q> and <q><tt>http:///cost-calculator.nl</tt></q>. In the end, you can make this work properly in the browser with the help of a cookie, but it&#8217;s still a pipe-dream because nothing else will work if you do not also offer another navigational path to the different variants. Maybe, we&#8217;ll revisit this topic for a later experiment.</p>

<p>Content-type negotiation is almost effortless with Apache thanks to <tt><a href="http://httpd.apache.org/docs/1.3/mod/mod_negotiation.html">mod_negotiation</a></tt>. If, like me, you despise to have <tt>.html</tt>, <tt>.htm</tt>, <tt>.xhtml</tt>, <tt>.phtml</tt>, <tt>.pxhtml</tt>. <tt>.sxhtml</tt>, <tt>.php</tt>, <tt>.xml</tt> in your URL (I actually used all of these at some time or other), you only have to make sure that <tt>MultiViews</tt> is in your options.</p>

<p>I&#8217;ve configured SSI by means of the following instead of a “magic mime-type”:</p>

<pre class="apache"><span style="color: #00007f;">AddType</span>         text/html       .shtml
<span style="color: #00007f;">AddHandler</span>      server-parsed   .shtml
<span style="color: #00007f;">AddCharset</span>      UTF<span style="color: #ff0000;">-8</span>           .shtml
AddOutputFilter <span style="color: #0000ff;">Includes</span>        .shtml</pre>

<p>For PHP I couldn&#8217;t do the same because my web host was still at Apache 1.3. Otherwise, the following should have worked equally well for PHP:</p>

<pre class="apache"><span style="color: #adadad; font-style: italic;"># This doesn't work with Apache 1.3</span>
<span style="color: #00007f;">AddType</span>        text/html       .phtml
<span style="color: #00007f;">AddHandler</span>     php-<span style="color: #00007f;">script</span>      .phtml
<span style="color: #00007f;">AddCharset</span>     UTF<span style="color: #ff0000;">-8</span>           .phtml</pre>

<p>Configuring language priority is easy with Apache:</p>

<h3>Integrating PHP and SSI</h3>

<p>The integration of PHP with all the weirdness that I had configured and created around SSI took some figuring out. Luckily, PHP offers a <a href="http://www.php.net/virtual"><tt>virtual()</tt></a> function that works roughly the same as <tt>mod_include's</tt> <tt>&lt;!--#include virtual--&gt;</tt>. Here&#8217;s an example:</p>

<pre class="php">&lt;body&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <a href="http://www.php.net/virtual"><span style="color: #000066;">virtual</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/inc/left-side.en.html'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #0000ff;">$uri</span> = <span style="color: #ff0000;">'/cost-calculator.en.phtml'</span>; <span style="color: #b1b100;">include</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'inc/alt-lang.phtml'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span></pre>

<p>In retrospect, it&#8217;s pretty much bullshit to use it. I could have just as well <tt>require()</tt>d the partials (which I actually did for the alternate language selection), but I probably started out using virtual on a more generic URL without language and content-type selection in it.</p>

<h3>406 handling</h3>

<p>Because I deployed on Apache 1.3 and the <tt><a href="http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html#forcelanguagepriority">ForceLanguagePriority</a></tt> directive was only introduced with Apache 2.0.30, I had to write an ugly hack to avoid visitors getting 406 errors. To that end, I added a 406 handler to my <tt>.htaccess</tt> file:</p>

<pre class="apache"><span style="color: #00007f;">LanguagePriority</span> en nl
ForceLanguagePriority Prefer Fallback <span style="color: #adadad; font-style: italic;"># This doesn't work with 1.3</span>
&nbsp;
<span style="color: #00007f;">ErrorDocument</span> <span style="color: #ff0000;">406</span> /error<span style="color: #ff0000;">-406</span>.php <span style="color: #adadad; font-style: italic;"># Luckily, this does </span></pre>

<p><tt>error-406.php</tt> is a PHP file that figures out the available variants based on <tt>$_SERVER['REQUEST_URI']</tt>. Then, it simply picks the first one (which works because, accidentally, that&#8217;s the one I&#8217;ve given priority using the <tt><a href="http://httpd.apache.org/docs/1.3/mod/mod_negotiation.html#languagepriority">LanguagePriority</a></tt> directive as well), outputs a 200 OK header instead of the 406, and <tt>virtual()</tt>s the file of the variant. The code looks somewhat like this:</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<a href="http://www.php.net/chdir"><span style="color: #000066;">chdir</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'DOCUMENT_ROOT'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$filenames</span> = <a href="http://www.php.net/glob"><span style="color: #000066;">glob</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/basename"><span style="color: #000066;">basename</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'REQUEST_URI'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;.*&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$filename</span> = <span style="color: #0000ff;">$filenames</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<a href="http://www.php.net/apache_setenv"><span style="color: #000066;">apache_setenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DOCUMENT_URI'</span>, <span style="color: #ff0000;">&quot;/$filename&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'HTTP/1.1 200 OK'</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/virtual"><span style="color: #000066;">virtual</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$filename&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre>

<div id="attachment_807" class="wp-caption alignright" style="width: 310px"><a rel="lightbox-ecosafe" href="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-cost-calculator.png"><img src="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-cost-calculator-300x137.png" alt="EcoSafe Cost Calculator" title="www.stichting-ecosafe.org/cost-calculator" width="300" height="137" class="size-medium wp-image-807" /></a><p class="wp-caption-text">EcoSafe Cost Calculator</p></div>

<div id="attachment_808" class="wp-caption alignright" style="width: 194px"><a href="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-cost-calculation.png"><img src="http://blog.bigsmoke.us/uploads/2009/09/ecosafe-cost-calculation-184x300.png" alt="EcoSafe Cost Calculator results" title="EcoSafe cost calculation" width="184" height="300" class="size-medium wp-image-808" /></a><p class="wp-caption-text">EcoSafe Cost Calculator results</p></div>

<h2>The Cost Calculator</h2>

<p>The EcoSafe Cost Calculator is some of the least neatly contained and most procedurally oriented PHP code I&#8217;ve ever produced while knowing full well what I was doing. It does almost everything it does in global scope. Yet, it does it well.</p>

<p>The thing is designed as a dynamic web page rather than a web application. What I mean by this is that it&#8217;s simply two pages (one for English and one for Dutch) using PHP among a number of pages using SSI. In an application, it&#8217;s usual to have just one ‘view’ that is the same for all languages, but here I chose to put the different language versions in different language pages and then include everything reusable (and language-neutral) from within these files.</p>

<p>Most of the actual processing and calculating is done in <tt>inc/costs-functions.php</tt>.  (The part about gotos is a joke. (Labeled blocks would have been quite sufficient. <img src='http://blog.bigsmoke.us/wp-factory/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ))</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #808080; font-style: italic;"># costs-functions.php - Stuff that's includes by cost-calculator.{nl,en}.phtml</span>
<span style="color: #808080; font-style: italic;">/**
 * Just remember that this code was never meant to be general purpose or anything.
 * So, relaxeeee and keep your OO-axe burried where it belongs.
 * Oh, if only PHP would support GOTO's ... Sigh ...
 */</span></pre>

<p>The rest of the file is just a whole lot of processing of form data and turning it into something that can be easily traversed for display to the user. There are even the function calls without arguments doing all their work on globals. These are actually only added to make it clearer <em>em</em> a piece of code is doing. And—I must say—after a few years it&#8217;s still remarkably clear to me what each part of the code is doing. There&#8217;s no deep, confusing nesting structures or anything. There&#8217;s just a whole lot of very simple code.</p>

<h3>Some simple AHAH increases form interactivity</h3>

<p>Users of the calculator can add any number of plantings and locations. When the user decides to add a planting or a location, the <tt>onClick</tt> event triggers the execution of <tt>addExtraPlanting()</tt> or <tt>addExtraLocation()</tt>. Here&#8217;s how <tt>addExtraPlanting()</tt> looks:</p>

<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> addExtraPlanting<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  lang = document.<span style="color: #006600;">documentElement</span>.<span style="color: #006600;">lang</span>;
&nbsp;
  <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Updater</span><span style="color: #66cc66;">&#40;</span>
    <span style="color: #3366CC;">'plantings'</span>, <span style="color: #3366CC;">'/inc/planting.'</span> + lang, <span style="color: #66cc66;">&#123;</span>
      method: <span style="color: #3366CC;">'get'</span>,
      insertion: Insertion.<span style="color: #006600;">Bottom</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>

<p><a href="http://api.prototypejs.org/ajax/ajax/updater.html"><tt>Ajax.Updater</tt></a> comes from the <a href="http://www.prototypejs.org/">Prototype JavaScript framework</a>.</p>

<p>Here&#8217;s what <tt>inc/planting.en.phtml</tt> looks like. The same file is also included in a loop to rebuild the form&#8217;s state after submitting.</p>

<pre class="php">&lt;li&gt;
  &lt;input name=<span style="color: #ff0000;">&quot;num_hectares[]&quot;</span> type=<span style="color: #ff0000;">&quot;text&quot;</span> size=<span style="color: #ff0000;">&quot;5&quot;</span> value=<span style="color: #ff0000;">&quot;&lt;?php echo $num_hectares ?&gt;&quot;</span> /&gt;
&nbsp;
  hectares have been planted in
&nbsp;
  &lt;select name=<span style="color: #ff0000;">&quot;plant_years[]&quot;</span>&gt;&lt;?php <span style="color: #b1b100;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'planting_options.php'</span><span style="color: #66cc66;">&#41;</span> ?&gt;&lt;/select&gt;
&nbsp;
  <span style="color: #66cc66;">&#40;</span>&lt;a title=<span style="color: #ff0000;">&quot;Remove this planting&quot;</span> href=<span style="color: #ff0000;">&quot;#&quot;</span> onclick=<span style="color: #ff0000;">&quot;removePlanting(this); return false;&quot;</span>&gt;x&lt;/a&gt;<span style="color: #66cc66;">&#41;</span>
&lt;/li&gt;</pre>

<p>I think that I&#8217;ve gone into small enough detail by now to get to the conclusion. Also showing the contents of <tt>planting_options.php</tt> would be pushing it. Ah, well…</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this_year</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$this_year</span> = <a href="http://www.php.net/intval"><span style="color: #000066;">intval</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/date"><span style="color: #000066;">date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Y'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$plant_year</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$plant_year</span> = <span style="color: #0000ff;">$this_year</span>;
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #0000ff;">$this_year</span>; <span style="color: #0000ff;">$i</span> &gt;= <span style="color: #0000ff;">$this_year</span> - <span style="color: #cc66cc;">20</span>; <span style="color: #0000ff;">$i</span>--<span style="color: #66cc66;">&#41;</span>
  <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;option&quot;</span> . <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> == <span style="color: #0000ff;">$plant_year</span> ? <span style="color: #ff0000;">&quot; selected='1'&quot;</span> : <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;&gt;$i&lt;/option&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre>

<p>(Yesterday, I couldn&#8217;t resist the temptation of turning this into a simple file to <tt>require()</tt> instead of the function definition it was. I think it&#8217;s funny to refactor something to <em>remove</em> encapsulation.)</p>

<h2>Conclusion</h2>

<p>As is usual when looking at old code, I see many things that I&#8217;d do (even just a little) different today, but I saw a surprising number of solutions that I actually still like now that I see them back after three years. Removing some of the remaining warts probably won&#8217;t do much good besides the masturbatory satisfaction it could give me. (It&#8217;s likely that the website won&#8217;t live much longer, making such extra attention very undeserved.) But, nevertheless, I&#8217;ve enjoyed blogging about it now to recoup the whole experience and to at least <em>look</em> at what I&#8217;d do different now and what I learned in the meantime.</p>

<h2>Some links</h2>

<ul>
  <li><a href="http://www.w3.org/TR/html401/types.html#type-links">The HTML spec. on <cite>Link types</cite></a></li>
  <li><a href="http://www.stichting-ecosafe.org/">The EcoSafe website</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/09/16/www.stichting-ecosafe.org/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP fgetcsv() behavior on empty lines</title>
		<link>http://blog.bigsmoke.us/2009/09/05/php-fgetcsv</link>
		<comments>http://blog.bigsmoke.us/2009/09/05/php-fgetcsv#comments</comments>
		<pubDate>Sat, 05 Sep 2009 08:55:13 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[fgetcsv]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=759</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>The PHP documentation for <a href="http://ww.php.net/fgetcsv"><tt>fgetcsv()</tt></a> states that <q cite="http://ww.php.net/fgetcsv">A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error. </q> Here&#8217;s a quick demonstration of this behavior.</p>

<p><tt>fgetcsv.php</tt>:</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fields</span> = <a href="http://www.php.net/fgetcsv"><span style="color: #000066;">fgetcsv</span></a><span style="color: #66cc66;">&#40;</span>STDIN, <span style="color: #cc66cc;">0</span>, <span style="color: #ff0000;">';'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/exit"><span style="color: #000066;">exit</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</pre>

<p>Execute the script and feed it some CSV with empty lines:</p>

<pre class="bash">php -q fgetcsv.php
<span style="color: #ff0000;">&quot;Veld 1&quot;</span>;<span style="color: #ff0000;">&quot;Veld 2&quot;</span>;<span style="color: #ff0000;">&quot;Veld 3&quot;</span>;;<span style="color: #ff0000;">&quot;Veld 5&quot;</span>
&nbsp;
<span style="color: #ff0000;">&quot;Field 1&quot;</span>;;<span style="color: #ff0000;">&quot;Field 3&quot;</span>;<span style="color: #ff0000;">&quot;Field 4&quot;</span>;
;;;;
;<span style="color: #ff0000;">&quot;Campo 2&quot;</span>;;;<span style="color: #ff0000;">&quot;Campo 5&quot;</span></pre>

<p>After pressing <kbd>Ctrl+D</kbd>, I&#8217;m presented with the following output:</p>

<pre>
Array
(
    [0] => Veld 1
    [1] => Veld 2
    [2] => Veld 3
    [3] => 
    [4] => Veld 5
)
Array
(
    [0] => 
)
Array
(
    [0] => Field 1
    [1] => 
    [2] => Field 3
    [3] => Field 4
    [4] => 
)
Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
)
Array
(
    [0] => 
    [1] => Campo 2
    [2] => 
    [3] => 
    [4] => Campo 5
)
Array
(
    [0] => 
)
</pre>

<p>This behaviour on empty lines is a little bit annoying if you want to test if the line is <tt>empty()</tt>:</p>

<pre class="php"><span style="color: #0000ff;">$a</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
  <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'$a is empty'</span>;
<span style="color: #b1b100;">else</span>
  <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'$a is not empty'</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre>

<p>This code will print:</p>

<pre>
Array
(
    [0] => 
)
$a is not empty
</pre>

<p>Hence, the following function:</p>

<pre class="php"><span style="color: #808080; font-style: italic;">/**
 * This function tests if the given array (as returned by fgetcsv())
 * is the result of an empty line in the CSV file.
 *
 * It does not work for lines that contain only delimiters.
 * From the POV of this function, these are simply records with
 * many empty fields.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> fgetcsv_empty_line<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row_array</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row_array</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> and <a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row_array</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>

<p>Now, if I change the call to <tt>empty()</tt> in my test to a call to <tt>fgetcsv_empty_line()</tt>:</p>

<pre>
$a is empty
</pre>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/09/05/php-fgetcsv/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enforcing Drupal URL aliases</title>
		<link>http://blog.bigsmoke.us/2009/06/10/enforcing-drupal-url-aliases</link>
		<comments>http://blog.bigsmoke.us/2009/06/10/enforcing-drupal-url-aliases#comments</comments>
		<pubDate>Wed, 10 Jun 2009 12:09:07 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=574</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>I hate modules, especially <em>core modules</em>. I prefer code to be tightly integrated. I want it to work together. Is that too much to ask? In Drupal, most functionality has been stuffed in modules. There&#8217;s a <em>Locale</em> module, a <em>Content Translation</em> module and a <em>Path</em> module. What&#8217;s missing is a <em>Working Together</em> module.</p>

<p>For me, clean, meaningful URLs are a number one, two and three requirement for any website that I do. Drupal considers <tt>/node/54673</tt> to be a cool URL. I don&#8217;t. So, as a kind of afterthought, Drupal comes with the Path module. This module allows you to set URL aliases per node.</p>

<p>The problem is that there&#8217;s no concept of a canonical URL. The URL alias works, but so does the <tt>node/3242</tt> URL. Neither redirects to the other. In many cases this is not much of a problem (because regular visitors will not notice this) but for our current project it is.</p>

<p>We have a lot of blocks with URL dependent visibility settings. For example, for a section about investing we have a menu that is displayed on all URLs starting with <q><tt>/investing</tt></q>, such as <q><tt>/investing/projects</tt></q> and so on.</p>

<p>After editing a page, Drupal helpfully redirects the user to <tt>node/[nodenumber]</tt>. For us, this means that the menu is no longer displayed and even the theme will be wrong. (We use the <a href="http://drupal.org/project/sections">Sections</a> module to select a subtheme based on which section you&#8217;re in.)</p>

<h2>Global Redirect doesn&#8217;t work</h2>

<p>The <a href="http://drupal.org/project/globalredirect">Global Redirect</a> module promises to solve this by allowing you to redirect <tt>node/[nodenumber]</tt> URLs to their alias if available. It kinda does, in some circumstances.</p>

<p>Our Drupal website sports two languages: English (EN) and Dutch (NL). English is the default language (not the fallback language; we don&#8217;t use a fallback) and doesn&#8217;t use a prefix. Dutch uses the <tt>nl</tt> prefix. Two example URLs:</p>

<table>
<tr>
<th>URL alias</th>
<th>Generic URL</th>
</tr>

<tr>
<td><tt>http://www.example.com/investing/projects</tt></td>
<td><tt>http://www.example.com/node/288</tt></td>
</tr>

<tr>
<td><tt>http://www.example.com/nl/beleggen/projecten</tt></td>
<td><tt>http://www.example.com/nl/node/110</tt></td>
</tr>
</table>

<p>When  <tt>/node/288</tt> is requested, the client is correctly redirected to <tt>/investing/projects</tt>, but when <tt>/node/110</tt> is requested, no redirect takes place. It will take place when prefixing <tt>/nl</tt>, but this is completely useless since Drupal&#8217;s built-in actions such as edit don&#8217;t redirect using this prefix, and these actions were what we needed this module for in the first place.</p>

<h2>A very simple hack that does work</h2>

<p>We ended up tearing our hair out trying to fix Global Redirect until we decided that we could just delete the module and replace it with a RewriteRule and a simple PHP script.</p>

<h3>Modify: <tt>.htaccess</tt></h3>

<pre class="apache"><span style="color: #adadad; font-style: italic;"># Put this after RewriteBase and before Drupal's default rewrite rules</span>
<span style="color: #00007f;">RewriteRule</span> ^<span style="color: #66cc66;">&#40;</span>../<span style="color: #66cc66;">&#41;</span>?node/<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>$ fixurl.php?nid=$<span style="color: #ff0000;">2</span> <span style="color: #66cc66;">&#91;</span>L<span style="color: #66cc66;">&#93;</span></pre>

<h3>Add: <tt>fixurl.php</tt></h3>

<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'./includes/bootstrap.inc'</span>;
drupal_bootstrap<span style="color: #66cc66;">&#40;</span>DRUPAL_BOOTSTRAP_DATABASE<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$result</span> = db_query<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM {url_alias} WHERE src = 'node/%d' LIMIT 1&quot;</span>, <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'nid'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> db_error<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;O agony!&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$url_alias_object</span> = db_fetch_object<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$destination</span> = <span style="color: #0000ff;">$url_alias_object</span>-&gt;<span style="color: #006600;">dst</span>;
&nbsp;
<span style="color: #0000ff;">$result</span> = db_query<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT prefix FROM {languages} WHERE language = '%s'&quot;</span>, <span style="color: #0000ff;">$url_alias_object</span>-&gt;<span style="color: #006600;">language</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> db_error<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;O agony!&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$prefix</span> = db_result<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$prefix</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
  <span style="color: #0000ff;">$prefix</span> .= <span style="color: #ff0000;">'/'</span>;
&nbsp;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Location: /$prefix$destination&quot;</span>,<span style="color: #000000; font-weight: bold;">TRUE</span>,<span style="color: #cc66cc;">301</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>

<h3>Shortcomings in our hack</h3>

<p>The code assumes that every content page has an URL alias. For us, this is okay, because we need these pretty URLs to even have menus show up or to have the right page be displayed with the right theme.</p>

<p>Also, this code is specifically tailored to language code in the URL prefix. For subdomain based language selection, for example, you&#8217;d need to modify it.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/06/10/enforcing-drupal-url-aliases/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>WordPress feed pagination</title>
		<link>http://blog.bigsmoke.us/2009/02/16/wordpress-feed-pagination</link>
		<comments>http://blog.bigsmoke.us/2009/02/16/wordpress-feed-pagination#comments</comments>
		<pubDate>Mon, 16 Feb 2009 21:52:37 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Atom]]></category>
		<category><![CDATA[feed]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[URL]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=399</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Wiebe uses his <a href="/author/halfgaar/feed/atom">author Atom feed</a> to generate a list of his blog posts here <a href="http://www.halfgaar.net/articles">on his own website</a>. WordPress feeds only display the latest 10 entries. He has written 16 so far. What he needs is a feed with <strong>all</strong> his entries.</p>

<p>First, I tried if pagination works for feeds. Appending &#8220;<tt>/page/2</tt>&#8221; (as is used an non-feed lists) to a feed URL gives a 404 so I was kind of stuck there. Four days ago, after Googling for some time, unable to find a solution anywhere, I <a href="http://wordpress.org/support/topic/242847">asked</a> on the forum. Still no answer today so I tried to find out which parameters WordPress accepts in the <tt>QUERY_STRING</tt>. The WordPress Codex does explain <a href="http://codex.wordpress.org/Query_Overview">how queries are handled</a> but not which parameters are accepted.</p>

<p>Digging into <tt>wp-includes/query.php</tt>, with much trial and error, I found out that I can append <q><tt>?paged=2</tt></q> to the URL to get the next page. At least I got that sorted then. There are a number of much more promising parameters supported by <tt>get_posts()</tt>, but these don&#8217;t seem to be parsed by <tt>parse_query()</tt>. Next time, I&#8217;d like to find out how how to use two of these: <tt>nopaging</tt> and <tt>posts_per_page</tt>.</p>

<p>Wiebe could complete his list by merging together all the pages of the feed, but I&#8217;d much prefer to find a relatively painless method to produce a feed with an unlimited number of posts.</p>

<h2>Notes</h2>

<ul>
<li>http://codex.wordpress.org/Query_Overview</li>
<li>http://codex.wordpress.org/Function_Reference/WP_Query</li>
<li>http://codex.wordpress.org/Template_Tags/query_posts</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/02/16/wordpress-feed-pagination/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New theme</title>
		<link>http://blog.bigsmoke.us/2008/07/13/new-theme</link>
		<comments>http://blog.bigsmoke.us/2008/07/13/new-theme#comments</comments>
		<pubDate>Sun, 13 Jul 2008 15:26:45 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[BigSmoke.US]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Gravatar]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=87</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>
After upgrading to WordPress 2.5.x, I had to fall back on a stock theme because my old customization of the <a href="http://www.plaintxt.org/themes/sandbox/">Sandbox</a> theme no longer worked with the upgrade. But, then, it was time to redo my theme anyway. So here you&#8217;re looking at the first version of my new theme. I might have let it stabilize some more before putting it on-line, but who cares? My reader maybe? Let&#8217;s just hope he or she doesn&#8217;t use IE. <img src='http://blog.bigsmoke.us/wp-factory/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<a title="The header with a single post below" href='http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap1.jpg' rel='lightbox[theme]'><img src="http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap1-300x193.jpg" alt="Screencap of my new WP theme" title="New WordPress theme for BigSmoke" width="300" height="193" class="alignright size-medium wp-image-89" /></a>

<a title="The oversized footer with all the site-wide navigation" href='http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap4.jpg' rel='lightbox[theme]'><img src="http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap4-300x201.jpg" alt="Screencap of my new WP theme" title="New WordPress theme for BigSmoke" width="300" height="201" class="alignright size-medium wp-image-92" /></a>

<a title="The box of relations below a single post with links to all kinds of lists at the left and some direct links to chronologically related posts" rel='lightbox[theme]' href='http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap3.jpg'><img src="http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap3-300x112.jpg" alt="Screencap of my new WP theme" title="New WordPress theme for BigSmoke" width="300" height="112" class="alignright size-medium wp-image-91" /></a>

<a title="This is how lists of posts look" href='http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap5.jpg' rel='lightbox[theme]'><img src="http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap5-300x207.jpg" alt="Screencap of my new WP theme" title="New WordPress theme for BigSmoke" width="300" height="207" class="alignright size-medium wp-image-93" /></a>

<a title="The bottom part of a single post with some comments below" href='http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap2.jpg' rel='lightbox[theme]'><img src="http://blog.bigsmoke.us/uploads/2008/07/new-theme-cap2-300x193.jpg" alt="Screencap of my new WP theme" title="New WordPress theme for BigSmoke" width="300" height="193" class="alignright size-medium wp-image-90" /></a>

<h2>Vertical navigation</h2>

<p>Ever since the first time that I saw a blog which featured vertical navigation instead of the typical columns, I&#8217;ve wanted to implement this for myself. Well, finally&#8230;</p>

<p>Site-wide elements use the complete width of the page. The page content is centered in the middle at 87.5%.  The identity stuff in the header and the navigation in the footer sits against a back blackground while the content area has the proven black on white for easy reading. I hope that the strong color-contrast as well as the clear difference in with between site-wide elements and page content makes it easy to keep focused on either reading or navigating without distractions.</p>

<h2>&#8230; and a talkative footer</h2>

<p>With this theme, I didn&#8217;t want another footer which consist of the odd logo and some loose copyright statements. I wanted a footer which you can actually read, even understand. And who cares if it takes up a little space? It&#8217;s at the bottom of the page.</p>

<h2>Related posts</h2>

<p>I&#8217;ve written an (unpublished, unpolished) plug-in which can generate a list of posts that are chronologically related. Traditionally, most blogs have a next/previous post link at the top and bottom of each post. This works very well if you limit your blog to one subject (which is really a very good idea anyway), but if, like mine, your blog is a little bit messy, you could say that someone who stumbled here searching for an article about <a href="/tag/subversion">Subversion</a> is not necessarily interested in the next post if this is a <a href="http://blog.bigsmoke.us/2007/07/29/linde-smiling-the-most-amazing-smile">photo of my baby niece</a>.</p>


<p>Hence the chronologically related posts plugin. With this plugin I can say wether I want a link to the first, previous and next post in the blog, within the same category, or matching a given number of tags. (The tag matching isn&#8217;t implemented yet, though. Also, matching on meta fields would be a kick-ass ass way to support explicit sequences.)</p>

<p>I put the list generated by this plug-in on top of a blue background besides the various context links of the post.</p>

<h2>Issues left</h2>

<p>I hope to have the first major revision of my theme ready soon. Here&#8217;s a list of some issues that I might address:</p>

<ul>
<li>The CSS renders a bit psychedelically in MSIE 6 (only version I tested) at the moment. Sigh&#8230; Let&#8217;s just hope that IE 7 will give better results. Then I&#8217;ll gladly drop the IE 6 support.</li>
<li>When viewing a category, the tag cloud in the navigation panel at the bottom only shows tags for that category. This has to do with the use with me calling the <tt>st_tag_cloud()</tt> from within the category template.</li>
<li>Some of the elements that I just showed to you don&#8217;t really look that good and most elements that I <em>didn&#8217;t</em> can be said to be &#8230; hideously ugly. <img src='http://blog.bigsmoke.us/wp-factory/wp-includes/images/smilies/icon_confused.gif' alt=':-?' class='wp-smiley' />  Some highlights: the header (should really be a few cool images), the comment form, and the Next/Previous Page links.</li>
</ul>

<h2>Comment!</h2>

<p>I&#8217;d almost forget all about the clean, new look of the comment list. And, if you register a <a href="http://www.gravatar.com/">Gravatar</a>, your comments will be accompanied by your avatar. Try it. Please!</p>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2008/07/13/new-theme/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Separate development/production environments for WordPress</title>
		<link>http://blog.bigsmoke.us/2008/07/12/separate-development-environment-for-wordpress</link>
		<comments>http://blog.bigsmoke.us/2008/07/12/separate-development-environment-for-wordpress#comments</comments>
		<pubDate>Sat, 12 Jul 2008 00:10:32 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=94</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re out Googling on how to maintain a separate development environment for a WordPress installation, you will only stumble across information about how to install all kinds of WAMPP packages. Well, I don&#8217;t care about WAMP (or WAMPP). I want to be able to edit my theme, change my plugins, mess with my database locally and then deploy my changes when they&#8217;re ready and well-tested (as if I ever&#8230;)</p>

<p>Rails was the obvious inspiration for how to do this properly. In Rails, the whole development and deployment process is very intuitive and powerful. In WordPress documentation I never even see the awareness of the need for this separation. They usually tell you to download stuff, upload it and muck about with it on the <em>life</em> production server. But, I&#8217;m not the <em>mucking-about-in-live-configurations</em> type. I&#8217;m the <em>I-fucked-this-up-so-often-I-want-a-staging-area</em> type. This post is about how I managed to fulfill this wish with WordPress.</p>

<h2>Changing the environment</h2>

<p>The first thing I had to do was to find some way to decide which environment to go into. For some reason I decided to use Apache&#8217;s mod_rewrite to set an environment variable based on the HTTP <tt>Host</tt> header. This is in fact very illogical, but we&#8217;ll get to that later.</p>

<pre class="apache">&lt;IfModule mod_rewrite.c&gt;
<span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">RewriteBase</span> /
<span style="color: #00007f;">RewriteCond</span> %<span style="color: #66cc66;">&#123;</span>HTTP_HOST<span style="color: #66cc66;">&#125;</span> =bsblog <span style="color: #66cc66;">&#91;</span>OR<span style="color: #66cc66;">&#93;</span>
<span style="color: #00007f;">RewriteCond</span> %<span style="color: #66cc66;">&#123;</span>HTTP_HOST<span style="color: #66cc66;">&#125;</span> =bsblog.molensteen
<span style="color: #00007f;">RewriteRule</span> . - <span style="color: #66cc66;">&#91;</span>env=WP_ENV:development<span style="color: #66cc66;">&#93;</span>
<span style="color: #00007f;">RewriteCond</span> %<span style="color: #66cc66;">&#123;</span>HTTP_HOST<span style="color: #66cc66;">&#125;</span> =blog.bigsmoke.us
<span style="color: #00007f;">RewriteRule</span> . - <span style="color: #66cc66;">&#91;</span>env=WP_ENV:production<span style="color: #66cc66;">&#93;</span>
&lt;/IfModule&gt;
&nbsp;
<span style="color: #adadad; font-style: italic;"># Keep out of WP's own block of rewrite rules below</span>
<span style="color: #adadad; font-style: italic;"># BEGIN WordPress </span></pre>

<p><q><tt>. -</tt></q> looks like a needle because it&#8217;s voodoo. The dot says I match anything and the dash says I change nothing of what I match. I do set an environment variable to whether I want to be in development or in production.</p>

<p>So I now have an Apache environment variable available for querying from within PHP (as if PHP doesn&#8217;t have a <tt>$_SERVER['HTTP_HOST']</tt> variable:-? ) and I can make use of that in my <tt>wp-config.php</tt>.</p>

<h2>Multiple configurations in one file</h2>

<p>I love configuration files that share the program&#8217;s language; <tt>wp-config.php</tt> being simple PHP code is what make this whole thing so easy:</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/getenv"><span style="color: #000066;">getenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'WP_ENV'</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #ff0000;">'production'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// ** MySQL settings ** //</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_NAME'</span>, <span style="color: #ff0000;">'blog'</span><span style="color: #66cc66;">&#41;</span>;             <span style="color: #808080; font-style: italic;">// The name of the database</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_USER'</span>, <span style="color: #ff0000;">'wordpress'</span><span style="color: #66cc66;">&#41;</span>;        <span style="color: #808080; font-style: italic;">// Your MySQL username</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_PASSWORD'</span>, <span style="color: #ff0000;">'[my password]'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// ...and password</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_HOST'</span>, <span style="color: #ff0000;">'bigsmoke.db'</span><span style="color: #66cc66;">&#41;</span>;        <span style="color: #808080; font-style: italic;">// 99% chance you won't need to change this value</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'WP_SITEURL'</span>, <span style="color: #ff0000;">'http://blog.bigsmoke.us'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/getenv"><span style="color: #000066;">getenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'WP_ENV'</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #ff0000;">'development'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// ** MySQL settings ** //</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_NAME'</span>, <span style="color: #ff0000;">'bsblog'</span><span style="color: #66cc66;">&#41;</span>;          <span style="color: #808080; font-style: italic;">// The name of the database</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_USER'</span>, <span style="color: #ff0000;">'root'</span><span style="color: #66cc66;">&#41;</span>;            <span style="color: #808080; font-style: italic;">// Your MySQL username</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_PASSWORD'</span>, <span style="color: #ff0000;">'[my password]'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// ...and password</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DB_HOST'</span>, <span style="color: #ff0000;">'127.0.0.1'</span><span style="color: #66cc66;">&#41;</span>;       <span style="color: #808080; font-style: italic;">// 99% chance you won't need to change this value</span>
  <a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'WP_SITEURL'</span>, <span style="color: #ff0000;">'http://bsblog'</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #808080; font-style: italic;">//define('WP_DEBUG', true);</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<a href="http://www.php.net/define"><span style="color: #000066;">define</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'WP_HOME'</span>, WP_SITEURL<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// You can have multiple installations in one database if you give each a unique prefix</span>
<span style="color: #0000ff;">$table_prefix</span>  = <span style="color: #ff0000;">'wp_'</span>;   <span style="color: #808080; font-style: italic;">// Only numbers, letters, and underscores please!</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// The rest of the stuff in this config file just isn't interesting</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>

<p>There&#8217;s a few things to note here. You have to use <tt>getenv()</tt> or <tt>$_SERVER</tt> instead of <tt>$_ENV</tt> because variables set by Apache end up in the former two. Another thing to note is that I should have just checked <tt>$_SERVER['HTTP_HOST']</tt> instead of resorting to mod_rewrite voodoo. For the rest it&#8217;s all very straight-forward: I make some database settings depend on which environment I&#8217;m in and I set the URL accordingly.</p>

<h2>Development URLs</h2>

<p>I had some trouble putting the pieces back together when newer WordPress versions started doing automatic redirects for URL that didn&#8217;t match <tt>siteurl</tt> in the <tt>wp_options</tt>. This change meant that when going to <tt>http://bsblog/</tt> (the development URL for this weblog) for example, I&#8217;d inevitably end up at <tt>http://blog.bigsmoke.us/</tt>.</p>

<p>Links had always been constructed according to this setting, so I had already been planning a plug-in to transform production URLs to development URLs. But, I learned (a little late) that this is completely unnecessary since <a href="http://codex.wordpress.org/Editing_wp-config.php"><tt>wp-config.php</tt> supports the configuration of a base URL</a>. I had wrongly assumed that settings that weren&#8217;t in the sample config file, simply didn&#8217;t exist.</p>

<p>Thus, after adding <tt>WP_SITEURL</tt> and <tt>WP_SITEHOME</tt> to <tt>wp-config.php</tt>, everything was working.</p>

<h2>Ideas to further enhance your configuration</h2>

<ul>
  <li>Don&#8217;t limit yourself to one development environment if you have more than one development server.</li>
  <li>Automate your deployment process. I use rsync for this.</li>
  <li>Write a script to clone your production database to your development database. There&#8217;s no substitute for actual data.</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2008/07/12/separate-development-environment-for-wordpress/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Apache&#8217;s ForceType directive overrides AddCharset directives</title>
		<link>http://blog.bigsmoke.us/2007/02/26/apache-forcetype-overrides-addcharset</link>
		<comments>http://blog.bigsmoke.us/2007/02/26/apache-forcetype-overrides-addcharset#comments</comments>
		<pubDate>Mon, 26 Feb 2007 22:30:01 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[406]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[iso-8859-1]]></category>
		<category><![CDATA[latin1]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sicirec]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[www.sicirec.org]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/2007/02/26/apache-forcetype-overrides-addcharset</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Yesterday, after uploading a refreshed <a href="http://www.sicirec.org/">www.sicirec.org</a>, some character encoding issues popped up because I had converted the website&#8217;s content from ISO-8859-1 (Latin 1) to UTF-8. (I wanted to be able to type and paste special characters from PuTTY into VIM without worrying about the particular encoding of each file.)</p>

<p>The Apache HTTPD at <a href="http://www.initfour.nl/">InitFour</a>, our webhosting provider, is configured to send ISO-8859-1 by default, while the one on our test server is configured for UTF-8. This caused a little bit of a surprise when I uploaded the refreshed website and saw all characters outside the ASCII range mangled on the life website!</p>

<p>I quickly dug into my <tt>.htaccess</tt> file to add the <tt><a href="http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addcharset">AddCharset</a> utf-8 .xhtml</tt> directive. To my surprise, this didn&#8217;t do squat. A lot of fiddling, reloading and researching later, I realized that the following section in my <tt>.htaccess</tt> file rendered the <tt><a href="http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addcharset">AddCharset</a></tt> directive irrelevant:</p>

<pre class="php">&lt;Files *.xhtml&gt;
ForceType text/html
&lt;/Files&gt;</pre>

<p>I had to change the <tt><a href="http://httpd.apache.org/docs/1.3/mod/mod_mime.html#forcetype">ForceType</a></tt> directive to include the charset as a MIME parameter:</p>

<pre class="php">&lt;Files *.xhtml&gt;
ForceType <span style="color: #ff0000;">'text/html; charset=UTF-8'</span>
&lt;/Files&gt;</pre>

<p>Now, it all <em>seemed</em> to work. (Except that it didn&#8217;t really because I do some ridiculously complex content negotiation stuff involving a 406 handler in PHP that <a href="http://www.php.net/virtual"><tt>virtual</tt></a>s the most appropriate variant when no match is found. This script didn&#8217;t send a useful <tt>Content-Type</tt> header. After first adding it to the script, I noticed that the <tt><a href="http://httpd.apache.org/docs/1.3/mod/core.html#adddefaultcharset">AddDefaultCharset</a></tt> is actually allowed in <tt>.htaccess</tt> context&#8212;a discovery which luckily rendered the other hacks useless.)</p>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2007/02/26/apache-forcetype-overrides-addcharset/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
