FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ResoAgent.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2021 Orange
4  Author: Bartłomiej Dróżdż <bartlomiej.drozdz@orange.com>
5  Author: Piotr Pszczoła <piotr.pszczola@orange.com>
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
10 namespace Fossology\Reso;
11 
15 
16 
17 include_once(__DIR__ . "/version.php");
18 
25 class ResoAgent extends Agent
26 {
27 
31  const REUSE_FILE_SUFFIX = ".license";
32 
36  private $uploadDao;
37 
42  protected $agentDao;
43 
48  function __construct()
49  {
50  parent::__construct(RESO_AGENT_NAME, AGENT_VERSION, AGENT_REV);
51  $this->uploadDao = $this->container->get('dao.upload');
52  $this->agentDao = $this->container->get('dao.agent');
53  }
54 
62  function processUploadId($uploadId)
63  {
64  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
65 
66  $reSoUploadFiles = $this->findReSoUploadFiles($uploadId);
67  if (empty($reSoUploadFiles)) {
68  return true;
69  }
70  $linkedFiles = $this->associateBaseFile($reSoUploadFiles, $uploadTreeTableName);
71  $this->copyOjoFindings($linkedFiles,$uploadId);
72  $this->copyCopyrightFindings($linkedFiles,$uploadId);
73 
74  return true;
75  }
76 
82  protected function findReSoUploadFiles($uploadId)
83  {
84  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
85  $param = array();
86  $stmt = __METHOD__ .'reSo_files'.self::REUSE_FILE_SUFFIX;
87  $sql = "SELECT * FROM $uploadTreeTableName
88  WHERE upload_fk=$1 AND pfile_fk != 0 AND ufile_name like '%" . self::REUSE_FILE_SUFFIX . "' ";
89  $param[] = $uploadId;
90  $this->dbManager->prepare($stmt, $sql);
91  $res = $this->dbManager->execute($stmt,$param);
92 
93  return $this->dbManager->fetchAll($res);
94  }
95 
102  protected function associateBaseFile($reSoUploadFiles, $uploadTreeTableName)
103  {
104  $mergedArray = array();
105  foreach ($reSoUploadFiles as $row) {
106  $baseFileRow = $this->findAssociatedFile($row, $uploadTreeTableName);
107  if (!empty($baseFileRow)) {
108  $row['assoc_file'] = $baseFileRow;
109  $mergedArray[] = $row;
110  }
111  }
112  return $mergedArray;
113  }
114 
121  protected function findAssociatedFile($row, $uploadTreeTableName)
122  {
123  $stmt = __METHOD__ .'find_reso_base_file';
124  $sql = "SELECT * FROM $uploadTreeTableName
125  WHERE upload_fk=$1 AND ufile_name =$2 AND pfile_fk != 0 AND realparent=$3";
126  $param[] = $row['upload_fk'];
127  $param[] = substr($row['ufile_name'],0,-1 * abs(strlen(self::REUSE_FILE_SUFFIX)));
128  $param[] = $row['realparent'];
129  $this->dbManager->prepare($stmt, $sql);
130  $res = $this->dbManager->execute($stmt,$param);
131 
132  return $this->dbManager->fetchAll($res);
133  }
134 
141  protected function copyOjoFindings($linkedFiles,$uploadId)
142  {
143  //find agentId used for specific upload
144  $latestOjoAgent=$this->agentDao->agentARSList("ojo_ars",$uploadId);
145  $resoAgentId=$this->agentDao->getCurrentAgentId("reso");
146 
147  foreach ($linkedFiles as $file) {
148  $this->heartbeat(1);
149  $param = array();
150  $insertParam = array();
151  $stmt = __METHOD__ .'readLicenseFindingsOjo';
152  $sql = "SELECT * FROM license_file
153  WHERE pfile_fk =$1 AND agent_fk=$2 AND rf_fk IS NOT NULL";
154  $param[] = $file['pfile_fk'];
155  $param[] = $latestOjoAgent[0]['agent_fk'];
156 
157  $this->dbManager->prepare($stmt, $sql);
158  $res = $this->dbManager->execute($stmt,$param);
159  while ($row=$this->dbManager->fetchArray($res)) {
160  $insertParam = array();
161  $Istmt = __METHOD__ .'insertLicenseFindingsReso';
162  $Isql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk)
163  (SELECT $1, $2, $3 WHERE NOT EXISTS (SELECT fl_pk FROM license_file where rf_fk=$1 AND agent_fk=$2 AND pfile_fk=$3))
164  RETURNING fl_pk";
165  $insertParam[] = $row['rf_fk'];
166  $insertParam[] = $resoAgentId;
167  $insertParam[] = $file['assoc_file'][0]['pfile_fk'];
168 
169  $this->dbManager->prepare($Istmt, $Isql);
170  $Ires = $this->dbManager->execute($Istmt,$insertParam);
171  }
172  }
173  return true;
174  }
175 
182  protected function copyCopyrightFindings($linkedFiles,$uploadId)
183  {
184  //find agentId used for specific upload
185  $latestCopyrightAgent = $this->agentDao->agentARSList("copyright_ars",$uploadId);
186  $resoAgentId = $this->agentDao->getCurrentAgentId("reso");
187  if (empty($latestCopyrightAgent)) {
188  return;
189  }
190 
191  foreach ($linkedFiles as $file) {
192  $this->heartbeat(1);
193  $param = array();
194  $insertParam = array();
195  $stmt = __METHOD__ .'readCopyrightFindings';
196  $sql = "SELECT * FROM copyright WHERE pfile_fk =$1 AND agent_fk=$2";
197  $param[] = $file['pfile_fk'];
198  $param[] = $latestCopyrightAgent[0]['agent_fk'];
199 
200  $this->dbManager->prepare($stmt, $sql);
201  $res = $this->dbManager->execute($stmt,$param);
202  while ($row=$this->dbManager->fetchArray($res)) {
203  $copytightrec = $this->dbManager->getSingleRow("SELECT * FROM copyright WHERE agent_fk=$1 AND pfile_fk=$2 AND hash=md5($3)", array($resoAgentId, $file['assoc_file'][0]['pfile_fk'], $row['content']), __METHOD__.'checkExistingCopyright');
204  if (empty($copytightrec)) {
205  $insertParam = array();
206  $Insertstmt = __METHOD__ .'insertCopyrightFindingsReso';
207  $Insertsql = "INSERT INTO copyright(agent_fk, pfile_fk, content, hash, type, copy_startbyte, copy_endbyte) VALUES ($1, $2, $3, md5($3), $4, $5, $6)";
208  $insertParam[] = $resoAgentId;
209  $insertParam[] = $file['assoc_file'][0]['pfile_fk'];
210  $insertParam[] = $row['content'];
211  $insertParam[] = $row['type'];
212  $insertParam[] = -1;
213  $insertParam[] = -1;
214  $this->dbManager->prepare($Insertstmt, $Insertsql);
215  $Insertresult = $this->dbManager->execute($Insertstmt,$insertParam);
216  }
217  }
218  }
219  return true;
220  }
221 }
Structure of an Agent with all required parameters.
Definition: Agent.php:41
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
copyCopyrightFindings($linkedFiles, $uploadId)
Copy copyright from .license file to base file.
Definition: ResoAgent.php:182
processUploadId($uploadId)
Run reso for a package.
Definition: ResoAgent.php:62
copyOjoFindings($linkedFiles, $uploadId)
Copy license from licene holder to base file.
Definition: ResoAgent.php:141
associateBaseFile($reSoUploadFiles, $uploadTreeTableName)
Find and associate base file to files containting license info.
Definition: ResoAgent.php:102
findAssociatedFile($row, $uploadTreeTableName)
Find associated base file.
Definition: ResoAgent.php:121
findReSoUploadFiles($uploadId)
Find all files with specific suffix.
Definition: ResoAgent.php:82
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16