/** Grabs the parameters sent via GET. Thanks for the htaccess file, the $_GET array doens't have them anymore, so we need to use the REQUEST_URI key. **/ private function getQueryStringParams() { $url = explode("?", $_SERVER['REQUEST_URI']); if (!isset($url[1])) { return array(); } else { $ret = array(); $params = $url[1]; $params = explode("&", $params); Makiavelo::info("Query params total::" . print_r($params, true)); foreach ($params as $value_key) { $parts = explode("=", $value_key); $key = urldecode($parts[0]); Makiavelo::info("query param: " . print_r($parts, true)); if (strpos($key, "[") !== false) { $new_key = substr($key, 0, strpos($key, "[")); $second_key = substr($key, strpos($key, "[") + 1, strpos($key, "]") - 1); $second_key = str_replace("[", "", $second_key); $second_key = str_replace("]", "", $second_key); if (!isset($ret[$new_key]) || !is_array($ret[$new_key])) { $ret[$new_key] = array(); } $ret[$new_key][$second_key] = $parts[1]; } else { if (isset($parts[1])) { $ret[$parts[0]] = $parts[1]; } } } Makiavelo::info("returning::" . print_r($ret, true)); return $ret; } }
public static function query($sql) { $db = DBLayer::connect(); $return = mysql_query($sql, $db); if (!$return) { Makiavelo::info("Error on MYSQL Query:: " . mysql_error()); } return $return; }
public function validate($val) { Makiavelo::info("Validating if '{$val}' is numeric..."); if (!is_numeric($val)) { if (class_exists("I18n")) { $err_msg = I18n::t("makiavelo.entities.errors.integer"); } else { $err_msg = " must be an integer"; } $this->setErrorMsg($err_msg); return false; } return true; }
public function handleRequest($get, $post, $named) { $this->statusCode = Makiavelo::RESPONSE_CODE_OK; Makiavelo::info("Handling uploading files..."); foreach ($_FILES as $field_name => $data) { foreach ($data['tmp_name'] as $fname => $tmp_name) { Makiavelo::info("-- File (" . $field_name . ") : " . ROOT_PATH . Makiavelo::UPLOADED_FILES_FOLDER . "/" . $data['name'][$fname]); $res = move_uploaded_file($tmp_name, ROOT_PATH . Makiavelo::UPLOADED_FILES_FOLDER . "/" . $data['name'][$fname]); $post[$field_name][$fname . "_path"] = str_replace("/public", "", Makiavelo::UPLOADED_FILES_FOLDER . "/" . $data['name'][$fname]); } } $this->request = new HTTPRequest($_SERVER['REQUEST_URI'], array_merge($get, $post, $named), $_SERVER['REQUEST_METHOD']); $this->flash = new Flash(); }
function count_project_commit($where = null) { global $__db_conn; $sql = "SELECT count(*) as cant from project_commit"; if ($where != null) { $sql .= " WHERE " . $where; } $result = mysql_query($sql, $__db_conn); if (!$result) { Makiavelo::info("ERROR MYSQL :: " . mysql_error() . "::" . $sql); return 0; } else { $row = mysql_fetch_assoc($result); return $row['cant']; } }
public function execute($params) { Makiavelo::info("Creating Database..."); $sql_folder_path = ROOT_PATH . Makiavelo::SQL_CREATE_TABLES_FOLDER; Makiavelo::puts("Creating database..."); $conn = DBLayer::connect(); $db_name = DBLayer::getDBName(); $sql = "CREATE DATABASE `{$db_name}`"; if (!mysql_query($sql, $conn)) { Makiavelo::info("ERROR creating db: " . mysql_error()); } //We also have to create the migrations table $sql_migrations = "CREATE TABLE migrations ( migration INT PRIMARY KEY);"; mysql_select_db($db_name); if (!mysql_query($sql_migrations, $conn)) { Makiavelo::info("ERROR creating migrations table:: " . mysql_error()); } DBLayer::disconnect($conn); }
function load_developer_where($where) { global $__db_conn; $sql = "SELECT * FROM developer WHERE {$where}"; #SELECT * FROM tipo_buque WHERE id = " . $id; $result = mysql_query($sql, $__db_conn); if (!$result) { Makiavelo::info("ERROR MYSQL:: " . __FILE__ . "::" . mysql_error()); return null; } else { if (mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); $new = new Developer(); $new->load_from_array($row); return $new; } else { return null; } } }
public function isUserAllowed($role) { Makiavelo::info("Checking if user can access url..."); Makiavelo::info("User role: " . $this->currentRole()); Makiavelo::info("Route minimun role: " . $role); Makiavelo::info("List of roles: " . print_r($this->roles, true)); if ($role == "" || $this->currentRole() == "") { return true; } if (!$this->isUserLoggedIn()) { if ($this->roles[$role] > 0) { return false; } else { return true; } } if ($this->isUserLoggedIn()) { return $this->roles[$this->currentRole()] >= $this->roles[$role]; } }
public function generateControllerFile() { $template_file = ROOT_PATH . Makiavelo::CONTROLLER_TEMPLATE_FILE; $target_file = ROOT_PATH . Makiavelo::CONTROLLERS_FOLDER . "/" . $this->controller_name . "Controller.php"; $fp = fopen($template_file, "r"); if ($fp) { $code = fread($fp, filesize($template_file)); $code = str_replace("[NAME]", $this->controller_name, $code); $code = str_replace("[UC_NAME]", $this->uc_controller_name, $code); fclose($fp); if (file_exists($target_file)) { Makiavelo::info("Controller file already exists ({$target_file}), not saving!"); } else { $fp = fopen($target_file, "w"); if ($fp) { fwrite($fp, $code); fclose($fp); } } } }
/** Allows the dev to modify the structure of a table: Supported operations: - add_field - drop_field */ protected function alter_table($tname, $params) { global $__db_conn; foreach ($params as $operation => $parms) { switch ($operation) { case "add_field": $keys = array_keys($parms); $new_field = $keys[0]; $type = $this->sql_types_mapping[$parms[$new_field]]; $sql = "ALTER TABLE {$tname} ADD COLUMN {$new_field} {$type}"; break; case "drop_field": $new_field = $parms; $sql = "ALTER TABLE {$tname} drop column {$new_field} "; break; default: break; } Makiavelo::info("Altering table :: " . $sql); DBLayer::query($sql); } }
public static function localize($datetime) { Makiavelo::info("---------------------------------------"); Makiavelo::info("Localizing the following date: {$datetime}"); $dtime = new DateTime($datetime); $res = $dtime->format(I18n::$date_format); Makiavelo::info("Result:: {$res}"); Makiavelo::info("---------------------------------------"); return $res; /* if(trim($datetime) == "") { return; } $date_time = explode(" ", $datetime); $date = trim($date_time[0]); $time = null; if(isset($date_time[1])) { $time = trim($date_time[1]); $time = explode(":", $time); } $date = explode("-", $date); $current_format = I18n::$date_format; $current_format = str_replace("%m", $date[1], $current_format); $current_format = str_replace("%d", $date[2], $current_format); $current_format = str_replace("%y", $date[0], $current_format); if($time != null) { $current_format = str_replace("%H", $time[0], $current_format); $current_format = str_replace("%i", $time[1], $current_format); $current_format = str_replace("%s", $time[2], $current_format); } return $current_format; */ }
/** * Returns all the issues given an user, a repository and a status * * @param (usr) the GitHub username * @param (repo) the GitHub repository name * @param (status) could be open or closed * @return an array with issues. */ public static function getProjectIssues($usr, $repo, $status, $since = null) { if (self::$TOKEN == null) { self::$TOKEN = self::requestAuth(); } $repo = str_replace(".git", "", $repo); $issues_url = "https://api.github.com/repos/" . $usr . "/" . $repo . "/issues?state=" . $status . "&page=1&per_page=100"; if ($since != null) { $issues_url = $issues_url . "&since=" . $since . "T00:00:00Z"; } Makiavelo::info("Querying URL: " . $issues_url); $data = self::sendRequest($issues_url); return $data; }
/** Retrieves a list of Faq @order = Optional, can be an array of keys or just a single key to order by the results @limit = Optional */ function list_faq($order = null, $limit = null, $where = null) { global $__db_conn; $sql = "SELECT * FROM faq"; if ($where != null) { $sql .= " WHERE " . $where; } if ($order != null) { $order_str = $order; if (is_array($order)) { $order_str = implode(",", $order); } $sql .= " order by {$order_str}"; } if ($limit != null) { $sql .= " limit {$limit}"; } $result = mysql_query($sql, $__db_conn); if (!$result) { Makiavelo::info("Mysql error: " . mysql_error() . "::" . $sql); } $results = array(); while ($row = mysql_fetch_assoc($result)) { $tmp = new Faq(); $tmp->load_from_array($row); $results[] = $tmp; } return $results; }
public function generateCRUDViewsFor($name, $attributes = array()) { Makiavelo::info("Generating VIEWS..."); $views_folder = ROOT_PATH . Makiavelo::VIEWS_FOLDER . $name; @mkdir($views_folder); //Index file: $index_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "index.html.php"; $template_lines = file($index_file_path); if (count($template_lines) > 0) { foreach ($template_lines as $i => $template_code) { #$template_code = fread($fp, filesize($index_file_path)); $template_lines[$i] = str_replace("[NAME]", $name, $template_code); //Headers for the list preg_match("/\\[HEADERS: (.*)\\]/", $template_code, $match); if (isset($match[0]) && $match[0] != "") { $headers_template_tag = $match[0]; $header_tag = $match[1]; $html_headers = ""; foreach ($attributes as $attr_name => $type) { $html_headers .= str_replace("{attr_name}", $attr_name, "{$header_tag}") . "\n"; } $html_headers = str_replace("\\t", "\t", $html_headers); $template_code = preg_replace("/(\t+)\\[/", "[", $template_code); $template_lines[$i] = str_replace($headers_template_tag, $html_headers, $template_code); } #Values for the list preg_match("/\\[ATTRIBUTE_VALUE: (.*)\\]/", $template_code, $match); if (isset($match[0]) && $match[0] != "") { $headers_template_tag = $match[0]; $header_tag = $match[1]; $html_headers = ""; foreach ($attributes as $attr_name => $type) { $html_headers .= str_replace("{attr_name}", $attr_name, "{$header_tag}") . "\n"; } $html_headers = str_replace("\\t", "\t", $html_headers); $template_code = preg_replace("/(\t+)\\[/", "[", $template_code); $template_lines[$i] = str_replace($headers_template_tag, $html_headers, $template_code); } $template_lines[$i] = str_replace("[UC_NAME]", Makiavelo::camel_to_underscore($name), $template_lines[$i]); } $destination_index = $views_folder . "/index.html.php"; if (file_exists($destination_index)) { Makiavelo::info("view/index file already exists, not saving..."); } else { $fp = fopen($destination_index, "w"); if ($fp) { //Save the file fwrite($fp, implode("", $template_lines)); fclose($fp); } } } //New file: $new_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "new.html.php"; $fp = fopen($new_file_path, "r"); if ($fp) { $template_code = fread($fp, filesize($new_file_path)); $template_code = str_replace("[NAME]", $name, $template_code); } $destination_new = $views_folder . "/new.html.php"; if (file_exists($destination_new)) { Makiavelo::info("view/new file already exists, not saving..."); } else { $fp = fopen($destination_new, "w"); if ($fp) { //Save the file fwrite($fp, $template_code); fclose($fp); } } //Show file: $show_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "show.html.php"; $fp = fopen($show_file_path, "r"); if ($fp) { $template_code = fread($fp, filesize($show_file_path)); $template_code = str_replace("[NAME]", $name, $template_code); #Values for the list preg_match("/\\[ATTRIBUTE: (.*)\\]/", $template_code, $match); if (isset($match[0]) && $match[0] != "") { $attribute_template_tag = $match[0]; $attribute_tag = $match[1]; $attribute_list = ""; foreach ($attributes as $attr_name => $type) { $attribute_list .= str_replace("{attr_name}", $attr_name, "{$attribute_tag}") . "\n"; } $template_code = str_replace($attribute_template_tag, $attribute_list, $template_code); } } $destination_show = $views_folder . "/show.html.php"; if (file_exists($destination_show)) { Makiavelo::info("view/show file already exists, not saving..."); } else { $fp = fopen($destination_show, "w"); if ($fp) { //Save the file fwrite($fp, $template_code); fclose($fp); } } //Edit file: $edit_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "edit.html.php"; $fp = fopen($edit_file_path, "r"); if ($fp) { $template_code = fread($fp, filesize($edit_file_path)); $template_code = str_replace("[NAME]", $name, $template_code); } $destination_edit = $views_folder . "/edit.html.php"; if (file_exists($destination_edit)) { Makiavelo::info("view/edit file already exists, not saving..."); } else { $fp = fopen($destination_edit, "w"); if ($fp) { //Save the file fwrite($fp, $template_code); fclose($fp); } } //_form file: $form_file_path = ROOT_PATH . Makiavelo::ABM_VIEWS_TEMPLATE_FOLDER . "_form.html.php"; $form_file_lines = file($form_file_path); Makiavelo::info("Reading '{$form_file_path}'..."); if (count($form_file_lines) > 0) { Makiavelo::info(count($form_file_lines) . " lines ..."); foreach ($form_file_lines as $i => $template_code) { //List of form fields preg_match("/\\[FORM_FIELD: (.*)\\]/", $template_code, $match); if (isset($match[0]) && $match[0] != "") { $form_template_tag = $match[0]; $form_tag = $match[1]; $html_form_fields = ""; foreach ($attributes as $attr_name => $type) { $tmp = str_replace("{type}", $this->type_mapping[$type], "{$form_tag}") . "\n"; $tmp = str_replace("{attr_name}", $attr_name, "{$tmp}") . "\n"; $html_form_fields .= $tmp; } $html_form_fields = str_replace("\\t", "\t", $html_form_fields); $template_code = preg_replace("/(\t+)\\[/", "[", $template_code); $form_file_lines[$i] = str_replace($form_template_tag, $html_form_fields, $template_code); } } } $destination_form = $views_folder . "/_form.html.php"; if (file_exists($destination_form)) { Makiavelo::info("view/_form file already exists, not saving..."); } else { $fp = fopen($destination_form, "w"); if ($fp) { //Save the file fwrite($fp, implode("", $form_file_lines)); fclose($fp); } } }
private function updateMigrationsTable($migration_number) { $sql = "INSERT INTO migrations (migration) values (" . $migration_number . ")"; Makiavelo::info("Updating migrations table:: {$sql}"); DBLayer::query($sql); }
/** Loads all projects and queries Githubs API for new data on stars and forks */ public function generateAction() { $projects = list_project(null, null, "published = 1"); Makiavelo::info("=== Starting stats process ==="); foreach ($projects as $proj) { $proj_name = $proj->name; $usr_name = $proj->owner()->name; $dev = $proj->owner(); Makiavelo::info("==== Querying for {$usr_name}/{$proj_name}"); Makiavelo::info("Last Update: " . $proj->updated_at); if (strstr($proj->updated_at, date("Y-m-d"))) { Makiavelo::info("Skiping project"); continue; //Avoid duplicated entries } $data = GithubAPI::queryProjectData($usr_name, $proj_name); //We update the dev's avatar if needed if ($data->owner->avatar_url != $dev->avatar_url) { $dev->avatar_url = $data->owner->avatar_url; save_developer($dev); } //Calculate the commits for today $commits_today = 0; $today = date("Y-m-d"); /* Makiavelo::info("========================"); Makiavelo::info(print_r($data->commits, true)); Makiavelo::info("========================"); exit(); */ foreach ($data->commits as $commit) { $commit_date = $commit->commit->committer->date; $commit_date = explode("T", $commit_date); $commit_date = $commit_date[0]; $pc = load_project_commit_where("sha = '" . $commit->sha . "'"); if ($pc == null) { //We make sure we haven't yet saved this commit $project_commit = new ProjectCommit(); $project_commit->project_id = $proj->id; $project_commit->committer = $commit->committer->login; $project_commit->commit_message = $commit->commit->message; $project_commit->sha = $commit->sha; $project_commit->commit_date = $commit_date; save_project_commit($project_commit); } if ($commit_date == $today) { $commits_today++; } } $new_pulls_today = $closed_pulls_today = $merged_pulls_today = 0; $total_pulls = 0; $total_merged_pulls = 0; foreach ($data->pulls as $pull) { $created_data = explode("T", $pull->created_at); $closed_data = explode("T", $pull->closed_at); $merged_data = explode("T", $pull->merged_at); $total_pulls++; if ($pull->merged_at) { $total_merged_pulls++; } if ($created_data[0] == $today) { $new_pulls_today++; } if ($closed_data[0] == $today) { $closed_pulls_today++; } if ($merged_data[0] == $today) { $merged_pulls_today++; } } if ($total_pulls > 0) { $proj->pr_acceptance_rate = $total_merged_pulls / $total_pulls * 100; } else { $proj->pr_acceptance_ratec = -1; } $delta_stars = $data->watchers - $proj->stars; $delta_forks = $data->forks - $proj->forks; //if($delta_stars != 0 || $delta_forks != 0) { Makiavelo::info("==== Delta found, saving..."); //We also update the project $proj->stars = $data->watchers; $proj->forks = $data->forks; $proj->readme = $data->readme; $proj->description = $data->description; $proj->open_issues = $data->open_issues; save_project($proj); $pd = new ProjectDelta(); $pd->forks = $data->forks; $pd->delta_forks = $delta_forks; $pd->open_issues = $data->open_issues; $pd->closed_issues = $data->closed_issues; $pd->stars = $data->watchers; $pd->delta_stars = $delta_stars; $pd->project_id = $proj->id; $pd->commits_count = $commits_today; $pd->new_pulls = $new_pulls_today; $pd->closed_pulls = $closed_pulls_today; $pd->merged_pulls = $merged_pulls_today; $pd->sample_date = date("Y-m-d H:i:s"); if (save_project_delta($pd)) { Makiavelo::info("===== Delta saved! "); Makiavelo::info(print_r($pd, true)); } else { Makiavelo::info("===== ERROR saving delta"); } //} delete_issues_by_project_id($proj->id); foreach ($data->open_issues_list as $issue) { $iss = new Issue(); $iss->title = $issue->title; $iss->body = MarkdownExtra::defaultTransform($issue->body); $iss->created_at = $issue->created_at; $iss->updated_at = $issue->updated_at; $iss->url = $issue->html_url; $iss->number = $issue->number; $iss->project_id = $proj->id; if (save_issue($iss)) { Makiavelo::info("===== Issue saved! "); } else { Makiavelo::info("===== ERROR saving issue::" . mysql_error()); } } } }
public function load() { $projects = list_project(); Makiavelo::info("=== Starting old stats process ==="); foreach ($projects as $proj) { $proj_name = $proj->name; $usr_name = $proj->owner()->name; Makiavelo::puts("==== Querying for {$usr_name}/{$proj_name}"); $g_data = GithubAPI::queryProjectData($usr_name, $proj_name); //Calculate the commits for today $data = array(); foreach ($g_data->commits as $commit) { $commit_date = $commit->commit->committer->date; $commit_date = explode("T", $commit_date); $commit_date = $commit_date[0]; $date_idx = intval(str_replace("-", "", $commit_date)); if (!isset($data[$date_idx]) || !isset($data[$date_idx]['commits'])) { $data[$date_idx] = array("commits" => 1); } else { $data[$date_idx]['commits']++; } } foreach ($g_data->pulls as $pull) { $created_data = explode("T", $pull->created_at); $closed_data = explode("T", $pull->closed_at); $merged_data = explode("T", $pull->merged_at); $created_idx = intval(str_replace("-", "", $created_data[0])); $merged_idx = intval(str_replace("-", "", $merged_data[0])); $closed_idx = intval(str_replace("-", "", $closed_data[0])); if (!isset($data[$created_idx]) || !isset($data[$created_idx]['new_pulls'])) { $data[$created_idx]['new_pulls'] = 1; } else { $data[$created_idx]['new_pulls']++; } if ($merged_idx != 0) { if (!isset($data[$merged_idx]) || !isset($data[$merged_idx]['merged_pulls'])) { $data[$merged_idx]['merged_pulls'] = 1; } else { $data[$merged_idx]['merged_pulls']++; } } if ($closed_idx != 0) { if (!isset($data[$closed_idx]) || !isset($data[$closed_idx]['closed_pulls'])) { $data[$closed_idx]['closed_pulls'] = 1; } else { $data[$closed_idx]['closed_pulls']++; } } } //Makiavelo::puts(print_r($data, true)); //exit; foreach ($data as $date => $stats) { $year = substr($date, 0, 4); $month = substr($date, 4, 2); $day = substr($date, 6, 2); $str_date = $year . "-" . $month . "-" . $day; Makiavelo::info("==== Delta found, saving..."); Makiavelo::puts("Saving delta for date: {$str_date}"); $pd = new ProjectDelta(); $pd->forks = -99; $pd->delta_forks = -99; $pd->stars = -99; $pd->delta_stars = -99; $pd->project_id = $proj->id; $pd->commits_count = isset($stats['commits']) ? $stats['commits'] : 0; $pd->new_pulls = isset($stats['new_pulls']) ? $stats['new_pulls'] : 0; $pd->closed_pulls = isset($stats['closed_pulls']) ? $stats['closed_pulls'] : 0; $pd->merged_pulls = isset($stats['merged_pulls']) ? $stats['merged_pulls'] : 0; $pd->sample_date = $str_date; if (save_project_delta($pd)) { Makiavelo::puts("===== Delta saved! "); } else { Makiavelo::puts("===== ERROR saving delta::" . mysql_error()); } } delete_issues_by_project_id($proj->id); foreach ($data->open_issues_list as $issue) { $iss = new Issue(); $iss->title = $issue->title; $iss->body = $issue->body; $iss->created_at = $issue->created_at; $iss->updated_at = $issue->updated_at; $iss->url = $issue->url; $iss->number = $issue->number; $iss->project_id = $proj->id; if (save_issue($iss)) { Makiavelo::info("===== Issue saved! "); } else { Makiavelo::info("===== ERROR saving issue::" . mysql_error()); } } } }
include_once ROOT_PATH . "/core/spyc.php"; include_once ROOT_PATH . "/core/I18nClass.php"; include_once ROOT_PATH . "/config/config.php"; //Includes all sql helpers $sql_helper_folder = ROOT_PATH . Makiavelo::SQL_HELPERS_FOLDER; $d = dir($sql_helper_folder); while (false !== ($entry = $d->read())) { if ($entry[0] != ".") { include $sql_helper_folder . "/" . $entry; } } //Includes all generic helpers $code_helper_folder = ROOT_PATH . Makiavelo::CODE_HELPERS_FOLDER; Makiavelo::info("loading code helpers from: " . $code_helper_folder); $d = dir($code_helper_folder); while (false !== ($entry = $d->read())) { if ($entry[0] != ".") { include $code_helper_folder . "/" . $entry; } } //DB connection... simple for now... $__db_conn = DBLayer::connect(); Makiavelo::info("Initializing bootstrap script..."); RoutesHandler::generateRoutesHelpers(); $rHandler = new RoutesHandler(); $sLayer = new SecurityLayer(); $q = isset($_GET['q']) ? $_GET['q'] : ""; Makiavelo::info("Querystring: " . print_r($_GET, true)); $response_code = $rHandler->checkRoute($q, $_SERVER, $sLayer); $mkCore = new MakiaveloCore($rHandler); $mkCore->handleResponseCode($response_code);
public function generateEntity($data) { Makiavelo::debug("Generating entity '" . $data['name'] . "'...", Makiavelo::DEBUG_LEVEL_INFO); $en_name = $this->en_name; $this->en_attributes = array_merge($this->default_entity_attributes, $data['fields']); //Grab the template for the class and replace the fields $entity_template = ROOT_PATH . Makiavelo::TEMPLATES_FOLDER . "EntityTemplateClass.php"; Makiavelo::debug("Opening entity template: {$entity_template}", Makiavelo::DEBUG_LEVEL_INFO); $fp = fopen($entity_template, "r"); $entity_code = ""; if ($fp) { $entity_code = fread($fp, filesize($entity_template)); $entity_code = str_replace("[NAME]", $en_name, $entity_code); $attr_code = ""; foreach ($this->en_attributes as $att_name => $att_type) { Makiavelo::debug("Adding attribute: {$att_name} => {$att_type}", Makiavelo::DEBUG_LEVEL_INFO); $attr_code .= "private \$" . $att_name . "; //type: " . $att_type . "\n"; } $entity_code = str_replace("[ATTRIBUTES]", $attr_code, $entity_code); fclose($fp); $entity_code = str_replace("[VALIDATIONS]", $this->validations_str, $entity_code); } //Save the template into the new entity $entity_destination_file = ROOT_PATH . Makiavelo::ENTITITES_FOLDER . $en_name . "Class.php"; Makiavelo::debug("Saving entity in: {$entity_destination_file}", Makiavelo::DEBUG_LEVEL_INFO); if (file_exists($entity_destination_file)) { Makiavelo::info("Entity class already exists, not saving..."); return; } else { $fp = fopen($entity_destination_file, "w"); if ($fp) { fwrite($fp, $entity_code); fclose($fp); } } }
public static function generateRoutesHelpers() { $general_routes_file = ROOT_PATH . Makiavelo::APP_CONFIG_FOLDER . "/routes.php"; include $general_routes_file; foreach ($_ROUTES as $entity_routes) { foreach ($entity_routes as $action => $data) { Makiavelo::info("Creating function helper: " . Makiavelo::camel_to_underscore($data['controller']) . "_" . $action . '_path ()'); $fnc = 'function ' . Makiavelo::camel_to_underscore($data['controller']) . "_" . $action . '_path ($params = array()) { $url = "' . $data['url'] . '"; $args = func_get_args(); preg_match_all("/:([a-zA-Z_0-9]*)/", $url, $matches); foreach($matches[1] as $i => $attr) { if(is_object($args[$i])) { $value = $args[$i]->$attr; } else { $value = $args[$i]; } $url = str_replace(":$attr", $value, $url); } if(is_array($params) && count($params) > 0) { $url .= "?"; $query_params = array(); foreach($params as $k => $v) { $query_params[] = $k . "=" . $v; } $url .= implode("&", $query_params); } return $url; }'; eval($fnc); } } }
public function run() { $projects = list_project(null, null, "published = 1"); Makiavelo::info("=== Starting stats process ==="); foreach ($projects as $proj) { $proj_name = $proj->name; $usr_name = $proj->owner()->name; $dev = $proj->owner(); Makiavelo::info("==== Querying for {$usr_name}/{$proj_name}"); Makiavelo::puts("==== Querying for {$usr_name}/{$proj_name}"); $data = GithubAPI::queryProjectData($usr_name, $proj_name); if (isset($data->message)) { Makiavelo::info("Project error: " . $data->message); continue; } //We update the dev's avatar if needed if ($data->owner->avatar_url != $dev->avatar_url) { $dev->avatar_url = $data->owner->avatar_url; save_developer($dev); } //Calculate the commits for today $commits_today = 0; $today = '2013-05-02'; foreach ($data->commits as $commit) { $commit_date = $commit->commit->committer->date; $commit_date = explode("T", $commit_date); $commit_date = $commit_date[0]; $pc = load_project_commit_where("sha = '" . $commit->sha . "'"); if ($pc == null) { //We make sure we haven't yet saved this commit $project_commit = new ProjectCommit(); } else { //if we have, then we'll update it $project_commit = $pc; } $project_commit->project_id = $proj->id; if (!$commit->committer) { Makiavelo::puts("**** No se encontró committer... ******"); Makiavelo::info(print_r($commit, true)); } $project_commit->committer = $commit->committer->login; $project_commit->commit_message = $commit->commit->message; $project_commit->sha = $commit->sha; $project_commit->commit_date = $commit_date; save_project_commit($project_commit); if ($commit_date == $today) { $commits_today++; } } $new_pulls_today = $closed_pulls_today = $merged_pulls_today = 0; foreach ($data->pulls as $pull) { $created_data = explode("T", $pull->created_at); $closed_data = explode("T", $pull->closed_at); $merged_data = explode("T", $pull->merged_at); if ($created_data[0] == $today) { $new_pulls_today++; } if ($closed_data[0] == $today) { $closed_pulls_today++; } if ($merged_data[0] == $today) { $merged_pulls_today++; } } $delta_stars = $data->watchers - $proj->stars; $delta_forks = $data->forks - $proj->forks; //if($delta_stars != 0 || $delta_forks != 0) { Makiavelo::info("==== Delta found, saving..."); //We also update the project $proj->stars = $data->watchers; $proj->forks = $data->forks; save_project($proj); $pd = new ProjectDelta(); $pd->forks = $data->forks; $pd->delta_forks = $delta_forks; $pd->stars = $data->watchers; $pd->delta_stars = $delta_stars; $pd->project_id = $proj->id; $pd->commits_count = $commits_today; $pd->new_pulls = $new_pulls_today; $pd->closed_pulls = $closed_pulls_today; $pd->merged_pulls = $merged_pulls_today; $pd->sample_date = $today; if (save_project_delta($pd)) { Makiavelo::info("===== Delta saved! "); Makiavelo::info(print_r($pd, true)); } else { Makiavelo::info("===== ERROR saving delta"); } //} } }
public function grabHistoricData() { $proj_name = $this->name; $usr_name = $this->owner()->name; Makiavelo::info("==== Querying for {$usr_name}/{$proj_name}"); $g_data = GithubAPI::queryProjectData($usr_name, $proj_name); $this->readme = $g_data->readme; //Makiavelo::puts("Updating project..."); save_project($this); $data = array(); foreach ($g_data->commits as $commit) { $commit_date = $commit->commit->committer->date; $commit_date = explode("T", $commit_date); $commit_date = $commit_date[0]; $date_idx = intval(str_replace("-", "", $commit_date)); if (!isset($data[$date_idx]) || !isset($data[$date_idx]['commits'])) { $data[$date_idx] = array("commits" => 1); } else { $data[$date_idx]['commits']++; } $pc = load_project_commit_where("sha = '" . $commit->sha . "'"); if ($pc == null) { //We make sure we haven't yet saved this commit $project_commit = new ProjectCommit(); $project_commit->project_id = $this->id; $project_commit->committer = $commit->committer->login; $project_commit->commit_message = $commit->commit->message; $project_commit->sha = $commit->sha; $project_commit->commit_date = $commit_date; save_project_commit($project_commit); } } foreach ($g_data->pulls as $pull) { $created_data = explode("T", $pull->created_at); $closed_data = explode("T", $pull->closed_at); $merged_data = explode("T", $pull->merged_at); $created_idx = intval(str_replace("-", "", $created_data[0])); $merged_idx = intval(str_replace("-", "", $merged_data[0])); $closed_idx = intval(str_replace("-", "", $closed_data[0])); if (!isset($data[$created_idx]) || !isset($data[$created_idx]['new_pulls'])) { $data[$created_idx]['new_pulls'] = 1; } else { $data[$created_idx]['new_pulls']++; } if ($merged_idx != 0) { if (!isset($data[$merged_idx]) || !isset($data[$merged_idx]['merged_pulls'])) { $data[$merged_idx]['merged_pulls'] = 1; } else { $data[$merged_idx]['merged_pulls']++; } } if ($closed_idx != 0) { if (!isset($data[$closed_idx]) || !isset($data[$closed_idx]['closed_pulls'])) { $data[$closed_idx]['closed_pulls'] = 1; } else { $data[$closed_idx]['closed_pulls']++; } } } foreach ($data as $date => $stats) { $year = substr($date, 0, 4); $month = substr($date, 4, 2); $day = substr($date, 6, 2); $str_date = $year . "-" . $month . "-" . $day; $pd = new ProjectDelta(); $pd->forks = -99; $pd->delta_forks = -99; $pd->stars = -99; $pd->delta_stars = -99; $pd->project_id = $this->id; $pd->commits_count = isset($stats['commits']) ? $stats['commits'] : 0; $pd->new_pulls = isset($stats['new_pulls']) ? $stats['new_pulls'] : 0; $pd->closed_pulls = isset($stats['closed_pulls']) ? $stats['closed_pulls'] : 0; $pd->merged_pulls = isset($stats['merged_pulls']) ? $stats['merged_pulls'] : 0; $pd->sample_date = $str_date; if (save_project_delta($pd)) { Makiavelo::info("===== Delta saved! "); } else { Makiavelo::info("===== ERROR saving delta::" . mysql_error()); } } foreach ($g_data->open_issues_list as $issue) { $iss = new Issue(); $iss->title = $issue->title; $iss->body = MarkdownExtra::defaultTransform($issue->body); $iss->created_at = $issue->created_at; $iss->updated_at = $issue->updated_at; $iss->url = $issue->html_url; $iss->number = $issue->number; $iss->project_id = $this->id; if (save_issue($iss)) { Makiavelo::info("===== Issue saved! "); } else { Makiavelo::info("===== ERROR saving issue::" . mysql_error()); } } }
/** Retrieves a list of Project @order = Optional, can be an array of keys or just a single key to order by the results @limit = Optional */ function list_project($order = null, $limit = null, $where = null) { global $__db_conn; $sql = "SELECT * FROM project"; if ($where != null) { $sql .= " WHERE {$where} "; } if ($order != null) { $order_str = $order; if (is_array($order)) { $order_str = implode(",", $order); } $sql .= " order by {$order_str}"; } if ($limit != null) { $sql .= " limit {$limit}"; } Makiavelo::info("== Loading projects:: " . $sql); $result = mysql_query($sql, $__db_conn); if (!$result) { Makiavelo::info("ERROR MYSQL: " . mysql_error() . " - " . $sql); return array(); } $results = array(); while ($row = mysql_fetch_assoc($result)) { $tmp = new Project(); $tmp->load_from_array($row); $results[] = $tmp; } return $results; }