[Typo3-dev] CLI API proposal

Kasper Skårhøj kasper2004 at typo3.com
Fri Dec 31 12:06:59 CET 2004


Hi Rainer,

I have changed the suggestions in Inside TYPO3 document for now so your
comment is taken notice off:


<snip>
Initialize TYPO3 backend in a PHP shell script (CLI mode)
Most scripts in TYPO3 expect to be executed from a web browser. However
you might need to create a PHP script which is executed in a Unix shell
as a cronjob. In itself PHP is capable of that as long as PHP was also
compiled as a binary (typically “/usr/bin/php”) but you need to do some
tricks in order to initialize TYPO3s backend environment.


Tricky script path
The greatest challenge is to make the script recognize its own path.
This is necessary for all includes afterwards. It seems that the path of
the script is available as the variable $_ENV['_'] in most cases.
However it changes value depending on how you call the script. In order
to make life easy for our programming we decide that the script must
always be executed by its absolute path. So “./myphpshellscript.phpsh”
will not work, but “/abs/path/to/typo3conf/ext/myext/
myphpshellscript.phpsh” will.



Note about FreeBSD: It has been reported (by Rainer Kuhn, punkt.de –
thanks) that on FreeBSD the path is not available in $_ENV['_'] but can
be extracted from $_SERVER['argv'][0]. Generally there are not enough
experience and knowledge about this issue to state something final, but
for now we suggest a fall back solution as seen below where $_ENV['_']
is tested and if not found, falls back to $_SERVER['argv'][0]


Basic framework
To set up a PHP shell script that initializes the TYPO3 you create a
file with the framework here:



   1: #! /usr/bin/php -q
   2: <?php
   3:
   4: // *****************************************
   5: // Standard initialization of a CLI module:
   6: // *****************************************
   7:
   8:     // Defining circumstances for CLI mode:
   9: define('TYPO3_cliMode', TRUE);
  10:
  11:     // Defining PATH_thisScript here: Must be the ABSOLUTE path of this script in the right context:
  12:     // This will work as long as the script is called by it's absolute path!
  13: define(PATH_thisScript, $_ENV['_'] ? $_ENV['_'] : $_SERVER['argv'][0]);
  14:
  15:     // Include configuration file:
  16: require(dirname(PATH_thisScript).'/conf.php');
  17:
  18:     // Include init file:
  19: require(dirname(PATH_thisScript).'/'.$BACK_PATH.'init.php');
  20:
  21:
  22:
  23: # HERE you run your application!
  24:
  25: ?>

</snip>


Happy new year!!

- kasper




On Wed, 2004-12-01 at 11:39, Rainer Kuhn wrote:
> Hi all,
> 
> in the last days I wrote some command line interface scripts using the CLI 
> API of TYPO3 3.7.0 as described in "Inside TYPO3" 
> (http://typo3.org/documentation/document-library/doc_core_inside/Initialize_TYPO3_bac/). 
> Yesterday I transferred them to our production server running on FreeBSD and 
> experienced problems with the script path using the suggested standard 
> initialization of a CLI module.
> 
> As described in chapter "Tricky script path" in the above mentioned doc, "it 
> seems that the path of the script is available as the variable 
> $HTTP_ENV_VARS['_'] in most cases". I think this is right for Linux system 
> (as $HTTP_ENV_VARS['_'] is supported apparently by bash and /bin/sh is 
> actually a bash at Linux), but not for some other UNIX systems like FreeBSD.
> 
> The suggested workaround for script path problems is: "If for some reason 
> the environment where the script is run does not offer this value in 
> $HTTP_ENV_VARS['_'] you will have to find it elsewhere and manuall insert 
> it. There seems to be no general solution for this problem."
> 
> As I thought manually inserting/hardcoding the path is not a good and 
> flexible way, I looked for another solution and found the following:
> 
> http://www.php.net/manual/en/features.commandline.php
> <cit>
> "There are certain php.ini directives which are overridden by the CLI SAPI 
> because they do not make sense in shell environments: [...]
> DIRECTIVE: register_argc_argv
> CLI SAPI default value: TRUE
> COMMENT: Because this setting is TRUE you will always have access to argc 
> (number of arguments passed to the application) and argv (array of the 
> actual arguments) in the CLI SAPI.
> As of PHP 4.3.0, the PHP variables $argc and $argv are registered and filled 
> in with the appropriate values when using the CLI SAPI. Prior to this 
> version, the creation of these variables behaved as they do in CGI and 
> MODULE versions which requires the PHP directive register_globals to be on. 
> Regardless of version or register_globals setting, you can always go through 
> either $_SERVER or $HTTP_SERVER_VARS. Example: $_SERVER['argv']"
> </cit>
> 
> In order to the current TYPO3 guidelines the command line script must always 
> be executed by its absolute path, so using argv could be an alternative. So 
> I tried the initialization of my CLI module using
> 
> define(PATH_thisScript, $_SERVER['argv'][0]);
> 
> instead of the suggested
> 
> define(PATH_thisScript, $HTTP_ENV_VARS['_']);
> 
> and this worked fine for me with different shells. Maybe this could be a 
> general solution for the script path problem (can somebody try this on 
> different systems -e.g. Windows- to check if it works in other environments)?
> 
> Comments appreciated...
> 
> Cheers,
> Rainer
> _______________________________________________
> Typo3-dev mailing list
> Typo3-dev at lists.netfielders.de
> http://lists.netfielders.de/cgi-bin/mailman/listinfo/typo3-dev
-- 
- kasper

----------------
Man søger fred i rigdom, glans og ære
og tænker: Hvad kan hjertet mer begære?
men dybt derinde bor den samme længsel,
og hjertet græder i sit gyldne fængsel.






More information about the TYPO3-dev mailing list