split www stuff to index.php and test things to tests/bootstrap.php

This commit is contained in:
Shish 2020-01-27 19:28:58 +00:00
parent eb9d63c2a2
commit 7e43e2e304
7 changed files with 71 additions and 83 deletions

View file

@ -1,26 +0,0 @@
<?php declare(strict_types=1);
/*
* Load all the files into memory, sanitise the environment, but don't
* actually do anything as far as the app is concerned
*/
global $cache, $config, $database, $user, $page, $_tracer;
require_once "core/sys_config.php";
require_once "core/polyfills.php";
require_once "core/util.php";
require_once "vendor/autoload.php";
_sanitise_environment();
$_tracer->begin("Bootstrap");
_load_core_files();
$cache = new Cache(CACHE_DSN);
$database = new Database(DATABASE_DSN);
$config = new DatabaseConfig($database);
ExtensionInfo::load_all_extension_info();
Extension::determine_enabled_extensions();
require_all(zglob("ext/{".Extension::get_enabled_extensions_as_string()."}/main.php"));
_load_theme_files();
$page = new Page();
_load_event_listeners();
$_tracer->end();

View file

@ -64,7 +64,7 @@ class Database
private function connect_engine(): void private function connect_engine(): void
{ {
if (preg_match("/^([^:]*)/", DATABASE_DSN, $matches)) { if (preg_match("/^([^:]*)/", $this->dsn, $matches)) {
$db_proto=$matches[1]; $db_proto=$matches[1];
} else { } else {
throw new SCoreException("Can't figure out database engine"); throw new SCoreException("Can't figure out database engine");

View file

@ -1,22 +1,15 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
/*
* First, load the user-specified settings
*/
@include_once "data/config/shimmie.conf.php";
@include_once "data/config/extensions.conf.php";
/** /**
* For any values that aren't defined in the above files, Shimmie * For any values that aren't defined in data/config/*.php,
* will set the values to their defaults * Shimmie will set the values to their defaults
* *
* All of these can be over-ridden by placing a 'define' in data/config/shimmie.conf.php * All of these can be over-ridden by placing a 'define' in
* data/config/shimmie.conf.php
* *
* Do NOT change them in this file. These are the defaults only! * Do NOT change them in this file. These are the defaults only!
* *
* Example: * Example:
* define("SPEED_HAX", true); * define("SPEED_HAX", true);
*
*/ */
function _d(string $name, $value): void function _d(string $name, $value): void

View file

@ -20,10 +20,6 @@ class Upgrade extends Extension
{ {
global $config, $database; global $config, $database;
if ($config->get_bool("in_upgrade")) {
return;
}
if (!is_numeric($config->get_string("db_version"))) { if (!is_numeric($config->get_string("db_version"))) {
$this->set_version("db_version", 2); $this->set_version("db_version", 2);
} }
@ -32,19 +28,14 @@ class Upgrade extends Extension
// now done again as v9 with PDO // now done again as v9 with PDO
if ($this->get_version("db_version") < 8) { if ($this->get_version("db_version") < 8) {
$config->set_bool("in_upgrade", true);
$database->execute($database->scoreql_to_sql( $database->execute($database->scoreql_to_sql(
"ALTER TABLE images ADD COLUMN locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N" "ALTER TABLE images ADD COLUMN locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N"
)); ));
$this->set_version("db_version", 8); $this->set_version("db_version", 8);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 9) { if ($this->get_version("db_version") < 9) {
$config->set_bool("in_upgrade", true);
if ($database->get_driver_name() == DatabaseDriver::MYSQL) { if ($database->get_driver_name() == DatabaseDriver::MYSQL) {
$tables = $database->get_col("SHOW TABLES"); $tables = $database->get_col("SHOW TABLES");
foreach ($tables as $table) { foreach ($tables as $table) {
@ -54,34 +45,25 @@ class Upgrade extends Extension
} }
$this->set_version("db_version", 9); $this->set_version("db_version", 9);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 10) { if ($this->get_version("db_version") < 10) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Adding foreign keys to images"); log_info("upgrade", "Adding foreign keys to images");
$database->Execute("ALTER TABLE images ADD FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE RESTRICT"); $database->Execute("ALTER TABLE images ADD FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE RESTRICT");
$this->set_version("db_version", 10); $this->set_version("db_version", 10);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 11) { if ($this->get_version("db_version") < 11) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Converting user flags to classes"); log_info("upgrade", "Converting user flags to classes");
$database->execute("ALTER TABLE users ADD COLUMN class VARCHAR(32) NOT NULL default :user", ["user" => "user"]); $database->execute("ALTER TABLE users ADD COLUMN class VARCHAR(32) NOT NULL default :user", ["user" => "user"]);
$database->execute("UPDATE users SET class = :name WHERE id=:id", ["name"=>"anonymous", "id"=>$config->get_int('anon_id')]); $database->execute("UPDATE users SET class = :name WHERE id=:id", ["name"=>"anonymous", "id"=>$config->get_int('anon_id')]);
$database->execute("UPDATE users SET class = :name WHERE admin=:admin", ["name"=>"admin", "admin"=>'Y']); $database->execute("UPDATE users SET class = :name WHERE admin=:admin", ["name"=>"admin", "admin"=>'Y']);
$this->set_version("db_version", 11); $this->set_version("db_version", 11);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 12) { if ($this->get_version("db_version") < 12) {
$config->set_bool("in_upgrade", true);
if ($database->get_driver_name() == DatabaseDriver::PGSQL) { if ($database->get_driver_name() == DatabaseDriver::PGSQL) {
log_info("upgrade", "Changing ext column to VARCHAR"); log_info("upgrade", "Changing ext column to VARCHAR");
$database->execute("ALTER TABLE images ALTER COLUMN ext SET DATA TYPE VARCHAR(4)"); $database->execute("ALTER TABLE images ALTER COLUMN ext SET DATA TYPE VARCHAR(4)");
@ -91,12 +73,9 @@ class Upgrade extends Extension
$database->execute("UPDATE images SET ext = LOWER(ext)"); $database->execute("UPDATE images SET ext = LOWER(ext)");
$this->set_version("db_version", 12); $this->set_version("db_version", 12);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 13) { if ($this->get_version("db_version") < 13) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Changing password column to VARCHAR(250)"); log_info("upgrade", "Changing password column to VARCHAR(250)");
if ($database->get_driver_name() == DatabaseDriver::PGSQL) { if ($database->get_driver_name() == DatabaseDriver::PGSQL) {
$database->execute("ALTER TABLE users ALTER COLUMN pass SET DATA TYPE VARCHAR(250)"); $database->execute("ALTER TABLE users ALTER COLUMN pass SET DATA TYPE VARCHAR(250)");
@ -105,12 +84,9 @@ class Upgrade extends Extension
} }
$this->set_version("db_version", 13); $this->set_version("db_version", 13);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 14) { if ($this->get_version("db_version") < 14) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Changing tag column to VARCHAR(255)"); log_info("upgrade", "Changing tag column to VARCHAR(255)");
if ($database->get_driver_name() == DatabaseDriver::PGSQL) { if ($database->get_driver_name() == DatabaseDriver::PGSQL) {
$database->execute('ALTER TABLE tags ALTER COLUMN tag SET DATA TYPE VARCHAR(255)'); $database->execute('ALTER TABLE tags ALTER COLUMN tag SET DATA TYPE VARCHAR(255)');
@ -123,12 +99,9 @@ class Upgrade extends Extension
} }
$this->set_version("db_version", 14); $this->set_version("db_version", 14);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 15) { if ($this->get_version("db_version") < 15) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Adding lower indexes for postgresql use"); log_info("upgrade", "Adding lower indexes for postgresql use");
if ($database->get_driver_name() == DatabaseDriver::PGSQL) { if ($database->get_driver_name() == DatabaseDriver::PGSQL) {
$database->execute('CREATE INDEX tags_lower_tag_idx ON tags ((lower(tag)))'); $database->execute('CREATE INDEX tags_lower_tag_idx ON tags ((lower(tag)))');
@ -136,12 +109,9 @@ class Upgrade extends Extension
} }
$this->set_version("db_version", 15); $this->set_version("db_version", 15);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 16) { if ($this->get_version("db_version") < 16) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Adding tag_id, image_id index to image_tags"); log_info("upgrade", "Adding tag_id, image_id index to image_tags");
$database->execute('CREATE UNIQUE INDEX image_tags_tag_id_image_id_idx ON image_tags(tag_id,image_id) '); $database->execute('CREATE UNIQUE INDEX image_tags_tag_id_image_id_idx ON image_tags(tag_id,image_id) ');
@ -158,12 +128,9 @@ class Upgrade extends Extension
// SQLite doesn't support altering existing columns? This seems like a problem? // SQLite doesn't support altering existing columns? This seems like a problem?
$this->set_version("db_version", 16); $this->set_version("db_version", 16);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 17) { if ($this->get_version("db_version") < 17) {
$config->set_bool("in_upgrade", true);
log_info("upgrade", "Adding media information columns to images table"); log_info("upgrade", "Adding media information columns to images table");
$database->execute($database->scoreql_to_sql( $database->execute($database->scoreql_to_sql(
"ALTER TABLE images ADD COLUMN lossless SCORE_BOOL NULL" "ALTER TABLE images ADD COLUMN lossless SCORE_BOOL NULL"
@ -197,7 +164,6 @@ class Upgrade extends Extension
$database->execute('CREATE INDEX images_ext_idx ON images(ext)'); $database->execute('CREATE INDEX images_ext_idx ON images(ext)');
$this->set_version("db_version", 17); $this->set_version("db_version", 17);
$config->set_bool("in_upgrade", false);
} }
if ($this->get_version("db_version") < 18) { if ($this->get_version("db_version") < 18) {
@ -215,7 +181,6 @@ class Upgrade extends Extension
$database->execute($database->scoreql_to_sql("UPDATE images SET audio = SCORE_BOOL_N WHERE ext IN ('webp')")); $database->execute($database->scoreql_to_sql("UPDATE images SET audio = SCORE_BOOL_N WHERE ext IN ('webp')"));
$database->execute($database->scoreql_to_sql("UPDATE images SET lossless = SCORE_BOOL_N, video = SCORE_BOOL_Y WHERE ext IN ('flv','mp4','m4v','ogv','webm')")); $database->execute($database->scoreql_to_sql("UPDATE images SET lossless = SCORE_BOOL_N, video = SCORE_BOOL_Y WHERE ext IN ('flv','mp4','m4v','ogv','webm')"));
$this->set_version("db_version", 18); $this->set_version("db_version", 18);
$config->set_bool("in_upgrade", false);
} }
} }

View file

@ -43,6 +43,10 @@
* Each of these can be imported at the start of a function with eg "global $page, $user;" * Each of these can be imported at the start of a function with eg "global $page, $user;"
*/ */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Make sure that shimmie is correctly installed *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
if (!file_exists("data/config/shimmie.conf.php")) { if (!file_exists("data/config/shimmie.conf.php")) {
require_once "core/_install.php"; require_once "core/_install.php";
exit; exit;
@ -79,7 +83,39 @@ EOD;
exit; exit;
} }
require_once "core/_bootstrap.php"; require_once "vendor/autoload.php";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Load files *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@include_once "data/config/shimmie.conf.php";
@include_once "data/config/extensions.conf.php";
require_once "core/sys_config.php";
require_once "core/polyfills.php";
require_once "core/util.php";
global $cache, $config, $database, $user, $page, $_tracer;
_sanitise_environment();
$_tracer->begin("Bootstrap");
_load_core_files();
$cache = new Cache(CACHE_DSN);
$database = new Database(DATABASE_DSN);
$config = new DatabaseConfig($database);
ExtensionInfo::load_all_extension_info();
Extension::determine_enabled_extensions();
require_all(zglob("ext/{".Extension::get_enabled_extensions_as_string()."}/main.php"));
_load_theme_files();
$page = new Page();
_load_event_listeners();
$_tracer->end();
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Send events, display output *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//$_tracer->mark(@$_SERVER["REQUEST_URI"]); //$_tracer->mark(@$_SERVER["REQUEST_URI"]);
$_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); $_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request");

View file

@ -1,14 +1,29 @@
<?php <?php
define("UNITTEST", true); chdir(dirname(dirname(__FILE__)));
define("TIMEZONE", 'UTC'); require_once "vendor/autoload.php";
define("EXTRA_EXTS", str_replace("ext/", "", implode(',', glob('ext/*')))); require_once "tests/defines.php";
define("BASE_HREF", "/"); require_once "core/sys_config.php";
define("CLI_LOG_LEVEL", 50); require_once "core/polyfills.php";
require_once "core/util.php";
$_SERVER['QUERY_STRING'] = '/'; $_SERVER['QUERY_STRING'] = '/';
chdir(dirname(dirname(__FILE__))); global $cache, $config, $database, $user, $page, $_tracer;
require_once "core/_bootstrap.php"; _sanitise_environment();
_load_core_files();
$cache = new Cache(CACHE_DSN);
$id = bin2hex(random_bytes(5));
$database = new Database("sqlite:data/shimmie.test.$id.sqlite");
create_dirs();
create_tables($database);
$config = new DatabaseConfig($database);
ExtensionInfo::load_all_extension_info();
Extension::determine_enabled_extensions();
require_all(zglob("ext/{".Extension::get_enabled_extensions_as_string()."}/main.php"));
_load_theme_files();
$page = new Page();
_load_event_listeners();
if (AUTO_DB_UPGRADE) { if (AUTO_DB_UPGRADE) {
send_event(new DatabaseUpgradeEvent()); send_event(new DatabaseUpgradeEvent());
} }

View file

@ -1,4 +1,7 @@
<?php <?php
define("UNITTEST", true);
define("EXTRA_EXTS", str_replace("ext/", "", implode(',', glob('ext/*'))));
define("DATABASE_DSN", null); define("DATABASE_DSN", null);
define("DATABASE_TIMEOUT", 10000); define("DATABASE_TIMEOUT", 10000);
define("CACHE_DSN", null); define("CACHE_DSN", null);
@ -12,8 +15,6 @@ define("NICE_URLS", false);
define("SEARCH_ACCEL", false); define("SEARCH_ACCEL", false);
define("WH_SPLITS", 1); define("WH_SPLITS", 1);
define("VERSION", '2.8-dev'); define("VERSION", '2.8-dev');
define("TIMEZONE", null);
define("EXTRA_EXTS", "");
define("BASE_URL", null); define("BASE_URL", null);
define("MIN_PHP_VERSION", '7.3'); define("MIN_PHP_VERSION", '7.3');
define("TRACE_FILE", null); define("TRACE_FILE", null);
@ -21,3 +22,7 @@ define("TRACE_THRESHOLD", 0.0);
define("ENABLED_MODS", "imageboard"); define("ENABLED_MODS", "imageboard");
define("SCORE_VERSION", 'develop/'.VERSION); define("SCORE_VERSION", 'develop/'.VERSION);
define("AUTO_DB_UPGRADE", true); define("AUTO_DB_UPGRADE", true);
define("TIMEZONE", 'UTC');
define("BASE_HREF", "/");
define("CLI_LOG_LEVEL", 50);