8 namespace Fossology\Lib\Report;
22 private $fileNameCache = array();
25 private $fileHashes = array();
32 public function __construct($groupBy =
"content")
36 $this->uploadDao = $container->get(
'dao.upload');
37 $this->treeDao = $container->get(
'dao.tree');
39 $this->groupBy = $groupBy;
47 $args = getopt(
"u:", array(
"uId:",
"gId:"));
49 if (!array_key_exists(
'u',$args)) {
50 throw new \Exception(
"missing required parameter -u {uploadId}\n",2);
53 $this->uploadId = intval($args[
'u']);
54 $this->userId = intval(@$args[
'uId']);
55 $this->groupId = intval(@$args[
'gId']);
58 public function getUploadId()
60 $uploadId = $this->uploadId;
63 print
"invalid uploadId ".$uploadId;
69 public function getUserId()
71 $userId = $this->userId;
74 print
"invalid user ".$userId;
80 public function getGroupId()
82 $groupId = $this->groupId;
85 print
"invalid group ".$groupId;
91 protected function changeTreeIdsToPaths(&$ungrupedStatements, $uploadTreeTableName, $uploadId)
93 $parentId = $this->treeDao->getMinimalCoveringItem($uploadId, $uploadTreeTableName);
95 foreach ($ungrupedStatements as &$statement) {
96 $uploadTreeId = $statement[
'uploadtree_pk'];
97 unset($statement[
'uploadtree_pk']);
99 if (!array_key_exists($uploadTreeId, $this->fileNameCache)) {
100 $this->fileNameCache[$uploadTreeId] = $this->treeDao->getFullPath($uploadTreeId, $uploadTreeTableName, $parentId);
101 $this->fileHashes[$uploadTreeId] = $this->treeDao->getItemHashes($uploadTreeId);
104 $statement[
'fileName'] = $this->fileNameCache[$uploadTreeId];
105 $statement[
'fileHash'] = strtolower($this->fileHashes[$uploadTreeId][
"sha1"]);
117 $uniqueArray = array();
118 foreach ($findings as $item) {
119 $contentKey = $item[
'content'];
120 if (!isset($uniqueArray[$contentKey])) {
121 $uniqueArray[$contentKey] = array(
122 "licenseId" => $item[
"licenseId"],
123 "content" => $item[
"content"],
124 "text" => $item[
"text"],
127 "comments" => $item[
"comments"]
130 $uniqueArray[$contentKey][
'files'] = array_merge($uniqueArray[$contentKey][
'files'], $item[
'files']);
131 $uniqueArray[$contentKey][
'hash'] = array_merge($uniqueArray[$contentKey][
'hash'], $item[
'hash']);
133 return array_values($uniqueArray);
136 protected function groupStatements($ungrupedStatements, $extended, $agentCall, $isUnifiedReport, $objectAgent)
138 $statements = array();
141 foreach ($ungrupedStatements as $statement) {
142 $licenseId = (array_key_exists(
'licenseId', $statement)) ?
convertToUTF8($statement[
'licenseId'],
false) :
'';
143 $content = (array_key_exists(
'content', $statement)) ?
convertToUTF8($statement[
'content'],
false) :
'';
144 $content = htmlspecialchars($content, ENT_DISALLOWED);
145 $comments = (array_key_exists(
'comments', $statement)) ?
convertToUTF8($statement[
'comments'],
false) :
'';
146 $fileName = $statement[
'fileName'];
147 $fileHash = $statement[
'fileHash'];
148 if (array_key_exists(
'acknowledgement', $statement)) {
149 $acknowledgement = $statement[
'acknowledgement'];
151 $acknowledgement =
"";
154 if (!array_key_exists(
'text', $statement)) {
155 $description = (array_key_exists(
'description', $statement)) ?
convertToUTF8($statement[
'description'],
false) :
'';
156 $textfinding = (array_key_exists(
'textfinding', $statement)) ?
convertToUTF8($statement[
'textfinding'],
false) :
'';
158 if ($description ===
null) {
161 if (!empty($textfinding) && empty($agentCall)) {
162 $content = $textfinding;
164 $text = $description;
167 $text = $statement[
'text'];
170 if ($agentCall ==
"license") {
171 $this->groupBy =
"text";
172 $groupBy = md5($statement[$this->groupBy].$statement[
"content"]);
174 $this->groupBy =
"content";
175 $groupBy = $statement[$this->groupBy];
178 if (empty($comments) && array_key_exists($groupBy, $statements)) {
179 $currentFiles = &$statements[$groupBy][
'files'];
180 $currentHash = &$statements[$groupBy][
'hash'];
181 $currentAcknowledgement = &$statements[$groupBy][
'acknowledgement'];
182 if (!in_array($fileName, $currentFiles)) {
183 $currentFiles[] = $fileName;
184 $currentHash[] = $fileHash;
185 $currentAcknowledgement[] = $acknowledgement;
188 $singleStatement = array(
189 "licenseId" => $licenseId,
192 "files" => array($fileName),
193 "hash" => array($fileHash),
194 "acknowledgement" => array($acknowledgement)
197 $singleStatement[
"licenseId"] = $licenseId;
198 $singleStatement[
"comments"] =
convertToUTF8($comments,
false);
199 $singleStatement[
"risk"] = (array_key_exists(
'risk', $statement)) ?
convertToUTF8($statement[
'risk'],
false) : 0;
202 if (empty($comments)) {
203 $statements[$groupBy] = $singleStatement;
205 $statements[] = $singleStatement;
209 if (!empty($statement[
'textfinding']) && !empty($agentCall) && $agentCall !=
"license") {
211 "licenseId" => $licenseId,
212 "content" =>
convertToUTF8($statement[
'textfinding'],
false),
214 "files" => array($fileName),
215 "hash" => array($fileHash)
218 $key = array_search($statement[
'textfinding'], array_column($findings,
'content'));
219 $findings[$key][
"comments"] =
convertToUTF8($comments,
false);
220 $findings[$key][
"licenseId"] = $licenseId;
225 if ($countLoop % 500 == 0) {
226 $objectAgent->heartbeat(0);
230 if ($agentCall ==
"copyright" && $isUnifiedReport) {
232 if (!empty($objectAgent)) {
233 $actualHeartbeat = (count($statements) + count($findings));
234 $objectAgent->heartbeat($actualHeartbeat);
236 return array(
"userFindings" => $this->
groupUserFindings($findings),
"scannerFindings" => $statements);
238 $statements = array_merge($findings, $statements);
239 if (!empty($objectAgent)) {
240 $objectAgent->heartbeat(count($statements));
242 if ($agentCall !=
"license") {
243 return array(
"statements" => $this->
groupUserFindings(array_values($statements)));
245 return array(
"statements" => array_values($statements));
255 abstract protected function getStatements($uploadId, $uploadTreeTableName, $groupId=
null);
257 public function getCleared($uploadId, $objectAgent, $groupId=
null, $extended=
true, $agentcall=
null, $isUnifiedReport=
false)
259 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
260 $ungroupedStatements = $this->
getStatements($uploadId, $uploadTreeTableName, $groupId);
261 $this->changeTreeIdsToPaths($ungroupedStatements, $uploadTreeTableName, $uploadId);
262 return $this->groupStatements($ungroupedStatements, $extended, $agentcall,
263 $isUnifiedReport, $objectAgent);
266 public function getLicenseHistogramForReport($uploadId, $groupId)
268 $histogramStatements = $this->getHistogram($uploadId, $groupId);
269 return array(
"statements" => $histogramStatements);
272 public function cJson($uploadId, $groupId=
null)
274 $escapeChars = array(
'\\f',
"\\",
"/",
"\"");
275 $withThisValue = array(
"",
"\\\\",
"\\/",
"\\\"");
276 $groupedStatements = $this->getCleared($uploadId,
null, $groupId,
false,
278 if (array_key_exists(
"statements", $groupedStatements)) {
279 $clearedString = str_replace($escapeChars, $withThisValue,
280 $groupedStatements[
"statements"]);
282 $clearedString = $groupedStatements;
284 $json = json_encode($clearedString);
285 return str_replace(
'\u001b',
'',$json);
288 public function cJsonHist($uploadId, $groupId=
null)
290 $jsonHist = json_encode($this->getLicenseHistogramForReport($uploadId, $groupId));
291 return str_replace(
'\u001b',
'',str_replace(
'\\f',
'',$jsonHist));
groupUserFindings($findings)
Group the content inside a array.
getStatements($uploadId, $uploadTreeTableName, $groupId=null)
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)