Skip to content

Commit

Permalink
[Tools] Enable PHPCS for config_to_db.php (aces#6796)
Browse files Browse the repository at this point in the history
Replaces aces#5988
  • Loading branch information
laemtl authored and AlexandraLivadas committed Jun 15, 2021
1 parent 2d65827 commit efb3f4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions test/run-php-linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ find docs modules htdocs php src tools \
declare -a tools_list=(
'assign_missing_instruments.php'
'configuration_check.php'
'config_to_db.php'
'data_integrity_check.php'
'delete_candidate.php'
'delete_ignored_conflicts.php'
Expand Down
57 changes: 30 additions & 27 deletions tools/config_to_db.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env php
<?php
/**
* script to move config.xml data to the database
* Script to move config.xml data to the database
*
* PHP version 5
* PHP version 7
*
* @category Main
* @package Loris
Expand All @@ -22,7 +22,7 @@
iterate($iterator, null);

/**
* iterate over the config xml
* Iterate over the config xml
*
* @param SimpleXmlIterator $iterator xml iterator
* @param string $parentKey parent of the current value of the iterator
Expand All @@ -34,29 +34,27 @@ function iterate($iterator, $parentKey)
$db = Database::singleton();
for ($iterator->rewind(); $iterator->valid(); $iterator->next()) {
$current = $iterator->current();
$name = $iterator->key();
$name = $iterator->key();
if ($iterator->hasChildren()) {
iterate($current, $name);
} else { // Else it is a leaf

// If a key by that name exists, get its ID
$configID = $db->pselectone(
"SELECT ID
FROM ConfigSettings
WHERE Name=:name",
array('name' => $name)
WHERE Name=:name",
['name' => $name]
);

// If the key already exists
if (!empty($configID)) {
$dbParentKey = $db->pselectone(
"SELECT Name
FROM ConfigSettings
WHERE ID=(SELECT Parent FROM ConfigSettings WHERE Name=:name)",
array('name' => $name)
WHERE ID=(SELECT Parent FROM ConfigSettings WHERE Name=:name)",
['name' => $name]
);
if ($parentKey==$dbParentKey) {

// Insert into the DB
processLeaf($name, $current, $configID);
}
Expand All @@ -66,7 +64,7 @@ function iterate($iterator, $parentKey)
}

/**
* insert a value into the Config table
* Insert a value into the Config table
*
* @param string $name name of the config field
* @param string $value value of the config field
Expand All @@ -76,48 +74,53 @@ function iterate($iterator, $parentKey)
*/
function processLeaf($name, $value, $configID)
{
$db = Database::singleton();
$db = Database::singleton();
$allowMultiple = $db->pselectone(
"SELECT AllowMultiple
FROM ConfigSettings
WHERE ID=:configID",
array('configID' => $configID)
WHERE ID=:configID",
['configID' => $configID]
);
$currentValue = $db->pselect(
"SELECT Value
FROM Config
WHERE ConfigID=:configID",
array('configID' => $configID)
WHERE ConfigID=:configID",
['configID' => $configID]
);

// if the configID is not already in the config table
if (empty($currentValue)) {

$db->insert(
'Config', array('ConfigID' => $configID, 'Value' => $value)
'Config',
[
'ConfigID' => $configID,
'Value' => $value,
]
);

} else if (!empty($currentValue) && $allowMultiple==0) {
} elseif (!empty($currentValue) && $allowMultiple==0) {
// if the configID exists and the field does not allow multiples

$db->update(
'Config', array('Value' => $value), array('ConfigID' => $configID)
'Config',
['Value' => $value],
['ConfigID' => $configID]
);

} else { // if the configID exists and the field does allow multiples

// if it is not a copy of an already existing value
if (!Recursive_In_array($value, $currentValue)) {
$db->insert(
'Config', array('ConfigID' => $configID, 'Value' => $value)
'Config',
[
'ConfigID' => $configID,
'Value' => $value,
]
);
}

}
}

/**
* recursive in_array function
* Recursive in_array function
*
* @param string $value the value being searched for
* @param array $array the array being searched through
Expand All @@ -127,7 +130,7 @@ function processLeaf($name, $value, $configID)
function Recursive_In_array($value, $array)
{
foreach ($array as $sub_array) {
if ($sub_array == $value
if ($sub_array == $value
|| (is_array($sub_array) && Recursive_In_array($value, $sub_array))
) {
return true;
Expand Down

0 comments on commit efb3f4e

Please sign in to comment.