瀏覽代碼

#246 Implemented the CLI mode for import.php

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@920 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 年之前
父節點
當前提交
f5a7808a01
共有 3 個文件被更改,包括 154 次插入20 次删除
  1. 83 0
      application/clipage.class.inc.php
  2. 4 3
      application/utils.inc.php
  3. 67 17
      webservices/import.php

+ 83 - 0
application/clipage.class.inc.php

@@ -0,0 +1,83 @@
+<?php
+// Copyright (C) 2010 Combodo SARL
+//
+//   This program is free software; you can redistribute it and/or modify
+//   it under the terms of the GNU General Public License as published by
+//   the Free Software Foundation; version 3 of the License.
+//
+//   This program is distributed in the hope that it will be useful,
+//   but WITHOUT ANY WARRANTY; without even the implied warranty of
+//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//   GNU General Public License for more details.
+//
+//   You should have received a copy of the GNU General Public License
+//   along with this program; if not, write to the Free Software
+//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+/**
+ * CLI page 
+ * The page adds the content-type text/XML and the encoding into the headers
+ *
+ * @author      Erwan Taloc <erwan.taloc@combodo.com>
+ * @author      Romain Quetiez <romain.quetiez@combodo.com>
+ * @author      Denis Flaven <denis.flaven@combodo.com>
+ * @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";
+		}
+	}
+}
+
+?>

+ 4 - 3
application/utils.inc.php

@@ -231,7 +231,7 @@ class utils
 	static public function GetAbsoluteUrl($bQueryString = true, $bForceHTTPS = false)
 	static public function GetAbsoluteUrl($bQueryString = true, $bForceHTTPS = false)
 	{
 	{
 		// Build an absolute URL to this page on this server/port
 		// 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 (self::GetConfig()->GetSecureConnectionRequired() || self::GetConfig()->GetHttpsHyperlinks())
 		{
 		{
 			// If a secure connection is required, or if the URL is requested to start with HTTPS
 			// If a secure connection is required, or if the URL is requested to start with HTTPS
@@ -246,13 +246,14 @@ class utils
 		else
 		else
 		{
 		{
 			$sProtocol = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!="off")) ? 'https' : 'http';
 			$sProtocol = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!="off")) ? 'https' : 'http';
+			$iPort = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
 			if ($sProtocol == 'http')
 			if ($sProtocol == 'http')
 			{
 			{
-				$sPort = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
+				$sPort = ($iPort == 80) ? '' : ':'.$iPort;
 			}
 			}
 			else
 			else
 			{
 			{
-				$sPort = ($_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT'];
+				$sPort = ($iPort == 443) ? '' : ':'.$iPort;
 			}
 			}
 		}
 		}
 		// $_SERVER['REQUEST_URI'] is empty when running on IIS
 		// $_SERVER['REQUEST_URI'] is empty when running on IIS

+ 67 - 17
webservices/import.php

@@ -36,7 +36,7 @@
 require_once('../application/application.inc.php');
 require_once('../application/application.inc.php');
 require_once('../application/webpage.class.inc.php');
 require_once('../application/webpage.class.inc.php');
 require_once('../application/csvpage.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');
 require_once('../application/startup.inc.php');
 
 
@@ -46,45 +46,66 @@ class BulkLoadException extends Exception
 
 
 $aPageParams = array
 $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
 	'class' => array
 	(
 	(
 		'mandatory' => true,
 		'mandatory' => true,
+		'modes' => 'http,cli',
 		'default' => null,
 		'default' => null,
 		'description' => 'class of loaded objects',
 		'description' => 'class of loaded objects',
 	),
 	),
 	'csvdata' => array
 	'csvdata' => array
 	(
 	(
 		'mandatory' => true,
 		'mandatory' => true,
+		'modes' => 'http',
 		'default' => null,
 		'default' => null,
 		'description' => 'data',
 		'description' => 'data',
 	),
 	),
+	'csvfile' => array
+	(
+		'mandatory' => true,
+		'modes' => 'cli',
+		'default' => '',
+		'description' => 'local data file, replaces csvdata if specified',
+	),
 	'charset' => array
 	'charset' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => 'UTF-8',
 		'default' => 'UTF-8',
 		'description' => 'Character set encoding of the CSV data: UTF-8, ISO-8859-1, WINDOWS-1251, WINDOWS-1252, ISO-8859-15',
 		'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
 	'separator' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => ';',
 		'default' => ';',
 		'description' => 'column separator in CSV data',
 		'description' => 'column separator in CSV data',
 	),
 	),
 	'qualifier' => array
 	'qualifier' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => '"',
 		'default' => '"',
 		'description' => 'test qualifier in CSV data',
 		'description' => 'test qualifier in CSV data',
 	),
 	),
 	'output' => array
 	'output' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => 'summary',
 		'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)',
 		'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
 	'reportlevel' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => 'errors|warnings|created|changed|unchanged',
 		'default' => 'errors|warnings|created|changed|unchanged',
 		'description' => 'combination of flags to limit the detailed output',
 		'description' => 'combination of flags to limit the detailed output',
 	),
 	),
