Skip to content

Commit

Permalink
Improve installation script and various small fixes (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
gartens authored Apr 24, 2024
1 parent 71525a8 commit 4b96de7
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .htaccess
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC,OR]
RewriteCond %{HTTP_HOST} ^
RewriteCond %{REQUEST_URI} !webroot/
RewriteCond %{REQUEST_URI} !^/webroot/
RewriteRule (.*) /webroot/$1 [L]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Systems evaluations are an important part of development and empirical research
* PHP 5.6 or higher
* Apache2 with active mod_rewrite
* Apache config `AllowOverride All` needs to be set
* Git


### Installation procedure
Expand Down
16 changes: 5 additions & 11 deletions api/v1/job.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function get() {
}
$filters = [];
if ($this->get['supports'] == 'demoall') { // demo mode to match all systems
// we acceppt all systems now
// we accept all systems now
} else {
$supports = Systems_Library::getArrayFromString($this->get['supports']);
$filters[] = new ContainFilter(Job::SYSTEM_ID, $supports);
Expand Down Expand Up @@ -103,22 +103,16 @@ public function get() {

if (isset($this->get['withLog']) && $this->get['withLog'] == true) {
$path = UPLOADED_DATA_PATH . '/log/' . $job->getId() . '.log';
if (file_exists($path)) {
// read log from file
$log = file_get_contents($path);
if ($log === false) {
$data->log = "";
} else {
$data->log = $log;
}
} else {
$log = Util::readFileContents($path);
if ($log === false) {
$data->log = "";
} else {
$data->log = $log;
}
}
$this->add($data);
}


public $post_access = Auth_Library::A_PUBLIC;

/**
Expand Down
6 changes: 5 additions & 1 deletion controllers/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ public function system() {
$this->view->assign('system', $system);
$systemLib = new System($system->getId());
$results = json_decode($systemLib->getResultsAll(), true);
$this->view->assign('results', $results['elements']);
if ($results === null) {
$this->view->assign('results', []);
} else {
$this->view->assign('results', $results['elements']);
}
$this->view->assign('identifier', $systemLib->getIdentifier());
$users = Factory::getUserFactory()->filter([]);
$this->view->assign('users', $users);
Expand Down
2 changes: 1 addition & 1 deletion controllers/project.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,4 @@ public function detail() {
throw new Exception("No project id provided!");
}
}
}
}
5 changes: 4 additions & 1 deletion core/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function loader($className) {
$folder = '/core/';
} else {
//parse out filename where class should be located
if (!str_contains($className, '_')) {
die("Invalid class name for autoload");
}
list($filename, $suffix) = preg_split('/_/', $className);

//select the folder where class should be located based on suffix
Expand Down Expand Up @@ -86,4 +89,4 @@ function loader($className) {
}
}

spl_autoload_register("loader");
spl_autoload_register("loader");
5 changes: 3 additions & 2 deletions core/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ class Element {
*/
public function __construct($path) {
$this->path = $path . "/";
if (!file_exists($this->path . "config.json")) {
$file = Util::readFileContents($this->path . "config.json");
if ($file === false) {
throw new Exception("No config available for element '$this->path'!");
}
$this->config = json_decode(file_get_contents($this->path . "config.json"), true);
$this->config = json_decode($file, true);
}

public function isValid() {
Expand Down
5 changes: 3 additions & 2 deletions core/plot.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class Plot {
*/
public function __construct($path) {
$this->path = $path . "/";
if (!file_exists($this->path . "config.json")) {
$file = Util::readFileContents($this->path . "config.json");
if ($file === false) {
throw new Exception("No config available for plot '$this->path'!");
}
$this->config = json_decode(file_get_contents($this->path . "config.json"), true);
$this->config = json_decode($file, true);
}

public function isValid() {
Expand Down
6 changes: 3 additions & 3 deletions core/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
$log->debug('query string: ' . $request);

// if $request is empty (user called www.domain.tld) show home
if ($request == '') {
if ($request === '' or $request === '/') {
$page = DEFAULT_CONTROLLER;
$action = DEFAULT_ACTION;
$getVars = [];
Expand Down Expand Up @@ -138,11 +138,11 @@
}

// check if function exists
if (!method_exists($controller, $action)) {
if ($action === null or !method_exists($controller, $action)) {
// function does not exist!
$found = false;
$log->debug('=> class found, but action does not exist: ' . $action);
die('=> class found, but action does not exist: ' . $action);
die('class found, but action does not exist: ' . $action);
}
$log->debug('controller found and instantiated: ' . $class);
} else {
Expand Down
44 changes: 25 additions & 19 deletions core/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public function __construct($systemId) {
throw new Exception("Failed to load system with ID $systemId!");
}
$this->path = SERVER_ROOT . "/webroot/systems/" . $this->model->getId() . "/";
if (!file_exists($this->path . "config.json")) {
$content = Util::readFileContents($this->path . System::CONFIG);
if ($content === false) {
throw new Exception("Failed to load config for system with ID " . $this->model->getId());
}
$this->config = json_decode(file_get_contents($this->path . System::CONFIG), true);
$this->config = json_decode($content, true);
}

public function getName() {
Expand Down Expand Up @@ -106,18 +107,19 @@ public function getViews() {
}

public function getParameters() {
if (!file_exists($this->path . System::PARAMETERS)) {
$contents = Util::readFileContents($this->path . System::PARAMETERS);
if ($contents === false) {
return "{}";
}
return file_get_contents($this->path . System::PARAMETERS);
return $contents;
}

public function getResultsAll($resultId = "") {
if (!file_exists($this->path . System::RESULTS)) {
$data = Util::readFileContents($this->path . System::RESULTS);
if ($data === false) {
$this->convertResults();
$data = Util::readFileContents($this->path . System::RESULTS);
}

$data = file_get_contents($this->path . System::RESULTS);
if ($resultId != "") {
$json = json_decode($data, true);
if (!isset($json["elements"][$resultId])) {
Expand All @@ -130,32 +132,36 @@ public function getResultsAll($resultId = "") {
}

private function convertResults() {
if (!file_exists($this->path . System::RESULTS_JOB)) {
$dataJob = Util::readFileContents($this->path . System::RESULTS_JOB);
$dataAll = Util::readFileContents($this->path . System::RESULTS_ALL);
if ($dataJob === false) {
$dataJob = "{}";
} else {
$dataJob = file_get_contents($this->path . System::RESULTS_JOB);
}
if (!file_exists($this->path . System::RESULTS_ALL)) {
if ($dataAll === false) {
$dataAll = "{}";
} else {
$dataAll = file_get_contents($this->path . System::RESULTS_ALL);
}
$jsonJob = json_decode($dataJob, true);
$jsonAll = json_decode($dataAll, true);

$json = ["version" => "1.0", "elements" => ["system-1" => ["job" => $jsonJob, "all" => $jsonAll, "name" => "Default"]]];
$this->setResultsAll(json_encode($json));
unlink($this->path . System::RESULTS_ALL);
unlink($this->path . System::RESULTS_JOB);
sleep(1);
if (file_exists($this->path . System::RESULTS_ALL)) {
unlink($this->path . System::RESULTS_ALL);
}
if (file_exists($this->path . System::RESULTS_JOB)) {
unlink($this->path . System::RESULTS_JOB);
}
sleep(1);
}

public function getResultsJob($resultId = "") {
if (!file_exists($this->path . System::RESULTS)) {
$data = file_get_contents($this->path . System::RESULTS);

if ($data === false) {
$this->convertResults();
$data = file_get_contents($this->path . System::RESULTS);
}

$data = file_get_contents($this->path . System::RESULTS);
if ($resultId != "") {
$json = json_decode($data, true);
if (!isset($json["elements"][$resultId])) {
Expand Down Expand Up @@ -219,4 +225,4 @@ public function deleteResults($resultId) {
file_put_contents($this->path . System::RESULTS, json_encode($data));
VCS_Library::commit($this->path, "Deleted results ID: $resultId");
}
}
}
25 changes: 12 additions & 13 deletions core/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static function mergeJobs($jobs, $parameter) {
* @throws Exception
*/
public static function scanForPlots($path) {
if (!file_exists($path) || !is_dir($path)) {
if (!is_dir($path)) {
return [];
}
$dir = scandir($path);
Expand Down Expand Up @@ -245,6 +245,16 @@ public static function recursiveCopy($src, $dst) {
closedir($dir);
}

public static function readFileContents(string $file): string|false {
// There is still a window where the file can be deleted, and
// file_get_contents prints a warning to the page. But when there
// is a way without that, only this helper needs to be changed.
if (file_exists($file)) {
return file_get_contents($file);
}
return false;
}

public static function getObjectFromPhasesBitMask($bitMask, $obj = null) {
if ($obj == null) {
$obj = new stdClass();
Expand Down Expand Up @@ -456,15 +466,4 @@ public static function getNextIdForSystemResults($systemId) {
return $newId;
}

public static function updateSumsAndCounts($array, &$sums, &$counts) {
foreach ($array as $index => $value) {
if (isset($sums[$index])) {
$sums[$index] += $value;
$counts[$index] += 1;
} else {
$sums[$index] = $value;
$counts[$index] = 1;
}
}
}
}
}
Loading

0 comments on commit 4b96de7

Please sign in to comment.