From 891261873272950698aa001f6baf8e8acd6833d4 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Mon, 15 Jun 2020 15:18:00 +0200 Subject: [PATCH] =?UTF-8?q?Revert=20"N=C2=B02214=20Add=20PHP=20check=20in?= =?UTF-8?q?=20CLI=20scripts"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit c768e18e2b792838887a00c21862f1d4abd7529b. No risk taken for the 2.7.1 : this will be included but for 2.8 ! --- application/clipage.class.inc.php | 18 +++-- datamodels/2.x/itop-backup/backup.php | 2 - datamodels/2.x/itop-backup/check-backup.php | 2 - setup/setuputils.class.inc.php | 74 --------------------- synchro/synchro_exec.php | 1 - synchro/synchro_import.php | 1 - webservices/cron.php | 9 +-- webservices/export-v2.php | 18 ++--- webservices/export.php | 19 ++---- webservices/import.php | 1 - 10 files changed, 22 insertions(+), 123 deletions(-) diff --git a/application/clipage.class.inc.php b/application/clipage.class.inc.php index db348322f..25153d17e 100644 --- a/application/clipage.class.inc.php +++ b/application/clipage.class.inc.php @@ -29,13 +29,9 @@ require_once(APPROOT."/application/webpage.class.inc.php"); class CLIPage implements Page { - /** @var string */ - public $s_title; - - function __construct($s_title) + function __construct($s_title) { - $this->s_title = $s_title; - } + } public function output() { @@ -52,22 +48,22 @@ class CLIPage implements Page public function add($sText) { echo $sText; - } + } public function p($sText) { echo $sText."\n"; - } + } public function pre($sText) { echo $sText."\n"; - } + } public function add_comment($sText) { echo "#".$sText."\n"; - } + } public function table($aConfig, $aData, $aParams = array()) { @@ -97,3 +93,5 @@ class CLIPage implements Page } } } + +?> diff --git a/datamodels/2.x/itop-backup/backup.php b/datamodels/2.x/itop-backup/backup.php index 4b93379ef..116f17564 100644 --- a/datamodels/2.x/itop-backup/backup.php +++ b/datamodels/2.x/itop-backup/backup.php @@ -134,8 +134,6 @@ set_time_limit(0); if (utils::IsModeCLI()) { $oP = new CLIPage("iTop - Database Backup"); - - SetupUtils::CheckPhpAndExtensionsForCli($oP); } else { diff --git a/datamodels/2.x/itop-backup/check-backup.php b/datamodels/2.x/itop-backup/check-backup.php index 4f17b32e1..4f8a5cfed 100644 --- a/datamodels/2.x/itop-backup/check-backup.php +++ b/datamodels/2.x/itop-backup/check-backup.php @@ -250,8 +250,6 @@ catch(Exception $e) if (utils::IsModeCLI()) { - SetupUtils::CheckPhpAndExtensionsForCli(new CLIPage('Check backup utility')); - echo date('Y-m-d H:i:s')." - running check-backup utility\n"; try { diff --git a/setup/setuputils.class.inc.php b/setup/setuputils.class.inc.php index c34921241..2b260890c 100644 --- a/setup/setuputils.class.inc.php +++ b/setup/setuputils.class.inc.php @@ -39,49 +39,6 @@ class CheckResult $this->sLabel = $sLabel; $this->sDescription = $sDescription; } - - /** - * @return string - * @since 2.7.1 2.8.0 N°2214 - */ - public function __toString() - { - $sPrintDesc = (empty($this->sDescription)) ? '' : " ({$this->sDescription})"; - return "{$this->sLabel}$sPrintDesc"; - } - - /** - * @param \CheckResult[] $aResults - * - * @return \CheckResult[] only elements that are error (iSeverity===ERROR) - * - * @since 2.7.1 2.8.0 N°2214 - */ - public static function KeepOnlyErrors($aResults) - { - return array_filter($aResults, - static function ($v) - { - if ($v->iSeverity === CheckResult::ERROR) { - return $v; - } - }, - ARRAY_FILTER_USE_BOTH); - } - - /** - * @param \CheckResult[] $aResults - * @return string[] - * @uses \CheckResult::__toString - * - * @since 2.7.1 2.8.0 N°2214 - */ - public static function FromObjetsToStrings($aResults) - { - return array_map(function($value) { - return $value->__toString(); - }, $aResults); - } } /** @@ -413,37 +370,6 @@ class SetupUtils return $aResult; } - /** - * @param \CLIPage $oCliPage - * @param int $iExitCode - * - * @since 2.7.1 2.8.0 N°2214 - */ - public static function CheckPhpAndExtensionsForCli($oCliPage, $iExitCode = -1) - { - $aPhpCheckResults = self::CheckPhpAndExtensions(); - $aPhpCheckErrors = CheckResult::KeepOnlyErrors($aPhpCheckResults); - if (empty($aPhpCheckErrors)) - { - return; - } - - $sMessageTitle = 'Error: PHP minimum requirements are not met !'; - $oCliPage->p($sMessageTitle); - $aPhpCheckErrorsForPrint = CheckResult::FromObjetsToStrings($aPhpCheckErrors); - foreach ($aPhpCheckErrorsForPrint as $sError) - { - $oCliPage->p(' * '.$sError); - } - $oCliPage->output(); - - // some CLI scripts are launched automatically - // we need a log so that we don't miss errors after migration ! - IssueLog::Error($oCliPage->s_title.' '.$sMessageTitle, 'CLI', $aPhpCheckErrorsForPrint); - - exit($iExitCode); - } - /** * @param CheckResult[] $aResult checks log */ diff --git a/synchro/synchro_exec.php b/synchro/synchro_exec.php index df22e9219..d212ca776 100644 --- a/synchro/synchro_exec.php +++ b/synchro/synchro_exec.php @@ -72,7 +72,6 @@ function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter') if (utils::IsModeCLI()) { $oP = new CLIPage(Dict::S("TitleSynchroExecution")); - SetupUtils::CheckPhpAndExtensionsForCli($oP, -2); } else { diff --git a/synchro/synchro_import.php b/synchro/synchro_import.php index 84b905fc0..0c065d03a 100644 --- a/synchro/synchro_import.php +++ b/synchro/synchro_import.php @@ -255,7 +255,6 @@ class CLILikeWebPage extends WebPage if (utils::IsModeCLI()) { $oP = new CLIPage(Dict::S('TitleSynchroExecution')); - SetupUtils::CheckPhpAndExtensionsForCli($oP, -2); } else { diff --git a/webservices/cron.php b/webservices/cron.php index b7717cede..145c758fe 100644 --- a/webservices/cron.php +++ b/webservices/cron.php @@ -60,7 +60,7 @@ function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter') function UsageAndExit($oP) { - $bModeCLI = ($oP instanceof CLIPage); + $bModeCLI = utils::IsModeCLI(); if ($bModeCLI) { @@ -477,12 +477,9 @@ function ReorderProcesses(&$aProcesses, $aTasks, $oNow, $bVerbose, &$oP) set_time_limit(0); // Some background actions may really take long to finish (like backup) -$bIsModeCLI = utils::IsModeCLI(); -if ($bIsModeCLI) +if (utils::IsModeCLI()) { $oP = new CLIPage("iTop - cron"); - - SetupUtils::CheckPhpAndExtensionsForCli($oP, EXIT_CODE_FATAL); } else { @@ -500,7 +497,7 @@ catch (Exception $e) exit(EXIT_CODE_FATAL); } -if ($bIsModeCLI) +if (utils::IsModeCLI()) { // Next steps: // specific arguments: 'csvfile' diff --git a/webservices/export-v2.php b/webservices/export-v2.php index 8b193fc9a..1fd7d6f01 100644 --- a/webservices/export-v2.php +++ b/webservices/export-v2.php @@ -32,12 +32,6 @@ require_once(APPROOT.'/core/bulkexport.class.inc.php'); require_once(APPROOT.'/application/startup.inc.php'); - -const EXIT_CODE_ERROR = -1; -const EXIT_CODE_FATAL = -2; - - - function ReportErrorAndExit($sErrorMessage) { if (utils::IsModeCLI()) @@ -45,14 +39,14 @@ function ReportErrorAndExit($sErrorMessage) $oP = new CLIPage("iTop - Export"); $oP->p('ERROR: '.$sErrorMessage); $oP->output(); - exit(EXIT_CODE_ERROR); + exit(-1); } else { $oP = new WebPage("iTop - Export"); $oP->p('ERROR: '.$sErrorMessage); $oP->output(); - exit(EXIT_CODE_ERROR); + exit(-1); } } @@ -64,7 +58,7 @@ function ReportErrorAndUsage($sErrorMessage) $oP->p('ERROR: '.$sErrorMessage); Usage($oP); $oP->output(); - exit(EXIT_CODE_ERROR); + exit(-1); } else { @@ -72,7 +66,7 @@ function ReportErrorAndUsage($sErrorMessage) $oP->p('ERROR: '.$sErrorMessage); Usage($oP); $oP->output(); - exit(EXIT_CODE_ERROR); + exit(-1); } } @@ -571,8 +565,6 @@ function DoExport(WebPage $oP, BulkExport $oExporter, $bInteractive = false) ///////////////////////////////////////////////////////////////////////////// if (utils::IsModeCLI()) { - SetupUtils::CheckPhpAndExtensionsForCli(new CLIPage('iTop - Export')); - try { // Do this before loging, in order to allow setting user credentials from within the file @@ -581,7 +573,7 @@ if (utils::IsModeCLI()) catch(Exception $e) { echo "Error: ".$e->GetMessage()."
\n"; - exit(EXIT_CODE_FATAL); + exit(-2); } $sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data'); diff --git a/webservices/export.php b/webservices/export.php index f19d03eaf..ee127ca32 100644 --- a/webservices/export.php +++ b/webservices/export.php @@ -29,11 +29,6 @@ require_once(APPROOT.'/application/excelexporter.class.inc.php'); require_once(APPROOT.'/application/startup.inc.php'); - -const EXIT_CODE_ERROR = -1; -const EXIT_CODE_FATAL = -2; - - try { // Do this before loging, in order to allow setting user credentials from within the file @@ -42,15 +37,12 @@ try catch(Exception $e) { echo "Error: ".$e->GetMessage()."
\n"; - exit(EXIT_CODE_FATAL); + exit -2; } if (utils::IsModeCLI()) { - $oP = new CLIPage("iTop - Export"); - SetupUtils::CheckPhpAndExtensionsForCli($oP, EXIT_CODE_FATAL); - - $sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data'); + $sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data'); $sAuthPwd = utils::ReadParam('auth_pwd', null, true /* Allow CLI */, 'raw_data'); if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) @@ -59,9 +51,10 @@ if (utils::IsModeCLI()) } else { - $oP->p("Access restricted or wrong credentials ('$sAuthUser')"); + $oP = new CLIPage("iTop - Export"); + $oP->p("Access restricted or wrong credentials ('$sAuthUser')"); $oP->output(); - exit(EXIT_CODE_ERROR); + exit -1; } } else @@ -81,7 +74,7 @@ if (utils::IsArchiveMode() && !UserRights::CanBrowseArchive()) $oP = new CLIPage("iTop - Export"); $oP->p("The user account is not authorized to access the archives"); $oP->output(); - exit(EXIT_CODE_ERROR); + exit -1; } $bLocalize = (utils::ReadParam('no_localize', 0) != 1); diff --git a/webservices/import.php b/webservices/import.php index 7907bcfcd..c43071822 100644 --- a/webservices/import.php +++ b/webservices/import.php @@ -212,7 +212,6 @@ function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter) if (utils::IsModeCLI()) { $oP = new CLIPage("iTop - Bulk import"); - SetupUtils::CheckPhpAndExtensionsForCli($oP, -2); } else {