add piwik installation

This commit is contained in:
coderkun 2014-04-25 03:56:02 +02:00
commit 8c5d4f0c31
3197 changed files with 563902 additions and 0 deletions

View file

@ -0,0 +1,20 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Tracker\Db;
use Exception;
/**
* Database Exception
*
*/
class DbException extends Exception
{
}

View file

@ -0,0 +1,279 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Tracker\Db;
use Exception;
use Piwik\Tracker\Db;
/**
* mysqli wrapper
*
*/
class Mysqli extends Db
{
protected $connection = null;
protected $host;
protected $port;
protected $socket;
protected $dbname;
protected $username;
protected $password;
protected $charset;
/**
* Builds the DB object
*
* @param array $dbInfo
* @param string $driverName
*/
public function __construct($dbInfo, $driverName = 'mysql')
{
if (isset($dbInfo['unix_socket']) && $dbInfo['unix_socket'][0] == '/') {
$this->host = null;
$this->port = null;
$this->socket = $dbInfo['unix_socket'];
} else if ($dbInfo['port'][0] == '/') {
$this->host = null;
$this->port = null;
$this->socket = $dbInfo['port'];
} else {
$this->host = $dbInfo['host'];
$this->port = $dbInfo['port'];
$this->socket = null;
}
$this->dbname = $dbInfo['dbname'];
$this->username = $dbInfo['username'];
$this->password = $dbInfo['password'];
$this->charset = isset($dbInfo['charset']) ? $dbInfo['charset'] : null;
}
/**
* Destructor
*/
public function __destruct()
{
$this->connection = null;
}
/**
* Connects to the DB
*
* @throws Exception|DbException if there was an error connecting the DB
*/
public function connect()
{
if (self::$profiling) {
$timer = $this->initProfiler();
}
$this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->dbname, $this->port, $this->socket);
if (!$this->connection || mysqli_connect_errno()) {
throw new DbException("Connect failed: " . mysqli_connect_error());
}
if ($this->charset && !mysqli_set_charset($this->connection, $this->charset)) {
throw new DbException("Set Charset failed: " . mysqli_error($this->connection));
}
$this->password = '';
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile('connect', $timer);
}
}
/**
* Disconnects from the server
*/
public function disconnect()
{
mysqli_close($this->connection);
$this->connection = null;
}
/**
* Returns an array containing all the rows of a query result, using optional bound parameters.
*
* @see query()
*
* @param string $query Query
* @param array $parameters Parameters to bind
* @return array
* @throws Exception|DbException if an exception occured
*/
public function fetchAll($query, $parameters = array())
{
try {
if (self::$profiling) {
$timer = $this->initProfiler();
}
$rows = array();
$query = $this->prepare($query, $parameters);
$rs = mysqli_query($this->connection, $query);
if (is_bool($rs)) {
throw new DbException('fetchAll() failed: ' . mysqli_error($this->connection) . ' : ' . $query);
}
while ($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_free_result($rs);
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile($query, $timer);
}
return $rows;
} catch (Exception $e) {
throw new DbException("Error query: " . $e->getMessage());
}
}
/**
* Returns the first row of a query result, using optional bound parameters.
*
* @see query()
*
* @param string $query Query
* @param array $parameters Parameters to bind
*
* @return array
*
* @throws DbException if an exception occurred
*/
public function fetch($query, $parameters = array())
{
try {
if (self::$profiling) {
$timer = $this->initProfiler();
}
$query = $this->prepare($query, $parameters);
$rs = mysqli_query($this->connection, $query);
if (is_bool($rs)) {
throw new DbException('fetch() failed: ' . mysqli_error($this->connection) . ' : ' . $query);
}
$row = mysqli_fetch_array($rs, MYSQLI_ASSOC);
mysqli_free_result($rs);
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile($query, $timer);
}
return $row;
} catch (Exception $e) {
throw new DbException("Error query: " . $e->getMessage());
}
}
/**
* Executes a query, using optional bound parameters.
*
* @param string $query Query
* @param array|string $parameters Parameters to bind array('idsite'=> 1)
*
* @return bool|resource false if failed
* @throws DbException if an exception occurred
*/
public function query($query, $parameters = array())
{
if (is_null($this->connection)) {
return false;
}
try {
if (self::$profiling) {
$timer = $this->initProfiler();
}
$query = $this->prepare($query, $parameters);
$result = mysqli_query($this->connection, $query);
if (!is_bool($result)) {
mysqli_free_result($result);
}
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile($query, $timer);
}
return $result;
} catch (Exception $e) {
throw new DbException("Error query: " . $e->getMessage() . "
In query: $query
Parameters: " . var_export($parameters, true));
}
}
/**
* Returns the last inserted ID in the DB
*
* @return int
*/
public function lastInsertId()
{
return mysqli_insert_id($this->connection);
}
/**
* Input is a prepared SQL statement and parameters
* Returns the SQL statement
*
* @param string $query
* @param array $parameters
* @return string
*/
private function prepare($query, $parameters)
{
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
$parameters = array($parameters);
}
$this->paramNb = 0;
$this->params = & $parameters;
$query = preg_replace_callback('/\?/', array($this, 'replaceParam'), $query);
return $query;
}
public function replaceParam($match)
{
$param = & $this->params[$this->paramNb];
$this->paramNb++;
if ($param === null) {
return 'NULL';
} else {
return "'" . addslashes($param) . "'";
}
}
/**
* Test error number
*
* @param Exception $e
* @param string $errno
* @return bool
*/
public function isErrNo($e, $errno)
{
return mysqli_errno($this->connection) == $errno;
}
/**
* Return number of affected rows in last query
*
* @param mixed $queryResult Result from query()
* @return int
*/
public function rowCount($queryResult)
{
return mysqli_affected_rows($this->connection);
}
}

