3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * Extended DB_DataObject to improve a few things:
24 * - free global resources from destructor
25 * - remove bogus global references from serialized objects
26 * - don't leak memory when loading already-used .ini files
27 * (eg when using the same schema on thousands of databases)
29 class Safe_DataObject extends DB_DataObject
32 * Destructor to free global memory resources associated with
33 * this data object when it's unset or goes out of scope.
34 * DB_DataObject doesn't do this yet by itself.
40 if (method_exists('DB_DataObject', '__destruct')) {
46 * Magic function called at clone() time.
48 * We use this to drop connection with some global resources.
49 * This supports the fairly common pattern where individual
50 * items being read in a loop via a single object are cloned
51 * for individual processing, then fall out of scope when the
52 * loop comes around again.
54 * As that triggers the destructor, we want to make sure that
55 * the original object doesn't have its database result killed.
56 * It will still be freed properly when the original object
61 $this->_DB_resultid = false;
65 * Magic function called at serialize() time.
67 * We use this to drop a couple process-specific references
68 * from DB_DataObject which can cause trouble in future
71 * @return array of variable names to include in serialization.
75 $vars = array_keys(get_object_vars($this));
76 $skip = array('_DB_resultid', '_link_loaded');
77 return array_diff($vars, $skip);
81 * Magic function called at unserialize() time.
83 * Clean out some process-specific variables which might
84 * be floating around from a previous process's cached
87 * Old cached objects may still have them.
91 // Refers to global state info from a previous process.
92 // Clear this out so we don't accidentally break global
93 // state in *this* process.
94 $this->_DB_resultid = null;
95 // We don't have any local DBO refs, so clear these out.
96 $this->_link_loaded = false;
101 * Work around memory-leak bugs...
102 * Had to copy-paste the whole function in order to patch a couple lines of it.
103 * Would be nice if this code was better factored.
105 * @param optional string name of database to assign / read
106 * @param optional array structure of database, and keys
107 * @param optional array table links
110 * @return true or PEAR:error on wrong paramenters.. or false if no file exists..
111 * or the array(tablename => array(column_name=>type)) if called with 1 argument.. (databasename)
113 function databaseStructure()
116 global $_DB_DATAOBJECT;
120 if ($args = func_get_args()) {
122 if (count($args) == 1) {
124 // this returns all the tables and their structure..
125 if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
126 $this->debug("Loading Generator as databaseStructure called with args",1);
129 $x = new DB_DataObject;
130 $x->_database = $args[0];
132 $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
134 $tables = $DB->getListOf('tables');
135 class_exists('DB_DataObject_Generator') ? '' :
136 require_once 'DB/DataObject/Generator.php';
138 foreach($tables as $table) {
139 $y = new DB_DataObject_Generator;
140 $y->fillTableSchema($x->_database,$table);
142 return $_DB_DATAOBJECT['INI'][$x->_database];
145 $_DB_DATAOBJECT['INI'][$args[0]] = isset($_DB_DATAOBJECT['INI'][$args[0]]) ?
146 $_DB_DATAOBJECT['INI'][$args[0]] + $args[1] : $args[1];
148 if (isset($args[1])) {
149 $_DB_DATAOBJECT['LINKS'][$args[0]] = isset($_DB_DATAOBJECT['LINKS'][$args[0]]) ?
150 $_DB_DATAOBJECT['LINKS'][$args[0]] + $args[2] : $args[2];
159 if (!$this->_database) {
164 if (!empty($_DB_DATAOBJECT['INI'][$this->_database])) {
166 // database loaded - but this is table is not available..
168 empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])
169 && !empty($_DB_DATAOBJECT['CONFIG']['proxy'])
171 if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
172 $this->debug("Loading Generator to fetch Schema",1);
174 class_exists('DB_DataObject_Generator') ? '' :
175 require_once 'DB/DataObject/Generator.php';
178 $x = new DB_DataObject_Generator;
179 $x->fillTableSchema($this->_database,$this->__table);
185 if (empty($_DB_DATAOBJECT['CONFIG'])) {
186 DB_DataObject::_loadConfig();
189 // if you supply this with arguments, then it will take those
190 // as the database and links array...
192 $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ?
193 array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") :
196 if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) {
197 $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ?
198 $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] :
199 explode(PATH_SEPARATOR,$_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]);
203 /* BEGIN CHANGED FROM UPSTREAM */
204 $_DB_DATAOBJECT['INI'][$this->_database] = $this->parseIniFiles($schemas);
205 /* END CHANGED FROM UPSTREAM */
207 // now have we loaded the structure..
209 if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) {
212 // - if not try building it..
213 if (!empty($_DB_DATAOBJECT['CONFIG']['proxy'])) {
214 class_exists('DB_DataObject_Generator') ? '' :
215 require_once 'DB/DataObject/Generator.php';
217 $x = new DB_DataObject_Generator;
218 $x->fillTableSchema($this->_database,$this->__table);
219 // should this fail!!!???
222 $this->debug("Cant find database schema: {$this->_database}/{$this->__table} \n".
223 "in links file data: " . print_r($_DB_DATAOBJECT['INI'],true),"databaseStructure",5);
224 // we have to die here!! - it causes chaos if we dont (including looping forever!)
225 $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);
229 /** For parseIniFiles */
230 protected static $iniCache = array();
233 * When switching site configurations, DB_DataObject was loading its
234 * .ini files over and over, leaking gobs of memory.
235 * This refactored helper function uses a local cache of .ini files
236 * to minimize the leaks.
238 * @param array of .ini file names $schemas
241 protected function parseIniFiles($schemas)
243 $key = implode("|", $schemas);
244 if (!isset(Safe_DataObject::$iniCache[$key])) {
246 foreach ($schemas as $ini) {
247 if (file_exists($ini) && is_file($ini)) {
248 $data = array_merge($data, parse_ini_file($ini, true));
250 if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
251 if (!is_readable ($ini)) {
252 $this->debug("ini file is not readable: $ini","databaseStructure",1);
254 $this->debug("Loaded ini file: $ini","databaseStructure",1);
258 if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
259 $this->debug("Missing ini file: $ini","databaseStructure",1);
263 Safe_DataObject::$iniCache[$key] = $data;
266 return Safe_DataObject::$iniCache[$key];