28 require_once
'SpdxTwoImportSource.php';
29 require_once
'XmlImportSource.php';
30 require_once
'ReportImportSink.php';
31 require_once
'ReportImportHelper.php';
32 require_once
'ReportImportConfiguration.php';
36 require_once
'version.php';
37 require_once
'services.php';
41 const REPORT_KEY =
"report";
42 const ACLA_KEY =
"addConcludedAsDecisions";
49 private $permissionDao;
53 protected $licenseDao;
55 protected $clearingDao;
57 private $copyrightDao;
61 function __construct()
63 parent::__construct(AGENT_REPORTIMPORT_NAME, AGENT_REPORTIMPORT_VERSION, AGENT_REPORTIMPORT_REV);
64 $this->uploadDao = $this->container->get(
'dao.upload');
65 $this->permissionDao = $this->container->get(
'dao.upload.permission');
66 $this->
dbManager = $this->container->get(
'db.manager');
67 $this->userDao = $this->container->get(
'dao.user');
68 $this->licenseDao = $this->container->get(
'dao.license');
69 $this->clearingDao = $this->container->get(
'dao.clearing');
70 $this->copyrightDao = $this->container->get(
'dao.copyright');
71 $this->agentSpecifLongOptions[] = self::REPORT_KEY.
':';
72 $this->agentSpecifLongOptions[] = self::ACLA_KEY.
':';
77 private function setAgent_PK()
81 "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
82 array(AGENT_REPORTIMPORT_NAME), __METHOD__.
"select" 87 throw new \Exception(
"agent_pk could not be determined");
89 $this->
agent_pk = intval($row[
'agent_pk']);
100 if (is_array(
$args) &&
101 array_key_exists($longArgsKey,
$args)){
102 echo
"DEBUG: unrefined \$longArgs are: ".$args[$longArgsKey].
"\n";
103 $chunks = explode(
" --",
$args[$longArgsKey]);
104 if(
sizeof($chunks) > 1)
106 $args[$longArgsKey] = $chunks[0];
107 foreach(array_slice($chunks, 1) as $chunk)
109 if (strpos($chunk,
'=') !==
false)
111 list($key, $value) = explode(
'=', $chunk, 2);
112 $args[$key] = $value;
116 $args[$chunk] =
true;
123 function processUploadId($uploadId)
127 self::preWorkOnArgsFlp($this->args, self::REPORT_KEY);
128 if (!$this->permissionDao->isEditable($uploadId, $this->groupId)) {
132 $reportPre = array_key_exists(self::REPORT_KEY,$this->args) ? $this->args[self::REPORT_KEY] :
"";
134 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/ReportImport/";
135 $report = $fileBase.$reportPre;
136 if(empty($reportPre) || !is_readable($report))
138 echo
"No report was uploaded\n";
139 echo
"Maybe the permissions on ".htmlspecialchars($fileBase).
" are not sufficient\n";
143 $this->
dbManager->insertTableRow(
'reportgen',
144 array(
'upload_fk'=>$uploadId,
'job_fk'=>$this->
jobId,
'filepath'=>$report),
145 __METHOD__.
'addToReportgen');
149 $this->walkAllFiles($report, $uploadId, $configuration);
154 private function getItemTreeBounds($upload_pk)
158 $uploadtreeRec = $this->
dbManager->getSingleRow(
159 'SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
161 __METHOD__.
'.find.uploadtree.to.use.in.browse.link');
162 $uploadtree_pk = $uploadtreeRec[
'uploadtree_pk'];
164 return $this->uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
167 static private function getEntries($fileId, $fileName, &$pfilePerFileName, &$hashMap=NULL, &$pfilesPerHash=NULL, $hashAlgo=
"sha1")
169 $pfilesByFilename = self::getEntriesForFilename($fileName, $pfilePerFileName);
171 if (($pfilesByFilename !== null ||
sizeof($pfilesByFilename) === 0))
173 if ( $hashMap !== null &&
sizeof($hashMap) > 0 )
176 foreach ($pfilesByFilename as $pfile)
178 if (strtolower($pfile[$hashAlgo]) !== strtolower($hashMap[$hashAlgo]))
180 print
"INFO: the file with fileName=[$fileName] does not match the hash of pfile_pk=[" . $pfile[
'pfile_pk'] .
"] and uploadtree_pk=[" . $pfile[
'uploadtree_pk'] .
"]\n";
191 return $pfilesByFilename;
195 if ($pfilesPerHash !== null &&
sizeof($pfilesPerHash) > 0 &&
196 $hashMap !== null &&
sizeof($hashMap) > 0 )
198 return self::getEntriesForHash($hashMap, $pfilesPerHash,
'sha1');
204 static private function getEntriesForFilename($filename, &$pfilesPerFileName)
206 if(array_key_exists($filename, $pfilesPerFileName))
208 return array($pfilesPerFileName[$filename]);
210 $length = strlen($filename);
213 foreach(array_keys($pfilesPerFileName) as $key)
215 if(substr($key, -$length) === $filename)
217 return array($pfilesPerFileName[$key]);
224 static private function getEntriesForHash(&$hashMap, &$pfilesPerHash, $hashAlgo)
226 if(!array_key_exists($hashAlgo, $hashMap))
231 $hash = strtolower($hashMap[$hashAlgo]);
232 if(!array_key_exists($hash, $pfilesPerHash))
236 return $pfilesPerHash[$hash];
247 if(substr($reportFilename, -4) ===
".xml")
250 if($importSource->parse())
252 return $importSource;
256 if(substr($reportFilename, -4) ===
".rdf")
259 if($importSource->parse())
261 return $importSource;
265 error_log(
"ERROR: can not handle report");
266 throw new \Exception(
"unsupported report type with filename: $reportFilename");
269 public function walkAllFiles($reportFilename, $upload_pk, $configuration)
279 $sink =
new ReportImportSink($this->
agent_pk, $this->userDao, $this->licenseDao, $this->clearingDao, $this->copyrightDao,
280 $this->
dbManager, $this->groupId, $this->userId, $this->
jobId, $configuration);
283 $itemTreeBounds = $this->getItemTreeBounds($upload_pk);
284 $pfilePerFileName = $this->uploadDao->getPFileDataPerFileName($itemTreeBounds);
285 $pfilesPerHash = $this->uploadDao->getPFilesDataPerHashAlgo($itemTreeBounds,
'sha1');
287 foreach ($source->getAllFiles() as $fileId => $fileName)
290 if ($pfilesPerHash !== NULL &&
sizeof($pfilesPerHash) > 0)
292 $hashMap = $source->getHashesMap($fileId);
295 $pfiles = self::getEntries($fileId,
296 $fileName, $pfilePerFileName,
297 $hashMap, $pfilesPerHash,
'sha1');
299 if ($pfiles === null ||
sizeof($pfiles) === 0)
301 print
"WARN: no match for fileId=[".$fileId.
"] with filename=[".$fileName.
"]\n";
307 $data = $source->getDataForFile($fileId)
308 ->setPfiles($pfiles);
309 $sink->handleData($data);
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
heartbeat($newProcessed)
Send hear beat to the scheduler.
Structure of an Agent with all required parameters.
int jobId
The id of the job.
static preWorkOnArgsFlp(&$args, $longArgsKey)
fo_dbManager * dbManager
fo_dbManager object
getImportSource($reportFilename)