View file

@ -0,0 +1,237 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Tracker\Db\Pdo;
use Exception;
use PDO;
use PDOException;
use PDOStatement;
use Piwik\Tracker\Db;
use Piwik\Tracker\Db\DbException;
/**
* PDO MySQL wrapper
*
*/
class Mysql extends Db
{
/**
* @var PDO
*/
protected $connection = null;
protected $dsn;
protected $username;
protected $password;
protected $charset;
/**
* Builds the DB object
*
* @param array $dbInfo
* @param string $driverName
*/
public function __construct($dbInfo, $driverName = 'mysql')
{
if (isset($dbInfo['unix_socket']) && $dbInfo['unix_socket'][0] == '/') {
$this->dsn = $driverName . ':dbname=' . $dbInfo['dbname'] . ';unix_socket=' . $dbInfo['unix_socket'];
} else if (!empty($dbInfo['port']) && $dbInfo['port'][0] == '/') {
$this->dsn = $driverName . ':dbname=' . $dbInfo['dbname'] . ';unix_socket=' . $dbInfo['port'];
} else {
$this->dsn = $driverName . ':dbname=' . $dbInfo['dbname'] . ';host=' . $dbInfo['host'] . ';port=' . $dbInfo['port'];
}
$this->username = $dbInfo['username'];
$this->password = $dbInfo['password'];
$this->charset = isset($dbInfo['charset']) ? $dbInfo['charset'] : null;
}
public function __destruct()
{
$this->connection = null;
}
/**
* Connects to the DB
*
* @throws Exception if there was an error connecting the DB
*/
public function connect()
{
if (self::$profiling) {
$timer = $this->initProfiler();
}
$this->connection = @new PDO($this->dsn, $this->username, $this->password, $config = array());
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// we may want to setAttribute(PDO::ATTR_TIMEOUT ) to a few seconds (default is 60) in case the DB is locked
// the piwik.php would stay waiting for the database... bad!
// we delete the password from this object "just in case" it could be printed
$this->password = '';
/*
* Lazy initialization via MYSQL_ATTR_INIT_COMMAND depends
* on mysqlnd support, PHP version, and OS.
* see ZF-7428 and http://bugs.php.net/bug.php?id=47224
*/
if (!empty($this->charset)) {
$sql = "SET NAMES '" . $this->charset . "'";
$this->connection->exec($sql);
}
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile('connect', $timer);
}
}
/**
* Disconnects from the server
*/
public function disconnect()
{
$this->connection = null;
}
/**
* Returns an array containing all the rows of a query result, using optional bound parameters.
*
* @param string $query Query
* @param array $parameters Parameters to bind
* @return array|bool
* @see query()
* @throws Exception|DbException if an exception occurred
*/
public function fetchAll($query, $parameters = array())
{
try {
$sth = $this->query($query, $parameters);
if ($sth === false) {
return false;
}
return $sth->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new DbException("Error query: " . $e->getMessage());
}
}
/**
* Fetches the first column of all SQL result rows as an array.
*
* @param string $sql An SQL SELECT statement.
* @param mixed $bind Data to bind into SELECT placeholders.
* @throws \Piwik\Tracker\Db\DbException
* @return string
*/
public function fetchCol($sql, $bind = array())
{
try {
$sth = $this->query($sql, $bind);
if ($sth === false) {
return false;
}
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
return $result;
} catch (PDOException $e) {
throw new DbException("Error query: " . $e->getMessage());
}
}
/**
* Returns the first row of a query result, using optional bound parameters.
*
* @param string $query Query
* @param array $parameters Parameters to bind
* @return bool|mixed
* @see query()
* @throws Exception|DbException if an exception occurred
*/
public function fetch($query, $parameters = array())
{
try {
$sth = $this->query($query, $parameters);
if ($sth === false) {
return false;
}
return $sth->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new DbException("Error query: " . $e->getMessage());
}
}
/**
* Executes a query, using optional bound parameters.
*
* @param string $query Query
* @param array|string $parameters Parameters to bind array('idsite'=> 1)
* @return PDOStatement|bool PDOStatement or false if failed
* @throws DbException if an exception occured
*/
public function query($query, $parameters = array())
{
if (is_null($this->connection)) {
return false;
}
try {
if (self::$profiling) {
$timer = $this->initProfiler();
}
if (!is_array($parameters)) {
$parameters = array($parameters);
}
$sth = $this->connection->prepare($query);
$sth->execute($parameters);
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile($query, $timer);
}
return $sth;
} catch (PDOException $e) {
throw new DbException("Error query: " . $e->getMessage() . "
In query: $query
Parameters: " . var_export($parameters, true));
}
}
/**
* Returns the last inserted ID in the DB
* Wrapper of PDO::lastInsertId()
*
* @return int
*/
public function lastInsertId()
{
return $this->connection->lastInsertId();
}
/**
* Test error number
*
* @param Exception $e
* @param string $errno
* @return bool
*/
public function isErrNo($e, $errno)
{
if (preg_match('/([0-9]{4})/', $e->getMessage(), $match)) {
return $match[1] == $errno;
}
return false;
}
/**
* Return number of affected rows in last query
*
* @param mixed $queryResult Result from query()
* @return int
*/
public function rowCount($queryResult)
{
return $queryResult->rowCount();
}
}

