29 $sql =
"SELECT count(*) AS cnt FROM $tableName WHERE $tableName.$columnName is NULL;";
30 $totalPfile = $dbManager->getSingleRow($sql, [], __METHOD__ .
31 ".calculateNumberOfRecordsToBeProcesses" . $tableName);
33 if ($totalPfile[
'cnt'] > 10000) {
36 $count = $totalPfile[
'cnt'];
38 return array($count, $totalPfile[
'cnt']);
51 if($dbManager ==
null){
52 echo
"No connection object passed!\n";
56 echo
"*** Removing any duplicate manual findings from $tableName ***\n";
59 DELETE FROM $tableName
60 WHERE " . $tableName .
"_pk IN (SELECT " . $tableName .
"_pk
61 FROM (SELECT " . $tableName .
"_pk, is_enabled,
62 ROW_NUMBER() OVER (PARTITION BY textfinding, pfile_fk
63 ORDER BY " . $tableName .
"_pk) AS rnum
65 WHERE a.is_enabled = FALSE AND a.rnum > 1);";
68 $dbManager->queryOnce($sql);
73 DELETE FROM $tableName
74 WHERE " . $tableName .
"_pk IN (SELECT " . $tableName .
"_pk
75 FROM (SELECT " . $tableName .
"_pk,
76 ROW_NUMBER() OVER (PARTITION BY textfinding, pfile_fk
77 ORDER BY " . $tableName .
"_pk) AS rnum
82 $dbManager->queryOnce($sql);
95 if($dbManager ==
null){
96 echo
"No connection object passed!\n";
101 echo
"Table $tableName does not exists, not updating!\n";
106 $numberOfRecords = $numberOfRecords[0];
107 while (!empty($numberOfRecords)) {
108 $sql =
"SELECT " . $tableName .
"_pk AS id, textfinding " .
109 "FROM $tableName WHERE hash IS NULL LIMIT $numberOfRecords;";
110 $statement = __METHOD__ .
".getNullHash.$tableName.$numberOfRecords";
111 $rows = $dbManager->getRows($sql, [], $statement);
113 $sql =
"UPDATE $tableName AS m " .
114 "SET hash = c.sha256 FROM (VALUES ";
116 foreach ($rows as $row) {
117 $fileShaList[] =
"(" . $row[
"id"] .
",'" .
118 hash(
'sha256', $row[
'textfinding']) .
"')";
120 $sql .= join(
",", $fileShaList);
121 $sql .=
") AS c(id, sha256) WHERE c.id = m.$tableName" .
"_pk;";
123 $dbManager->queryOnce($sql, __METHOD__ .
".update.$tableName.hash");
124 $dbManager->commit();
126 $totalCount += $numberOfRecords;
128 $numberOfRecords = $numberOfRecords[0];
143 if ($dbManager ==
null) {
144 echo
"No connection object passed!\n";
150 echo
"Table $tableName does not exists, not updating!\n";
155 $lastCount = $records[1];
156 $numberOfRecords = $records[0];
157 while (!empty($numberOfRecords)) {
158 $sql =
"SELECT ".$tableName.
".".$tableName .
"_pk AS id " .
159 "FROM $tableName WHERE $tableName." . $tableName .
"_sha256 is NULL " .
160 "LIMIT $numberOfRecords";
161 $statement = __METHOD__ .
".getNullSHA256.$tableName.$numberOfRecords";
162 $rows = $dbManager->getRows($sql, [], $statement);
164 $sql =
"UPDATE $tableName AS m " .
165 "SET " . $tableName .
"_sha256 = c.sha256 " .
168 foreach ($rows as $row) {
169 $oneRow =
"(" . $row[
"id"];
170 $filePath =
RepPath($row[
'id'],
"files");
171 if (file_exists($filePath)) {
172 $hash =
strtoupper(hash_file(
'sha256', $filePath));
173 $oneRow .=
",'$hash')";
177 $fileShaList[] = $oneRow;
179 $sql .= join(
",", $fileShaList);
180 $sql .=
") AS c(id, sha256) WHERE c.id = m.$tableName" .
"_pk;";
182 $dbManager->queryOnce($sql, __METHOD__ .
".updatePfile_SHA256");
183 $dbManager->commit();
185 $totalCount += $numberOfRecords;
186 echo
"* $totalCount pfile records updated *\n";
189 if ($lastCount == $records[1]) {
195 $lastCount = $records[1];
196 $numberOfRecords = $records[0];
210 if (!empty($where)) {
211 $where =
"AND $where";
213 $sql =
"SELECT count(*) AS cnt FROM $tableName " .
214 "WHERE $colName != UPPER($colName) $where;";
215 $row = $dbManager->getSingleRow($sql, [], __METHOD__ .
216 ".checkLowerCaseIn.$tableName".strlen($where));
217 return ($row[
"cnt"] == 0);
220 function updatePfileSha256($dbManager, $force =
false)
222 if (!
isColumnUpperCase($dbManager,
"pfile",
"pfile_sha256",
"pfile_sha256 IS NOT NULL")) {
224 $sql =
"UPDATE pfile SET pfile_sha256 = UPPER(pfile_sha256);";
225 $statement = __METHOD__ .
".updatePfileSHA256ToUpper";
227 $dbManager->queryOnce($sql, $statement);
228 $dbManager->commit();
232 if (!empty($totalPfile)) {
233 $totalPfile = $totalPfile[1];
238 if ($totalPfile == 0) {
242 $envYes = getenv(
'FOSSPFILE');
244 $force = !empty($envYes);
247 $timePerJob = 0.00905919;
248 $totalTime = floatval($totalPfile) * $timePerJob;
249 $minutes = intval($totalTime / 60.0);
250 $hours = floor($minutes / 60);
251 $actualMinutes = $minutes - ($hours * 60);
253 echo
"*** Calculation of SHA256 for pfiles will require approx $hours hrs " .
254 "$actualMinutes mins. ***\n";
256 if (!$force && $minutes > 45) {
257 $REDCOLOR =
"\033[0;31m";
258 $NOCOLOR =
"\033[0m";
259 echo
"\n*********************************************************" .
260 "***********************\n";
261 echo
"*** " . $REDCOLOR .
"Error, script will take too much time. Not " .
262 "calculating SHA256 for pfile." . $NOCOLOR .
" ***\n";
263 echo
"*** Either rerun the fo-postinstall with \"--force-pfile\" flag " .
265 "*** \"FOSSPFILE=1\" in environment or run script at " .
267 echo
"*** \"" . dirname(__FILE__) .
268 "/dbmigrate_pfile_calculate_sha256.php\" to continue as a separate process ***\n";
269 echo
"*********************************************************" .
270 "***********************\n";
275 echo
"*** Updating the sha256 values of pfiles ***\n";
277 echo
"*** Updated sha256 of $countPfile/$totalPfile records of pfile ***\n";
278 }
catch (Exception $e) {
279 echo
"*** Something went wrong. Try again! ***\n";
280 $dbManager->rollback();
294 "copyright_decision",
299 $sql =
"WITH decision_tables AS(".
300 " SELECT count(*) AS cnt FROM $tables[0] WHERE hash IS NULL" .
302 " SELECT count(*) AS cnt FROM $tables[1] WHERE hash IS NULL" .
304 " SELECT count(*) AS cnt FROM $tables[2] WHERE hash IS NULL" .
305 ") SELECT SUM(cnt) AS total FROM decision_tables;";
306 $total = $dbManager->getSingleRow($sql, [], __METHOD__ .
307 ".checkIfMigrationDone");
308 $total = intval($total[
"total"]);
319 echo
"*** Updating the hash values of manual copyright/ecc/keyword findings ***\n";
321 foreach ($tables as $table) {
326 echo
"*** Updated hash of $count/$total manual copyright/ecc/keyword findings ***\n";
327 }
catch (Exception $e) {
328 echo
"*** Something went wrong. Try running postinstall again! ***\n";
329 $dbManager->rollback();
DB_TableExists($tableName)
Check if table exists.
RepPath($PfilePk, $Repo="files")
Given a pfile id, retrieve the pfile path.
updateSHA256($dbManager, $tableName)
Update the sha256 column of the table with value from textfinding.
calculateNumberOfRecordsToBeProcessed($dbManager, $tableName, $columnName)
calculate number of records and return offset
isColumnUpperCase($dbManager, $tableName, $colName, $where)
Check if the given column contains only upper case entries.
cleanDecisionTable($dbManager, $tableName)
Removes duplicate decisions based on same textfinding for same pfile.
migrate_35_36($dbManager, $force=false)
updateHash($dbManager, $tableName)
Update the hash column of the table with value from textfinding.
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.