forked from grumpydevelop/federator
		
	- added php-resque for queue job and worker support - removed all document_root dependencies (as that doesn't work in the queue-environment - added project_root global variable for more robust setup - some file-constistency fixed - config referring paths are now starting in project-root - inbox now does the heavy-duty on a queue
		
			
				
	
	
		
			125 lines
		
	
	
	
		
			3.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			125 lines
		
	
	
	
		
			3.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?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;
 | 
						|
 | 
						|
/**
 | 
						|
 * maintenance functions
 | 
						|
 */
 | 
						|
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) {
 | 
						|
            include PROJECT_ROOT . '/php/' . str_replace("\\", "/", strtolower($className)) . '.php';
 | 
						|
        });
 | 
						|
        if ($argc < 2) {
 | 
						|
            self::printUsage();
 | 
						|
        }
 | 
						|
        // pretend that we are running from web directory
 | 
						|
        define('PROJECT_ROOT', dirname(__DIR__, 2));
 | 
						|
        $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 = PROJECT_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.\n";
 | 
						|
        echo " Run this after you updated the program files\n";
 | 
						|
        exit();
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
Maintenance::run($argc, $argv);
 |