From 3d299f015b48fb2362c00a3cb82f4a2b7e9582ed Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Fri, 22 Oct 2010 22:22:13 +0000 Subject: [PATCH] #246 Implemented the CLI mode for import.php SVN:trunk[920] --- application/clipage.class.inc.php | 83 ++++++++++++++++++++++++++++++ application/utils.inc.php | 7 +-- webservices/import.php | 84 ++++++++++++++++++++++++------- 3 files changed, 154 insertions(+), 20 deletions(-) create mode 100644 application/clipage.class.inc.php diff --git a/application/clipage.class.inc.php b/application/clipage.class.inc.php new file mode 100644 index 000000000..2514a391f --- /dev/null +++ b/application/clipage.class.inc.php @@ -0,0 +1,83 @@ + + * @author Romain Quetiez + * @author Denis Flaven + * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL + */ + +require_once("../application/webpage.class.inc.php"); + +class CLIPage +{ + function __construct($s_title) + { + } + + public function output() + { + } + + public function add($sText) + { + echo $sText; + } + + public function p($sText) + { + echo $sText."\n"; + } + + public function add_comment($sText) + { + echo "#".$sText."\n"; + } + + public function table($aConfig, $aData, $aParams = array()) + { + $aCells = array(); + foreach($aConfig as $sName=>$aDef) + { + if (strlen($aDef['description']) > 0) + { + $aCells[] = $aDef['label'].' ('.$aDef['description'].')'; + } + else + { + $aCells[] = $aDef['label']; + } + } + echo implode(';', $aCells)."\n"; + + foreach($aData as $aRow) + { + $aCells = array(); + foreach($aConfig as $sName=>$aAttribs) + { + $sValue = $aRow["$sName"]; + $aCells[] = $sValue; + } + echo implode(';', $aCells)."\n"; + } + } +} + +?> diff --git a/application/utils.inc.php b/application/utils.inc.php index ced553213..1f2a02388 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -231,7 +231,7 @@ class utils static public function GetAbsoluteUrl($bQueryString = true, $bForceHTTPS = false) { // Build an absolute URL to this page on this server/port - $sServerName = $_SERVER['SERVER_NAME']; + $sServerName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; if (self::GetConfig()->GetSecureConnectionRequired() || self::GetConfig()->GetHttpsHyperlinks()) { // If a secure connection is required, or if the URL is requested to start with HTTPS @@ -246,13 +246,14 @@ class utils else { $sProtocol = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!="off")) ? 'https' : 'http'; + $iPort = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80; if ($sProtocol == 'http') { - $sPort = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT']; + $sPort = ($iPort == 80) ? '' : ':'.$iPort; } else { - $sPort = ($_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT']; + $sPort = ($iPort == 443) ? '' : ':'.$iPort; } } // $_SERVER['REQUEST_URI'] is empty when running on IIS diff --git a/webservices/import.php b/webservices/import.php index 9b1009a85..c95af98e2 100644 --- a/webservices/import.php +++ b/webservices/import.php @@ -36,7 +36,7 @@ require_once('../application/application.inc.php'); require_once('../application/webpage.class.inc.php'); require_once('../application/csvpage.class.inc.php'); -require_once('../application/xmlpage.class.inc.php'); +require_once('../application/clipage.class.inc.php'); require_once('../application/startup.inc.php'); @@ -46,45 +46,66 @@ class BulkLoadException extends Exception $aPageParams = array ( + 'auth_user' => array + ( + 'mandatory' => true, + 'modes' => 'cli', + 'default' => null, + 'description' => 'login (must have enough rights to create objects of the given class)', + ), + 'auth_pwd' => array + ( + 'mandatory' => true, + 'modes' => 'cli', + 'default' => null, + 'description' => 'password', + ), 'class' => array ( 'mandatory' => true, + 'modes' => 'http,cli', 'default' => null, 'description' => 'class of loaded objects', ), 'csvdata' => array ( 'mandatory' => true, + 'modes' => 'http', 'default' => null, 'description' => 'data', ), + 'csvfile' => array + ( + 'mandatory' => true, + 'modes' => 'cli', + 'default' => '', + 'description' => 'local data file, replaces csvdata if specified', + ), 'charset' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => 'UTF-8', 'description' => 'Character set encoding of the CSV data: UTF-8, ISO-8859-1, WINDOWS-1251, WINDOWS-1252, ISO-8859-15', ), -// 'csvfile' => array -// ( -// 'mandatory' => false, -// 'default' => '', -// 'description' => 'local data file, replaces csvdata if specified', -// ), 'separator' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => ';', 'description' => 'column separator in CSV data', ), 'qualifier' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => '"', 'description' => 'test qualifier in CSV data', ), 'output' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => 'summary', 'description' => '[retcode] to return the count of lines in error, [summary] to return a concise report, [details] to get a detailed report (each line listed)', ), @@ -92,6 +113,7 @@ $aPageParams = array 'reportlevel' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => 'errors|warnings|created|changed|unchanged', 'description' => 'combination of flags to limit the detailed output', ), @@ -99,18 +121,21 @@ $aPageParams = array 'reconciliationkeys' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => '', 'description' => 'name of the columns used to identify existing objects and update them, or create a new one', ), 'simulate' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => '0', 'description' => 'If set to 1, then the load will not be executed, but the expected report will be produced', ), 'comment' => array ( 'mandatory' => false, + 'modes' => 'http,cli', 'default' => '', 'description' => 'Comment to be added into the change log', ), @@ -119,11 +144,28 @@ $aPageParams = array function UsageAndExit($oP) { global $aPageParams; + $bModeCLI = utils::IsModeCLI(); + $oP->p("USAGE:\n"); foreach($aPageParams as $sParam => $aParamData) { - $sDesc = $aParamData['description'].', '.($aParamData['mandatory'] ? 'mandatory' : 'optional, defaults to ['.$aParamData['default'].']'); - $oP->p("$sParam = $sDesc\n"); + $aModes = explode(',', $aParamData['modes']); + if ($bModeCLI) + { + if (in_array('cli', $aModes)) + { + $sDesc = $aParamData['description'].', '.($aParamData['mandatory'] ? 'mandatory' : 'optional, defaults to ['.$aParamData['default'].']'); + $oP->p("$sParam = $sDesc"); + } + } + else + { + if (in_array('http', $aModes)) + { + $sDesc = $aParamData['description'].', '.($aParamData['mandatory'] ? 'mandatory' : 'optional, defaults to ['.$aParamData['default'].']'); + $oP->p("$sParam = $sDesc"); + } + } } $oP->output(); exit; @@ -157,16 +199,16 @@ function ReadMandatoryParam($oP, $sParam) ///////////////////////////////// // Main program -if (false && utils::IsModeCLI()) +if (utils::IsModeCLI()) { - // Mode CLI under construction... sorry! + $oP = new CLIPage("iTop - Bulk import"); + // Next steps: - // implement a new page: CLIPage, that does a very clean output - // branch if in CLI mode - // enable the new argument 'csvfile' + // specific arguments: 'csvfile' + // $sAuthUser = ReadMandatoryParam($oP, 'auth_user'); - $sAuthPwd = ReadMandatoryParam($oP, 'auth_user'); - $sCsvFile = ReadMandatoryParam($oP, 'csv_file'); + $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd'); + $sCsvFile = ReadMandatoryParam($oP, 'csvfile'); if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) { UserRights::Login($sAuthUser); // Login & set the user's language @@ -176,6 +218,14 @@ if (false && utils::IsModeCLI()) $oP->p("Access restricted or wrong credentials ('$sAuthUser')"); exit; } + + if (!is_readable($sCsvFile)) + { + $oP->p("Input file could not be found or could not be read: '$sCsvFile'"); + exit; + } + $sCSVData = file_get_contents($sCsvFile); + } else { @@ -184,6 +234,7 @@ else LoginWebPage::DoLogin(); // Check user rights and prompt if needed $oP = new CSVPage("iTop - Bulk import"); + $sCSVData = utils::ReadPostedParam('csvdata'); } @@ -196,7 +247,6 @@ try $sClass = ReadMandatoryParam($oP, 'class'); $sSep = ReadParam($oP, 'separator'); $sQualifier = ReadParam($oP, 'qualifier'); - $sCSVData = utils::ReadPostedParam('csvdata'); $sCharSet = ReadParam($oP, 'charset'); $sOutput = ReadParam($oP, 'output'); // $sReportLevel = ReadParam($oP, 'reportlevel');