SimplePie_Cache_MySQL

Caches data to a MySQL database.

Defined (1)

The class is defined in the following location(s).

/wp-includes/SimplePie/Cache/MySQL.php  
  1. class SimplePie_Cache_MySQL extends SimplePie_Cache_DB 
  2. /** 
  3. * PDO instance 
  4. * @var PDO 
  5. */ 
  6. protected $mysql; 
  7.  
  8. /** 
  9. * Options 
  10. * @var array 
  11. */ 
  12. protected $options; 
  13.  
  14. /** 
  15. * Cache ID 
  16. * @var string 
  17. */ 
  18. protected $id; 
  19.  
  20. /** 
  21. * Create a new cache object 
  22. * @param string $location Location string (from SimplePie::$cache_location) 
  23. * @param string $name Unique ID for the cache 
  24. * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data 
  25. */ 
  26. public function __construct($location, $name, $type) 
  27. $this->options = array( 
  28. 'user' => null,  
  29. 'pass' => null,  
  30. 'host' => '127.0.0.1',  
  31. 'port' => '3306',  
  32. 'path' => '',  
  33. 'extras' => array( 
  34. 'prefix' => '',  
  35. ),  
  36. ); 
  37. $this->options = array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location)); 
  38.  
  39. // Path is prefixed with a "/" 
  40. $this->options['dbname'] = substr($this->options['path'], 1); 
  41.  
  42. try 
  43. $this->mysql = new PDO("mysql:dbname={$this->options['dbname']};host={$this->options['host']};port={$this->options['port']}", $this->options['user'], $this->options['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); 
  44. catch (PDOException $e) 
  45. $this->mysql = null; 
  46. return; 
  47.  
  48. $this->id = $name . $type; 
  49.  
  50. if (!$query = $this->mysql->query('SHOW TABLES')) 
  51. $this->mysql = null; 
  52. return; 
  53.  
  54. $db = array(); 
  55. while ($row = $query->fetchColumn()) 
  56. $db[] = $row; 
  57.  
  58. if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db)) 
  59. $query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))'); 
  60. if ($query === false) 
  61. $this->mysql = null; 
  62.  
  63. if (!in_array($this->options['extras']['prefix'] . 'items', $db)) 
  64. $query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` TEXT CHARACTER SET utf8 NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))'); 
  65. if ($query === false) 
  66. $this->mysql = null; 
  67.  
  68. /** 
  69. * Save data to the cache 
  70. * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property 
  71. * @return bool Successfulness 
  72. */ 
  73. public function save($data) 
  74. if ($this->mysql === null) 
  75. return false; 
  76.  
  77. if ($data instanceof SimplePie) 
  78. $data = clone $data; 
  79.  
  80. $prepared = self::prepare_simplepie_object_for_cache($data); 
  81.  
  82. $query = $this->mysql->prepare('SELECT COUNT(*) FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed'); 
  83. $query->bindValue(':feed', $this->id); 
  84. if ($query->execute()) 
  85. if ($query->fetchColumn() > 0) 
  86. $items = count($prepared[1]); 
  87. if ($items) 
  88. $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed'; 
  89. $query = $this->mysql->prepare($sql); 
  90. $query->bindValue(':items', $items); 
  91. else 
  92. $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed'; 
  93. $query = $this->mysql->prepare($sql); 
  94.  
  95. $query->bindValue(':data', $prepared[0]); 
  96. $query->bindValue(':time', time()); 
  97. $query->bindValue(':feed', $this->id); 
  98. if (!$query->execute()) 
  99. return false; 
  100. else 
  101. $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)'); 
  102. $query->bindValue(':feed', $this->id); 
  103. $query->bindValue(':count', count($prepared[1])); 
  104. $query->bindValue(':data', $prepared[0]); 
  105. $query->bindValue(':time', time()); 
  106. if (!$query->execute()) 
  107. return false; 
  108.  
  109. $ids = array_keys($prepared[1]); 
  110. if (!empty($ids)) 
  111. foreach ($ids as $id) 
  112. $database_ids[] = $this->mysql->quote($id); 
  113.  
  114. $query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `id` = ' . implode(' OR `id` = ', $database_ids) . ' AND `feed_id` = :feed'); 
  115. $query->bindValue(':feed', $this->id); 
  116.  
  117. if ($query->execute()) 
  118. $existing_ids = array(); 
  119. while ($row = $query->fetchColumn()) 
  120. $existing_ids[] = $row; 
  121.  
  122. $new_ids = array_diff($ids, $existing_ids); 
  123.  
  124. foreach ($new_ids as $new_id) 
  125. if (!($date = $prepared[1][$new_id]->get_date('U'))) 
  126. $date = time(); 
  127.  
  128. $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)'); 
  129. $query->bindValue(':feed', $this->id); 
  130. $query->bindValue(':id', $new_id); 
  131. $query->bindValue(':data', serialize($prepared[1][$new_id]->data)); 
  132. $query->bindValue(':date', $date); 
  133. if (!$query->execute()) 
  134. return false; 
  135. return true; 
  136. else 
  137. return true; 
  138. else 
  139. $query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed'); 
  140. $query->bindValue(':feed', $this->id); 
  141. if ($query->execute()) 
  142. if ($query->rowCount() > 0) 
  143. $query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed'); 
  144. $query->bindValue(':data', serialize($data)); 
  145. $query->bindValue(':time', time()); 
  146. $query->bindValue(':feed', $this->id); 
  147. if ($this->execute()) 
  148. return true; 
  149. else 
  150. $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)'); 
  151. $query->bindValue(':id', $this->id); 
  152. $query->bindValue(':data', serialize($data)); 
  153. $query->bindValue(':time', time()); 
  154. if ($query->execute()) 
  155. return true; 
  156. return false; 
  157.  
  158. /** 
  159. * Retrieve the data saved to the cache 
  160. * @return array Data for SimplePie::$data 
  161. */ 
  162. public function load() 
  163. if ($this->mysql === null) 
  164. return false; 
  165.  
  166. $query = $this->mysql->prepare('SELECT `items`, `data` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id'); 
  167. $query->bindValue(':id', $this->id); 
  168. if ($query->execute() && ($row = $query->fetch())) 
  169. $data = unserialize($row[1]); 
  170.  
  171. if (isset($this->options['items'][0])) 
  172. $items = (int) $this->options['items'][0]; 
  173. else 
  174. $items = (int) $row[0]; 
  175.  
  176. if ($items !== 0) 
  177. if (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0])) 
  178. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]; 
  179. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0])) 
  180. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]; 
  181. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0])) 
  182. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]; 
  183. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0])) 
  184. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]; 
  185. else 
  186. $feed = null; 
  187.  
  188. if ($feed !== null) 
  189. $sql = 'SELECT `data` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC'; 
  190. if ($items > 0) 
  191. $sql .= ' LIMIT ' . $items; 
  192.  
  193. $query = $this->mysql->prepare($sql); 
  194. $query->bindValue(':feed', $this->id); 
  195. if ($query->execute()) 
  196. while ($row = $query->fetchColumn()) 
  197. $feed['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry'][] = unserialize($row); 
  198. else 
  199. return false; 
  200. return $data; 
  201. return false; 
  202.  
  203. /** 
  204. * Retrieve the last modified time for the cache 
  205. * @return int Timestamp 
  206. */ 
  207. public function mtime() 
  208. if ($this->mysql === null) 
  209. return false; 
  210.  
  211. $query = $this->mysql->prepare('SELECT `mtime` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id'); 
  212. $query->bindValue(':id', $this->id); 
  213. if ($query->execute() && ($time = $query->fetchColumn())) 
  214. return $time; 
  215. else 
  216. return false; 
  217.  
  218. /** 
  219. * Set the last modified time to the current time 
  220. * @return bool Success status 
  221. */ 
  222. public function touch() 
  223. if ($this->mysql === null) 
  224. return false; 
  225.  
  226. $query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id'); 
  227. $query->bindValue(':time', time()); 
  228. $query->bindValue(':id', $this->id); 
  229. if ($query->execute() && $query->rowCount() > 0) 
  230. return true; 
  231. else 
  232. return false; 
  233.  
  234. /** 
  235. * Remove the cache 
  236. * @return bool Success status 
  237. */ 
  238. public function unlink() 
  239. if ($this->mysql === null) 
  240. return false; 
  241.  
  242. $query = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id'); 
  243. $query->bindValue(':id', $this->id); 
  244. $query2 = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id'); 
  245. $query2->bindValue(':id', $this->id); 
  246. if ($query->execute() && $query2->execute()) 
  247. return true; 
  248. else 
  249. return false;