From 350b44f250babdee5f7dfa0e9798448750dd608e Mon Sep 17 00:00:00 2001
From: discomrade <83621080+discomrade@users.noreply.github.com>
Date: Fri, 14 Jun 2024 13:37:24 +0000
Subject: [PATCH] [speed_hax] extract SPEED_HAX tweaks to extension
---
core/imageboard/search.php | 20 +++++++-------
core/permissions.php | 2 +-
core/send_event.php | 7 ++---
core/sys_config.php | 3 +--
core/urls.php | 2 +-
ext/comment/main.php | 3 ++-
ext/index/main.php | 9 ++++---
ext/rss_images/main.php | 2 +-
ext/speed_hax/info.php | 33 +++++++++++++++++++++++
ext/speed_hax/main.php | 55 ++++++++++++++++++++++++++++++++++++++
ext/tag_list/main.php | 8 +++---
ext/user/main.php | 2 +-
index.php | 2 +-
tests/defines.php | 1 -
tests/phpstan.neon | 1 -
15 files changed, 120 insertions(+), 30 deletions(-)
create mode 100644 ext/speed_hax/info.php
create mode 100644 ext/speed_hax/main.php
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.
+
+
Don't auto-upgrade database
: Database schema upgrades are no longer automatic; you'll need to run php index.php db-upgrade
from the CLI each time you update the code.Cache event listeners
: Mapping from Events to Extensions is cached - you'll need to delete data/cache/shm_event_listeners.php
after each code change, and after enabling or disabling any extensions.Purge cookie on logout
: Clears the user
cookie when a user logs out, to keep as few versions of content as possible.List only recent comments
: Only comments from the past 24 hours show up in /comment/list
.Fast page limit
: We only show the first 500 pages of results for any query, except for the most simple (no tags, or one positive tag). Web crawlers are blocked from creating too many nonsense searches by limiting to 10 pages.Anonymous search tag limit
: Anonymous users can only search for this many tags at once. To disable, set to 0.Limit complex searches
: Only ever show the first 5,000 results for complex queries.Fast page limit
: We only show the first 500 pages of results for any query, except for the most simple (no tags, or one positive tag) or users with the BIG_SEARCH
permission. Web crawlers are blocked from creating too many nonsense searches by limiting to 10 pages. Consider enabling Extra caching on first pages
as well.Extra caching on first pages
: The first 10 pages in the post/list
index get extra caching.Limit images RSS
: RSS is limited to 10 pages for the image list.