FOSSology  4.7.0-rc1
Open Source License Compliance by Open Source Software
BulkTextExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Kaushlendra Pratap <kaushlendra-pratap.singh@siemens.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
11 
22 {
25  protected $dbManager;
28  protected $delimiter = ',';
31  protected $enclosure = '"';
32 
37  public function __construct(DbManager $dbManager)
38  {
39  $this->dbManager = $dbManager;
40  }
41 
46  public function setDelimiter($delimiter=',')
47  {
48  if (!is_string($delimiter) || strlen($delimiter) !== 1) {
49  throw new \InvalidArgumentException("CSV delimiter must be a non-empty single-byte character.");
50  }
51  if ($delimiter === $this->enclosure) {
52  throw new \InvalidArgumentException("CSV delimiter and enclosure must be different characters.");
53  }
54  $this->delimiter = $delimiter;
55  }
56 
61  public function setEnclosure($enclosure='"')
62  {
63  if (!is_string($enclosure) || strlen($enclosure) !== 1) {
64  throw new \InvalidArgumentException("CSV enclosure must be a non-empty single-byte character.");
65  }
66  if ($enclosure === $this->delimiter) {
67  throw new \InvalidArgumentException("CSV delimiter and enclosure must be different characters.");
68  }
69  $this->enclosure = $enclosure;
70  }
71 
79  public function exportBulkText($user_pk=0, $group_pk=0, $generateJson=false)
80  {
81  $whereClause = "";
82  $params = array();
83 
84  if ($user_pk > 0) {
85  $whereClause = "WHERE lrb.user_fk = $1";
86  $params[] = $user_pk;
87  } elseif ($group_pk > 0) {
88  $whereClause = "WHERE lrb.group_fk = $1";
89  $params[] = $group_pk;
90  }
91 
92  $sql = "SELECT DISTINCT
93  lrb.rf_text,
94  lr.rf_shortname,
95  lsb.removing,
96  lsb.comment,
97  lsb.acknowledgement,
98  lr.rf_active
99  FROM license_ref_bulk lrb
100  LEFT JOIN license_set_bulk lsb ON lsb.lrb_fk = lrb.lrb_pk
101  LEFT JOIN license_ref lr ON lr.rf_pk = lsb.rf_fk
102  $whereClause
103  ORDER BY lrb.rf_text, lr.rf_shortname";
104 
105  $result = $this->dbManager->getRows($sql, $params);
106 
107  if ($generateJson) {
108  return $this->createJson($result);
109  } else {
110  return $this->createCsvContent($result);
111  }
112  }
113 
119  private function groupResultsByText($result)
120  {
121  $grouped = array();
122  foreach ($result as $row) {
123  $text = $row['rf_text'] ?: '';
124  if (!isset($grouped[$text])) {
125  $grouped[$text] = array(
126  'licenses_to_add' => array(),
127  'licenses_to_remove' => array(),
128  'comments' => array(),
129  'acknowledgements' => array(),
130  'is_active_values' => array()
131  );
132  }
133  if (!empty($row['rf_shortname'])) {
134  if ($row['removing'] === 't' || $row['removing'] === true) {
135  $grouped[$text]['licenses_to_remove'][] = $row['rf_shortname'];
136  } else {
137  $grouped[$text]['licenses_to_add'][] = $row['rf_shortname'];
138  }
139  }
140 
141  if (!empty($row['comment'])) {
142  $grouped[$text]['comments'][] = $row['comment'];
143  }
144 
145  if (!empty($row['acknowledgement'])) {
146  $grouped[$text]['acknowledgements'][] = $row['acknowledgement'];
147  }
148 
149  if ($row['rf_active'] !== null) {
150  $isActive = ($row['rf_active'] === 't' || $row['rf_active'] === true ||
151  $row['rf_active'] === 1 || $row['rf_active'] === '1');
152  $grouped[$text]['is_active_values'][] = $isActive;
153  }
154  }
155 
156  foreach ($grouped as $text => $values) {
157  $grouped[$text]['licenses_to_add'] = array_values(array_unique($values['licenses_to_add']));
158  $grouped[$text]['licenses_to_remove'] = array_values(array_unique($values['licenses_to_remove']));
159  $grouped[$text]['comments'] = array_values(array_unique($values['comments']));
160  $grouped[$text]['acknowledgements'] = array_values(array_unique($values['acknowledgements']));
161 
162  if (empty($values['is_active_values'])) {
163  $grouped[$text]['is_active'] = null;
164  } else {
165  $grouped[$text]['is_active'] = !in_array(false, $values['is_active_values'], true);
166  }
167 
168  unset($grouped[$text]['is_active_values']);
169  }
170 
171  return $grouped;
172  }
173 
179  private function createCsvContent($result)
180  {
181  $headers = array('text', 'licenses_to_add', 'licenses_to_remove', 'comments', 'acknowledgements', 'is_active');
182  $out = fopen('php://output', 'w');
183  ob_start();
184  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
185  fputcsv($out, $headers, $this->delimiter, $this->enclosure);
186 
187  foreach ($this->groupResultsByText($result) as $text => $licenses) {
188  $csvRow = array(
189  $this->normalizeNewlinesForCsv($text),
190  implode('|', array_map(array($this, 'normalizeNewlinesForCsv'), $licenses['licenses_to_add'])),
191  implode('|', array_map(array($this, 'normalizeNewlinesForCsv'), $licenses['licenses_to_remove'])),
192  implode('|', array_map(array($this, 'normalizeNewlinesForCsv'), $licenses['comments'])),
193  implode('|', array_map(array($this, 'normalizeNewlinesForCsv'), $licenses['acknowledgements'])),
194  $licenses['is_active'] === null ? '' : ($licenses['is_active'] ? 'true' : 'false')
195  );
196  fputcsv($out, $csvRow, $this->delimiter, $this->enclosure);
197  }
198 
199  $content = ob_get_contents();
200  ob_end_clean();
201  return $content;
202  }
203 
209  private function normalizeNewlinesForCsv($value)
210  {
211  if ($value === null) {
212  return '';
213  }
214  return str_replace(array("\r\n", "\r", "\n"), '\\n', (string)$value);
215  }
216 
222  private function createJson($result)
223  {
224  $data = array();
225 
226  foreach ($this->groupResultsByText($result) as $text => $licenses) {
227  $data[] = array(
228  'text' => $text,
229  'licenses_to_add' => $licenses['licenses_to_add'],
230  'licenses_to_remove' => $licenses['licenses_to_remove'],
231  'comments' => $licenses['comments'],
232  'acknowledgements' => $licenses['acknowledgements'],
233  'is_active' => $licenses['is_active']
234  );
235  }
236 
237  return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
238  }
239 }
Helper class to export license reference bulk data as CSV or JSON from the DB.
setDelimiter($delimiter=',')
Update the delimiter.
createCsvContent($result)
Create CSV content from result array.
normalizeNewlinesForCsv($value)
Convert CR/LF variants to literal for line-safe CSV rows.
exportBulkText($user_pk=0, $group_pk=0, $generateJson=false)
Export license reference bulk data from the DB as CSV or JSON.
createJson($result)
Create JSON content from result array.
groupResultsByText($result)
Group flat DB rows by bulk text, splitting licenses into add/remove buckets.
setEnclosure($enclosure='"')
Update the enclosure.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.