From 0512858bd465e18124583b6610bebe1ed8e002d3 Mon Sep 17 00:00:00 2001 From: jgen Date: Fri, 4 Oct 2013 17:17:42 -0400 Subject: [PATCH 1/3] Some more small tweaks for speed. --- core/imageboard.pack.php | 28 +++++++++++++++------------- core/util.inc.php | 6 +++--- ext/index/main.php | 11 ++++++++--- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index eed9b604..11710214 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -148,9 +148,9 @@ class Image { public static function count_images($tags=array()) { assert(is_array($tags)); global $database; - $tag_count = count($tags); - - if($tag_count == 0) { + $tag_count = count($tags); + + if($tag_count === 0) { $total = $database->cache->get("image-count"); if(!$total) { $total = $database->get_one("SELECT COUNT(*) FROM images"); @@ -158,7 +158,7 @@ class Image { } return $total; } - else if($tag_count == 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { + else if($tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { $term = Tag::resolve_alias($tags[0]); return $database->get_one( $database->scoreql_to_sql("SELECT count FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"), @@ -207,7 +207,7 @@ class Image { $dir = "ASC"; } - if(count($tags) == 0) { + if(count($tags) === 0) { $row = $database->get_row('SELECT images.* FROM images WHERE images.id '.$gtlt.' '.$this->id.' ORDER BY images.id '.$dir.' LIMIT 1'); } else { @@ -584,14 +584,14 @@ class Image { $_flexihash = new Flexihash(); foreach(explode(",", $opts) as $opt) { $parts = explode("=", $opt); - $parts_count = count($parts); + $parts_count = count($parts); $opt_val = ""; $opt_weight = 0; - if($parts_count == 2) { + if($parts_count === 2) { $opt_val = $parts[0]; $opt_weight = $parts[1]; } - elseif($parts_count == 1) { + elseif($parts_count === 1) { $opt_val = $parts[0]; $opt_weight = 1; } @@ -694,9 +694,11 @@ class Image { } $img_search = new Querylet($sql, $terms); + // How many tag querylets are there? + $count_tag_querylets = count($tag_querylets); // no tags, do a simple search (+image metadata if we have any) - if(count($tag_querylets) == 0) { + if(count_tag_querylets === 0) { $query = new Querylet("SELECT images.* FROM images "); if(!empty($img_search->sql)) { @@ -706,7 +708,7 @@ class Image { } // one positive tag (a common case), do an optimised search - else if(count($tag_querylets) == 1 && $tag_querylets[0]->positive) { + else if(count_tag_querylets === 1 && $tag_querylets[0]->positive) { $query = new Querylet($database->scoreql_to_sql(" SELECT images.* FROM images JOIN image_tags ON images.id=image_tags.image_id @@ -990,7 +992,7 @@ class Tag { } } - if(count($tag_array) == 0 && $tagme) { + if(count($tag_array) === 0 && $tagme) { $tag_array = array("tagme"); } @@ -1106,8 +1108,8 @@ function move_upload_to_archive(DataUploadEvent $event) { function get_thumbnail_size(/*int*/ $orig_width, /*int*/ $orig_height) { global $config; - if($orig_width == 0) $orig_width = 192; - if($orig_height == 0) $orig_height = 192; + if($orig_width === 0) $orig_width = 192; + if($orig_height === 0) $orig_height = 192; if($orig_width > $orig_height * 5) $orig_width = $orig_height * 5; if($orig_height > $orig_width * 5) $orig_height = $orig_width * 5; diff --git a/core/util.inc.php b/core/util.inc.php index b225dfff..b1331a4a 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -797,7 +797,7 @@ function data_path($filename) { function transload($url, $mfile) { global $config; - if($config->get_string("transload_engine") == "curl" && function_exists("curl_init")) { + if($config->get_string("transload_engine") === "curl" && function_exists("curl_init")) { $ch = curl_init($url); $fp = fopen($mfile, "w"); @@ -814,7 +814,7 @@ function transload($url, $mfile) { return true; } - if($config->get_string("transload_engine") == "wget") { + if($config->get_string("transload_engine") === "wget") { $s_url = escapeshellarg($url); $s_mfile = escapeshellarg($mfile); system("wget --no-check-certificate $s_url --output-document=$s_mfile"); @@ -822,7 +822,7 @@ function transload($url, $mfile) { return file_exists($mfile); } - if($config->get_string("transload_engine") == "fopen") { + if($config->get_string("transload_engine") === "fopen") { $fp = @fopen($url, "r"); if(!$fp) { return false; diff --git a/ext/index/main.php b/ext/index/main.php index 839cb7a8..f851bde7 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -162,10 +162,13 @@ class Index extends Extension { $search_terms = $event->get_search_terms(); $page_number = $event->get_page_number(); $page_size = $event->get_page_size(); + + $count_search_terms = count($search_terms); + try { #log_debug("index", "Search for ".implode(" ", $search_terms), false, array("terms"=>$search_terms)); $total_pages = Image::count_pages($search_terms); - if(SPEED_HAX && count($search_terms) == 0 && ($page_number < 10)) { // extra caching for the first few post/list pages + if(SPEED_HAX && $count_search_terms === 0 && ($page_number < 10)) { // extra caching for the first few post/list pages $images = $database->cache->get("post-list-$page_number"); if(!$images) { $images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms); @@ -182,11 +185,13 @@ class Index extends Extension { $images = array(); } - if(count($search_terms) == 0 && count($images) == 0 && $page_number == 1) { + $count_images = count($images); + + if($count_search_terms === 0 && $count_images === 0 && $page_number === 1) { $this->theme->display_intro($page); send_event(new PostListBuildingEvent($search_terms)); } - else if(count($search_terms) > 0 && count($images) == 1 && $page_number == 1) { + else if($count_search_terms > 0 && $count_images === 1 && $page_number === 1) { $page->set_mode("redirect"); $page->set_redirect(make_link('post/view/'.$images[0]->id)); } From b01ca892207d6bae220b4ca7172c146ed01ecd97 Mon Sep 17 00:00:00 2001 From: jgen Date: Fri, 4 Oct 2013 17:53:26 -0400 Subject: [PATCH 2/3] Some more small speed related tweaks. --- core/config.class.php | 4 ++-- ext/artists/main.php | 8 ++++---- ext/chatbox/history/index.php | 2 +- themes/danbooru/layout.class.php | 4 +++- themes/danbooru2/layout.class.php | 4 +++- themes/lite/layout.class.php | 8 ++++++-- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/core/config.class.php b/core/config.class.php index 66d5d58a..538dd496 100644 --- a/core/config.class.php +++ b/core/config.class.php @@ -69,7 +69,7 @@ abstract class BaseConfig implements Config { $this->save($name); } public function set_array(/*string*/ $name, $value) { - assert(is_array($value)); + assert(isset($value) && is_array($value)); $this->values[$name] = implode(",", $value); $this->save($name); } @@ -90,7 +90,7 @@ abstract class BaseConfig implements Config { } } public function set_default_array(/*string*/ $name, $value) { - assert(is_array($value)); + assert(isset($value) && is_array($value)); if(is_null($this->get($name))) { $this->values[$name] = implode(",", $value); } diff --git a/ext/artists/main.php b/ext/artists/main.php index a4b3d6c3..d426d512 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -112,7 +112,7 @@ class Artists extends Extension { global $database; $author = strtolower($event->author); - if (strlen($author) == 0 || strpos($author, " ")) + if (strlen($author) === 0 || strpos($author, " ")) return; $paddedAuthor = str_replace(" ", "_", $author); @@ -709,7 +709,7 @@ class Artists extends Extension { if (is_null($aliasID) || !is_numeric($aliasID)) return; - if (is_null($alias) || strlen($alias) == 0) + if (is_null($alias) || strlen($alias) === 0) return; global $user; @@ -767,7 +767,7 @@ class Artists extends Extension { if (is_null($memberID) || !is_numeric($memberID)) return; - if (is_null($memberName) || strlen($memberName) == 0) + if (is_null($memberName) || strlen($memberName) === 0) return; global $user; @@ -796,7 +796,7 @@ class Artists extends Extension { global $user; $name = html_escape(strtolower($_POST["name"])); - if (is_null($name) || (strlen($name) == 0) || strpos($name, " ")) + if (is_null($name) || (strlen($name) === 0) || strpos($name, " ")) return -1; $notes = html_escape(ucfirst($_POST["notes"])); diff --git a/ext/chatbox/history/index.php b/ext/chatbox/history/index.php index 681dfc8c..94642523 100644 --- a/ext/chatbox/history/index.php +++ b/ext/chatbox/history/index.php @@ -23,7 +23,7 @@ $ys = ys($log); $posts = $ys->posts(); - if (sizeof($posts) == 0) + if (sizeof($posts) === 0) $html .= '
diff --git a/themes/danbooru/layout.class.php b/themes/danbooru/layout.class.php index dbe89cf6..611d8df6 100644 --- a/themes/danbooru/layout.class.php +++ b/themes/danbooru/layout.class.php @@ -256,7 +256,9 @@ EOD; $url=$matches[1][0]; } - for($i=0;$i Date: Sat, 5 Oct 2013 14:28:02 -0400 Subject: [PATCH 3/3] Fixing missing $. (Thanks Shish!) --- core/imageboard.pack.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 11710214..e36c40ee 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -698,7 +698,7 @@ class Image { $count_tag_querylets = count($tag_querylets); // no tags, do a simple search (+image metadata if we have any) - if(count_tag_querylets === 0) { + if($count_tag_querylets === 0) { $query = new Querylet("SELECT images.* FROM images "); if(!empty($img_search->sql)) { @@ -708,7 +708,7 @@ class Image { } // one positive tag (a common case), do an optimised search - else if(count_tag_querylets === 1 && $tag_querylets[0]->positive) { + else if($count_tag_querylets === 1 && $tag_querylets[0]->positive) { $query = new Querylet($database->scoreql_to_sql(" SELECT images.* FROM images JOIN image_tags ON images.id=image_tags.image_id