Safe_DataObject.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * Extended DB_DataObject to improve a few things:
  22. * - free global resources from destructor
  23. * - remove bogus global references from serialized objects
  24. * - don't leak memory when loading already-used .ini files
  25. * (eg when using the same schema on thousands of databases)
  26. */
  27. class Safe_DataObject extends GS_DataObject
  28. {
  29. /**
  30. * Destructor to free global memory resources associated with
  31. * this data object when it's unset or goes out of scope.
  32. * DB_DataObject doesn't do this yet by itself.
  33. */
  34. function __destruct()
  35. {
  36. $this->free();
  37. if (method_exists('DB_DataObject', '__destruct')) {
  38. parent::__destruct();
  39. }
  40. }
  41. /**
  42. * Magic function called at clone() time.
  43. *
  44. * We use this to drop connection with some global resources.
  45. * This supports the fairly common pattern where individual
  46. * items being read in a loop via a single object are cloned
  47. * for individual processing, then fall out of scope when the
  48. * loop comes around again.
  49. *
  50. * As that triggers the destructor, we want to make sure that
  51. * the original object doesn't have its database result killed.
  52. * It will still be freed properly when the original object
  53. * gets destroyed.
  54. */
  55. function __clone()
  56. {
  57. $this->_DB_resultid = false;
  58. }
  59. /**
  60. * Magic function called at serialize() time.
  61. *
  62. * We use this to drop a couple process-specific references
  63. * from DB_DataObject which can cause trouble in future
  64. * processes.
  65. *
  66. * @return array of variable names to include in serialization.
  67. */
  68. function __sleep()
  69. {
  70. $vars = array_keys(get_object_vars($this));
  71. $skip = array('_DB_resultid', '_link_loaded');
  72. return array_diff($vars, $skip);
  73. }
  74. /**
  75. * Magic function called at unserialize() time.
  76. *
  77. * Clean out some process-specific variables which might
  78. * be floating around from a previous process's cached
  79. * objects.
  80. *
  81. * Old cached objects may still have them.
  82. */
  83. function __wakeup()
  84. {
  85. // Refers to global state info from a previous process.
  86. // Clear this out so we don't accidentally break global
  87. // state in *this* process.
  88. $this->_DB_resultid = null;
  89. // We don't have any local DBO refs, so clear these out.
  90. $this->_link_loaded = false;
  91. }
  92. /**
  93. * Magic function called when someone attempts to call a method
  94. * that doesn't exist. DB_DataObject uses this to implement
  95. * setters and getters for fields, but neglects to throw an error
  96. * when you just misspell an actual method name. This leads to
  97. * silent failures which can cause all kinds of havoc.
  98. *
  99. * @param string $method
  100. * @param array $params
  101. * @return mixed
  102. * @throws Exception
  103. */
  104. function __call($method, $params)
  105. {
  106. $return = null;
  107. // Yes, that's _call with one underscore, which does the
  108. // actual implementation.
  109. if ($this->_call($method, $params, $return)) {
  110. return $return;
  111. } else {
  112. // Low level exception. No need for i18n as discussed with Brion.
  113. throw new Exception('Call to undefined method ' .
  114. get_class($this) . '::' . $method);
  115. }
  116. }
  117. /**
  118. * Work around memory-leak bugs...
  119. * Had to copy-paste the whole function in order to patch a couple lines of it.
  120. * Would be nice if this code was better factored.
  121. *
  122. * @param optional string name of database to assign / read
  123. * @param optional array structure of database, and keys
  124. * @param optional array table links
  125. *
  126. * @access public
  127. * @return true or PEAR:error on wrong paramenters.. or false if no file exists..
  128. * or the array(tablename => array(column_name=>type)) if called with 1 argument.. (databasename)
  129. */
  130. function databaseStructure()
  131. {
  132. global $_DB_DATAOBJECT;
  133. // Assignment code
  134. if ($args = func_get_args()) {
  135. if (count($args) == 1) {
  136. // this returns all the tables and their structure..
  137. if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
  138. $this->debug("Loading Generator as databaseStructure called with args",1);
  139. }
  140. $x = new DB_DataObject;
  141. $x->_database = $args[0];
  142. $this->_connect();
  143. $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
  144. $tables = $DB->getListOf('tables');
  145. class_exists('DB_DataObject_Generator') ? '' :
  146. require_once 'DB/DataObject/Generator.php';
  147. foreach($tables as $table) {
  148. $y = new DB_DataObject_Generator;
  149. $y->fillTableSchema($x->_database,$table);
  150. }
  151. return $_DB_DATAOBJECT['INI'][$x->_database];
  152. } else {
  153. $_DB_DATAOBJECT['INI'][$args[0]] = isset($_DB_DATAOBJECT['INI'][$args[0]]) ?
  154. $_DB_DATAOBJECT['INI'][$args[0]] + $args[1] : $args[1];
  155. if (isset($args[1])) {
  156. $_DB_DATAOBJECT['LINKS'][$args[0]] = isset($_DB_DATAOBJECT['LINKS'][$args[0]]) ?
  157. $_DB_DATAOBJECT['LINKS'][$args[0]] + $args[2] : $args[2];
  158. }
  159. return true;
  160. }
  161. }
  162. if (!$this->_database) {
  163. $this->_connect();
  164. }
  165. // loaded already?
  166. if (!empty($_DB_DATAOBJECT['INI'][$this->_database])) {
  167. // database loaded - but this is table is not available..
  168. if (
  169. empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])
  170. && !empty($_DB_DATAOBJECT['CONFIG']['proxy'])
  171. ) {
  172. if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
  173. $this->debug("Loading Generator to fetch Schema",1);
  174. }
  175. class_exists('DB_DataObject_Generator') ? '' :
  176. require_once 'DB/DataObject/Generator.php';
  177. $x = new DB_DataObject_Generator;
  178. $x->fillTableSchema($this->_database,$this->tableName());
  179. }
  180. return true;
  181. }
  182. if (empty($_DB_DATAOBJECT['CONFIG'])) {
  183. self::_loadConfig();
  184. }
  185. // if you supply this with arguments, then it will take those
  186. // as the database and links array...
  187. $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ?
  188. array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") :
  189. array() ;
  190. if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) {
  191. $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ?
  192. $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] :
  193. explode(PATH_SEPARATOR,$_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]);
  194. }
  195. /* BEGIN CHANGED FROM UPSTREAM */
  196. $_DB_DATAOBJECT['INI'][$this->_database] = $this->parseIniFiles($schemas);
  197. /* END CHANGED FROM UPSTREAM */
  198. // now have we loaded the structure..
  199. if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) {
  200. return true;
  201. }
  202. // - if not try building it..
  203. if (!empty($_DB_DATAOBJECT['CONFIG']['proxy'])) {
  204. class_exists('DB_DataObject_Generator') ? '' :
  205. require_once 'DB/DataObject/Generator.php';
  206. $x = new DB_DataObject_Generator;
  207. $x->fillTableSchema($this->_database,$this->tableName());
  208. // should this fail!!!???
  209. return true;
  210. }
  211. $this->debug("Cant find database schema: {$this->_database}/{$this->tableName()} \n".
  212. "in links file data: " . print_r($_DB_DATAOBJECT['INI'],true),"databaseStructure",5);
  213. // we have to die here!! - it causes chaos if we don't (including looping forever!)
  214. // Low level exception. No need for i18n as discussed with Brion.
  215. $this->raiseError( "Unable to load schema for database and table (turn debugging up to 5 for full error message)", DB_DATAOBJECT_ERROR_INVALIDARGS, PEAR_ERROR_DIE);
  216. return false;
  217. }
  218. /** For parseIniFiles */
  219. protected static $iniCache = array();
  220. /**
  221. * When switching site configurations, DB_DataObject was loading its
  222. * .ini files over and over, leaking gobs of memory.
  223. * This refactored helper function uses a local cache of .ini files
  224. * to minimize the leaks.
  225. *
  226. * @param array of .ini file names $schemas
  227. * @return array
  228. */
  229. protected function parseIniFiles(array $schemas)
  230. {
  231. $key = implode("|", $schemas);
  232. if (!isset(Safe_DataObject::$iniCache[$key])) {
  233. $data = array();
  234. foreach ($schemas as $ini) {
  235. if (file_exists($ini) && is_file($ini)) {
  236. $data = array_merge($data, parse_ini_file($ini, true));
  237. if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
  238. if (!is_readable ($ini)) {
  239. $this->debug("ini file is not readable: $ini","databaseStructure",1);
  240. } else {
  241. $this->debug("Loaded ini file: $ini","databaseStructure",1);
  242. }
  243. }
  244. } else {
  245. if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
  246. $this->debug("Missing ini file: $ini","databaseStructure",1);
  247. }
  248. }
  249. }
  250. Safe_DataObject::$iniCache[$key] = $data;
  251. }
  252. return Safe_DataObject::$iniCache[$key];
  253. }
  254. }