3 * Table Definition for builder_app
5 require_once 'DB/DataObject.php';
7 class Pman_Builder_DataObjects_Builder_modules extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'builder_modules'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
14 public $name; // string(64) not_null
15 public $path; // string(128) not_null
16 public $public; // int(2) not_null
19 /* the code above is auto generated do not remove the tag below */
21 // * - beforeDelete() -- return false for fail and set DO->err;
23 function beforeDelete()
26 // fixme - check builder components.
31 function applyFilters($q, $au)
33 if (!empty($q['query']['_sync'])) {
35 // use the basic builder modules for this project based on configurion.
37 //DB_DataObject::DebugLevel(1);
38 $x = DB_DataObject::factory('builder_modules');
39 $modpaths = $x->fetchAll('name', 'path');
41 $ff = HTML_FlexyFramework::get();
44 foreach($ff->enableArray as $m) {
45 if (isset($modpaths[$m])) {
48 if (file_exists($ff->baseDir .'/'. $m)) {
49 $x = DB_DataObject::factory('builder_modules');
52 'path' => $ff->baseDir .'/'. $m,
64 $ff->page->jok("Synced");
65 //$mods = $ff->enableArray;
66 echo '<PRE>';print_R($ff);exit;
73 function scanDir() // return name => mtime for files in path..
75 $gd = $this->gitDir();
79 $working = $this->gitWorking($gd['url']);
80 $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
82 // list of bjs files...
83 // or should this be totally database related...
86 foreach(glob($working . '/'. $path .'*.bjs') as $bjs) {
87 $js = preg_replace('/\.bjs$/', '.js', $bjs);
88 if (!file_exists($js)) {
91 $n = preg_replace('/\.bjs$/', '', basename($bjs));
93 $ret[$n] = filemtime( $bjs);
98 * This updates the contents of a DataObjects with the file contents if they are newer..
106 $files = $this->scanDir();
108 $gd = $this->gitDir();
112 $working = $this->gitWorking($gd['url']);
113 $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
116 $d = DB_DataObject::factory('builder_part');
117 $d->module_id = $this->id;
118 // DB_DataObject::debugLevel(1);
119 $cur = $d->fetchAll();
120 foreach($cur as $d) {
121 if (isset($files[$d->name]) && strtotime($d->updated) < $files[$d->name]) {
122 //file mtime is greater than db. -- replace!
123 $d->json = file_get_contents($working . '/'. $path . $d->name . '.bjs');
124 $d->jsource = file_get_contents($working . '/'. $path . $d->name . '.js');
126 // do not need to create it...
127 unset($files[$d->name]);
129 if (isset($files[$d->name])) {
130 unset($files[$d->name]);
133 // we do not delete anything...
134 // next create stuff..
135 foreach($files as $f=>$mt) {
136 $d = DB_DataObject::factory('builder_part');
138 $d->json = file_get_contents($working . '/'. $path . $f . '.bjs');
139 $d->jsource = file_get_contents($working . '/'. $path . $f . '.js');
140 $d->updated = date('Y-m-d H:i:s', $mt);
141 $d->module_id = $this->id;
148 * get the git configuration...
149 * @return {Array|false} false if it can not find path..
154 static $paths= array();
155 if (isset($paths[$this->path])) {
156 return $paths[$this->path];
158 // using the path determine
164 //var_dump(file_exists($path.'/.git'));
166 while (!file_exists($path.'/.git')) {
167 $path = dirname($path);
169 if (empty($path) || $path == '/') {
173 //$gpath = $path.'/.git';
175 $sub = substr($this->path, strlen($path) + 1);
177 require_once 'System.php';
178 $git = System::which('git');
182 $url = trim(`$git config --get remote.origin.url`);
189 $paths[$this->path] = $ret;
190 //var_Dump($ret); //exit;
195 function gitWorking($url)
199 $pg = HTML_FlexyFramework::get()->page;
200 $working = ini_get('session.save_path'). '/' .
201 urlencode($pg->authUser->email) . '-' .
204 require_once 'System.php';
205 $git = System::which('git');
208 if (!$this->checkURL($url)) {
209 $pg->jerr("netrc authentication not set up for www-data to access $url");
211 // var_dump($working);exit;
212 if (file_exists($working)) {
219 // might take some time..
220 chdir (ini_get('session.save_path'));
222 $cmd = "$git clone ". escapeshellarg($url) ." " . basename($working);
228 * dumb http check to see if we are authenticated..
230 function checkURL($u)
232 if (!preg_match('#^(http|https):/#i', $u)) {
236 $curl = System::which('curl');
237 $cmd = "$curl -n -I " . escapeshellarg($u);
239 //var_dump($res);exit;
240 $lines = explode("\n", $res);
241 if (!preg_match('/401/', $lines[0])) {
248 function gitCommit($file,$data)
250 $gd = $this->gitDir();
253 // directory is not git..
254 if (empty($this->path)) {
257 if (!file_exists($this->path)) {
258 HTML_FlexyFramework::get()->page->jerr("configured url is not git or writable: {$this->path}");
261 file_put_contents(rtrim($this->path,'/') .'/'. $file, $data);
266 $working = $this->gitWorking($gd['url']);
268 $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
269 $target = $working.'/'.$path . $file;
270 $exist = file_exists($target);
271 file_put_contents($working.'/'.$path . $file, $data);
273 require_once 'System.php';
274 $git = System::which('git');
278 $cmd = "$git add {$path}{$file}";
282 $cmd = "$git commit -m '{$path}{$file} - Commit from online editor' {$path}{$file}";
290 function gitCommitDelete($file)
292 $gd = $this->gitDir();
296 $working = $this->gitWorking($gd['url']);
299 $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
300 $target = $working.'/'.$path . $file;
301 //var_dump($target);exit;
302 if (!file_exists($target)) {
303 return; // no need to do anything..
307 require_once 'System.php';
308 $git = System::which('git');
311 `$git rm {$path}{$file}`;
313 $cmd = "$git commit -m '{$path}{$file} - Commit (DELETE) from online editor' ";