Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

various changes

avoid instantiating Database when not needed, changed
Database_Connection indentation character,
  • Loading branch information...
commit 93847d35929021479b66d0995510f7efb3d7829c 1 parent 2078906
Josh Freeman authored
5 check-generation.php
View
@@ -5,6 +5,7 @@
set_time_limit(0);
file_put_contents(RECOMBINING_FLAG, 1);
+$db = new Database();
$currentGenomes = $db->getCurrentGenerationGenomes();// select genome IDs from current generation
$generationIDs = '';
@@ -57,14 +58,14 @@
$timeStarted = 0;
$pagesVisited = array();
}
-
+
if ($row['data']['type'] == 'click') {
if ($row['data']['target'] == 'A') {
$f += 10;// +10 for clicking on an actual link
if (isset($analytics[$i + 1]) && $analytics[$i + 1]['data']['type'] == 'load') $f += 10;// +10 for that link going to a new page
} else $f -= 10;// -10 for clicking somewhere other than a link
}
-
+
$fitness[$row['genome']] += $f;
}
18 common.php
View
@@ -1,17 +1,31 @@
<?php
ini_set('display_errors', 0);
if ($_SERVER['SERVER_NAME'] == 'evolve.joshfreeman.ca') ini_set('include_path', '/home/jfreem/php'.PATH_SEPARATOR.ini_get('include_path'));
+
define("INCLUDES_DIR", $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR);
define("PHP_INCLUDES_DIR", INCLUDES_DIR.'php'.DIRECTORY_SEPARATOR);
define("PAGES_INCLUDES_DIR", INCLUDES_DIR.'pages'.DIRECTORY_SEPARATOR);
define("CACHE_DIR", $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR);
-if (!is_dir(CACHE_DIR)) mkdir(CACHE_DIR, 0755);
define("RECOMBINING_FLAG", CACHE_DIR.'RECOMBINING_FLAG');
define("ABOUT_PAGE_CACHE", CACHE_DIR.'about-cache.html');
define("GENOME_PAGE", PAGES_INCLUDES_DIR.'genome.php');
define("ABOUT_PAGE", PAGES_INCLUDES_DIR.'about.php');
define("NEW_GENERATION_PAGE", PAGES_INCLUDES_DIR.'new-generation.html');
+define("NOT_FOUND_PAGE", PAGES_INCLUDES_DIR.'404.html');
+
+if (!is_dir(CACHE_DIR)) mkdir(CACHE_DIR, 0755);
require_once PHP_INCLUDES_DIR.'Database.php';
session_start();
-$db = new Database();
+
+function newSession($genomeID) {
+ clearSession();
+ $_SESSION['genome'] = $genomeID;
+ $_SESSION['user'] = md5(time() . $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] . rand(0, 1000));
+}
+
+function clearSession() {
+ session_destroy();
+ session_start();
+ session_regenerate_id();
+}
?>
10 includes/pages/404.html
View
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <title>Not Found</title>
+ </head>
+ <body>
+ <p>Page not found!</p>
+ </body>
+</html>
2  includes/pages/new-generation.html
View
@@ -6,6 +6,6 @@
<title>Generating new generation</title>
</head>
<body>
- Generating new generation, please wait...<img src="/images/loading.gif" alt="loading">
+ <p>Generating new generation, please wait...<img src="/images/loading.gif" alt="loading"></p>
</body>
</html>
138 includes/php/Database_Connection.php
View
@@ -1,77 +1,71 @@
<?php
class Database_Connection {
- /**
- * @var resource The variable to store the database resource for the current connection.
- */
- private $db;
-
- /**
- * Opens the database connection.
- *
- * @param string $database The name of the database to select.
- * @param string $username The username to use to connect to the database.
- * @param string $password The password to use to connect to the database.
- * @param string $address The address of the database. Defaults to localhost.
- *
- * @return bool True on success, throws Exception on error.
- */
- function __construct($database, $username, $password, $address = 'localhost', $new_link = true) {
- if ($this->db = @mysql_connect($address, $username, $password, $new_link)) {
- if (@mysql_select_db($database, $this->db)) return true;
- else throw new Exception("Could not select '$database'.");
- }
- throw new Exception("Could not connect to '$address' as '$username'. Error: ".mysql_error().' Code: '.mysql_errno());
- }
-
- /**
- * Closes the database connection. Not always necessary but useful for minimizing script resources.
- *
- * @return bool True on success, throws Exception on error.
- */
- function close() {
- if ($this->db) {
- if (@mysql_close($this->db)) return true;
- else throw new Exception("Could not close connection.");
- }
- throw new Exception("No database connection found.");
- }
-
- /**
- * Queries the database using the specified MySQL query.
- *
- * @param string $query The query.
- * @global int _total_queries Sets and/or increments $_total_queries for debug purposes.
- * @global array _querylog Sets and/or adds to $_querylog for debug purposes.
- *
- * @return array Array of result rows, throws Exception on error.
- */
- function q($query) {
- if ($this->db) {
- $querytype = strtoupper(substr($query, 0, strpos($query, ' ')));
- $result = @mysql_query($query, $this->db);
-
- global $_total_queries, $_querylog;
- $_total_queries++;
- $backtrace = debug_backtrace();
- $file = substr($backtrace[0]['file'], strrpos($backtrace[0]['file'], '/')+1);
- $_querylog[] = "$file > {$backtrace[0]['line']} : $query";
-
- if ($result) {
- if ($querytype == 'SELECT' || $querytype == 'SHOW' || $querytype == 'DESCRIBE' || $querytype == 'EXPLAIN') {
- $output = array();
- if (mysql_num_rows($result) > 0) while($row = mysql_fetch_assoc($result)) $output[] = $row;
- mysql_free_result($result);
- return $output;
- }
- return true; // result is positive, but don't need to return anything
- }
- throw new Exception("Query could not be completed.");
- }
- throw new Exception("No database connection found.");
- }
+ /**
+ * @var resource The variable to store the database resource for the current connection.
+ */
+ private $db;
- function mysql_real_escape_string($str) {
- return mysql_real_escape_string($str, $this->db);
- }
+ /**
+ * Opens the database connection.
+ *
+ * @param string $database The name of the database to select.
+ * @param string $username The username to use to connect to the database.
+ * @param string $password The password to use to connect to the database.
+ * @param string $address The address of the database. Defaults to localhost.
+ *
+ * @return bool True on success, throws Exception on error.
+ */
+ function __construct($database, $username, $password, $address = 'localhost', $new_link = true) {
+ if ($this->db = @mysql_connect($address, $username, $password, $new_link)) {
+ if (@mysql_select_db($database, $this->db)) return true;
+ throw new Exception("Could not select '$database'.");
+ }
+ throw new Exception("Could not connect to '$address' as '$username'. Error: ".mysql_error().' Code: '.mysql_errno());
+ }
+
+ /**
+ * Closes the database connection. Not always necessary but useful for minimizing script resources.
+ *
+ * @return bool True on success, throws Exception on error.
+ */
+ function close() {
+ if (!$this->db) throw new Exception("No database connection found.");
+
+ if (@mysql_close($this->db)) return true;
+ else throw new Exception("Could not close connection.");
+ }
+
+ /**
+ * Queries the database using the specified MySQL query.
+ *
+ * @param string $query The query.
+ * @global int _total_queries Sets and/or increments $_total_queries for debug purposes.
+ * @global array _querylog Sets and/or adds to $_querylog for debug purposes.
+ *
+ * @return array Array of result rows, throws Exception on error.
+ */
+ function q($query) {
+ if (!$this->db) throw new Exception("No database connection found.");
+
+ $querytype = strtoupper(substr($query, 0, strpos($query, ' ')));
+ $result = @mysql_query($query, $this->db);
+
+ global $_total_queries, $_querylog;
+ $_total_queries++;
+ $backtrace = debug_backtrace();
+ $file = substr($backtrace[0]['file'], strrpos($backtrace[0]['file'], '/')+1);
+ $_querylog[] = "$file > {$backtrace[0]['line']} : $query";
+
+ if (!$result) throw new Exception("Query could not be completed.");
+
+ if ($querytype == 'SELECT' || $querytype == 'SHOW' || $querytype == 'DESCRIBE' || $querytype == 'EXPLAIN') {
+ $output = array();
+ if (mysql_num_rows($result) > 0) while($row = mysql_fetch_assoc($result)) $output[] = $row;
+ mysql_free_result($result);
+ return $output;
+ }
+
+ return true; // result is positive, but don't need to return anything
+ }
}
?>
27 index.php
View
@@ -1,7 +1,8 @@
<?php
require_once 'common.php';
-$page = isset($_GET['page']) ? preg_replace('/[^a-z0-9-]/i', '', $_GET['page']) : false;
+// home page (no url params) should show next genome to test
+$page = isset($_GET['page']) ? preg_replace('/[^a-z0-9-]/i', '', $_GET['page']) : 'genome';
if ($page == 'genome') {
// check if new generation is being generated. if so print loading screen which rechecks every 5 seconds.
@@ -9,7 +10,7 @@
include NEW_GENERATION_PAGE;
exit();
}
-
+ $db = new Database();
$genomeID = preg_replace('/[^0-9]/', '', @$_GET['genome']);
// don't allow users to go directly to another /genome/ url
// restart if no ID is set, or doesn't match session ID
@@ -22,9 +23,10 @@
$genomeID = preg_replace('/[^0-9]/', '', @$_GET['genome']);
if (empty($genomeID)) {
header('HTTP/1.1 404 Not Found');
- echo 'Not Found';
+ include NOT_FOUND_PAGE;
exit();
}
+ $db = new Database();
$_SESSION['page'] = isset($_GET['content']) ? preg_replace('/[^a-z0-9.-]/i', '', $_GET['content']) : 'about.html';
$genomePageCache = CACHE_DIR.md5($genomeID.$_SESSION['page']).'.html';
$db->saveAnalytics(array('type' => 'load'));
@@ -38,7 +40,8 @@
} else if ($page == 'about') {
clearSession();
- if (!file_exists(RECOMBINING_FLAG) && (!file_exists(ABOUT_PAGE_CACHE))) {
+ if (!file_exists(RECOMBINING_FLAG) && !file_exists(ABOUT_PAGE_CACHE)) {
+ $db = new Database();
ob_start();
include ABOUT_PAGE;
$aboutpage .= ob_get_clean();
@@ -46,20 +49,10 @@
}
include_once ABOUT_PAGE_CACHE;
-} else newSession($db->getNextGenomeID());// any other url redirects to genome
-
-function newSession($genomeID) {
- clearSession();
- $_SESSION['genome'] = $genomeID;
- $_SESSION['user'] = md5(time() . $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] . rand(0, 1000));
- header('HTTP/1.1 302 Found');
- header('Location: http://'.$_SERVER['SERVER_NAME'].'/genome/'.$genomeID);
+} else {
+ header('HTTP/1.1 404 Not Found');// any other url is invalid
+ include NOT_FOUND_PAGE;
exit();
}
-function clearSession() {
- session_destroy();
- session_start();
- session_regenerate_id();
-}
?>
1  save-analytics.php
View
@@ -1,6 +1,7 @@
<?php
require_once 'common.php';
+$db = new Database();
if (!isset($_POST['data'])) exit();
$analytics = array();
Please sign in to comment.
Something went wrong with that request. Please try again.