2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2008-04-09 01:10:56 +00:00
|
|
|
require_once "compat.inc.php";
|
2008-04-01 10:11:36 +00:00
|
|
|
$ADODB_CACHE_DIR=sys_get_temp_dir();
|
2007-04-16 11:58:25 +00:00
|
|
|
require_once "lib/adodb/adodb.inc.php";
|
2009-01-04 14:34:27 +00:00
|
|
|
require_once "lib/adodb/adodb-exceptions.inc.php";
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/* Querylet {{{
|
|
|
|
* A fragment of a query, used to build large search queries
|
|
|
|
*/
|
|
|
|
class Querylet {
|
2007-04-16 11:58:25 +00:00
|
|
|
var $sql;
|
|
|
|
var $variables;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-02-06 17:24:08 +00:00
|
|
|
public function Querylet($sql, $variables=array()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql = $sql;
|
|
|
|
$this->variables = $variables;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function append($querylet) {
|
2008-02-06 17:24:08 +00:00
|
|
|
assert(!is_null($querylet));
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql .= $querylet->sql;
|
|
|
|
$this->variables = array_merge($this->variables, $querylet->variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function append_sql($sql) {
|
|
|
|
$this->sql .= $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_variable($var) {
|
|
|
|
$this->variables[] = $var;
|
|
|
|
}
|
2008-05-19 15:59:58 +00:00
|
|
|
}
|
|
|
|
class TagQuerylet {
|
|
|
|
var $tag;
|
|
|
|
var $positive;
|
|
|
|
|
|
|
|
public function TagQuerylet($tag, $positive) {
|
|
|
|
$this->tag = $tag;
|
|
|
|
$this->positive = $positive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class ImgQuerylet {
|
|
|
|
var $qlet;
|
|
|
|
var $positive;
|
|
|
|
|
|
|
|
public function ImgQuerylet($qlet, $positive) {
|
|
|
|
$this->qlet = $qlet;
|
|
|
|
$this->positive = $positive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2008-05-18 02:14:51 +00:00
|
|
|
// {{{ dbengines
|
|
|
|
class DBEngine {
|
|
|
|
var $name = null;
|
|
|
|
var $auto_increment = null;
|
|
|
|
var $create_table_extras = "";
|
|
|
|
}
|
|
|
|
class MySQL extends DBEngine {
|
|
|
|
var $name = "mysql";
|
|
|
|
var $auto_increment = "INTEGER PRIMARY KEY auto_increment";
|
|
|
|
var $create_table_extras = "TYPE=INNODB DEFAULT CHARSET='utf8'";
|
2008-08-11 20:27:24 +00:00
|
|
|
|
|
|
|
function init($db) {
|
|
|
|
$db->Execute("SET NAMES utf8;");
|
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
|
|
|
class PostgreSQL extends DBEngine {
|
|
|
|
var $name = "pgsql";
|
|
|
|
var $auto_increment = "SERIAL PRIMARY KEY";
|
2008-08-11 20:27:24 +00:00
|
|
|
|
|
|
|
function init($db) {
|
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
|
|
|
//}}}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/*
|
2009-01-03 20:32:57 +00:00
|
|
|
* A class for controlled database access
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
class Database {
|
|
|
|
var $db;
|
|
|
|
var $extensions;
|
2008-04-30 11:26:12 +00:00
|
|
|
var $cache_hits = 0, $cache_misses = 0;
|
2008-05-18 02:14:51 +00:00
|
|
|
var $engine = null;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-12-11 18:37:11 +00:00
|
|
|
/*
|
|
|
|
* Create a new database object using connection info
|
|
|
|
* stored in config.php in the root shimmie folder
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function Database() {
|
|
|
|
if(is_readable("config.php")) {
|
|
|
|
require_once "config.php";
|
2008-05-18 02:14:51 +00:00
|
|
|
$this->engine = new MySQL();
|
2007-07-28 14:15:03 +00:00
|
|
|
$this->db = @NewADOConnection($database_dsn);
|
2008-04-30 11:26:12 +00:00
|
|
|
$this->use_memcache = isset($memcache);
|
2007-07-28 14:15:03 +00:00
|
|
|
if($this->db) {
|
|
|
|
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
|
2008-08-11 20:27:24 +00:00
|
|
|
$this->engine->init($this->db);
|
2007-07-28 14:15:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$version = VERSION;
|
|
|
|
print "
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Internal error - Shimmie-$version</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Internal error: Could not connect to database
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
";
|
|
|
|
exit;
|
|
|
|
}
|
2008-04-30 11:26:12 +00:00
|
|
|
if($this->use_memcache) {
|
|
|
|
$this->memcache = new Memcache;
|
|
|
|
$this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
header("Location: install.php");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-30 11:26:12 +00:00
|
|
|
// memcache {{{
|
|
|
|
public function cache_get($key) {
|
2008-07-25 17:42:24 +00:00
|
|
|
assert(!is_null($key));
|
2008-04-30 11:26:12 +00:00
|
|
|
if($this->use_memcache) {
|
|
|
|
$val = $this->memcache->get($key);
|
|
|
|
if($val) {
|
|
|
|
$this->cache_hits++;
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->cache_misses++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cache_set($key, $val, $time=0) {
|
2008-07-25 17:42:24 +00:00
|
|
|
assert(!is_null($key));
|
2008-04-30 11:26:12 +00:00
|
|
|
if($this->use_memcache) {
|
|
|
|
$this->memcache->set($key, $val, false, $time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cache_delete($key) {
|
2008-07-25 17:42:24 +00:00
|
|
|
assert(!is_null($key));
|
2008-04-30 11:26:12 +00:00
|
|
|
if($this->use_memcache) {
|
|
|
|
$this->memcache->delete($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2009-01-04 16:15:00 +00:00
|
|
|
// safety wrapping {{{
|
2007-05-23 23:26:11 +00:00
|
|
|
public function execute($query, $args=array()) {
|
2007-10-21 17:13:57 +00:00
|
|
|
$result = $this->db->Execute($query, $args);
|
|
|
|
if($result === False) {
|
|
|
|
print "SQL Error: " . $this->db->ErrorMsg();
|
|
|
|
print "<br>Query: $query";
|
|
|
|
print "<br>Args: "; print_r($args);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return $result;
|
2007-08-08 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 00:22:19 +00:00
|
|
|
public function get_all($query, $args=array()) {
|
|
|
|
$result = $this->db->GetAll($query, $args);
|
|
|
|
if($result === False) {
|
|
|
|
print "SQL Error: " . $this->db->ErrorMsg();
|
|
|
|
print "<br>Query: $query";
|
|
|
|
print "<br>Args: "; print_r($args);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2009-01-04 16:15:00 +00:00
|
|
|
|
2008-08-23 12:05:24 +00:00
|
|
|
public function get_row($query, $args=array()) {
|
|
|
|
$result = $this->db->GetRow($query, $args);
|
|
|
|
if($result === False) {
|
|
|
|
print "SQL Error: " . $this->db->ErrorMsg();
|
|
|
|
print "<br>Query: $query";
|
|
|
|
print "<br>Args: "; print_r($args);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if(count($result) == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-27 15:30:44 +00:00
|
|
|
public function upgrade_schema($filename) {
|
2008-05-18 02:14:51 +00:00
|
|
|
$this->install_schema($filename);
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-05-18 02:14:51 +00:00
|
|
|
public function install_schema($filename) {
|
2008-01-27 15:30:44 +00:00
|
|
|
//print "<br>upgrading $filename";
|
2008-01-05 00:22:19 +00:00
|
|
|
|
2007-08-08 05:47:23 +00:00
|
|
|
global $config;
|
2008-01-27 15:30:44 +00:00
|
|
|
if($config->get_bool("in_upgrade")) return;
|
|
|
|
$config->set_bool("in_upgrade", true);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-01-27 15:30:44 +00:00
|
|
|
require_once "lib/adodb/adodb-xmlschema03.inc.php";
|
|
|
|
$schema = new adoSchema($this->db);
|
|
|
|
$sql = $schema->ParseSchema($filename);
|
|
|
|
//echo "<pre>"; var_dump($sql); echo "</pre>";
|
|
|
|
$result = $schema->ExecuteSchema();
|
2007-08-08 05:47:23 +00:00
|
|
|
|
2008-01-27 15:30:44 +00:00
|
|
|
if(!$result) {
|
|
|
|
die("Error creating tables from XML schema ($filename)");
|
2007-05-17 03:48:34 +00:00
|
|
|
}
|
2008-01-27 15:30:44 +00:00
|
|
|
|
|
|
|
$config->set_bool("in_upgrade", false);
|
2007-05-17 03:48:34 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
?>
|