FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
sanity_check.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2014 Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
25 
35 {
37  protected $dbManager;
39  protected $verbose;
41  protected $errors = 0;
42 
43  function __construct(&$dbManager,$verbose)
44  {
45  $this->dbManager = $dbManager;
46  $this->verbose = $verbose;
47  }
48 
55  public function check()
56  {
57  $this->checkDecisionScopes();
58  $this->checkUploadStatus();
59  $this->checkLicenseEventTypes();
60  $this->checkExistsTable('license_candidate');
61  $folderDao = new FolderDao($this->dbManager, $GLOBALS['container']->get('dao.user'), $GLOBALS['container']->get('dao.upload'));
62  $folderDao->ensureTopLevelFolder();
63 
64  return $this->errors;
65  }
66 
70  private function checkDecisionScopes()
71  {
72  $decScopes = new DecisionScopes();
73  $scopeMap = $decScopes->getMap();
74  $this->errors += $this->checkDatabaseEnum($tablename = 'clearing_decision', 'scope', $scopeMap);
75  $decTypes = new DecisionTypes();
76  $typeMap = $decTypes->getExtendedMap();
77  $this->errors += $this->checkDatabaseEnum($tablename = 'clearing_decision', 'decision_type', $typeMap);
78  }
79 
83  private function checkUploadStatus()
84  {
85  $uploadStatus = new UploadStatus();
86  $statusMap = $uploadStatus->getMap();
87  $this->errors += $this->checkDatabaseEnum($tablename = 'upload_clearing', 'status_fk', $statusMap);
88  }
89 
93  private function checkLicenseEventTypes()
94  {
95  $licenseEventTypes = new ClearingEventTypes();
96  $map = $licenseEventTypes->getMap();
97  $this->errors += $this->checkDatabaseEnum($tablename='clearing_event', 'type_fk', $map);
98  }
99 
107  private function checkDatabaseEnum($tablename,$columnname,$map)
108  {
109  $errors = 0;
110  $stmt = __METHOD__.".$tablename.$columnname";
111  $sql = "SELECT $columnname,count(*) FROM $tablename GROUP BY $columnname";
112  $this->dbManager->prepare($stmt,$sql);
113  $res = $this->dbManager->execute($stmt);
114  while($row = $this->dbManager->fetchArray($res))
115  {
116  if(!array_key_exists($row[$columnname], $map))
117  {
118  echo "(-) found invalid $columnname '".$row[$columnname]."' in table '$tablename'\n";
119  $errors++;
120  }
121  else if($this->verbose)
122  {
123  echo "(+) found valid $columnname '".$row[$columnname]."' in table '$tablename'\n";
124  }
125  }
126  $this->dbManager->freeResult($res);
127  return $errors;
128  }
129 
135  private function checkExistsTable($tableName)
136  {
137  $error = intval(!$this->dbManager->existsTable($tableName));
138  if($error){
139  echo "(-) table $tableName does not exists";
140  }
141  else if($this->verbose)
142  {
143  echo "(+) table $tableName exists";
144  }
145  $this->errors += $error;
146  }
147 }
checkExistsTable($tableName)
checkUploadStatus()
Check if upload_clearing have proper values in status_fk column.
int verbose
The verbose flag for the cli.
Definition: fo_cli.c:49
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
checkLicenseEventTypes()
Check if clearing_event have proper values in type_fk column.
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. ...