folder restructuring, namespace adjustments

This commit is contained in:
Sascha Nitsch 2024-07-20 19:17:36 +02:00
parent 0d3a7c871b
commit c4596ace03
24 changed files with 134 additions and 111 deletions

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
composer.lock
vendor
php/version.php
php/federator/version.php
php-docs
.phpdoc
phpdoc

View file

@ -367,7 +367,5 @@ return [
// A list of individual files to include in analysis
// with a path relative to the root directory of the
// project.
'file_list' => [
'maintenance.php'
],
'file_list' => [],
];

View file

@ -9,8 +9,8 @@ path = '../templates/'
compiledir = '../cache'
[plugins]
rediscache = '../plugins/rediscache.php'
dummy = '../plugins/dummyconnector.php'
rediscache = 'rediscache.php'
dummy = 'dummyconnector.php'
[maintenance]
username = 'federatoradmin'

View file

@ -12,8 +12,6 @@ if (! array_key_exists('_call', $_REQUEST)) {
}
date_default_timezone_set("Europe/Berlin");
spl_autoload_register(static function (string $className) {
// strip Federator from class path
$className = str_replace('Federator\\', '', $className);
include '../php/' . str_replace("\\", "/", strtolower($className)) . '.php';
});

View file

@ -1,95 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
function dbupgrade(\Federator\Main $main)
{
$config = $main->getConfig();
$maintenance = $config['maintenance'];
$main->openDatabase($maintenance['username'], $maintenance['password']);
$dbh = $main->getDatabase();
if ($dbh === false) {
die();
}
$sql = 'select `value` from settings where `key`="database_version"';
try {
$sth = $dbh->query($sql);
} catch (mysqli_sql_exception) {
$sth = false;
}
$version = '0';
if ($sth instanceof mysqli_result) {
$row = $sth->fetch_row();
if (is_array($row)) {
$version = $row[0];
}
}
echo "current version: $version\n";
$updateFolder = opendir("sql");
if ($updateFolder === false) {
die();
}
$updates = [];
while (($file = readdir($updateFolder)) !== false) {
$matches = [];
if (preg_match('/^(\d{4}-\d{2}-\d{2}).sql$/', $file, $matches) == 1) {
if (strcmp($matches[1], $version) > 0) { // file is newer than our version
$updates[] = $file;
}
}
}
closedir($updateFolder);
sort($updates);
foreach ($updates as $update) {
echo "applying update $update\n";
$u = fopen('sql/' . $update, 'r');
if ($u !== false) {
try {
$dbh->begin_transaction();
// apply updates line by line
while (($line = fgets($u)) !== false) {
$line = trim($line);
$dbh->query($line);
}
$dbh->commit();
} catch (mysqli_sql_exception $e) {
//print_r($e);
echo "error while applying update $update: " . $e->getMessage() . "\n";
}
fclose($u);
}
}
}
function printUsage()
{
echo "usage php maintenance.php <command>\n";
echo "command can be one of:\n";
echo " dbupgrade - this upgrades the db to the most recent version. Run this after you updated the program files\n";
exit();
}
date_default_timezone_set("Europe/Berlin");
spl_autoload_register(static function (string $className) {
$root = array_key_exists('DOCUMENT_ROOT', $_SERVER) ? $_SERVER['DOCUMENT_ROOT'] : '';
// strip Federator from class path
$className = str_replace('Federator\\', '', $className);
include $root . 'php/' . str_replace("\\", "/", strtolower($className)) . '.php';
});
if ($argc < 2) {
printUsage();
}
$main = new \Federator\Main('');
switch ($argv[1]) {
case 'dbupgrade':
dbupgrade($main);
break;
default:
printUsage();
}

View file

