DataObjects/Core_watch.php
[Pman.Core] / UpdateDatabase.php
1 <?php
2
3 /**
4  *
5  * This applies database files from
6  * a) OLD - {MODULE}/DataObjects/XXXX.{dbtype}.sql
7  *
8  * b) NEW - {MODULE}/sql/XXX.sql (SHARED or translable)
9  *  and {MODULE}/{dbtype}/XXX.sql (SHARED or translable)
10  *
11  *
12  */
13
14 require_once 'Pman.php';
15 class Pman_Core_UpdateDatabase extends Pman
16 {
17     
18     static $cli_desc = "Update SQL - Beta (it will run updateData of all modules)";
19  
20     static $cli_opts = array(
21       
22         'prefix' => array(
23             'desc' => 'prefix for the password (eg. fred > xxx4fred - prefix is xxx4)',
24             'short' => 'p',
25             'default' => '',
26             'min' => 1,
27             'max' => 1,
28         ),
29         'data-only' => array(
30             'desc' => 'only run the updateData - do not run import the tables and procedures.',
31             'short' => 'p',
32             'default' => '',
33             'min' => 1,
34             'max' => 1,
35             
36         ),
37         'add-company' => array(
38             'desc' => 'add a company name of the company',
39             'short' => 'n',
40             'default' => '',
41             'min' => 1,
42             'max' => 1,
43         ),
44         'add-company-with-type' => array(
45             'desc' => 'the type of company (default OWNER)',
46             'short' => 't',
47             'default' => 'OWNER',
48             'min' => 1,
49             'max' => 1,
50         ),
51         'init' => array(
52             'desc' => 'Initialize the database (pg only supported)',
53             'short' => 'i',
54             'default' => '',
55             'min' => 1,
56             'max' => 1,
57         ),
58         'only-module-sql' => array(
59             'desc' => 'Only run sql import on this modules - eg. Core',
60             'default' => '',
61             'min' => 1,
62             'max' => 1,
63         ),
64         'procedures-only' => array(
65             'desc' => 'Only import procedures (not supported by most modules yet) - ignores sql directory',
66             'default' => '',
67             'min' => 1,
68             'max' => 1,
69         ),
70         
71         
72         'json-person' => array(
73             'desc' => 'Person JSON file',
74             'default' => '',
75             'min' => 1,
76             'max' => 1,
77             
78         ),
79     );
80     
81     static function cli_opts()
82     {
83         
84         $ret = self::$cli_opts;
85         $ff = HTML_FlexyFramework::get();
86         $a = new Pman();
87         $mods = $a->modulesList();
88         foreach($mods as $m) {
89             
90             $fd = $ff->rootDir. "/Pman/$m/UpdateDatabase.php";
91             if (!file_exists($fd)) {
92                 continue;
93             }
94             
95             require_once $fd;
96             
97             $cls = new ReflectionClass('Pman_'. $m . '_UpdateDatabase');
98             
99             $ret = array_merge($ret, $cls->getStaticPropertyValue('cli_opts'));
100             
101             
102         }
103         
104         return $ret;
105     }
106     
107     var $opts = false;
108     
109     
110     var $cli = false;
111     function getAuth() {
112         
113         
114         $ff = HTML_FlexyFramework::get();
115         if (!empty($ff->cli)) {
116             $this->cli = true;
117             return true;
118         }
119         
120         parent::getAuth(); // load company!
121         $au = $this->getAuthUser();
122         if (!$au || $au->company()->comptype != 'OWNER') {
123             $this->jerr("Not authenticated", array('authFailure' => true));
124         }
125         $this->authUser = $au;
126         return true;
127     }
128      
129     function get($args, $opts)
130     {
131         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
132    
133         
134         //$this->fixSequencesPgsql();exit;
135         $this->opts = $opts;
136         
137         // ask all the modules to verify the opts
138         
139         $this->checkOpts($opts);
140         
141         
142      
143         if (empty($opts['data-only'])) {
144             $this->importSQL();
145         }
146         if (!empty($opts['only-module-sql'])) {
147             return;
148         }
149         
150         $this->runUpdateModulesData();
151         
152         
153         if (!empty($opts['add-company'])) {
154             // make sure we have a good cache...?
155            
156             DB_DataObject::factory('companies')->initCompanies($this, $opts);
157         }
158         $this->fixLinks();
159          
160          
161     }
162     function output() {
163         return '';
164     }
165      /**
166      * imports SQL files from all DataObjects directories....
167      * 
168      * except any matching /migrate/
169      */
170     function importSQL()
171     {
172         
173         // loop through all the modules, and see if they have a importSQL method?
174         
175         
176         $ff = HTML_Flexyframework::get();
177         
178         $dburl = parse_url($ff->DB_DataObject['database']);
179         
180         //$this->{'import' . $url['scheme']}($url);
181         
182         $dbtype = $dburl['scheme'];
183         $dirmethod = 'import' . $dburl['scheme'] . 'dir';
184         
185         
186        
187         
188         $ar = $this->modulesList();
189        
190         
191         foreach($ar as $m) {
192              echo "Importing SQL from module $m\n";
193             if (!empty($this->opts['only-module-sql']) && $m != $this->opts['only-module-sql']) {
194                 continue;
195             }
196             
197             
198             // check to see if the class has
199             
200             
201             
202             $file = $this->rootDir. "/Pman/$m/UpdateDatabase.php";
203             if($m != 'Core' && file_exists($file)){
204                 
205                 require_once $file;
206                 $class = "Pman_{$m}_UpdateDatabase";
207                 $x = new $class;
208                 if(method_exists($x, 'importModuleSQL')){
209                     echo "Importing SQL from module $m using Module::importModuleSQL\n";
210                     $x->opts = $this->opts;
211                     $x->rootDir = $this->rootDir;
212                     $x->importModuleSQL($dburl);
213                     continue;
214                 }
215             };
216
217             echo "Importing SQL from module $m\n";
218             
219             
220             // if init has been called
221             // look in pgsql.ini
222             if (!empty($this->opts['init'])) {
223                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
224                 
225             }
226             
227             
228             
229             $fd = $this->rootDir. "/Pman/$m/DataObjects";
230             
231             $this->{$dirmethod}($dburl, $fd);
232             
233             // new -- sql directory..
234             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
235             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
236             
237             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/sql");
238             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
239             
240             
241             
242             if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
243                 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
244                 
245                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
246                 $this->{'fixSequences'. $dbtype}();
247                 
248             }
249               
250             
251         }
252         
253     }
254     
255     
256     
257      
258     /** -------------- code to handle importing a whole directory of files into the database  -------  **/
259     
260     
261     function importpgsqldir($url, $dir, $disable_triggers = false)
262     {
263         $ff = HTML_FlexyFramework::get();
264         
265         require_once 'System.php';
266         $cat = System::which('cat');
267         $psql = System::which('psql');
268         
269          
270         if (!empty($url['pass'])) { 
271             putenv("PGPASSWORD=". $url['pass']);
272         }
273            
274         $psql_cmd = $psql .
275             ' -h ' . $url['host'] .
276             ' -U' . escapeshellarg($url['user']) .
277              ' ' . basename($url['path']);
278         
279         
280         echo $psql_cmd . "\n" ;
281         echo "scan : $dir\n";
282         
283         if (is_file($dir)) {
284             $files = array($dir);
285
286         } else {
287         
288         
289             $files = glob($dir.'/*.sql');
290             uksort($files, 'strcasecmp');
291         }
292         //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
293         //usort($files, $lsort);
294         
295         
296         foreach($files as $bfn) {
297
298
299             if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
300                 continue;
301             }
302             if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
303                 && !preg_match('#\.pg\.sql#i', basename($bfn))
304             ) { // skip migration scripts at present..
305                 continue;
306             }
307             $fn = false;
308
309             if (!preg_match('/pgsql/', basename($dir) )) {
310                  if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
311                     $fn = $this->convertToPG($bfn);
312                 }
313             }
314
315             // files ending in .pg.sql are native postgres files.. ## depricated
316
317
318             $cmd = "$psql_cmd  < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
319
320             echo "$bfn:   $cmd ". ($ff->cli ? "\n" : "<BR>\n");
321
322             passthru($cmd);
323
324             if ($fn) {
325                 unlink($fn);
326             }
327         }
328
329               
330              
331         
332     }
333     
334     
335     /**
336      * mysql - does not support conversions.
337      * 
338      *
339      */
340     
341     
342     function importmysqldir($dburl, $dir)
343     {
344         echo "Import MYSQL :: $dir\n";
345         
346         
347         require_once 'System.php';
348         $cat = System::which('cat');
349         $mysql = System::which('mysql');
350         
351        
352            
353         $mysql_cmd = $mysql .
354             ' -h ' . $dburl['host'] .
355             ' -u' . escapeshellarg($dburl['user']) .
356             (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass'])  :  '') .
357             ' ' . basename($dburl['path']);
358         //echo $mysql_cmd . "\n" ;
359         
360         $files = glob($dir.'/*.sql');
361         uksort($files, 'strcasecmp');
362         
363        
364         foreach($files as $fn) {
365                 
366                  
367                 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
368                     continue;
369                 }
370                 // .my.sql but not .pg.sql
371                 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
372                     && !preg_match('#\.my\.sql#i', basename($fn))
373                 ) { // skip migration scripts at present..
374                     continue;
375                 }
376                 if (!strlen(trim($fn))) {
377                     continue;
378                 }
379                 
380                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
381                 
382                 echo basename($dir).'/'. basename($fn) .    '::' .  $cmd. ($this->cli ? "\n" : "<BR>\n");
383                 
384                 passthru($cmd);
385             
386                 
387         }
388        
389         
390         
391     }
392     
393     
394     /**
395      * simple regex based convert mysql to pgsql...
396      */
397     function convertToPG($src)
398     {
399         //echo "Convert $src\n";
400                
401         $fn = $this->tempName('sql');
402         
403         $ret = array( ); // pad it a bit.
404         $extra = array("", "" );
405         
406         $tbl = false;
407         foreach(file($src) as $l) {
408             $l = trim($l);
409             
410             if (!strlen($l) || $l[0] == '#') {
411                 continue;
412             }
413             $m = array();
414             if (preg_match('#create\s+table\s+([a-z0-9_]+)#i',  $l, $m)) {
415                 $tbl = $m[1];
416              }
417             if (preg_match('#create\s+table\s+\`([a-z0-9_]+)\`#i',  $l, $m)) {
418                 $tbl = 'shop_' . strtolower($m[1]);
419                 $l = preg_replace('#create\s+table\s+\`([a-z0-9_]+)\`#i', "CREATE TABLE {$tbl}", $l);
420             }
421             if (preg_match('#\`([a-z0-9_]+)\`#i',  $l, $m) && !preg_match('#alter\s+table\s+#i',  $l)) {
422                 $l = preg_replace('#\`([a-z0-9_]+)\`#i', "{$m[1]}_name", $l);
423             }
424             // autoinc
425             if ($tbl && preg_match('#auto_increment#i',  $l, $m)) {
426                 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
427                 $extra[]  =   "create sequence {$tbl}_seq;";
428               
429             }
430             
431             if (preg_match('#alter\s+table\s+(\`[a-z0-9_]+\`)#i',  $l, $m)){
432                 $l = preg_replace('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', "ALTER TABLE {$tbl}", $l);
433             }
434             
435             // enum value -- use the text instead..
436             
437             if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i',  $l, $m)) {
438                 $l = preg_replace('#enum\([\w|\W]+\)#i', "TEXT", $l);
439             }
440             // ignore the alter enum
441             if ($tbl && preg_match('#alter\s+table\s+([\w|\W]+)\s+enum\([\w|\W]+\)#i',  $l, $m)) {
442                 continue;
443             }
444             
445             // UNIQUE KEY .. ignore
446             if ($tbl && preg_match('#UNIQUE KEY#i',  $l, $m)) {
447                 $last = array_pop($ret);
448                 $ret[] = trim($last, ",");
449                 continue;
450             }
451             
452             if ($tbl && preg_match('#RENAME\s+TO#i',  $l, $m)) {
453                 continue;
454             }
455             
456             if ($tbl && preg_match('#change\s+column#i',  $l, $m)) {
457                 continue;
458             }
459             
460             // INDEX lookup ..ignore
461             if ($tbl && preg_match('#INDEX lookup+([\w|\W]+)#i',  $l, $m)) {
462                $last = array_pop($ret);
463                $ret[] = trim($last, ",");
464                continue;
465                
466             }
467             
468             // CREATE INDEX ..ignore
469             if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+#i',  $l, $m)) {
470 //               $l = "CREATE INDEX  {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
471                 continue;
472              }
473              
474             // basic types..
475             $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
476             
477             $l = preg_replace('# datetime#i', ' TIMESTAMP WITHOUT TIME ZONE', $l);
478             $l = preg_replace('# blob#i', ' TEXT', $l);
479             $l = preg_replace('# longtext#i', ' TEXT', $l);
480             $l = preg_replace('# tinyint#i', ' INT', $l);
481             
482             $ret[] = $l;
483             
484         }
485         
486         $ret = array_merge($extra,$ret);
487 //        echo implode("\n", $ret); exit;
488         
489         file_put_contents($fn, implode("\n", $ret));
490         
491         return $fn;
492     }
493     
494     
495     function checkOpts($opts)
496     {
497         
498         
499         foreach($opts as $o=>$v) {
500             if (!preg_match('/^json-/', $o) || empty($v)) {
501                 continue;
502             }
503             if (!file_exists($v)) {
504                 die("File does not exist : OPTION --{$o} = {$v} \n");
505             }
506         }
507         
508         $modules = array_reverse($this->modulesList());
509         
510         // move 'project' one to the end...
511         
512         foreach ($modules as $module){
513             $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
514             if($module == 'Core' || !file_exists($file)){
515                 continue;
516             }
517             require_once $file;
518             $class = "Pman_{$module}_UpdateDatabase";
519             $x = new $class;
520             if(!method_exists($x, 'checkOpts')){
521                 continue;
522             };
523             $x->checkOpts($opts);
524         }
525                 
526     }
527     static function jsonImportFromArray($opts)
528     {
529         foreach($opts as $o=>$v) {
530             if (!preg_match('/^json-/', $o) || empty($v)) {
531                 continue;
532             }
533             $type = str_replace('_', '-', substr($o,5));
534             
535             $data= json_decode(file_get_contents($v),true);
536             $pg = HTML_FlexyFramework::get()->page;
537             DB_DataObject::factory($type)->importFromArray($pg ,$data,$opts);
538             
539         }
540         
541         
542         
543     }
544     
545     
546     
547     function runUpdateModulesData()
548     {
549         
550         
551         HTML_FlexyFramework::get()->generateDataobjectsCache(true);
552         echo "Running jsonImportFromArray\n";
553         Pman_Core_UpdateDatabase::jsonImportFromArray($this->opts);
554         
555         
556         echo "Running updateData on modules\n";
557         // runs core...
558         echo "Core\n";
559         $this->updateData(); 
560         $modules = array_reverse($this->modulesList());
561         
562         // move 'project' one to the end...
563         
564         foreach ($modules as $module){
565             $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
566             if($module == 'Core' || !file_exists($file)){
567                 continue;
568             }
569             
570             require_once $file;
571             $class = "Pman_{$module}_UpdateDatabase";
572             $x = new $class;
573             if(!method_exists($x, 'updateData')){
574                 continue;
575             };
576             echo "$module\n";
577             $x->updateData();
578         }
579                 
580     }
581     
582     
583     function updateDataEnums()
584     {
585         
586         $enum = DB_DataObject::Factory('core_enum');
587         //DB_DAtaObject::debugLevel(1);
588         $enum->initEnums(
589             array(
590                 array(
591                     'etype' => '',
592                     'name' => 'COMPTYPE',
593                     'display_name' =>  'Company Types',
594                     'is_system_enum' => 1,
595                     'cn' => array(
596                         array(
597                             'name' => 'OWNER',
598                             'display_name' => 'Owner',
599                             'seqid' => 999, // last...
600                             'is_system_enum' => 1,
601                         )
602                         
603                     )
604                 ),
605                 array(
606                     'etype' => '',
607                     'name' => 'HtmlEditor.font-family',
608                     'display_name' =>  'HTML Editor font families',
609                     'is_system_enum' => 1,
610                     'cn' => array(
611                         array(
612                             'name' => 'Helvetica,Arial,sans-serif',
613                             'display_name' => 'Helvetica',
614                             
615                         ),
616                         
617                         array(
618                             'name' => 'Courier New',
619                             'display_name' => 'Courier',
620                              
621                         ),
622                         array(
623                             'name' => 'Tahoma',
624                             'display_name' => 'Tahoma',
625                             
626                         ),
627                         array(
628                             'name' => 'Times New Roman,serif',
629                             'display_name' => 'Times',
630                            
631                         ),
632                         array(
633                             'name' => 'Verdana',
634                             'display_name' => 'Verdana',
635                             
636                         ),
637                         
638                             
639                         
640                     )
641                 ),
642             )
643         ); 
644         
645     }
646     function updateDataGroups()
647     {
648          
649         $groups = DB_DataObject::factory('groups');
650         $groups->initGroups();
651         
652         $groups->initDatabase($this,array(
653             array(
654                 'name' => 'bcc-email', // group who are bcc'ed on all requests.
655                 'type' => 0, // system
656             ),
657             array(
658                 'name' => 'system-email-from',
659                 'type' => 0, // system
660             ),
661         ));
662         
663     }
664     
665     function updateDataCompanies()
666     {
667          
668         // fix comptypes enums..
669         $c = DB_DataObject::Factory('Companies');
670         $c->selectAdd();
671         $c->selectAdd('distinct(comptype) as comptype');
672         $c->whereAdd("comptype != ''");
673         
674         $ctb = array();
675         foreach($c->fetchAll('comptype') as $cts) {
676             
677             
678             
679            $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
680         
681         }
682          $c = DB_DataObject::Factory('core_enum');
683          
684         $c->initEnums($ctb);
685         //DB_DataObject::debugLevel(1);
686         // fix comptypeid
687         $c = DB_DataObject::Factory('Companies');
688         $c->query("
689             UPDATE Companies 
690                 SET
691                     comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype LIMIT 1)
692                 WHERE
693                     comptype_id = 0
694                     AND
695                     LENGTH(comptype) > 0
696                   
697                   
698                   ");
699          
700         
701         
702     }
703     
704     function updateData()
705     {
706         // fill i18n data..
707         HTML_FlexyFramework::get()->generateDataobjectsCache(true);
708         $this->updateDataEnums();
709         $this->updateDataGroups();
710         $this->updateDataCompanies();
711         
712         $c = DB_DataObject::Factory('I18n');
713         $c->buildDB();
714          
715        
716         
717         
718     }
719     
720     function fixMysqlInnodb()
721     {
722         // innodb in single files is far more efficient that MYD or one big innodb file.
723         // first check if database is using this format.
724         $db = DB_DataObject::factory('core_enum');
725         $db->query("show variables like 'innodb_file_per_table'");
726         $db->fetch();
727         var_dump($db);
728         // Value should == ON
729         
730         // I think it needs to look in the mysql directory.... to see if the idb exists..
731         $db = DB_DataObject::factory('core_enum');
732         $db->query("SHOW TABLE STATUS");
733         // Engine should be InnoDB - and file XXX.idb should exist...
734         
735         
736
737  
738         
739         
740         
741         
742         
743     }
744     
745     
746     /** ------------- schema fixing ... there is an issue with data imported having the wrong sequence names... --- */
747     
748     function fixSequencesMysql()
749     {
750         // not required...
751     }
752     
753     function fixSequencesPgsql()
754     {
755      
756      
757         //DB_DataObject::debugLevel(1);
758         $cs = DB_DataObject::factory('core_enum');
759         $cs->query("
760          SELECT
761                     'ALTER SEQUENCE '||
762                     CASE WHEN strpos(seq_name, '.') > 0 THEN
763                         min(seq_name)
764                     ELSE 
765                         quote_ident(min(schema_name)) ||'.'|| quote_ident(min(seq_name))
766                     END 
767                     
768                     ||' OWNED BY '|| quote_ident(min(schema_name)) || '.' ||
769                     quote_ident(min(table_name)) ||'.'|| quote_ident(min(column_name)) ||';' as cmd
770              FROM (
771                       
772                        SELECT 
773                      n.nspname AS schema_name,
774                      c.relname AS table_name,
775                      a.attname AS column_name, 
776                      regexp_replace(regexp_replace(d.adsrc, E'nextval\\\\(+[''\\\"]*', ''),E'[''\\\"]*::.*\$','') AS seq_name 
777                  FROM pg_class c 
778                  JOIN pg_attribute a ON (c.oid=a.attrelid) 
779                  JOIN pg_attrdef d ON (a.attrelid=d.adrelid AND a.attnum=d.adnum) 
780                  JOIN pg_namespace n ON (c.relnamespace=n.oid)
781                  WHERE has_schema_privilege(n.oid,'USAGE')
782                    AND n.nspname NOT LIKE 'pg!_%' escape '!'
783                    AND has_table_privilege(c.oid,'SELECT')
784                    AND (NOT a.attisdropped)
785                    AND d.adsrc ~ '^nextval'
786               
787              ) seq
788              WHERE
789                  CASE WHEN strpos(seq_name, '.') > 0 THEN
790                      substring(seq_name, 1,strpos(seq_name,'.')-1)
791                 ELSE
792                     schema_name
793                 END = schema_name
794              
795              GROUP BY seq_name HAVING count(*)=1
796              ");
797         $cmds = array();
798         while ($cs->fetch()) {
799             $cmds[] = $cs->cmd;
800         }
801         foreach($cmds as $cmd) {
802             $cs = DB_DataObject::factory('core_enum');
803             echo "$cmd\n";
804             $cs->query($cmd);
805         }
806         $cs = DB_DataObject::factory('core_enum');
807          $cs->query("
808                SELECT  'SELECT SETVAL(' ||
809                          quote_literal(quote_ident(nspname) || '.' || quote_ident(S.relname)) ||
810                         ', MAX(' || quote_ident(C.attname)|| ')::integer )  FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd 
811                 FROM pg_class AS S,
812                     pg_depend AS D,
813                     pg_class AS T,
814                     pg_attribute AS C,
815                     pg_namespace AS NS
816                 WHERE S.relkind = 'S'
817                     AND S.oid = D.objid
818                     AND D.refobjid = T.oid
819                     AND D.refobjid = C.attrelid
820                     AND D.refobjsubid = C.attnum
821                     AND NS.oid = T.relnamespace
822                 ORDER BY S.relname   
823         ");
824          $cmds = array();
825         while ($cs->fetch()) {
826             $cmds[] = $cs->cmd;
827         }
828         foreach($cmds as $cmd) {
829             $cs = DB_DataObject::factory('core_enum');
830             echo "$cmd\n";
831             $cs->query($cmd);
832         }
833        
834     }
835     
836     function fixLinks()
837     {
838         
839         $ff = HTML_Flexyframework::get();
840         
841         $dburl = parse_url($ff->DB_DataObject['database']);
842         
843         $dbtype = $dburl['scheme'];
844        
845         $scls = ucfirst($dbtype). 'Links';
846         $cls = 'Pman_Core_UpdateDatabase_'. $scls;
847         $fn = implode('/',explode('_', $cls)).'.php';
848         if (!file_exists(__DIR__.'/UpdateDatabase/'. $scls .'.php')) {
849             return;
850         }
851         require_once $fn;
852         $c = new $cls();
853         
854         
855         
856     }
857 }