More small changes for speed.

This commit is contained in:
green-ponies (jgen) 2012-01-11 15:57:00 -05:00
parent ea15574226
commit 26d383198a
5 changed files with 45 additions and 43 deletions

View File

@ -56,7 +56,7 @@ class DBEngine {
} }
public function create_table_sql($name, $data) { public function create_table_sql($name, $data) {
return "CREATE TABLE $name ($data)"; return 'CREATE TABLE '.$name.' ('.$data.')';
} }
} }
class MySQL extends DBEngine { class MySQL extends DBEngine {
@ -82,7 +82,7 @@ class MySQL extends DBEngine {
public function create_table_sql($name, $data) { public function create_table_sql($name, $data) {
$data = $this->scoreql_to_sql($data); $data = $this->scoreql_to_sql($data);
$ctes = "ENGINE=InnoDB DEFAULT CHARSET='utf8'"; $ctes = "ENGINE=InnoDB DEFAULT CHARSET='utf8'";
return "CREATE TABLE $name ($data) $ctes"; return 'CREATE TABLE '.$name.' ('.$data.') '.$ctes;
} }
} }
class PostgreSQL extends DBEngine { class PostgreSQL extends DBEngine {
@ -103,7 +103,7 @@ class PostgreSQL extends DBEngine {
public function create_table_sql($name, $data) { public function create_table_sql($name, $data) {
$data = $this->scoreql_to_sql($data); $data = $this->scoreql_to_sql($data);
return "CREATE TABLE $name ($data)"; return 'CREATE TABLE '.$name.' ('.$data.')';
} }
} }
@ -151,14 +151,14 @@ class SQLite extends DBEngine {
$matches = array(); $matches = array();
if(preg_match("/INDEX\s*\((.*)\)/", $bit, $matches)) { if(preg_match("/INDEX\s*\((.*)\)/", $bit, $matches)) {
$col = $matches[1]; $col = $matches[1];
$extras .= "CREATE INDEX {$name}_{$col} on $name($col);"; $extras .= 'CREATE INDEX '.$name.'_'.$col.' on '.$name($col).';';
} }
else { else {
$cols[] = $bit; $cols[] = $bit;
} }
} }
$cols_redone = implode(", ", $cols); $cols_redone = implode(", ", $cols);
return "CREATE TABLE $name ($cols_redone); $extras"; return 'CREATE TABLE '.$name.' ('.$cols_redone.'); '.$extras;
} }
} }
// }}} // }}}
@ -331,10 +331,10 @@ class Database {
if (!array_key_exists(0, $args)) { if (!array_key_exists(0, $args)) {
foreach($args as $name=>$value) { foreach($args as $name=>$value) {
if(is_numeric($value)) { if(is_numeric($value)) {
$stmt->bindValue(":$name", $value, PDO::PARAM_INT); $stmt->bindValue(':'.$name, $value, PDO::PARAM_INT);
} }
else { else {
$stmt->bindValue(":$name", $value, PDO::PARAM_STR); $stmt->bindValue(':'.$name, $value, PDO::PARAM_STR);
} }
} }
$stmt->execute(); $stmt->execute();

View File

@ -68,7 +68,7 @@ class PageRequestEvent extends Event {
} }
public function count_args() { public function count_args() {
return $this->arg_count - $this->part_count; return (int)($this->arg_count - $this->part_count);
} }
/* /*
@ -76,20 +76,20 @@ class PageRequestEvent extends Event {
*/ */
public function get_search_terms() { public function get_search_terms() {
$search_terms = array(); $search_terms = array();
if($this->count_args() == 2) { if($this->count_args() === 2) {
$search_terms = explode(' ', $this->get_arg(0)); $search_terms = explode(' ', $this->get_arg(0));
} }
return $search_terms; return $search_terms;
} }
public function get_page_number() { public function get_page_number() {
$page_number = 1; $page_number = 1;
if($this->count_args() == 1) { if($this->count_args() === 1) {
$page_number = int_escape($this->get_arg(0)); $page_number = int_escape($this->get_arg(0));
} }
else if($this->count_args() == 2) { else if($this->count_args() === 2) {
$page_number = int_escape($this->get_arg(1)); $page_number = int_escape($this->get_arg(1));
} }
if($page_number == 0) $page_number = 1; // invalid -> 0 if($page_number === 0) $page_number = 1; // invalid -> 0
return $page_number; return $page_number;
} }
public function get_page_size() { public function get_page_size() {

View File

@ -514,7 +514,7 @@ class Image {
private static function build_search_querylet($terms) { private static function build_search_querylet($terms) {
assert(is_array($terms)); assert(is_array($terms));
global $database; global $database;
if($database->engine->name == "mysql") if($database->engine->name === "mysql")
return Image::build_ugly_search_querylet($terms); return Image::build_ugly_search_querylet($terms);
else else
return Image::build_accurate_search_querylet($terms); return Image::build_accurate_search_querylet($terms);
@ -750,11 +750,13 @@ class Image {
foreach($tag_querylets as $tq) { foreach($tag_querylets as $tq) {
global $tag_n; global $tag_n;
$sign = $tq->positive ? "+" : "-"; $sign = $tq->positive ? "+" : "-";
$sql .= " $sign (tag LIKE :tag$tag_n)"; //$sql .= " $sign (tag LIKE :tag$tag_n)";
$terms["tag$tag_n"] = $tq->tag; $sql .= ' '.$sign.' (tag LIKE :tag'.$tag_n.')';
//$terms["tag$tag_n"] = $tq->tag;
$terms['tag'.$tag_n] = $tq->tag;
$tag_n++; $tag_n++;
if($sign == "+") $positive_tag_count++; if($sign === "+") $positive_tag_count++;
else $negative_tag_count++; else $negative_tag_count++;
} }
$tag_search = new Querylet($sql, $terms); $tag_search = new Querylet($sql, $terms);
@ -783,7 +785,7 @@ class Image {
} }
// one positive tag (a common case), do an optimised search // one positive tag (a common case), do an optimised search
else if($positive_tag_count == 1 && $negative_tag_count == 0) { else if($positive_tag_count === 1 && $negative_tag_count === 0) {
$query = new Querylet( $query = new Querylet(
// MySQL is braindead, and does a full table scan on images, running the subquery once for each row -_- // MySQL is braindead, and does a full table scan on images, running the subquery once for each row -_-
// "{$this->get_images} WHERE images.id IN (SELECT image_id FROM tags WHERE tag LIKE ?) ", // "{$this->get_images} WHERE images.id IN (SELECT image_id FROM tags WHERE tag LIKE ?) ",
@ -819,22 +821,22 @@ class Image {
if($tags_ok) { if($tags_ok) {
$tag_id_list = join(', ', $tag_id_array); $tag_id_list = join(', ', $tag_id_array);
$subquery = new Querylet(" $subquery = new Querylet('
SELECT images.*, SUM({$tag_search->sql}) AS score SELECT images.*, SUM('.$tag_search->sql.') AS score
FROM images FROM images
LEFT JOIN image_tags ON image_tags.image_id = images.id LEFT JOIN image_tags ON image_tags.image_id = images.id
JOIN tags ON image_tags.tag_id = tags.id JOIN tags ON image_tags.tag_id = tags.id
WHERE tags.id IN ({$tag_id_list}) WHERE tags.id IN ('.$tag_id_list.')
GROUP BY images.id GROUP BY images.id
HAVING score = :score", HAVING score = :score',
array_merge( array_merge(
$tag_search->variables, $tag_search->variables,
array("score"=>$positive_tag_count) array("score"=>$positive_tag_count)
) )
); );
$query = new Querylet(" $query = new Querylet('
SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp
FROM ({$subquery->sql}) AS images ", $subquery->variables); FROM ('.$subquery->sql.') AS images ', $subquery->variables);
if(strlen($img_search->sql) > 0) { if(strlen($img_search->sql) > 0) {
$query->append_sql(" WHERE "); $query->append_sql(" WHERE ");
@ -882,9 +884,9 @@ class Tag {
if(is_string($tags)) { if(is_string($tags)) {
$tags = explode(' ', $tags); $tags = explode(' ', $tags);
} }
else if(is_array($tags)) { //else if(is_array($tags)) {
// do nothing // do nothing
} //}
$tags = array_map("trim", $tags); $tags = array_map("trim", $tags);
@ -907,13 +909,13 @@ class Tag {
public static function implode($tags) { public static function implode($tags) {
assert(is_string($tags) || is_array($tags)); assert(is_string($tags) || is_array($tags));
if(is_string($tags)) { if(is_array($tags)) {
// do nothing
}
else if(is_array($tags)) {
sort($tags); sort($tags);
$tags = implode(' ', $tags); $tags = implode(' ', $tags);
} }
//else if(is_string($tags)) {
// do nothing
//}
return $tags; return $tags;
} }

View File

@ -242,22 +242,22 @@ class Page {
// caching failed, add all files to html_headers. // caching failed, add all files to html_headers.
foreach(glob("lib/*.css") as $css) { foreach(glob("lib/*.css") as $css) {
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css' type='text/css'>"); $this->add_html_header('<link rel="stylesheet" href="'.$data_href.'/'.$css.'" type="text/css">');
} }
$css_files = glob("ext/*/style.css"); $css_files = glob("ext/*/style.css");
if($css_files) { if($css_files) {
foreach($css_files as $css_file) { foreach($css_files as $css_file) {
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css_file' type='text/css'>"); $this->add_html_header('<link rel="stylesheet" href="'.$data_href.'/'.$css_file.'" type="text/css">');
} }
} }
foreach(glob("lib/*.js") as $js) { foreach(glob("lib/*.js") as $js) {
$this->add_html_header("<script src='$data_href/$js' type='text/javascript'></script>"); $this->add_html_header('<script src="'.$data_href.'/'.$js'" type="text/javascript"></script>');
} }
$js_files = glob("ext/*/script.js"); $js_files = glob("ext/*/script.js");
if($js_files) { if($js_files) {
foreach($js_files as $js_file) { foreach($js_files as $js_file) {
$this->add_html_header("<script src='$data_href/$js_file' type='text/javascript'></script>"); $this->add_html_header('<script src="'.$data_href.'/'.$js_file'" type="text/javascript"></script>');
} }
} }
} }
@ -362,12 +362,12 @@ class Page {
} else { } else {
// Caching of CSS disabled. // Caching of CSS disabled.
foreach(glob("lib/*.css") as $css) { foreach(glob("lib/*.css") as $css) {
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css' type='text/css'>"); $this->add_html_header('<link rel="stylesheet" href="'.$data_href.'/'.$css.'" type="text/css">');
} }
$css_files = glob("ext/*/style.css"); $css_files = glob("ext/*/style.css");
if($css_files) { if($css_files) {
foreach($css_files as $css_file) { foreach($css_files as $css_file) {
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css_file' type='text/css'>"); $this->add_html_header('<link rel="stylesheet" href="'.$data_href.'/'.$css_file.'" type="text/css">');
} }
} }
} }
@ -412,12 +412,12 @@ class Page {
} else { } else {
// Caching of Javascript disabled. // Caching of Javascript disabled.
foreach(glob("lib/*.js") as $js) { foreach(glob("lib/*.js") as $js) {
$this->add_html_header("<script src='$data_href/$js' type='text/javascript'></script>"); $this->add_html_header('<script src="'.$data_href.'/'.$js'" type="text/javascript"></script>');
} }
$js_files = glob("ext/*/script.js"); $js_files = glob("ext/*/script.js");
if($js_files) { if($js_files) {
foreach($js_files as $js_file) { foreach($js_files as $js_file) {
$this->add_html_header("<script src='$data_href/$js_file' type='text/javascript'></script>"); $this->add_html_header('<script src="'.$data_href.'/'.$js_file'" type="text/javascript"></script>');
} }
} }
} }

View File

@ -40,7 +40,7 @@ class User {
public static function by_session($name, $session) { public static function by_session($name, $session) {
global $config, $database; global $config, $database;
if($database->engine->name == "mysql") { if($database->engine->name === "mysql") {
$query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess"; $query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess";
} }
else { else {
@ -53,12 +53,12 @@ class User {
public static function by_id($id) { public static function by_id($id) {
assert(is_numeric($id)); assert(is_numeric($id));
global $database; global $database;
if($id == 1) { if($id === 1) {
$cached = $database->cache->get("user-id:$id"); $cached = $database->cache->get('user-id:'.$id);
if($cached) return new User($cached); if($cached) return new User($cached);
} }
$row = $database->get_row("SELECT * FROM users WHERE id = :id", array("id"=>$id)); $row = $database->get_row("SELECT * FROM users WHERE id = :id", array("id"=>$id));
if($id == 1) $database->cache->set("user-id:$id", $row, 300); if($id === 1) $database->cache->set('user-id:'.$id, $row, 300);
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
} }
@ -148,7 +148,7 @@ class User {
public function get_avatar_html() { public function get_avatar_html() {
// FIXME: configurable // FIXME: configurable
global $config; global $config;
if($config->get_string("avatar_host") == "gravatar") { if($config->get_string("avatar_host") === "gravatar") {
if(!empty($this->email)) { if(!empty($this->email)) {
$hash = md5(strtolower($this->email)); $hash = md5(strtolower($this->email));
$s = $config->get_string("avatar_gravatar_size"); $s = $config->get_string("avatar_gravatar_size");
@ -180,7 +180,7 @@ class User {
public function get_auth_html() { public function get_auth_html() {
$at = $this->get_auth_token(); $at = $this->get_auth_token();
return "<input type='hidden' name='auth_token' value='$at'>"; return '<input type="hidden" name="auth_token" value="'.$at.'">';
} }
public function check_auth_token() { public function check_auth_token() {