7 namespace Fossology\ReportImport;
18 require_once
'SpdxTwoImportSource.php';
19 require_once
'XmlImportSource.php';
20 require_once
'ReportImportSink.php';
21 require_once
'ReportImportHelper.php';
22 require_once
'ReportImportConfiguration.php';
24 require_once
'version.php';
25 require_once
'services.php';
29 const REPORT_KEY =
"report";
30 const ACLA_KEY =
"addConcludedAsDecisions";
39 protected $licenseDao;
41 protected $clearingDao;
43 private $copyrightDao;
47 function __construct()
49 parent::__construct(AGENT_REPORTIMPORT_NAME, AGENT_REPORTIMPORT_VERSION, AGENT_REPORTIMPORT_REV);
50 $this->uploadDao = $this->container->get(
'dao.upload');
51 $this->
dbManager = $this->container->get(
'db.manager');
52 $this->userDao = $this->container->get(
'dao.user');
53 $this->licenseDao = $this->container->get(
'dao.license');
54 $this->clearingDao = $this->container->get(
'dao.clearing');
55 $this->copyrightDao = $this->container->get(
'dao.copyright');
56 $this->agentSpecifLongOptions[] = self::REPORT_KEY.
':';
57 $this->agentSpecifLongOptions[] = self::ACLA_KEY.
':';
62 private function setAgent_PK()
66 "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
67 array(AGENT_REPORTIMPORT_NAME), __METHOD__.
"select"
72 throw new \Exception(
"agent_pk could not be determined");
74 $this->
agent_pk = intval($row[
'agent_pk']);
85 if (is_array(
$args) &&
86 array_key_exists($longArgsKey,
$args)){
87 echo
"DEBUG: unrefined \$longArgs are: ".$args[$longArgsKey].
"\n";
88 $chunks = explode(
" --",
$args[$longArgsKey]);
89 if(
sizeof($chunks) > 1)
91 $args[$longArgsKey] = $chunks[0];
92 foreach(array_slice($chunks, 1) as $chunk)
94 if (strpos($chunk,
'=') !==
false)
96 list($key, $value) = explode(
'=', $chunk, 2);
101 $args[$chunk] =
true;
114 $reportPre = array_key_exists(self::REPORT_KEY,$this->args) ? $this->args[self::REPORT_KEY] :
"";
116 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'].
"/ReportImport/";
117 $report = $fileBase.$reportPre;
118 if(empty($reportPre) || !is_readable($report))
120 echo
"No report was uploaded\n";
121 echo
"Maybe the permissions on ".htmlspecialchars($fileBase).
" are not sufficient\n";
125 $this->
dbManager->insertTableRow(
'reportgen',
126 array(
'upload_fk'=>$uploadId,
'job_fk'=>$this->
jobId,
'filepath'=>$report),
127 __METHOD__.
'addToReportgen');
131 $this->walkAllFiles($report, $uploadId, $configuration);
136 private function getItemTreeBounds($upload_pk)
140 $uploadtreeRec = $this->
dbManager->getSingleRow(
141 'SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
143 __METHOD__.
'.find.uploadtree.to.use.in.browse.link');
144 $uploadtree_pk = $uploadtreeRec[
'uploadtree_pk'];
146 return $this->uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
149 static private function getEntries($fileId, $fileName, &$pfilePerFileName, &$hashMap=NULL, &$pfilesPerHash=NULL, $hashAlgo=
"sha1")
151 $pfilesByFilename = self::getEntriesForFilename($fileName, $pfilePerFileName);
153 if (($pfilesByFilename !==
null ||
sizeof($pfilesByFilename) === 0))
155 if ( $hashMap !==
null &&
sizeof($hashMap) > 0 )
158 foreach ($pfilesByFilename as $pfile)
160 if (strtolower($pfile[$hashAlgo]) !== strtolower($hashMap[$hashAlgo]))
162 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";
173 return $pfilesByFilename;
177 if ($pfilesPerHash !==
null &&
sizeof($pfilesPerHash) > 0 &&
178 $hashMap !==
null &&
sizeof($hashMap) > 0 )
180 return self::getEntriesForHash($hashMap, $pfilesPerHash,
'sha1');
186 static private function getEntriesForFilename($filename, &$pfilesPerFileName)
188 if(array_key_exists($filename, $pfilesPerFileName))
190 return array($pfilesPerFileName[$filename]);
192 $length = strlen($filename);
195 foreach(array_keys($pfilesPerFileName) as $key)
197 if(substr($key, -$length) === $filename)
199 return array($pfilesPerFileName[$key]);
206 static private function getEntriesForHash(&$hashMap, &$pfilesPerHash, $hashAlgo)
208 if(!array_key_exists($hashAlgo, $hashMap))
213 $hash = strtolower($hashMap[$hashAlgo]);
214 if(!array_key_exists($hash, $pfilesPerHash))
218 return $pfilesPerHash[$hash];
229 if(substr($reportFilename, -4) ===
".xml")
232 if($importSource->parse())
234 return $importSource;
238 if(substr($reportFilename, -4) ===
".rdf")
241 if($importSource->parse())
243 return $importSource;
247 error_log(
"ERROR: can not handle report");
248 throw new \Exception(
"unsupported report type with filename: $reportFilename");
251 public function walkAllFiles($reportFilename, $upload_pk, $configuration)
261 $sink =
new ReportImportSink($this->
agent_pk, $this->userDao, $this->licenseDao, $this->clearingDao, $this->copyrightDao,
262 $this->
dbManager, $this->groupId, $this->userId, $this->
jobId, $configuration);
265 $itemTreeBounds = $this->getItemTreeBounds($upload_pk);
266 $pfilePerFileName = $this->uploadDao->getPFileDataPerFileName($itemTreeBounds);
267 $pfilesPerHash = $this->uploadDao->getPFilesDataPerHashAlgo($itemTreeBounds,
'sha1');
269 foreach ($source->getAllFiles() as $fileId => $fileName)
272 if ($pfilesPerHash !== NULL &&
sizeof($pfilesPerHash) > 0)
274 $hashMap = $source->getHashesMap($fileId);
277 $pfiles = self::getEntries($fileId,
278 $fileName, $pfilePerFileName,
279 $hashMap, $pfilesPerHash,
'sha1');
281 if ($pfiles ===
null ||
sizeof($pfiles) === 0)
283 print
"WARN: no match for fileId=[".$fileId.
"] with filename=[".$fileName.
"]\n";
289 $data = $source->getDataForFile($fileId)
290 ->setPfiles($pfiles);
291 $sink->handleData($data);
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
getImportSource($reportFilename)
static preWorkOnArgsFlp(&$args, $longArgsKey)
processUploadId($uploadId)
Given an upload ID, process the items in it.
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".
int jobId
The id of the job.
fo_dbManager * dbManager
fo_dbManager object
list_t type structure used to keep various lists. (e.g. there are multiple lists).