View file

@ -0,0 +1,116 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Tracker\Db\Pdo;
use Exception;
use PDO;
/**
* PDO PostgreSQL wrapper
*
*/
class Pgsql extends Mysql
{
/**
* Builds the DB object
*
* @param array $dbInfo
* @param string $driverName
*/
public function __construct($dbInfo, $driverName = 'pgsql')
{
parent::__construct($dbInfo, $driverName);
}
/**
* Connects to the DB
*
* @throws Exception if there was an error connecting the DB
*/
public function connect()
{
if (self::$profiling) {
$timer = $this->initProfiler();
}
$this->connection = new PDO($this->dsn, $this->username, $this->password, $config = array());
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// we may want to setAttribute(PDO::ATTR_TIMEOUT ) to a few seconds (default is 60) in case the DB is locked
// the piwik.php would stay waiting for the database... bad!
// we delete the password from this object "just in case" it could be printed
$this->password = '';
if (!empty($this->charset)) {
$sql = "SET NAMES '" . $this->charset . "'";
$this->connection->exec($sql);
}
if (self::$profiling && isset($timer)) {
$this->recordQueryProfile('connect', $timer);
}
}
/**
* Test error number
*
* @param Exception $e
* @param string $errno
* @return bool
*/
public function isErrNo($e, $errno)
{
// map MySQL driver-specific error codes to PostgreSQL SQLSTATE
$map = array(
// MySQL: Unknown database '%s'
// PostgreSQL: database "%s" does not exist
'1049' => '08006',
// MySQL: Table '%s' already exists
// PostgreSQL: relation "%s" already exists
'1050' => '42P07',
// MySQL: Unknown column '%s' in '%s'
// PostgreSQL: column "%s" does not exist
'1054' => '42703',
// MySQL: Duplicate column name '%s'
// PostgreSQL: column "%s" of relation "%s" already exists
'1060' => '42701',
// MySQL: Duplicate entry '%s' for key '%s'
// PostgreSQL: duplicate key violates unique constraint
'1062' => '23505',
// MySQL: Can't DROP '%s'; check that column/key exists
// PostgreSQL: index "%s" does not exist
'1091' => '42704',
// MySQL: Table '%s.%s' doesn't exist
// PostgreSQL: relation "%s" does not exist
'1146' => '42P01',
);
if (preg_match('/([0-9]{2}[0-9P][0-9]{2})/', $e->getMessage(), $match)) {
return $match[1] == $map[$errno];
}
return false;
}
/**
* Return number of affected rows in last query
*
* @param mixed $queryResult Result from query()
* @return int
*/
public function rowCount($queryResult)
{
return $queryResult->rowCount();
}
}