5 * This applies database files from
6 * a) OLD - {MODULE}/DataObjects/XXXX.{dbtype}.sql
8 * b) NEW - {MODULE}/sql/XXX.sql (SHARED or translable)
9 * and {MODULE}/{dbtype}/XXX.sql (SHARED or translable)
14 require_once 'Pman.php';
15 class Pman_Core_UpdateDatabase extends Pman
18 static $cli_desc = "Update SQL - Beta (it will run updateData of all modules)";
20 static $cli_opts = array(
23 'desc' => 'prefix for the password (eg. fred > xxx4fred - prefix is xxx4)',
30 'desc' => 'only run the updateData - do not run import the tables and procedures.',
37 'add-company' => array(
38 'desc' => 'add a company name of the company',
44 'add-company-with-type' => array(
45 'desc' => 'the type of company (default OWNER)',
52 'desc' => 'Initialize the database (pg only supported)',
58 'only-module-sql' => array(
59 'desc' => 'Only run sql import on this modules - eg. Core',
64 'skip-mysql-checks' => array(
65 'desc' => 'Skip mysql checks',
70 'procedures-only' => array(
71 'desc' => 'Only import procedures (not supported by most modules yet) - ignores sql directory',
77 'json-person' => array(
78 'desc' => 'Person JSON file',
85 'desc' => 'Debug the database',
94 static function cli_opts()
97 $ret = self::$cli_opts;
98 $ff = HTML_FlexyFramework::get();
100 $mods = $a->modulesList();
101 foreach($mods as $m) {
103 $fd = $ff->rootDir. "/Pman/$m/UpdateDatabase.php";
104 if (!file_exists($fd)) {
110 $cls = new ReflectionClass('Pman_'. $m . '_UpdateDatabase');
111 $ar = $cls->getStaticProperties();
112 if (isset($ar['cli_opts'])) {
114 $ret = array_merge($ret, $cls->getStaticPropertyValue('cli_opts'));
123 var $disabled = array();
128 var $local_base_url = false;
130 var $emailTemplates = array(
131 'EVENT_ERRORS_REPORT' => array(
132 'test_class' => 'Pman/Admin/Report/SendEventErrors',
133 'to_group' => 'Administrators',
135 'description' => '9. System Error Messages',
136 'template_dir' => '/Pman/Admin/templates/mail/'
143 $ff = HTML_FlexyFramework::get();
144 if (!empty($ff->cli)) {
149 parent::getAuth(); // load company!
150 $au = $this->getAuthUser();
151 if (!$au || $au->company()->comptype != 'OWNER') {
152 $this->jerr("Not authenticated", array('authFailure' => true));
154 $this->authUser = $au;
158 function get($args, $opts=array())
161 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
163 $this->checkSystem();
165 $this->verifyExtensions(array('curl'));
167 if (class_exists('PDO_DataObjects_Introspection')) {
168 PDO_DataObject_Introspection::$cache = array();
170 echo "Generate DB cache\n";
171 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
172 echo "Generated DB cache\n";
173 $ff = HTML_FlexyFramework::get();
175 if(!isset($ff->Pman) || !isset($ff->Pman['local_base_url'])){
176 die("Please setup Pman[local_base_url]\n");
179 $this->local_base_url = $ff->Pman['local_base_url'];
181 if(!empty($ff->Core_Notify)){
182 // require_once 'Pman/Core/NotifySmtpCheck.php';
183 // $x = new Pman_Core_NotifySmtpCheck();
187 $this->disabled = explode(',', $ff->disable);
189 //$this->fixSequencesPgsql();exit;
192 if (!empty($opts['debug'])) {
193 DB_DataObject::DebugLevel($opts['debug']);
196 // ask all the modules to verify the opts
197 echo "Checi options\n";
198 $this->checkOpts($opts);
202 // do this first, so the innodb change + utf8 fixes column max sizes
204 // this will trigger errors about freetext indexes - we will have to remove them manually.?
205 // otherwise we need to do an sql query to find them, then remove them (not really worth it as it only affects really old code..)
207 $this->runExtensions();
210 if (empty($opts['data-only'])) {
213 if (!empty($opts['only-module-sql'])) {
219 $this->runUpdateModulesData();
221 if (!empty($opts['add-company']) && !in_array('Core', $this->disabled)) {
222 // make sure we have a good cache...?
224 DB_DataObject::factory('core_company')->initCompanies($this, $opts);
227 $this->runExtensions();
229 $this->clearApacheDataobjectsCache();
231 $this->clearApacheAssetCache();
235 echo "\nUpdate Completed SUCCESS\n";
239 * imports SQL files from all DataObjects directories....
241 * except any matching /migrate/
246 // loop through all the modules, and see if they have a importSQL method?
249 $ff = HTML_Flexyframework::get();
251 $dburl = parse_url($ff->database); // used to be DB_DataObject['database'] - but not portable to PDO
253 //$this->{'import' . $url['scheme']}($url);
255 $dbtype = $dburl['scheme'];
258 $dirmethod = 'import' . $dburl['scheme'] . 'dir';
263 $ar = $this->modulesList();
268 if(in_array($m, $this->disabled)){
269 echo "module $m is disabled \n";
273 echo "Importing SQL from module $m\n";
274 if (!empty($this->opts['only-module-sql']) && $m != $this->opts['only-module-sql']) {
279 // check to see if the class has
283 $file = $this->rootDir. "/Pman/$m/UpdateDatabase.php";
284 if($m != 'Core' && file_exists($file)){
287 $class = "Pman_{$m}_UpdateDatabase";
289 if(method_exists($x, 'importModuleSQL')){
290 echo "Importing SQL from module $m using Module::importModuleSQL\n";
291 $x->opts = $this->opts;
292 $x->rootDir = $this->rootDir;
293 $x->importModuleSQL($dburl);
298 echo "Importing SQL from module $m\n";
301 // if init has been called
303 if (!empty($this->opts['init'])) {
304 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
310 $fd = $this->rootDir. "/Pman/$m/DataObjects";
312 $this->{$dirmethod}($dburl, $fd);
315 // new -- sql directory..
316 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
317 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
318 $dbdir = $dbtype == 'mysqli' ? 'mysql' : $dbtype;
320 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/sql");
321 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbdir}");
325 if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
326 if (class_exists('PDO_DataObjects_Introspection')) {
327 PDO_DataObject_Introspection::$cache = array();
329 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
331 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
332 $this->{'fixSequences'. $dbtype}();
345 /** -------------- code to handle importing a whole directory of files into the database ------- **/
348 function importpgsqldir($url, $dir, $disable_triggers = false)
350 $ff = HTML_FlexyFramework::get();
352 require_once 'System.php';
353 $cat = System::which('cat');
354 $psql = System::which('psql');
357 if (!empty($url['pass'])) {
358 putenv("PGPASSWORD=". $url['pass']);
362 ' -h ' . $url['host'] .
363 ' -U' . escapeshellarg($url['user']) .
364 ' ' . basename($url['path']);
367 echo $psql_cmd . "\n" ;
368 echo "scan : $dir\n";
371 $files = array($dir);
376 $files = glob($dir.'/*.sql');
377 uksort($files, 'strcasecmp');
379 //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
380 //usort($files, $lsort);
383 foreach($files as $bfn) {
386 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
389 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
390 && !preg_match('#\.pg\.sql#i', basename($bfn))
391 ) { // skip migration scripts at present..
396 if (!preg_match('/pgsql/', basename($dir) )) {
397 if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
398 $fn = $this->convertToPG($bfn);
402 // files ending in .pg.sql are native postgres files.. ## depricated
405 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
407 echo "$bfn: $cmd ". ($ff->cli ? "\n" : "<BR>\n");
423 * mysql - does not support conversions.
427 function importmysqlidir($dburl, $dir) {
428 return $this->importmysqldir($dburl, $dir);
431 function importmysqldir($dburl, $dir)
434 $this->fixMysqlInnodb(); /// run once
436 echo "Import MYSQL :: $dir\n";
439 require_once 'System.php';
440 $cat = System::which('cat');
441 $mysql = System::which('mysql');
445 $mysql_cmd = $mysql .
446 ' -h ' . $dburl['host'] .
447 ' -u' . escapeshellarg($dburl['user']) .
448 (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass']) : '') .
449 ' ' . basename($dburl['path']);
450 //echo $mysql_cmd . "\n" ;
452 $files = glob($dir.'/*.sql');
453 uksort($files, 'strcasecmp');
456 foreach($files as $fn) {
459 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
462 // .my.sql but not .pg.sql
463 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
464 && !preg_match('#\.my\.sql#i', basename($fn))
465 ) { // skip migration scripts at present..
468 if (!strlen(trim($fn))) {
472 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ." 2>&1" ;
474 echo basename($dir).'/'. basename($fn) . '::' . $cmd. ($this->cli ? "\n" : "<BR>\n");
477 $fp = popen($cmd, "r");
480 // send the current file part to the browser
481 $line = trim(fgets($fp, 1024));
486 if (!preg_match('/^ERROR\s+([0-9]+)/', $line, $matches)) {
487 echo " ---- {$line}\n"; flush();
491 switch($matches[1]) {
492 case 1017: // cause by renaming table -- old one does not exist..
493 case 1050: // create tables triggers this..
494 case 1060: // Duplicate column name
495 case 1061: // Duplicate key name - triggered by add index.. but could hide error. - unlikely though.
496 case 1091: // drop index -- name does not exist.. might hide errors..
498 case 1146: // drop a index on an unknown table.. - happens rarely...
499 case 1054: // Unknown column -- triggered by CHANGE COLUMN - but may hide other errrors..
505 echo " ---- {$line}\n"; flush();
509 // 1051: // Unknown table -- normally drop = add iff exists..
510 echo "File: $fn\n$line\n";
526 * simple regex based convert mysql to pgsql...
528 function convertToPG($src)
530 //echo "Convert $src\n";
532 $fn = $this->tempName('sql');
534 $ret = array( ); // pad it a bit.
535 $extra = array("", "" );
538 foreach(file($src) as $l) {
541 if (!strlen($l) || $l[0] == '#') {
545 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
548 if (preg_match('#create\s+table\s+\`([a-z0-9_]+)\`#i', $l, $m)) {
549 $tbl = 'shop_' . strtolower($m[1]);
550 $l = preg_replace('#create\s+table\s+\`([a-z0-9_]+)\`#i', "CREATE TABLE {$tbl}", $l);
552 if (preg_match('#\`([a-z0-9_]+)\`#i', $l, $m) && !preg_match('#alter\s+table\s+#i', $l)) {
553 $l = preg_replace('#\`([a-z0-9_]+)\`#i', "{$m[1]}_name", $l);
556 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
557 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
558 $extra[] = "create sequence {$tbl}_seq;";
562 if (preg_match('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', $l, $m)){
563 $l = preg_replace('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', "ALTER TABLE {$tbl}", $l);
566 // enum value -- use the text instead..
568 if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i', $l, $m)) {
569 $l = preg_replace('#enum\([\w|\W]+\)#i', "TEXT", $l);
571 // ignore the alter enum
572 if ($tbl && preg_match('#alter\s+table\s+([\w|\W]+)\s+enum\([\w|\W]+\)#i', $l, $m)) {
576 // UNIQUE KEY .. ignore
577 if ($tbl && preg_match('#UNIQUE KEY#i', $l, $m)) {
578 $last = array_pop($ret);
579 $ret[] = trim($last, ",");
583 if ($tbl && preg_match('#RENAME\s+TO#i', $l, $m)) {
587 if ($tbl && preg_match('#change\s+column#i', $l, $m)) {
591 // INDEX lookup ..ignore
592 if ($tbl && preg_match('#INDEX lookup+([\w|\W]+)#i', $l, $m)) {
593 $last = array_pop($ret);
594 $ret[] = trim($last, ",");
599 // CREATE INDEX ..ignore
600 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+#i', $l, $m)) {
601 // $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
606 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
608 $l = preg_replace('# datetime#i', ' TIMESTAMP WITHOUT TIME ZONE', $l);
609 $l = preg_replace('# blob#i', ' TEXT', $l);
610 $l = preg_replace('# longtext#i', ' TEXT', $l);
611 $l = preg_replace('# tinyint#i', ' INT', $l);
617 $ret = array_merge($extra,$ret);
618 // echo implode("\n", $ret); exit;
620 file_put_contents($fn, implode("\n", $ret));
626 function checkOpts($opts)
630 foreach($opts as $o=>$v) {
631 if (!preg_match('/^json-/', $o) || empty($v)) {
634 if (!file_exists($v)) {
635 die("File does not exist : OPTION --{$o} = {$v} \n");
639 $modules = array_reverse($this->modulesList());
641 // move 'project' one to the end...
643 foreach ($modules as $module){
644 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
645 if($module == 'Core' || !file_exists($file)){
649 $class = "Pman_{$module}_UpdateDatabase";
651 if(!method_exists($x, 'checkOpts')){
654 $x->checkOpts($opts);
658 static function jsonImportFromArray($opts)
660 foreach($opts as $o=>$v) {
661 if (!preg_match('/^json-/', $o) || empty($v)) {
664 $type = str_replace('_', '-', substr($o,5));
666 $data= json_decode(file_get_contents($v),true);
667 $pg = HTML_FlexyFramework::get()->page;
668 DB_DataObject::factory($type)->importFromArray($pg ,$data,$opts);
678 function runUpdateModulesData()
680 if (class_exists('PDO_DataObjects_Introspection')) {
681 PDO_DataObject_Introspection::$cache = array();
683 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
685 if(!in_array('Core', $this->disabled)){
686 echo "Running jsonImportFromArray\n";
687 Pman_Core_UpdateDatabase::jsonImportFromArray($this->opts);
690 echo "Running updateData on modules\n";
696 $modules = array_reverse($this->modulesList());
698 // move 'project' one to the end...
700 foreach ($modules as $module){
701 if(in_array($module, $this->disabled)){
704 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
705 if($module == 'Core' || !file_exists($file)){
710 $class = "Pman_{$module}_UpdateDatabase";
712 if(!method_exists($x, 'updateData')){
722 function updateDataEnums()
724 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
726 $enum = DB_DataObject::Factory('core_enum');
727 //DB_DAtaObject::debugLevel(1);
732 'name' => 'COMPTYPE',
733 'display_name' => 'Company Types',
734 'is_system_enum' => 1,
738 'display_name' => 'Owner',
739 'seqid' => 999, // last...
740 'is_system_enum' => 1,
747 'name' => 'HtmlEditor.font-family',
748 'display_name' => 'HTML Editor font families',
749 'is_system_enum' => 1,
752 'name' => 'Helvetica,Arial,sans-serif',
753 'display_name' => 'Helvetica',
758 'name' => 'Courier New',
759 'display_name' => 'Courier',
764 'display_name' => 'Tahoma',
768 'name' => 'Times New Roman,serif',
769 'display_name' => 'Times',
774 'display_name' => 'Verdana',
787 function updateDataGroups()
790 $groups = DB_DataObject::factory('core_group');
791 $groups->initGroups();
793 $groups->initDatabase($this,array(
795 'name' => 'bcc-email', // group who are bcc'ed on all requests.
796 'type' => 0, // system
797 'display_name' => 'Standard BCC Group'
800 'name' => 'system-email-from',
801 'type' => 0, // system
804 'name' => 'core-person-signup-bcc',
805 'type' => 0, // system
808 'name' => 'Empty Group', // use for no bcc emails.
816 function updateDataCompanies()
819 // fix comptypes enums..
820 $c = DB_DataObject::Factory('core_company');
822 $c->selectAdd('distinct(comptype) as comptype');
823 $c->whereAdd("comptype != ''");
826 foreach($c->fetchAll('comptype') as $cts) {
830 $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
833 $c = DB_DataObject::Factory('core_enum');
836 //DB_DataObject::debugLevel(1);
838 $c = DB_DataObject::Factory('core_company');
840 UPDATE {$c->tableName()}
842 comptype_id = (SELECT id FROM core_enum where etype='comptype' and name={$c->tableName()}.comptype LIMIT 1)
855 function updateDataEmails()
857 foreach ($this->emailTemplates as $k => $mail) {
859 $mail_dir = "{$this->rootDir}{$mail['template_dir']}";
869 function initEmails($templateDir, $emails, $mapping = false)
871 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
873 $pg = HTML_FlexyFramework::get()->page;
874 foreach($emails as $name=>$data) {
875 $cm = DB_DataObject::factory('core_email');
876 $update = $cm->get('name', $name);
879 if (empty($cm->bcc_group_id)) {
880 if (empty($data['bcc_group'])) {
881 $this->jerr("missing bcc_group for template $name");
884 $g = DB_DataObject::Factory('core_group')->lookup('name',$data['bcc_group']);
887 $this->jerr("bcc_group {$data['bcc_group']} does not exist when importing template $name");
890 if (!$g->members('email')) {
891 $this->jerr("bcc_group {$data['bcc_group']} does not have any members");
894 $cm->bcc_group_id = $g->id;
896 // initEmails will always have the latest location of the test class - in theory the user should not be changign the value of this...
897 //if (empty($cm->test_class)) {
898 if (empty($data['test_class'])) {
899 $this->jerr("missing test_class for template $name");
902 $cm->test_class = $data['test_class'];
904 if(isset($cm->to_group_id)) {
909 !empty($data['to_group']) &&
910 (!isset($cm->to_group_id) || !empty($cm->to_group_id))
912 $gp = DB_DataObject::Factory('core_group')->lookup('name',$data['to_group']);
914 if (empty($gp->id)) {
915 $this->jerr("to_group {$data['to_group']} does not exist when importing template $name");
918 $cm->to_group_id = $gp->id;
922 isset($data['active']) && !isset($cm->active)
924 $cm->active = $data['active'];
927 if(!empty($data['description'])){
928 $cm->description = $cm->escape($data['description']);
931 require_once $cm->test_class . '.php';
933 $clsname = str_replace('/','_', $cm->test_class);
935 $method = new ReflectionMethod($clsname , 'test_'. $name) ;
936 $got_it = $method->isStatic();
937 } catch(Exception $e) {
942 $this->jerr("template {$name} does not have a test method {$clsname}::test_{$name}");
946 echo "email: {$name} - checked\n";
947 continue; /// we do not import the body content of templates that exist...
954 // $basedir = $this->bootLoader->rootDir . $mail_template_dir;
958 'file' => $templateDir. $name .'.html'
961 if (!empty($data['master'])) {
962 $opts['master'] = $templateDir . $master .'.html';
964 require_once 'Pman/Core/Import/Core_email.php';
965 $x = new Pman_Core_Import_Core_email();
967 $x->updateOrCreateEmail('', $opts, $cm, $mapping);
969 echo "email: {$name} - CREATED\n";
974 function updateData()
977 if (class_exists('PDO_DataObjects_Introspection')) {
978 PDO_DataObject_Introspection::$cache = array();
980 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
983 $this->updateDataEnums();
984 $this->updateDataGroups();
985 $this->updateDataCompanies();
987 $this->updateDataEmails();
989 $c = DB_DataObject::Factory('I18n');
997 function fixMysqlInnodb()
1000 static $done_check = false;
1006 if (!empty($this->opts['skip-mysql-checks'])) {
1009 // innodb in single files is far more efficient that MYD or one big innodb file.
1010 // first check if database is using this format.
1011 $db = DB_DataObject::factory('core_enum');
1012 $db->query("show variables like 'innodb_file_per_table'");
1014 if ($db->Value == 'OFF') {
1015 die("Error: set innodb_file_per_table = 1 in my.cnf (or run with --skip-mysql-checks\n\n");
1018 $db = DB_DataObject::factory('core_enum');
1019 $db->query("select version() as version");
1022 if (version_compare($db->version, '5.7', '>=' )) {
1024 $db = DB_DataObject::factory('core_enum');
1025 $db->query("show variables like 'sql_mode'");
1028 $modes = explode(",", $db->Value);
1030 // these are 'new' problems with mysql.
1032 in_array('NO_ZERO_IN_DATE', $modes) ||
1033 in_array('NO_ZERO_DATE', $modes) ||
1034 in_array('STRICT_TRANS_TABLES', $modes) ||
1035 !in_array('ALLOW_INVALID_DATES', $modes)
1037 die("Error: set sql_mode include 'ALLOW_INVALID_DATES', remove 'NO_ZERO_IN_DATE' AND 'STRICT_TRANS_TABLES' AND 'NO_ZERO_DATE' in my.cnf\n\n");
1041 $done_check = true;;
1052 /** ------------- schema fixing ... there is an issue with data imported having the wrong sequence names... --- */
1054 function fixSequencesMysql()
1059 function fixSequencesPgsql()
1063 //DB_DataObject::debugLevel(1);
1064 $cs = DB_DataObject::factory('core_enum');
1068 CASE WHEN strpos(seq_name, '.') > 0 THEN
1071 quote_ident(min(schema_name)) ||'.'|| quote_ident(min(seq_name))
1074 ||' OWNED BY '|| quote_ident(min(schema_name)) || '.' ||
1075 quote_ident(min(table_name)) ||'.'|| quote_ident(min(column_name)) ||';' as cmd
1079 n.nspname AS schema_name,
1080 c.relname AS table_name,
1081 a.attname AS column_name,
1082 regexp_replace(regexp_replace(d.adsrc, E'nextval\\\\(+[''\\\"]*', ''),E'[''\\\"]*::.*\$','') AS seq_name
1084 JOIN pg_attribute a ON (c.oid=a.attrelid)
1085 JOIN pg_attrdef d ON (a.attrelid=d.adrelid AND a.attnum=d.adnum)
1086 JOIN pg_namespace n ON (c.relnamespace=n.oid)
1087 WHERE has_schema_privilege(n.oid,'USAGE')
1088 AND n.nspname NOT LIKE 'pg!_%' escape '!'
1089 AND has_table_privilege(c.oid,'SELECT')
1090 AND (NOT a.attisdropped)
1091 AND d.adsrc ~ '^nextval'
1095 CASE WHEN strpos(seq_name, '.') > 0 THEN
1096 substring(seq_name, 1,strpos(seq_name,'.')-1)
1101 GROUP BY seq_name HAVING count(*)=1
1104 while ($cs->fetch()) {
1107 foreach($cmds as $cmd) {
1108 $cs = DB_DataObject::factory('core_enum');
1112 $cs = DB_DataObject::factory('core_enum');
1114 SELECT 'SELECT SETVAL(' ||
1115 quote_literal(quote_ident(nspname) || '.' || quote_ident(S.relname)) ||
1116 ', MAX(' || quote_ident(C.attname)|| ')::integer ) FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd
1122 WHERE S.relkind = 'S'
1124 AND D.refobjid = T.oid
1125 AND D.refobjid = C.attrelid
1126 AND D.refobjsubid = C.attnum
1127 AND NS.oid = T.relnamespace
1131 while ($cs->fetch()) {
1134 foreach($cmds as $cmd) {
1135 $cs = DB_DataObject::factory('core_enum');
1142 var $extensions = array(
1147 function runExtensions()
1150 $ff = HTML_Flexyframework::get();
1152 $dburl = parse_url($ff->database);
1154 $dbtype = $dburl['scheme'];
1156 foreach($this->extensions as $ext) {
1158 $scls = ucfirst($dbtype). $ext;
1159 $cls = __CLASS__ . '_'. $scls;
1160 $fn = implode('/',explode('_', $cls)).'.php';
1161 if (!file_exists(__DIR__.'/UpdateDatabase/'. $scls .'.php')) {
1172 function checkSystem($req = false, $pref = false)
1174 // most of these are from File_Convert...
1176 // these are required - and have simple dependancies.
1177 require_once 'System.php';
1178 $req = $req !== false ? $req : array(
1183 'rsvg-convert', //librsvg2-bin
1190 // these are prefered - but may have complicated depenacies
1191 $pref = $pref !== false ? $pref : array(
1195 'html2text', // not availabe in debian squeeze
1196 'pdftocairo', //poppler-utils - not available in debian squeeze.
1206 foreach($req as $r) {
1207 if (!System::which($r)) {
1213 die("Missing these programs - need installing\n" . implode("\n",$res). "\n");
1215 foreach($pref as $r) {
1216 if (!System::which($r)) {
1222 echo "WARNING: Missing these programs - they may need installing\n". implode("\n",$res);
1229 function clearApacheDataobjectsCache()
1231 echo "Clearing Database Cache\n";
1232 // this needs to clear it's own cache along with remote one..
1235 $response = $this->curl("http://localhost{$this->local_base_url}/Core/RefreshDatabaseCache");
1237 $json = json_decode($response, true);
1239 if(empty($json['data']) || $json['data'] != 'DONE'){
1240 echo $response. "\n";
1241 echo "Clear DataObjects Cache failed\n";
1248 function clearApacheAssetCache()
1250 echo "Clearing Asset Cache\n";
1251 $response = $this->curl(
1252 "http://localhost{$this->local_base_url}/Core/Asset",
1253 array( '_clear_cache' => 1 ,'returnHTML' => 'NO' ),
1256 $json = json_decode($response, true);
1258 if(empty($json['success']) || !$json['success']) {
1259 echo $response. "\n";
1260 echo "CURL Clear Asset cache failed\n";
1267 function curl($url, $request = array(), $method = 'GET')
1269 if($method == 'GET'){
1270 $request = http_build_query($request);
1271 $url = $url . "?" . $request;
1274 $ch = curl_init($url);
1276 if ($method == 'POST') {
1278 curl_setopt($ch, CURLOPT_POST, 1);
1279 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
1282 curl_setopt($ch, CURLOPT_HTTPHEADER,
1283 array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($request)));
1287 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1289 curl_setopt($ch, CURLOPT_HEADER, false);
1290 curl_setopt($ch, CURLOPT_VERBOSE, 0);
1291 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
1292 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1294 $response = curl_exec($ch);
1301 static function verifyExtensions($extensions)
1305 foreach ($extensions as $e){
1307 if(extension_loaded($e)) {
1311 $error[] = "Error: Please install php extension: {$e}";
1317 $ff = HTML_FLexyFramework::get();
1319 $ff->page->jerr(implode('\n', $error));