1<?php
2// Copyright (C) 2010-2012 Combodo SARL
3//
4//   This file is part of iTop.
5//
6//   iTop is free software; you can redistribute it and/or modify
7//   it under the terms of the GNU Affero General Public License as published by
8//   the Free Software Foundation, either version 3 of the License, or
9//   (at your option) any later version.
10//
11//   iTop is distributed in the hope that it will be useful,
12//   but WITHOUT ANY WARRANTY; without even the implied warranty of
13//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14//   GNU Affero General Public License for more details.
15//
16//   You should have received a copy of the GNU Affero General Public License
17//   along with iTop. If not, see <http://www.gnu.org/licenses/>
18
19/**
20 * Internal: synchronize part of the records - cannot be invoked separately
21 *
22 * @copyright   Copyright (C) 2010-2012 Combodo SARL
23 * @license     http://opensource.org/licenses/AGPL-3.0
24 */
25
26if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
27require_once(__DIR__.'/../approot.inc.php');
28require_once(APPROOT.'/application/application.inc.php');
29require_once(APPROOT.'/application/webpage.class.inc.php');
30require_once(APPROOT.'/application/csvpage.class.inc.php');
31require_once(APPROOT.'/application/clipage.class.inc.php');
32
33require_once(APPROOT.'/application/startup.inc.php');
34
35
36function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
37{
38	$sValue = utils::ReadParam($sParam, null, true /* Allow CLI */, $sSanitizationFilter);
39	if (is_null($sValue))
40	{
41		$oP->p("ERROR: Missing argument '$sParam'\n");
42		exit(29);
43	}
44	return trim($sValue);
45}
46
47/////////////////////////////////
48// Main program
49
50if (!utils::IsModeCLI())
51{
52	$oP = new WebPage(Dict::S("TitleSynchroExecution"));
53	$oP->p("This page is used internally by iTop");
54	$oP->output();
55	exit -2;
56}
57
58$oP = new CLIPage(Dict::S("TitleSynchroExecution"));
59
60try
61{
62	utils::UseParamFile();
63}
64catch(Exception $e)
65{
66	$oP->p("Error: ".$e->GetMessage());
67	$oP->output();
68	exit -2;
69}
70
71// Next steps:
72//   specific arguments: 'csvfile'
73//
74$sAuthUser = ReadMandatoryParam($oP, 'auth_user', 'raw_data');
75$sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd', 'raw_data');
76if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
77{
78	UserRights::Login($sAuthUser); // Login & set the user's language
79}
80else
81{
82	$oP->p("Access restricted or wrong credentials ('$sAuthUser')");
83	$oP->output();
84	exit -1;
85}
86
87$iStepCount = ReadMandatoryParam($oP, 'step_count');
88$oP->p('Executing a partial synchro - step '.$iStepCount);
89
90$iSource = ReadMandatoryParam($oP, 'source');
91
92$iStatLog = ReadMandatoryParam($oP, 'log');
93$iChange = ReadMandatoryParam($oP, 'change');
94$sLastFullLoad = ReadMandatoryParam($oP, 'last_full_load', 'raw_data');
95$iChunkSize = ReadMandatoryParam($oP, 'chunk');
96
97$oP->p('Last full load: '.$sLastFullLoad);
98$oP->p('Chunk size: '.$iChunkSize);
99$oP->p('Source: '.$iSource);
100
101try
102{
103	$oSynchroDataSource = MetaModel::GetObject('SynchroDataSource', $iSource);
104	$oLog = MetaModel::GetObject('SynchroLog', $iStatLog);
105	$oChange = MetaModel::GetObject('CMDBChange', $iChange);
106
107	if (strlen($sLastFullLoad) > 0)
108	{
109		$oLastFullLoad = new DateTime($sLastFullLoad);
110		$oSynchroExec = new SynchroExecution($oSynchroDataSource, $oLastFullLoad);
111	}
112	else
113	{
114		$oSynchroExec = new SynchroExecution($oSynchroDataSource);
115	}
116	if ($oSynchroExec->DoSynchronizeChunk($oLog, $oChange, $iChunkSize))
117	{
118		// The last line MUST follow this convention
119		$oP->p("continue");
120	}
121	else
122	{
123		// The last line MUST follow this convention
124		$oP->p("finished");
125	}
126	$oP->output();
127}
128catch(Exception $e)
129{
130	$oP->p("Error: ".$e->GetMessage());
131	$oP->add($e->getTraceAsString());
132	$oP->output();
133	exit(28);
134}
135?>
136