40 $sql =
"SELECT count(*) AS cnt FROM $tableName WHERE $tableName.$columnName is NULL;";
41 $totalPfile = $dbManager->getSingleRow($sql, [], __METHOD__ .
42 ".calculateNumberOfRecordsToBeProcesses" . $tableName);
44 if ($totalPfile[
'cnt'] > 10000) {
47 $count = $totalPfile[
'cnt'];
49 return array($count, $totalPfile[
'cnt']);
62 if($dbManager == null){
63 echo
"No connection object passed!\n";
67 echo
"*** Removing any duplicate manual findings from $tableName ***\n";
70 DELETE FROM $tableName 71 WHERE " . $tableName .
"_pk IN (SELECT " . $tableName .
"_pk 72 FROM (SELECT " . $tableName .
"_pk, is_enabled, 73 ROW_NUMBER() OVER (PARTITION BY textfinding, pfile_fk 74 ORDER BY " . $tableName .
"_pk) AS rnum 76 WHERE a.is_enabled = FALSE AND a.rnum > 1);";
79 $dbManager->queryOnce($sql);
84 DELETE FROM $tableName 85 WHERE " . $tableName .
"_pk IN (SELECT " . $tableName .
"_pk 86 FROM (SELECT " . $tableName .
"_pk, 87 ROW_NUMBER() OVER (PARTITION BY textfinding, pfile_fk 88 ORDER BY " . $tableName .
"_pk) AS rnum 93 $dbManager->queryOnce($sql);
106 if($dbManager == null){
107 echo
"No connection object passed!\n";
112 echo
"Table $tableName does not exists, not updating!\n";
117 $numberOfRecords = $numberOfRecords[0];
118 while (!empty($numberOfRecords)) {
119 $sql =
"SELECT " . $tableName .
"_pk AS id, textfinding " .
120 "FROM $tableName WHERE hash IS NULL LIMIT $numberOfRecords;";
121 $statement = __METHOD__ .
".getNullHash.$tableName.$numberOfRecords";
122 $rows = $dbManager->getRows($sql, [], $statement);
124 $sql =
"UPDATE $tableName AS m " .
125 "SET hash = c.sha256 FROM (VALUES ";
127 foreach ($rows as $row) {
128 $fileShaList[] =
"(" . $row[
"id"] .
",'" .
129 hash(
'sha256', $row[
'textfinding']) .
"')";
131 $sql .= join(
",", $fileShaList);
132 $sql .=
") AS c(id, sha256) WHERE c.id = m.$tableName" .
"_pk;";
134 $dbManager->queryOnce($sql, __METHOD__ .
".update.$tableName.hash");
135 $dbManager->commit();
137 $totalCount = $totalCount + $numberOfRecords;
139 $numberOfRecords = $numberOfRecords[0];
154 if ($dbManager == null) {
155 echo
"No connection object passed!\n";
161 echo
"Table $tableName does not exists, not updating!\n";
166 $lastCount = $records[1];
167 $numberOfRecords = $records[0];
168 while (!empty($numberOfRecords)) {
169 $sql =
"SELECT ".$tableName.
".".$tableName .
"_pk AS id " .
170 "FROM $tableName WHERE $tableName." . $tableName .
"_sha256 is NULL " .
171 "LIMIT $numberOfRecords";
172 $statement = __METHOD__ .
".getNullSHA256.$tableName.$numberOfRecords";
173 $rows = $dbManager->getRows($sql, [], $statement);
175 $sql =
"UPDATE $tableName AS m " .
176 "SET " . $tableName .
"_sha256 = c.sha256 " .
179 foreach ($rows as $row) {
180 $oneRow =
"(" . $row[
"id"];
181 $filePath =
RepPath($row[
'id'],
"files");
182 if (file_exists($filePath)) {
183 $hash = strtoupper(hash_file(
'sha256', $filePath));
184 $oneRow .=
",'$hash')";
188 $fileShaList[] = $oneRow;
190 $sql .= join(
",", $fileShaList);
191 $sql .=
") AS c(id, sha256) WHERE c.id = m.$tableName" .
"_pk;";
193 $dbManager->queryOnce($sql, __METHOD__ .
".updatePfile_SHA256");
194 $dbManager->commit();
196 $totalCount = $totalCount + $numberOfRecords;
197 echo
"* $totalCount pfile records updated *\n";
200 if ($lastCount == $records[1]) {
206 $lastCount = $records[1];
207 $numberOfRecords = $records[0];
221 if (!empty($where)) {
222 $where =
"AND $where";
224 $sql =
"SELECT count(*) AS cnt FROM $tableName " .
225 "WHERE $colName != UPPER($colName) $where;";
226 $row = $dbManager->getSingleRow($sql, [], __METHOD__ .
227 ".checkLowerCaseIn.$tableName".strlen($where));
228 return ($row[
"cnt"] == 0);
231 function updatePfileSha256($dbManager, $force =
false)
233 if (!
isColumnUpperCase($dbManager,
"pfile",
"pfile_sha256",
"pfile_sha256 IS NOT NULL")) {
235 $sql =
"UPDATE pfile SET pfile_sha256 = UPPER(pfile_sha256);";
236 $statement = __METHOD__ .
".updatePfileSHA256ToUpper";
238 $dbManager->queryOnce($sql, $statement);
239 $dbManager->commit();
243 if (!empty($totalPfile)) {
244 $totalPfile = $totalPfile[1];
249 if ($totalPfile == 0) {
253 $envYes = getenv(
'FOSSPFILE');
255 $force = !empty($envYes);
258 $timePerJob = 0.00905919;
259 $totalTime = floatval($totalPfile) * $timePerJob;
260 $minutes = intval($totalTime / 60.0);
261 $hours = floor($minutes / 60);
262 $actualMinutes = $minutes - ($hours * 60);
264 echo
"*** Calculation of SHA256 for pfiles will require approx $hours hrs " .
265 "$actualMinutes mins. ***\n";
267 if (!$force && $minutes > 45) {
268 $REDCOLOR =
"\033[0;31m";
269 $NOCOLOR =
"\033[0m";
270 echo
"\n*********************************************************" .
271 "***********************\n";
272 echo
"*** " . $REDCOLOR .
"Error, script will take too much time. Not " .
273 "calculating SHA256 for pfile." . $NOCOLOR .
" ***\n";
274 echo
"*** Either rerun the fo-postinstall with \"--force-pfile\" flag " .
276 "*** \"FOSSPFILE=1\" in environment or run script at " .
278 echo
"*** \"" . dirname(__FILE__) .
279 "/dbmigrate_pfile_calculate_sha256.php\" to continue as a separate process ***\n";
280 echo
"*********************************************************" .
281 "***********************\n";
286 echo
"*** Updating the sha256 values of pfiles ***\n";
288 echo
"*** Updated sha256 of $countPfile/$totalPfile records of pfile ***\n";
289 }
catch (Exception $e) {
290 echo
"*** Something went wrong. Try again! ***\n";
291 $dbManager->rollback();
305 "copyright_decision",
310 $sql =
"WITH decision_tables AS(".
311 " SELECT count(*) AS cnt FROM $tables[0] WHERE hash IS NULL" .
313 " SELECT count(*) AS cnt FROM $tables[1] WHERE hash IS NULL" .
315 " SELECT count(*) AS cnt FROM $tables[2] WHERE hash IS NULL" .
316 ") SELECT SUM(cnt) AS total FROM decision_tables;";
317 $total = $dbManager->getSingleRow($sql, [], __METHOD__ .
318 ".checkIfMigrationDone");
319 $total = intval($total[
"total"]);
330 echo
"*** Updating the hash values of manual copyright/ecc/keyword findings ***\n";
332 foreach ($tables as $table) {
337 echo
"*** Updated hash of $count/$total manual copyright/ecc/keyword findings ***\n";
338 }
catch (Exception $e) {
339 echo
"*** Something went wrong. Try running postinstall again! ***\n";
340 $dbManager->rollback();
migrate_35_36($dbManager, $force=false)
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
RepPath($PfilePk, $Repo="files")
Given a pfile id, retrieve the pfile path.
DB_TableExists($tableName)
Check if table exists.
updateHash($dbManager, $tableName)
Update the hash column of the table with value from textfinding.
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.