@@ -99,18 +121,21 @@ $aPageParams = array
 	'reconciliationkeys' => array
 	'reconciliationkeys' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => '',
 		'default' => '',
 		'description' => 'name of the columns used to identify existing objects and update them, or create a new one',
 		'description' => 'name of the columns used to identify existing objects and update them, or create a new one',
 	),
 	),
 	'simulate' => array
 	'simulate' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => '0',
 		'default' => '0',
 		'description' => 'If set to 1, then the load will not be executed, but the expected report will be produced',
 		'description' => 'If set to 1, then the load will not be executed, but the expected report will be produced',
 	),
 	),
 	'comment' => array
 	'comment' => array
 	(
 	(
 		'mandatory' => false,
 		'mandatory' => false,
+		'modes' => 'http,cli',
 		'default' => '',
 		'default' => '',
 		'description' => 'Comment to be added into the change log',
 		'description' => 'Comment to be added into the change log',
 	),
 	),
@@ -119,11 +144,28 @@ $aPageParams = array
 function UsageAndExit($oP)
 function UsageAndExit($oP)
 {
 {
 	global $aPageParams;
 	global $aPageParams;
+	$bModeCLI = utils::IsModeCLI();
+
 	$oP->p("USAGE:\n");
 	$oP->p("USAGE:\n");
 	foreach($aPageParams as $sParam => $aParamData)
 	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();
 	$oP->output();
 	exit;
 	exit;
@@ -157,16 +199,16 @@ function ReadMandatoryParam($oP, $sParam)
 /////////////////////////////////
 /////////////////////////////////
 // Main program
 // Main program
 
 
-if (false && utils::IsModeCLI())
+if (utils::IsModeCLI())
 {
 {
-	// Mode CLI under construction... sorry!
+	$oP = new CLIPage("iTop - Bulk import");
+
 	// Next steps:
 	// 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');
 	$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))
 	if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
 	{
 	{
 		UserRights::Login($sAuthUser); // Login & set the user's language
 		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')");
 		$oP->p("Access restricted or wrong credentials ('$sAuthUser')");
 		exit;
 		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
 else
 {
 {
@@ -184,6 +234,7 @@ else
 	LoginWebPage::DoLogin(); // Check user rights and prompt if needed
 	LoginWebPage::DoLogin(); // Check user rights and prompt if needed
 
 
 	$oP = new CSVPage("iTop - Bulk import");
 	$oP = new CSVPage("iTop - Bulk import");
+	$sCSVData = utils::ReadPostedParam('csvdata');
 }
 }
 
 
 
 
@@ -196,7 +247,6 @@ try
 	$sClass = ReadMandatoryParam($oP, 'class');
 	$sClass = ReadMandatoryParam($oP, 'class');
 	$sSep = ReadParam($oP, 'separator');
 	$sSep = ReadParam($oP, 'separator');
 	$sQualifier = ReadParam($oP, 'qualifier');
 	$sQualifier = ReadParam($oP, 'qualifier');
-	$sCSVData = utils::ReadPostedParam('csvdata');
 	$sCharSet = ReadParam($oP, 'charset');
 	$sCharSet = ReadParam($oP, 'charset');
 	$sOutput = ReadParam($oP, 'output');
 	$sOutput = ReadParam($oP, 'output');
 //	$sReportLevel = ReadParam($oP, 'reportlevel');
 //	$sReportLevel = ReadParam($oP, 'reportlevel');