FOSSology  4.4.0
Open Source License Compliance by Open Source Software
sanity_check.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 
24 {
26  protected $dbManager;
28  protected $verbose;
30  protected $errors = 0;
31 
32  function __construct(&$dbManager,$verbose)
33  {
34  $this->dbManager = $dbManager;
35  $this->verbose = $verbose;
36  }
37 
44  public function check()
45  {
46  $this->checkDecisionScopes();
47  $this->checkUploadStatus();
48  $this->checkLicenseEventTypes();
49  $this->checkExistsTable('license_candidate');
50  $folderDao = new FolderDao($this->dbManager, $GLOBALS['container']->get('dao.user'), $GLOBALS['container']->get('dao.upload'));
51  $folderDao->ensureTopLevelFolder();
52 
53  return $this->errors;
54  }
55 
59  private function checkDecisionScopes()
60  {
61  $decScopes = new DecisionScopes();
62  $scopeMap = $decScopes->getMap();
63  $this->errors += $this->checkDatabaseEnum($tablename = 'clearing_decision', 'scope', $scopeMap);
64  $decTypes = new DecisionTypes();
65  $typeMap = $decTypes->getExtendedMap();
66  $this->errors += $this->checkDatabaseEnum($tablename = 'clearing_decision', 'decision_type', $typeMap);
67  }
68 
72  private function checkUploadStatus()
73  {
74  $uploadStatus = new UploadStatus();
75  $statusMap = $uploadStatus->getMap();
76  $this->errors += $this->checkDatabaseEnum($tablename = 'upload_clearing', 'status_fk', $statusMap);
77  }
78 
82  private function checkLicenseEventTypes()
83  {
84  $licenseEventTypes = new ClearingEventTypes();
85  $map = $licenseEventTypes->getMap();
86  $this->errors += $this->checkDatabaseEnum($tablename='clearing_event', 'type_fk', $map);
87  }
88 
96  private function checkDatabaseEnum($tablename,$columnname,$map)
97  {
98  $errors = 0;
99  $stmt = __METHOD__.".$tablename.$columnname";
100  $sql = "SELECT $columnname,count(*) FROM $tablename GROUP BY $columnname";
101  $this->dbManager->prepare($stmt,$sql);
102  $res = $this->dbManager->execute($stmt);
103  while($row = $this->dbManager->fetchArray($res))
104  {
105  if(!array_key_exists($row[$columnname], $map))
106  {
107  echo "(-) found invalid $columnname '".$row[$columnname]."' in table '$tablename'\n";
108  $errors++;
109  }
110  else if($this->verbose)
111  {
112  echo "(+) found valid $columnname '".$row[$columnname]."' in table '$tablename'\n";
113  }
114  }
115  $this->dbManager->freeResult($res);
116  return $errors;
117  }
118 
124  private function checkExistsTable($tableName)
125  {
126  $error = intval(!$this->dbManager->existsTable($tableName));
127  if($error){
128  echo "(-) table $tableName does not exists";
129  }
130  else if($this->verbose)
131  {
132  echo "(+) table $tableName exists";
133  }
134  $this->errors += $error;
135  }
136 }
checkExistsTable($tableName)
checkDatabaseEnum($tablename, $columnname, $map)
Check if every values in given column are values from the given map.
check()
Check the sanity of decision, upload status, License Event types license_candidate table and ensures ...
checkDecisionScopes()
Check if clearing_decision have proper values in scope and decision_type columns.
checkLicenseEventTypes()
Check if clearing_event have proper values in type_fk column.
checkUploadStatus()
Check if upload_clearing have proper values in status_fk column.
int verbose
The verbose flag for the cli.
Definition: fo_cli.c:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16