diff options
-rw-r--r-- | phpBB/admin/admin_words.php | 2 | ||||
-rw-r--r-- | phpBB/common.php | 30 | ||||
-rw-r--r-- | phpBB/db/mysql.php | 132 | ||||
-rw-r--r-- | phpBB/includes/acm/cache_file.php | 244 | ||||
-rw-r--r-- | phpBB/includes/functions.php | 47 | ||||
-rw-r--r-- | phpBB/includes/page_header.php | 14 | ||||
-rw-r--r-- | phpBB/includes/page_tail.php | 2 | ||||
-rw-r--r-- | phpBB/includes/session.php | 9 | ||||
-rw-r--r-- | phpBB/install/install.php | 1 | ||||
-rw-r--r-- | phpBB/posting.php | 10 |
10 files changed, 393 insertions, 98 deletions
diff --git a/phpBB/admin/admin_words.php b/phpBB/admin/admin_words.php index d4599191af..23a1d6a60f 100644 --- a/phpBB/admin/admin_words.php +++ b/phpBB/admin/admin_words.php @@ -132,6 +132,7 @@ if ($mode != '') $sql = ($word_id) ? "UPDATE " . WORDS_TABLE . " SET word = '" . sql_quote($word) . "', replacement = '" . sql_quote($replacement) . "' WHERE word_id = $word_id" : "INSERT INTO " . WORDS_TABLE . " (word, replacement) VALUES ('" . sql_quote($word) . "', '" . sql_quote($replacement) . "')"; $db->sql_query($sql); + $cache->destroy('word_censors'); $log_action = ($word_id) ? 'log_edit_word' : 'log_add_word'; add_admin_log($log_action, stripslashes($word)); @@ -153,6 +154,7 @@ if ($mode != '') $sql = "DELETE FROM " . WORDS_TABLE . " WHERE word_id = $word_id"; $db->sql_query($sql); + $cache->destroy('word_censors'); add_admin_log('log_delete_word'); diff --git a/phpBB/common.php b/phpBB/common.php index 62d0ce3af1..6c4da7da4a 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -37,7 +37,7 @@ if ( !get_magic_quotes_gpc() ) } require($phpbb_root_path . 'config.'.$phpEx); -require($phpbb_root_path . 'config_cache.'.$phpEx); +//require($phpbb_root_path . 'config_cache.'.$phpEx); if ( !defined('PHPBB_INSTALLED') ) { @@ -46,6 +46,7 @@ if ( !defined('PHPBB_INSTALLED') ) } // Include files +require($phpbb_root_path . 'includes/acm/cache_' . $acm_type . '.'.$phpEx); require($phpbb_root_path . 'includes/template.'.$phpEx); require($phpbb_root_path . 'includes/session.'.$phpEx); require($phpbb_root_path . 'includes/functions.'.$phpEx); @@ -148,10 +149,14 @@ define('VOTE_USERS_TABLE', $table_prefix.'vote_voters'); // Set PHP error handler to ours set_error_handler('msg_handler'); +// Experimental cache manager +$cache = new acm(); + // Need these here so instantiate them now $template = new Template(); $db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false); +/* // Obtain boardwide default config (rebuilding cache if reqd) if ( empty($config) ) { @@ -176,6 +181,29 @@ if ( empty($acl_options) ) $auth_admin = new auth_admin(); $acl_options = $auth_admin->acl_cache_options(); } +*/ + +if (!$config = $cache->load('config')) +{ + $config = array(); + + $sql = 'SELECT * FROM ' . CONFIG_TABLE; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $config[$row['config_name']] = $row['config_value']; + } + + $cache->save('config', $config); +} + +/* +if (time() - $config['cache_interval'] >= $config['cache_last_gc']) +{ + $cache->tidy($config['cache_gc']); +} +*/ // Instantiate some basic classes $user = new user(); diff --git a/phpBB/db/mysql.php b/phpBB/db/mysql.php index cf86637b29..6ed2118bd8 100644 --- a/phpBB/db/mysql.php +++ b/phpBB/db/mysql.php @@ -36,7 +36,7 @@ class sql_db // // Constructor // - function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $port, $persistency = false) + function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) { $this->open_queries = array(); $this->num_queries = 0; @@ -117,74 +117,84 @@ class sql_db // // Base query method // - function sql_query($query = '') + function sql_query($query = '', $expire_time = 0) { if ($query != '') { - $this->query_result = false; - $this->num_queries++; - - if (!empty($_REQUEST['explain'])) - { - global $starttime; - $curtime = explode(' ', microtime()); - $curtime = $curtime[0] + $curtime[1] - $starttime; - } - if (!$this->query_result = @mysql_query($query, $this->db_connect_id)) + global $cache; + if (!$expire_time || !$cache->sql_load($query)) { - $this->sql_error($query); - } - if (!empty($_REQUEST['explain'])) - { - $endtime = explode(' ', microtime()); - $endtime = $endtime[0] + $endtime[1] - $starttime; + $this->query_result = false; + $this->num_queries++; - $this->sql_report .= "<pre>Query:\t" . htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n\t", $query)) . "\n\n"; - if ($this->query_result) + if (!empty($_REQUEST['explain'])) { - $this->sql_report .= "Time before: $curtime\nTime after: $endtime\nElapsed time: <b>" . ($endtime - $curtime) . "</b>\n</pre>"; + global $starttime; + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1] - $starttime; } - else + if (!$this->query_result = @mysql_query($query, $this->db_connect_id)) { - $error = $this->sql_error(); - $this->sql_report .= '<b>FAILED</b> - MySQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']) . '<br><br><pre>'; + $this->sql_error($query); } - $this->sql_time += $endtime - $curtime; - if (preg_match('/^SELECT/', $query)) + if (!empty($_REQUEST['explain'])) { - $html_table = FALSE; - if ($result = mysql_query("EXPLAIN $query", $this->db_connect_id)) + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1] - $starttime; + + $this->sql_report .= "<pre>Query:\t" . htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n\t", $query)) . "\n\n"; + if ($this->query_result) { - while ($row = mysql_fetch_assoc($result)) + $this->sql_report .= "Time before: $curtime\nTime after: $endtime\nElapsed time: <b>" . ($endtime - $curtime) . "</b>\n</pre>"; + } + else + { + $error = $this->sql_error(); + $this->sql_report .= '<b>FAILED</b> - MySQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']) . '<br><br><pre>'; + } + $this->sql_time += $endtime - $curtime; + if (preg_match('/^SELECT/', $query)) + { + $html_table = FALSE; + if ($result = mysql_query("EXPLAIN $query", $this->db_connect_id)) { - if (!$html_table && count($row)) + while ($row = mysql_fetch_assoc($result)) { - $html_table = TRUE; - $this->sql_report .= "<table width=100% border=1 cellpadding=2 cellspacing=1>\n"; - $this->sql_report .= "<tr>\n<td><b>" . implode("</b></td>\n<td><b>", array_keys($row)) . "</b></td>\n</tr>\n"; + if (!$html_table && count($row)) + { + $html_table = TRUE; + $this->sql_report .= "<table width=100% border=1 cellpadding=2 cellspacing=1>\n"; + $this->sql_report .= "<tr>\n<td><b>" . implode("</b></td>\n<td><b>", array_keys($row)) . "</b></td>\n</tr>\n"; + } + $this->sql_report .= "<tr>\n<td>" . implode(" </td>\n<td>", array_values($row)) . " </td>\n</tr>\n"; } - $this->sql_report .= "<tr>\n<td>" . implode(" </td>\n<td>", array_values($row)) . " </td>\n</tr>\n"; + } + if ($html_table) + { + $this->sql_report .= '</table><br>'; } } - if ($html_table) - { - $this->sql_report .= '</table><br>'; - } + $this->sql_report .= "<hr>\n"; } - $this->sql_report .= "<hr>\n"; - } - $this->open_queries[] = $this->query_result; + $this->open_queries[] = $this->query_result; + } } else { return false; } + if ($expire_time && $this->query_result) + { + $cache->sql_save($query, $this->query_result); + @mysql_free_result(array_pop($this->open_queries)); + } + return ( $this->query_result) ? $this->query_result : false; } - function sql_query_limit($query = '', $total, $offset = 0) + function sql_query_limit($query = '', $total, $offset = 0, $expire_time = 0) { if ( $query != '' ) { @@ -196,7 +206,7 @@ class sql_db $query .= ' LIMIT ' . ( ( !empty($offset) ) ? $offset . ', ' . $total : $total ); } - return $this->sql_query($query); + return $this->sql_query($query, $expire_time); } else { @@ -288,59 +298,62 @@ class sql_db $query_id = $this->query_result; } + global $cache; + if ($cache->sql_exists($query_id)) + { + return $cache->sql_fetchrow($query_id); + } + return ( $query_id ) ? @mysql_fetch_assoc($query_id) : false; } function sql_fetchrowset($query_id = 0) { - if(!$query_id) + if (!$query_id) { $query_id = $this->query_result; } - if($query_id) + if ($query_id) { unset($this->rowset[$query_id]); unset($this->row[$query_id]); - while($this->rowset[$query_id] = @mysql_fetch_assoc($query_id)) + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) { $result[] = $this->rowset[$query_id]; } return $result; } - else - { - return false; - } + return false; } function sql_fetchfield($field, $rownum = -1, $query_id = 0) { - if(!$query_id) + if (!$query_id) { $query_id = $this->query_result; } - if($query_id) + if ($query_id) { - if($rownum > -1) + if ($rownum > -1) { $result = @mysql_result($query_id, $rownum, $field); } else { - if(empty($this->row[$query_id]) && empty($this->rowset[$query_id])) + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) { - if($this->sql_fetchrow()) + if ($this->sql_fetchrow()) { $result = $this->row[$query_id][$field]; } } else { - if($this->rowset[$query_id]) + if ($this->rowset[$query_id]) { $result = $this->rowset[$query_id][$field]; } - else if($this->row[$query_id]) + elseif ($this->row[$query_id]) { $result = $this->row[$query_id][$field]; } @@ -348,10 +361,7 @@ class sql_db } return $result; } - else - { - return false; - } + return false; } function sql_rowseek($rownum, $query_id = 0) diff --git a/phpBB/includes/acm/cache_file.php b/phpBB/includes/acm/cache_file.php new file mode 100644 index 0000000000..a840274e01 --- /dev/null +++ b/phpBB/includes/acm/cache_file.php @@ -0,0 +1,244 @@ +<?php +/*************************************************************************** + * cache_file.php + * ------------------- + * begin : Saturday, Feb 13, 2001 + * copyright : (C) 2001 The phpBB Group + * email : support@phpbb.com + * + * $Id$ + * + ***************************************************************************/ + +/*************************************************************************** + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + ***************************************************************************/ + +// +// This class is part of the Advanced Cache Manager +// + +class acm +{ + var $vars = ''; + var $vars_ts = array(); + var $modified = FALSE; + + var $sql_rowset = array(); + var $sql_rowset_index = array(); + + function acm() + { + //$this->load_cache(); + $this->cache_dir = $phpbb_root_path . 'cache/'; + } + + function tidy($expire_time = 0) + { + global $phpEx; + + $dir = opendir($this->cache_dir); + while ($entry = readdir($dir)) + { + if ($entry{0} == '.') + { + continue; + } + if (!$expire_time || time() - $expire_time >= filemtime($this->cache_dir . $entry)) + { + unlink($this->cache_dir . $entry); + } + } + if (file_exists($this->cache_dir . 'global.' . $phpEx)) + { + foreach ($this->vars_ts as $varname => $timestamp) + { + if (time() - $expire_time >= $timestamp) + { + $this->destroy($varname); + } + } + } + else + { + $this->vars = $this->vars_ts = array(); + $this->modified = TRUE; + } + } + + function save($varname, $var) + { + $this->vars[$varname] = $var; + $this->vars_ts[$varname] = time(); + $this->modified = TRUE; + } + + function destroy($varname) + { + if (isset($this->vars[$varname])) + { + $this->modified = TRUE; + unset($this->vars[$varname]); + unset($this->vars_ts[$varname]); + } + } + + function load($varname, $expire_time = 0) + { + if (!is_array($this->vars)) + { + $this->load_cache(); + } + if (isset($this->vars[$varname])) + { + if ($expire_time && time() - $this->vars_ts[$varname] > $expire_time) + { + $this->destroy($varname); + return null; + } + return $this->vars[$varname]; + } + + return null; + } + + function exists($varname, $expire_time = 0) + { + if (!is_array($this->vars)) + { + $this->load_cache(); + } + + if ($expire_time > 0) + { + if (!isset($this->vars[$varname])) + { + return FALSE; + } + if ($this->vars_ts[$varname] <= time() - $expire_time) + { + $this->destroy($varname); + return FALSE; + } + } + + return isset($this->vars[$varname]); + } + + function load_cache() + { + global $phpEx; + + $this->vars = array(); + @include($this->cache_dir . 'global.' . $phpEx); + } + + function save_cache() + { + if (!$this->modified) + { + return; + } + + global $phpEx; + $file = '<?php $this->vars=' . $this->format_array($this->vars) . ";\n\$this->vars_ts=" . $this->format_array($this->vars_ts) . ' ?>'; + + $fp = fopen($this->cache_dir . 'global.' . $phpEx, 'wb'); + fwrite($fp, $file); + fclose($fp); + } + + function format_array($array) + { + $lines = array(); + foreach ($array as $k => $v) + { + if (is_array($v)) + { + $lines[] = "'$k'=>" . $this->format_array($v); + } + elseif (is_int($v)) + { + $lines[] = "'$k'=>$v"; + } + elseif (is_bool($v)) + { + $lines[] = "'$k'=>" . (($v) ? 'TRUE' : 'FALSE'); + } + else + { + $lines[] = "'$k'=>'" . str_replace('\\\\', '\\\\\\\\', str_replace("'", "\'", $v)) . "'"; + } + } + return 'array(' . implode(',', $lines) . ')'; + } + + function sql_load($query) + { + global $db, $phpEx; + if (!file_exists($this->cache_dir . md5($query) . '.' . $phpEx)) + { + return false; + } + + include($this->cache_dir . md5($query) . '.' . $phpEx); + + $query_id = 'Cache id #' . count($this->sql_rowset); + $this->sql_rowset[$query_id] = $rowset; + $this->sql_rowset_index[$query_id] = 0; + $db->query_result = $query_id; + + return true; + } + + function sql_save($query, $result) + { + global $db, $phpEx; + $fp = fopen($this->cache_dir . md5($query) . '.' . $phpEx, 'wb'); + + $lines = array(); + $query_id = 'Cache id #' . count($this->sql_rowset); + $this->sql_rowset[$query_id] = array(); + $this->sql_rowset_index[$query_id] = 0; + $db->query_result = $query_id; + + while ($row = $db->sql_fetchrow($result)) + { + $this->sql_rowset[$query_id][] = $row; + + $line = 'array('; + foreach ($row as $key => $val) + { + $line .= "'$key'=>'" . str_replace('\\\\', '\\\\\\\\', str_replace("'", "\'", $val)) . "',"; + } + $lines[] = substr($line, 0, -1) . ')'; + } + + fwrite($fp, "<?php\n\n/*\n$query\n*/\n\n\$rowset = array(" . implode(',', $lines) . ') ?>'); + fclose($fp); + } + + function sql_exists($query_id) + { + return isset($this->sql_rowset[$query_id]); + } + + function sql_fetchrow($query_id) + { + if (!isset($this->sql_rowset[$query_id][$this->sql_rowset_index[$query_id]])) + { + return false; + } + + $row = $this->sql_rowset[$query_id][$this->sql_rowset_index[$query_id]]; + ++$this->sql_rowset_index[$query_id]; + + return $row; + } +} +?>
\ No newline at end of file diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index ec8301ae88..8689ca7b2a 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -29,6 +29,18 @@ function sql_quote($msg) return str_replace("\'", "''", $msg); } +function set_config($config_name, $config_value) +{ + global $db, $cache, $config; + $config[$config_name] = $config_value; + $cache->save('config', $config); + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '" . sql_escape($config_value) . "' + WHERE config_name = '$config_name'"; + $db->sql_query($sql); +} + function get_userdata($user) { global $db; @@ -140,7 +152,7 @@ function generate_forum_nav(&$forum_data) // Obtain list of moderators of each forum function get_moderators(&$forum_moderators, $forum_id = false) { - global $SID, $db, $acl_options, $phpEx; + global $cache, $SID, $db, $acl_options, $phpEx; if (!empty($forum_id) && is_array($forum_id)) { @@ -189,7 +201,9 @@ function make_jumpbox($action, $forum_id = false) $sql = 'SELECT forum_id, forum_name, forum_postable, left_id, right_id FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC'; - $result = $db->sql_query($sql); + + // Cache the forums list for 60 seconds + $result = $db->sql_query($sql, 60); $right = $cat_right = 0; $padding = $forum_list = $holding = ''; @@ -646,16 +660,27 @@ function on_page($num_items, $per_page, $start) // to return both sets of arrays function obtain_word_list(&$orig_word, &$replacement_word) { - global $db; + global $db, $cache; + if ($cache->exists('word_censors')) + { + $words = $cache->load('word_censors'); + $orig_word = $words['orig']; + $replacement_word = $words['replacement']; + } + else + { + $sql = "SELECT word, replacement + FROM " . WORDS_TABLE; + $result = $db->sql_query($sql); - $sql = "SELECT word, replacement - FROM " . WORDS_TABLE; - $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $orig_word[] = '#\b(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')\b#i'; + $replacement_word[] = $row['replacement']; + } - while ($row = $db->sql_fetchrow($result)) - { - $orig_word[] = '#\b(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')\b#i'; - $replacement_word[] = $row['replacement']; + $words = array('orig' => $orig_word, 'replacement' => $replacement_word); + $cache->save('word_censors', $words); } return true; @@ -833,7 +858,7 @@ function validate_optional_fields(&$icq, &$aim, &$msnm, &$yim, &$website, &$loca // Error and message handler, call with trigger_error if reqd function msg_handler($errno, $msg_text, $errfile, $errline) { - global $db, $auth, $template, $config, $user, $nav_links; + global $cache, $db, $auth, $template, $config, $user, $nav_links; global $phpEx, $phpbb_root_path, $starttime; switch ($errno) diff --git a/phpBB/includes/page_header.php b/phpBB/includes/page_header.php index 55504d505b..a4806eaf4d 100644 --- a/phpBB/includes/page_header.php +++ b/phpBB/includes/page_header.php @@ -139,18 +139,8 @@ $total_online_users = $logged_visible_online + $logged_hidden_online + $guests_o if ($total_online_users > $config['record_online_users']) { - $config['record_online_users'] = $total_online_users; - $config['record_online_date'] = time(); - - $sql = "UPDATE " . CONFIG_TABLE . " - SET config_value = '$total_online_users' - WHERE config_name = 'record_online_users'"; - $db->sql_query($sql); - - $sql = "UPDATE " . CONFIG_TABLE . " - SET config_value = '" . $config['record_online_date'] . "' - WHERE config_name = 'record_online_date'"; - $db->sql_query($sql); + set_config('record_online_users', $total_online_users); + set_config('record_online_date', time()); } if ($total_online_users == 0) diff --git a/phpBB/includes/page_tail.php b/phpBB/includes/page_tail.php index 32de512e4f..9c7f03a330 100644 --- a/phpBB/includes/page_tail.php +++ b/phpBB/includes/page_tail.php @@ -51,6 +51,8 @@ $template->assign_vars(array( 'DEBUG_OUTPUT' => (defined('DEBUG')) ? $debug_output : '' )); + +$cache->save_cache(); $template->display('body'); exit; diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index b87a10b725..d642b79bd0 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -326,10 +326,7 @@ class session { // Less than 5 sessions, update gc timer ... else we want gc // called again to delete other sessions - $sql = "UPDATE " . CONFIG_TABLE . " - SET config_value = '$current_time' - WHERE config_name = 'session_last_gc'"; - $db->sql_query($sql); + set_config('session_last_gc', $current_time); } return; @@ -442,7 +439,9 @@ class user extends session AND t.template_id = s.template_id AND c.theme_id = s.style_id AND i.imageset_id = s.imageset_id"; - $result = $db->sql_query($sql); + + // Cache this query for 60 seconds + $result = $db->sql_query($sql, 60); if (!($this->theme = $db->sql_fetchrow($result))) { diff --git a/phpBB/install/install.php b/phpBB/install/install.php index cb33ca7c73..ef975423e2 100644 --- a/phpBB/install/install.php +++ b/phpBB/install/install.php @@ -690,6 +690,7 @@ else $config_data .= '$dbname = "' . $dbname . '";' . "\n"; $config_data .= '$dbuser = "' . $dbuser . '";' . "\n"; $config_data .= '$dbpasswd = "' . $dbpasswd . '";' . "\n\n"; + $config_data .= "\$acm_type = 'file';\n"; $config_data .= '$table_prefix = "' . $table_prefix . '";' . "\n\n"; $config_data .= 'define(\'PHPBB_INSTALLED\', true);'."\n\n"; $config_data .= '?' . '>'; // Done this to prevent highlighting editors getting confused! diff --git a/phpBB/posting.php b/phpBB/posting.php index 35e1893480..23d699fc7f 100644 --- a/phpBB/posting.php +++ b/phpBB/posting.php @@ -467,16 +467,10 @@ if (isset($post)) // post counts for index, etc. if ($mode == 'post') { - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . ($config['num_topics'] + 1) . "' - WHERE config_name = 'num_topics'"; - $db->sql_query($sql); + set_config('num_topics', $config['num_topics'] + 1); } - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . ($config['num_posts'] + 1) . "' - WHERE config_name = 'num_posts'"; - $db->sql_query($sql); + set_config('num_posts', $config['num_posts'] + 1); } // Topic notification |