Source for file trax.php
Documentation is available at trax.php
* Create Trax application work area
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @copyright (c) Walter O. Haas 2006
* @version $Id: trax.php 227 2006-07-09 20:53:36Z john $
* @author Walt Haas <haas@xmission.com>
* Define where to find files to copy to the work area
* Set automatically by the Pear installer when you install Trax with
* the <b>pear install</b> command. If you are prevented from using
* <b>pear install</b>, change "@DATA-DIR@/PHPonTrax" by hand to the
* full filesystem path of the location where you installed the Trax
define("SOURCE_DIR", "@DATA-DIR@/PHPonTrax/data/");
* Symbol substitution tables
* $search and $replace below are used to perform substitutions of
* symbols in a file being copied. $search is an array of
* Perl-compatible regular expressions, and $replace is a congruent
* array of replacements for RE matches. So everywhere that the RE
* in, for example, $search[3] is matched in a file, the matching
* string is replaced by the contents of $replace[3].
'/@TRAX-CONFIG@/' // symbol for the full filesystem path
// to the Trax config/ directory in
'' // actual value of the full filesystem
// path to the Trax config/ directory
// in the user's work area
global $search, $replace;
// Get command line argument, if any
|| ($GLOBALS['argc'] < 2)) {
usage(); // print Usage message and exit
// Check for excess arguments
if ($GLOBALS['argc'] > 2) {
echo "unrecognized command argument ". $GLOBALS['argv'][2]. "\n";
// Destination directory on command line
$dstdir = $GLOBALS['argv'][1];
// Guarantee it ends with DIRECTORY_SEPARATOR
if (substr($dstdir,- 1,1) != DIRECTORY_SEPARATOR) {
$dstdir .= DIRECTORY_SEPARATOR;
// Assign real values for symbol substitution
$replace[0] = realpath($dstdir). '/config'; // actual value of
// the full filesystem path to the
// Trax config/ directory in the
// copy source directory to destination directory
* Copy a directory with all its contents
* When a file whose filename ends '.log' is created, its permissions
* are set to be world writable.
* @param string $src_path Path to source directory
* @param string $dst_path Path to destination directory
* @return boolean true=>success, false=>failure.
function copy_dir($src_path,$dst_path) {
// Make sure we have directories as arguments
echo $src_path. " is not a directory\n";
echo $dst_path. " is not a directory\n";
// Open the source directory
echo "unable to open $src_path\n";
// Copy contents of source directory
while (false !== ($src_file = readdir($src_handle))) {
if (!is_dir($src_path . $src_file)) {
// If this file exists only to make the directory
// non-empty so that PackageFileManager will add it to
// the installable package, don't bother to copy it.
if ($src_file == '.delete_this_file') {
// This is a regular file, need to copy it
// A destination file or directory with this name exists
if (is_file( $dst_path . $src_file )) {
// A regular destination file with this name exists.
// Check whether it's different from source.
if ($src_content == $dst_content) {
// Source and destination are identical
echo "$dst_path$src_file exists\n";
// New and old files differ. Save the old file.
$stat = stat($dst_path. $src_file);
$new_name = $dst_path. $src_file. '.'. $stat[9];
if (!rename($dst_path. $src_file,$new_name)) {
echo "unable to rename $dst_path$src_file to $new_name\n";
echo "renamed $dst_path$src_file to $new_name\n";
// Destination file does not exist. Create it
if (!copy_file($src_path . $src_file, $dst_path . $src_file)) {
// Log files need to be world writeable
if (substr($src_file,- 4,4) == '.log') {
chmod($dst_path . $src_file, 0666);
// Generator & Console needs to be executable
if ($src_file == 'generate.php' || $src_file == 'console.php') {
chmod($dst_path . $src_file, 0754);
echo "$dst_path$src_file created\n";
// This is a directory. Ignore '.' and '..'
if ( ($src_file == '.') || ($src_file == '..') ) {
// This directory needs to be copied.
// Recursive call to copy directory
if (!copy_dir($src_path . $src_file . DIRECTORY_SEPARATOR,
$dst_path . $src_file . DIRECTORY_SEPARATOR)) {
* Create a directory if it doesn't exist
* @param string $dst_dir Path of directory to create
* @return boolean true=>success, false=>failed
// Does a directory of this name exist?
// A destination file or directory with this name exists
// A destination directory with this name exists.
echo "$dst_dir". DIRECTORY_SEPARATOR. " exists\n";
// There is an old destination file with the same
// name as the new destination directory.
$new_name = $dst_dir. '.'. $stat[9];
if (!rename($dst_dir,$new_name)) {
echo "unable to rename $dst_dir to $new_name\n";
echo "renamed $dst_dir to $new_name\n";
// Destination directory does not exist. Create it
if (!mkdir($dst_dir,0775,true)) {
echo "$dst_dir". DIRECTORY_SEPARATOR. " created\n";
* Copy a Trax file into user's work area, substituting @TRAX-...@
* @param string $src_path Path to source file
* @param string $dst_path Path to destination file
* @return boolean true=>success, false=>failure.
global $search, $replace;
// Read source file into a string
// Substitute @TRAX-...@ symbols for appropriate values
// Write out file contents
* Output a Usage message and exit
echo "Usage: @BIN-DIR@". DIRECTORY_SEPARATOR. "trax"
. " ". DIRECTORY_SEPARATOR. "path". DIRECTORY_SEPARATOR. "to"
. DIRECTORY_SEPARATOR. "your". DIRECTORY_SEPARATOR. "app
The 'trax' command creates a new Trax application with a default
directory structure and configuration at the path you specify.
trax ". DIRECTORY_SEPARATOR. "var". DIRECTORY_SEPARATOR. "www"
. DIRECTORY_SEPARATOR. "html
This generates a skeletal Trax installation in "
. DIRECTORY_SEPARATOR. "var". DIRECTORY_SEPARATOR. "www"
. DIRECTORY_SEPARATOR. "html.
See the README in the newly created application to get going.
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|