diff --git a/core/imageboard/search.php b/core/imageboard/search.php index b89b70a6..c43c368e 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -84,7 +84,7 @@ class Search */ private static function find_images_internal(int $start = 0, ?int $limit = null, array $tags = []): \FFSPHP\PDOStatement { - global $database, $user; + global $config, $database, $user; if ($start < 0) { $start = 0; @@ -93,9 +93,10 @@ class Search $limit = 1; } - if (SPEED_HAX) { - if (!$user->can(Permissions::BIG_SEARCH) and count($tags) > 3) { - throw new PermissionDenied("Anonymous users may only search for up to 3 tags at a time"); + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_int(SpeedHaxConfig::BIG_SEARCH) > 0) { + $anon_limit = $config->get_int(SpeedHaxConfig::BIG_SEARCH); + if (!$user->can(Permissions::BIG_SEARCH) and count($tags) > $anon_limit) { + throw new PermissionDenied("Anonymous users may only search for up to $anon_limit tags at a time"); } } @@ -182,15 +183,16 @@ class Search */ public static function count_images(array $tags = []): int { - global $cache, $database; + global $cache, $config, $database; $tag_count = count($tags); - // SPEED_HAX ignores the fact that extensions can add img_conditions + // speed_hax ignores the fact that extensions can add img_conditions // even when there are no tags being searched for - if (SPEED_HAX && $tag_count === 0) { + $speed_hax = (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::LIMIT_COMPLEX)); + if ($speed_hax && $tag_count === 0) { // total number of images in the DB $total = self::count_total_images(); - } elseif (SPEED_HAX && $tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { + } elseif ($speed_hax && $tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { if (!str_starts_with($tags[0], "-")) { // one positive tag - we can look that up directly $total = self::count_tag($tags[0]); @@ -207,7 +209,7 @@ class Search [$tag_conditions, $img_conditions, $order] = self::terms_to_conditions($tags); $querylet = self::build_search_querylet($tag_conditions, $img_conditions, null); $total = (int)$database->get_one("SELECT COUNT(*) AS cnt FROM ($querylet->sql) AS tbl", $querylet->variables); - if (SPEED_HAX && $total > 5000) { + if ($speed_hax && $total > 5000) { // when we have a ton of images, the count // won't change dramatically very often $cache->set($cache_key, $total, 3600); diff --git a/core/permissions.php b/core/permissions.php index a4123099..a1b7d0d1 100644 --- a/core/permissions.php +++ b/core/permissions.php @@ -20,7 +20,7 @@ abstract class Permissions public const CHANGE_USER_SETTING = "change_user_setting"; public const CHANGE_OTHER_USER_SETTING = "change_other_user_setting"; - /** search for more than 3 tags at once (only applies if SPEED_HAX is active) */ + /** search for more than 3 tags at once (only applies if Speed Hax is active) */ public const BIG_SEARCH = "big_search"; /** enable or disable extensions */ diff --git a/core/send_event.php b/core/send_event.php index 553a4b15..ce3dad8e 100644 --- a/core/send_event.php +++ b/core/send_event.php @@ -19,18 +19,19 @@ $_shm_event_listeners = []; function _load_event_listeners(): void { - global $_shm_event_listeners; + global $_shm_event_listeners, $config; $ver = preg_replace("/[^a-zA-Z0-9\.]/", "_", VERSION); $key = md5(Extension::get_enabled_extensions_as_string()); + $speed_hax = (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::CACHE_EVENT_LISTENERS)); $cache_path = data_path("cache/event_listeners/el.$ver.$key.php"); - if (SPEED_HAX && file_exists($cache_path)) { + if ($speed_hax && file_exists($cache_path)) { require_once($cache_path); } else { _set_event_listeners(); - if (SPEED_HAX) { + if ($speed_hax) { _dump_event_listeners($_shm_event_listeners, $cache_path); } } diff --git a/core/sys_config.php b/core/sys_config.php index 8c286d2c..f89fc094 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -14,7 +14,7 @@ namespace Shimmie2; * Do NOT change them in this file. These are the defaults only! * * Example: - * define("SPEED_HAX", true); + * define("DEBUG", true); */ function _d(string $name, mixed $value): void @@ -29,7 +29,6 @@ _d("DATABASE_TIMEOUT", 10000); // int Time to wait for each statement to c _d("CACHE_DSN", null); // string cache connection details _d("DEBUG", false); // boolean print various debugging details _d("COOKIE_PREFIX", 'shm'); // string if you run multiple galleries with non-shared logins, give them different prefixes -_d("SPEED_HAX", false); // boolean do some questionable things in the name of performance _d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse _d("VERSION", "2.12.0-alpha"); // string shimmie version _d("TIMEZONE", null); // string timezone diff --git a/core/urls.php b/core/urls.php index b6ecca4d..4b0307cf 100644 --- a/core/urls.php +++ b/core/urls.php @@ -55,7 +55,7 @@ function make_link(?string $page = null, ?string $query = null, ?string $fragmen $parts = []; $install_dir = get_base_href(); - if (SPEED_HAX || $config->get_bool(SetupConfig::NICE_URLS, false)) { + if ($config->get_bool(SetupConfig::NICE_URLS, false)) { $parts['path'] = "$install_dir/$page"; } else { $parts['path'] = "$install_dir/index.php"; diff --git a/ext/comment/main.php b/ext/comment/main.php index ddb77b66..9285af4e 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -236,7 +236,8 @@ class CommentList extends Extension if ($event->page_matches("comment/list", paged: true)) { $threads_per_page = 10; - $where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : ""; + $speed_hax = (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::CACHE_TAG_LISTS)); + $where = $speed_hax ? "WHERE posted > now() - interval '24 hours'" : ""; $total_pages = cache_get_or_set("comment_pages", fn () => (int)ceil($database->get_one(" SELECT COUNT(c1) diff --git a/ext/index/main.php b/ext/index/main.php index d1bbfdf3..3fa85aa7 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -43,11 +43,12 @@ class Index extends Extension $page_number = $event->get_iarg('page_num', 1); $page_size = $config->get_int(IndexConfig::IMAGES); + $speed_hax = (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::FAST_PAGE_LIMIT)); $fast_page_limit = 500; $ua = $_SERVER["HTTP_USER_AGENT"] ?? "No UA"; if ( - SPEED_HAX + $speed_hax && ( str_contains($ua, "Googlebot") || str_contains($ua, "YandexBot") @@ -63,7 +64,7 @@ class Index extends Extension $fast_page_limit = 10; } - if (SPEED_HAX && $page_number > $fast_page_limit && !$user->can("big_search")) { + if ($speed_hax && $page_number > $fast_page_limit && !$user->can("big_search")) { throw new PermissionDenied( "Only $fast_page_limit pages of results are searchable - " . "if you want to find older results, use more specific search terms" @@ -71,12 +72,12 @@ class Index extends Extension } $total_pages = (int)ceil(Search::count_images($search_terms) / $config->get_int(IndexConfig::IMAGES)); - if (SPEED_HAX && $total_pages > $fast_page_limit && !$user->can("big_search")) { + if ($speed_hax && $total_pages > $fast_page_limit && !$user->can("big_search")) { $total_pages = $fast_page_limit; } $images = null; - if (SPEED_HAX) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::CACHE_FIRST_FEW)) { if ($count_search_terms === 0 && ($page_number < 10)) { // extra caching for the first few post/list pages $images = cache_get_or_set( diff --git a/ext/rss_images/main.php b/ext/rss_images/main.php index 85d8ce8d..e35ee3da 100644 --- a/ext/rss_images/main.php +++ b/ext/rss_images/main.php @@ -31,7 +31,7 @@ class RSSImages extends Extension $search_terms = Tag::explode($event->get_arg('search', "")); $page_number = $event->get_iarg('page_num', 1); $page_size = $config->get_int(IndexConfig::IMAGES); - if (SPEED_HAX && $page_number > 9) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::RSS_LIMIT) && $page_number > 9) { return; } $images = Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms); diff --git a/ext/speed_hax/info.php b/ext/speed_hax/info.php new file mode 100644 index 00000000..313478e8 --- /dev/null +++ b/ext/speed_hax/info.php @@ -0,0 +1,33 @@ + self::SHISH_EMAIL, "jgen" => "jgen.tech@gmail.com", "Matthew Barbour" => "matthew@darkholme.net", "Discomrade" => ""]; + public string $license = self::LICENSE_GPLV2; + public ExtensionCategory $category = ExtensionCategory::ADMIN; + public string $description = "Show performance tweak options. Read the documentation."; + public ?string $documentation = +"Many of these changes reduce the correctness of the software and increase admin workload for the sake of speed. You almost certainly don't want to set some of them, but if you do (e.g. you're trying to run a site with 10,000 concurrent users on a single server), it can be a huge help. +

+ +"; +} diff --git a/ext/speed_hax/main.php b/ext/speed_hax/main.php new file mode 100644 index 00000000..9f81a37a --- /dev/null +++ b/ext/speed_hax/main.php @@ -0,0 +1,55 @@ +set_default_bool(SpeedHaxConfig::NO_AUTO_DB_UPGRADE, false); + $config->set_default_bool(SpeedHaxConfig::CACHE_EVENT_LISTENERS, false); + $config->set_default_bool(SpeedHaxConfig::CACHE_TAG_LISTS, false); + $config->set_default_bool(SpeedHaxConfig::PURGE_COOKIE, false); + $config->set_default_bool(SpeedHaxConfig::RECENT_COMMENTS, false); + $config->set_default_int(SpeedHaxConfig::BIG_SEARCH, 0); + $config->set_default_bool(SpeedHaxConfig::LIMIT_COMPLEX, false); + $config->set_default_bool(SpeedHaxConfig::FAST_PAGE_LIMIT, false); + $config->set_default_bool(SpeedHaxConfig::CACHE_FIRST_FEW, false); + $config->set_default_bool(SpeedHaxConfig::RSS_LIMIT, false); + } + + public function onSetupBuilding(SetupBuildingEvent $event): void + { + $sb = $event->panel->create_new_block("Speed Hax"); + $sb->start_table(); + $sb->add_bool_option(SpeedHaxConfig::NO_AUTO_DB_UPGRADE, "Don't auto-upgrade database: ", false); + $sb->add_bool_option(SpeedHaxConfig::CACHE_EVENT_LISTENERS, "
Cache event listeners: ", false); + $sb->add_bool_option(SpeedHaxConfig::CACHE_TAG_LISTS, "
Cache tag lists: ", false); + $sb->add_bool_option(SpeedHaxConfig::PURGE_COOKIE, "
Purge cookie on logout: ", false); + $sb->add_bool_option(SpeedHaxConfig::RECENT_COMMENTS, "
List only recent comments: ", false); + $sb->add_int_option(SpeedHaxConfig::BIG_SEARCH, "
Anonymous search tag limit: ", false); + $sb->add_bool_option(SpeedHaxConfig::LIMIT_COMPLEX, "
Limit complex searches: ", false); + $sb->add_bool_option(SpeedHaxConfig::FAST_PAGE_LIMIT, "
Fast page limit: ", false); + $sb->add_bool_option(SpeedHaxConfig::CACHE_FIRST_FEW, "
Extra caching on first pages: ", false); + $sb->add_bool_option(SpeedHaxConfig::RSS_LIMIT, "
Limit images RSS: ", false); + $sb->end_table(); + } +} diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index 5e6fba44..fa46ed6d 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -275,7 +275,7 @@ class TagList extends Extension $html .= " $h_tag_no_underscores \n"; } - if (SPEED_HAX) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::CACHE_TAG_LISTS)) { file_put_contents($cache_key, $html); } @@ -349,7 +349,7 @@ class TagList extends Extension $html .= "$h_tag\n"; } - if (SPEED_HAX) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::CACHE_TAG_LISTS)) { file_put_contents($cache_key, $html); } @@ -358,7 +358,7 @@ class TagList extends Extension private function build_tag_popularity(int $tags_min): string { - global $database; + global $config, $database; // Make sure that the value of $tags_min is at least 1. // Otherwise the database will complain if you try to do: LOG(0) @@ -396,7 +396,7 @@ class TagList extends Extension $html .= "$h_tag ($count)\n"; } - if (SPEED_HAX) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::CACHE_TAG_LISTS)) { file_put_contents($cache_key, $html); } diff --git a/ext/user/main.php b/ext/user/main.php index eff8d1c5..13ddd5ac 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -636,7 +636,7 @@ class UserPage extends Extension { global $page, $config; $page->add_cookie("session", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/"); - if (SPEED_HAX) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::PURGE_COOKIE)) { # to keep as few versions of content as possible, # make cookies all-or-nothing $page->add_cookie("user", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/"); diff --git a/index.php b/index.php index a061d996..4ef516c4 100644 --- a/index.php +++ b/index.php @@ -71,7 +71,7 @@ try { ] ); - if (!SPEED_HAX) { + if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::NO_AUTO_DB_UPGRADE)) { send_event(new DatabaseUpgradeEvent()); } send_event(new InitExtEvent()); diff --git a/tests/defines.php b/tests/defines.php index 3d5a9a4c..2e279f99 100644 --- a/tests/defines.php +++ b/tests/defines.php @@ -14,7 +14,6 @@ define("DATABASE_TIMEOUT", 10000); define("CACHE_DSN", null); define("DEBUG", false); define("COOKIE_PREFIX", 'shm'); -define("SPEED_HAX", false); define("WH_SPLITS", 1); define("VERSION", 'unit-tests'); define("TRACE_FILE", null); diff --git a/tests/phpstan.neon b/tests/phpstan.neon index d80d46f1..970dcc36 100644 --- a/tests/phpstan.neon +++ b/tests/phpstan.neon @@ -8,7 +8,6 @@ parameters: - ../themes dynamicConstantNames: - DEBUG - - SPEED_HAX - TRUSTED_PROXIES - TIMEZONE - BASE_HREF