Tag Archives: PHP

Installing PHPDepend

When installing PHPDepend thru PEAR on Ubuntu, I ran into an error caused by a missing MagickWand-config:

$ pear channel-discover pear.pdepend.org
$ pear install --alldeps pdepend/PHP_Depend-beta

checking ImageMagick MagickWand API configuration program…
configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.
ERROR: `/tmp/pear/temp/imagick/configure –with-imagick’ failed

Installing imagick thru PECL results in the same error message:

$ pecl install --alldeps imagick

The MagickWand API provides interfaces between high level programming languages and the ImageMagick image processing libraries.

Install the MagickWand libraries:

$ apt-get install libmagickwand-dev libmagickcore-dev

This finally enables the installation of the aforementioned PEAR & PECL libraries. After that, test the installation:

$ MagickWand-config --version
6.6.0 Q16
$ pdepend --version
PHP_Depend 1.0.7 by Manuel Pichler

Piwik Notes #1 :: Cascading Include References

In ‘index.php’ (line 35) a ‘testMinimumPhpVersion.php’ is required. If you comment this line out, taking it for what it seems, Piwik dies with a Fatal Error, since the file includes another file, which defines necessary functions etc.

This is an example for using Cascading Include References. Do not take this as a role model for software development, it makes your code confusing, since you do not expect an all-purpose loader defined in a file ‘upgrade.php’ included in a file ‘testMinimumPhpVersion.php’ required in your ‘index.php’. Dig it?

Btw, PHP version and Zend PHP4 compatibility mode indeed are checked in the ‘testMinimumPhpVersion.php’, and, if inappropriate, a warning message is defined, but not displayed at first. Why not? A function showing error messages in an indeed! ‘friendly UI’ is defined. Then, if a warning message is set, this function is called with the warning message.

Don’t do this, unless you intend to rename your error message display function every now and then. Keep your processes in logical blocks, the refactoring guy will be grateful.

The mentioned error message display function is referenced in five different files.

The checks for PHP version and Zend PHP4 compatibility mode can be commented out once your Piwik instance is running, due to scalability reasons, such tests belong to a prerequisite check upon the installation process.

Piwik is a high end Open Source Web Analytics Tool.

Switch hates me…

After all, my ignorance caused a complicated troubleshooting on an control structure of a Java method: a ‘break’ was missing. But what happened? Reason enough for some thoughts about the Switch Case syntax, something, we all, of course, know very well.

The following code

int x = 1;
switch (x) {
		
 case 1:
	System.out.println( "1" );
 case 2:
	System.out.println( "2" );
 case 3:
	System.out.println( "3" );
 default:
	System.out.println( "default" );
		
} // switched

prints on the command line:

$:
1
2
3
default

Changing the value of the variable x to 3, prints:

$:
3
default

Nothing changes, if you move the default block before the first case block, as long as the value of the switch variable is found in any case expression.

Only if the default block is before the first case block and the value of the switch variable is not found in any case expression, the default block and all case blocks will be executed, until a ‘break’ is found or the end of the switch structure is reached.

In short: If a ‘break’ statement is missing after a case block, all following case blocks will be executed, whether the case expression is true, or not. This continues until a ‘break’ or the end of the switch structure is reached and is called fall-through.

This behaviour is identical in PHP.

PEAR::Console_CommandLine

With minor bugfixes the stable version 1.1.1 of PEAR::Console_CommandLine was recently released. The package provides full featured command line options and arguments parser and can easily be included in your application. The parser can be built by passing an array of options to the constructor or by setting the public properties of an instantiated parser object.

require_once 'Console/CommandLine.php';

$parser = new Console_CommandLine();
$parser -> description = 'Dump an OWL (Document Management System) 
Database into a corresponding file system';
$parser -> version = '0.9.1';

$parser -> addOption('simulation', array(
    'short_name'  => '-s',
    'long_name'   => '--simulation',
    'description' => 'simulate dump, do not write into filesystem',
    'action'      => 'StoreTrue'
));

$parser -> addOption('verbose', array(
    'short_name'  => '-v',
    'long_name'   => '--verbose',
    'description' => "print status messages to stdout",
    'action'      => 'StoreTrue'
));

The command line:

$>php owl2fs.php --help

Dump an OWL (Document Management System) 
Database into a corresponding file system

Usage:
  owl2fs.php [options]

Options:
  -s, --simulation  simulate dump, do not write into filesystem
  -v, --verbose     print status messages to stdout
  -h, --help        show this help message and exit
  --version         show the program version and exit

Using the fromXmlFile() or fromXmlString() methods, options can be passed in the XML format.

The ‘action’ key in the options section allows you to pass several datatypes and can as well prompt for a password. User input can be passed to a callback function, e.g. for validation.

To give a clear structure to your interface, subcommands can easily be created using the addCommand() method:

$subcommand = $parser -> addCommand('extract', array(
   'description' => 'extract content from pdf files'
));

$subcommand -> addOption('images', array(
   'short_name'  => '-i',
   'long_name'   => '--image',
   'action'      => 'StoreTrue',
   'description' => 'extract images from pdf files'
));

$subcommand -> addOption('text', array(
   'short_name'  => '-t',
   'long_name'   => '--text',
   'action'      => 'StoreTrue',
   'description' => 'extract text from pdf files'
));

The command line:

$>php owl2fs.php --help

Dump an OWL (Document Management System) Database 
into a corresponding file system

Usage:
  testfile5.php [options]
  testfile5.php [options] <command> [options]

Options:
  -s, --simulation  simulate dump, do not write into filesystem
  -v, --verbose     print status messages to stdout
  -h, --help        show this help message and exit
  --version         show the program version and exit

Commands:
  extract  extract content from pdf files

To get information about the subcommand, enter:

$>php owl2fs.php extract --help

extract content from pdf files

Usage:
  owls2fs.php [options] extract [options]

Options:
  -i, --image  extract images from pdf files
  -t, --text   extract text from pdf files
  -h, --help   show this help message and exit

Console_CommandLine can as well be used in web environments as it accesses the HTTP $_GET and $_POST variables. So using e.g.

http://myApp.php?name=value

, ‘value’ will be passed to an option ‘name’ defined with

$parser->addOption(
'name', array(...))

A valuable documentation with many examples can be found on the project docs page