2012-08-30 08:00:17 +00:00
|
|
|
<?
|
|
|
|
if (!extension_loaded('mysqli')) {
|
|
|
|
error('Mysqli Extension not loaded.');
|
|
|
|
}
|
|
|
|
|
2013-03-17 08:00:17 +00:00
|
|
|
class Sphinxql extends mysqli {
|
2012-08-30 08:00:17 +00:00
|
|
|
private static $Connections = array();
|
|
|
|
private $Server;
|
|
|
|
private $Port;
|
|
|
|
private $Socket;
|
2012-09-15 08:00:25 +00:00
|
|
|
private $Ident;
|
2012-08-30 08:00:17 +00:00
|
|
|
private $Connected = false;
|
|
|
|
|
|
|
|
public static $Queries = array();
|
|
|
|
public static $Time = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-03-17 08:00:17 +00:00
|
|
|
* Initialize Sphinxql object
|
2012-08-30 08:00:17 +00:00
|
|
|
*
|
2012-09-15 08:00:25 +00:00
|
|
|
* @param string $Server server address or hostname
|
|
|
|
* @param int $Port listening port
|
|
|
|
* @param string $Socket Unix socket address, overrides $Server:$Port
|
2012-08-30 08:00:17 +00:00
|
|
|
*/
|
|
|
|
public function __construct($Server, $Port, $Socket) {
|
|
|
|
$this->Server = $Server;
|
|
|
|
$this->Port = $Port;
|
|
|
|
$this->Socket = $Socket;
|
|
|
|
$this->Ident = $this->get_ident($Server, $Port, $Socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create server ident based on connection information
|
|
|
|
*
|
2012-09-15 08:00:25 +00:00
|
|
|
* @param string $Server server address or hostname
|
|
|
|
* @param int $Port listening port
|
|
|
|
* @param string $Socket Unix socket address, overrides $Server:$Port
|
2012-08-30 08:00:17 +00:00
|
|
|
* @return identification string
|
|
|
|
*/
|
|
|
|
private function get_ident($Server, $Port, $Socket) {
|
2013-04-30 18:18:07 +00:00
|
|
|
if ($Socket) {
|
2012-08-30 08:00:17 +00:00
|
|
|
return $Socket;
|
|
|
|
} else {
|
|
|
|
return "$Server:$Port";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-17 08:00:17 +00:00
|
|
|
* Create Sphinxql object or return existing one
|
2012-08-30 08:00:17 +00:00
|
|
|
*
|
2012-09-15 08:00:25 +00:00
|
|
|
* @param string $Server server address or hostname
|
|
|
|
* @param int $Port listening port
|
|
|
|
* @param string $Socket Unix socket address, overrides $Server:$Port
|
2013-03-17 08:00:17 +00:00
|
|
|
* @return Sphinxql object
|
2012-08-30 08:00:17 +00:00
|
|
|
*/
|
|
|
|
public static function init_connection($Server, $Port, $Socket) {
|
|
|
|
$Ident = self::get_ident($Server, $Port, $Socket);
|
2013-04-30 18:18:07 +00:00
|
|
|
if (!isset(self::$Connections[$Ident])) {
|
2013-03-17 08:00:17 +00:00
|
|
|
self::$Connections[$Ident] = new Sphinxql($Server, $Port, $Socket);
|
2012-08-30 08:00:17 +00:00
|
|
|
}
|
|
|
|
return self::$Connections[$Ident];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-17 08:00:17 +00:00
|
|
|
* Connect the Sphinxql object to the Sphinx server
|
2012-08-30 08:00:17 +00:00
|
|
|
*/
|
2013-06-28 08:01:04 +00:00
|
|
|
public function sphconnect() {
|
2013-08-19 08:00:44 +00:00
|
|
|
if ($this->Connected || $this->connect_errno) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
global $Debug;
|
|
|
|
$Debug->set_flag("Connecting to Sphinx server $this->Ident");
|
|
|
|
for ($Attempt = 0; $Attempt < 3; $Attempt++) {
|
2012-08-30 08:00:17 +00:00
|
|
|
parent::__construct($this->Server, '', '', '', $this->Port, $this->Socket);
|
2013-08-19 08:00:44 +00:00
|
|
|
if (!$this->connect_errno) {
|
|
|
|
$this->Connected = true;
|
|
|
|
break;
|
2012-08-30 08:00:17 +00:00
|
|
|
}
|
2013-08-19 08:00:44 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
if ($this->connect_errno) {
|
|
|
|
$Errno = $this->connect_errno;
|
|
|
|
$Error = $this->connect_error;
|
|
|
|
$this->error("Connection failed. (".strval($Errno).": ".strval($Error).")");
|
|
|
|
$Debug->set_flag("Could not connect to Sphinx server $this->Ident. (".strval($Errno).": ".strval($Error).")");
|
|
|
|
} else {
|
|
|
|
$Debug->set_flag("Connected to Sphinx server $this->Ident");
|
2012-08-30 08:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a message to privileged users and optionally halt page processing
|
|
|
|
*
|
2012-09-15 08:00:25 +00:00
|
|
|
* @param string $Msg message to display
|
|
|
|
* @param bool $Halt halt page processing. Default is to continue processing the page
|
2013-03-17 08:00:17 +00:00
|
|
|
* @return Sphinxql object
|
2012-08-30 08:00:17 +00:00
|
|
|
*/
|
|
|
|
public function error($Msg, $Halt = false) {
|
|
|
|
global $Debug;
|
|
|
|
$ErrorMsg = 'SphinxQL ('.$this->Ident.'): '.strval($Msg);
|
2012-08-31 08:00:22 +00:00
|
|
|
$Debug->analysis('SphinxQL Error', $ErrorMsg, 3600*24);
|
2013-04-30 18:18:07 +00:00
|
|
|
if ($Halt === true && (DEBUG_MODE || check_perms('site_debug'))) {
|
2012-08-30 08:00:17 +00:00
|
|
|
echo '<pre>'.display_str($ErrorMsg).'</pre>';
|
|
|
|
die();
|
2013-04-30 18:18:07 +00:00
|
|
|
} elseif ($Halt === true) {
|
2012-08-30 08:00:17 +00:00
|
|
|
error('-1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape special characters before sending them to the Sphinx server.
|
|
|
|
* Two escapes needed because the first one is eaten up by the mysql driver.
|
|
|
|
*
|
2012-09-15 08:00:25 +00:00
|
|
|
* @param string $String string to escape
|
2012-08-30 08:00:17 +00:00
|
|
|
* @return escaped string
|
|
|
|
*/
|
|
|
|
public function escape_string($String) {
|
|
|
|
return strtr($String, array(
|
|
|
|
'('=>'\\\\(',
|
|
|
|
')'=>'\\\\)',
|
|
|
|
'|'=>'\\\\|',
|
|
|
|
'-'=>'\\\\-',
|
|
|
|
'@'=>'\\\\@',
|
|
|
|
'~'=>'\\\\~',
|
|
|
|
'&'=>'\\\\&',
|
|
|
|
'\''=>'\\\'',
|
|
|
|
'<'=>'\\\\<',
|
|
|
|
'!'=>'\\\\!',
|
|
|
|
'"'=>'\\\\"',
|
2012-09-28 08:00:21 +00:00
|
|
|
'/'=>'\\\\/',
|
2012-11-01 08:00:21 +00:00
|
|
|
'*'=>'\\\\*',
|
|
|
|
'$'=>'\\\\$',
|
|
|
|
'^'=>'\\\\^',
|
2012-08-30 08:00:17 +00:00
|
|
|
'\\'=>'\\\\\\\\')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register sent queries globally for later retrieval by debug functions
|
|
|
|
*
|
2012-09-15 08:00:25 +00:00
|
|
|
* @param string $QueryString query text
|
|
|
|
* @param param $QueryProcessTime time building and processing the query
|
2012-08-30 08:00:17 +00:00
|
|
|
*/
|
|
|
|
public function register_query($QueryString, $QueryProcessTime) {
|
2013-03-17 08:00:17 +00:00
|
|
|
Sphinxql::$Queries[] = array($QueryString, $QueryProcessTime);
|
|
|
|
Sphinxql::$Time += $QueryProcessTime;
|
2012-08-30 08:00:17 +00:00
|
|
|
}
|
|
|
|
}
|