7 namespace Fossology\ReportImport;
20 require_once
'SpdxTwoImportSource.php';
21 require_once
'SpdxThreeImportSource.php';
22 require_once
'XmlImportSource.php';
23 require_once
'ReportImportSink.php';
24 require_once
'ReportImportHelper.php';
25 require_once
'ReportImportConfiguration.php';
27 require_once
'version.php';
28 require_once
'services.php';
32 const REPORT_KEY =
"report";
33 const ACLA_KEY =
"addConcludedAsDecisions";
34 const ACLAO_KEY =
"addConcludedAsDecisionsOverwrite";
35 const ACLATBD_KEY =
"addConcludedAsDecisionsTBD";
36 const ALIFI_KEY =
"addLicenseInfoFromInfoInFile";
37 const ALFC_KEY =
"addLicenseInfoFromConcluded";
38 const ANLA_KEY =
"addNewLicensesAs";
39 const LMATCH_KEY =
"licenseMatch";
40 const COPYRIGHTS_KEY =
"addCopyrights";
49 protected $licenseDao;
51 protected $clearingDao;
53 private $copyrightDao;
57 function __construct()
59 parent::__construct(AGENT_REPORTIMPORT_NAME, AGENT_REPORTIMPORT_VERSION, AGENT_REPORTIMPORT_REV);
60 $this->uploadDao = $this->container->get(
'dao.upload');
61 $this->
dbManager = $this->container->get(
'db.manager');
62 $this->userDao = $this->container->get(
'dao.user');
63 $this->licenseDao = $this->container->get(
'dao.license');
64 $this->clearingDao = $this->container->get(
'dao.clearing');
65 $this->copyrightDao = $this->container->get(
'dao.copyright');
67 $this->agentSpecifLongOptions[] = self::REPORT_KEY.
':';
68 $this->agentSpecifLongOptions[] = self::ACLA_KEY.
':';
69 $this->agentSpecifLongOptions[] = self::ACLAO_KEY.
':';
70 $this->agentSpecifLongOptions[] = self::ACLATBD_KEY.
':';
71 $this->agentSpecifLongOptions[] = self::ALIFI_KEY.
':';
72 $this->agentSpecifLongOptions[] = self::ALFC_KEY.
':';
73 $this->agentSpecifLongOptions[] = self::ANLA_KEY.
':';
74 $this->agentSpecifLongOptions[] = self::LMATCH_KEY.
':';
75 $this->agentSpecifLongOptions[] = self::COPYRIGHTS_KEY.
':';
87 "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
88 array(AGENT_REPORTIMPORT_NAME), __METHOD__.
"select"
92 throw new \Exception(
"agent_pk could not be determined");
94 $this->
agent_pk = intval($row[
'agent_pk']);
101 $reportPre = array_key_exists(self::REPORT_KEY,$this->args) ? $this->args[self::REPORT_KEY] :
"";
102 $reportPre =
trim($reportPre,
"\"'");
104 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'] .
"/ReportImport/";
105 $report = $fileBase . $reportPre;
106 if(empty($reportPre) || !is_readable($report)) {
107 echo
"No report was uploaded\n";
108 echo
"Maybe the permissions on " . htmlspecialchars($fileBase) .
" are not sufficient\n";
112 $this->
dbManager->insertTableRow(
'reportgen',
113 array(
'upload_fk'=>$uploadId,
'job_fk'=>$this->
jobId,
'filepath'=>$report),
114 __METHOD__.
'addToReportgen');
118 $this->walkAllFiles($report, $uploadId, $configuration);
123 private function getItemTreeBounds($upload_pk)
127 $uploadtreeRec = $this->
dbManager->getSingleRow(
128 'SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
130 __METHOD__.
'.find.uploadtree.to.use.in.browse.link');
131 $uploadtree_pk = $uploadtreeRec[
'uploadtree_pk'];
133 return $this->uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
136 static private function getEntries($fileId, $fileName, &$pfilePerFileName, &$hashMap=NULL, &$pfilesPerHash=NULL, $hashAlgo=
"sha1")
138 $pfilesByFilename = self::getEntriesForFilename($fileName, $pfilePerFileName);
140 if (($pfilesByFilename !==
null &&
sizeof($pfilesByFilename) > 0))
142 if ( $hashMap !==
null &&
sizeof($hashMap) > 0 )
145 foreach ($pfilesByFilename as $pfile)
147 if (strtolower($pfile[$hashAlgo]) !== strtolower($hashMap[$hashAlgo]))
149 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";
160 return $pfilesByFilename;
164 if ($pfilesPerHash !==
null &&
sizeof($pfilesPerHash) > 0 &&
165 $hashMap !==
null &&
sizeof($hashMap) > 0 )
167 return self::getEntriesForHash($hashMap, $pfilesPerHash,
'sha1');
173 static private function getEntriesForFilename($filename, &$pfilesPerFileName)
175 if(array_key_exists($filename, $pfilesPerFileName))
177 return array($pfilesPerFileName[$filename]);
179 # Allow matching "./README.MD" with "pack.tar.gz/pack.tar/README.MD" by
180 # matching "/README.MD" with "/README.MD".
181 $length = strlen($filename) - 1;
182 $fileWithoutDot = substr($filename, -$length);
185 foreach(array_keys($pfilesPerFileName) as $key)
187 if(substr($key, -$length) === $fileWithoutDot)
189 return array($pfilesPerFileName[$key]);
196 static private function getEntriesForHash(&$hashMap, &$pfilesPerHash, $hashAlgo)
198 if(!array_key_exists($hashAlgo, $hashMap))
203 $hash = strtolower($hashMap[$hashAlgo]);
204 if(!array_key_exists($hash, $pfilesPerHash))
208 return $pfilesPerHash[$hash];
227 $version = $parse->getVersion();
228 if($version ==
"2.2" || $version ==
"2.3"){
230 if($importSource->parse()) {
231 return $importSource;
236 if($importSource->parse()) {
237 return $importSource;
244 if($importSource->parse()) {
245 return $importSource;
249 error_log(
"ERROR: can not handle report");
250 throw new \Exception(
"unsupported report type with filename: $reportFilename");
256 public function walkAllFiles($reportFilename, $upload_pk, $configuration)
260 if ($source === NULL) {
265 $sink =
new ReportImportSink($this->
agent_pk, $this->userDao,
266 $this->licenseDao, $this->clearingDao, $this->copyrightDao,
271 $itemTreeBounds = $this->uploadDao->getParentItemBounds($upload_pk);
272 $pfilePerFileName = $this->uploadDao->getPFileDataPerFileName($itemTreeBounds);
273 $pfilesPerHash = $this->uploadDao->getPFilesDataPerHashAlgo($itemTreeBounds,
'sha1');
275 foreach ($source->getAllFiles() as $fileId => $fileName)
278 if ($pfilesPerHash !== NULL &&
sizeof($pfilesPerHash) > 0)
280 $hashMap = $source->getHashesMap($fileId);
283 $pfiles = self::getEntries($fileId,
284 $fileName, $pfilePerFileName,
285 $hashMap, $pfilesPerHash,
'sha1');
287 if ($pfiles ===
null ||
sizeof($pfiles) === 0) {
288 print
"WARN: no match for fileId=[".$fileId.
"] with filename=[".$fileName.
"]\n";
294 $data = $source->getDataForFile($fileId)
295 ->setPfiles($pfiles);
296 $sink->handleData($data);
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
static stringEndsWith($haystack, $needle)
getImportSource($reportFilename)
processUploadId($uploadId)
Given an upload ID, process the items in it.
char * trim(char *ptext)
Trimming whitespace.
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