FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CustomTextExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Harshit Gandhi <gandhiharshit716@gmail.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 
48  public function export($cp_pk=0, $generateJson=false)
49  {
50  $whereClause = "";
51  $params = array();
52 
53  if ($cp_pk > 0) {
54  $whereClause = "WHERE cp.cp_pk = $1";
55  $params[] = $cp_pk;
56  }
57 
58  $sql = "SELECT
59  cp.cp_pk,
60  cp.text,
61  cp.created_date,
62  cp.is_active,
63  u.user_name,
64  g.group_name,
65  lr.rf_shortname,
66  cplm.removing,
67  cplm.comment,
68  cplm.reportinfo,
69  cplm.acknowledgement
70  FROM custom_phrase cp
71  LEFT JOIN users u ON cp.user_fk = u.user_pk
72  LEFT JOIN groups g ON cp.group_fk = g.group_pk
73  LEFT JOIN custom_phrase_license_map cplm ON cp.cp_pk = cplm.cp_fk
74  LEFT JOIN license_ref lr ON cplm.rf_fk = lr.rf_pk
75  $whereClause
76  ORDER BY cp.created_date DESC, cp.cp_pk, lr.rf_shortname";
77 
78  $rows = $this->dbManager->getRows($sql, $params);
79 
80  $phrases = array();
81  foreach ($rows as $row) {
82  $cpPk = $row['cp_pk'];
83  if (!isset($phrases[$cpPk])) {
84  $phrases[$cpPk] = array(
85  'text' => $row['text'],
86  'created_date' => $row['created_date'],
87  // booleanFromDb(), not raw truthiness: Postgres returns 'f' for
88  // false, which PHP treats as truthy.
89  'is_active' => $this->dbManager->booleanFromDb($row['is_active']),
90  'user_name' => $row['user_name'] ?: '',
91  'group_name' => $row['group_name'] ?: '',
92  'licenses' => array()
93  );
94  }
95  if (!empty($row['rf_shortname'])) {
96  $phrases[$cpPk]['licenses'][] = array(
97  'shortname' => $row['rf_shortname'],
98  'removing' => $this->dbManager->booleanFromDb($row['removing']),
99  'comment' => $row['comment'] ?: '',
100  'reportinfo' => $row['reportinfo'] ?: '',
101  'acknowledgement' => $row['acknowledgement'] ?: ''
102  );
103  }
104  }
105 
106  if ($generateJson) {
107  return $this->createJson(array_values($phrases));
108  } else {
109  return $this->createCsvContent(array_values($phrases));
110  }
111  }
112 
118  private function createCsvContent($phrases)
119  {
120  $csv = '';
121 
122  $headers = array(
123  'Text',
124  'Created Date',
125  'Is Active',
126  'Created By',
127  'Group',
128  'License Shortname',
129  'Removing',
130  'Comment',
131  'License Text',
132  'Acknowledgement'
133  );
134  $csv .= $this->arrayToCsvLine($headers);
135 
136  foreach ($phrases as $phrase) {
137  // Same newline-flattening scheme as BulkTextExport, so CustomTextImport
138  // can reverse it regardless of which exporter produced the file.
139  $text = CustomTextEscaping::escapeNewlines($phrase['text']);
140 
141  if (empty($phrase['licenses'])) {
142  $csv .= $this->arrayToCsvLine(array(
143  $text,
144  $phrase['created_date'],
145  $phrase['is_active'] ? 'true' : 'false',
146  $phrase['user_name'],
147  $phrase['group_name'],
148  '', '', '', '', ''
149  ));
150  } else {
151  foreach ($phrase['licenses'] as $lic) {
152  $csv .= $this->arrayToCsvLine(array(
153  $text,
154  $phrase['created_date'],
155  $phrase['is_active'] ? 'true' : 'false',
156  $phrase['user_name'],
157  $phrase['group_name'],
158  $lic['shortname'],
159  $lic['removing'] ? 'true' : 'false',
160  CustomTextEscaping::escapeNewlines($lic['comment']),
161  CustomTextEscaping::escapeNewlines($lic['reportinfo']),
162  CustomTextEscaping::escapeNewlines($lic['acknowledgement'])
163  ));
164  }
165  }
166  }
167 
168  return $csv;
169  }
170 
176  private function createJson($phrases)
177  {
178  $data = array();
179 
180  foreach ($phrases as $phrase) {
181  $data[] = array(
182  'text' => $phrase['text'],
183  'created_date' => $phrase['created_date'],
184  'is_active' => (bool) $phrase['is_active'],
185  'created_by' => $phrase['user_name'],
186  'group' => $phrase['group_name'],
187  'licenses' => array_map(function ($lic) {
188  return array(
189  'shortname' => $lic['shortname'],
190  'removing' => (bool) $lic['removing'],
191  'comment' => $lic['comment'],
192  'reportinfo' => $lic['reportinfo'],
193  'acknowledgement' => $lic['acknowledgement']
194  );
195  }, $phrase['licenses'])
196  );
197  }
198 
199  return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
200  }
201 
207  private function arrayToCsvLine($array)
208  {
209  $csvLine = '';
210  foreach ($array as $key => $value) {
211  if ($key > 0) {
212  $csvLine .= $this->delimiter;
213  }
214 
215  // Escape the value if it contains delimiter, enclosure, or newline
216  if (strpos($value, $this->delimiter) !== false ||
217  strpos($value, $this->enclosure) !== false ||
218  strpos($value, "\n") !== false ||
219  strpos($value, "\r") !== false) {
220  $value = $this->enclosure . str_replace($this->enclosure, $this->enclosure . $this->enclosure, $value) . $this->enclosure;
221  }
222 
223  $csvLine .= $value;
224  }
225  $csvLine .= "\n";
226 
227  return $csvLine;
228  }
229 }
static escapeNewlines($value)
Flatten real newlines to a literal so a field stays on one physical CSV line. Backslashes are escap...
Helper class to export custom text phrases as CSV/JSON from the DB.
createJson($phrases)
Create JSON content from result array.
arrayToCsvLine($array)
Convert array to CSV line.
export($cp_pk=0, $generateJson=false)
Create the CSV/JSON export from the DB.
createCsvContent($phrases)
Create CSV content from result array.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.