Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ModuleTool.php
Go to the documentation of this file.
1 <?php
7 
8  var $_modules;
9 
11 
12 
13  public static function &getInstance(){
14  static $instance = 0;
15 
16  if ( !is_object($instance) ){
17  $instance = new Dataface_ModuleTool();
18 
19  }
20  return $instance;
21 
22  }
23 
24 
28  public function getDbVersion($modname){
29  if ( !isset($this->_db_versions) ){
30  $this->_db_versions = array();
31  $sql = "select module_name, module_version from dataface__modules";
32  $res = mysql_query($sql, df_db());
33  if ( !$res ){
34  $res = mysql_query("create table dataface__modules (
35  module_name varchar(255) not null primary key,
36  module_version int(11)
37  )", df_db());
38  if ( !$res ) throw new Exception(mysql_error(df_db()));
39  $res = mysql_query($sql, df_db());
40  }
41  if ( !$res ) throw new Exception(mysql_error(df_db()));
42  while ($row = mysql_fetch_assoc($res) ){
43  $this->_db_versions[$row['module_name']] = $row['module_version'];
44  }
45  @mysql_free_result($res);
46 
47  }
48  $out = @$this->_db_versions[$modname];
49  if ( !$out ) return 0;
50  return $out;
51  }
52 
65  public function getFsVersion($modname, $path){
66  $versionPath = dirname($path).DIRECTORY_SEPARATOR.'version.txt';
67  if ( !file_exists($versionPath) ) return 0;
68  $str = trim(file_get_contents($versionPath));
69  if ( preg_match('/(\d+)$/', $str, $matches) ){
70  return intval($matches[1]);
71  } else {
72  return 0;
73  }
74  }
75 
82  public function updateModule($modname, $path){
83  $installpath = dirname($path).DIRECTORY_SEPARATOR.'installer.php';
84  if ( file_exists($installpath) ){
85  import($installpath);
86  $classname = $modname.'_installer';
87  $installer = new $classname;
88 
89  $methods = get_class_methods($classname);
90  $methods = preg_grep('/^update_([0-9]+)$/', $methods);
91 
92  $updates = array();
93  $fsversion = $this->getFsVersion($modname, $path);
94  $dbversion = $this->getDbVersion($modname);
95 
96  foreach ($methods as $method){
97  preg_match('/^update_([0-9]+)$/', $method, $matches);
98  $version = intval($matches[1]);
99  if ( $version > $dbversion and $version <= $fsversion ){
100  $updates[] = $version;
101  }
102  }
103 
104  sort($updates);
105 
106  if ( $dbversion == 0 ){
107  $res = mysql_query("insert into dataface__modules (module_name,module_version)
108  values ('".addslashes($modname)."',-1)", df_db());
109  if ( !$res ) throw new Exception(mysql_error(df_db()));
110  }
111 
112  foreach ($updates as $update ){
113  $method = 'update_'.$update;
114  $res = $installer->$method();
115  if ( PEAR::isError($res) ) return $res;
116  $res = mysql_query("update dataface__modules set `module_version`='".addslashes($update)."'", df_db());
117  if ( !$res ) throw new Exception(mysql_error(df_db()), E_USER_ERROR);
118  }
119 
120  $res = mysql_query("update dataface__modules set `module_version`='".addslashes($fsversion)."'", df_db());
121  if ( !$res ) throw new Exception(mysql_error(df_db()), E_USER_ERROR);
122 
123 
124  }
125 
126 
127  }
128 
152  public function getModuleURL($file){
153  $s = DIRECTORY_SEPARATOR;
154  if ( strtolower(realpath($file)) == strtolower(realpath(DATAFACE_SITE_PATH.$s.'modules'.$s.basename(dirname($file)).$s.basename($file))) ){
155  return DATAFACE_SITE_URL.'/modules/'.rawurlencode(basename(dirname($file)));
156  } else if (realpath($file) == realpath(DATAFACE_PATH.$s.'modules'.$s.basename(dirname($file)).$s.basename($file)) ){
157  return DATAFACE_URL.'/modules/'.rawurlencode(basename(dirname($file)));
158  } else {
159  throw new Exception("Could not find URL for file $file in module tool");
160  }
161  }
162 
173  function displayBlock($blockName, $params=array()){
174  //echo "here";
176  if ( !isset($app->_conf['_modules']) or count($app->_conf['_modules']) == 0 ){
177  return false;
178  }
179  $out = false;
180  foreach ($app->_conf['_modules'] as $name=>$path){
181  //echo "Checking $name : $path";
182  $mod =& $this->loadModule($name);
183  if ( method_exists($mod,'block__'.$blockName) ){
184  //echo "Method exists";
185  $res = call_user_func(array(&$mod, 'block__'.$blockName), $params);
186  if ( !$res !== false ){
187  $out = true;
188  }
189 
190  }
191  }
192  return $out;
193  }
194 
200  function &loadModule($name, $path=null){
201  if ( !isset($path) ){
202  if ( preg_match('/^modules_/', $name) ){
203  $s = DIRECTORY_SEPARATOR;
204  $path = preg_replace('/^modules_/', 'modules'.$s, $name).$s.substr($name, strpos($name, '_')+1).'.php';
205  }
206 
207  }
209 
210  if ( isset($this->_modules[$name]) ) return $this->_modules[$name];
211  if ( class_exists($name) ){
212  $this->_modules[$name] = new $name;
213  return $this->_modules[$name];
214  }
215 
216  if ( !isset($path) and (!@$app->_conf['_modules'] or !is_array($app->_conf['_modules']) or !isset($app->_conf['_modules'][$name])) ){
217  return PEAR::raiseError(
218  df_translate(
219  'scripts.Dataface.ModuleTool.loadModule.ERROR_MODULE_DOES_NOT_EXIST',
220  "The module '$name' does not exist.",
221  array('name'=>$name)
222  )
223  );
224  }
225  if ( !isset($app->_conf['_modules'][$name]) and isset($path) ){
226  $app->_conf['_modules'][$name] = $path;
227  import($path);
228  if ( $this->getFsVersion($name, $path) > $this->getDbVersion($name) ){
229  $this->updateModule($name, $path);
230  }
231 
232  } else {
233  import($app->_conf['_modules'][$name]);
234  if ( $this->getFsVersion($name, $app->_conf['_modules'][$name]) > $this->getDbVersion($name) ){
235  $this->updateModule($name, $app->_conf['_modules'][$name]);
236  }
237  }
238  if ( !class_exists($name) ){
239  return PEAR::raiseError(
240  df_translate(
241  'scripts.Dataface.ModuleTool.loadModule.ERROR_CLASS_DOES_NOT_EXIST',
242  "Attempted to load the module '$name' from path '{$app->_conf['_modules'][$name]}' but after loading - no such class was found. Please check to make sure that the class is defined. Or you can disable this module by commenting out the line that says '{$name}={$app->_conf['_modules'][$name]}' in the conf.ini file.",
243  array('name'=>$name,'path'=>$app->_conf['_modules'][$name])
244  )
245  );
246  }
247  $this->_modules[$name] = new $name;
248  return $this->_modules[$name];
249  }
250 
254  function loadModules(){
256  if ( @$app->_conf['_modules'] and is_array($app->_conf['_modules']) ){
257  foreach ( array_keys($app->_conf['_modules']) as $module){
258  $this->loadModule($module);
259  }
260  }
261  }
262 
263 
268  function getMigrations(){
269  $this->loadModules();
270  $out = array();
271  foreach ($this->_modules as $name=>$mod ){
272  if ( method_exists($mod, 'requiresMigration') and ( $req = $mod->requiresMigration()) ){
273  $out[$name] = $req;
274  }
275  }
276 
277  return $out;
278 
279  }
280 
281 
282 
283 
289  function migrate($modules=array()){
290  $log = array();
291  $this->loadModules();
292  $migrations = $this->getMigrations();
293  foreach ($modules as $mod){
294  $mod_obj = $this->loadModule($mod);
295  if ( isset($migrations[$mod]) and method_exists( $mod_obj, 'migrate' ) ){
296  $log[$mod] = $mod_obj->migrate();
297  }
298  unset($mod_obj);
299  }
300 
301  return $log;
302  }
303 
309  function install($modules=array()){
310  $log = array();
311  $this->loadModules();
312  $migrations = $this->getMigrations();
313  foreach ($modules as $mod){
314  $mod_obj = $this->loadModule($mod);
315 
316  if ( !$this->isInstalled($mod) and method_exists($mod_obj,'install') ){
317  $log[$mod] = $mod_obj->install();
318  }
319 
320  unset($mod_obj);
321  }
322 
323  return $log;
324  }
325