class Utils (View source)

Class to manage utility methods

Properties

DoliDB $db
$error
string[] $errors
string $output
array{commandbackuplastdone: string, commandbackuptorun: string} $result

Methods

__construct($db)

Constructor

purgeFiles(string $choices = 'tempfilesold+logfiles', $nbsecondsold = 86400)

Purge files into directory of data files.

dumpDatabase($compression = 'none', string $type = 'auto', $usedefault = 1, $file = 'auto', $keeplastnfiles = 0, $execmethod = 0, $lowmemorydump = 0)

Make a backup of database CAN BE A CRON TASK

array{result: int, output: string, error: string}
executeCLI(string $command, string $outputfile, int<0, 2> $execmethod = 0, ?string $redirectionfile = null, int<0, 1> $noescapecommand = 0, ?string $redirectionfileerr = null)

Execute a CLI command.

int
generateDoc(string $module)

Generate documentation of a Module

int
compressSyslogs()

This saves syslog files and compresses older ones.

int
backupTables(string $outputfile, string $tables = '*')

Backup the db OR just a table without mysqldump binary, with PHP only (does not require any exec permission) Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php) Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump.

sendBackup(string $sendto = '', string $from = '', string $subject = '', string $message = '', string $filename = '', $filter = '', $sizelimit = 100000000)

Make a send last backup of database or fil in param CAN BE A CRON TASK

int
cleanUnfinishedCronjob()

Clean unfinished cronjob in processing when pid is no longer present in the system CAN BE A CRON TASK

Details

__construct($db)

Constructor

@param DoliDB $db Database handler

Parameters

$db

purgeFiles(string $choices = 'tempfilesold+logfiles', $nbsecondsold = 86400)

Purge files into directory of data files.

CAN BE A CRON TASK

Parameters

string $choices

Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than $nbsecondsold seconds, 'logfiles', or mix of this). Note that 'allfiles' is also possible but very dangerous. @param int $nbsecondsold Nb of seconds old to accept deletion of a directory if $choice is 'tempfilesold', or deletion of file if $choice is 'allfiles' @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)

$nbsecondsold

dumpDatabase($compression = 'none', string $type = 'auto', $usedefault = 1, $file = 'auto', $keeplastnfiles = 0, $execmethod = 0, $lowmemorydump = 0)

Make a backup of database CAN BE A CRON TASK

@param string $compression 'gz' or 'bz' or 'none'

Parameters

$compression
string $type

'mysql', 'postgresql', ... @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron) @param string $file 'auto' or filename to build @param int $keeplastnfiles Keep only last n files (not used yet) @param int $execmethod 0=Use default method (that is 1 by default), 1=Use the PHP 'exec' - need size of dump in memory, but low memory method is used if GETPOST('lowmemorydump') is set, 2=Use the 'popen' method (low memory method) @param int $lowmemorydump 1=Use the low memory method. If $lowmemorydump is set, it means we want to make the compression using an external pipe instead retrieving the content of the dump in PHP memory array $output_arr and then print it into the PHP pipe open with xopen(). @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)

$usedefault
$file
$keeplastnfiles
$execmethod
$lowmemorydump

array{result: int, output: string, error: string} executeCLI(string $command, string $outputfile, int<0, 2> $execmethod = 0, ?string $redirectionfile = null, int<0, 1> $noescapecommand = 0, ?string $redirectionfileerr = null)

Execute a CLI command.

Parameters

string $command

Command line to execute. Warning: The command line is sanitize by escapeshellcmd(), except if $noescapecommand set, so can't contains any redirection char '>'. Use param $redirectionfile if you need it.

string $outputfile

A path for an output file (used only when method is 2). For example: $conf->admin->dir_temp.'/out.tmp';

int<0, 2> $execmethod

0=Use default method (that is 1 by default), 1=Use the PHP 'exec', 2=Use the 'popen' method

?string $redirectionfile

If defined, a redirection of output to this file is added.

int<0, 1> $noescapecommand

1=Do not escape command. Warning: Using this parameter needs you already have sanitized the $command parameter. If not, it will lead to security vulnerability. This parameter is provided for backward compatibility with external modules. Always use 0 in core.

?string $redirectionfileerr

If defined, a redirection of error is added to this file instead of to channel 1.

Return Value

array{result: int, output: string, error: string}

array('result'=>...,'output'=>...,'error'=>...). result = 0 means OK.

int generateDoc(string $module)

Generate documentation of a Module

Parameters

string $module

Module name

Return Value

int

Return integer <0 if KO, >0 if OK

int compressSyslogs()

This saves syslog files and compresses older ones.

Nb of archive to keep is defined into $conf->global->SYSLOG_FILE_SAVES CAN BE A CRON TASK

Return Value

int

0 if OK, < 0 if KO

int backupTables(string $outputfile, string $tables = '*')

Backup the db OR just a table without mysqldump binary, with PHP only (does not require any exec permission) Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php) Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump.

Note2: db2SQL by Howard Yeend can be an alternative, by using SHOW FIELDS FROM and SHOW KEYS FROM we could generate a more precise dump (eg: by getting the type of the field and then precisely outputting the right formatting - in quotes, numeric or null - instead of trying to guess like we are doing now).

Parameters

string $outputfile

Output file name

string $tables

Table name or '*' for all

Return Value

int

Return integer <0 if KO, >0 if OK

sendBackup(string $sendto = '', string $from = '', string $subject = '', string $message = '', string $filename = '', $filter = '', $sizelimit = 100000000)

Make a send last backup of database or fil in param CAN BE A CRON TASK

Parameters

string $sendto

Recipients emails

string $from

Sender email

string $subject

Topic/Subject of mail

string $message Message
string $filename

List of files to attach (full path of filename on file system) @param string $filter Filter file send @param int $sizelimit Limit size to send file @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)

$filter
$sizelimit

int cleanUnfinishedCronjob()

Clean unfinished cronjob in processing when pid is no longer present in the system CAN BE A CRON TASK

Return Value

int

0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)

Exceptions

Exception