function beforeSave() { if (!$this['type']) { throw $this->exception('Please specify type', 'ValidityCheck')->setField('type'); } $existing_check = $this->add('Model_MarketPlace'); $existing_check->addCondition('id', '<>', $this->id); $existing_check->addCondition('namespace', $this['namespace']); $existing_check->tryLoadAny(); if ($existing_check->loaded()) { throw $this->exception('Name Space Already Used', 'ValidityCheck')->setField('namespace'); } // TODO :: check namespace on server as well... if (file_exists(getcwd() . DS . 'epan-components' . DS . $this['namespace']) and !$this->isInstalling) { throw $this->exception('namespace folder is already created', 'ValidityCheck')->setField('namespace'); } if (!$this->isInstalling) { //Added in AddComponentTorepository View $create_component_folder = true; if ($this['initialize_and_clone_from_git'] and $this['git_path']) { $repo = Git::create($dest = getcwd() . DS . 'epan-components' . DS . $this['namespace'], $this['git_path']); $create_component_folder = false; } $this->createNewFiles($create_component_folder); } }
function update($dynamic_model_update = true) { if ($this->git_path == null) { throw $this->exception('public variable git_path must be defined in page class'); } $class = get_class($this); preg_match('/page_(.*)_page_(.*)/', $class, $match); $this->component_namespace = $match[1]; $mp = $this->add('Model_MarketPlace')->loadBy('namespace', $this->component_namespace); $this->component_name = $mp['name']; $component_path = getcwd() . DS . 'epan-components' . DS . $this->component_namespace; if ($_GET['git_exec_path']) { Git::set_bin($_GET['git_exec_path']); } try { if (file_exists($component_path . DS . '.git')) { $repo = Git::open($component_path); } else { $repo = Git::create($component_path); } } catch (Exception $e) { // No Git Found ... So just return return; } $remote_branches = $repo->list_remote_branches(); if (count($remote_branches) == 0) { $repo->add_remote_address($this->git_path); } $branch = 'master'; if ($_GET['git_branch']) { $branch = $_GET['git_branch']; } $repo->run('fetch --all'); $repo->run('reset --hard origin/' . $branch); if ($dynamic_model_update) { $dir = $component_path . DS . 'lib' . DS . 'Model'; if (file_exists($dir)) { $lst = scandir($dir); array_shift($lst); array_shift($lst); foreach ($lst as $item) { $model = $this->add($this->component_namespace . '/Model_' . str_replace(".php", '', $item)); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); } } } // Re process Config file $this->add('Model_MarketPlace')->loadBy('namespace', $this->component_namespace)->reProcessConfig(); // Get new code from git // Get all models in lib/Model // add dynamic line on object // tryLoanAny }
function create_note($author_id, $author_pseudo, $author_email, $path, $note_name, $file_name, $cat_id) { $git_repo = Git::create($path); $git_repo->run("config user.name \"{$author_pseudo}\""); $git_repo->run("config user.email \"{$author_email}\""); $git_repo->add(); $git_repo->commit('Première version'); //insert data on database $note_array = array('path' => $path, 'file_name' => $file_name, 'name' => $note_name, 'visible' => 1, 'category' => $cat_id, 'author_id' => $author_id, 'creation_date' => date("Y-m-d H:i:s"), 'modification_date' => date("Y-m-d H:i:s")); $this->db->insert('note', $note_array); $note_id = $this->db->insert_id(); return $note_id; }
/** * Setup GIT Repos from Stash * * Lists all repos from remote project and downloaded them, skipping devtools * * @param string $directory Directory to work with * @param bool $force True or False on whether to rm -Rf and then recreate the repo * @return array */ function setupDevRepos($directory, $force = false, $mode = 'ssh', $branch = 'master', $project_key = 'freepbx') { $skipr = array('devtools', 'moh_sounds', 'versionupgrade', 'fw_langpacks', 'custom-freepbx-modules', 'sipstation_module'); $o = $this->stash->getAllRepos($project_key); if ($mode == 'http' && version_compare(Git::version(), '1.7.9', '<')) { freepbx::out("HTTP Mode is only supported with GIT 1.7.9 or Higher"); die; } elseif ($mode == 'http') { Git::enable_credential_cache(); } foreach ($o['values'] as $repos) { $dir = $directory . '/' . $repos['name']; if (in_array($repos['name'], $skipr)) { continue; } freepbx::outn("Cloning " . $repos['name'] . " into " . $dir . "..."); if (file_exists($dir) && $force) { freepbx::out($dir . " Already Exists but force is enabled so deleting and restoring"); exec('rm -Rf ' . $dir); } elseif (file_exists($dir)) { freepbx::out($dir . " Already Exists, Skipping (use --force to force)"); continue; } $uri = $mode == 'http' ? $repos['cloneUrl'] : $repos['cloneSSH']; $repo = Git::create($dir, $uri); $repo->add_merge_driver(); freepbx::out("Done"); $obranch = $branch; while ($branch) { try { freepbx::outn("\tChecking you out into the " . $branch . " branch..."); $repo->checkout($branch); freepbx::out("Done"); break; } catch (Exception $e) { freepbx::out("Doesnt Exist!"); $branch = $this->getLowerBranch($branch); if ($branch === false) { try { freepbx::outn("\tChecking you out into the master branch..."); $repo->checkout('master'); freepbx::out("Done"); } catch (Exception $e) { //TODO: error? } } } } $branch = $obranch; freepbx::out(" "); } }
foreach ($projects as $project => $description) { $repo = $stash->getRepo($options['m'], $project); if ($repo === false) { freepbx::out("[WARN] " . $options['m'] . " is NOT in the " . $description); } else { break; } } if ($repo === false) { freepbx::out("[ERROR] Unable to find " . $options['m']); exit(0); } $uri = $mode == 'http' ? $repo['cloneUrl'] : $repo['cloneSSH']; $dir = $directory . '/' . $options['m']; freepbx::out("Cloning " . $repo['name'] . " into " . $dir); $repo = Git::create($dir, $uri); $repo->add_merge_driver(); freepbx::out("Done"); $freepbx = new freepbx($username, $password); $freepbx->setupSymLinks($directory); } } else { freepbx::out("Module Already Exists"); if (!file_exists($directory . '/framework/amp_conf/htdocs/admin/modules/' . $options['m'])) { $username = freepbx::getInput("FreePBX Username"); $password = freepbx::getPassword("FreePBX Password", true); $freepbx = new freepbx($username, $password); $freepbx->setupSymLinks($directory); } } if (empty($vars['dev_symlinks'])) {
function testDelete() { $Git->logReponse = true; $Git = new Git($this->__repos[1]); $this->assertTrue($Git->create()); $this->assertTrue(file_exists(TMP . 'tests/git/repo/test.git')); $this->assertTrue(file_exists(TMP . 'tests/git/working/test/master/.git')); $Git->run('branch new'); $Git->update(); $Git->branch('new', true); $this->assertTrue($Git->delete()); $Git->branch('master', true); $results = $Git->find('branches'); $this->assertEqual($results, array('master')); //pr($Git->debug); //pr($Git->response); //die(); }
} else { $return = array(1, "Git could not be found at the default location"); } return $return; }, 'Git::create()' => function () { $return = null; $repo = Git::create(DIR . "/create"); if (!Git::is_repo($repo)) { $return = array(2, "Git::create() failed to produce expected output."); } else { $return = array(0, "Git::create() executed successfully"); } return $return; }, 'Git::create([ $source ])' => function () { $return = null; $repo = Git::create(DIR . "/createfrom", DIR . "/test.git"); if (!Git::is_repo($repo)) { $return = array(2, "Git::create([ \$source ]) failed to produce expected output."); } else { $return = array(0, "Git::create([ \$source ]) executed successfully"); } return $return; }, 'Git::open()' => function () { $return = null; $repo = Git::open(DIR . "/test.git"); if (!Git::is_repo($repo)) { $return = array(2, "Git::open() failed to produce expected output."); } else { $return = array(0, "Git::open() executed successfully"); } return $return;
function get_update($sname, $file_lock, $base_url, $path, $mail_lock, $remote_git = '', $remote_branch = '') { session_name($sname); session_start(); set_time_limit(300); ignore_user_abort(true); $stime = time(); $work = false; //检测、设置工作标志,存在session里(同一个session_name在一个页面未结束前会保持读写锁状态) if (empty($_SESSION['working'])) { $work = true; } if (file_exists($file_lock)) { $last_work_time = filemtime($file_lock); if ($last_work_time > 0 && time() - $last_work_time < 120) { //上次更新至今有120秒 echo "检测到正在进行工作中,本页面停止载入,请稍后再次访问。"; return false; //exit; } $work = true; unlink($file_lock); } elseif (empty($_SESSION['work_time']) || $stime - $_SESSION['work_time'] > LOCK_TIME) { //保证 LOCK_TIME 秒内只访问一次 $work = true; } if ($work && !file_exists($file_lock)) { $_SESSION['working'] = true; $_SESSION['work_time'] = $stime; file_put_contents($file_lock, $stime); } else { echo "距离上次获取请求时间间隔多短,请稍后再次访问。"; return false; //exit; } echo date("Y-m-d H:i:s") . " 读取列表中...<br>"; $list = get_list($base_url); //获取列表 if (count($list) < 2) { echo "读取Wiki列表失败,等待下次检测。<br>"; $_SESSION['work_time'] = $stime - LOCK_TIME; //取消检查时间,让检测可在稍后再次发起 unlink($file_lock); return false; } session_write_close(); //解除session,防止使其他访问页面一直等待session mk_dir($path); if (IS_WIN) { Git::windows_mode(); } if (!file_exists($path . '.git')) { echo date("Y-m-d H:i:s") . " 没有git库,尝试创建...<br>"; if ($remote_git) { //是否设置了远程仓库 echo "尝试从远程仓库克隆数据...<br>"; $ret = Git::clone_remote($path, $remote_git, $remote_branch); //从远程仓库clone(可指定分支) if (!Git::is_repo($ret) || !file_exists($path . '.git') || !$ret) { echo "从远程仓库克隆失败,本地创建...<br>"; $ret = Git::create($path); //如果clone失败,则本地创建 } } else { $ret = Git::create($path); } //直接本地创建 echo date("Y-m-d H:i:s") . " 创建结果:" . (Git::is_repo($ret) ? '成功' : '失败') . "<br>"; } $files = ls_file($path); foreach ($files as $file) { if (!is_dir($path . $file)) { unlink($path . $file); } } $tmp_arr = parse_url($base_url); $url_base = $tmp_arr['scheme'] . '://' . $tmp_arr['host']; write($path . 'list.txt', json($list)); //写出列表 echo date("Y-m-d H:i:s") . " 读取wiki列表完毕,开始读取内容页...<br>"; $count = 0; $content = get_content($base_url); write($path . 'index.html', $content); $count++; foreach ($list as $arr) { if (isset($arr['ul'])) { foreach ($arr['ul'] as $a) { if (substr($a['url'], 0, 1) == '.' || substr($a['url'], 0, 1) == '/') { //地址以.或/开头,则为wiki文档 $content = get_content((substr($a['url'], 0, 1) == '/' ? $url_base : (substr($a['url'], 0, 1) == '.' ? $url_base . $tmp_arr['path'] : '')) . $a['url']); if ($content) { write($path . $a['title'] . '.html', $content); $count++; } } } } } echo date("Y-m-d H:i:s") . " 获取操作完成,读取页面数量:" . $count . "<br>"; $repo = Git::open($path); $ret = $repo->status(true); $no_commit = preg_match('/nothing to commit, working directory clean/', $ret); if ($no_commit) { echo " 未检测到更新,共计用时:" . (time() - $stime) . "秒<br>"; } else { echo "待更新内容:<hr>" . $ret . "<hr>"; $ret0 = $repo->add(); $repo->run('config --global user.email "' . GIT_EMAIL . '"'); //git config --global user.email "*****@*****.**" $repo->run('config --global user.name "' . GIT_NAME . '"'); //git config --global user.name "Your Name" $repo->run('config --global core.quotepath false'); //配置git显示中文不转码 $ret = $repo->commit('check time: ' . date("Y-m-d H:i:s")); echo time() . " 已进行git提交,共计用时:" . (time() - $stime) . "秒<br><br>"; if ($remote_git) { $branch = $repo->active_branch(); echo "检测到远程仓库参数,提交到远程仓库...<br>"; $repo->run("remote add {$stime} " . $remote_git); //添加远程仓库 $repo->run("push -f {$stime} {$branch}:" . (empty($remote_branch) ? 'master' : "{$remote_branch}")); //强制覆盖远程仓库(可指定分支) $repo->run("remote remove {$stime}"); //删除远程仓库 } echo "提交git日志内容如下:<hr>" . nl2br(htmlspecialchars($ret)); $ret2 = $repo->run('log --stat -p -1'); echo "<hr>其他日志:<br>" . nl2br(htmlspecialchars($ret0)) . nl2br(htmlspecialchars($ret2)); unlink($mail_lock); } unlink($file_lock); return !$no_commit; }
function update($dynamic_model_update = true, $git_exec_path = null, $git_branch = 'master') { if ($this->git_path == null) { throw $this->exception('public variable git_path must be defined in page class'); } $installation_path = getcwd(); if ($git_exec_path) { Git::set_bin($git_exec_path); } if (file_exists($installation_path . DS . '.git')) { $repo = Git::open($installation_path); } else { $repo = Git::create($installation_path); } $remote_branches = $repo->list_remote_branches(); if (count($remote_branches) == 0) { $repo->add_remote_address($this->git_path); } $repo->run('fetch --all'); $repo->run('reset --hard origin/' . $git_branch); if ($dynamic_model_update) { $model = $this->add('Model_Branch'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Staff'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_EpanCategory'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Epan'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_EpanTemplates'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_EpanPage'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_EpanPageSnapshots'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_MarketPlace'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_InstalledComponents'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Tools'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Plugins'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Alerts'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Aliases'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Messages'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); $model = $this->add('Model_Users'); $model->add('dynamic_model/Controller_AutoCreator'); $model->tryLoadAny(); } // fire queries to convert superuser to 100 etc $this->query('UPDATE users SET type=IF(type="SuperUser",100,IF(type="BackEndUser",80,IF(type=100,100,50)))'); // change users type to int $this->query('ALTER TABLE `users` CHANGE `type` `type` INT NULL DEFAULT NULL'); // re Process base Element Config $base_element_market_place = $this->add('Model_MarketPlace')->loadBy('namespace', 'baseElements'); $base_element_market_place->reProcessConfig(); }