System class

From PhiWiki
Jump to: navigation, search

The system class contains data related to perform system calls.

Usage

The request class is a global object in the server scripting environment.

loadModule('system');
var s=system;

Properties

  • Number exitcode [read]
  • String lasterror [read]
  • StringArray properties [read]
  • String systemname [read]

Number exitcode

Returns the exit code of the last exec call or '0' on success.

var code=system.exitcode;

String lasterror

Returns the last error occured or an empty string.

var error=system.lasterror;

See also: system.exitcode.

StringArray properties

Returns the available properties for the system class in a string array.

var i=0;
while ( system.properties[i] ) print( system.properties[i++] );

String systemname

Returns the system name.

var name=system.systemname;

Functions

String countryName()

Returns the english name for a given locale.

var s=system.countryName( langCode="C" );
var s=system.countryName( "de_CH" );
// s='Switzerland'

String createUuid()

Creates a unique uuid in the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.

var uuid=system.createUuid();

Note: this is also known as GUID in MS Windows.

String dayName()

Returns the translated day name for a locale.

var name=system.dayName( day_id, langCode="C" );
var name=system.dayName( 5, "de" );
// name='Samstag'

A valid day_id is in the range from 0 (Monday) to 6 (Sunday).

String exec()

Executes the program (shell, batch, exe) and returns the string written to stdout.

var result=system.exec( program, attributes, codec='utf-8' );

The program argument is mandatory, attributes and codec is optional.

var result=system.exec( '/opt/bin/myprog.sh', '-l -2' );

The command above executes myprog.sh with the parameters -l and -2 and treats the returned text as utf-8 text.

The argument attributes may be a single string like the example above or an array of strings:

var args=new Array();
args[0]='first argument';
args[1]='second argument';
var res=system.exec( 'C:\Programs\Phisys\bin\myproc.exe', args );

This is i.e. necessary if an argument contains spaces (usually spaces are used to separate arguments).

You may check the system.exitcode which is unequal '0' if an error occured.

See also: Additional notes.

String languageName()

Returns the english language name for a given locale.

var s=system.languageName( langCode="C" );
var s=system.languageName( "de_DE" );
// s='German'

Void log()

Logs string to the error log (Apache module or Phis server log).

system.log( string );

Note: for you convenience this is also provided through server.log().

String md4()

Returns a MD4 hash of string s.

var md4=system.md4( s );

String md5()

Returns a MD5 hash of string s.

var md5=system.md5( s );

String monthName()

Returns the translated month name for a locale.

var name=system.monthName( month_id, langCode="C" );
var name=system.monthame( 5, "de" );
// name='Juni'

A valid month_id is in the range from 0 (January) to 11 (December).

String sha1()

Returns a SHA1 hash of string s.

var sha1=system.sha1( s );

String toFormatedString()

Returns a Date to a localized formated string.

loadModule('system');

// system.toFormatedString( Date, format, langCode="C" );
var d=new Date( 2010, 2, 3 );
var s=system.toFormatedString( d, "yyyy MMMM dd", "de" );
// s='2010 März 03'

You may also use the return value of an SQL statement if the data type represents a DATETIME, DATE or TIME:

sql.query( "SELECT created FROM invoice WHERE id='"+id+"'" ); // 'created' is a DATETIME type
if ( sql.next ) {
  var s=system.toFormatedString( sql.value( 0 ), "yyyy-MMM-dd", document.lang );
  // s='2010-Jun-11'
}

Valid date format characters:

  • d - the day as number without a leading zero (1 to 31)
  • dd - the day as number with a leading zero (01 to 31)
  • ddd - the abbreviated localized day name (e.g. 'Mon' to 'Sun')
  • dddd - the long localized day name (e.g. 'Monday' to 'Sunday')
  • M - the month as number without a leading zero (1-12)
  • MM - the month as number with a leading zero (01-12)
  • MMM - the abbreviated localized month name (e.g. 'Jan' to 'Dec')
  • MMMM - the long localized month name (e.g. 'January' to 'December')
  • yy - the year as two digit number (00-99)
  • yyyy - the year as four digit number

Valid time format characters:

  • h - the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
  • hh - the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
  • H - the hour without a leading zero (0 to 23, even with AM/PM display)
  • HH - the hour with a leading zero (00 to 23, even with AM/PM display)
  • m - the minute without a leading zero (0 to 59)
  • mm - the minute with a leading zero (00 to 59)
  • s - the second without a leading zero (0 to 59)
  • ss - the second with a leading zero (00 to 59)
  • z - the milliseconds without leading zeroes (0 to 999)
  • zzz - the milliseconds with leading zeroes (000 to 999)
  • AP or A - interpret as an AM/PM time (AP must be either "AM" or "PM")
  • ap or a - interpret as an AM/PM time (ap must be either "am" or "pm")

String toLocaleString()

Returns a Date to a string usually used for the appropriate locale.

var string=system.toLocaleString( Date, langCode="C" );
var d=new Date();
var s=system.toLocaleString( d, "de_DE" );

Additional notes

You may use different codecs to translate the text returned by the exec call. If you overwrite the codec with an empty string Phi will perform a system call to get the codec for the current locale the server is running on.

All functions using locale settings need the appropriate locale installed on the system otherwise the 'C'-locale is used (which is usually an english translation).