/** @param params the parameters to match against
 		@param defaults the default values to be passed to upsert
 		@return returns the primary key for a model using cached results */
 public function identifier($params, $defaults = array())
 {
     $key = $this->key($params);
     if (!isset($this->cache[$key])) {
         $model = $this->model;
         $changes = array("summary" => "none");
         $obj = $this->readonly ? new $this->model($this->db->filter($params)) : $model::upsert($this->db->filter($params), $params, $defaults, $changes);
         $this->cache[$key] = $obj->primaryKey();
         if ($changes["summary"] == "new") {
             DTLog::info(DTLog::colorize("New object '{$model}' ({$this->cache[$key]})", "warn"));
             DTLog::info($changes);
         }
     }
     return $this->cache[$key];
 }
 /**
  * Executes the necessary commands to deploy the website.
  */
 public function execute()
 {
     try {
         // Make sure we're in the right directory
         //exec('cd '.$this->_directory, $output);
         chdir($this->_directory);
         DTLog::info("Changing working directory to {$this->_directory}...");
         // Discard any changes to tracked files since our last deploy
         exec('git reset --hard HEAD', $output);
         DTLog::info('Reseting repository... ' . implode("\n", $output));
         // Update the local repository
         exec('git pull ' . $this->_remote . ' ' . $this->_branch, $output2);
         DTLog::info('Pulling in changes... ' . implode("\n", $output2));
         // Secure the .git directory
         //exec('chmod -R o-rx .git');
         //DTLog::info('Securing .git directory... ');
         if (is_callable($this->post_deploy)) {
             call_user_func($this->post_deploy, $this->_data);
         }
         DTLog::info('Deployment successful.');
     } catch (Exception $e) {
         DTLog::info($e, 'ERROR');
     }
 }
 /**
  * Handle commit hook post from bitbucket or github
  * @return string
  */
 public function commit_hook()
 {
     if (isset($_POST['payload'])) {
         // github and bitbucket use a 'payload' parameter
         $json = $_POST['payload'];
     } else {
         $json = file_get_contents('php://input');
     }
     if (!$json) {
         DTLog::debug('ignored #1');
         return 'ignored';
     }
     $data = $json ? json_decode($json, true) : null;
     if (!$data || !is_array($data['commits'])) {
         DTLog::debug('ignored #2');
         return 'ignored';
     }
     // look through the commits
     $found = false;
     $tags = array();
     foreach ($data['commits'] as $commit) {
         if (preg_match('/\\[deploy(:.+)?\\]/', $commit['message'], $matches)) {
             $found = true;
             if (count($matches) > 1 && $matches[1] != '') {
                 $tags[] = substr($matches[1], 1);
             } else {
                 $tags[] = 'live';
             }
         }
     }
     if (!$found) {
         return 'ignored';
     }
     if (defined('DEPLOY_TAG') && !in_array(DEPLOY_TAG, $tags)) {
         DTLog::debug('ignored #3');
         return 'ignored';
     }
     // create the deployment
     increase_time_limit_to(600);
     $deploy = new Deploy(BASE_PATH, array());
     $deploy->post_deploy = function () use($deploy) {
         global $_FILE_TO_URL_MAPPING;
         // composer install if detected
         if (file_exists(BASE_PATH . DIRECTORY_SEPARATOR . 'composer.json')) {
             if (file_exists('/usr/local/bin/composer')) {
                 // TODO: more flexible composer detection
                 exec('composer install', $output);
                 DTLog::info('Executing composer install...' . implode("\n", $output));
                 //Checking for composer.phar
             } elseif (file_exists('/usr/local/bin/composer.phar')) {
                 exec('/usr/local/bin/composer.phar install', $output);
                 DTLog::info('Executing composer install...' . implode("\n", $output));
             } else {
                 DTLog::info('composer.json detected but unable to locate composer.');
             }
         }
         // clear cache
         DTLog::info('Clearing cache...');
         DeployController::clear_cache();
         // update database
         if (isset($_FILE_TO_URL_MAPPING[BASE_PATH])) {
             exec('php framework/cli-script.php dev/build', $output2);
             DTLog::info('Updating database...' . implode("\n", $output2));
         } else {
             DTLog::info('Database not updated. $_FILE_TO_URL_MAPPING must be set for ' . BASE_PATH);
         }
         //    		SS_ClassLoader::instance()->getManifest()->regenerate();
         //            ob_start();
         //            DatabaseAdmin::create()->doBuild(false, true, false);
         //            DTLog::info('dev/build complete: '.ob_get_contents());
         //            ob_end_clean();
     };
     $deploy->execute();
     return 'ok';
 }
 public function synchronizeItems(&$version, $prefix = null)
 {
     $count = 0;
     $prefix = empty($prefix) ? "users/{$this->user}" : $prefix;
     $items = $this->itemsList($version, $prefix);
     DTLog::info("synchronizing %s items...", count($items));
     $highest_version = 0;
     foreach ($items as $itemKey => $item_version) {
         //step through the modified items
         $count++;
         if ($count % 20 == 0) {
             DTLog::info("> %s%%", round($count / count($items) * 100));
         }
         $highest_version = max($highest_version, $item_version);
         $item = $this->item($itemKey, $prefix);
         if ($item == null || $item["itemType"] == "note" || $item["itemType"] == "attachment") {
             //well, we didn't get it, what can we do?
             continue;
         }
         $item["itemKey"] = $itemKey;
         //we keep this for the delete logic (see +synchronize()+)
         //DTLog::debug($item);
         $params = new DTParams($item);
         //give us the chance to clean the params (only once!)
         $title = strtolower($params->stringParam("title"));
         $itemKey = $params->stringParam("itemKey");
         //$qb = $this->db->where("item_key='{$itemKey}' OR LEVENSHTEIN(LOWER(substring(title from 1 for 250)),substring('{$title}' from 1 for 250))<10");
         $qb = $this->db->where("item_key='{$itemKey}' OR LEVENSHTEIN(LOWER(title),'{$title}')<6");
         $this->mergePublication($qb, $params->allParams());
     }
     return $highest_version;
 }
 protected function ok($message)
 {
     DTLog::info($message);
     echo "{$message}\n";
 }