DataObjects/Core_event_audit.php
[Pman.Core] / UpdateDatabase.php
1 <?php
2
3
4
5 require_once 'Pman.php';
6 class Pman_Core_UpdateDatabase extends Pman
7 {
8     
9     static $cli_desc = "Update SQL - Beta";
10  
11  
12   
13     var $cli = false;
14     function getAuth() {
15         
16         
17         $ff = HTML_FlexyFramework::get();
18         if (!empty($ff->cli)) {
19             $this->cli = true;
20             return true;
21         }
22         
23         parent::getAuth(); // load company!
24         $au = $this->getAuthUser();
25         if (!$au || $au->company()->comptype != 'OWNER') {
26             $this->jerr("Not authenticated", array('authFailure' => true));
27         }
28         $this->authUser = $au;
29         return true;
30     }
31      
32     function get()
33     {
34         $this->importSQL();
35          
36     }
37     function output() {
38         return '';
39     }
40      /**
41      * imports SQL files from all DataObjects directories....
42      * 
43      * except any matching /migrate/
44      */
45     function importSQL()
46     {
47         
48         $ff = HTML_Flexyframework::get();
49         
50         $url = parse_url($ff->DB_DataObject['database']);
51         // hide stuff for web..
52         
53         require_once 'System.php';
54         $cat = System::which('cat');
55         $mysql = System::which('mysql');
56         
57         $ar = $this->modulesList();
58         
59            
60             $mysql_cmd = $mysql .
61                 ' -h ' . $url['host'] .
62                 ' -u' . escapeshellarg($url['user']) .
63                 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass'])  :  '') .
64                 ' ' . basename($url['path']);
65         echo $mysql_cmd . "\n" ;
66         
67         
68         
69         
70         foreach($ar as $m) {
71             
72             $fd = $this->rootDir. "/Pman/$m/DataObjects";
73             
74             foreach(glob($fd.'/*.sql') as $fn) {
75                 
76                  
77                 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
78                     continue;
79                 }
80                 
81                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
82                 
83                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
84                 
85                 passthru($cmd);
86             
87                 
88             }
89         }
90         
91         
92         
93     }
94     
95 }