PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/MobileFrontend/library/WURFL/Cache/MysqlCacheProvider.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 189 lines | 115 code | 13 blank | 61 comment | 16 complexity | 4298752507d9167dcb94f8bd594492b9 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2011 ScientiaMobile, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * Refer to the COPYING file distributed with this package.
  11. *
  12. * @category WURFL
  13. * @package WURFL_Cache
  14. * @copyright ScientiaMobile, Inc.
  15. * @license GNU Affero General Public License
  16. * @version $id$
  17. */
  18. /**
  19. * MySQL Cache Provider
  20. * @package WURFL_Cache
  21. */
  22. class WURFL_Cache_MysqlCacheProvider implements WURFL_Cache_CacheProvider {
  23. const EXTENSION_MODULE_NAME = "mysql";
  24. const DEFAULT_HOST = "localhost";
  25. const DEFAULT_USER = "";
  26. const DEFAULT_PASS = "";
  27. const DEFAULT_DB = "";
  28. const DEFAULT_TABLE = "wurfl_object_cache";
  29. const DEFAULT_PORT = 3306;
  30. const DEFAULT_KEYCOLUMN = "key";
  31. const DEFAULT_VALUECOLUMN = "value";
  32. /**
  33. * @var int mysql link resource
  34. */
  35. private $_link;
  36. /**
  37. * @var string mysql host
  38. */
  39. private $_host;
  40. /**
  41. * @var string mysql database name
  42. */
  43. private $_db;
  44. /**
  45. * @var string mysql user
  46. */
  47. private $_user;
  48. /**
  49. * @var string mysql password
  50. */
  51. private $_pass;
  52. /**
  53. * @var string mysql port
  54. */
  55. private $_port;
  56. /**
  57. * @var string mysql table for storing cached data
  58. */
  59. private $_table;
  60. /**
  61. * @var string mysql key column name
  62. */
  63. private $_keycolumn;
  64. /**
  65. * @var string mysql value column name
  66. */
  67. private $_valuecolumn;
  68. /**
  69. * Creates a new MySQL Cache Provider with the given $params
  70. * @param array $params
  71. */
  72. public function __construct($params) {
  73. if (is_array($params)) {
  74. $this->_host = isset($params["host"]) ? $params["host"] : self::DEFAULT_HOST;
  75. $this->_port = isset($params["port"]) ? $params["port"] : self::DEFAULT_PORT;
  76. $this->_user = isset($params["user"]) ? $params["user"] : self::DEFAULT_USER;
  77. $this->_pass = isset($params["pass"]) ? $params["pass"] : self::DEFAULT_PASS;
  78. $this->_db = isset($params["db"]) ? $params["db"] : self::DEFAULT_DB;
  79. $this->_table = isset($params["table"]) ? $params["table"] : self::DEFAULT_TABLE;
  80. $this->_keycolumn = isset($params["keycolumn"]) ? $params["keycolumn"] : self::DEFAULT_KEYCOLUMN;
  81. $this->_valuecolumn = isset($params["valuecolumn"]) ? $params["valuecolumn"] : self::DEFAULT_VALUECOLUMN;
  82. }
  83. $this->initialize();
  84. }
  85. /**
  86. * Initializes the MySQL Module
  87. * @throws WURFL_Xml_PersistenceProvider_Exception Various database errors
  88. */
  89. public final function initialize() {
  90. $this->_ensureModuleExistance();
  91. // Initializes link to MySQL
  92. $this->_link = mysql_connect("$this->_host:$this->_port", $this->_user, $this->_pass);
  93. if (!$this->_link) {
  94. throw new WURFL_Xml_PersistenceProvider_Exception("Couldn't connect to $this->_host (".mysql_error($this->_link).")");
  95. }
  96. // Initializes link to database
  97. if (!mysql_select_db($this->_db, $this->_link)) {
  98. throw new WURFL_Xml_PersistenceProvider_Exception("Couldn't change to database to $this->_db (".mysql_error($this->_link).")");
  99. }
  100. // Check for database
  101. $test = mysql_query("SHOW TABLES FROM $this->_db LIKE '$this->_table'",$this->_link);
  102. if (!is_resource($test)) {
  103. throw new WURFL_Xml_PersistenceProvider_Exception("Couldn't show tables from database $this->_db (".mysql_error($this->_link).")");
  104. }
  105. // create table if it's not there.
  106. if (mysql_num_rows($test) == 0) {
  107. $query = sprintf("CREATE TABLE `%s`.`%s` (
  108. `%s` varchar(255) collate latin1_general_ci NOT NULL,
  109. `%s` mediumblob NOT NULL,
  110. `ts` timestamp NOT NULL default CURRENT_TIMESTAMP,
  111. PRIMARY KEY (`%s`)
  112. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci",
  113. $this->_db,
  114. $this->_table,
  115. $this->_keycolumn,
  116. $this->_valuecolumn,
  117. $this->_keycolumn
  118. );
  119. $success = mysql_query($query, $this->_link);
  120. if (!$success) {
  121. throw new WURFL_Xml_PersistenceProvider_Exception("Table $this->_table missing in $this->_db (".mysql_error($this->_link).")");
  122. }
  123. }
  124. if (is_resource($test)) {
  125. mysql_free_result($test);
  126. }
  127. }
  128. function get($key) {
  129. $key = mysql_escape_string($key);
  130. $sql="select `value` from `$this->_db`.`$this->_table` where `key`='$key'";
  131. $result=mysql_query($sql,$this->_link);
  132. if (!is_resource($result)) {
  133. throw new WURFL_Xml_PersistenceProvider_Exception("MySql error ".mysql_error($this->_link)."in $this->_db");
  134. }
  135. $row = mysql_fetch_assoc($result);
  136. if (is_array($row)) {
  137. $return = unserialize($row['value']);
  138. } else {
  139. $return=false;
  140. }
  141. if (is_resource($result)) mysql_free_result($result);
  142. return $return;
  143. }
  144. function put($key, $value) {
  145. $value = mysql_escape_string(serialize($value));
  146. $key = mysql_escape_string($key);
  147. $sql = sprintf("DELETE FROM `%s`.`%s` WHERE `key` = '%s'", $this->_db, $this->_table, $key);
  148. $success = mysql_query($sql, $this->_link);
  149. if (!$success) {
  150. throw new WURFL_Xml_PersistenceProvider_Exception("MySql error ".mysql_error($this->_link)." while deleting $key in $this->_db");
  151. }
  152. $sql = sprintf("INSERT INTO `%s`.`%s` (`key`,`value`) VALUES ('%s','%s')", $this->_db, $this->_table, $key, $value);
  153. $success = mysql_query($sql,$this->_link);
  154. if (!$success) {
  155. throw new WURFL_Xml_PersistenceProvider_Exception("MySql error ".mysql_error($this->_link)." while setting $key in $this->_db");
  156. }
  157. return $success;
  158. }
  159. function clear() {
  160. }
  161. function close() {
  162. mysql_close($this->_link);
  163. $this->_link = null;
  164. }
  165. /**
  166. * Ensures the existance of the the PHP Extension memcache
  167. * @throws WURFL_Xml_PersistenceProvider_Exception mysql extension is not present
  168. */
  169. private function _ensureModuleExistance() {
  170. if(!extension_loaded(self::EXTENSION_MODULE_NAME)) {
  171. throw new WURFL_Xml_PersistenceProvider_Exception("The PHP extension mysql must be installed and loaded in order to use the mysql cache provider.");
  172. }
  173. }
  174. }