Adapter
6 years ago
Schema
6 years ago
Adapter.php
6 years ago
AdapterInterface.php
6 years ago
BatchInsert.php
6 years ago
Schema.php
6 years ago
SchemaInterface.php
6 years ago
Settings.php
6 years ago
TransactionLevel.php
6 years ago
Adapter.php
133 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Db; |
| 10 | |
| 11 | use Zend_Db_Table; |
| 12 | |
| 13 | /** |
| 14 | */ |
| 15 | class Adapter |
| 16 | { |
| 17 | /** |
| 18 | * Create adapter |
| 19 | * |
| 20 | * @param string $adapterName database adapter name |
| 21 | * @param array $dbInfos database connection info |
| 22 | * @param bool $connect |
| 23 | * @return AdapterInterface |
| 24 | */ |
| 25 | public static function factory($adapterName, & $dbInfos, $connect = true) |
| 26 | { |
| 27 | if ($connect) { |
| 28 | if (isset($dbInfos['port']) && is_string($dbInfos['port']) && $dbInfos['port'][0] == '/') { |
| 29 | $dbInfos['unix_socket'] = $dbInfos['port']; |
| 30 | unset($dbInfos['host']); |
| 31 | unset($dbInfos['port']); |
| 32 | } |
| 33 | |
| 34 | // not used by Zend Framework |
| 35 | unset($dbInfos['tables_prefix']); |
| 36 | unset($dbInfos['adapter']); |
| 37 | unset($dbInfos['schema']); |
| 38 | } |
| 39 | |
| 40 | $className = self::getAdapterClassName($adapterName); |
| 41 | |
| 42 | // make sure not to pass any references otherwise they will modify $dbInfos |
| 43 | $infos = array(); |
| 44 | foreach ($dbInfos as $key => $val) { |
| 45 | $infos[$key] = $val; |
| 46 | } |
| 47 | |
| 48 | $adapter = new $className($infos); |
| 49 | |
| 50 | if ($connect) { |
| 51 | $adapter->getConnection(); |
| 52 | |
| 53 | Zend_Db_Table::setDefaultAdapter($adapter); |
| 54 | // we don't want the connection information to appear in the logs |
| 55 | $adapter->resetConfig(); |
| 56 | } |
| 57 | |
| 58 | return $adapter; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get adapter class name |
| 63 | * |
| 64 | * @param string $adapterName |
| 65 | * @return string |
| 66 | * @throws \Exception |
| 67 | */ |
| 68 | private static function getAdapterClassName($adapterName) |
| 69 | { |
| 70 | $className = 'Piwik\Db\Adapter\\' . str_replace(' ', '\\', ucwords(str_replace(array('_', '\\'), ' ', strtolower($adapterName)))); |
| 71 | if (!class_exists($className)) { |
| 72 | throw new \Exception(sprintf("Adapter '%s' is not valid. Maybe check that your Matomo configuration files in config/*.ini.php are readable by the webserver.", $adapterName)); |
| 73 | } |
| 74 | return $className; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get default port for named adapter |
| 79 | * |
| 80 | * @param string $adapterName |
| 81 | * @return int |
| 82 | */ |
| 83 | public static function getDefaultPortForAdapter($adapterName) |
| 84 | { |
| 85 | $className = self::getAdapterClassName($adapterName); |
| 86 | return call_user_func(array($className, 'getDefaultPort')); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get list of adapters |
| 91 | * |
| 92 | * @return array |
| 93 | */ |
| 94 | public static function getAdapters() |
| 95 | { |
| 96 | static $adapterNames = array( |
| 97 | // currently supported by Piwik |
| 98 | 'Pdo\Mysql', |
| 99 | 'Mysqli', |
| 100 | |
| 101 | // other adapters supported by Zend_Db |
| 102 | // 'Pdo_Pgsql', |
| 103 | // 'Pdo_Mssql', |
| 104 | // 'Sqlsrv', |
| 105 | // 'Pdo_Ibm', |
| 106 | // 'Db2', |
| 107 | // 'Pdo_Oci', |
| 108 | // 'Oracle', |
| 109 | ); |
| 110 | |
| 111 | $adapters = array(); |
| 112 | |
| 113 | foreach ($adapterNames as $adapterName) { |
| 114 | $className = '\Piwik\Db\Adapter\\' . $adapterName; |
| 115 | if (call_user_func(array($className, 'isEnabled'))) { |
| 116 | $adapters[strtoupper($adapterName)] = call_user_func(array($className, 'getDefaultPort')); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $adapters; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Checks if the available adapters are recommended by Piwik or not. |
| 125 | * @param string $adapterName |
| 126 | * @return bool |
| 127 | */ |
| 128 | public static function isRecommendedAdapter($adapterName) |
| 129 | { |
| 130 | return strtolower($adapterName) === 'pdo/mysql'; |
| 131 | } |
| 132 | } |
| 133 |