Running TrackStar on Windows/XAMPP and FreeBSD

I’m learning Yii and worked through getting TrackStar (from Web Application Development with Yii and PHP) running locally on Windows/XAMPP, and then on FreeBSD. The trickiest part was getting the trackstar/.htaccess file to correctly hide index.php.

I have also been experimenting with cbdb (Comic Book DataBase) from Yii Rapid Application Development Hotshot), and next will be exploring CMS (Content Management System) from The Yii Book. I plan to use the best most applicable features of each to bootstrap Maestro development.

I started with a zip download of the full TrackStar app from Jeff Winesett’s trackstar repo on GitHub (using trackstar/ directory in the repo) and a yii-1.1.13 download (instead of using the YiiRoot/ directory in the repo). The code on GitHub includes the book errata (compared to the chapter code in the download bundle). I tested TrackStar locally first using Windows XAMPP 1.8.1 (PHP 5.4.7 and MySQL 5.5.27), then moved it to a FreeBSD server.

1. At first (locally), yiic and phpunit didn’t behave as expected, but it was my fault. I edited trackstar/index.php to use yii-1.1.13, but neglected to also edit trackstar/protected/yiic.php and trackstar/protected/tests/bootstrap.php (and for completeness, trackstar/index-test.php also).

2. I had to change “truncateTABLE” to “delete” in two of the migration scripts in order to “migrate down” (MySQL 5.5+ can’t truncate InnoDB tables with foreign keys).

3. TrackStar now runs, but I can’t login because there are no users in the database. The solution was to manually add “User One” and “User Two” with SQL as per the book.

INSERT INTO tbl_user (email, username, password) VALUES
  ('test1@notanaddress.com','User One', MD5('test1')),
  ('test2@notanaddress.com','User Two', MD5('test2'));

4. Now I can login as either “User One” or “User Two”, but there are errors creating new projects (and there are new projects being added in tbl_project, which could be a bug…). Running “./yiic rbac” fixes the project creation errors. Now I can create projects, issues and comments, assign users to projects, delete projects, etc. TrackStar Rocks!

5. Next, I copied TrackStar from my development workstation to a FreeBSD server (PHP 5.4.10 and MySQL 5.5.30), modified permissions on directories (for reading, and writing to where necessary by Apache), created a trackstar.conf file, restarted apache and created a database. However, in order to connect to the database I had to change the connection string in main.php and console.php from “host=127.0.0.1” to “host=localhost” to get the migration scripts to run and the main trackstar page to load (I also had to change the database username and password in the connection string, but those are secret 😉 ).

6. Now connecting to the database and with the main page loading, navigating using the menu results in a 404 error “The requested URL /usr/local/www/trackstar/index.php was not found on this server.”

The cause of the final problem turned out to be the .htaccess file provided with TrackStar, which  assumes that trackstar is served from the root of the web server (i.e. https://www.dalescott.net/ is TrackStar), when I want “https://www.dalescott.net/trackstar/”. I don’t know why it worked locally on XAMPP with the original .htaccess (but figuring it out is on my ToDo list).

Here is my final Apache trackstar.conf

casper# cat /usr/local/etc/apache22/Includes/trackstar.conf
Alias /trackstar "/usr/local/www/trackstar"

<Directory "/usr/local/www/trackstar">
    Options All
    AllowOverride All
    Order Deny,Allow
    Deny from All
    Allow from All
</Directory>
casper#

and .htaccess

casper# cat /usr/local/www/trackstar/.htaccess
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
</IfModule>

# Unless an explicit file or directory exists, redirect all request to Yii
# entry script
<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  # de facto std recommendation, but assumes/requires that app is root of webserver
  # RewriteRule . index.php

  # Yii forum post "Problem with .htaccess"
  # www.yiiframework.com/forum/index.php/topic/15694-problem-with-htaccess/
  # (1) RewriteRule ^.*$ /index.php
  # (2) RewriteRule ^.*$ /trackstar/index.php

  # seems to be more formal version of Yii forum post (1)
  # forums.laravel.io/viewtopic.php?id=5504
  # RewriteRule ^(.*)$ /index.php/$1 [L]

  # seems to be more formal version of Yii forum post (2)
  # ellislab.com/forums/membe%20r/140390/viewthread/234295/
  # RewriteRule ^(.*)$ https://www.dalescott.net/trackstar/index.php/$1 [L,QSA]

  RewriteRule ^.*$ /trackstar/index.php
</IfModule>
casper#

Converting text files between Windows and Unix

I develop on both Windows and Unix laptops for deployment on Unix servers, and invariably forget to save text files from Windows in Unix-format (in Windows, lines end with both the line feed and carriage return ASCII characters, but Unix uses only a line feed). It doesn’t cause any problems, but it’s sure ugly to look at on the server. Googling around, I found the following solution using the tr command.

> mv file file.tmp
> tr -d '\r' < file.tmp > file
> rm file.tmp

After putting up with what was really more of an ugly hack for about a year, I decided it was time to write a quick and dirty shell script to hide the mess – but before reinventing the wheel thought I’d try a quick ‘net search. After all, I can’t be the first one to do this, could I?

Asking Google again for how to convert text from DOS to Unix, but this time including “shell script”, I mostly got the same links to tr, sed, and perl tutorials that I got the first time. There were also a couple links to shell scripts, but almost hidden in the hits was a reference to “flip”, a ~250 line C program that has apparently been the de facto standard command since it was released into the wild in 1998.

flip does a lot more than I was going to implement in my quick and dirty shell script. It detects binary files and leaves them alone unless intentionally overridden. It doesn’t modify files that are already in the specified format, and it preserves file timestamps. It handles user interrupts gracefully and doesn’t leave behind garbage or corrupted files.

Compiling and installing flip from the FreeBSD ports collection took all of 20 seconds. I don’t know why I didn’t find flip a year ago, but think I’ll dig out my venerable Unix in a Nutshell and see what other utilities are out there I could use, but don’t know about.