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";
42 protected $licenseDao;
44 protected $clearingDao;
46 private $copyrightDao;
50 function __construct()
52 parent::__construct(AGENT_REPORTIMPORT_NAME, AGENT_REPORTIMPORT_VERSION, AGENT_REPORTIMPORT_REV);
53 $this->uploadDao = $this->container->get(
'dao.upload');
54 $this->
dbManager = $this->container->get(
'db.manager');
55 $this->userDao = $this->container->get(
'dao.user');
56 $this->licenseDao = $this->container->get(
'dao.license');
57 $this->clearingDao = $this->container->get(
'dao.clearing');
58 $this->copyrightDao = $this->container->get(
'dao.copyright');
59 $this->agentSpecifLongOptions[] = self::REPORT_KEY.
':';
60 $this->agentSpecifLongOptions[] = self::ACLA_KEY.
':';
72 "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
73 array(AGENT_REPORTIMPORT_NAME), __METHOD__.
"select"
77 throw new \Exception(
"agent_pk could not be determined");
79 $this->
agent_pk = intval($row[
'agent_pk']);
90 if (is_array(
$args) &&
91 array_key_exists($longArgsKey,
$args)){
92 echo
"DEBUG: unrefined \$longArgs are: ".$args[$longArgsKey].
"\n";
93 $chunks = explode(
" --",
$args[$longArgsKey]);
94 if(
sizeof($chunks) > 1)
96 $args[$longArgsKey] = $chunks[0];
97 foreach(array_slice($chunks, 1) as $chunk)
99 if (strpos($chunk,
'=') !==
false)
101 list($key, $value) = explode(
'=', $chunk, 2);
102 $args[$key] = $value;
106 $args[$chunk] =
true;
119 $reportPre = array_key_exists(self::REPORT_KEY,$this->args) ? $this->args[self::REPORT_KEY] :
"";
121 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'] .
"/ReportImport/";
122 $report = $fileBase . $reportPre;
123 if(empty($reportPre) || !is_readable($report)) {
124 echo
"No report was uploaded\n";
125 echo
"Maybe the permissions on " . htmlspecialchars($fileBase) .
" are not sufficient\n";
129 $this->
dbManager->insertTableRow(
'reportgen',
130 array(
'upload_fk'=>$uploadId,
'job_fk'=>$this->
jobId,
'filepath'=>$report),
131 __METHOD__.
'addToReportgen');
135 $this->walkAllFiles($report, $uploadId, $configuration);
140 private function getItemTreeBounds($upload_pk)
144 $uploadtreeRec = $this->
dbManager->getSingleRow(
145 'SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
147 __METHOD__.
'.find.uploadtree.to.use.in.browse.link');
148 $uploadtree_pk = $uploadtreeRec[
'uploadtree_pk'];
150 return $this->uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
153 static private function getEntries($fileId, $fileName, &$pfilePerFileName, &$hashMap=NULL, &$pfilesPerHash=NULL, $hashAlgo=
"sha1")
155 $pfilesByFilename = self::getEntriesForFilename($fileName, $pfilePerFileName);
157 if (($pfilesByFilename !==
null &&
sizeof($pfilesByFilename) > 0))
159 if ( $hashMap !==
null &&
sizeof($hashMap) > 0 )
162 foreach ($pfilesByFilename as $pfile)
164 if (strtolower($pfile[$hashAlgo]) !== strtolower($hashMap[$hashAlgo]))
166 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";
177 return $pfilesByFilename;
181 if ($pfilesPerHash !==
null &&
sizeof($pfilesPerHash) > 0 &&
182 $hashMap !==
null &&
sizeof($hashMap) > 0 )
184 return self::getEntriesForHash($hashMap, $pfilesPerHash,
'sha1');
190 static private function getEntriesForFilename($filename, &$pfilesPerFileName)
192 if(array_key_exists($filename, $pfilesPerFileName))
194 return array($pfilesPerFileName[$filename]);
196 # Allow matching "./README.MD" with "pack.tar.gz/pack.tar/README.MD" by
197 # matching "/README.MD" with "/README.MD".
198 $length = strlen($filename) - 1;
199 $fileWithoutDot = substr($filename, -$length);
202 foreach(array_keys($pfilesPerFileName) as $key)
204 if(substr($key, -$length) === $fileWithoutDot)
206 return array($pfilesPerFileName[$key]);
213 static private function getEntriesForHash(&$hashMap, &$pfilesPerHash, $hashAlgo)
215 if(!array_key_exists($hashAlgo, $hashMap))
220 $hash = strtolower($hashMap[$hashAlgo]);
221 if(!array_key_exists($hash, $pfilesPerHash))
225 return $pfilesPerHash[$hash];
244 $version = $parse->getVersion();
245 if($version ==
"2.2" || $version ==
"2.3"){
247 if($importSource->parse()) {
248 return $importSource;
253 if($importSource->parse()) {
254 return $importSource;
261 if($importSource->parse()) {
262 return $importSource;
266 error_log(
"ERROR: can not handle report");
267 throw new \Exception(
"unsupported report type with filename: $reportFilename");
273 public function walkAllFiles($reportFilename, $upload_pk, $configuration)
277 if ($source === NULL) {
282 $sink =
new ReportImportSink($this->
agent_pk, $this->userDao,
283 $this->licenseDao, $this->clearingDao, $this->copyrightDao,
288 $itemTreeBounds = $this->uploadDao->getParentItemBounds($upload_pk);
289 $pfilePerFileName = $this->uploadDao->getPFileDataPerFileName($itemTreeBounds);
290 $pfilesPerHash = $this->uploadDao->getPFilesDataPerHashAlgo($itemTreeBounds,
'sha1');
292 foreach ($source->getAllFiles() as $fileId => $fileName)
295 if ($pfilesPerHash !== NULL &&
sizeof($pfilesPerHash) > 0)
297 $hashMap = $source->getHashesMap($fileId);
300 $pfiles = self::getEntries($fileId,
301 $fileName, $pfilePerFileName,
302 $hashMap, $pfilesPerHash,
'sha1');
304 if ($pfiles ===
null ||
sizeof($pfiles) === 0) {
305 print
"WARN: no match for fileId=[".$fileId.
"] with filename=[".$fileName.
"]\n";
311 $data = $source->getDataForFile($fileId)
312 ->setPfiles($pfiles);
313 $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)
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).