@ -67,7 +67,7 @@ class User
/**
* check if use has asked permission
* @param string $p @unused-param
* @param string $p
* permission to check
*
* @return bool true if user has permission, false if not

View file

@ -79,11 +79,11 @@ class Main
/**
* constructor
*
* @param string $rootDir root directory
*/
public function __construct($rootDir = '../')
public function __construct()
{
$this->responseCode = 200;
$rootDir = $_SERVER['DOCUMENT_ROOT'] . '../';
$config = parse_ini_file($rootDir . 'config.ini', true);
if ($config !== false) {
$this->config = $config;
@ -141,9 +141,10 @@ class Main
public function loadPlugins() : void
{
if (array_key_exists('plugins', $this->config)) {
$basepath = $_SERVER['DOCUMENT_ROOT'] . '../plugins/federator/';
$plugins = $this->config['plugins'];
foreach ($plugins as $name => $file) {
require_once($file);
require_once($basepath . $file);
$fktn = 'Federator\\' . $name . '_load';
if (function_exists($fktn)) {
$fktn($this);

View file

@ -0,0 +1,121 @@
<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
namespace Federator;
class Maintenance
{
/**
* run maintenance
*
* @param int $argc number of arguments
* @param string[] $argv arguments
* @return void
*/
public static function run($argc, $argv)
{
date_default_timezone_set("Europe/Berlin");
spl_autoload_register(static function (string $className) {
$root = $_SERVER['DOCUMENT_ROOT'];
include $root . '../php/' . str_replace("\\", "/", strtolower($className)) . '.php';
});
if ($argc < 2) {
self::printUsage();
}
// pretend that we are running from web directory
$_SERVER['DOCUMENT_ROOT'] = realpath('../../htdocs') . '/';
$main = new \Federator\Main();
switch ($argv[1]) {
case 'dbupgrade':
self::dbupgrade($main);
break;
default:
self::printUsage();
}
}
/**
* upgrade database to newest version
*
* @param \Federator\Main $main main instance
* @return void
*/
public static function dbupgrade($main)
{
$config = $main->getConfig();
$maintenance = $config['maintenance'];
$main->openDatabase($maintenance['username'], $maintenance['password']);
$dbh = $main->getDatabase();
if ($dbh === false) {
die();
}
$sql = 'select `value` from settings where `key`="database_version"';
try {
$sth = $dbh->query($sql);
} catch (\mysqli_sql_exception) {
$sth = false;
}
$version = '0';
if ($sth instanceof \mysqli_result) {
$row = $sth->fetch_row();
if (is_array($row)) {
$version = $row[0];
}
}
echo "current version: $version\n";
$root = $_SERVER['DOCUMENT_ROOT'] . '../';
$updateFolder = opendir($root . 'sql');
if ($updateFolder === false) {
die();
}
$updates = [];
while (($file = readdir($updateFolder)) !== false) {
$matches = [];
if (preg_match('/^(\d{4}-\d{2}-\d{2}).sql$/', $file, $matches) == 1) {
if (strcmp($matches[1], $version) > 0) { // file is newer than our version
$updates[] = $file;
}
}
}
closedir($updateFolder);
sort($updates);
foreach ($updates as $update) {
echo "applying update $update\n";
$u = fopen($root . 'sql/' . $update, 'r');
if ($u !== false) {
try {
$dbh->begin_transaction();
// apply updates line by line
while (($line = fgets($u)) !== false) {
$line = trim($line);
$dbh->query($line);
}
$dbh->commit();
} catch (\mysqli_sql_exception $e) {
echo "error while applying update $update: " . $e->getMessage() . "\n";
}
fclose($u);
}
}
}
/**
* print usage of maintenance tool
*
* @return void
*/
public static function printUsage()
{
echo "usage php maintenance.php <command>\n";
echo "command can be one of:\n";
echo " dbupgrade - this upgrades the db to the most recent version. Run this after you updated the program files\n";
exit();
}
}
Maintenance::run($argc, $argv);

View file

@ -35,9 +35,9 @@ class RedisCache implements Cache
private $redis;
/**
* user cache time to life
* user cache time to live in secods
*
* @var int time to life in secons
* @var int $userTTL
*/
private $userTTL;

View file

@ -1,4 +1,4 @@
#!/bin/bash
git log | head -n1 | awk '{print "<?php\nglobal $version;\n$version=\"" $2 "\";\n"}' > php/version.php
vendor/phan/phan/phan
git log | head -n1 | awk '{print "<?php\nglobal $version;\n$version=\"" $2 "\";\n"}' > php/federator/version.php
vendor/bin/phan
./phpdoc