Skip to content

Commit

Permalink
Improve Schema management workflow
Browse files Browse the repository at this point in the history
- Add try/catch to db_connection file and connection test within Manager class that reports when connection to database fails (e.g. no connection, authentication issue, etc)
- Sitemap adjustments that automatically forwards user to schema manager whenever schema is missing (e.g. fresh install).
- Add backward ticks to all table and stored procedure names within schema files so that installation logger is able to properly report table name within logs
- Misc adjustments to schema manager to improved output reports
- Add message to schema manager that warns user when log directories are not writable by web user (e.g. apache)
- Only check users accessibility settings if they are logged in to the portal
  • Loading branch information
egbot committed Aug 23, 2024
1 parent 7391079 commit c54952e
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 94 deletions.
22 changes: 18 additions & 4 deletions admin/schemamanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
label{ font-weight:bold; }
fieldset{ padding: 15px }
fieldset legend{ font-weight:bold; }
.info-div{ margin:10px 5px; }
.info-div{ margin:5px 5px 20px 5px; }
.form-section{ margin: 5px 10px; }
button{ margin: 15px; }
</style>
Expand Down Expand Up @@ -87,8 +87,22 @@
?>
<fieldset style="width:800px">
<legend>Database Schema Assistant</legend>
<div class="info-div">Enter login criteria for database user that has full DDL privileges (e.g. create/alter tables, routines, indexes, etc.).<br>
We recommend creating a backup of the database before applying any database patches.</div>
<div class="info-div">
Enter login for database user who has full DDL privileges (e.g. create/alter tables, routines, indexes, etc.).<br>
We recommend creating a backup of the database before applying any database patches.
</div>
<?php
if($curentVersion != '3.1'){
if(!is_writable($SERVER_ROOT . '/content/logsd/install/')){
?>
<div class="info-div">
<span style="color: orange">WARNING</span>: The log directory (e.g. /content/logs/schema/) is not writable by web user.
We strongly recommend that you adjust directory permissions as defined within the installation before running installation/update scripts.
</div>
<?php
}
}
?>
<form name="databaseMaintenanceForm" action="schemamanager.php" method="post">
<div class="form-section">
<label>Schema: </label>
Expand All @@ -99,7 +113,7 @@
foreach($schemaPatchArr as $schemaOption){
if($schemaOption > $curentVersion) echo '<option value="' . $schemaOption . '">Schema Patch ' . $schemaOption . '</option>';
}
echo '<option value="">Schema is Current - nothing to do</option>';
if($curentVersion == '3.1') echo '<option value="">Schema is Current - nothing to do</option>';
}
else{
echo '<option value="baseInstall">New Install (ver. 3.0)</option>';
Expand Down
1 change: 1 addition & 0 deletions classes/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct($id=null, $conType='readonly', $connOverride = null)
$this->isConnInherited = true;
}
else $this->conn = MySQLiConnectionFactory::getCon($conType);
if($this->conn === null) exit;
if($id != null || is_numeric($id)){
$this->id = $id;
}
Expand Down
64 changes: 32 additions & 32 deletions classes/ProfileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function register($postArr, $adminRegister = false){
$lastName = strip_tags($postArr['lastname']);
$pwd = $postArr['pwd'];
$email = filter_var($postArr['email'], FILTER_VALIDATE_EMAIL);

$title = array_key_exists('title', $postArr) ? strip_tags($postArr['title']) : '';
$institution = array_key_exists('institution', $postArr) ? strip_tags($postArr['institution']) : '';
$city = array_key_exists('city', $postArr) ? strip_tags($postArr['city']) : '';
Expand Down Expand Up @@ -1032,70 +1032,51 @@ private function getTempPath(){
return $tPath;
}

//setter and getters
public function setRememberMe($test){
$this->rememberMe = $test;
}

public function getRememberMe(){
return $this->rememberMe;
}

public function setToken($token){
$this->token = $token;
}

public function setUid($uid){
if(is_numeric($uid)){
$this->uid = $uid;
}
}

public function setAccessibilityPreference($pref, $uid){
//Accessubility functions
private function setAccessibilityPreference($pref, $uid){
$status = false;
$currentDynamicProperties = $this->getDynamicProperties($uid) ? $this->getDynamicProperties($uid) : array();
$currentDynamicProperties = $this->getDynamicProperties($uid);
if(!$currentDynamicProperties) $currentDynamicProperties = array();
$currentDynamicProperties['accessibilityPref'] = $pref;
$status = $this->setDynamicProperties($uid, $currentDynamicProperties);
return $status;
}

public function setDynamicProperties($uid, $dynPropArr){
private function setDynamicProperties($uid, $dynPropArr){
$status = false;
if(!$uid) return $status;

$jsonDynProps = json_encode($dynPropArr);


$this->resetConnection(); // @TODO decided whether this is necessary
$sql = 'UPDATE users SET dynamicProperties = ? WHERE (uid = ?)';
if($stmt = $this->conn->prepare($sql)){
$stmt->bind_param('si', $jsonDynProps, $uid);
$stmt->execute();
if(!$stmt->error) $status = true; // note: removed $stmt->affected_rows &&
if(!$stmt->error) $status = true; // note: removed $stmt->affected_rows &&
$stmt->close();
}
return $status;

}

public function getAccessibilityPreference($uid){
if(!$uid){
return false;
}
$returnVal = false;
$dynPropArr = $this->getDynamicProperties($uid);

if($dynPropArr && isset($dynPropArr['accessibilityPref'])){
$returnVal = ($dynPropArr['accessibilityPref'] === true) ? true : false;
}
return $returnVal;
}

public function getDynamicProperties($uid){
if(! $uid){
return false;
}
private function getDynamicProperties($uid){
$returnVal = false;
$sql = 'SELECT dynamicProperties FROM users WHERE uid = ?';
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("i", $uid);
$stmt->bind_param('i', $uid);
$stmt->execute();
$respns= $stmt->get_result();
if($fetchedObj = $respns->fetch_object()){
Expand Down Expand Up @@ -1133,4 +1114,23 @@ private function encodeArr(&$inArr,$cSet){
$inArr[$k] = $this->encodeString($v,$cSet);
}
}

//setter and getters
public function setRememberMe($test){
$this->rememberMe = $test;
}

public function getRememberMe(){
return $this->rememberMe;
}

public function setToken($token){
$this->token = $token;
}

public function setUid($uid){
if(is_numeric($uid)){
$this->uid = $uid;
}
}
}
12 changes: 6 additions & 6 deletions classes/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function installPatch(){
$this->logOrEcho($fragment, 1);
}
elseif(!$stmtType){
if(preg_match('/`([a-z]+)`/', $fragment, $m)){
if(preg_match('/`([a-zA-Z]+)`/', $fragment, $m)){
$targetTable = $m[1];
}
$stmtType = 'undefined';
Expand All @@ -67,10 +67,10 @@ public function installPatch(){
$this->setActiveTable($targetTable);
}
elseif(strpos($fragment, '/*!') === 0) $stmtType = 'Conditional statement';
elseif(preg_match('/^([A-Z\s]+)/', $fragment, $m)){
elseif(preg_match('/^([A-Z0-9_=\s]+)/', $fragment, $m)){
$stmtType = $m[1];
}
$this->logOrEcho('Type: ' . $stmtType . ($targetTable ? ' '.$targetTable : ''), 1);
$this->logOrEcho('Statement prefix: ' . $stmtType . ($targetTable ? ' '.$targetTable : ''), 1);
$sql = $fragment;
}
else{
Expand Down Expand Up @@ -115,7 +115,7 @@ public function installPatch(){
fwrite($this->amendmentFH, '# ERROR: '.$this->conn->error."\n\n");
fwrite($this->amendmentFH, $sql . "\n\n");
$this->logOrEcho('ERROR: ' . $this->conn->error, 2);
$this->logOrEcho('SQL: ' . $sql, 2);
//$this->logOrEcho('SQL: ' . $sql, 2);
//break;
}
}
Expand Down Expand Up @@ -209,8 +209,8 @@ private function setActiveTable($targetTable){
$rs->free();
}
catch(Exception $e){
$this->logOrEcho('ERROR: '.$this->conn->error, 2);
$this->logOrEcho($sql, 2);
//$this->logOrEcho('ERROR: '.$this->conn->error, 2);
//$this->logOrEcho($sql, 2);
}
}
}
Expand Down
43 changes: 21 additions & 22 deletions classes/SiteMapManager.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<?php
include_once($SERVER_ROOT.'/config/dbconnection.php');
include_once('Manager.php');

class SiteMapManager{
class SiteMapManager extends Manager{

private $conn;
private $collArr = array();
private $obsArr = array();
private $genObsArr = array();

function __construct() {
$this->conn = MySQLiConnectionFactory::getCon("readonly");
parent::__construct();
}

function __destruct(){
if(!($this->conn === false)) $this->conn->close();
parent::__destruct();
}

public function setCollectionList(){
global $USER_RIGHTS, $IS_ADMIN;
$adminArr = array();
$editorArr = array();
$sql = 'SELECT c.collid, CONCAT_WS(":",c.institutioncode, c.collectioncode) AS ccode, c.collectionname, c.colltype '.
'FROM omcollections c ';
$sql = 'SELECT collid, CONCAT_WS(":", institutioncode, collectioncode) AS ccode, collectionname, colltype FROM omcollections ';
if(!$IS_ADMIN){
if(array_key_exists("CollAdmin",$USER_RIGHTS)){
$adminArr = $USER_RIGHTS['CollAdmin'];
Expand All @@ -30,14 +28,14 @@ public function setCollectionList(){
$editorArr = $USER_RIGHTS['CollEditor'];
}
if($adminArr || $editorArr){
$sql .= 'WHERE (c.collid IN('.implode(',',array_merge($adminArr,$editorArr)).')) ';
$sql .= 'WHERE (collid IN('.implode(',',array_merge($adminArr,$editorArr)).')) ';
}
else{
$sql = '';
}
}
if($sql){
$sql .= "ORDER BY c.collectionname";
$sql .= "ORDER BY collectionname";
//echo "<div>".$sql."</div>";
$rs = $this->conn->query($sql);
if($rs){
Expand Down Expand Up @@ -100,12 +98,11 @@ public function getChecklistList($clArr){

public function getProjectList($projArr = ""){
$returnArr = Array();
$sql = 'SELECT p.pid, p.projname, p.managers FROM fmprojects p '.
'WHERE p.ispublic = 1 ';
$sql = 'SELECT pid, projname, managers FROM fmprojects WHERE ispublic = 1 ';
if($projArr){
$sql .= 'AND (p.pid IN('.implode(',',$projArr).')) ';
$sql .= 'AND (pid IN('.implode(',',$projArr).')) ';
}
$sql .= 'ORDER BY p.projname';
$sql .= 'ORDER BY projname';
//echo '<div>'.$sql.'</div>';
$rs = $this->conn->query($sql);
if($rs){
Expand Down Expand Up @@ -134,17 +131,19 @@ public function hasGlossary(){
* @return string representation of the most recently applied schema version
*/
public function getSchemaVersion() {
$result = "No Schema Version Found";
$sql = "select versionnumber, dateapplied from schemaversion order by dateapplied desc limit 1 ";
$statement = $this->conn->prepare($sql);
$statement->execute();
$statement->bind_result($version,$dateapplied);
while ($statement->fetch()) {
$result = $version;
$result = false;
$sql = 'SELECT versionnumber FROM schemaversion ORDER BY dateapplied DESC LIMIT 1 ';
try{
$statement = $this->conn->prepare($sql);
$statement->execute();
$statement->bind_result($result);
$statement->fetch();
$statement->close();
}
catch(Exception $e){
$this->errorMessage = $e->getMessage();
}
$statement->close();
return $result;
}

}
?>
19 changes: 11 additions & 8 deletions config/dbconnection_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ public static function getCon($type) {
for ($i = 0, $n = count(MySQLiConnectionFactory::$SERVERS); $i < $n; $i++) {
$server = MySQLiConnectionFactory::$SERVERS[$i];
if($server['type'] == $type){
$connection = new mysqli($server['host'], $server['username'], $server['password'], $server['database'], $server['port']);
if(mysqli_connect_errno()){
throw new Exception('Could not connect to any databases! Please try again later.');
}
if(isset($server['charset']) && $server['charset']) {
if(!$connection->set_charset($server['charset'])){
throw new Exception('Error loading character set '.$server['charset'].': '.$connection->error);
try{
$connection = new mysqli($server['host'], $server['username'], $server['password'], $server['database'], $server['port']);
if(isset($server['charset']) && $server['charset']) {
if(!$connection->set_charset($server['charset'])){
throw new Exception('Error loading character set '.$server['charset'].': '.$connection->error);
}
}
return $connection;
}
catch(Exception $e){
echo $e->getMessage();
return null;
}
return $connection;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion config/schema/3.0/db_schema-3.0.sql

Large diffs are not rendered by default.

Loading

0 comments on commit c54952e

Please sign in to comment.